VR transform tree mesh on axe hit
I'm lost. I want to deform a tree-mesh on an axe hit, at the position of the collision. How would this be possible? Mesh manipulation? Fake-holes? Shaders?
So far I tried it with moving the vertices, which can be seen in this gif:
https://imgur.com/a/KEfS5EQ
What I did to achieve that:
Find closest vertex to hit.point (raycast)
Find all triangles containing this point (+highlight them)
Find all vertices on a horizontal plane (the same level as the first found vertex)
Take those (3) vertices and assign them some velocity, code example (pseudo):
vertexVelocities = new Vector3[mesh.vertices.Length];
[...]
Vector3 point = hit.point;
point += hit.normal * forceOffset;
foreach vertex on same level
{
Vector3 pointVertex = vertex - point;
float attenuatedForce = force / (1f + pointVertex.sqrMagnitude);
float velocity = attenuatedForce * Time.deltaTime;
vertexVelocities[index] += pointVertex.normalized * velocity;
displacedVertices[i] += vertexVelocities[index] * (Time.deltaTime);
mesh.SetVertices(displacedVertices.ToList());
}
Problems can be seen in the gif.
I think I need to create more triangles at the position of impact, to get a higher resolution, but how?
Also, the direction of the vertex movement is not quite right. Also, the vertices are poking out of the tree...
Is this even a "good" approach? Maybe someone here has an idea to improve that, or even an alternative way of handling the issue.
Some help would be greatly appreciated.
Your answer
Follow this Question
Related Questions
How can I create a plane at any given vertex position? 0 Answers
Vertex colors not working in shader graph. 0 Answers
Create mesh but change x and z to x any axis. 0 Answers
Is it possible to change a vertex (height) after combining meshes? 1 Answer
How to get triangle index of meshe's desired part? 0 Answers