public void DeformAxis(Axis axis) { Deformer deformer = m_editor.Selection.activeGameObject.GetComponent <Deformer>(); if (deformer == null) { m_editor.Undo.BeginRecord(); m_editor.Undo.AddComponent(m_editor.Selection.activeGameObject.GetComponent <ExposeToEditor>(), typeof(Deformer)); m_editor.Undo.CreateRecord(redo => { EnableSplineRenderers(Mode == MeshDeformerToolMode.ControlPoint); return(false); }, undo => false); m_editor.Undo.EndRecord(); } else { BaseSplineState oldState = deformer.GetState(); PickResult oldSelection = m_picker.Selection != null ? new PickResult(m_picker.Selection) : null; deformer.Axis = axis; BaseSplineState newState = deformer.GetState(); PickResult newSelection = m_picker.Selection != null ? new PickResult(m_picker.Selection) : null; RecordState(deformer.gameObject, oldState, newState, m_picker, oldSelection, newSelection); } EnableSplineRenderers(false); }
private void OnDeformerRefreshed(Deformer obj) { if (obj.gameObject == m_editor.Selection.activeObject) { UpdateSelection(); } }
public void DeformAxis(Axis axis) { Deformer meshDeformer = m_editor.Selection.activeGameObject.GetComponent <Deformer>(); if (meshDeformer == null) { m_editor.Undo.BeginRecord(); GameObject go = m_editor.Selection.activeGameObject; m_editor.Undo.AddComponent(go.GetComponent <ExposeToEditor>(), typeof(Deformer)); meshDeformer = go.GetComponent <Deformer>(); meshDeformer.Axis = axis; m_editor.Undo.CreateRecord(redo => { Deformer deformer = go.GetComponent <Deformer>(); deformer.Axis = axis; EnableSplineRenderers(Mode == MeshDeformerToolMode.ControlPoint); GetSelectedDeformer(); return(false); }, undo => { return(false); } ); m_editor.Undo.EndRecord(); GetSelectedDeformer(); } EnableSplineRenderers(false); }
public void Deform(Deformer deformer, Mesh original, Mesh colliderOriginal, bool isRigid) { if (original != null) { Mesh mesh = Mesh; if (mesh != null) { mesh.vertices = Deform(m_slices, original, deformer, isRigid); mesh.RecalculateBounds(); mesh.RecalculateNormals(); } } if (colliderOriginal != null && m_meshCollider != null) { Mesh colliderMesh = ColliderMesh; if (colliderMesh != null) { colliderMesh.vertices = Deform(m_colliderSlices, colliderOriginal, deformer, isRigid); colliderMesh.RecalculateBounds(); colliderMesh.RecalculateNormals(); m_meshCollider.sharedMesh = null; m_meshCollider.sharedMesh = colliderMesh; } } if (Deformed != null) { Deformed(deformer, this); } }
private void GetSelectedDeformer() { m_selectedDeformer = m_controlPointPicker.Selection != null && m_controlPointPicker.Selection.Spline != null?m_controlPointPicker.Selection.Spline.GetComponentInParent <Deformer>() : null; if (m_selectedDeformer == null) { m_selectedDeformer = m_editor.Selection.activeGameObject != null?m_editor.Selection.activeGameObject.GetComponentInParent <Deformer>() : null; } }
public void Destroy() { GameObject go = m_editor.Selection.activeGameObject; GameObject copy = Instantiate(go); copy.name = go.name; Deformer deformer = copy.GetComponent <Deformer>(); Destroy(deformer); m_editor.Undo.BeginRecord(); m_editor.RegisterCreatedObjects(new[] { copy }, true); m_editor.Delete(new[] { go }); m_editor.Undo.EndRecord(); }
public void SlerpContacts(Deformer deformer, Mesh original, Mesh colliderOriginal, Segment prev, Segment next, bool isRigid) { if (isRigid) { return; } Mesh prevMesh = null; Mesh nextMesh = null; if (prev != null) { prevMesh = prev.Mesh; } if (next != null) { nextMesh = next.Mesh; } SlerpContacts(deformer, Mesh, deformer.Contacts, prev, prevMesh, next, nextMesh); if (colliderOriginal == null) { return; } if (prev != null) { prevMesh = prev.ColliderMesh; } if (next != null) { nextMesh = next.ColliderMesh; } SlerpContacts(deformer, ColliderMesh, deformer.ColliderContacts, prev, prevMesh, next, nextMesh); if (m_meshCollider != null) { Mesh colliderMesh = ColliderMesh; m_meshCollider.sharedMesh = null; m_meshCollider.sharedMesh = colliderMesh; } }
public void Deform(Deformer deformer, Mesh original, Mesh colliderOriginal, bool isRigid) { if (original != null) { m_mesh.vertices = Deform(m_slices, original, deformer, isRigid); m_mesh.RecalculateBounds(); m_mesh.RecalculateNormals(); } if (colliderOriginal != null && m_collider != null) { m_colliderMesh.vertices = Deform(m_colliderSlices, colliderOriginal, deformer, isRigid); m_colliderMesh.RecalculateBounds(); m_colliderMesh.RecalculateNormals(); m_collider.sharedMesh = null; m_collider.sharedMesh = m_colliderMesh; } }
private Vector3[] Deform(Slice[] slices, Mesh mesh, Deformer deformer, bool isRigid) { Vector3[] vertices = mesh.vertices; for (int s = 0; s < slices.Length; ++s) { Slice slice = slices[s]; Vector3 center = deformer.GetPosition(slice.SegmentIndex, slice.T); center = deformer.transform.InverseTransformPoint(center); Vector3 dir = deformer.transform.InverseTransformVector(deformer.GetDirection(slice.SegmentIndex, slice.T)); float t = slice.T; if (isRigid) { t = 1.0f; } if (dir == Vector3.zero) { continue; } float twistAngle = deformer.GetTwist(slice.SegmentIndex, t); Vector3 thickness = deformer.GetThickness(slice.SegmentIndex, t); float wrapCurvature = deformer.GetWrap(slice.SegmentIndex, t); Quaternion rotation = Quaternion.AngleAxis(twistAngle, dir) * Quaternion.LookRotation(dir, m_up) * m_axisRotation; Matrix4x4 matrix = Matrix4x4.TRS(center, rotation, Vector3.one); int[] indices = slice.Indices; if (indices != null) { for (int i = 0; i < indices.Length; ++i) { int index = indices[i]; Vector3 vertex = vertices[index]; vertex = AxisTransform(deformer, vertex, slice.Center, thickness); vertex = WrapAroundAxisTransform(deformer, vertex, slice.Center, wrapCurvature); vertex = matrix.MultiplyPoint(vertex); vertices[index] = vertex; } } } return(vertices); }
private static Vector3 WrapAroundAxisTransform(Deformer deformer, Vector3 vertex, Vector3 center, float curvature = 0.1f) { Vector3 result = vertex; if (deformer.Axis == Axis.X) { result.y += Mathf.Pow((vertex.z - center.z), 2) * curvature * 0.01f; } else if (deformer.Axis == Axis.Y) { result.z += Mathf.Pow((vertex.x - center.x), 2) * curvature * 0.01f; } else { result.y += Mathf.Pow((vertex.x - center.x), 2) * curvature * 0.01f; } return(result); }
private void RecordState(GameObject spline, BaseSplineState oldState, BaseSplineState newState, ControlPointPicker picker, PickResult oldSelection, PickResult newSelection) { m_editor.Undo.CreateRecord(record => { Deformer deformer = spline.GetComponent <Deformer>(); deformer.SetState(newState); picker.Selection = newSelection; return(true); }, record => { Deformer deformer = spline.GetComponent <Deformer>(); deformer.SetState(oldState); picker.Selection = oldSelection; return(true); }); }
private static Vector3 AxisTransform(Deformer deformer, Vector3 vertex, Vector3 center, Vector3 scale) { Vector3 toVertex = vertex - center; if (deformer.Axis == Axis.X) { toVertex.x = 0; center.x = vertex.x - center.x; } else if (deformer.Axis == Axis.Y) { toVertex.y = 0; center.y = vertex.y - center.y; } else { toVertex.z = 0; center.z = vertex.z - center.z; } return(center + Vector3.Scale(toVertex, scale)); }
private void SlerpContacts(Deformer deformer, Mesh mesh, Contact[] contacts, Segment prev, Mesh prevMesh, Segment next, Mesh nextMesh) { Vector3[] normals = null; Vector3[] prevNormals = null; Vector3[] nextNormals = null; if (mesh == null) { return; } if (prev != null || next != null) { normals = mesh.normals; } if (prevMesh != null && prev != null && (prev != this || deformer.SegmentsCount == 1 && deformer.IsLooping)) { prevNormals = prevMesh.normals; for (int i = 0; i < contacts.Length; ++i) { Contact contact = contacts[i]; Vector3 prevNormal = prevNormals[contact.Index2]; Vector3 normal = normals[contact.Index1]; Vector3 slerped = Vector3.Slerp(prevNormal, normal, 0.5f); prevNormals[contact.Index2] = slerped; normals[contact.Index1] = slerped; } } if (nextMesh != null && next != null && (next != this || deformer.SegmentsCount == 1 && deformer.IsLooping)) { nextNormals = nextMesh.normals; for (int i = 0; i < contacts.Length; ++i) { Contact contact = contacts[i]; Vector3 normal = normals[contact.Index2]; Vector3 nextNormal = nextNormals[contact.Index1]; Vector3 slerped = Vector3.Slerp(normal, nextNormal, 0.5f); normals[contact.Index2] = slerped; nextNormals[contact.Index1] = slerped; } } if (prev != null) { if (mesh != null) { mesh.normals = normals; } if (this != prev) { if (prevMesh != null) { prevMesh.normals = prevNormals; } } if (next != null && next != prev) { if (nextMesh != null) { nextMesh.normals = nextNormals; } } } else if (next != null) { if (mesh != null) { mesh.normals = normals; } if (prev != null && prev != next) { if (prevMesh != null) { prevMesh.normals = prevNormals; } } if (this != next) { if (nextMesh != null) { nextMesh.normals = nextNormals; } } } }
public void Subdivide() { Deformer meshDeformer = m_editor.Selection.activeGameObject.GetComponentInParent <Deformer>(); MeshFilter meshFilter = meshDeformer.GetComponent <MeshFilter>(); MeshCollider meshCollider = meshDeformer.GetComponent <MeshCollider>(); if (meshFilter.sharedMesh != null) { Mesh newMesh = MeshSubdivider.Subdivide(meshFilter.sharedMesh, 2); Mesh oldMesh = Instantiate(meshFilter.sharedMesh); oldMesh.name = meshFilter.sharedMesh.name; newMesh.name = meshFilter.sharedMesh.name; meshFilter.sharedMesh = null; meshFilter.sharedMesh = Instantiate(newMesh); if (meshCollider != null) { meshCollider.sharedMesh = null; meshCollider.sharedMesh = meshFilter.sharedMesh; } meshDeformer.Refresh(); GameObject go = meshDeformer.gameObject; m_editor.Undo.CreateRecord(redo => { Mesh newMeshInstance = Instantiate(newMesh); newMeshInstance.name = newMesh.name; MeshFilter filter = go.GetComponent <MeshFilter>(); Destroy(filter.sharedMesh); filter.sharedMesh = newMeshInstance; MeshCollider collider = go.GetComponent <MeshCollider>(); if (collider != null) { Destroy(collider.sharedMesh); collider.sharedMesh = newMeshInstance; } Deformer deformer = go.GetComponent <Deformer>(); deformer.Refresh(); return(true); }, undo => { Mesh oldMeshInstance = Instantiate(oldMesh); oldMeshInstance.name = oldMesh.name; MeshFilter filter = go.GetComponent <MeshFilter>(); Destroy(filter.sharedMesh); filter.sharedMesh = oldMeshInstance; MeshCollider collider = go.GetComponent <MeshCollider>(); if (collider != null) { Destroy(collider.sharedMesh); collider.sharedMesh = oldMeshInstance; } Deformer deformer = go.GetComponent <Deformer>(); deformer.Refresh(); return(true); }, purge => { Destroy(newMesh); Destroy(oldMesh); }); } }
public void SelectControlPoint(Camera camera, Vector2 point) { if (m_selectionComponentState == null) { return; } if (m_selectionComponentState.Component.IsPositionHandleEnabled) { BaseSpline spline = m_controlPointPicker.Selection != null?m_controlPointPicker.Selection.GetSpline() : null; if (spline != null && !(spline is Deformer)) { return; } m_editor.Undo.BeginRecord(); PickResult oldSelection = m_controlPointPicker.Selection != null ? new PickResult(m_controlPointPicker.Selection) : null; PickResult newSelection = m_controlPointPicker.Pick(camera, point); if (newSelection != null && newSelection.Spline != null && newSelection.Spline.GetComponent <Deformer>() == null) { newSelection = null; } GameObject deformerGo = null; if (newSelection == null) { RaycastHit hit; if (Physics.Raycast(camera.ScreenPointToRay(point), out hit)) { Segment segment = hit.collider.GetComponent <Segment>(); if (segment != null) { Deformer deformer = segment.GetComponentInParent <Deformer>(); if (deformer != null) { deformerGo = deformer.gameObject; } } } } if (deformerGo != null) { m_editor.Selection.Select(deformerGo, new[] { deformerGo }); } m_controlPointPicker.ApplySelection(newSelection, true); newSelection = newSelection != null ? new PickResult(newSelection) : null; m_editor.Undo.CreateRecord(record => { m_controlPointPicker.Selection = newSelection; if (deformerGo != null) { m_editor.Selection.Select(deformerGo, new[] { deformerGo }); } return(true); }, record => { m_controlPointPicker.Selection = oldSelection; if (deformerGo != null) { m_editor.Selection.Select(deformerGo, new[] { deformerGo }); } return(true); }); m_editor.Undo.EndRecord(); } }