Search Unity

OcclusionMap

Discussion in 'Scripting' started by cruising, Feb 20, 2018.

  1. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    Hello!

    Trying to add Occolusion to a doublesided shader.

    Here is what im adding to the script
    Code (csharp):
    1.  
    2. _OcclusionMap("Occlusion", 2D) = "white" {}
    3. sampler2D _OcclusionMap;
    4. fixed occlusion = tex2D(_OcclusionMap, i.uv).r;
    5. c.rgb *= occlusion;
    6.  
    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):
    1.  
    2. Shader "Custom/Two Sided SurfaceShader" {
    3.     Properties {
    4.         _Color ("Color", Color) = (1,1,1,1)
    5.         [NoScaleOffset] _MainTex ("Albedo (RGB)", 2D) = "white" {}
    6.         [NoScaleOffset] _OcclusionMap("Occlusion", 2D) = "white" {} //Added Code
    7.         [Toggle] _UseMetallicMap ("Use Metallic Map", Float) = 0.0
    8.         [NoScaleOffset] _MetallicGlossMap("Metallic", 2D) = "black" {}
    9.         [Gamma] _Metallic ("Metallic", Range(0,1)) = 0.0
    10.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    11.         _BumpScale("Scale", Float) = 1.0
    12.         [NoScaleOffset] _BumpMap("Normal Map", 2D) = "bump" {}
    13.         _Cutoff("Alpha Cutoff", Range(0.01,1)) = 0.5
    14.     }
    15.     SubShader {
    16.         Tags { "Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout" }
    17.         Blend SrcAlpha OneMinusSrcAlpha
    18.         LOD 200
    19.         ZWrite Off
    20.         Cull Off
    21.  
    22.         Pass {
    23.             ColorMask 0
    24.             ZWrite On
    25.  
    26.             CGPROGRAM
    27.             #pragma vertex vert
    28.             #pragma fragment frag
    29.          
    30.             #include "UnityCG.cginc"
    31.  
    32.             struct v2f {
    33.                 float4 vertex : SV_POSITION;
    34.                 float2 texcoord : TEXCOORD0;
    35.             };
    36.  
    37.             sampler2D _MainTex;
    38.             fixed _Cutoff;
    39.  
    40.             v2f vert (appdata_img v)
    41.             {
    42.                 v2f o;
    43.                 o.vertex = UnityObjectToClipPos(v.vertex);
    44.                 o.texcoord = v.texcoord;
    45.                 return o;
    46.             }
    47.  
    48.             fixed4 frag (v2f i) : SV_Target
    49.             {
    50.                 fixed4 col = tex2D(_MainTex, i.texcoord);
    51.                 fixed occlusion = tex2D(_OcclusionMap, i.uv).r; //Added Code
    52.                 clip(col.a - _Cutoff);
    53.                 c.rgb * = occlusion; //Added Code
    54.                 return 0;
    55.             }
    56.             ENDCG
    57.         }
    58.  
    59.         Pass
    60.         {
    61.             Tags {"LightMode"="ShadowCaster"}
    62.             ZWrite On
    63.             Cull Off
    64.  
    65.             CGPROGRAM
    66.             #pragma vertex vert
    67.             #pragma fragment frag
    68.             #pragma multi_compile_shadowcaster
    69.             #include "UnityCG.cginc"
    70.  
    71.             struct v2f {
    72.                 V2F_SHADOW_CASTER;
    73.                 float2 texcoord : TEXCOORD1;
    74.             };
    75.  
    76.             v2f vert(appdata_base v)
    77.             {
    78.                 v2f o;
    79.                 TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
    80.                 o.texcoord = v.texcoord;
    81.                 return o;
    82.             }
    83.          
    84.             sampler2D _MainTex;
    85.             fixed _Cutoff;
    86.  
    87.             float4 frag(v2f i) : SV_Target
    88.             {
    89.                 fixed4 col = tex2D(_MainTex, i.texcoord);
    90.                 clip(col.a - _Cutoff);
    91.                 SHADOW_CASTER_FRAGMENT(i)
    92.             }
    93.             ENDCG
    94.         }
    95.      
    96.         CGPROGRAM
    97.         #pragma surface surf Standard fullforwardshadows alpha:fade nolightmap
    98.         #pragma shader_feature _USEMETALLICMAP_ON
    99.         #pragma target 3.0
    100.  
    101.         sampler2D _MainTex;
    102.         sampler2D _OcclusionMap; //Added Code
    103.         sampler2D _MetallicGlossMap;
    104.         sampler2D _BumpMap;
    105.  
    106.  
    107.         struct Input {
    108.             float2 uv_MainTex;
    109.             fixed facing : VFACE;
    110.         };
    111.  
    112.         half _Glossiness;
    113.         half _Metallic;
    114.         fixed4 _Color;
    115.         half _BumpScale;
    116.         fixed _Cutoff;
    117.  
    118.         void surf (Input IN, inout SurfaceOutputStandard o) {
    119.             // Albedo comes from a texture tinted by color
    120.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    121.             o.Albedo = c.rgb;
    122.  
    123.             #ifdef _USEMETALLICMAP_ON
    124.             fixed4 mg = tex2D(_MetallicGlossMap, IN.uv_MainTex);
    125.             o.Metallic = mg.r;
    126.             o.Smoothness = mg.a;
    127.             #else
    128.             o.Metallic = _Metallic;
    129.             o.Smoothness = _Glossiness;
    130.             #endif
    131.  
    132.             // Rescales the alpha on the blended pass
    133.             o.Alpha = saturate(c.a / _Cutoff);
    134.  
    135.             o.Normal = UnpackScaleNormal(tex2D(_BumpMap, IN.uv_MainTex), _BumpScale);
    136.  
    137.             if (IN.facing < 0.5)
    138.                 o.Normal *= -1.0;
    139.         }
    140.         ENDCG
    141.     }
    142.     FallBack "Diffuse"
    143. }
    144.  
     
  2. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    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)
     
  3. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329

    If i "sampler2D _OcclusionMap" there i getting errors.

    Code (csharp):
    1. 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):
    1. Shader error in 'Custom/Two Sided SurfaceShader': invalid subscript 'uv' at line 53 (on d3d11)
    This is line 53
    Code (csharp):
    1. fixed occlusion = tex2D(_OcclusionMap, i.uv).r;
     
  4. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    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.