public override void OnInspectorGUI() { Color baseCol = GUI.color; spline = (CatmullRomSpline)target; CheckRotations(); EditorGUI.BeginChangeCheck(); { EditorGUILayout.Space(); Blue_BoldLable("Spline 基本设定"); SplineBasicSettings(); EditorGUILayout.Space(); ParentingSplineUI(); AddCatmullRomSegment(); EditorGUILayout.Space(); if (Button("Export as mesh")) { string path = EditorUtility.SaveFilePanelInProject("Save river mesh", "", "asset", "Save river mesh"); if (path.Length != 0 && spline.meshfilter.sharedMesh != null) { AssetDatabase.CreateAsset(spline.meshfilter.sharedMesh, path); AssetDatabase.Refresh(); spline.GenerateSpline(); } } } if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(spline, "Spline changed"); spline.GenerateSpline(); } EditorGUILayout.Space(); if (spline.beginningSpline) { if (!spline.beginningSpline.endingChildSplines.Contains(spline)) { spline.beginningSpline.endingChildSplines.Add(spline); } } if (spline.endingSpline) { if (!spline.endingSpline.beginnigChildSplines.Contains(spline)) { spline.endingSpline.beginnigChildSplines.Add(spline); } } }
private void DrawCurve(List <Vector2> controlPoints, int steps, int t) { if (controlPoints.Count >= 2) { List <Vector2> points = CatmullRomSpline.GenerateSpline(controlPoints, steps, t); Vector3 previousPoint = points[0]; for (int i = 1; i < points.Count; i++) { Gizmos.DrawLine(previousPoint, points[i]); previousPoint = points[i]; } } }
private void GenerateRiverBed(float[,] heightMap) { int resolution = heightMap.GetLength(0); // River width int riverWidth = (int)(resolution * riverWidthPercent) - 2; int maxRiverVariance = (int)(riverWidth * 0.17f); // Shore int shoreWidth = (int)(resolution * 0.1f); int maxShoreVariance = (int)(shoreWidth * 0.15f); // River indices int startOfRiver = (resolution - riverWidth) / 2; int endOfRiver = (resolution + riverWidth) / 2; // Generate control points List <Vector2> mainControlPoints = GenerateControlPoints(resolution, maxRiverVariance, 2); List <Vector2> leftControlPoints = AdjustControlPoints(mainControlPoints, startOfRiver); List <Vector2> rightControlPoints = AdjustControlPoints(mainControlPoints, endOfRiver); // Spline Curves List <Vector2> leftSplineCurve = CatmullRomSpline.GenerateSpline(leftControlPoints); List <Vector2> rightSplineCurve = CatmullRomSpline.GenerateSpline(rightControlPoints); // Interpolate rest of curve var leftRiver = ConnectRiverBends(leftSplineCurve); var rightRiver = ConnectRiverBends(rightSplineCurve); // Set left boundary foreach (Vector2Int leftBoundary in leftRiver) { heightMap[leftBoundary.x, leftBoundary.y] = riverDepth; } // Color left half of river, starting from center for (int y = 0; y < resolution; y++) { for (int i = resolution / 2; heightMap[i, y] != riverDepth; i--) { heightMap[i, y] = riverDepth; } } // Set right boundary foreach (Vector2Int rightBoundary in rightRiver) { heightMap[rightBoundary.x, rightBoundary.y] = riverDepth; } // Color until we hit right boundary, start from center for (int y = 0; y < resolution; y++) { for (int i = resolution / 2 + 1; heightMap[i, y] != riverDepth; i++) { heightMap[i, y] = riverDepth; } } }
private void DrawCurve(List <Vector3> controlPoints, int steps, int t) { if (controlPoints.Count >= 2) { List <Vector3> points = CatmullRomSpline.GenerateSpline(controlPoints, steps, t); Vector3 previousPoint = points[0]; for (int i = 1; i < points.Count; i++) { var rasterization = BresenhamLine.PlotLine(ToVector2Int(previousPoint), ToVector2Int(points[i])); foreach (var p in rasterization) { Gizmos.DrawCube(ToVector3(p), Vector3.one * 0.2f); } previousPoint = points[i]; } } }
void CheckRotations() { bool nan = false; //1. Create Rotation List if (spline.controlPointHandleRotations == null) { spline.controlPointHandleRotations = new List <Quaternion> (); nan = true; } //1. Add Rotation ---- make controlPoints == rotationPoints if (spline.controlPoints.Count > spline.controlPointHandleRotations.Count) { nan = true; for (int i = 0; i < spline.controlPoints.Count - spline.controlPointHandleRotations.Count; i++) { spline.controlPointHandleRotations.Add(Quaternion.identity); } } // Reset Rotations for (int i = 0; i < spline.controlPointHandleRotations.Count; i++) { //chech if the element is null if (float.IsNaN(spline.controlPointHandleRotations [i].x) || float.IsNaN(spline.controlPointHandleRotations [i].y) || float.IsNaN(spline.controlPointHandleRotations [i].z) || float.IsNaN(spline.controlPointHandleRotations [i].w)) { spline.controlPointHandleRotations [i] = Quaternion.identity; nan = true; } //Check if all element are 0 if (spline.controlPointHandleRotations [i].x == 0 && spline.controlPointHandleRotations [i].y == 0 && spline.controlPointHandleRotations [i].z == 0 && spline.controlPointHandleRotations [i].w == 0) { spline.controlPointHandleRotations [i] = Quaternion.identity; nan = true; } //spline.controlPointsRotations [i] = Quaternion.Euler(spline.controlPointsRotations [i].eulerAngles); } if (nan) { spline.GenerateSpline(); } }