Question by
x2stone · Jun 02, 2021 at 07:37 AM ·
shadershader programmingshader writingsurface shader
Custom Surface shader don't work with fresnel reflection and normal map at the same time
Hi Unity users,
As the Standard shader don't have the fresnel reflection parameter exposed, I'm trying to write my own custom surface shader with "classic" parameters and a fresnel exponent which control the reflection amount on glazing angles. I succeeded to have a working shader with that fresnel reflection, but the moment I add the normal map parameter, it doesn't work anymore... Any idea ? Of course I read the Unity documentation on Custom Shaders but they use the emissive slot to mimic reflected material and I can't use that method because I will also need to keep that parameter, I really need to deal with metallic/smoothness parameters. Thanks a lot for any help. Here is my shader code :
Shader "MyShaders/StandardFresnel" {
Properties {
_Color ("Tint", Color) = (0, 0, 0, 1)
_Diffuse ("Diffuse", 2D) = "white" {}
_Smoothness ("Smoothness", Range(0, 1)) = 0
_Metallic ("Metalness", Range(0, 1)) = 0
_Normal ("Normal", 2D) = "bump" {}
[HDR] _Emission ("Emission", color) = (0,0,0)
[PowerSlider(4)] _FresnelExponent ("Fresnel Exponent", Range(0.25, 4)) = 1
}
SubShader {
Tags{ "RenderType"="Opaque" "Queue"="Geometry"}
CGPROGRAM
#pragma surface surf Standard fullforwardshadows
#pragma target 3.0
sampler2D _Diffuse;
sampler2D _Normal;
fixed4 _Color;
half _Smoothness;
half _Metallic;
half3 _Emission;
float _FresnelExponent;
struct Input {
float2 uv_MainTex;
float2 uv_Normal;
float3 worldNormal;
float3 viewDir;
INTERNAL_DATA
};
void surf (Input i, inout SurfaceOutputStandard o) {
fixed4 col = tex2D(_Diffuse, i.uv_MainTex);
fixed4 normal = tex2D (_Normal, i.uv_Normal);
col *= _Color;
o.Albedo = col.rgb;
// Fresnel stuff
float fresnel = dot(i.worldNormal, i.viewDir);
fresnel = saturate(1 - fresnel);
fresnel = pow(fresnel, _FresnelExponent);
o.Metallic = _Metallic * fresnel;
o.Smoothness = _Smoothness;
// When I add the above line, fesnel parameter don't work anymore...
o.Normal = UnpackNormal (normal);
}
ENDCG
}
FallBack "Standard"
}
Comment