So I want implement a simple vertex painter editor tool, just for painting vertex color. First thing I’ve done is to show the vertex color and make the vertex can be selected & hovered, just like the Cloth component’s editor. Now I get the interactive result that I want. But the problem is the Handle’s drawing seems have a long delay. When my mouse hanging on top of a vertex, it takes seconds for it’s handle cube to become large. However when I try debugging, I found the handle’s size is set right after my mouse moves on it. So I’m guessing maybe I miss something of the Handle’s API? Please help me out of this.
here’s my editor code in scratch :
void OnSceneGUI()
{
Mesh mesh = container.mesh//container is the editor's target
Transform meshTransform = container.transform;
Event e = Event.current;
HandleUtility.AddDefaultControl(0);
RaycastHit raycastHit;
Ray mouseRay = HandleUtility.GUIPointToWorldRay(e.mousePosition);
mHoveredIndex = -1;
if (Physics.Raycast(mouseRay, out raycastHit, Mathf.Infinity) && raycastHit.transform == meshTransform)
{
if (raycastHit.barycentricCoordinate.x > 0.7f)
mHoveredIndex = mesh.triangles[raycastHit.triangleIndex * 3];
else if (raycastHit.barycentricCoordinate.y > 0.7f)
mHoveredIndex = mesh.triangles[raycastHit.triangleIndex * 3 + 1];
else if (raycastHit.barycentricCoordinate.z > 0.7f)
mHoveredIndex = mesh.triangles[raycastHit.triangleIndex * 3 + 2];
}
Handles.color = Color.black;
//Handles.lighting = false //if unparse this line, the handle will be invisible, don't know why
float size = HandleUtility.GetHandleSize(meshTransform.position) * 0.04f;
for (int i = 0; i < mesh.vertexCount; ++i)
{
if (mesh.colors.Length > i)
Handles.Color = mesh.colors[i];
Handles.CubeCap(0, meshTransform.TransformPoint(mesh.vertices[i]), Quaternion.identity, size);
}
Handles.color = new Color(0f, 0f, 0f, 0.5f);
if (mHoveredIndex > 0)
{
Handles.CubeCap(0, meshTransform.TransformPoint(mesh.vertices[mHoveredIndex]), Quaternion.identity, size * 2f);
}
Handles.BeginGUI();
GUI.Window(0, new Rect(Screen.width - 200, Screen.height - 150, 180, 100), OnDrawSceneEditor, "Mesh Editor");
Handles.EndGUI():
}