private void HandleSelection(LatticeGridComponent lattice, Transform transform) { if (selectedInfo.HasSelection) { using (var cc = new EditorGUI.ChangeCheckScope()) { var handlePos = CalcSelectionCenter(lattice); handlePos = transform.TransformPoint(handlePos); var newV = Handles.PositionHandle(handlePos, transform.rotation); if (cc.changed) { Undo.RecordObject(lattice, "Lattice Vertex Change"); Vector3 delta = newV - handlePos; delta = transform.worldToLocalMatrix * delta; var indices = selectedInfo.SelectedPointsIndices; foreach (int i in indices) { lattice.Grid.Vertices[i] += delta; } if (liveEvaluation) { if (lattice.HasSnapshot) { lattice.UpdateTargetSnapshotVertices(); } } } } } }
void Draw(LatticeGridComponent lattice) { Camera cam = SceneView.lastActiveSceneView.camera; var verts = lattice.Grid.Vertices; Vector3 locCam = lattice.transform.InverseTransformPoint(cam.transform.position); float closeDist = float.MaxValue; float farDist = 0; for (int i = 0; i < verts.Length; i++) { float dist = Vector3.Distance(verts[i], locCam); closeDist = Mathf.Min(closeDist, dist); farDist = Mathf.Max(farDist, dist); } using (var scope = new Handles.DrawingScope(lattice.transform.localToWorldMatrix)) { var handleNormal = cam == null ? Vector3.up : cam.transform.forward; handleNormal = lattice.transform.InverseTransformDirection(handleNormal); var grid = lattice.Grid; for (int i = 0; i < verts.Length; i++) { int forward = grid.StepIndex(i, 0, 0, 1); int right = grid.StepIndex(i, 0, 1, 0); int up = grid.StepIndex(i, 1, 0, 0); float alpha = drawOpaque ? 1 : CalcAlpha(verts[i]); if (forward >= 0) { DrawLine(i, forward, verts, alpha); } if (right >= 0) { DrawLine(i, right, verts, alpha); } if (up >= 0) { DrawLine(i, up, verts, alpha); } DrawPoint(i, verts[i], handleNormal, alpha); } needsRepaint = false; } float CalcAlpha(Vector3 p) { float dist = Vector3.Distance(p, locCam); var a = 1.0f - ((dist - closeDist) / (farDist - closeDist)); return(a * a * a); } }
private void CheckForPropertyChange(LatticeGridComponent lattice) { if (lattice.XLength != lattice.Grid.XLength || lattice.YLength != lattice.Grid.YLength || lattice.ZLength != lattice.Grid.ZLength) { lattice.ResizeGrid(lattice.XLength, lattice.YLength, lattice.ZLength); selectedInfo.ClearSelection(); } if (lattice.CellSize != lattice.Grid.CellSize) { // this looks strange, but the Vector3 value can be set from the editor // which won't call the setter function, so force it if the cellSize changed lattice.CellSize = lattice.CellSize; } }
private Vector3 CalcSelectionCenter(LatticeGridComponent lattice) { var verts = lattice.Grid.Vertices; var selected = selectedInfo.SelectedPointsIndices; if (selected.Count == 1) { return(verts[selected[0]]); } else if (selected.Count > 1) { Vector3 center = Vector3.zero; foreach (var i in selected) { center += verts[i]; } center /= selected.Count; return(center); } return(Vector3.zero); }
void UpdateMouseOverInfo(Vector3 mousePosition, LatticeGridComponent lattice) { selectedInfo.NotOverAnything(); var tNormal = lattice.transform.up; Camera cam = SceneView.lastActiveSceneView.camera; Vector3 camPos = cam.transform.position; var candidates = new List <KeyValuePair <int, float> >(); var grid = lattice.Grid; var verts = lattice.Grid.Vertices; for (int i = 0; i < verts.Length; i++) { var p = lattice.transform.TransformPoint(verts[i]); var screenPos = HandleUtility.WorldToGUIPoint(p); if (Vector2.Distance(mousePosition, screenPos) < PointSelectionDistance) { candidates.Add(new KeyValuePair <int, float>(i, Vector3.Distance(p, camPos))); } } if (candidates.Count > 0) { float closeDist = candidates[0].Value; int closeInd = candidates[0].Key; for (int i = 1; i < candidates.Count; ++i) { if (candidates[i].Value < closeDist) { closeDist = candidates[i].Value; closeInd = candidates[i].Key; } } selectedInfo.MouseOverPoint(closeInd); } }
void HandleInput(Event guiEvent, LatticeGridComponent lattice) { if (guiEvent.button == LeftClick) { if (guiEvent.type == EventType.MouseDown) { bool additiveSelect = IsAdditiveModifier(guiEvent.modifiers); HandleLeftMouseDown(additiveSelect); } else if (guiEvent.type == EventType.MouseUp) { if (clearSelection) { clearSelection = false; selectedInfo.ClearSelection(); needsRepaint = true; } } } UpdateMouseOverInfo(guiEvent.mousePosition, lattice); }
private void DrawScreenGUI(LatticeGridComponent lattice) { Handles.BeginGUI(); int menuWidth = menuOpen ? 140 : 80; GUILayout.BeginArea(new Rect(5, 5, menuWidth, 160)); GUILayout.BeginVertical(); if (GUILayout.Button(menuOpen ? "<<<" : "menu >>>", GUILayout.Height(20))) { menuOpen = !menuOpen; } if (menuOpen) { if (GUILayout.Button(drawOpaque ? "transparent": "opaque")) { drawOpaque = !drawOpaque; needsRepaint = true; } if (GUILayout.Button(allowSelection ? "disallow selection" : "allow selection")) { allowSelection = !allowSelection; selectedInfo.ClearSelection(); needsRepaint = true; } if (GUILayout.Button("reset grid")) { lattice.ResetVerticesPosition(); needsRepaint = true; } GUI.enabled = lattice.target != null; string label = GUI.enabled ? "take snapshot" : "take snapshot (no target)"; if (GUILayout.Button(label)) { lattice.TakeTargetSnapshot(); } GUILayout.Space(10); GUI.enabled = true; label = "live evaluation:" + (liveEvaluation ? "on" : "off"); if (GUILayout.Button(label)) { liveEvaluation = !liveEvaluation; } GUI.enabled = true; label = "evaluate vertices"; if (!lattice.HasSnapshot) { GUI.enabled = false; label = "evaluate vertices (no snapshot)"; } if (GUILayout.Button(label)) { lattice.UpdateTargetSnapshotVertices(); } GUI.enabled = true; } GUILayout.EndVertical(); GUILayout.EndArea(); Handles.EndGUI(); }