public Bezier3D AddSegment() { GameObject go = GameObject.Instantiate(segmentPrefab, Vector3.zero, Quaternion.identity) as GameObject; go.transform.SetParent(this.transform, true); Bezier3D segment = go.GetComponent <Bezier3D>(); if (segment != null) { segments.Add(segment); } if (lastSegment != null) { segment.prevSegment = lastSegment; lastSegment.nextSegment = segment; segment.p1 = lastSegment.p4; segment.p2 += lastSegment.p4; segment.p3 += lastSegment.p4; segment.p4 += lastSegment.p4; lastSegment.UpdateMesh(); } segment.UpdateMesh(); lastSegment = segment; return(segment); }
public Mesh BuildMesh(float length, float radiusA, float radiusB, float roundnessA, float roundnessB, float angleA, float angleB, int heightSegments, int radialSegments) { MeshBuilder meshBuilder = new MeshBuilder(); // Test proportions here and log any debug or error messages if (length < radiusA || length < radiusB) { Debug.LogWarning("Warning: Bad body segment proportions. Length should be greater than minimum radius."); } // Determine cap segments based on height segments and relative lengths int capSegments = (int)Mathf.RoundToInt(((radiusA + radiusB) / (2 * length)) * heightSegments); // Define all curve points as 3D Vectors with 0 in z direction Vector3 A = new Vector3(0.0f, -roundnessA * radiusA, 0.0f); Vector3 B = new Vector3(radiusA, 0.0f, 0.0f); Vector3 C = new Vector3(radiusB, length, 0.0f); Vector3 D = new Vector3(0.0f, length + roundnessB * radiusB, 0.0f); // Distances between vectors float d1 = Vector3.Distance(A, B); float d2 = Vector3.Distance(B, C); float d3 = Vector3.Distance(C, D); Vector3 midAB1 = A + new Vector3(d1 / 3.0f, 0.0f, 0.0f); Vector3 midAB2 = B + new Vector3((d1 / 3.0f) * Mathf.Cos(Mathf.Deg2Rad * (angleA - 90)), (d1 / 3.0f) * Mathf.Sin(Mathf.Deg2Rad * (angleA - 90)), 0.0f); Vector3 midBC1 = B + new Vector3((d2 / 3.0f) * Mathf.Cos(Mathf.Deg2Rad * (angleA + 90)), (d2 / 3.0f) * Mathf.Sin(Mathf.Deg2Rad * (angleA + 90)), 0.0f); Vector3 midBC2 = C + new Vector3((d2 / 3.0f) * Mathf.Cos(Mathf.Deg2Rad * (angleB - 90)), (d2 / 3.0f) * Mathf.Sin(Mathf.Deg2Rad * (angleB - 90)), 0.0f); Vector3 midCD1 = C + new Vector3((d3 / 3.0f) * Mathf.Cos(Mathf.Deg2Rad * (angleB + 90)), (d3 / 3.0f) * Mathf.Sin(Mathf.Deg2Rad * (angleB + 90)), 0.0f); Vector3 midCD2 = D + new Vector3(d3 / 3.0f, 0.0f, 0.0f); // Define the 3 bezier curves to use Bezier3D capCurveA = new Bezier3D(A, midAB1, midAB2, B); Bezier3D mainCurve = new Bezier3D(B, midBC1, midBC2, C); Bezier3D capCurveB = new Bezier3D(C, midCD1, midCD2, D); // Loop along curve to define geometry for (int t = 0; t <= capSegments; t++) { Vector3 radiusVec = capCurveA.Point((float)t / (float)capSegments); float v = (float)t / (float)capSegments; BuildRing(meshBuilder, new Vector3(0.0f, radiusVec.y, 0.0f), radiusVec.x, radialSegments, v, t != 0); } for (int t = 1; t <= heightSegments; t++) { Vector3 radiusVec = mainCurve.Point((float)t / (float)heightSegments); float v = (float)t / (float)heightSegments; BuildRing(meshBuilder, new Vector3(0.0f, radiusVec.y, 0.0f), radiusVec.x, radialSegments, v, true); } for (int t = 1; t <= capSegments; t++) { Vector3 radiusVec = capCurveB.Point((float)t / (float)capSegments); float v = (float)t / (float)heightSegments; BuildRing(meshBuilder, new Vector3(0.0f, radiusVec.y, 0.0f), radiusVec.x, radialSegments, v, true); } return(meshBuilder.CreateMesh()); }
// Update is called once per frame void Update() { m_builder = new MeshBuilder(); List <BranchPoint> branchList = new List <BranchPoint>(); branchList.Add(new BranchPoint()); float branchLength = m_branchLength; // For as many branches are wanted for (int b = 0; b < m_branchNum; b++) { // For each branch... List <BranchPoint> newBranchList = new List <BranchPoint>(); foreach (BranchPoint branch in branchList) { // Build branches Vector3 finalPosition = Vector3.zero; Quaternion finalRotation = Quaternion.identity; for (int n = 0; n < 2; n++) { for (int i = 0; i < 8; i++) { Vector3 branchStart = branch.point; Quaternion branchRotation = Quaternion.FromToRotation(Vector3.up, branch.rotation * Quaternion.AngleAxis((2 * n - 1) * 45.0f + m_branchRotation, Vector3.forward) * Vector3.up); Vector3 branchEnd = branchStart + branchRotation * Vector3.up * branchLength; Vector3 branchMid1 = (branchEnd - branchStart) * 0.33f; Vector3 branchMid2 = (branchEnd - branchStart) * 0.67f; Bezier3D branchCurve = new Bezier3D(branchStart, branchMid1, branchMid2, branchEnd); float t = i / (8.0f - 1.0f); Vector3 ringPosition = branchCurve.Point(t); Quaternion rotation = Quaternion.FromToRotation(Vector3.up, branchCurve.Tangent(t)); BuildRing(m_builder, 8, ringPosition, m_startWidth, t, i != 0, rotation, Vector3.zero); finalPosition = ringPosition; finalRotation = rotation; } newBranchList.Add(new BranchPoint(finalPosition, finalRotation)); } } branchList.Clear(); branchList = newBranchList; branchLength *= m_branchLengthFalloff; } Mesh newMesh = m_builder.CreateMesh(); newMesh.RecalculateNormals(); MeshFilter filter = GetComponent <MeshFilter>(); filter.sharedMesh = newMesh; }
public Mesh BuildMesh(float length, float upper_width, float lower_width, int heightSegments, int radialSegments) { MeshBuilder meshBuilder = new MeshBuilder(); // Define bezier curve Vector3 start = Vector3.zero; Vector3 end = Vector3.up * length; Vector3 mid1 = start + Vector3.right * lower_width; Vector3 mid2 = end + Vector3.right * upper_width; for (int t = 0; t <= heightSegments; t++) { Bezier3D curve = new Bezier3D(start, mid1, mid2, end); Vector3 radiusVec = curve.Point((float)(t) / (float)heightSegments); //Vector3 radiusVec = Bezier(start,mid1,mid2,end,(float)(t)/(float)heightSegments); //if(radiusVec.x > maxRadius) {maxRadius = radiusVec.x;} //Debug.Log("radiusVec = " + radiusVec); float v = (float)t / (float)heightSegments; BuildRing(meshBuilder, /*offset+*/ new Vector3(0.0f, radiusVec.y, 0.0f), radiusVec.x, radialSegments, v, t != 0); } /* * start = end; * end = Vector3.up*height; * mid1 = start+Vector3.up*Random.Range(0.05f,0.5f)*height; * mid2 = end + Vector3.right*Random.Range(radiusGuide*0.25f,radiusGuide)+Vector3.up*Random.Range(-0.25f,0.25f)*height; * * for(int t = 1; t <= heightSegments/2; t++) { * Vector3 radiusVec = Bezier(start,mid1,mid2,end,(float)(t*2)/(float)heightSegments); * if(radiusVec.x > maxRadius) {maxRadius = radiusVec.x;} * //Debug.Log("radiusVec = " + radiusVec); * float v = (float)t/(float)heightSegments+0.5f; * BuildRing(meshBuilder,offset+new Vector3(0.0f,radiusVec.y,0.0f),radiusVec.x,radialSegments,v,true); * } */ return(meshBuilder.CreateMesh()); }
// 点をすべて登録した後に曲線を再計算します public void setup() { if (poses_.Count == 0) { // 点が定義出来ないけど認める dist_ = 0.0f; return; } if (poses_.Count == 1) { // 点だけどOK dist_ = 0.0f; return; } if (poses_.Count == 2) { // 直線だけどOK dist_ = (poses_[1] - poses_[0]).magnitude; return; } // 最初の2点間はsを計算 Vector3 p0 = poses_[0]; Vector3 p1 = poses_[1]; Vector3 p2 = poses_[2]; Vector3 p02 = p2 - p0; Vector3 p01 = p1 - p0; Vector3 en = -p02.normalized; float dot = Vector3.Dot(en, p01.normalized); float eLen = 0.25f; if (dot != 0.0f) { eLen = p01.magnitude / dot * -0.25f; } Vector3 e = en * eLen; Vector3 s = e + 0.5f * p01; var bezier = new Bezier3D(); bezier.setPoints(p0, p0 + s, p1 + e, p1); beziers_.Add(bezier); // 次からはsを前のeから算出 // 制御点までの長さは点間の半分 for (int i = 1; i < poses_.Count - 2; ++i) { p0 = poses_[i]; p1 = poses_[i + 1]; p2 = poses_[i + 2]; float len01 = (p1 - p0).magnitude * 0.5f; s = -e.normalized * len01; e = (p0 - p2).normalized * len01; bezier = new Bezier3D(); bezier.setPoints(p0, p0 + s, p1 + e, p1); beziers_.Add(bezier); } // 最後の2点間は特別 p0 = poses_[poses_.Count - 2]; p1 = poses_[poses_.Count - 1]; p01 = p1 - p0; dot = Vector3.Dot(-en, p01.normalized); eLen = 0.25f; if (dot != 0.0f) { eLen = p01.magnitude / dot * 0.25f; } s = -en * eLen; e = s - 0.5f * p01; bezier = new Bezier3D(); bezier.setPoints(p0, p0 + s, p1 + e, p1); beziers_.Add(bezier); foreach (var bz in beziers_) { dist_ += bz.getDist(); } }
// Update is called once per frame void Update() { //第一次曲线运动 if (flg == 0) { Fish_Rotation.transform.position = Vector2.MoveTowards(Fish_Rotation.transform.position, new Vector2(0, Fish_Rotation.transform.position.y), Time.deltaTime * 3.5f); if (Fish_Rotation.transform.position.x == 0 && Fish_Rotation.transform.position.y == pos.y) { flg = 1; path1 = Bezier3D.GetBeizerList(Fish_Rotation.transform.position, new Vector2(Fish_Rotation.transform.position.x + 1.3333f, Fish_Rotation.transform.position.y), new Vector2(Fish_Rotation.transform.position.x + 1.3333f, Fish_Rotation.transform.position.y - 2), new Vector2(Fish_Rotation.transform.position.x, Fish_Rotation.transform.position.y - 2), 10); } } if (flg == 1) { if (Fish_Rotation.transform.position.x == path1[i].x && Fish_Rotation.transform.position.y == path1[i].y) { if (i == 9) { i = 0; flg = 2; path2 = Bezier3D.GetBeizerList(Fish_Rotation.transform.position, new Vector2(Fish_Rotation.transform.position.x - 1.3333f, Fish_Rotation.transform.position.y), new Vector2(Fish_Rotation.transform.position.x - 1.3333f, Fish_Rotation.transform.position.y + 2), new Vector2(Fish_Rotation.transform.position.x, Fish_Rotation.transform.position.y + 2), 10); } else { i++; } } Fish_Rotation.transform.position = Vector2.MoveTowards(Fish_Rotation.transform.position, path1[i], Time.deltaTime * 3.5f); } if (flg == 2) { if (Fish_Rotation.transform.position.x == path2[i].x && Fish_Rotation.transform.position.y == path2[i].y) { if (i == 9) { i = 0; flg = 3; } else { i++; } } Fish_Rotation.transform.position = Vector2.MoveTowards(Fish_Rotation.transform.position, path2[i], Time.deltaTime * 3.5f); } if (flg == 3) { Fish_Rotation.transform.position = Vector2.MoveTowards(Fish_Rotation.transform.position, new Vector2(3.6f, Fish_Rotation.transform.position.y), Time.deltaTime * 3.5f); } Fish_Rotation.transform.Rotate(new Vector3(0, 0, -1), 10); //自我销毁 if (Fish_Rotation.transform.position.x > 3.5 || Fish_Rotation.transform.position.x < -3.5 || Fish_Rotation.transform.position.y < -4 || Fish_Rotation.transform.position.y > 4) { MyDestroy(); } //死亡 if (Hp < 0 && life) { life = false; PowerAndPoint(); FishDie.Play(); StartCoroutine("OnAtkDie"); } }
public override void OnPaint() { BackColor = Color.White; switch (ActiveSnapKind) { case Snapkind.SurfaceSnapItem: // Problem Ränder //if (SnappItems.Count >0) //{ // Emission = Color.Red; // SnappItems[0].DrawTriangleInfo(); // Emission = Color.Black; // return; //} CurveExtruder CurveEx = new CurveExtruder(); CurveEx.Direction = new xyz(0, 1, 1); CurveEx.Height = -1; CurveEx.Curve = new Bezier(new xy(9, 0), new xy(6, 3), new xy(3, 3), new xy(0, 0)); CurveEx.DownPlane = new Plane(new xyz(0, 0, 0), new xyz(0, 0, 1)); CurveEx.UpPlane = new Plane(new xyz(0, 0, 0) + CurveEx.Direction * 7, CurveEx.Direction); CurveEx.Paint(this); break; case Snapkind.LineSnapItem: drawCurve(new Line(new xy(0, 0), new xy(10, 0))); break; case Snapkind.PointSnapItem: drawPoint(new xyz(4, 3, 0), 0.2); break; case Snapkind.CurveSnapItem: Curve C = new Bezier(new xy(9, 0), new xy(6, 3), new xy(3, 3), new xy(0, 0)); PolygonMode = PolygonMode.Line; Emission = Color.Black; PenWidth = 3; drawCurve(C); PolygonMode = PolygonMode.Fill; break; case Snapkind.PolyCurveSnapItem: Lights[0].Position = new xyzwf(-10, -10, 10, 1); CurveArray _Curves0 = new CurveArray(); PolygonMode = PolygonMode.Line; _Curves0.Count = 4; _Curves0[0] = new Line(new xy(-3, -3), new xy(-3, 4)); _Curves0[1] = new Line(new xy(-3, 4), new xy(4, 4)); _Curves0[2] = new Line(new xy(4, 4), new xy(4, -3)); _Curves0[3] = new Bezier(new xy(4, -3), new xy(3, -3.5), new xy(1, -3.5), new xy(-3, -3)); drawPolyCurve(_Curves0); PolygonMode = PolygonMode.Fill; break; case Snapkind.PolyPolyCurveSnapItem: Loca _Loca = new Loca(); CurveArray Curves0 = new CurveArray(); Curves0.Count = 4; Curves0[0] = new Line(new xy(-3, -3), new xy(-3, 4)); Curves0[1] = new Line(new xy(-3, 4), new xy(4, 4)); Curves0[2] = new Line(new xy(4, 4), new xy(4, -3)); Curves0[3] = new Bezier(new xy(4, -3), new xy(3, -3.5), new xy(1, -3.5), new xy(-3, -3)); _Loca.Add(Curves0); OpenGlDevice.CheckError(); CurveArray Curves1 = new CurveArray(); Curves1.Count = 4; Curves1[0] = new Line(new xy(0, 0), new xy(1, 0)); Curves1[1] = new Line(new xy(1, 0), new xy(1, 1)); Curves1[2] = new Line(new xy(1, 1), new xy(0, 1)); Curves1[3] = new Line(new xy(0, 1), new xy(0, 0)); _Loca.Add(Curves1); PolygonMode = PolygonMode.Fill; drawPolyPolyCurve(_Loca); break; case Snapkind.TextSnapItem: Font.FontSize = 4; xy Pos = getEnvText(Font, "Drawing 3D"); drawText(Font, new xyz(-Pos.X / 2, 0, 0), "Drawing 3D", 2); break; case Snapkind.MeshSnapItem: int[] Indices = new int[] { 0, 1, 2 }; xyzf[] Position = new xyzf[] { new xyzf(0, 0, 0), new xyzf(3, 5, 0), new xyzf(6, 0, 0) }; xyzf[] Normal = new xyzf[] { new xyzf(0, 0, 1), new xyzf(0, 0, 1), new xyzf(0, 0, 1) }; xyf[] Texture = new xyf[] { new xyf(0, 0), new xyf(3, 5), new xyf(6, 0) }; xyzf[] Colors = new xyzf[] { new xyzf(1, 0, 0), new xyzf(0, 1, 0), new xyzf(0, 0, 1) }; drawMesh(Indices, Position, Normal, null, Colors); break; case Snapkind.PolyLineSnapItem: xyArray Poly = new xyArray(); Poly.data = new xy[] { new xy(1, 1), new xy(3, 4), new xy(5, 4), new xy(6, 2), new xy(1, 1) }; PolygonMode = PolygonMode.Fill; PenWidth = 3; drawPolyLine(Poly); PolygonMode = PolygonMode.Fill; break; case Snapkind.CurveSnapItem3D: Bezier3D Bezier = new Bezier3D(new xyz(1, 2, 1), new xyz(3, -2, 3), new xyz(5, 3, 2), new xyz(7, 1, 1)); drawCurve(Bezier); break; case Snapkind.PolyLineSnapItem3D: xyzArray Poly3d = new xyzArray(); Poly3d.data = new xyz[] { new xyz(0, 0, 1), new xyz(0, 3, 1), new xyz(3, 3, 1), new xyz(4, 0, 1), new xyz(0, 0, 1) }; drawPolyLine(Poly3d); break; case Snapkind.PolyPolyLineSnapItem3D: xyzArray AA = new xyzArray(); AA.Add(new xyz(1, 1, 1)); AA.Add(new xyz(0, 1, 1)); AA.Add(new xyz(0, 0, 1)); AA.Add(new xyz(1, 0, 1)); AA.Add(new xyz(1, 1, 1)); xyzArray BB = new xyzArray(); BB.Add(new xyz(3, 3, 1)); BB.Add(new xyz(3, -2, 1)); BB.Add(new xyz(-1, -2, 1)); BB.Add(new xyz(-1, 3, 1)); BB.Add(new xyz(3, 3, 1)); Loxyz L = new Loxyz(); L.Add(AA); L.Add(BB); drawPolyPolyLine(L); break; case Snapkind.PolyPolyLineSnapItem: xyArray _AA = new xyArray(); _AA.Add(new xy(2, 0.5)); _AA.Add(new xy(0, 0.5)); _AA.Add(new xy(0, 0)); _AA.Add(new xy(2, 0)); _AA.Add(new xy(2, 0.5)); xyArray _BB = new xyArray(); _BB.Add(new xy(4, 4)); _BB.Add(new xy(4, -2)); _BB.Add(new xy(-1, -2)); _BB.Add(new xy(-1, 4)); _BB.Add(new xy(4, 4)); Loxy _L = new Loxy(); _L.Add(_AA); _L.Add(_BB); drawPolyPolyLine(_L); break; case Snapkind.SphereSnapItem: drawSphere(new xyz(2, 1, 1), 3); break; case Snapkind.BoxSnapItem: drawBox(new xyz(0, 0, 0), new xyz(4, 3, 4)); break; default: break; } ACursor.Paint(this); }
public void FixedUpdate() { if (time <= timerToStart) { time += Time.fixedDeltaTime; if (time > timerToStart) { GetComponent <Rigidbody>().useGravity = true; } } else { if (this.transform.position.y <= -20) { if (m_pathIndex - 1 < 0) { this.transform.position = new Vector3(m_currentBezier.prevSegment.GetPath()[m_currentBezier.prevSegment.GetPath().Length - 1].position.x, m_currentBezier.prevSegment.GetPath()[m_currentBezier.prevSegment.GetPath().Length - 1].position.y + 1, m_currentBezier.prevSegment.GetPath()[m_currentBezier.prevSegment.GetPath().Length - 1].position.z); } else { this.transform.position = new Vector3(m_currentBezier.GetPath()[m_pathIndex - 1].position.x, m_currentBezier.GetPath()[m_pathIndex - 1].position.y + 1, m_currentBezier.GetPath()[m_pathIndex - 1].position.z); } this.transform.LookAt(m_currentBezier.GetPath()[m_pathIndex].position); } float motor = maxMotorTorque; float steering = 0; Vector3 targetPos = new Vector3(m_currentBezier.GetPath()[m_pathIndex].position.x, 0, m_currentBezier.GetPath()[m_pathIndex].position.z); Vector3 myPos = new Vector3(this.transform.position.x, 0, this.transform.position.z); m_targetDir = targetPos - myPos; if (m_targetDir.sqrMagnitude < 100) { m_pathIndex++; if (m_pathIndex >= m_currentBezier.GetPath().Length) { m_pathIndex = 0; m_currentBezier = m_currentBezier.nextSegment; } } if (playerControlled) { steering = maxSteeringAngle * CrossPlatformInputManager.GetAxis("Horizontal"); } else { m_targetDir = targetPos - myPos; float angleBetween = 0.0F; angleBetween = Vector3.Angle(this.transform.right, m_targetDir); float modifier = 0; if (angleBetween - 90 > 5) { modifier = -1; } else if (angleBetween - 90 < -5) { modifier = 1; } steering = modifier * maxSteeringAngle; } foreach (AxleInfo axleInfo in axleInfos) { if (axleInfo.steering) { axleInfo.leftWheel.steerAngle = steering; axleInfo.rightWheel.steerAngle = steering; } if (axleInfo.motor) { axleInfo.leftWheel.motorTorque = motor; axleInfo.rightWheel.motorTorque = motor; } //Braking if (braking) { axleInfo.leftWheel.brakeTorque = brakeForce; axleInfo.rightWheel.brakeTorque = brakeForce; } else { axleInfo.leftWheel.brakeTorque = 0; axleInfo.rightWheel.brakeTorque = 0; } ApplyLocalPositionToVisuals(axleInfo.leftWheel); ApplyLocalPositionToVisuals(axleInfo.rightWheel); } } }
public void OnSceneGUI() { Bezier3D curve = target as Bezier3D; Transform handleTransform = curve.transform; Quaternion handleRotation = handleTransform.rotation; Vector3 p1 = handleTransform.TransformPoint(curve.p1); Vector3 p2 = handleTransform.TransformPoint(curve.p2); Vector3 p3 = handleTransform.TransformPoint(curve.p3); Vector3 p4 = handleTransform.TransformPoint(curve.p4); if (curve.prevSegment != null) { // P1 EditorGUI.BeginChangeCheck(); handleRotation = Handles.DoRotationHandle(handleRotation, p1); Handles.color = Color.red; Handles.SphereCap(0, p1, Quaternion.identity, 3f); if (EditorGUI.EndChangeCheck()) { curve.p1 = handleTransform.InverseTransformPoint(p1); float rotation = handleRotation.eulerAngles.z; if (rotation > 50 && rotation < 180) { rotation = 50; } if (rotation > 180 && rotation < 310) { rotation = 310; } if (rotation >= 310) { rotation = -360 + rotation; } curve.rotationP1 = rotation; curve.ReDrawMesh(); curve.UpdateMesh(); } // P2 EditorGUI.BeginChangeCheck(); p2 = Handles.DoPositionHandle(p2, handleRotation); Handles.color = Color.gray; Handles.SphereCap(0, p2, Quaternion.identity, 1.5f); if (EditorGUI.EndChangeCheck()) { curve.p2 = handleTransform.InverseTransformPoint(p2); curve.ReDrawMesh(); } Handles.color = Color.gray; Handles.DrawLine(p1, p2); } // P3 EditorGUI.BeginChangeCheck(); p3 = Handles.DoPositionHandle(p3, handleRotation); Handles.color = Color.gray; Handles.SphereCap(0, p3, Quaternion.identity, 1.5f); if (EditorGUI.EndChangeCheck()) { curve.p3 = handleTransform.InverseTransformPoint(p3); curve.UpdateMesh(); } // P4 EditorGUI.BeginChangeCheck(); p4 = Handles.DoPositionHandle(p4, handleRotation); Handles.color = Color.gray; Handles.SphereCap(0, p4, Quaternion.identity, 3f); if (EditorGUI.EndChangeCheck()) { curve.p4 = handleTransform.InverseTransformPoint(p4); curve.UpdateMesh(); } Handles.color = Color.gray; Handles.DrawLine(p3, p4); }