Search Unity

Glass shader unlit - why?

Discussion in 'Shaders' started by tosi, Jul 18, 2013.

  1. tosi

    tosi

    Joined:
    Jul 5, 2012
    Posts:
    45
    Hi there,

    I have been working on a glass shader the last few days and did some progress. Unfortunately, I don't see any specular highlights on my glass. Can anyone please explain this to me? I expect the right cup having the same highlight as the left cup. Specular and Gloss are hardcoded to 1 in my surface shader.

    $glass.png

    Code (csharp):
    1. Shader "StrumpyGlas2"
    2. {
    3.     Properties
    4.     {
    5.         _Color("Main Color", Color) = (0.3529412,0.3529412,0.3529412,1)
    6.         _GlossNoYes("_GlossNoYes", Range(0,1) ) = 0
    7.         _Spraying("Base (RGB) Gloss (A)", 2D) = "white" {}
    8.         _Metalization("_Metalization", 2D) = "black" {}
    9.         _Painting("_Painting", 2D) = "black" {}
    10.         _UVOffset("_UVOffset", Range(0,1) ) = 0.5
    11.         _Reflection("_Reflection", Cube) = "black" {}
    12.         _BumpMap("Normalmap", 2D) = "bump" {}
    13.         _Distorsion("_Distorsion", Range(0,0.1) ) = 0
    14.         _Refraction("_Refraction", Range(0,1) ) = 0
    15.         _Shininess("Shininess", Range(0.01,1) ) = 1
    16.         _FresnelPower("_Fresnel Power", Range(0.1,3) ) = 0.45
    17.    
    18.     }
    19.  
    20.    
    21.     SubShader
    22.     {
    23.        
    24.         Tags
    25.         {
    26.             "Queue"="Transparent"
    27.             "IgnoreProjector"="False"
    28.             "RenderType"="Transparent"
    29.         }
    30.  
    31.        
    32.        
    33.         //--------------------------1---------------------- Original-Hintergrund als Textur sichern
    34.         GrabPass { "_GrabTexture1" }
    35.  
    36.  
    37.  
    38.         //--------------------------2---------------------- Bereich des Objektes schwarz färben
    39.         //------------------------------------------------- Grundlage für den nächsten Pass, der additiv arbeitet - sonst würde alles überstrahlt werden
    40.         Blend One Zero
    41.         ZWrite Off
    42.         Lighting Off
    43.        
    44.         CGPROGRAM
    45.         #pragma surface surf Lambert
    46.        
    47.         struct Input
    48.         {
    49.             float4 color : COLOR;
    50.         };
    51.        
    52.         void surf( Input IN, inout SurfaceOutput o )
    53.         {
    54.             o.Albedo = 0;
    55.         }
    56.        
    57.         ENDCG
    58.  
    59.  
    60.  
    61.         //--------------------------3---------------------- Transparenz wird mit GrabTexture #1 simuliert
    62.         //------------------------------------------------- Refraction wird errechnet aus der Oberflächen-Normale (Richtung der Brechung)
    63.         ZWrite Off
    64.         Lighting On
    65.        
    66.         CGPROGRAM
    67.         #pragma surface surf Lambert
    68.        
    69.         struct Input
    70.         {
    71.             float4 screenPos;
    72.             float3 viewDir;
    73.             float3 worldNormal;
    74.         };
    75.        
    76.         sampler2D _GrabTexture1;
    77.         float _Refraction;
    78.        
    79.         void surf( Input IN, inout SurfaceOutput o )
    80.         {
    81.             float3 obj = mul( (float3x3)_Object2World, float3( 0,0,0 ) );
    82.             float3 view = float3( IN.viewDir.x, 0, IN.viewDir.z );
    83.             float3 middle = obj + normalize( IN.viewDir );
    84.             float3 offsetVector = obj + normalize( IN.worldNormal );
    85.             float deltaX = max( offsetVector.x - middle.x, offsetVector.z - middle.z );
    86.             float deltaY = ( offsetVector.y - middle.y );
    87.            
    88.             float2 screenPosition = ( IN.screenPos.xy / IN.screenPos.w ).xy - float2( deltaX, deltaY ) * pow( _Refraction, 3 );// * ( 1 - glassDepth );
    89.             float3 bgColor = tex2D( _GrabTexture1, screenPosition ).rgb;
    90.            
    91.             o.Albedo = 0;
    92.             o.Emission = bgColor;
    93.         }
    94.        
    95.         ENDCG
    96.        
    97.    
    98.        
    99.         //--------------------------4---------------------- Blende Farbe (Spraying), Transparenz (Metalization) und Painting drüber
    100.         //------------------------------------------------- Hier wird in den Z-Buffer geschrieben und sicher gestellt, das undurchsichtige Bereiche auch undurchsichtig und im Vordergrund angezeigt werden
    101.         Blend SrcAlpha OneMinusSrcAlpha
    102.         ZWrite On
    103.         Lighting On
    104.        
    105.         CGPROGRAM
    106.         #pragma surface surf BlinnPhong
    107.         #pragma target 3.0
    108.        
    109.         struct Input
    110.         {
    111.             float2 uv_Spraying;
    112.             float3 viewDir;
    113.             float2 uv_BumpMap;
    114.             float2 uv_Metalization;
    115.             float3 worldNormal;
    116.             INTERNAL_DATA
    117.         };
    118.        
    119.         float4 _Color;
    120.         sampler2D _Spraying;
    121.         samplerCUBE _Reflection;
    122.         sampler2D _BumpMap;
    123.         float _FresnelPower;
    124.         float _Shininess;
    125.         float _GlossNoYes;
    126.         sampler2D _Metalization;
    127.        
    128.         void surf( Input IN, inout SurfaceOutput o )
    129.         {
    130.             float3 sprayColor = tex2D( _Spraying, IN.uv_Spraying );
    131.             float3 metal = tex2D( _Metalization, IN.uv_Metalization ).rgb;
    132.            
    133.             float3 n = UnpackNormal( tex2D( _BumpMap, IN.uv_BumpMap ) );
    134.            
    135.             o.Normal = float3( 0, 0, 1 ); // wird hart gesetzt für Fresnel-Effekt
    136.            
    137.             float fresnel = ( 1.0 - dot( normalize( IN.viewDir ), float3( 0, 0, 1 ) ) );
    138.             float fresnelPower = max( 0.2, pow( fresnel, _FresnelPower ) );
    139.             float3 reflectionVector = -reflect( IN.viewDir, IN.worldNormal );
    140.             float3 reflectionColor = texCUBE( _Reflection, reflectionVector ).rgb;
    141.  
    142.             o.Emission = lerp( sprayColor, reflectionColor, fresnelPower );
    143.             o.Alpha = max( fresnelPower, max( metal.r, _Color.a ) );
    144.            
    145. //          o.Specular = _Shininess;
    146. //          o.Gloss = sprayColor * _GlossNoYes.xxxx;
    147.             o.Specular = 1;
    148.             o.Gloss = 1;
    149.                 }
    150.        
    151.         ENDCG
    152.     }
    153.    
    154.    
    155. //  Fallback "Transparent"
    156.    
    157.    
    158. }