UnityMol  0.9.6-875
UnityMol viewer / In developement
GenInterpolationPoint.cs
Go to the documentation of this file.
1 
66 using UnityEngine;
67 using System.Collections;
68 using System.Collections.Generic;
69 
70 public class GenInterpolationPoint {
71 
72  //public parameters
73  public GameObject SplineParent;
74  public float Duration = 1.0f;
75  public int lineCount=1000;
76  public ArrayList InputKeyNodes;
77  public List<string> InputTypeArray;
78 
79  public List<float[]> OutputKeyNodes;
80  public List<string> OutputTypeArray;
81 // private Transform[] mTransforms;
82  private List<SplineNode> mNodes;
83 
84  public static int smoothnessFactor = 8 ;
85 
86  //test the node data varibles
87 
88  public GameObject temp;
89  public float moveSpeed=10f;
90 // private int curIdx=0;
91 // private float curTime=0;
92 // private float moveTime=0;
93  private Vector3 NextPos;
94  private Vector3 CurPos;
95  private Quaternion CurRot;
96  private Quaternion NextRot;
97 
98  private Vector3[] Nodes;
99 
100 
101  public void CalculateSpline() {
102  lineCount=InputKeyNodes.Count*smoothnessFactor;
103 
105  OutputKeyNodes=new List<float[]>();
106  OutputTypeArray=new List<string>();
107 
108  Gizmos.color = new Color(0.0f, 0.0f, 1.0f);
109  for (int c=1; c <lineCount; c++) {
110  float currTime = c * Duration / lineCount;
111  Vector3 currPos = GetHermiteAtTime(currTime);
112 
113  float[] currposfloat=new float[3];
114  currposfloat[0]=currPos.x;
115  currposfloat[1]=currPos.y;
116  currposfloat[2]=currPos.z;
117  OutputKeyNodes.Add(currposfloat);
118  OutputTypeArray.Add(InputTypeArray[0]);
119  // Debug.Log ("InputTypePoint " + InputTypeArray[0]);
120 // MonoBehaviour.print("CalculateSpline");
121  }
122  }
123 
125  int c;
126  float step;
127  mNodes = new List<SplineNode>();
128  step = Duration/InputKeyNodes.Count;
129 
130  for (c = 0; c < InputKeyNodes.Count; c++) {
131  float[] vect=(float[])InputKeyNodes[c];
132  Vector3 inputnode=new Vector3(vect[0],vect[1],vect[2]);
133  mNodes.Add(new SplineNode(inputnode, step*c));
134  }
135 // SetAutoCloseMode(step*c);
136 
137  mNodes.Insert(0,mNodes[0]);
138  mNodes.Add(mNodes[mNodes.Count-1]);
139  }
140 
141 
142  public Vector3 GetHermiteInternal(int idxFirstPoint ,float t){
143  float t2 = t*t; float t3 = t2*t;
144 
145  Vector3 P0 = mNodes[idxFirstPoint-1].Point;
146  Vector3 P1 = mNodes[idxFirstPoint].Point;
147  Vector3 P2 = mNodes[idxFirstPoint+1].Point;
148  Vector3 P3 = mNodes[idxFirstPoint+2].Point;
149 
150  float tension = 0.5f; // 0.5 equivale a catmull-rom
151 
152  Vector3 T1 = tension * (P2 - P0);
153  Vector3 T2 = tension * (P3 - P1);
154 
155  float Blend1 = 2*t3 - 3*t2 + 1;
156  float Blend2 = -2*t3 + 3*t2;
157  float Blend3 = t3 - 2*t2 + t;
158  float Blend4 = t3 - t2;
159 
160  return Blend1*P1 + Blend2*P2 + Blend3*T1 + Blend4*T2;
161  }
162 
163  public Vector3 GetHermiteAtTime(float timeParam){
164  int c;
165  if (timeParam >= mNodes[mNodes.Count-2].Time)
166  return mNodes[mNodes.Count-2].Point;
167 
168  for (c = 1; c < mNodes.Count-2; c++){
169  if (mNodes[c].Time > timeParam)
170  break;
171  }
172 
173  int idx = c-1;
174  float param = (timeParam - mNodes[idx].Time) / (mNodes[idx+1].Time - mNodes[idx].Time);
175  param = MathUtils.Ease(param, 0.0f, 0.1f);
176 
177  return GetHermiteInternal(idx, param);
178  }
179 
180  //define the node class
181  class SplineNode{
182  public Vector3 Point;
183  public float Time;
184 
185  public SplineNode(Vector3 p,float t){
186  Point=p;
187  Time=t;
188  }
189 
190  public SplineNode(SplineNode o){
191  Point=o.Point;
192  Time=o.Time;
193  }
194  }
195 
196 }
static float Ease(float t, float k1, float k2)
Definition: MathUtils.cs:147
List< SplineNode > mNodes
Vector3 GetHermiteAtTime(float timeParam)
Vector3 GetHermiteInternal(int idxFirstPoint, float t)