Advertisement
LittleAngel

Model Viewer

Oct 27th, 2011
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Viewer : MonoBehaviour {
  5. public Transform sopwith;
  6. public Transform fokker;
  7. public Light keyLight;
  8. public Light fillLight;
  9. public float sensitivity;
  10.  
  11. private Transform thisTransform;
  12. private Vector3 previousMousePos;
  13. private Vector3 deltaPos;
  14. private float cameraDistance;
  15. private float keyLightStrength;
  16. private float fillLightStrenth;
  17. private float ambientLightStrength;
  18. private bool overGUI;
  19. private Rect sliderRect;
  20.  
  21.  
  22. void Awake () {
  23. thisTransform = sopwith;
  24. fokker.gameObject.SetActiveRecursively (false);
  25. cameraDistance = 75.0f;
  26. keyLightStrength = PlayerPrefs.GetFloat ("Viewer-KeyIntensity", 1.0f);
  27. fillLightStrenth = PlayerPrefs.GetFloat ("Viewer-FillIntensity", 0.2f);
  28. ambientLightStrength = PlayerPrefs.GetFloat ("Viewer-AmbientIntensity", 0.2f);
  29. sliderRect = new Rect(25, Screen.height - 175, 200, 175 );
  30. }
  31.  
  32. void OnDisable () {
  33. PlayerPrefs.SetFloat ("Viewer-KeyIntensity", keyLightStrength);
  34. PlayerPrefs.SetFloat ("Viewer-FillIntensity", fillLightStrenth);
  35. PlayerPrefs.SetFloat ("Viewer-AmbientIntensity", ambientLightStrength);
  36. }
  37.  
  38. void OnGUI () {
  39. GUILayout.BeginArea (new Rect(25, 25, 80, 200));
  40. if (GUILayout.Button ("Sopwith")) {
  41. thisTransform = sopwith;
  42. // thisTransform.eulerAngles = Vector3.zero;
  43. sopwith.gameObject.SetActiveRecursively (true);
  44. fokker.gameObject.SetActiveRecursively (false);
  45. }
  46. if (GUILayout.Button ("Fokker")) {
  47. thisTransform = fokker;
  48. // thisTransform.eulerAngles = Vector3.zero;
  49. fokker.gameObject.SetActiveRecursively (true);
  50. sopwith.gameObject.SetActiveRecursively (false);
  51. }
  52. if (GUILayout.Button ("Reset")) {
  53. thisTransform.eulerAngles = Vector3.zero;
  54. }
  55. GUILayout.EndArea ();
  56.  
  57. GUILayout.BeginArea (sliderRect);
  58. GUILayout.Label ("Key Light Intensity: " + keyLight.intensity);
  59. GUILayout.BeginHorizontal ();
  60. GUILayout.Label ("0", GUILayout.MaxWidth(10));
  61. keyLightStrength = GUILayout.HorizontalSlider (keyLightStrength, 0.0f, 1.0f);
  62. GUILayout.Label ("1", GUILayout.MaxWidth(10));
  63. keyLight.intensity = keyLightStrength;
  64. GUILayout.EndHorizontal ();
  65. GUILayout.Label ("Fill Light Intensity: " + fillLight.intensity);
  66. GUILayout.BeginHorizontal ();
  67. GUILayout.Label ("0", GUILayout.MaxWidth(10));
  68. fillLightStrenth = GUILayout.HorizontalSlider (fillLightStrenth, 0.0f, 1.0f);
  69. GUILayout.Label ("1", GUILayout.MaxWidth(10));
  70. fillLight.intensity = fillLightStrenth;
  71. GUILayout.EndHorizontal ();
  72. GUILayout.Label ("Ambient Light Intensity: " + (ambientLightStrength * 255));
  73. GUILayout.BeginHorizontal ();
  74. GUILayout.Label ("0", GUILayout.MaxWidth(10));
  75. ambientLightStrength = GUILayout.HorizontalSlider (ambientLightStrength, 0.0f, 1.0f);
  76. GUILayout.Label ("1", GUILayout.MaxWidth(10));
  77. RenderSettings.ambientLight = new Color (ambientLightStrength, ambientLightStrength, ambientLightStrength);
  78. GUILayout.EndHorizontal ();
  79. GUILayout.EndArea ();
  80.  
  81. GUILayout.BeginArea (new Rect(Screen.width - 105, 25, 80, 200));
  82. if (GUILayout.Button ("Close View")) cameraDistance = 75;
  83. if (GUILayout.Button ("RR1")) cameraDistance = 150;
  84. if (GUILayout.Button ("RR2")) cameraDistance = 300;
  85. if (GUILayout.Button ("RR3")) cameraDistance = 450;
  86. if (GUILayout.Button ("RR4")) cameraDistance = 600;
  87.  
  88. Vector3 newPosition = Vector3.zero;
  89. newPosition.z = cameraDistance;
  90. Camera.main.transform.position = newPosition;
  91. GUILayout.EndArea ();
  92.  
  93. // if (sliderRect.Contains (Input.mousePosition)) {
  94. //// if (Input.GetMouseButton(0) && sliderRect.Contains (Input.mousePosition)) {
  95. //// overGUI = true;
  96. //// Debug.Log ("Over GUI");
  97. // } else {
  98. // overGUI = false;
  99. //// Debug.Log ("NOT Over GUI");
  100. // }
  101.  
  102. if (sliderRect.Contains (Event.current.mousePosition)) {
  103. // if (Input.GetMouseButton(0) && sliderRect.Contains (Input.mousePosition)) {
  104. overGUI = true;
  105. // Debug.Log ("Over GUI");
  106. } else {
  107. overGUI = false;
  108. // Debug.Log ("NOT Over GUI");
  109. }
  110. // Debug.Log ("Rect: " + sliderRect.x + ", " + sliderRect.y + ", " + (sliderRect.x + sliderRect.width) + ", " + (sliderRect.y + sliderRect.height));
  111. // Debug.Log ("-----Event.current.mousePosition: " + Event.current.mousePosition);
  112. // Rect guiRect = GUILayoutUtility.GetLastRect();
  113. // if (Event.current != null && Event.current.isMouse) {
  114. // if (Event.current != null && Event.current.type == EventType.Repaint && Event.current.isMouse) {
  115. // overGUI = sliderRect.Contains(Input.mousePosition);
  116. // Debug.Log (overGUI);
  117. // }
  118. }
  119.  
  120. void Update () {
  121. if (!overGUI) {
  122. // if (sliderRect.Contains (Input.mousePosition)) {
  123. //// if (Input.GetMouseButton(0) && sliderRect.Contains (Input.mousePosition)) {
  124. // Debug.Log ("Over GUI");
  125. // } else {
  126. // Debug.Log ("NOT Over GUI");
  127.  
  128. if (Input.GetMouseButtonDown(0)) {
  129. previousMousePos = Input.mousePosition;
  130. } else {
  131. deltaPos = Vector3.zero;
  132. }
  133.  
  134. if (Input.GetMouseButton(0)) {
  135. deltaPos = Input.mousePosition - previousMousePos;
  136. previousMousePos = Input.mousePosition;
  137. Vector3 newRotation = Vector3.zero;
  138. newRotation.x = -deltaPos.y * sensitivity;
  139. newRotation.y = -deltaPos.x * sensitivity;
  140. thisTransform.Rotate (newRotation, Space.World);
  141. }
  142. }
  143. // Debug.Log ("Input.mousePosition: " + Input.mousePosition);
  144. // Debug.Log ("Rect: " + sliderRect.x + ", " + sliderRect.y + ", " + (sliderRect.x + sliderRect.width) + ", " + (sliderRect.y + sliderRect.height));
  145.  
  146. // if (!overGUI) {
  147. // if (Input.GetMouseButtonDown(0)) {
  148. // previousMousePos = Input.mousePosition;
  149. // } else {
  150. // deltaPos = Vector3.zero;
  151. // }
  152. //
  153. // if (Input.GetMouseButton(0)) {
  154. // deltaPos = Input.mousePosition - previousMousePos;
  155. // previousMousePos = Input.mousePosition;
  156. // Vector3 newRotation = Vector3.zero;
  157. // newRotation.x = -deltaPos.y * sensitivity;
  158. // newRotation.y = -deltaPos.x * sensitivity;
  159. // thisTransform.Rotate (newRotation, Space.World);
  160. // }
  161. // }
  162. }
  163. }
  164.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement