Monday, April 30, 2018

unity - Volume preserving procedural mesh deformation


I'm having difficulties trying to create a procedural mesh that deforms on impact. The deformation is meant to look and feel like metal when it is being forged. Since this is less of an aesthetic feature and more of something that would affect gameplay directly, it needs to be somewhat accurate. This means that volume of the mesh should be preserved (at least visually) such that the mesh does not gradually get too much smaller.


What I have tried:



1) Applying force on every vertex, based on the force and distance from impact point


foreach (Vector3 vertexPos in vertexList)
{
vertexPos += (force / distanceToImpactPoint.sqrMagnitude) * impactDir;
}

This led to the entire mesh gradually becoming smaller (as mentioned earlier)


2) Take into account the dot product between the impact direction and vertex normal for each vertex, as well as lerp between impact direction and normal (to simulate sides of objects bulging out when compressed


foreach (Vector3 vertexPos in vertexList)
{

float attenuatedForce = (force / distanceToImpactPoint.sqrMagnitude);
float nDotD = Vector3.Dot(impactDir, currVertexNormal);
vertexPos = attenuatedForce * nDotD * Vector3.Lerp(impactDir, currVertexNormal, nDotD);
}

This led to mesh completely splitting apart for anything that has hard shading (as there are duplicate vertices where the edges are, and also artifacts on smooth shaded objects.


Hard Shading: enter image description here


Soft Shading (sort of works, but lots of artefacts and not volume preserving) enter image description here


Also, both methods break completely if force is applied to an edge.


Does anyone know of a more elegant solution to do this?





No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...