- Home /
Flawed Mesh Manipulation Question!
I have the deforming mesh script curtesy of World of Zero (https://www.youtube.com/watch?v=l_2uGpjBMl4) and it seems to be pretty flawed. It doesn't work when I have it above a certain y value or when it's rotated. I'm looking to fix that but I suck at mesh manipulation.
Heres the deformable mesh script:
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
[RequireComponent(typeof(GeneratePlaneMesh))]
public class DeformableMesh : MonoBehaviour
{
public float maximumDepression;
public List<Vector3> originalVertices;
public List<Vector3> modifiedVertices;
private GeneratePlaneMesh plane;
public void MeshRegenerated()
{
plane = GetComponent<GeneratePlaneMesh>();
plane.mesh.MarkDynamic();
originalVertices = plane.mesh.vertices.ToList();
modifiedVertices = plane.mesh.vertices.ToList();
Debug.Log("Mesh Regenerated");
}
public void AddDepression(Vector3 depressionPoint, float radius)
{
var worldPos4 = this.transform.worldToLocalMatrix * depressionPoint;
var worldPos = new Vector3(worldPos4.x + transform.position.x, worldPos4.y, worldPos4.z + transform.position.z);
Debug.Log(worldPos);
for (int i = 0; i < modifiedVertices.Count; ++i)
{
var distance = (worldPos - (modifiedVertices[i] + Vector3.down * maximumDepression)).magnitude;
//and if verticy height is higher than maximum height
if (distance < radius)
{
var newVert = originalVertices[i] + Vector3.down * maximumDepression;
// times depression per hit instead of maximum depression
modifiedVertices.RemoveAt(i);
modifiedVertices.Insert(i, newVert);
}
}
plane.mesh.SetVertices(modifiedVertices);
Debug.Log("Mesh Depressed");
}
}
I will be forever grateful if someone helps out. I've never had a question answered on this stupid site but 10+ hours have gone into this.
Answer by Eno-Khaon · Dec 29, 2020 at 08:04 PM
There are a few things that can be addressed here:
First, by this use of a radius away from the point(s) of contact, if the modified vertices have already been shoved far enough away (down?), then their neighboring vertices would become hard to reach using that radius. You could dig an incredibly deep pit (theoretically) and not be able to lower adjacent vertices if the height difference is currently greater than the provided radius (for better or worse). Similarly, depressions made are a fixed depth, but that seems to be the result of the video cutting the process short and not working things through to completion.
Next, regardless of whether the depression is scaled by the distance from the point of contact (it's currently not), an unnecessary square root calculation is making this whole process needlessly taxing.
// Untested, but should be a more straight-forward approach to this
// (Also, not having looked into how the video author generated the plane mesh,
// this assumes that its vertices are centered around or near a local (0, 0, 0) )
public void AddDepression(Vector3 depressionPoint, float radius)
{
// Simplified converting contact point to local position
Vector3 contactPoint = plane.transform.InverseTransformPoint(depressionPoint);
for(int i = 0; i < originalVertices.Count; i++)
{
float distance = (contactPoint - originalVertices[i]).sqrMagnitude;
if(distance <= radius)
{
// Since changes are made locally to the mesh, this uses
// Vector3.down rather than transform.down
Vector3 newVertex = originalVertices + Vector3.down * maximumDepression;
// Changed this because... why was it handled the way it was?
modifiedVertices[i] = newVertex;
}
}
plane.mesh.SetVertices(modifiedVertices);
}
Unfortunately, this approach to handling ground deformation will always see significant performance hits as the vertex count/ground complexity increases. Ultimately, this leads most modern games to use a mainly- Shader-driven system for mostly-fixed deformations like snow.
YES!! It works! I cannot thank you enough. Your the first person to help with the problem and not just provide a link.
Your answer
Follow this Question
Related Questions
Is it possible to enable Read/Write of a imported Mesh at Runtime? 0 Answers
Remove unnecessary vertices from plane mesh 1 Answer
Is there any way to get the optimized (cooked) mesh of a mesh collider? 0 Answers
How to manipulate(flatten) the Mesh of an object 0 Answers
Edit Mesh Edge to Match Overlapping Object or Obstruction 0 Answers