- Home /
How to access other vertices in vertex shader?
I have a special mesh where my vertices are in groups of four. Vertices in a group are continuously in the vertex array. I would like to have a vertex shader that linearly scales so that with scale value of 1.0 all vertices would have orginal positions and with scale value of 0.0 all vertices would be in the center of their groups of four.
Semi pseudo code could be something like:
Shader property _Scale Range(0.0, 1.0)
CGPROGRAM
#pragma vertex vert
uniform float _Scale;
vert(appdata v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
int vertexindex = (vertexIndex / 4) * 4; // floor to /4
float4 center = average_float4(
vertex[vertexindex],
vertex[vertexindex + 1],
vertex[vertexindex + 2],
vertex[vertexindex + 3]);
o.pos = lerp_float4(center, o.pos, _Scale);
return o;
}
ENDCG
Subquestions:
-How to access other vertices within vertex shader?
-Is there average and lerp fuctions for float4 type?
Answer by tanoshimi · May 26, 2015 at 05:01 PM
You can't access vertices other than the one currently being processed - the shader pipeline is designed to be parallelized so there is no way to logically define vertex[i+1].
However, you could encode the "group centerpoint" in an additional property associated with each vertex (the xyz coordinates encoded as the rgb components of the vertex colour of each vertex in the mesh, for example) and pass them into the shader that way.
That is a good idea! I have been working with compute shaders and CUDA and there you can access the index of parallel process. And it's okay to use same (vertex)data as long as you just read it. I hope your answer's $$anonymous$$d set will help me also in the future.
Your answer
![](https://koobas.hobune.stream/wayback/20220613200312im_/https://answers.unity.com/themes/thub/images/avi.jpg)