Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Standard Surface Shader ambient occlusion uv2 doesn't work in forward rendering

Discussion in 'Shaders' started by StaffanEk, Sep 24, 2016.

  1. StaffanEk

    StaffanEk

    Joined:
    Jul 13, 2012
    Posts:
    380
    Hi,

    I added occlusion to the standard shader using uv2 and it works perfectly in deferred mode. If I use forward rendering, occlusion is completely missing. I used the Create/Shader/Standard Surface Shader dropdown and added a very simple small modification.

    Code (csharp):
    1.  
    2. Shader "Custom/StandardUV2Occlusion" {
    3.    Properties {
    4.      _Color ("Color", Color) = (1,1,1,1)
    5.      _MainTex ("Albedo (RGB)", 2D) = "white" {}
    6.      _OcclusionMap("Occlusion", 2D) = "white" {} // Occlusion texture
    7.      _Glossiness ("Smoothness", Range(0,1)) = 0.5
    8.      _Metallic ("Metallic", Range(0,1)) = 0.0
    9.      _OcclusionStrength("Occlusion intensity", Range(0,1)) = 1.0 // Occlusion intensity
    10.    }
    11.    SubShader {
    12.      Tags { "RenderType"="Opaque" }
    13.      LOD 200
    14.      
    15.      CGPROGRAM
    16.      // Physically based Standard lighting model, and enable shadows on all light types
    17.      #pragma surface surf Standard fullforwardshadows
    18.  
    19.      // Use shader model 3.0 target, to get nicer looking lighting
    20.      #pragma target 3.0
    21.  
    22.      sampler2D _MainTex, _OcclusionMap;
    23.  
    24.      struct Input {
    25.        float2 uv_MainTex;
    26.        float2 uv2_OcclusionMap; // use the second uv set
    27.      };
    28.  
    29.      half _Glossiness;
    30.      half _Metallic;
    31.      half _OcclusionStrength;
    32.      fixed4 _Color;
    33.  
    34.      void surf (Input IN, inout SurfaceOutputStandard o) {
    35.        // Albedo comes from a texture tinted by color
    36.        fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    37.        o.Albedo = c.rgb;
    38.        // Metallic and smoothness come from slider variables
    39.        o.Metallic = _Metallic;
    40.        o.Smoothness = _Glossiness;
    41.        o.Alpha = c.a;
    42.        o.Occlusion = lerp(1.0, tex2D(_OcclusionMap, IN.uv2_OcclusionMap), _OcclusionStrength);
    43. // interpolate the multiplication of occlusion via OcclusionStrength
    44.      }
    45.      ENDCG
    46.    }
    47.    FallBack "Diffuse"
    48. }
    49.  
    The manual has no mention of uv2 only working in deferred, so I assume I made a mistake somewhere. If anyone can help me I would be very grateful.