Search Unity

how to get smooth curvature

Discussion in 'Shaders' started by dreamerflyer, Jan 29, 2020.

  1. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    I get flat render ,how to fixed?
    Code (CSharp):
    1. Shader "Unlit/Curvature"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.     }
    7.     SubShader
    8.     {
    9.         Tags { "RenderType"="Opaque" }
    10.         LOD 100
    11.  
    12.         Pass
    13.         {
    14.             CGPROGRAM
    15.             #pragma vertex vert
    16.             #pragma fragment frag
    17.             // make fog work
    18.             #pragma multi_compile_fog
    19.             #pragma target 3.0
    20.             #include "UnityCG.cginc"
    21.  
    22.             struct appdata
    23.             {
    24.                 float4 vertex : POSITION;
    25.                 float2 uv : TEXCOORD0;
    26.                 float3 normal:NORMAL;
    27.             };
    28.  
    29.             struct v2f
    30.             {
    31.                 float2 uv : TEXCOORD0;
    32.                 UNITY_FOG_COORDS(1)
    33.                 float4 vertex : SV_POSITION;
    34.                 float3 viewNormal:TEXCOORD1;
    35.             };
    36.  
    37.             sampler2D _MainTex;
    38.             float4 _MainTex_ST;
    39.  
    40.             v2f vert (appdata v)
    41.             {
    42.                 v2f o;
    43.                 o.vertex = UnityObjectToClipPos(v.vertex);
    44.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    45.                 o.viewNormal = mul((float3x3)UNITY_MATRIX_IT_MV, v.normal);
    46.                
    47.                 UNITY_TRANSFER_FOG(o,o.vertex);
    48.                 return o;
    49.             }
    50.  
    51.             fixed4 frag (v2f i) : SV_Target
    52.             {
    53.                 // sample the texture
    54.                // fixed4 col = tex2D(_MainTex, i.uv);
    55.                 fixed4 curvature= length(fwidth(float4(i.viewNormal,1)))*40;
    56.  
    57.                  return curvature;
    58.                 // apply fog
    59.               //  UNITY_APPLY_FOG(i.fogCoord, col);
    60.                // return col;
    61.             }
    62.             ENDCG
    63.         }
    64.     }
    65. }
    66.  
     

    Attached Files:

  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Technically that's correct. The curvature is constant across the entire face of the mesh, so there's nothing to "fix".

    Obviously it's ugly though. Usually the solution is to bake out the curvature using an external application, either saving it as a texture or baking it into the vertex data.
     
  3. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    http://madebyevan.com/shaders/curvature/ it seems smooth.curvature can not calculate vertex normal ,only face normal?and what agrithm use in curvature Node within substance ?
     
    Last edited: Jan 30, 2020
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Nope, that's faceted too. It's just a really, really high poly mesh so it's hiding that fact. You could do that too if you wanted, but it has to be an actual high poly mesh. Real time tessellation doesn't affect the normal, so the derivatives retain their faceted appearance after tessellation.

    In a shader, yes. Derivatives, the fwidth or ddx & ddy functions, only work in the fragment shader. There are academic papers out there showing how to compute the curvature using geometry shaders, but those use adjacency information which Unity doesn't support.

    The closest you can get to real time per-vertex curvature would be to use a compute shader to iterate over the mesh and store the data in a compute buffer to be read from SV_VertexID, or something like Keijiro's Skinner which stores the "index" of the vertex in the UV data.
     
    LooperVFX and dreamerflyer like this.