Moving vertices in shaders

Hi there,

For my project I have to manipulate mesh at runtime, to do this without clearing and refilling the mesh array (which is very expensive) I’m going to do it in a shader, atleast, try to.

I’ve browsed the web, including unity answers intensely but I just can’t find a tutorial or document which tells me step by step how to achieve this.

The tutorial on displaying normal colors was already a great help, but the shader documentation is still a bit confusing for me (someone who just started to delve into Unity shaders).

I have come as far as:

    Properties{
         _vertPos("VertPos", vector) = (0,0,0,1) //this to initialize
    } 

then outside the struct i define my
float4 vertPos;
I am able to fill in the vector from script and read it by using

set/getVector("_vertPos");

I’ve added

#pragma surface surf Lambert vertex:vert

to specify my vertex function, and made a function:

float4 vert(float4 v:POSITION) : SV_POSITION
{

}

But now I’m stuck, and I honestly can’t find something that simply specifies how to move a specific vertex. I got the function parameters from the unity documentation, but i’m still unclear what this does, or where i have to put the inout, because I think i have to get the appdata_base in there and find and access the vertices, and hopefully manipulate them according to their location. The main thing I want to manipulate is the height.

I am quite aware of matrix structures, it’s just the syntax I’m really struggling with

Any help or an alternative way of moving vertices from a mesh would be much appreciated.

The vertex modifier function of a surface shader doesn’t just have an float4 POSITION input and return a float4 SV_POSITION output - rather it operates on the entire appdata_full structure, of which “vertex” is a member. Here’s an example of a vertex modifier that modifies the “height” (i.e. z value) of vertices:

  Shader "Vertex Modifier" {
    Properties {
      _MainTex ("Texture", 2D) = "white" {}
      _Amount ("Height Adjustment", Float) = 1.0
    }
    SubShader {
      Tags { "RenderType" = "Opaque" }
      CGPROGRAM
      #pragma surface surf Lambert vertex:vert
      struct Input {
          float2 uv_MainTex;
      };

      // Access the shaderlab properties
      float _Amount;
      sampler2D _MainTex;

      // Vertex modifier function
      void vert (inout appdata_full v) {
          // Do whatever you want with the "vertex" property of v here
          v.vertex.z += _Amount;
      }

      // Surface shader function
      void surf (Input IN, inout SurfaceOutput o) {
          o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
      }
      ENDCG
    }
  }