Add fresnel effect to fragment shader

Hi there, I am trying to add a fresnel/rim effect to a fragment shader, but am encountering some issues in getting the correct rim color to apply.

fixed4 frag (v2f i) : SV_Target
            {
                // Sample texture
                half4 col = tex2D(_MainTex, i.uv) * _Tint; // Texture times tint color;

                // View direction of camera
                float3 viewDir = normalize(_WorldSpaceCameraPos - i.worldPos);
                half rim = 1.0 - saturate(dot(normalize(viewDir), i.normal));
                half4 newCol = half4(col * _RimColor);
                newCol.a = col.a;
                newCol *= pow(rim, _RimPower);
                col = newCol;

                return col;
            }

This snippet seems to apply the fresnel effect correctly, but the rim color isn’t displaying correctly. The rim color appears as black in-game regardless of what color I set it to in the inspector. Does anyone have any insight as to what I’m doing wrong here? Thanks!

What exactly are you expecting? You’re multiplying your texture by the tint color, the rim color, and by the inverted dot product. That’s a lot of multiplying.

I suspect you really want a lerp in there someplace, but it depends on what the final look you’re going for is.

half rim = 1.0 - saturate(dot(normalize(viewDir), i.normal));
col.rgb = lerp(col.rgb, col.rgb * _RimColor.rgb, pow(rim, _RimPower));

1 Like

Thanks, this is indeed the look I was trying to get.