- Home /
Question by
imran-farooq · Jan 01, 2018 at 11:34 PM ·
meshmouse-dragmesh vertices
Drag Vertices of mesh with mouse
I want to do something like this video. https://www.youtube.com/watch?v=ioiaDXOI6zI
I am stuck at this, would appreciate any help or guidance. Thanks
This is my code so far:
public class MeshDeformation : MonoBehaviour {
MeshFilter mf;
Vector3[] verts;
public Vector3 value = new Vector3(0.01f,0.01f,0.01f);
void Start () {
mf = GetComponent<MeshFilter>();
verts = mf.mesh.vertices;
Debug.Log("Vertices" + verts.Length);
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButton(0))
{
RaycastHit hit;
Vector3 input = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,out hit, 300))
{
Vector3 hitVertices = FindNearestPoint(hit.point);
RaisePoint(hitVertices,value);
Debug.Log("coming inside");
}
}
}
private Vector3 FindNearestPoint(Vector3 point)
{
Vector3 NearestPoint = new Vector3();
float lastDistance = 99999999f;
for (int i = 0; i < verts.Length; i++)
{
float distance = GetDistance(point, verts[i]);
if (distance < lastDistance)
{
lastDistance = distance;
NearestPoint = verts[i];
}
}
return NearestPoint;
}
private float GetDistance(Vector3 start, Vector3 end)
{
return Mathf.Sqrt(Mathf.Pow((start.x - end.x), 2) + Mathf.Pow((start.y - end.y), 2) + Mathf.Pow((start.z - end.z), 2));
}
private void RaisePoint(Vector3 point, Vector3 val)
{
int index = -1;
for (int i = 0; i < verts.Length; i++)
{
if (verts[i] == point)
{
index = i;
break;
}
}
if (index == -1)
{
Debug.LogError("Could not match points");
}
else
{
Vector3 newPoint = verts[index];
newPoint += val;
verts[index] = newPoint;
// mf.mesh.Clear();
mf.mesh.vertices = verts;
mf.mesh.RecalculateNormals();
mf.mesh.RecalculateBounds();
}
}
}
Comment
Answer by kathibacher · Nov 02, 2019 at 02:04 PM
Hi, I'm working on a similar project right now. Unfortunately I haven't managed to make it work yet... have you found any solutions in the meantime? greets
Your answer
Follow this Question
Related Questions
Multiple Meshes 0 Answers
if I rotate an object, do they vertices of its mesh change? 1 Answer
Create a droplet wave effect using Mesh deformation 0 Answers
Edit SkinnedMeshRenderer 1 Answer