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

Vertex Displacement in Unlit Vertex Shader

Discussion in 'Shaders' started by michaelthewanderer, Jun 22, 2020.

  1. michaelthewanderer

    michaelthewanderer

    Joined:
    Jul 15, 2018
    Posts:
    12
    Hello all,

    I'm having some trouble getting basic vertex displacement to work in an unlit vertex shader. In the past I have been able to generate vertex displacement effects using surface shaders, but my understanding is that these are no longer supported in URP.

    The shader is applied to a surface and is intended to offset the vertices close to a touch on the surface (like a trampoline). The color offset in the fragment shader works fine, but I cannot see any indication that the vertices are displacing. Is this just an artifact of there being no lighting on the object (similar process works fine if the shader is lit or, like previously, a surface shader)? I don't think the vertices are displacing because when I apply this to a 3D object, even when viewing it at an angle I cannot see any deformation of the object.

    Any advice appreciated! Thank You!

    Code (CSharp):
    1. Shader "Unlit/ManifoldUnlit"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}  
    6.         _Color ("Color", Color) = (1,1,1,1)
    7.         _Scale ("Scale", float) = 1
    8.      
    9.     }
    10.     SubShader
    11.     {
    12.         Tags { "RenderType"="Opaque" }
    13.         LOD 200
    14.  
    15.         Pass
    16.         {
    17.             CGPROGRAM
    18. // Upgrade NOTE: excluded shader from DX11, OpenGL ES 2.0 because it uses unsized arrays
    19. #pragma exclude_renderers d3d11 gles
    20. // Upgrade NOTE: excluded shader from DX11; has structs without semantics (struct v2f members worldpos)
    21.             #pragma exclude_renderers d3d11
    22.             #pragma vertex vert
    23.             #pragma fragment frag
    24.             // make fog work
    25.            // #pragma multi_compile_fog
    26.  
    27.             #include "UnityCG.cginc"
    28.  
    29.             struct appdata
    30.             {
    31.                 float4 vertex : POSITION;
    32.                 fixed4 color : COLOR;
    33.                // float2 uv : TEXCOORD0;
    34.             };
    35.  
    36.             struct v2f
    37.             {
    38.                
    39.                // float2 uv : TEXCOORD0;
    40.                 float4 vertex : SV_POSITION;
    41.                
    42.                 float3 worldPos: TEXCOORD0;
    43.                 fixed4 color: COLOR;
    44.  
    45.             };
    46.  
    47.             sampler2D _MainTex;
    48.             float4 _MainTex_ST;
    49.             fixed4 _Color;
    50.             float4 _upColor;
    51.             float _scale;
    52.  
    53.             float4 _touch;
    54.             float _touchActive;
    55.  
    56.            // float4 _paths[1023];
    57.  
    58.             float3 TouchOffset(float3 pt, float3 touch, int touchActive)
    59.             {
    60.        
    61.                 float3 diff = pt - touch;
    62.                 float dist = length(diff);
    63.                 //dist *= touch.w;
    64.  
    65.                 return -pt/min(1,(dist*dist)) * touchActive;
    66.  
    67.             }
    68.  
    69.             float4 ColorOffset(float3 pt, float4 touch)
    70.             {
    71.                
    72.                 float dist = distance(pt, touch.xyz);
    73.                 dist = 50*dist*dist;
    74.                  //   * (1+sin(_Time.y*dist))/2
    75.  
    76.                 float r = log(1 * log(1+touch.w)/(dist))/2; //* touchActive;
    77.                 float g = log(1 / log(1+touch.w)/(dist))/2;
    78.  
    79.                 return float4(r,g,0,1) ;
    80.                
    81.             }
    82.  
    83.             v2f vert (appdata v)
    84.             {
    85.  
    86.                 float3 p = v.vertex.xyz;
    87.                 p += TouchOffset(p, _touch, _touchActive);
    88.  
    89.                 p *= 1 + _scale*sin(_Time.y);
    90.                
    91.  
    92.                 v.vertex.xyz = p;
    93.  
    94.                 v2f o;
    95.                 o.worldPos = mul(unity_ObjectToWorld, v.vertex);
    96.                 o.vertex = UnityObjectToClipPos(v.vertex);
    97.                 o.color = v.color;
    98.                 //o.vertex = UnityObjectToClipPos(v.vertex);
    99.                 //o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    100.              //   UNITY_TRANSFER_FOG(o,o.vertex);
    101.                 return o;
    102.             }
    103.  
    104.             fixed4 frag (v2f i) : SV_Target
    105.             {
    106.                // fixed4 color = _Color;
    107.                 // sample the texture
    108.                // fixed4 col = tex2D(_MainTex, i.uv) * _Color;
    109.  
    110.                
    111.                 fixed4 color = i.color;
    112.                 //color*= _Color;
    113.                 color += ColorOffset(i.worldPos, _touch);
    114.  
    115.                 return color;
    116.             }
    117.  
    118.             ENDCG
    119.         }
    120.     }
    121. }