No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PSDGameObjectPreviewData.cs 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityObject = UnityEngine.Object;
  5. namespace UnityEditor.U2D.PSD
  6. {
  7. internal class PSDGameObjectPreviewData : IDisposable
  8. {
  9. static int s_SliderHash = "PSDGameObjectPreviewData_Slider".GetHashCode();
  10. Texture m_Texture;
  11. bool m_Disposed;
  12. PreviewRenderUtility m_RenderUtility;
  13. Rect m_PreviewRect = new Rect();
  14. Vector2 m_PreviewDir = Vector2.zero;
  15. GameObject m_PreviewObject;
  16. string m_PrefabAssetPath;
  17. Bounds m_RenderableBounds;
  18. Vector2 m_GameObjectOffset;
  19. bool m_ShowPivot;
  20. GameObject m_PivotInstance;
  21. GameObject m_Root;
  22. Rect m_DocumentPivot;
  23. public PSDGameObjectPreviewData(GameObject assetPrefab, bool showPivot, Rect documentPivot)
  24. {
  25. m_RenderUtility = new PreviewRenderUtility();
  26. m_RenderUtility.camera.fieldOfView = 30f;
  27. m_ShowPivot = showPivot;
  28. m_Root = new GameObject();
  29. m_PreviewObject = GameObject.Instantiate(assetPrefab, Vector3.zero, Quaternion.identity);
  30. m_PreviewObject.transform.parent = m_Root.transform;
  31. var renderableBounds = GetRenderableBounds(m_PreviewObject);
  32. var axisScale = Math.Max(renderableBounds.extents.x, m_RenderableBounds.extents.y) * 0.5f;
  33. GameObject pivotGO = AssetDatabase.LoadAssetAtPath<GameObject>("Packages/com.unity.2d.psdimporter/Editor/Assets/pivot.fbx");
  34. m_PivotInstance = GameObject.Instantiate(pivotGO, Vector3.zero, Quaternion.identity);
  35. m_PivotInstance.transform.localScale = new Vector3(axisScale, axisScale, axisScale);
  36. m_PivotInstance.transform.parent = m_Root.transform;
  37. m_PivotInstance.SetActive(m_ShowPivot);
  38. m_DocumentPivot = documentPivot;
  39. m_RenderUtility.AddSingleGO(m_Root);
  40. }
  41. static Vector2 Drag2D(Vector2 scrollPosition, Rect position)
  42. {
  43. int controlId = GUIUtility.GetControlID(s_SliderHash, FocusType.Passive);
  44. var current = Event.current;
  45. switch (current.GetTypeForControl(controlId))
  46. {
  47. case UnityEngine.EventType.MouseDown:
  48. if (position.Contains(current.mousePosition) && (double)position.width > 50.0)
  49. {
  50. GUIUtility.hotControl = controlId;
  51. current.Use();
  52. EditorGUIUtility.SetWantsMouseJumping(1);
  53. break;
  54. }
  55. break;
  56. case UnityEngine.EventType.MouseUp:
  57. if (GUIUtility.hotControl == controlId)
  58. GUIUtility.hotControl = 0;
  59. EditorGUIUtility.SetWantsMouseJumping(0);
  60. break;
  61. case UnityEngine.EventType.MouseDrag:
  62. if (GUIUtility.hotControl == controlId)
  63. {
  64. scrollPosition -= current.delta * (current.shift ? 3f : 1f) / Mathf.Min(position.width, position.height) * 140f;
  65. current.Use();
  66. GUI.changed = true;
  67. break;
  68. }
  69. break;
  70. }
  71. return scrollPosition;
  72. }
  73. public void DrawPreview(Rect r, GUIStyle background, Vector2 offset, bool showPivot)
  74. {
  75. if (!ShaderUtil.hardwareSupportsRectRenderTexture)
  76. {
  77. if (Event.current.type != UnityEngine.EventType.Repaint)
  78. return;
  79. EditorGUI.DropShadowLabel(new Rect(r.x, r.y, r.width, 40f), "Preview requires\nrender texture support");
  80. }
  81. else
  82. {
  83. Vector2 vector2 = Drag2D(m_PreviewDir, r);
  84. if (vector2 != m_PreviewDir)
  85. {
  86. UnityEngine.Object.DestroyImmediate(m_Texture);
  87. m_Texture = null;
  88. m_PreviewDir = vector2;
  89. }
  90. if (m_GameObjectOffset != offset)
  91. {
  92. UnityEngine.Object.DestroyImmediate(m_Texture);
  93. m_Texture = null;
  94. m_GameObjectOffset = offset;
  95. }
  96. if (m_ShowPivot != showPivot)
  97. {
  98. m_ShowPivot = showPivot;
  99. m_PivotInstance.SetActive(m_ShowPivot);
  100. UnityEngine.Object.DestroyImmediate(m_Texture);
  101. m_Texture = null;
  102. }
  103. if (Event.current.type != EventType.Repaint)
  104. return;
  105. if (m_PreviewRect != r)
  106. {
  107. UnityEngine.Object.DestroyImmediate(m_Texture);
  108. m_Texture = null;
  109. m_PreviewRect = r;
  110. }
  111. if (m_Texture == null)
  112. {
  113. m_PreviewObject.transform.position = m_ShowPivot ? new Vector2(-m_DocumentPivot.x, -m_DocumentPivot.y) - m_GameObjectOffset : Vector2.zero;
  114. m_RenderUtility.BeginPreview(r, background);
  115. DoRenderPreview();
  116. m_Texture = m_RenderUtility.EndPreview();
  117. }
  118. GUI.DrawTexture(r, m_Texture, ScaleMode.StretchToFill, false);
  119. }
  120. }
  121. void DoRenderPreview()
  122. {
  123. m_RenderableBounds = GetRenderableBounds(m_Root);
  124. float num1 = Mathf.Max(m_RenderableBounds.extents.magnitude, 0.0001f);
  125. float num2 = num1 * 3.8f;
  126. Quaternion quaternion = Quaternion.Euler(-m_PreviewDir.y, -m_PreviewDir.x, 0.0f);
  127. Vector3 vector3 = m_RenderableBounds.center - quaternion * (Vector3.forward * num2);
  128. m_RenderUtility.camera.transform.position = vector3;
  129. m_RenderUtility.camera.transform.rotation = quaternion;
  130. m_RenderUtility.camera.nearClipPlane = num2 - num1 * 1.1f;
  131. m_RenderUtility.camera.farClipPlane = num2 + num1 * 5.1f;
  132. m_RenderUtility.lights[0].intensity = 0.7f;
  133. m_RenderUtility.lights[0].transform.rotation = quaternion * Quaternion.Euler(40f, 40f, 0.0f);
  134. m_RenderUtility.lights[1].intensity = 0.7f;
  135. m_RenderUtility.lights[1].transform.rotation = quaternion * Quaternion.Euler(340f, 218f, 177f);
  136. m_RenderUtility.ambientColor = new Color(0.1f, 0.1f, 0.1f, 0.0f);
  137. if (m_ShowPivot)
  138. {
  139. GL.PushMatrix();
  140. Camera.SetupCurrent(m_RenderUtility.camera);
  141. GL.LoadProjectionMatrix(m_RenderUtility.camera.projectionMatrix);
  142. Handles.color = Color.white;
  143. var p = (Vector2)m_PreviewObject.transform.position;
  144. Handles.DrawLine(m_DocumentPivot.min + p, new Vector2(m_DocumentPivot.min.x, m_DocumentPivot.max.y) + p);
  145. Handles.DrawLine(m_DocumentPivot.min + p, new Vector2(m_DocumentPivot.max.x, m_DocumentPivot.min.y) + p);
  146. Handles.DrawLine(m_DocumentPivot.max + p, new Vector2(m_DocumentPivot.min.x, m_DocumentPivot.max.y) + p);
  147. Handles.DrawLine(m_DocumentPivot.max + p, new Vector2(m_DocumentPivot.max.x, m_DocumentPivot.min.y) + p);
  148. GL.End();
  149. GL.PopMatrix();
  150. }
  151. m_RenderUtility.Render(true);
  152. }
  153. public static Bounds GetRenderableBounds(GameObject go)
  154. {
  155. Bounds bounds = new Bounds();
  156. if (go == null)
  157. return bounds;
  158. var renderers = new List<Renderer>();
  159. go.GetComponentsInChildren(renderers);
  160. foreach (Renderer rendererComponents in renderers)
  161. {
  162. if (bounds.extents == Vector3.zero)
  163. bounds = rendererComponents.bounds;
  164. else if(rendererComponents.enabled)
  165. bounds.Encapsulate(rendererComponents.bounds);
  166. }
  167. return bounds;
  168. }
  169. public void Dispose()
  170. {
  171. if (m_Disposed)
  172. return;
  173. m_RenderUtility.Cleanup();
  174. UnityEngine.Object.DestroyImmediate(m_PreviewObject);
  175. m_PreviewObject = null;
  176. m_Disposed = true;
  177. }
  178. }
  179. }