// Use this for initialization void Start() { segments = new List <CatmullRomSegment>(); spheres = new List <DraggablePoint>(); if (points.Count < 4) { createSpheres(); return; } if (randomize) { for (int i = 0; i < points.Count; i++) { points[i] = 2f * Random.insideUnitSphere; points[i] += 2.5f * Vector3.up; } } SetMaterial(DesignerController.instance.defaultLineMaterial); lineRender.SetColors(Color.white, Color.red); makeSegments(); createSpheres(); MoveEndpoints(); updateRenderPoints(); if (!containingCanvas) { containingCanvas = FindObjectOfType <SplineCanvas>(); containingCanvas.AddExistingSpline(this); } }
CatmullRomSpline AddLine(List <int> pointIndices) { Vector3[] points = new Vector3[pointIndices.Count]; for (int i = 0; i < points.Length; i++) { Vector3 pos = mesh.vertices[pointIndices[i]]; points[i] = pos; } GameObject lineObj = new GameObject(); lineObj.transform.parent = outputCanvas.transform; lineObj.transform.localPosition = Vector3.zero; lineObj.transform.localRotation = Quaternion.identity; lineObj.name = "skeleton-spline"; // Create spline from points // Transform to world space for (int i = 0; i < points.Length; i++) { points[i] = transform.rotation * points[i] + transform.position; } CatmullRomSpline crs = lineObj.AddComponent <CatmullRomSpline>(); crs.points = new List <Vector3>(); Vector3 firstDiff = points[1] - points[0]; Vector3 first = points[0]; points[0] += firstDiff.normalized * 0.1f; Vector3 lastDiff = points[points.Length - 2] - points[points.Length - 1]; Vector3 last = points[points.Length - 1]; points[points.Length - 1] += lastDiff.normalized * 0.1f; crs.points.Add(first); crs.points.AddRange(points); crs.points.Add(last); outputCanvas.AddExistingSpline(crs); /* * LineRenderer line = lineObj.AddComponent<LineRenderer>(); * line.useWorldSpace = false; * * line.SetVertexCount(points.Length); * line.SetPositions(points); * line.SetWidth(0.1f, 0.1f); * line.material = DesignerController.instance.defaultLineMaterial; * Color c = Color.HSVToRGB(hue, 1, 1); * line.SetColors(c, c); * hue = Mathf.Repeat(hue + 0.3f, 1); */ return(crs); }
public void ReplaceWithBulb(int index) { // Do nothing if this is an endpoint -- we can only insert // a bulb at an interior point. if (index <= 1) { return; } if (index >= points.Count - 2) { return; } List <Vector3> beforePoints = new List <Vector3>(); List <Vector3> afterPoints = new List <Vector3>(); // We want to copy all of the points up to the split position, // and make a new spline out of that. for (int i = 0; i < index; i++) { beforePoints.Add(points[i]); } // Add the split position as the last point beforePoints.Add(points[index]); // Add the last tangent handle as a point farther down the spline Vector3 middleBefore = sample(index + 0.5f); beforePoints.Add(middleBefore); // We also want to make a new spline of all the points // starting after the split position. // Add first tangent handle as coming before the split Vector3 middleAfter = sample(index - 0.5f); afterPoints.Add(middleAfter); // Add the split position as the first point afterPoints.Add(points[index]); for (int i = index + 1; i < points.Count; i++) { afterPoints.Add(points[i]); } // Make the splines CatmullRomSpline before = SplineOfPoints(beforePoints); containingCanvas.AddExistingSpline(before); CatmullRomSpline after = SplineOfPoints(afterPoints); containingCanvas.AddExistingSpline(after); // Connect splines to the bulb we just added DraggablePoint bulb = containingCanvas.AddBulb(points[index]); before.StartBulb = StartBulb; before.EndBulb = bulb; after.StartBulb = bulb; after.EndBulb = EndBulb; bulb.SetSize(0.1f); // Delete the current spline that was split containingCanvas.DeleteSpline(this); Destroy(gameObject); }