Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Normal Extrusion as in tutorial is not working

Discussion in 'Shaders' started by mathiasj, Jan 27, 2016.

  1. mathiasj

    mathiasj

    Joined:
    Nov 3, 2015
    Posts:
    64
    Hello,
    I've been trying to extrude my mesh along the normals using the code in the tutorial(http://docs.unity3d.com/460/Documentation/Manual/SL-SurfaceShaderExamples.html) but it won't work. Instead of making my cube bigger, the faces get separated, like this:



    This is the code from the tutorial:

    Code (CSharp):
    1.   Shader "Example/Normal Extrusion" {
    2.     Properties {
    3.       _MainTex ("Texture", 2D) = "white" {}
    4.       _Amount ("Extrusion Amount", Range(-1,1)) = 0.5
    5.     }
    6.     SubShader {
    7.       Tags { "RenderType" = "Opaque" }
    8.       CGPROGRAM
    9.       #pragma surface surf Lambert vertex:vert
    10.       struct Input {
    11.           float2 uv_MainTex;
    12.       };
    13.       float _Amount;
    14.       void vert (inout appdata_full v) {
    15.           v.vertex.xyz += v.normal * _Amount;
    16.       }
    17.       sampler2D _MainTex;
    18.       void surf (Input IN, inout SurfaceOutput o) {
    19.           o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
    20.       }
    21.       ENDCG
    22.     }
    23.     Fallback "Diffuse"
    24.   }
    What is wrong?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Nothing, it's working exactly as it should. The shader pushes the edges out in the direction of the face normals. That means any edge with sharp normals, like that box, the faces will push out away from eachother as that is the direction their edges point. The example mesh is smooth with no hard edges, so the faces push out together.
     
  3. mathiasj

    mathiasj

    Joined:
    Nov 3, 2015
    Posts:
    64
    You're right. Once I used a model with smooth normals it worked as I had expected it. Thanks!