- Home /
Handles does not modify mesh...
Hello everyone,
I'm trying to make a simple custom editor so I'm able to modify a mesh.
Here's the code:
[CustomEditor(typeof(MeshFilter))]
public class MeshEditor : Editor
{
private MeshFilter _meshF;
private Mesh _mesh;
private GameObject _obj;
private Vector3 meshPos;
void Awake()
{
_meshF = (MeshFilter)target;
_obj = _meshF.gameObject;
_mesh = _meshF.sharedMesh;
meshPos = _obj.transform.TransformPoint(_mesh.vertices[0]);
}
void OnSceneGUI()
{
Handles.Label(_obj.transform.position + Vector3.up * 2, "Verts: " + _mesh.vertexCount + " Tris: " + _mesh.triangles.Length);
//for (var i = 0; i < mesh.vertexCount; i++)
//{
// mesh.vertices[i] = Handles.PositionHandle(obj.transform.TransformPoint(mesh.vertices[i]), Quaternion.identity);
//}
meshPos = Handles.PositionHandle(meshPos, Quaternion.identity);
if (GUI.changed)
{
_mesh.vertices[0] = meshPos;
EditorUtility.SetDirty(target);
}
}
}
I can move the handle but the mesh is not changing... any ideas?
Thanks
Answer by Aram-Azhari · Jun 22, 2014 at 06:16 AM
I have asked the exact question in the forums. My answer according to Here:
Update the whole vertex array instead just updating an element of it.
So add
private Vector3[] _verts;
In Awake() add:
_verts = _mesh.vertices;
And in OnSceneGUI() modify the line:
_mesh.vertices[0] = meshPos;
to
_verts[0] = meshPos;
_mesh.vertices = verts;
:)
Thanks! works like a charm.
I'm having another issue tho, maybe you can help me.
It seems that the vert positions are effected by my objects position in a weird way.
If the object's position is at (0,0,0) then it works perfectly, but but if the position is different, the mesh jumps off as soon as i move any of the handles.
Any ideas?
You'll need to use Transform.TransformPoint and Transform.InverseTransformPoint to find the vertex position in world space, modify it, then convert back to local space
Thanks @alucardj.
I've managed to solve both position and scaling issues I had.
But now I'm facing an issue that is beyond my math skills.
It is basically the same issue like in position and scale.
If the value is not 0, the vertices are being added the value.
For example: if my starting rotation is 45, my object jumps so its now 90.
So I need to somehow inverse the rotation, but quaternions is something I'm clueless about.
Any help will be appreciated.
@alucardj, looks like InverseTransformPoint fixes the rotation too.
I solved it differently before seeing your answer, but then decided to check your suggestion.
from me :)
Your answer
Follow this Question
Related Questions
Flattening a mesh by deforming it 0 Answers
How do you bend a cylindrical gameobject? 1 Answer
Mesh deformation simulating a fire 0 Answers
Mesh Deformation 2 Answers
Expanding mesh animation 0 Answers