Search Unity

Enhanced Cross Section Shader

Discussion in 'Shaders' started by Qwertzus, Nov 26, 2014.

  1. Qwertzus

    Qwertzus

    Joined:
    Jul 4, 2013
    Posts:
    1
    Hi,

    I need your help for building a cross section shader and have two different issues. I hope you can help me!
    First of all: Although I have some experience with shaders in unity, I'm still a beginner.

    Here is my basic (and working) cross section shader:

    https://drive.google.com/file/d/0B5GmHCLEk27GSHFpNlRfWmpzQWM/view?usp=sharing

    Code (CSharp):
    1.  
    2. Shader "Slice/Slice1"
    3.     {
    4.         Properties
    5.         {
    6.           _MainTex ("Texture", 2D) = "white" {}
    7.           _section ("Section plane (x angle, y angle, z angle, w displacement)", vector) = (0,0,0,0.15)
    8.         }
    9.    
    10.         SubShader
    11.         {
    12.           Tags { "RenderType" = "Opaque" "Queue"="Geometry" "IgnoreProjector"="True"}
    13.           Cull Off
    14.    
    15.           CGPROGRAM
    16.           #pragma surface surf Lambert
    17.  
    18.           struct Input
    19.           {
    20.               float2 uv_MainTex;
    21.               float3 worldPos;
    22.               float3 viewDir;
    23.               float3 worldNormal;
    24.           };
    25.  
    26.           sampler2D _MainTex;
    27.           float4 _section;
    28.      
    29.           void surf (Input IN, inout SurfaceOutput o)
    30.           {
    31.             float toClip = _section.x * 0.1 * IN.worldPos.x +
    32.                            _section.y * 0.1 * IN.worldPos.y +
    33.                            _section.z * 0.1 * IN.worldPos.z +
    34.                            _section.w;
    35.                        
    36.             clip( toClip);
    37.          
    38.             float fd = dot( IN.viewDir, IN.worldNormal);
    39.  
    40.             if (fd.x > 0)
    41.             {
    42.                 o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
    43.                 return;
    44.             }
    45.        
    46.             o.Emission = tex2D (_MainTex, IN.uv_MainTex).rgb;
    47.           }
    48.           ENDCG
    49.         }
    50.         Fallback "Diffuse"
    51.      }
    52.  

    My next step is to implement some normal map functionality.
    I tried this:
    Code (CSharp):
    1.     Shader "Slice/Slice1"
    2.     {
    3.         Properties
    4.         {
    5.           _MainTex ("Texture", 2D) = "white" {}
    6.           _BumpMap ("Bumpmap", 2D) = "bump" {}
    7.           _section ("Section plane (x angle, y angle, z angle, w displacement)", vector) = (0,0,0,0.15)
    8.         }
    9.    
    10.         SubShader
    11.         {
    12.           Tags { "RenderType" = "Opaque" "Queue"="Geometry" "IgnoreProjector"="True"}
    13.           Cull Off
    14.    
    15.           CGPROGRAM
    16.           #pragma surface surf Lambert
    17.  
    18.           struct Input
    19.           {
    20.               float2 uv_MainTex;
    21.               float3 worldPos;
    22.               float3 viewDir;
    23.               float3 worldNormal;
    24.           };
    25.  
    26.           sampler2D _MainTex;
    27.           sampler2D _BumpMap;
    28.           float4 _section;
    29.      
    30.           void surf (Input IN, inout SurfaceOutput o)
    31.           {
    32.             float toClip = _section.x * 0.1 * IN.worldPos.x +
    33.                            _section.y * 0.1 * IN.worldPos.y +
    34.                            _section.z * 0.1 * IN.worldPos.z +
    35.                            _section.w;
    36.                        
    37.             clip( toClip);
    38.          
    39.             float fd = dot( IN.viewDir, IN.worldNormal);
    40.  
    41.             if (fd.x > 0)
    42.             {
    43.                 o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
    44.                 o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_MainTex));
    45.                 return;
    46.             }
    47.        
    48.             o.Emission = tex2D (_MainTex, IN.uv_MainTex).rgb;
    49.           }
    50.           ENDCG
    51.         }
    52.         Fallback "Diffuse"
    53.      }
    54.  

    But I get the following error: "... Too many texture interpolators would be used for ForwardBase pass at line..."

    I also tried to add " #pragma target 3.0 ", but then I get another error which doesn't really tell me where the problem is : " ... invalid subscript 'TtoW0' at line 145 ..."

    What is the problem there? :(

    My next problem is, that I want to cap the holes.
    My idea is using Stencil Buffers and create a mask from the Back and Front Cull to get a mask from the "seams" and finally take a plane and control its visibilty by the mask I have created...
    But the problem I have is, that I don't even know how to get started...
    Does someone has an idea?

    big thanks to you in advance!
     
    Last edited: Nov 28, 2014