3DSelfie  Hansjoerg Hofer (1026632), Sebastian Metzler (0927550)
Controller.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using System.Collections;
3 
28 public class Controller : MonoBehaviour {
29 
30  GameObject _genericModel;
31  GameObject _snakeImageplane;
32 
33  ControlPoints _controlPoints;
34  Snake _snake;
35 
39  void Start () {
40  _genericModel = GameObject.Find("Head_mesh");
41 
42  _controlPoints = _genericModel.GetComponent<ControlPoints> ();
43 
44  _snake = gameObject.GetComponent<Snake> ();
45  }
46 
47  bool finished = false;
48 
52  void Update () {}
53 
61  public void findFaceContours(Vector2 leftEyeCenter, Vector2 rightEyeCenter, Vector2 mouthCenter){
62 
63  RunLEyeDeformation (leftEyeCenter);
64  RunREyeDeformation (rightEyeCenter);
65  RunMouthDeformation (mouthCenter);
66  }
73  public void findEyebrows(Vector2 leftEyeBrowCenter, Vector2 rightEyeBrowCenter){
74 
75  RunLEyeBrowDeformation (leftEyeBrowCenter);
76  RunREyeBrowDeformation (rightEyeBrowCenter);
77  }
78 
84  public void resetSnake(){
85  _snake.enabled = false;
86  _controlPoints.enabled = false;
87  _snake.enabled = true;
88  _controlPoints.enabled = true;
89  }
90 
97  public void RunContourDeformation () {
98  _snake.enabled = true;
99  _controlPoints.enabled = true;
100 
101  _snake.SetControlPoints (_controlPoints.ContourFCP);
102 
103  Vector3[] offsets = RunExpandingSnake (_controlPoints.ContourFCP);
104 
105  _controlPoints.DeformContour (offsets);
106  }
107 
108 
109  Vector3 eyeScale = new Vector3(1.5f, 2.3f, 1f);
110  Vector3 eyeBrowsScale = new Vector3(1.5f, 2f, 1f);
111 
118  public void RunLEyeDeformation (Vector2 center) {
119  _snake.enabled = true;
120  _controlPoints.enabled = true;
121 
122  _snake.SetControlPoints(transformContour(_controlPoints.LEyeFCP, eyeScale, center));
123  Vector3[] offsets = RunContractingSnake (_controlPoints.LEyeFCP);
124 
125  _controlPoints.DeformLEye (offsets);
126  }
127 
134  public void RunREyeDeformation (Vector2 center) {
135 
136  _snake.enabled = true;
137  _controlPoints.enabled = true;
138 
139  _snake.SetControlPoints(transformContour(_controlPoints.REyeFCP, eyeScale, center));
140 
141  Vector3[] offsets = RunContractingSnake (_controlPoints.REyeFCP);
142 
143  _controlPoints.DeformREye (offsets);
144  }
145 
152  public void RunLEyeBrowDeformation (Vector2 center) {
153  _snake.enabled = true;
154  _controlPoints.enabled = true;
155 
156  _snake.SetControlPoints (transformContour(_controlPoints.LEyebrowCP, eyeBrowsScale, center));
157  Vector3[] offsets = RunContractingSnake (_controlPoints.LEyebrowCP);
158 
159  _controlPoints.DeformLEyeBrow (offsets);
160  //_controlPoints.ApplyDeformation();
161  }
162 
169  public void RunREyeBrowDeformation (Vector2 center) {
170  _snake.enabled = true;
171  _controlPoints.enabled = true;
172 
173  _snake.SetControlPoints(transformContour(_controlPoints.REyebrowCP, eyeBrowsScale, center));
174  Vector3[] offsets = RunContractingSnake (_controlPoints.REyebrowCP);
175 
176  _controlPoints.DeformREyeBrow (offsets);
177  }
178 
185  public void RunMouthDeformation (Vector2 center) {
186 
187  _snake.enabled = true;
188  _controlPoints.enabled = true;
189 
190  _snake.SetControlPoints (transformContour(_controlPoints.MouthFCP, new Vector3(0.5f, 0.5f, 1f), center));
191 
192  Vector3[] offsets = RunExpandingSnake (_controlPoints.MouthFCP);
193 
194  _controlPoints.DeformMouth (offsets);
195  }
196 
197  bool drawSteps = true;
207  Vector3[] RunExpandingSnake(Vector3[] startpoints) {
208 
209  _snake.InitSnake(true);
210  _snake.UpdateSnake(150, drawSteps);
211 
212  Vector3[] startExpWPoints = startpoints;
213  Vector3 meshPos = new Vector3 (1f, 0f, 0f);
214 
215  for (int i = 0; i < startExpWPoints.Length; i++ ) {
216  startExpWPoints[i] -= meshPos;
217  startExpWPoints[i].z = -2.0f;
218  }
219 
220  //startExpWPoints = transformContour (_snake.GetStartWorldPoints (), new Vector3(1f/scale.x, 1f/scale.y, 1f/scale.z), center);
221 
222 
223  Vector3[] expWPoints = _snake.GetSnakeWorldPoints ();
224  Vector3[] expOffsets = _controlPoints.CalculateOffset (startExpWPoints, expWPoints);
225 
226  return expOffsets;
227  }
228 
238  Vector3[] RunContractingSnake(Vector3[] startpoints){
239  _snake.InitSnake();
240  _snake.UpdateSnake(200, drawSteps);
241 
242  Vector3[] startContWPoints = startpoints;
243  Vector3 meshPos = new Vector3 (1f, 0f, 0f);
244 
245  for (int i = 0; i < startContWPoints.Length; i++ ) {
246  startContWPoints[i] -= meshPos;
247  startContWPoints[i].z = -2.0f;
248  }
249 
250  //startContWPoints = transformContour (_snake.GetStartWorldPoints (), new Vector3(1f/scale.x, 1f/scale.y, 1f/scale.z), center);
251 
252  Vector3[] contWPoints = _snake.GetSnakeWorldPoints ();
253  Vector3[] contOffsets = _controlPoints.CalculateOffset (startContWPoints, contWPoints);
254 
255  return contOffsets;
256  }
257 
266  Vector3[] transformContour(Vector3[] points, Vector3 scaleFactor, Vector2 newCenter){
267 
268  float xMin = float.MaxValue;
269  float xMax = 0;
270  float yMin = float.MaxValue;
271  float yMax = 0;
272  float zMin = float.MaxValue;
273  float zMax = 0;
274 
275 
276  foreach (Vector3 p in points) {
277  if(p.x<xMin)
278  xMin = p.x;
279  if(p.x>xMax)
280  xMax = p.x;
281  if(p.y<yMin)
282  yMin = p.y;
283  if(p.y>yMax)
284  yMax = p.y;
285  if(p.z<zMin)
286  zMin = p.z;
287  if(p.z>zMax)
288  zMax = p.z;
289  }
290 
291  Vector3 center = new Vector3 ((xMin + xMax) / 2, (yMin + yMax) / 2, (zMin + zMax) / 2);
292 
293  Vector3 newCenter3 = new Vector3 (newCenter.x + 1f, newCenter.y, center.z);
294 
295  for (int i=0; i<points.Length; i++)
296  points[i] = newCenter3 - Vector3.Scale((center - points[i]), scaleFactor);
297 
298 
299  return points;
300  }
301 
307  public void ApplyDeformation() {
308  _controlPoints.ApplyDeformation();
309  }
310 }
void DeformMouth(Vector3[] offsets)
Deforms the mouth.
Vector3[] LEyeFCP
ControlPoints script on mesh.
void DeformREye(Vector3[] offsets)
Deforms the right eye.
void RunLEyeDeformation(Vector2 center)
runs the deformation for the left eye
Definition: Controller.cs:118
void resetSnake()
Reinitializes the snake and control points script.
Definition: Controller.cs:84
void DeformLEyeBrow(Vector3[] offsets)
Deforms the left eyebrow.
void ApplyDeformation()
Applies the average of the calculated weighted offsets to every vertex in the mesh.
void findEyebrows(Vector2 leftEyeBrowCenter, Vector2 rightEyeBrowCenter)
Runs the eyebrow deformation.
Definition: Controller.cs:73
void RunContourDeformation()
runs the deformation for the face contour
Definition: Controller.cs:97
Vector3[] REyebrowCP
void DeformLEye(Vector3[] offsets)
Deforms the left eye.
Control script.
Definition: Controller.cs:28
Vector3[] REyeFCP
void SetControlPoints(Vector3[] controlPoints)
Sets the contour.
Definition: Snake.cs:101
void DeformContour(Vector3[] offsets)
Deforms the face contour.
void DeformREyeBrow(Vector3[] offsets)
Deforms the right eyebrow.
Vector3[] GetSnakeWorldPoints()
Gets the resulting contour in world coordinates.
Definition: Snake.cs:65
void RunREyeDeformation(Vector2 center)
runs the deformation for the right eye
Definition: Controller.cs:134
Vector3[] LEyebrowCP
void RunLEyeBrowDeformation(Vector2 center)
runs the deformation for the left eyebrow
Definition: Controller.cs:152
Vector3[] ContourFCP
Snake script.
Definition: Snake.cs:10
void InitSnake(bool expanding=false)
Initializes the snake with the given contour.
Definition: Snake.cs:300
Vector3[] MouthFCP
void UpdateSnake(int iterations, bool drawSteps)
Executes the snake algorithm.
Definition: Snake.cs:218
void ApplyDeformation()
Applies the deformation to the model.
Definition: Controller.cs:307
void RunREyeBrowDeformation(Vector2 center)
runs the deformation for the right eyebrow
Definition: Controller.cs:169
Vector3[] CalculateOffset(Vector3[] startPoints, Vector3[] endPoints, bool side=false)
Calculates the offset of two contours in world coordinates.
void RunMouthDeformation(Vector2 center)
runs the deformation for the mouth
Definition: Controller.cs:185
void findFaceContours(Vector2 leftEyeCenter, Vector2 rightEyeCenter, Vector2 mouthCenter)
Runs the left/rigt eye and mouth deformation.
Definition: Controller.cs:61