public static Vector3 Velocity(Path pts, float t, EasingType ease = EasingType.Linear, bool easeIn = true, bool easeOut = true) { t = Ease(t); if (pts.Length == 0) { return(Vector3.zero); } else if (pts.Length == 1) { return(pts[0]); } else if (pts.Length == 2) { return(Vector3.Lerp(pts[0], pts[1], t)); } else if (pts.Length == 3) { return(QuadBez.Velocity(pts[0], pts[2], pts[1], t)); } else if (pts.Length == 4) { return(CubicBez.Velocity(pts[0], pts[3], pts[1], pts[2], t)); } else { return(CRSpline.Velocity(Wrap(pts), t)); } }
public static Vector3 Velocity(Path pts, float t, Easing.EaseType easeType = Easing.EaseType.linear) { t = Easing.ease(easeType, t); if (pts.Length == 0) { return(Vector3.zero); } else if (pts.Length == 1) { return(pts[0]); } else if (pts.Length == 2) { return(Vector3.Lerp(pts[0], pts[1], t)); } else if (pts.Length == 3) { return(QuadBez.Velocity(pts[0], pts[2], pts[1], t)); } else if (pts.Length == 4) { return(CubicBez.Velocity(pts[0], pts[3], pts[1], pts[2], t)); } else { return(CRSpline.Velocity(Wrap(pts), t)); } }
public static Vector3 InterpConstantSpeed(Spline.Path pts, float t, Easing.EaseType easeType = Easing.EaseType.linear) { t = Easing.ease(easeType, t, 0f, 1f); if (pts.Length == 0) { return(Vector3.zero); } if (pts.Length == 1) { return(pts[0]); } if (pts.Length == 2) { return(Vector3.Lerp(pts[0], pts[1], t)); } if (pts.Length == 3) { return(QuadBez.Interp(pts[0], pts[2], pts[1], t)); } if (pts.Length == 4) { return(CubicBez.Interp(pts[0], pts[3], pts[1], pts[2], t)); } return(CRSpline.InterpConstantSpeed(Spline.Wrap(pts), t)); }
//Json文件加载 private void JsonLoad(string path_name, CRSpline crSpline, ref bool is_right, Transform parent_trans) { string filePath = MotionPara.taskRootPath + MotionPara.taskName + "/PathControl.json"; if (File.Exists(filePath)) { JsonOperator jsonOp = new JsonOperator(); DataTable jsonTable = jsonOp.JsonReader(filePath, path_name); if (jsonTable == null) { Debug.LogError(path_name + ", 该路径名称不存在!"); return; } GameObject loadEmpty = new GameObject(); loadEmpty.name = "JsonLoad_empty"; loadEmpty.transform.parent = parent_trans; for (int i = 0; i < jsonTable.Rows.Count; i++) { loadEmpty.transform.localPosition = ConvertToVector3((string)jsonTable.Rows[i][0].ToString()); loadEmpty.transform.localEulerAngles = ConvertToVector3((string)jsonTable.Rows[i][1].ToString()); crSpline.controlPoints.Add(loadEmpty.transform.position); crSpline.rotationList.Add(loadEmpty.transform.eulerAngles); crSpline.cameraViewList.Add(float.Parse((string)jsonTable.Rows[i][2].ToString())); } GameObject.DestroyImmediate(loadEmpty); } else { Debug.LogError(filePath + ", 该文件不存在!"); } }
public static Vector3 InterpConstantSpeed(Path pts, float t, EasingType ease, bool easeIn, bool easeOut) { t = Ease(t, ease, easeIn, easeOut); if (pts.Length == 0) { return(Vector3.zero); } else if (pts.Length == 1) { return(pts[0]); } else if (pts.Length == 2) { return(Vector3.Lerp(pts[0], pts[1], t)); } else if (pts.Length == 3) { return(QuadBez.Interp(pts[0], pts[2], pts[1], t)); } else if (pts.Length == 4) { return(CubicBez.Interp(pts[0], pts[3], pts[1], pts[2], t)); } else { return(CRSpline.InterpConstantSpeed(Wrap(pts), t)); } }
Object PutOnPath(Transform obj, float pathPercentage, Vector3 offset) { pathPercentage = Mathf.Min(Mathf.Max(0.0f, pathPercentage), 0.999f); Quaternion rotation = new Quaternion(); rotation.SetLookRotation(CRSpline.InterpConstantSpeed(thePath.ToArray(), pathPercentage) - CRSpline.InterpConstantSpeed(thePath.ToArray(), pathPercentage * 0.99f)); return(Instantiate(obj, CRSpline.InterpConstantSpeed(thePath.ToArray(), pathPercentage) + offset, rotation)); }
private void Draw(Trajectory t, PaintEventArgs e) { Curve c = new Curve(); c.AddRange(CRSpline.Create(t).Interpolate(t.Count * Quality)); c.Pencil = new Pen(t.Finished ? Brushes.OliveDrab : Brushes.MediumSlateBlue, 1); DrawCurve(e.Graphics, c); }
void SetOnPath(Transform obj, float pathPercentage, Vector3 offset) { pathPercentage = Mathf.Min(Mathf.Max(0.0f, pathPercentage), 0.999f); Quaternion rotation = new Quaternion(); rotation.SetLookRotation(CRSpline.InterpConstantSpeed(thePath.ToArray(), pathPercentage) - CRSpline.InterpConstantSpeed(thePath.ToArray(), pathPercentage * 0.99f)); obj.position = CRSpline.InterpConstantSpeed(thePath.ToArray(), pathPercentage) + offset; obj.rotation = rotation; }
void Awake() { // Allocate the various arrays here to avoid // allocations during the game loop. bandData = new float[Spectrum.N_BANDS]; bandPoints = new Vector3[Spectrum.N_BANDS + 2]; splinePoints = new Vector3[nPoints]; colliderPoints = new Vector2[nPoints + 2]; spline = new CRSpline(bandPoints); }
// Use this for initialization void Start() { Vector3[] path = { new Vector3(976.656f, 1160.348f, 57.45f), new Vector3(20.833f, 972.301f, 430.565f), new Vector3(-930.696f, 0.0f, 176.947f), new Vector3(-383.768f, 0.0f, -489.477f), new Vector3(-158.563f, 335.323f, 75.834f), new Vector3(714.683f, 274.965f, -402.153f), new Vector3(-140.179f, 0.0f, -843.371f), new Vector3(737.663f, -176.377f, -1022.617f), new Vector3(883.723f, -33.156f, -685.02f), new Vector3(1559.897f, 118.9f, -608.974f), new Vector3(1541.968f, 0.0f, 158.764f), new Vector3(549.226f, 0.0f, -80.431f), new Vector3(-282.656f, -344.386f, -719.279f) }; path = path.Select(x => x * 0.2f).ToArray(); thePath = Interpolate.NewCatmullRom(path, 1000, false); this.transform.parent.transform.position = path[0]; Transform theBdnf; theBdnf = (Transform)PutOnPath(BDNF, firstBDNFPath, Vector3.up * BDNFHeight); theBdnf.gameObject.SendMessage("SetPictureTaker", PictureTaker); theBdnf.gameObject.SendMessage("SetMessageCaught", Badge09); theBdnf = (Transform)PutOnPath(BDNF, lastBDNFPath, Vector3.up * BDNFHeight); theBdnf.gameObject.SendMessage("SetPictureTaker", PictureTaker); theBdnf.gameObject.SendMessage("SetMessageCaught", Badge09); var spread = (lastBDNFPath - firstBDNFPath) / BDNFAmount; for (int i = 1; i <= BDNFAmount - 2; i++) { theBdnf = (Transform)PutOnPath(BDNF, firstBDNFPath + spread * i + (Random.value - 0.5f) * spread * 0.7f, Vector3.up * BDNFHeight); theBdnf.gameObject.SendMessage("SetPictureTaker", PictureTaker); theBdnf.gameObject.SendMessage("SetMessageCaught", Badge09); } for (float i = ringBeginning; i <= 0.99f; i += ringSeparation) { var splinePos = CRSpline.InterpConstantSpeed(thePath.ToArray(), i); ringPositions.Add(splinePos); ringAngles.Add(splinePos - CRSpline.InterpConstantSpeed(thePath.ToArray(), i * 0.99f)); } for (int i = 0; i < rings.Length; i++) { rings[i] = (Transform)PutOnPathCache(tubeRing, i, Vector3.zero, ringPositions, ringAngles); rings[i].Rotate(90, 0, 0); ringOffsets[i] = i * ringSeparation + ringBeginning; ribosomes[i] = (Transform)PutOnPathCache(ribosome, i, ribosomeDist * new Vector3(Random.value, Random.value, Random.value), ringPositions, ringAngles); } currentCache = rings.Length; //MoveBall(); }
void Awake() { if (mPathes.ContainsKey(gameObject.name)) { Debug.LogError("CR样条线路径名重复:" + gameObject.name); //Destroy(gameObject); return; } mPathes.Add(gameObject.name, this); spline = new CRSpline(nodes); }
private void Start() { sprite = GetComponentInChildren <SpriteRenderer>(); Vector3[] points = new Vector3[] { new Vector3(0, Random.Range(-30.0f, -20.0f), 0), transform.position, recycleBinPosition, recycleBinPosition + new Vector3(Random.Range(-15.0f, 15.0f), -20.0f, 0) }; spline = new CRSpline(points); startingScale = transform.localScale; startingAlpha = sprite.color.a; }
public static void GizmoDraw(Vector3[] pts, float t) { Gizmos.color = Color.white; Vector3 to = CRSpline.Interp(pts, 0f); for (int i = 1; i <= 20; i++) { float t2 = (float)i / 20f; Vector3 vector = CRSpline.Interp(pts, t2); Gizmos.DrawLine(vector, to); to = vector; } Gizmos.color = Color.blue; Vector3 vector2 = CRSpline.Interp(pts, t); Gizmos.DrawLine(vector2, vector2 + CRSpline.Velocity(pts, t)); }
void MoveBall() { if (pathCompletion < 1.0f) { var toMove = CRSpline.InterpConstantSpeed(thePath.ToArray(), pathCompletion) - this.transform.parent.transform.position; Quaternion rotation = new Quaternion(); if (toMove.magnitude > 0.0f) { rotation.SetLookRotation(toMove); this.transform.parent.transform.localRotation = rotation; } this.transform.parent.transform.position += toMove; } else { TriggerEnd(); } }
// Use this for initialization void Start () { Vector3[] drawPath = iTweenPath.GetPath("test path"); spline = new CRSpline(drawPath); t = 0.0f; /* iTween.MoveTo(gameObject, iTween.Hash( "path", iTweenPath.GetPath("test path"), "looptype", "loop", "time", 10, "movetopath", false )); */ }
public void DistributeNodes() { int numNodes = path.Count; //if (numNodes<5) return; Vector3[] newpositions = new Vector3[numNodes]; Vector3[] positions = new Vector3[numNodes]; for(int i=0; i<positions.Length; i++) { if( path[i] != null ) positions[i] = path[i].position; } CRSpline posSpline = new CRSpline( positions ); for (int a=1; a<positions.Length-1; a++) { float p = (1.0f / ((float)numNodes-3.0f)) * (a-1); //Debug.Log(p+","+getT( splineLenght*p )); newpositions[a] = posSpline.Interp(getT( splineLenght*p )); } for (int a=1; a<positions.Length-1; a++) path[a].position = newpositions[a]; }
void generateMesh() { if(meshPrefab==null) return; List<Vector3> vertices=new List<Vector3>(); List<int>[] triangles=new List<int>[3]; triangles[0]=new List<int>(); triangles[1]=new List<int>(); triangles[2]=new List<int>(); List<Vector2> uv=new List<Vector2>(); List<Vector3> normals=new List<Vector3>(); List<Vector4> tangents=new List<Vector4>(); CRSpline posSpline = new CRSpline( path.getPositions() ); //CRSpline rotSpline = new CRSpline( path.getRotations() ); Mesh[] mesh=new Mesh[3]; mesh[0]=meshPrefab.FindChild("start").GetComponent<MeshFilter>().sharedMesh; mesh[1]=meshPrefab.FindChild("middle").GetComponent<MeshFilter>().sharedMesh; mesh[2]=meshPrefab.FindChild("end").GetComponent<MeshFilter>().sharedMesh; Vector3[][] meshVertices=new Vector3[3][]; int[][] meshTriangles=new int[3][]; Vector2[][] meshUv=new Vector2[3][]; Vector3[][] meshNormals=new Vector3[3][]; Vector4[][] meshTangents=new Vector4[3][]; float[] max=new float[3]; for(int i=0;i<3;i++) { meshVertices[i]=mesh[i].vertices; meshTriangles[i]=mesh[i].GetTriangles(0); meshUv[i]=mesh[i].uv; meshNormals[i]=mesh[i].normals; meshTangents[i]=mesh[i].tangents; for(int vi=0;vi<meshTriangles[i].Length;vi++) { if(meshVertices[i][meshTriangles[i][vi]].x>max[i]) max[i]=meshVertices[i][meshTriangles[i][vi]].x; } } float cursor=0; int vertI=0; int meshI=0; if(path.round) meshI=1; bool last=false; //float scale=1; while(true) { if(meshI==2) break; if(getT(cursor+max[meshI])<1.0f || last) { if(last) { //scale=1.0f/(getT(cursor+max[meshI])-getT(cursor))*(1.0f-getT(cursor)); } if(getT(cursor+max[meshI]+max[2])>1.0f && !path.round) meshI=2; for(int i=0;i<meshTriangles[meshI].Length;i++) { triangles[meshI].Add(vertI+meshTriangles[meshI][i]); } vertI+=mesh[meshI].vertexCount; for(int i=0;i<meshVertices[meshI].Length;i++) { float vertexCursor=cursor+Mathf.Max(meshVertices[meshI][i].x,0); float t=getT(vertexCursor); Vector3 vPos=posSpline.Interp(t); Vector3 look=posSpline.Velocity(t).normalized; Quaternion r=Quaternion.LookRotation(look); r=Quaternion.Euler(0,-90,0)*Quaternion.Euler(r.eulerAngles.z,(r.eulerAngles.y),-r.eulerAngles.x); Vector3 localVert=meshVertices[meshI][i]; localVert.x=0; Vector3 vert=(r*localVert)+(vPos-transform.position); //vert.x-=middleVertices[i].x; //Debug.Log(t); vertices.Add(vert); uv.Add(meshUv[meshI][i]); normals.Add(r*meshNormals[meshI][i]); tangents.Add(r*meshTangents[meshI][i]); } //Debug.Log(max); cursor+=max[meshI]; if(meshI==0) meshI=1; if(last) break; } else { if(!last) last=true; else break; } } Mesh finalMesh=new Mesh(); finalMesh.vertices=vertices.ToArray(); finalMesh.subMeshCount=3; finalMesh.SetTriangles(triangles[0].ToArray(),0); finalMesh.SetTriangles(triangles[1].ToArray(),1); finalMesh.SetTriangles(triangles[2].ToArray(),2); finalMesh.uv=uv.ToArray(); finalMesh.normals=normals.ToArray(); finalMesh.tangents=tangents.ToArray(); GetComponent<MeshFilter>().sharedMesh=finalMesh; Material[] sharedMaterials=new Material[3]; sharedMaterials[0]=meshPrefab.FindChild("start").GetComponent<MeshRenderer>().sharedMaterial; sharedMaterials[1]=meshPrefab.FindChild("middle").GetComponent<MeshRenderer>().sharedMaterial; sharedMaterials[2]=meshPrefab.FindChild("end").GetComponent<MeshRenderer>().sharedMaterial; GetComponent<Renderer>().sharedMaterials=sharedMaterials; }
void GenerateMoveToPathTargets() { Vector3[] suppliedPath; //create and store path points: if(tweenArguments["path"].GetType() == typeof(Vector3[])){ Vector3[] temp = (Vector3[])tweenArguments["path"]; //if only one point is supplied fall back to MoveTo's traditional use since we can't have a curve with one value: if(temp.Length==1){ Debug.LogError("iTween Error: Attempting a path movement with MoveTo requires an array of more than 1 entry!"); Dispose(); } suppliedPath=new Vector3[temp.Length]; Array.Copy(temp,suppliedPath, temp.Length); }else{ Transform[] temp = (Transform[])tweenArguments["path"]; //if only one point is supplied fall back to MoveTo's traditional use since we can't have a curve with one value: if(temp.Length==1){ Debug.LogError("iTween Error: Attempting a path movement with MoveTo requires an array of more than 1 entry!"); Dispose(); } suppliedPath = new Vector3[temp.Length]; for (int i = 0; i < temp.Length; i++) { suppliedPath[i]=temp[i].position; } } //do we need to plot a path to get to the beginning of the supplied path? bool plotStart; int offset; if(transform.position != suppliedPath[0]){ if(!tweenArguments.Contains("movetopath") || (bool)tweenArguments["movetopath"]==true){ plotStart=true; offset=3; }else{ plotStart=false; offset=2; } }else{ plotStart=false; offset=2; } //build calculated path: vector3s = new Vector3[suppliedPath.Length+offset]; if(plotStart){ vector3s[1]=transform.position; offset=2; }else{ offset=1; } //populate calculate path; Array.Copy(suppliedPath,0,vector3s,offset,suppliedPath.Length); //populate start and end control points: //vector3s[0] = vector3s[1] - vector3s[2]; vector3s[0] = vector3s[1] + (vector3s[1] - vector3s[2]); vector3s[vector3s.Length-1] = vector3s[vector3s.Length-2] + (vector3s[vector3s.Length-2] - vector3s[vector3s.Length-3]); //is this a closed, continuous loop? yes? well then so let's make a continuous Catmull-Rom spline! if(vector3s[1] == vector3s[vector3s.Length-2]){ Vector3[] tmpLoopSpline = new Vector3[vector3s.Length]; Array.Copy(vector3s,tmpLoopSpline,vector3s.Length); tmpLoopSpline[0]=tmpLoopSpline[tmpLoopSpline.Length-3]; tmpLoopSpline[tmpLoopSpline.Length-1]=tmpLoopSpline[2]; vector3s=new Vector3[tmpLoopSpline.Length]; Array.Copy(tmpLoopSpline,vector3s,tmpLoopSpline.Length); } //create Catmull-Rom path: path = new CRSpline(vector3s); //need for speed? if(tweenArguments.Contains("speed")){ float distance = PathLength(vector3s); time = distance/(float)tweenArguments["speed"]; } }
protected void easePath( ) { positionSpline = new CRSpline( path.getPositions() ); rotationSpline = new CRSpline( path.getRotations() ); float startI=(1.0f/(positionSpline.pts.Length-3))*(pathStartNode-1); float endI=(1.0f/(positionSpline.pts.Length-3))*(pathEndNode-1); float i=startI+ ((endI-startI)*normalizedTime); Vector3 newPos = positionSpline.Interp( i ); if (transform==null) { Debug.Log("EasePath error: "+easingGroup + " " + animationCurveName); return; } if(space==Space.World) transform.position = newPos; else transform.localPosition = newPos; if( lookAtTransform!= null ) { transform.LookAt( lookAtTransform, vectorUp ); } else if( pathAlignWithSpeed ) { Vector3 velVector = positionSpline.Velocity( i); if(space==Space.World) transform.rotation = Quaternion.LookRotation( velVector ); else transform.localRotation = Quaternion.LookRotation( velVector ); } else { Vector3 newRot = rotationSpline.Interp( i ); if(space==Space.World) transform.rotation = Quaternion.Euler( newRot ); else transform.localRotation = Quaternion.Euler( newRot ); } }