Search Unity

Unity Not Rendering Extruded Verts

Discussion in 'Shaders' started by Silent8Strike, Sep 19, 2014.

  1. Silent8Strike

    Silent8Strike

    Joined:
    Mar 10, 2014
    Posts:
    29
    I'm new to shaders and I've been working on porting the planet shader from the Wiki to a surface shader. I've managed to port the first pass, the rim lighting on the planet, but the second pass, the actual atmosphere is giving me trouble.

    I'm extruding the verts of the sphere, then rendering it with transparency based on lighting a view direction, but my issue is it will not render the extruded planet. Even if I set culling to back, and set the alpha to 1, the surface of the non extruded planet is affected by the second pass, but its obvious the planet does not change size. One thing I did notice however, is that the extruded planet shows up in the material preview window, albet with the atmosphere on the wrong side.

    Is this a shader code issue, or a Unity one? Any help would be much appreciated.

    Here is my current shader code:
    Code (CSharp):
    1.  
    2. Shader "Custom/PlanetSurface"
    3. {
    4.     Properties
    5.     {
    6.         _MainTex("Texture (RGB)", 2D) = "black" {}
    7.         _Color("Color", Color) = (0, 0, 0, 1)
    8.         _AtmoColor("Atmosphere Color", Color) = (0.5, 0.5, 1.0, 1)
    9.         _Size("Size", Float) = 0.1
    10.         _Falloff("Falloff", Float) = 5
    11.         _FalloffPlanet("Falloff Planet", Float) = 5
    12.         _Transparency("Transparency", Float) = 15
    13.         _TransparencyPlanet("Transparency Planet", Float) = 1
    14.     }
    15.    
    16.     SubShader
    17.     {
    18.         Tags { "RenderType"="Opaque" }
    19.         LOD 200
    20.        
    21.         //1st pass
    22.         CGPROGRAM
    23.         #pragma surface surf Lambert
    24.  
    25.         sampler2D _MainTex;
    26.         float _FalloffPlanet;
    27.         float _TransparencyPlanet;
    28.         fixed4 _Color;
    29.         fixed4 _AtmoColor;
    30.  
    31.         struct Input
    32.         {
    33.             float2 uv_MainTex;
    34.             float3 viewDir;
    35.         };
    36.  
    37.         void surf (Input IN, inout SurfaceOutput o)
    38.         {
    39.             float4 atmo = _AtmoColor;
    40.            
    41.             atmo.a = pow(saturate(1.0 - dot(normalize(IN.viewDir), o.Normal)), _FalloffPlanet);
    42.             atmo.a *= _TransparencyPlanet * _Color;
    43.                        
    44.             fixed4 color = tex2D(_MainTex, IN.uv_MainTex)*_Color;  //Get color from texture
    45.             color.rgb = lerp(color.rgb, atmo.rgb, atmo.a);  //Glow on planet amount fade
    46.                                
    47.             o.Albedo = color * dot(_WorldSpaceLightPos0, o.Normal); //Darken effect
    48.             o.Alpha = color.a;
    49.         }
    50.         ENDCG
    51.        
    52.         //Second pass
    53.         Tags {"Queue"="Transparent" "RenderType"="Transparent" }
    54.        
    55.         Cull Front
    56.         Blend SrcAlpha One
    57.         ZWrite Off
    58.        
    59.         CGPROGRAM
    60.         #pragma surface surf Lambert vertex:vert
    61.        
    62.         float4 _Color;
    63.         float4 _AtmoColor;
    64.         float _Size;
    65.         float _Falloff;
    66.         float _Transparency;      
    67.        
    68.         struct Input
    69.         {
    70.             float3 viewDir;
    71.         };
    72.        
    73.         void vert (inout appdata_full v)
    74.         {
    75.             v.vertex.xyz += v.normal *_Size;
    76.         }
    77.        
    78.         void surf (Input IN, inout SurfaceOutput o)
    79.         {
    80.             float4 color = _AtmoColor;
    81.             color.a = dot(normalize(IN.viewDir), o.Normal);
    82.             color.a *= dot(o.Normal, _WorldSpaceLightPos0);
    83.             color.a = saturate(color.a);
    84.             color.a = pow(color.a, _Falloff);
    85.             color.a *= _Transparency;
    86.            
    87.             o.Albedo = color.rgb;
    88.             o.Alpha = color.a;
    89.         }      
    90.         ENDCG
    91.        
    92.     }
    93.     FallBack "Diffuse"
    94. }
    95.  
     
  2. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Your problem is, you are trying to use two cgprogram blocks at once, which won't work. try moving the vertex shader bit into the first block, and see what happens.

    and if you want to make two-pass shader, I suggest using Vertex/Fragment shader, as surface shader makes the passes itself, leaving no real control over the rendering order.
     
  3. Silent8Strike

    Silent8Strike

    Joined:
    Mar 10, 2014
    Posts:
    29
    Thanks for the reply. From what I could find out, when you have two CGPROGRAMs in one shader, Unity will combine them when compiling. However, I think your correct in using a Vert/Frag shader as it seems much more suited to the task, and I would have more control over the passes.
     
  4. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Good to see my advice helped! If you wish, I can help further by hard-coding forward base and additive passes for you! :)
    Also, if I do so, then we would put the vertex program at the end of the passes, so it's rendered last. is this right? :D
     
  5. Silent8Strike

    Silent8Strike

    Joined:
    Mar 10, 2014
    Posts:
    29
    Thanks a lot for the offer, but the shader I was working to port was written exactly as you suggested, which makes sense now.

    Here is a link to the original::http://wiki.unity3d.com/index.php?title=Planet

    I was mostly porting it to try and learn more about Unity shaders, and I definitely did.
     
  6. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    @Silent8Strike: Good work man, that's the spirit!

    As for how I learned shaders... believe it or not, I learned through trial and error! :D