Hello! Trying to add Occolusion to a doublesided shader. Here is what im adding to the script Code (csharp): _OcclusionMap("Occlusion", 2D) = "white" {} sampler2D _OcclusionMap; fixed occlusion = tex2D(_OcclusionMap, i.uv).r; c.rgb *= occlusion; But i getting a error "undeclared identifier '_OcclusionMap' at line 52 (on d3d11)" And makes the mesh pink Is there a better way to add Occolusion? Here is the full code with Occolusion added, the lines are marked "//Added Code" at the right side Code (csharp): Shader "Custom/Two Sided SurfaceShader" { Properties { _Color ("Color", Color) = (1,1,1,1) [NoScaleOffset] _MainTex ("Albedo (RGB)", 2D) = "white" {} [NoScaleOffset] _OcclusionMap("Occlusion", 2D) = "white" {} //Added Code [Toggle] _UseMetallicMap ("Use Metallic Map", Float) = 0.0 [NoScaleOffset] _MetallicGlossMap("Metallic", 2D) = "black" {} [Gamma] _Metallic ("Metallic", Range(0,1)) = 0.0 _Glossiness ("Smoothness", Range(0,1)) = 0.5 _BumpScale("Scale", Float) = 1.0 [NoScaleOffset] _BumpMap("Normal Map", 2D) = "bump" {} _Cutoff("Alpha Cutoff", Range(0.01,1)) = 0.5 } SubShader { Tags { "Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout" } Blend SrcAlpha OneMinusSrcAlpha LOD 200 ZWrite Off Cull Off Pass { ColorMask 0 ZWrite On CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct v2f { float4 vertex : SV_POSITION; float2 texcoord : TEXCOORD0; }; sampler2D _MainTex; fixed _Cutoff; v2f vert (appdata_img v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.texcoord = v.texcoord; return o; } fixed4 frag (v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.texcoord); fixed occlusion = tex2D(_OcclusionMap, i.uv).r; //Added Code clip(col.a - _Cutoff); c.rgb * = occlusion; //Added Code return 0; } ENDCG } Pass { Tags {"LightMode"="ShadowCaster"} ZWrite On Cull Off CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_shadowcaster #include "UnityCG.cginc" struct v2f { V2F_SHADOW_CASTER; float2 texcoord : TEXCOORD1; }; v2f vert(appdata_base v) { v2f o; TRANSFER_SHADOW_CASTER_NORMALOFFSET(o) o.texcoord = v.texcoord; return o; } sampler2D _MainTex; fixed _Cutoff; float4 frag(v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.texcoord); clip(col.a - _Cutoff); SHADOW_CASTER_FRAGMENT(i) } ENDCG } CGPROGRAM #pragma surface surf Standard fullforwardshadows alpha:fade nolightmap #pragma shader_feature _USEMETALLICMAP_ON #pragma target 3.0 sampler2D _MainTex; sampler2D _OcclusionMap; //Added Code sampler2D _MetallicGlossMap; sampler2D _BumpMap; struct Input { float2 uv_MainTex; fixed facing : VFACE; }; half _Glossiness; half _Metallic; fixed4 _Color; half _BumpScale; fixed _Cutoff; void surf (Input IN, inout SurfaceOutputStandard o) { // Albedo comes from a texture tinted by color fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color; o.Albedo = c.rgb; #ifdef _USEMETALLICMAP_ON fixed4 mg = tex2D(_MetallicGlossMap, IN.uv_MainTex); o.Metallic = mg.r; o.Smoothness = mg.a; #else o.Metallic = _Metallic; o.Smoothness = _Glossiness; #endif // Rescales the alpha on the blended pass o.Alpha = saturate(c.a / _Cutoff); o.Normal = UnpackScaleNormal(tex2D(_BumpMap, IN.uv_MainTex), _BumpScale); if (IN.facing < 0.5) o.Normal *= -1.0; } ENDCG } FallBack "Diffuse" }
I'm just guessing as I'm not super experienced with ShaderLab, but it looks like you're missing a "sampler2D _OcclusionMap" in the first pass block. Have a look at line 37 in your pasted code, and observe that _MainTex is defined there, but not the occlusion map. Yet you're trying to use it on line 51 (or 52 since that's what the error says)
If i "sampler2D _OcclusionMap" there i getting errors. Code (csharp): Shader error in 'Custom/Two Sided SurfaceShader': 'tex2D': no matching 2 parameter intrinsic function; Possible intrinsic functions are: tex2D(sampler2D, float2|half2|min10float2|min16float2) tex2D(sampler2D, float2|half2|min10float2|min16float2, float2|half2|min10float2|min16float2, float2|half2|min10float2|min16float2) at line 53 (on d3d11) And this Code (csharp): Shader error in 'Custom/Two Sided SurfaceShader': invalid subscript 'uv' at line 53 (on d3d11) This is line 53 Code (csharp): fixed occlusion = tex2D(_OcclusionMap, i.uv).r;
It looks like that first error was caused by the second error. It says "invalid subscript 'uv'", which is because your v2f struct doesn't contain a uv field. You may need to add that.