- Home /
Shader Vert Offsets in local space
I have a shader that applies offset to each vertex based on a sin wave, this is the vert function:
void vert ( inout appdata_full v )
{
float twoPi = 2 * 3.141592653;
float4 change = float4( 0, 0, 0, 0 );
float redSin = sin(_Time.x * _Speed) * _Amplifier;
change.x += redSin * _RedStrength;
change = mul ( _World2Object, change );
v.vertex += change;
}
If object has no rotations, everything works great. But if object is rotated, it moves as if it is not rotated. So, regardless of rotations all of the objects with this shader move uniformly. I made it so that each material is instanced, not shared, but that didn't help. What am I missing?
Also, I looked at this: http://answers.unity3d.com/questions/561978/modifying-vertex-position-in-a-surface-shader.html and http://answers.unity3d.com/questions/819002/getting-the-local-vertex-positions-in-vertex-shade.html and http://answers.unity3d.com/questions/561900/get-local-position-in-surface-shader.html, but none of these helped :(.
Also, if I don't multiply change by _World2Object, shader behaves differently depending on the distance from the camera... If object is rotated, axis of movement switches at different distances. Weird!!!....
Please HALP! How do I apply vert changes in local space?
EDIT: This is a surface shader.
Answer by FortisVenaliter · May 10, 2016 at 05:38 PM
The point of a vertex subshader is to transform the vertices from local to screen space. You still need to do that conversion manually, after you've done your sin wave transformation. Check out these official examples.
These are structured differently to what I am used to. I am kind of a noob when it comes to shaders, I followed "Normal Extrusion with Vertex $$anonymous$$odifier" example to get started from this page: http://docs.unity3d.com/$$anonymous$$anual/SL-SurfaceShaderExamples.html. Would you $$anonymous$$d nudging me closer to what I need to take a look at?
The main changes to make are based on your vertex shader's declaration:
void vert ( inout appdata_full v )
By using inout, you're letting Unity automatically create and pass data through the vertex shader for you.
You need to grab the vertex position information before it's automatically been converted (or convert it back yourself, but then you're transfor$$anonymous$$g from object->world->object->world space for no real reason).
So, I should try switching to "v2f vert (appdata v)" and calculate transforms using something like this:
v2f o;
o.vertex = mul(UNITY_$$anonymous$$ATRIX_$$anonymous$$VP, v.vertex);
If i understand you correctly.
Your answer