private void RebuildPath() { _CurvePoints = CRSpline3D.GeneratorPathControlPoints(_Path.Keys); if (!_Path.UseWorldSpace) { for (int i = 0; i < _CurvePoints.Length; i++) { _CurvePoints[i] = _Path.transform.TransformPoint(_CurvePoints[i]); } } _Path.transform.hasChanged = false; // rebuild render points int count = _Path.Length * _Path.SmoothAmount; if (_RenderPoints == null || _RenderPoints.Length != count + 1) { _RenderPoints = new Vector3[count + 1]; } _RenderPoints[0] = CRSpline3D.Interpolate(_CurvePoints, 0); for (int i = 1; i <= count; i++) { float pm = (float)i / count; _RenderPoints[i] = CRSpline3D.Interpolate(_CurvePoints, pm); } }
void _BtnSmoothPath_Click(object sender, System.EventArgs e) { Undo.RecordObject(_Path, "smooth path"); _Path.Keys = CRSpline3D.SmoothCurve(_Path.Keys, _Path.Interpolations); RebuildPath(); EditorUtility.SetDirty(_Path); }
private float CalcDistance(float startTime, float endTime, int resolution = 30) { float timeStep = (endTime - startTime) / resolution; float distance = 0; float time = startTime; for (int i = 0; i < resolution; i++) { distance += Vector3.Distance(CRSpline3D.Interpolate(_CurvePoints, time), CRSpline3D.Interpolate(_CurvePoints, time + timeStep)); time += timeStep; } return(distance); }
void OnEnable() { _Path = serializedObject.targetObject as CRSpline3D; if (_Path.Keys == null || _Path.Keys.Length < 2) { _Path.Keys = new Vector3[] { _Path.transform.position, _Path.transform.position + Vector3.forward } } ; if (_Path.Times == null || _Path.Times.Length != _Path.Keys.Length) { _Path.Times = new float[_Path.Keys.Length]; for (int i = 0; i < _Path.Keys.Length; i++) { _Path.Times[i] = i; } } _Path.ShowPath = false; _PreSmoothAmount = _Path.SmoothAmount = Mathf.Max(2, _Path.SmoothAmount); _PreUseWorldSpace = _Path.UseWorldSpace; RebuildPath(); CreateUI(); ValidateGrid(); _BtnRemove.IsEnabled = _Path.Keys.Length > 2; ValidateSelectedIndex(); _GridPoints.SelectedIndex = _Path.SelectedIndex; _SimulationStarted = false; _SimulationSpeed = 1.0f; _SimulationCurrectTime = 0; _Path.transform.hasChanged = false; } void OnDisable() { _Path.ShowPath = true; }
void OnSceneGUI() { //if (Event.current != null) //{ // Event e = Event.current; // if (e.isMouse && e.type == EventType.mouseDown && e.button == 0) // _GridPoints.SelectedIndex = CheckSelectedIndexByMouse(e.mousePosition); //} if (_CurvePoints != null && _Path != null) { Handles.color = _Path.Color; if (_RenderPoints != null) { Handles.DrawAAPolyLine(3, _RenderPoints); } if (_LabelStyle == null) { _LabelStyle = new GUIStyle(EditorStyles.largeLabel); _LabelStyle.fontSize = 12; _LabelStyle.fontStyle = FontStyle.Bold; } if (_Path.ShowPoints) { _LabelStyle.normal.textColor = _Path.Color; for (int i = 1; i < _CurvePoints.Length - 1; i++) { Vector3 point = _CurvePoints[i]; float handleSize = HandleUtility.GetHandleSize(point); point.y += _Path.PointRadius + handleSize * 0.25f; Handles.Label(point, (i - 1).ToString(), _LabelStyle); } } } int index = _Path.SelectedIndex; if (index >= 0) { bool changed = _Path.transform.hasChanged; Vector3 point = _Path.Keys[index]; if (!_Path.UseWorldSpace) { point = _Path.transform.TransformPoint(point); } Vector3 newPos = Handles.PositionHandle(point, Quaternion.identity); if (point != newPos) { if (!_Path.UseWorldSpace) { newPos = _Path.transform.InverseTransformPoint(newPos); } _VFValue.Value = _Path.Keys[index] = newPos; changed = true; } if (changed) { EditorUtility.SetDirty(_Path); RebuildPath(); ReloadProperties(); } } // ********************************* Simulation ************************************* if (_SimulationStarted) { if (_CurvePoints != null) { _SimulationCurrectTime += _SimulationSpeed / 120.0f; if (_SimulationCurrectTime >= _Path.TimeLength) { StopSimulation(); } else { Vector3 pos = CRSpline3D.Interpolate(_CurvePoints, _Path.ConvertToInterpolationTime(_SimulationCurrectTime)); Handles.color = Color.red; Handles.SphereCap(0, pos, Quaternion.identity, HandleUtility.GetHandleSize(pos) * 0.2f); SceneView.focusedWindow.Repaint(); } } } // ********************************* GUI ************************************* Handles.BeginGUI(); GUILayout.BeginArea(new Rect(Screen.width - 200, Screen.height - 90, 190, 120)); GUILayout.BeginVertical(); if (_SimulationStarted) { if (GUILayout.Button("Stop Simulation")) { StopSimulation(); } } else { if (GUILayout.Button("Start Simulation")) { StartSimulation(); } } GUILayout.BeginHorizontal(); GUILayout.Label(string.Format("Speed : {0:F2}", _SimulationSpeed), GUILayout.MaxWidth(80)); _SimulationSpeed = GUILayout.HorizontalSlider(_SimulationSpeed, 0.1f, 2.0f); GUILayout.EndHorizontal(); GUILayout.EndVertical(); GUILayout.EndArea(); Handles.EndGUI(); }