- Home /
Shader Vertex Data Format
Hi,
I am attempting to use the vertex data in a surface shader to get an angle. I have the camera centered at 0,0. In what coordinate system is v.vertex in the vertex function?
#pragma surface surf Lambert vertex:vert nolightmap
void vert (inout appdata_full v, out Input o)
{
o.svc =(1-(_vc*v.vertex.z)/sqrt(pow(v.vertex.x,2) + pow(v.vertex.y,2) + pow(v.vertex.z,2)))/sqrt( 1 - _vc * _vc);
}
I thought it would be the world coordinate system, but I get different results for objects that are rotated in unity when compared to objects that are rotated in 3DS Max ahead of time to look the same vertex wise. What data am I actually recieving with v.vertex.xyz?
Answer by Owen-Reynolds · Mar 23, 2012 at 12:10 AM
It's the unmodified "raw" coords, also known as local; before position or rotation, based on the model's personal (0,0,0).
For world xyz, take v.vertex
times the world matrix.
Thanks, how would I get the world matrix? I know that I can get the worldPos in the main shader, but I'm running out of instructions so I need to do it in the vertex modifier function.
Yep -- do as much math in the vertex shader as you can.
Looking at http://unity3d.com/support/documentation/Components/SL-BuiltinStateInPrograms.html, they have almost everything but "worldView" $$anonymous$$ATRIX_$$anonymous$$. I think most people use $$anonymous$$V ("world" coords in the camera's frame of reference.) SL-SurfaceShaderExamples.html has an example of use: float4 hpos = mul (UNITY_$$anonymous$$ATRIX_$$anonymous$$VP, v.vertex);
It looks like $$anonymous$$atrix4x4.SetTRS
could be used in a script to create a worldView matrix for your model, and be passed as an extra parm to the shader (but I've never done that.)
Answer by cupsster · May 02, 2012 at 01:38 PM
Maybe you could also inspect Unity3D.cginc there should be some snippets that handle this if I remember it right.
Your answer
![](https://koobas.hobune.stream/wayback/20220613065418im_/https://answers.unity.com/themes/thub/images/avi.jpg)