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[] 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 )); */ }

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"]; } }

//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 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; }

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 ); } }