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

Lighting/normals produce "Cuts" through my object.

Discussion in 'Shaders' started by Hotpockets26, Feb 23, 2019.

  1. Hotpockets26

    Hotpockets26

    Joined:
    Dec 16, 2017
    Posts:
    32
    Hello, I am following this tutorial https://catlikecoding.com/unity/tutorials/rendering/part-4/

    I've gotten up to the point of 2.4 in his tutorials, but my shader ends up with these cuts through spheres and capsules, while his are smooth.

    Mine


    His


    As far as I can tell our code is exactly the same, so I was wondering if anything in unity has changed since this tutorial was released?

    Here is my code

    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2.  
    3. Shader "Custom/My First Lighting Shader" {
    4.  
    5.     Properties{
    6.         _Tint("Tint", Color) = (1, 1, 1, 1)
    7.         _MainTex("Texture", 2D) = "white" {}
    8.     }
    9.  
    10.         SubShader{
    11.  
    12.             Pass{
    13.  
    14.                 Tags {
    15.                     "LightMode"  = "ForwardBase"
    16.                      }
    17.  
    18.                 CGPROGRAM
    19.  
    20.                 #pragma vertex MyVertexProgram
    21.                 #pragma fragment MyFragmentProgram
    22.  
    23.                 //#include "UnityCG.cginc"
    24.                 #include "UnityStandardBRDF.cginc" // this includes "#include "UnityCG.cginc""
    25.  
    26.                 float4 _Tint;
    27.                 sampler2D _MainTex;
    28.                 float4 _MainTex_ST; // for tiling and offset
    29.  
    30.                 struct Interpolators {
    31.                     float4 position : SV_POSITION;
    32.                     float3 normal : NORMAL;
    33.                     float2 uv : TEXCOORD0;
    34.                 };
    35.  
    36.                 struct VertexData {
    37.                     float4 position : POSITION;
    38.                     float2 uv : TEXCOORD0;
    39.                     float3 normal : TEXCOORD1;
    40.                 };
    41.  
    42.             Interpolators MyVertexProgram(VertexData v) {
    43.                 Interpolators i;
    44.  
    45.                 i.position = UnityObjectToClipPos(v.position);
    46.                 i.normal = UnityObjectToWorldNormal (v.normal);
    47.                 i.uv = TRANSFORM_TEX(v.uv, _MainTex); // i.uv = TRANSFORM_TEX(v.uv, _MainTex); is a shorthand macro for  "i.uv = v.uv * _MainTex_ST.xy + _MainTex_ST.zw" // * _MainTex_ST.xy is used for tiling // + _MainTex_ST.zw is used for offset
    48.                 return i;
    49.             }
    50.  
    51.             float4 MyFragmentProgram (Interpolators i) : SV_TARGET {
    52.                 i.normal = normalize(i.normal);
    53.                 float3 lightDir = _WorldSpaceLightPos0.xyz;
    54.                 return DotClamped(lightDir, i.normal);
    55.             }
    56.              
    57.             ENDCG
    58.              
    59.         }
    60.     }
    61. }
    Any help is appreciated,
    Thanks!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    You have your interpolators and vertex data flipped in order vs what the Catlike Coding tutorial has, and you're using the mesh's TEXCOORD1 instead of it's NORMAL for the normal data. Line 39 should be:

    float3 normal : NORMAL;

    You can pass the normal through the interpolator using the NORMAL symantics as well, which is why the shader is still working, though traditionally you map it to a TEXCOORD. The reality is, apart from SV_Position, what semantics you use for passing data in the interpolator struct is essentially arbitrary.
     
    Hotpockets26 likes this.