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

Tesselation shader doesn't smooth geometry

Discussion in 'Shaders' started by fataldevelop, Sep 8, 2020.

  1. fataldevelop

    fataldevelop

    Joined:
    Jul 5, 2014
    Posts:
    17
    Hey guys, I was trying Phong Tesselation and Fixed Tesselation shaders from https://docs.unity3d.com/Manual/SL-SurfaceShaderTessellation.html and I can see that Tesselation is working in Wireframe mode, but it doesnt smooth the geometry like in example, it just makes flat tesselation. Editor is in DX11 mode, i have GTX 1060, everything should work, what do you think is the problem?
     
  2. fataldevelop

    fataldevelop

    Joined:
    Jul 5, 2014
    Posts:
    17

    This is what i get. Tesselated, but not smoothed
     
  3. AlphaSilverback

    AlphaSilverback

    Joined:
    Aug 18, 2016
    Posts:
    10
    Very late answer, but the reason is probably that you are not actually displacing the vertices in the vertex shader.
    Usually you would use a heightmap or some other math to push the vertices along their normals.

    If you look at this shader fx, from https://docs.unity3d.com/Manual/SL-SurfaceShaderTessellation.html, you can see that the "vertex shader" is set to be the function called "disp". The extra geometry generated by the tessellation(hull and domain) stages, is being pushed along their normals by some factor and a map color.

    Code (CSharp):
    1.  Shader "Tessellation Sample" {
    2.         Properties {
    3.             _Tess ("Tessellation", Range(1,32)) = 4
    4.             _MainTex ("Base (RGB)", 2D) = "white" {}
    5.             _DispTex ("Disp Texture", 2D) = "gray" {}
    6.             _NormalMap ("Normalmap", 2D) = "bump" {}
    7.             _Displacement ("Displacement", Range(0, 1.0)) = 0.3
    8.             _Color ("Color", color) = (1,1,1,0)
    9.             _SpecColor ("Spec color", color) = (0.5,0.5,0.5,0.5)
    10.         }
    11.         SubShader {
    12.             Tags { "RenderType"="Opaque" }
    13.             LOD 300
    14.            
    15.             CGPROGRAM
    16.             #pragma surface surf BlinnPhong addshadow fullforwardshadows vertex:disp tessellate:tessDistance nolightmap
    17.             #pragma target 4.6
    18.             #include "Tessellation.cginc"
    19.  
    20.             struct appdata {
    21.                 float4 vertex : POSITION;
    22.                 float4 tangent : TANGENT;
    23.                 float3 normal : NORMAL;
    24.                 float2 texcoord : TEXCOORD0;
    25.             };
    26.  
    27.             float _Tess;
    28.  
    29.             float4 tessDistance (appdata v0, appdata v1, appdata v2) {
    30.                 float minDist = 10.0;
    31.                 float maxDist = 25.0;
    32.                 return UnityDistanceBasedTess(v0.vertex, v1.vertex, v2.vertex, minDist, maxDist, _Tess);
    33.             }
    34.  
    35.             sampler2D _DispTex;
    36.             float _Displacement;
    37.  
    38.             void disp (inout appdata v)
    39.             {
    40.                 float d = tex2Dlod(_DispTex, float4(v.texcoord.xy,0,0)).r * _Displacement;
    41.                 v.vertex.xyz += v.normal * d;
    42.             }
    43.  
    44.             struct Input {
    45.                 float2 uv_MainTex;
    46.             };
    47.  
    48.             sampler2D _MainTex;
    49.             sampler2D _NormalMap;
    50.             fixed4 _Color;
    51.  
    52.             void surf (Input IN, inout SurfaceOutput o) {
    53.                 half4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    54.                 o.Albedo = c.rgb;
    55.                 o.Specular = 0.2;
    56.                 o.Gloss = 1.0;
    57.                 o.Normal = UnpackNormal(tex2D(_NormalMap, IN.uv_MainTex));
    58.             }
    59.             ENDCG
    60.         }
    61.         FallBack "Diffuse"
    62.     }
     
    fataldevelop likes this.
  4. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    5,994
    because you need to use the one at the bottom: phong will displace the vertices to follow the interpolated normals
     
    DimitriX89 and fataldevelop like this.