UnityMol  0.9.6-875
UnityMol viewer / In developement
maxCamera.cs
Go to the documentation of this file.
1 
66 // $Id: maxCamera.cs 798 2015-05-05 14:26:43Z s.doutreligne $
67 // Filename: maxCamera.cs
68 //
69 // original: http://www.unifycommunity.com/wiki/index.php?title=MouseOrbitZoom
70 //
71 // --01-18-2010 - create temporary target, if none supplied at start
72 using UnityEngine;
73 using UnityEngine.UI;
74 using System.Collections;
75 using System.Collections.Generic;
76 using UI;
77 using Molecule.Model;
78 using System;
79 using UnityEngine.EventSystems;
80 using GuidedNav;
81 
82 //[AddComponentMenu("Camera-Control/3dsMax Camera Style")]
83 
84 
85 
86 public class maxCamera : MonoBehaviour
87 {
88  public static bool cameraStop = false;
89  public bool automove = false;
90 
91  public float xSpeed = 400.0f;
92  public float ySpeed = 400.0f;
93 
94  public static int zoomRate = 200;
95  public float panSpeed = 2.0f;
96  public float zoomDampening = 20.0f ;
97  public float rotationFactor = 50.0f ;
98  public float strength = 0.5f; // for camera translation
99 
100  public Vector3 originalCamPosition;
101  public bool centerChanged = false; // for camera rotation;
102 
103  public Vector3 newposition;
104  public Vector3 cameraLocalPositionSave;
105  public GameObject newUIToggle;
106 
107  public bool cameraTranslation = false;
108  public float joypadDeadzone = 0.01f;
109  private float xDeg = 0.0f;
110  private float yDeg = 0.0f;
111  private float zDeg = 0.0f;
112 
113  private Quaternion currentRotation;
114  private Quaternion desiredRotation;
115  private Quaternion rotation;
116  private Vector3 position;
117  public static bool guiControl = false;
118  private GameObject LoadBox;
119 
120  public static GameObject LocCamera;
121  private Camera mainCamera;
122 
124 
125  public static Transform target;
126  public static Vector3 targetOffset;
127 
128  public static float currentDistance;
129  public static float desiredDistance;
130 
131  public float distance = 20.0f;
132 
133  public static float weight_cam = 0.0f;
134 
135 
136  //Nouvelle variable
137 
138  void Awake ()
139  {
140  Init ();
141  }
142 
143  void OnEnable ()
144  {
145  Init ();
146  }
147 
148  public void Init ()
149  {
150  LoadBox = GameObject.Find ("LoadBox");
151 
152  mainCamera = Camera.main;
153  LocCamera = mainCamera.gameObject;
154 
155 
156  Molecule3DComp = LoadBox.GetComponent<Molecule3D> ();
157 
158  //If there is no target, create a temporary target at 'distance' from the cameras current viewpoint
159  if (!target) {
160  GameObject go = new GameObject ("Cam Target");
161  //transform.position = go.transform.position + (transform.forward * distance);
162  target = go.transform;
163  }
164 
165  distance = Vector3.Distance (transform.position, target.position);
166  currentDistance = distance;
167  desiredDistance = distance;
168 
169  //be sure to grab the current rotations as starting points.
170  position = transform.position;
171  rotation = transform.rotation;
172  // position = Vector3.zero;
173  // rotation = Quaternion.identity;
174  currentRotation = transform.rotation;
175  desiredRotation = transform.rotation;
176 
177  xDeg = Vector3.Angle (Vector3.right, transform.right);
178  yDeg = Vector3.Angle (Vector3.up, transform.up);
179 
180  newUIToggle = GameObject.Find("New UI Toggle Canvas/Toggle New GUI");
181  }
182 
183  public void upxDeg ()
184  {
185  xDeg = 1;
186  guiControl = true;
187  }
188 
189  public void upyDeg ()
190  {
191  yDeg = -1;
192  guiControl = true;
193  }
194 
195  public void downxDeg ()
196  {
197  xDeg = -1;
198  guiControl = true;
199  }
200 
201  public void downyDeg ()
202  {
203  yDeg = 1;
204  guiControl = true;
205  }
206 
207  public void downzDeg ()
208  {
209  zDeg = 1;
210  guiControl = true;
211  }
212 
213  public void upzDeg ()
214  {
215  zDeg = -1;
216  guiControl = true;
217  }
218 
219  void Update ()
220  {
221 
222  UIData.newUIActivated = newUIToggle.GetComponent<Toggle>().isOn;
223 
224  if (Input.GetButtonUp ("Spin Molecule")) {
225  Debug.Log ("Space push");
226  automove = !automove;
228  }
229 
230  //Camera sliding and re-center on a atom or a group of atoms
231  if (Input.GetKeyUp (KeyCode.R)) {
232  cameraTranslation = true;
233  centerChanged = true;
234  newposition = Vector3.zero;
235  }
236 
237  if (Input.GetKeyUp (KeyCode.C)) {
238 
240 
241  /*if just one atom is selected, we center the camera (CAM TARGEt!!! THE CAMERA FOLLOW CAM TARGET)
242  on this atom*/
243 
244  if (mainCamera.GetComponent<ClickAtom> ().objList.Count == 1) {
245 
246  int atomnumber = (int)mainCamera.GetComponent<ClickAtom> ().objList [0].GetComponent<BallUpdate> ().number;
247 
248  newposition = new Vector3 (MoleculeModel.atomsLocationlist [atomnumber] [0],
249  MoleculeModel.atomsLocationlist [atomnumber] [1],
250  MoleculeModel.atomsLocationlist [atomnumber] [2]);
251 
252  cameraTranslation = true;
253  centerChanged = true;
254  /* if multiple atom is selected, we calcul the barycenter*/
255 
256  } else if (mainCamera.GetComponent<ClickAtom> ().objList.Count > 1) {
257 
258  //barycenter of all atom selection calculation
259  Vector3 barycenter = new Vector3 ();
260  float xTot = 0.0f, yTot = 0.0f, zTot = 0.0f;
261 
262  for (int i=0; i<mainCamera.GetComponent<ClickAtom>().objList.Count; i++) {
263  int atomnumber = (int)mainCamera.GetComponent<ClickAtom> ().objList [i].GetComponent<BallUpdate> ().number;
264 
265  xTot = xTot + MoleculeModel.atomsLocationlist [atomnumber] [0];
266  yTot = yTot + MoleculeModel.atomsLocationlist [atomnumber] [1];
267  zTot = zTot + MoleculeModel.atomsLocationlist [atomnumber] [2];
268  }
269 
270  barycenter.x = xTot / mainCamera.GetComponent<ClickAtom> ().objList.Count;
271  barycenter.y = yTot / mainCamera.GetComponent<ClickAtom> ().objList.Count;
272  barycenter.z = zTot / mainCamera.GetComponent<ClickAtom> ().objList.Count;
273 
274  newposition = barycenter;
275 
276  cameraTranslation = true;
277  centerChanged = true;
278  }
279  }
280  }
281 
282 
283  }
284 
285  /*
286  * Camera logic on LateUpdate to only update after all character movement logic has been handled.
287  */
288  void LateUpdate ()
289  {
290  if (!cameraStop) {
291  keyboardOperate ();
292  if (!guiControl)
293  joypadOperate ();
294 
295  // Check if guided navigation is on, if yes, no mouse interactions
296  if (!UIData.guided) {
297  // If Control and Alt and Middle button? ZOOM!
298  if (Input.GetMouseButton (1) && !(UIData.newUIActivated & EventSystem.current.IsPointerOverGameObject())) {
299 
300  //
301  // ???????
302  //
303  if (UIData.switchmode)
304  Molecule3DComp.ToParticle ();
305  desiredDistance -= Input.GetAxis ("Mouse Y") * Time.deltaTime * zoomRate;//* Mathf.Abs(desiredDistance);
306  }
307  // If middle mouse and left alt are selected? ORBIT
308  else if (Input.GetMouseButton (0)
309  && !guiControl && !(UIData.newUIActivated & EventSystem.current.IsPointerOverGameObject())) { //Click on UI parts (not canvas)
310 
311  if (UIData.switchmode)
312  Molecule3DComp.ToParticle ();
313  if (Input.mousePosition.x < Screen.width * 0.85f && Input.mousePosition.y < Screen.height * 0.85f && Input.mousePosition.y > Screen.height * 0.15f) {
314  xDeg = Input.GetAxis ("Mouse X") * xSpeed * 0.02f;
315  yDeg = -Input.GetAxis ("Mouse Y") * ySpeed * 0.02f;
316  } else if (Input.mousePosition.x > Screen.width * 0.85f) {
317  yDeg = -Input.GetAxis ("Mouse Y") * xSpeed * 0.02f;
318  xDeg = 0;
319  } else if (Input.mousePosition.y > Screen.height * 0.85f) {
320  xDeg = Input.GetAxis ("Mouse X") * xSpeed * 0.02f;
321  yDeg = 0;
322  } else {
323  zDeg = Input.GetAxis ("Mouse X") * ySpeed * 0.02f;
324  yDeg = 0;
325  }
326 
327 
328  }
329  // otherwise if middle mouse is selected, we pan by way of transforming the target in screenspace
330  else if (Input.GetMouseButton (2) && !(UIData.newUIActivated & EventSystem.current.IsPointerOverGameObject()) ) {
331 
332  if (UIData.switchmode)
333  Molecule3DComp.ToParticle ();
334  Vector3 v = LocCamera.transform.localPosition;
335  v.x -= Input.GetAxis ("Mouse X") * panSpeed;
336  v.y -= Input.GetAxis ("Mouse Y") * panSpeed;
337  LocCamera.transform.localPosition = v;
338  }
339  else {
340  if (UIData.switchmode)
341  Molecule3DComp.ToNotParticle ();
342 
343  }
344  } //End if guided ????????????
345 
346  // get the desired Zoom distance if we roll the scrollwheel
347  //if (!UIData.hiddenCamera)
348 
349  if(mainCamera.orthographic && !(UIData.newUIActivated & EventSystem.current.IsPointerOverGameObject())){ // orthographic mode we can achieve the same effet by making the camera bigger/smaller
350 
351  float tmp_size = mainCamera.orthographicSize - Input.GetAxis ("Mouse ScrollWheel") * Time.deltaTime * zoomRate;
352  if (tmp_size <= LoadTypeGUI.maxOrthoSize && tmp_size >= LoadTypeGUI.minOrthoSize)
353  mainCamera.orthographicSize = tmp_size;
354 
355  }
356  else if(!mainCamera.orthographic && !(UIData.newUIActivated & EventSystem.current.IsPointerOverGameObject())){
357  desiredDistance -= Input.GetAxis ("Mouse ScrollWheel") * Time.deltaTime * zoomRate;
358 
359  }
360 
361  if (automove == true) {
362  if (UIData.switchmode)
363  Molecule3DComp.ToParticle ();
364  xDeg += Mathf.Lerp (0.0F, 100.0F, Time.deltaTime * 0.8f);
365  yDeg = 0;
366  }
367 
368  if (centerChanged) {
369  //if we reach the position (for camera sliding) we stop!
370 
371  //otherwise we continue the translation.
372  if (cameraTranslation) {
373 
374  weight_cam += Time.deltaTime * 1;
375  target.localPosition = Vector3.Lerp (target.localPosition, newposition, weight_cam);
376 
377  mainCamera.transform.localPosition = Vector3.Lerp (mainCamera.transform.localPosition, Vector3.zero, weight_cam);
378 
379  //mainCamera.transform.eulerAngles = Vector3.Lerp (mainCamera.transform.eulerAngles, Vector3.zero, weight);
380 
381  if (target.position == newposition && mainCamera.transform.localPosition== Vector3.zero) {
382 
383  weight_cam = 0;
384  cameraTranslation = false;
385  centerChanged = false;
386  }
387  }
388  }
389  if(xDeg != 0 || yDeg != 0 || zDeg != 0){
390  //Camera rotation
391  desiredRotation *= Quaternion.Euler (yDeg, xDeg, zDeg);
392  currentRotation = transform.rotation;
393  rotation = Quaternion.Lerp (currentRotation, desiredRotation, Time.deltaTime * zoomDampening);
394  }
395  if(!UIData.guided){ //|| reset_panoramic){
396  transform.rotation = rotation;
397  }
398 
399  //Camera movement
400  currentDistance = Mathf.Lerp (currentDistance, desiredDistance, Time.deltaTime * zoomDampening);
401  position = target.position - (rotation * Vector3.forward * currentDistance + targetOffset);
402 
403 
404  if(!UIData.guided){ //|| reset_panoramic){
405  transform.position = position;
406  //reset_panoramic = false;
407  }
408 
409  xDeg = 0;
410  yDeg = 0;
411  zDeg = 0;
412  guiControl = false;
413  }
414  //}
415 
416  }
417 
418  private static float ClampAngle (float angle, float min, float max)
419  {
420  if (angle < -360)
421  angle += 360;
422  if (angle > 360)
423  angle -= 360;
424  return Mathf.Clamp (angle, min, max);
425  }
426 
427  public void ToCenter ()
428  {
429  LocCamera.transform.localPosition = Vector3.zero;
430  target.transform.localPosition = Vector3.zero;
431  transform.position = new Vector3 (0, 0, MoleculeModel.cameraLocation.z);
432  LocCamera.transform.rotation = new Quaternion (0, 0, 0, 0);
433  transform.rotation = Quaternion.identity;
434  centerChanged = false;
435  weight_cam = 0;
436  Init ();
437  }
438 
439 // control of the joypad =============================================================================================
440 // =============================================================================================
441  private void joypadOperate ()
442  {
443  target.rotation = transform.rotation;
444 
445  Vector3 v = LocCamera.transform.localPosition;
446 
447  /*
448  * Left pad
449  */
450  v.x -= (Input.GetAxis ("Horizontal") * panSpeed * Time.deltaTime);
451  v.y -= (Input.GetAxis ("Vertical") * panSpeed * Time.deltaTime);
452 
453  LocCamera.transform.localPosition = v;
454 
455  /*
456  * Right pad
457  */
458  if (Input.GetAxis ("Axis3") > joypadDeadzone || (Input.GetAxis ("Axis3") < -joypadDeadzone))
459  {
460  xDeg = Input.GetAxis ("Axis3") * xSpeed * 0.08f;
461  }
462 
463  if (Input.GetAxis ("Axis4") > joypadDeadzone || (Input.GetAxis ("Axis4") < -joypadDeadzone))
464  {
465  yDeg = Input.GetAxis ("Axis4") * ySpeed * 0.08f;
466  }
467 
468  //Z rotation
469  if (Input.GetButton("Rotate Z Right"))
470  {
471  zDeg = Time.deltaTime * xSpeed * 0.5f;
472  }
473 
474  if (Input.GetButton("Rotate Z Left"))
475  {
476  zDeg = - Time.deltaTime * xSpeed * 0.5f;
477  }
478 
479  //Zoom in
480  if (Input.GetButton("Zoom In")) {
481  desiredDistance -= Time.deltaTime * zoomRate * 0.08f;
482  if (UIData.switchmode)
483  Molecule3DComp.ToParticle ();
484  }
485  //zoom out
486  if (Input.GetButton("Zoom Out")) {
487  desiredDistance += Time.deltaTime * zoomRate * 0.08f;
488  if (UIData.switchmode)
489  Molecule3DComp.ToParticle ();
490  }
491  }
492 
496  private void keyboardOperate ()
497  {
498 
499  /*
500  * X Rotation
501  */
502 
503  if (Input.GetButton ("Rotate X Right")){
504  if(!UIData.guided)
505  upxDeg ();
506  }
507 
508  if (Input.GetButton ("Rotate X Left")) {
509  if (!UIData.guided)
510  downxDeg ();
511  }
512 
513  /*
514  * Y Rotation
515  */
516 
517  if (Input.GetButton ("Rotate Y Up")){
518  if (!UIData.guided)
519  upyDeg ();
520  }
521 
522  if (Input.GetButton ("Rotate Y Down")){
523  if(!UIData.guided)
524  downyDeg ();
525  }
526 
527 
528 
530 
531  } // End of KeyboardOperate
532 
533 
534 
535 }
bool centerChanged
Definition: maxCamera.cs:101
Vector3 position
Definition: maxCamera.cs:116
void ToNotParticle()
Switch the protein representation to Hyperball.
Definition: Molecule3D.cs:920
void ToCenter()
Definition: maxCamera.cs:427
static float ClampAngle(float angle, float min, float max)
Definition: maxCamera.cs:418
static bool newUIActivated
Definition: UIData.cs:163
Vector3 newposition
Definition: maxCamera.cs:103
void OnEnable()
Definition: maxCamera.cs:143
Vector3 originalCamPosition
Definition: maxCamera.cs:100
static Transform target
Definition: maxCamera.cs:125
void downxDeg()
Definition: maxCamera.cs:195
static bool switchmode
Definition: UIData.cs:155
void downyDeg()
Definition: maxCamera.cs:201
static float weight_cam
Definition: maxCamera.cs:133
static bool guided
Definition: UIData.cs:201
void ToParticle()
Switch the protein representation to Particle.
Definition: Molecule3D.cs:931
bool automove
Definition: maxCamera.cs:89
float zDeg
Definition: maxCamera.cs:111
float zoomDampening
Definition: maxCamera.cs:96
static float currentDistance
Definition: maxCamera.cs:128
float xDeg
Definition: maxCamera.cs:109
static bool guiControl
Definition: maxCamera.cs:117
void LateUpdate()
Definition: maxCamera.cs:288
Quaternion currentRotation
Definition: maxCamera.cs:113
void Init()
Definition: maxCamera.cs:148
float joypadDeadzone
Definition: maxCamera.cs:108
GameObject LoadBox
Definition: maxCamera.cs:118
bool cameraTranslation
Definition: maxCamera.cs:107
void joypadOperate()
Definition: maxCamera.cs:441
float xSpeed
Definition: maxCamera.cs:91
float panSpeed
Definition: maxCamera.cs:95
Quaternion desiredRotation
Definition: maxCamera.cs:114
static GameObject LocCamera
Definition: maxCamera.cs:120
Quaternion rotation
Definition: maxCamera.cs:115
!WiP Includes FLAGS of GUI.
Definition: UIData.cs:78
The GNParameters class regroups settings used for Guided Navigation and GLIC spreading.
static float desiredDistance
Definition: maxCamera.cs:129
float ySpeed
Definition: maxCamera.cs:92
static Vector3 targetOffset
Definition: maxCamera.cs:126
float yDeg
Definition: maxCamera.cs:110
float strength
Definition: maxCamera.cs:98
Molecule3D Molecule3DComp
Definition: maxCamera.cs:123
GameObject newUIToggle
Definition: maxCamera.cs:105
float rotationFactor
Definition: maxCamera.cs:97
void keyboardOperate()
Manage the camera rotation with keyboard inputs.
Definition: maxCamera.cs:496
void downzDeg()
Definition: maxCamera.cs:207
static int zoomRate
Definition: maxCamera.cs:94
void Awake()
Definition: maxCamera.cs:138
Camera mainCamera
Definition: maxCamera.cs:121
void upxDeg()
Definition: maxCamera.cs:183
void upyDeg()
Definition: maxCamera.cs:189
static bool cameraStop
Definition: maxCamera.cs:88
Vector3 cameraLocalPositionSave
Definition: maxCamera.cs:104
List< GameObject > objList
Definition: ClickAtom.cs:76
void Update()
Definition: maxCamera.cs:219
float distance
Definition: maxCamera.cs:131
Definition: GUIDisplay.cs:66
static List< float[]> atomsLocationlist
The coordinates of each atom.
void upzDeg()
Definition: maxCamera.cs:213