- Home /
streching mesh along the velocity
I make a shader that multiply vertices with velocity but it is not stay along the direction of movement. How was calculation that I need? vertices are in objectspace; Here is my code:
private void LateUpdate()
{
mat.SetVector("_Velocity", transform.InverseTransformVector( strecthK * new Vector3(rb.velocity.x, rb.velocity.y, rb.velocity.z)));
}
Answer by Bunny83 · Mar 22 at 04:50 PM
First of all I would recommend using InverseTransformDirection instead. InverseTransformVector expects a 4d vector in homogeneous space. While it should also work this way (since implicitly converting a Vector3 to a Vector4 would make the w component 0), it's cheaper to use InverseTransformDirection.
Second, we don't know how your vertex shader code looks like. The vertex shader is responsible for transforming the vertices from object space to NDC space (-1 to +1). So when you want to stretch / scale your vertices you have to do that before you transform with the MVP matrix.
Finally since you want to "scale" your mesh based on your velocity, it means you need a scaling factor which you use to multiply your vertices with. Usually at 0 velocity you want a scale of 1 while you want an increasing factor when you move faster in a certain direction. Keep in mind that the scaling would happen around the object pivot. So for different objects you may get quite different results depending on where their pivots are located. So how or where do you calculate that scaling factor? Because the _Velocity value is just your velocity that was prescaled by a factor. However it would still be (0,0,0) when you don't move.
I make some stupid tries.Scaling has problems using default a positive scale has problems as you mentioned also negative scale values are problem too.actually I need a plane(cent er of object andlooking velocity direction) side detection and propotional shift for vertices. How can I do this shader side?
Answer by pastersteli · Mar 22 at 07:29 PM
I figured out the stretching part. But not properly updated velocity on material.
Vector3 temp = strecthK * new Vector3(transform.InverseTransformPoint(transform.position+rb.velocity).x, transform.InverseTransformPoint(transform.position+rb.velocity).z, transform.InverseTransformPoint(transform.position+rb.velocity).y);
mat.SetVector("_Velocity", temp);
Your answer
![](https://koobas.hobune.stream/wayback/20220613054507im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Object velocity affect vertex position in shader graph?? 0 Answers
vertex displacement shader and ssao 0 Answers
Scene Color Node in Shader Graph not working with Unity's 2D Renderer and URP 5 Answers
Rendering full-screen gradient background without overdraw? 1 Answer
Distortion Shader does not render transparent objects (Shader Graph) 2 Answers