Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

cutaway shader using discard

Discussion in 'Shaders' started by aku286, Feb 19, 2015.

  1. aku286

    aku286

    Joined:
    Feb 1, 2014
    Posts:
    5
    Hi, i tried to write a cutaway shader to get the effect here http://gm-cdn.gamezebo.netdna-cdn.com/wp-content/uploads/2015/02/overland-windmill-600x4201.jpg, i attatched the result i got.

    i result i got does not render inside faces as you can see(like a cross section), also i want to use a solid color for ground at the cutaway like in the example above.

    this is the code for it - (i modified a toon shader) using discard to stop rendering fragments after a certain distance(x,y,z directions) from a gameobject(blue cube) that i control.

    Code (CSharp):
    1. fragmentInput vert(vertexInput i){
    2.                     fragmentInput o;
    3.                     o.pos = mul(UNITY_MATRIX_MVP, i.vertex);
    4.                     o.uv = TRANSFORM_TEX(i.texcoord, _MainTex);
    5.                  
    6.                      o.Pos_In_World_Coords = mul(_Object2World, i.vertex);
    7.                     // Here we set up the normal and light dir for use in the fragment shader, both in object space.
    8.                     o.normal = i.normal;
    9.                     o.lightDir = ObjSpaceLightDir(i.vertex);
    10.                     return o;
    11.                 }
    12.            
    13.                 float4 frag(fragmentInput i) : COLOR
    14.                 {
    15.                     // Normalize the vectors to fix interpolation shortening.
    16.                     i.normal = normalize(i.normal);
    17.                     i.lightDir = normalize(i.lightDir);
    18.                     // Get dot product to use as ramp UV coords.
    19.                     // Transformed from -1 to 1 range to 0 - 1 range so it can use the full dimensions of the ramp texture.
    20.                     float NdotL = dot(i.normal, i.lightDir) * 0.5 + 0.5;
    21.                                      
    22.                     // Put the vector maths in brackets so it doesn't try and do scalar-vector maths where it doesn't need to.
    23.                     fixed4 c;
    24.                     c.rgb = (tex2D(_MainTex, i.uv).rgb * tex2D(_RampTex, NdotL.xx).rgb * _LightColor0.rgb * _Color.rgb) * 2;
    25.                     c.a = 1.0;
    26.                  
    27.                     if (abs(i.Pos_In_World_Coords.y - _Point.y) > _Distance || abs(i.Pos_In_World_Coords.x- _Point.x) > _Distance || abs(i.Pos_In_World_Coords.z - _Point.z) > _Distance)
    28.                     {
    29.                        discard; // drop the fragment if xyz coordinate > _Distance
    30.                     }
    31.                     return c;
    32.                  
    33.                  }
    i found a thread similar to it in unreal forums https://forums.unrealengine.com/showthread.php?1441-Cross-Section-Shader-help-please , i dont know how to use opacity mask in code.
     

    Attached Files:

  2. varfare

    varfare

    Joined:
    Feb 12, 2013
    Posts:
    227
    Try adding this after your Tags:
    Code (CSharp):
    1. Cull Off
     
  3. aku286

    aku286

    Joined:
    Feb 1, 2014
    Posts:
    5
    it worked, if i dont want to show the interiors of cross section(ex: a thick ground plane) what to do?any idea?
     

    Attached Files:

  4. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,414
  5. aku286

    aku286

    Joined:
    Feb 1, 2014
    Posts:
    5
    i figured it out after going through that thread , they were doing the same thing with plane equation. Now i added shadows to it using this http://forum.unity3d.com/threads/adding-shadows-to-custom-shader-vert-frag.108612/
    it still has a few problems:
    -not able to change the color of the shadow
    -still getting shadows for the entire object even though i cut it(second image)
    For the second problem google says unity still thinks it has all the vertices and calculates the shadow accordingly, so i have to somehow skip the shadow calculation at those vertices.

    Any thoughts?
     

    Attached Files:

    Last edited: Feb 19, 2015