Search Unity

Custom lighting model, want to use SurfaceOutputStandardSpecular

Discussion in 'Shaders' started by Gorion, May 3, 2017.

  1. Gorion

    Gorion

    Joined:
    Jun 13, 2014
    Posts:
    6
    I'm writing a custom surface shader for our game, and I want to work with multiple passes. This was easy enough, just add multiple CGPROGRAM to the shader. However in the 2nd pass I wanted to redefine the lighting model, however, this seems to break the multipass functionality.

    Is there a way for me to reuse the SurfaceOutputStandardSpecular instead of using the old (without specular) SurfaceOutput struct instead?
     
  2. Gorion

    Gorion

    Joined:
    Jun 13, 2014
    Posts:
    6
    Some extra information, these are the errors unity giving me:


    Shader error in 'Totems/UberTotem': Unexpected identifier "SurfaceOutputStandardSpecular". Expected one of: sampler sampler1D sampler2D sampler3D samplerCUBE sampler_state SamplerState SamplerComparisonState bool int uint half float double or a user-defined type at line 90

    Shader error in 'Totems/UberTotem': Unexpected identifier "SurfaceOutputStandardSpecular". Expected: ')' at line 109

    The shader looks like following:

    Code (CSharp):
    1.                
    2. Shader "Totems/UberTotem" {
    3.    Properties
    4.    {
    5.        _Color ("Color", Color) = (1,1,1,1)
    6.        _MainTex ("Albedo (RGB)", 2D) = "white" {}
    7.  
    8.        _BumpMap("Normal Map", 2D) = "bump" {}
    9.  
    10.        _SpecGlossMap("Specular", 2D) = "white" {}
    11.        _Glossiness("Smoothness", Range(0, 1)) = 1
    12.  
    13.        _EmissionMap("Emission", 2D) = "white" {}
    14.    }
    15.    SubShader
    16.    {
    17.        Tags { "Queue" = "Geometry+1" "RenderType" = "Opaque" }
    18.        LOD 200
    19.  
    20.        
    21.        CGPROGRAM
    22.         // Physically based Standard lighting model, and enable shadows on all light types
    23.         #pragma surface surf StandardSpecular fullforwardshadows
    24.  
    25.         // Use shader model 3.0 target, to get nicer looking lighting
    26.         #pragma target 3.0
    27.  
    28.         sampler2D _MainTex;
    29.         sampler2D _SpecGlossMap;
    30.         sampler2D _BumpMap;
    31.         sampler2D _EmissionMap;
    32.  
    33.         fixed4 _Color;
    34.         half _Glossiness;
    35.  
    36.         struct Input
    37.         {
    38.             float2 uv_MainTex;
    39.         };
    40.                
    41.        
    42.         //struct SurfaceOutputStandardSpecular
    43.         //{
    44.         //    fixed3 Albedo;      // diffuse color
    45.         //    fixed3 Specular;    // specular color
    46.         //    fixed3 Normal;      // tangent space normal, if written
    47.         //    half3 Emission;
    48.         //    half Smoothness;    // 0=rough, 1=smooth
    49.         //    half Occlusion;     // occlusion (default 1)
    50.         //    fixed Alpha;        // alpha for transparencies
    51.         //};
    52.  
    53.         void surf(Input IN, inout SurfaceOutputStandardSpecular o)
    54.         {
    55.             fixed4 albedo = tex2D(_MainTex, IN.uv_MainTex);
    56.             fixed4 specular = tex2D(_SpecGlossMap, IN.uv_MainTex);
    57.             fixed4 normal = tex2D(_BumpMap, IN.uv_MainTex);
    58.             half3 emission = tex2D(_EmissionMap, IN.uv_MainTex).rgb;
    59.  
    60.             o.Albedo = albedo.rgb * _Color.rgb;
    61.             o.Specular = specular.rgb * _Glossiness;
    62.             o.Emission = emission;
    63.             o.Smoothness = specular.a;
    64.             o.Normal = UnpackNormal(normal);
    65.             o.Alpha = albedo.a * _Color.a;
    66.             o.Occlusion = 0;
    67.         }
    68.         ENDCG
    69.  
    70.        //Cull Front
    71.        CGPROGRAM
    72.        // Physically based Standard lighting model, and enable shadows on all light types
    73.        #pragma surface surf CustomSpecular vertex:vert
    74.  
    75.        // Use shader model 3.0 target, to get nicer looking lighting
    76.        #pragma target 3.0
    77.  
    78.        sampler2D _MainTex;
    79.        sampler2D _SpecGlossMap;
    80.        sampler2D _BumpMap;
    81.        sampler2D _EmissionMap;
    82.  
    83.        fixed4 _Color;
    84.        half _Glossiness;
    85.  
    86.        struct Input
    87.        {
    88.            float2 uv_MainTex;
    89.        };
    90.  
    91.        inline half4 LightingCustomSpecular(inout SurfaceOutputStandardSpecular s, half3 viewDir, UnityGI gi)
    92.        {
    93.            s.Normal = normalize(s.Normal);
    94.            
    95.            // energy conservation
    96.            half oneMinusReflectivity;
    97.            s.Albedo = EnergyConservationBetweenDiffuseAndSpecular(s.Albedo, s.Specular, /*out*/ oneMinusReflectivity);
    98.  
    99.            // shader relies on pre-multiply alpha-blend (_SrcBlend = One, _DstBlend = OneMinusSrcAlpha)
    100.            // this is necessary to handle transparency in physically correct way - only diffuse component gets affected by alpha
    101.            half outputAlpha;
    102.            s.Albedo = PreMultiplyAlpha(s.Albedo, s.Alpha, oneMinusReflectivity, /*out*/ outputAlpha);
    103.  
    104.            half4 c = UNITY_BRDF_PBS(s.Albedo, s.Specular, oneMinusReflectivity, s.Smoothness, s.Normal, viewDir, gi.light, gi.indirect);
    105.            c.rgb += UNITY_BRDF_GI(s.Albedo, s.Specular, oneMinusReflectivity, s.Smoothness, s.Normal, viewDir, s.Occlusion, gi);
    106.            c.a = outputAlpha;
    107.            return c;
    108.        }
    109.  
    110.        inline void LightingCustomSpecular_GI(SurfaceOutputStandardSpecular s, UnityGIInput data, inout UnityGI gi)
    111.        {
    112.            gi = UnityGlobalIllumination(data, s.Occlusion, s.Smoothness, s.Normal);
    113.        }
    114.  
    115.        /*half4 LightingWrapLambert(inout SurfaceOutput s, half3 lightDir, half atten)
    116.         {
    117.             return half4(1, 1, 1, 1);
    118.         }*/
    119.  
    120.        /*
    121.         float4 vertex : POSITION;
    122.         float4 tangent : TANGENT;
    123.         float3 normal : NORMAL;
    124.         float4 texcoord : TEXCOORD0;
    125.         float4 texcoord1 : TEXCOORD1;
    126.         float4 texcoord2 : TEXCOORD2;
    127.         float4 texcoord3 : TEXCOORD3;
    128.         fixed4 color : COLOR;
    129.         UNITY_VERTEX_INPUT_INSTANCE_ID
    130.         */
    131.  
    132.        void vert(inout appdata_full v)
    133.        {
    134.            v.vertex.xyz += v.normal * 0.1;
    135.        }
    136.  
    137.        /*
    138.         struct SurfaceOutputStandardSpecular
    139.         {
    140.         fixed3 Albedo;      // diffuse color
    141.         fixed3 Specular;    // specular color
    142.         fixed3 Normal;      // tangent space normal, if written
    143.         half3 Emission;
    144.         half Smoothness;    // 0=rough, 1=smooth
    145.         half Occlusion;     // occlusion (default 1)
    146.         fixed Alpha;        // alpha for transparencies
    147.         }; */
    148.  
    149.        void surf(Input IN, inout SurfaceOutputStandardSpecular o)
    150.        {
    151.            fixed4 albedo = tex2D(_MainTex, IN.uv_MainTex);
    152.            fixed4 specular = tex2D(_SpecGlossMap, IN.uv_MainTex);
    153.            fixed4 normal = tex2D(_BumpMap, IN.uv_MainTex);
    154.            half3 emission = tex2D(_EmissionMap, IN.uv_MainTex).rgb;
    155.  
    156.  
    157.            o.Albedo = albedo.rgb * _Color.rgb;
    158.            o.Specular = specular.rgb * _Glossiness;
    159.            o.Emission = emission;
    160.            o.Normal = UnpackNormal(normal);
    161.            o.Alpha = albedo.a * _Color.a;
    162.  
    163.        }
    164.        ENDCG
    165.        
    166.  
    167.      
    168.    }
    169.    FallBack "Diffuse"
    170. }
    171.  
    172.