暫無描述
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 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. GameObject m_Root;
  20. Rect m_DocumentPivot;
  21. public PSDGameObjectPreviewData(GameObject assetPrefab, Rect documentPivot)
  22. {
  23. m_RenderUtility = new PreviewRenderUtility();
  24. m_RenderUtility.camera.fieldOfView = 30f;
  25. m_Root = new GameObject();
  26. m_PreviewObject = GameObject.Instantiate(assetPrefab, Vector3.zero, Quaternion.identity);
  27. m_PreviewObject.transform.parent = m_Root.transform;
  28. m_DocumentPivot = documentPivot;
  29. m_RenderUtility.AddSingleGO(m_Root);
  30. }
  31. static Vector2 Drag2D(Vector2 scrollPosition, Rect position)
  32. {
  33. int controlId = GUIUtility.GetControlID(s_SliderHash, FocusType.Passive);
  34. var current = Event.current;
  35. switch (current.GetTypeForControl(controlId))
  36. {
  37. case UnityEngine.EventType.MouseDown:
  38. if (position.Contains(current.mousePosition) && (double)position.width > 50.0)
  39. {
  40. GUIUtility.hotControl = controlId;
  41. current.Use();
  42. EditorGUIUtility.SetWantsMouseJumping(1);
  43. break;
  44. }
  45. break;
  46. case UnityEngine.EventType.MouseUp:
  47. if (GUIUtility.hotControl == controlId)
  48. GUIUtility.hotControl = 0;
  49. EditorGUIUtility.SetWantsMouseJumping(0);
  50. break;
  51. case UnityEngine.EventType.MouseDrag:
  52. if (GUIUtility.hotControl == controlId)
  53. {
  54. scrollPosition -= current.delta * (current.shift ? 3f : 1f) / Mathf.Min(position.width, position.height) * 140f;
  55. current.Use();
  56. GUI.changed = true;
  57. break;
  58. }
  59. break;
  60. }
  61. return scrollPosition;
  62. }
  63. public void DrawPreview(Rect r, GUIStyle background, Vector2 offset, bool showPivot)
  64. {
  65. if (!ShaderUtil.hardwareSupportsRectRenderTexture)
  66. {
  67. if (Event.current.type != UnityEngine.EventType.Repaint)
  68. return;
  69. EditorGUI.DropShadowLabel(new Rect(r.x, r.y, r.width, 40f), "Preview requires\nrender texture support");
  70. }
  71. else
  72. {
  73. Vector2 vector2 = Drag2D(m_PreviewDir, r);
  74. if (vector2 != m_PreviewDir)
  75. {
  76. UnityEngine.Object.DestroyImmediate(m_Texture);
  77. m_Texture = null;
  78. m_PreviewDir = vector2;
  79. }
  80. if (m_GameObjectOffset != offset)
  81. {
  82. UnityEngine.Object.DestroyImmediate(m_Texture);
  83. m_Texture = null;
  84. m_GameObjectOffset = offset;
  85. }
  86. if (Event.current.type != EventType.Repaint)
  87. return;
  88. if (m_PreviewRect != r)
  89. {
  90. UnityEngine.Object.DestroyImmediate(m_Texture);
  91. m_Texture = null;
  92. m_PreviewRect = r;
  93. }
  94. if (m_Texture == null)
  95. {
  96. m_PreviewObject.transform.position = Vector2.zero;
  97. m_RenderUtility.BeginPreview(r, background);
  98. DoRenderPreview();
  99. m_Texture = m_RenderUtility.EndPreview();
  100. }
  101. GUI.DrawTexture(r, m_Texture, ScaleMode.StretchToFill, false);
  102. }
  103. }
  104. void DoRenderPreview()
  105. {
  106. m_RenderableBounds = GetRenderableBounds(m_Root);
  107. float num1 = Mathf.Max(m_RenderableBounds.extents.magnitude, 0.0001f);
  108. float num2 = num1 * 3.8f;
  109. Quaternion quaternion = Quaternion.Euler(-m_PreviewDir.y, -m_PreviewDir.x, 0.0f);
  110. Vector3 vector3 = m_RenderableBounds.center - quaternion * (Vector3.forward * num2);
  111. m_RenderUtility.camera.transform.position = vector3;
  112. m_RenderUtility.camera.transform.rotation = quaternion;
  113. m_RenderUtility.camera.nearClipPlane = num2 - num1 * 1.1f;
  114. m_RenderUtility.camera.farClipPlane = num2 + num1 * 5.1f;
  115. m_RenderUtility.lights[0].intensity = 0.7f;
  116. m_RenderUtility.lights[0].transform.rotation = quaternion * Quaternion.Euler(40f, 40f, 0.0f);
  117. m_RenderUtility.lights[1].intensity = 0.7f;
  118. m_RenderUtility.lights[1].transform.rotation = quaternion * Quaternion.Euler(340f, 218f, 177f);
  119. m_RenderUtility.ambientColor = new Color(0.1f, 0.1f, 0.1f, 0.0f);
  120. m_RenderUtility.Render(true);
  121. }
  122. public static Bounds GetRenderableBounds(GameObject go)
  123. {
  124. Bounds bounds = new Bounds();
  125. if (go == null)
  126. return bounds;
  127. var renderers = new List<Renderer>();
  128. go.GetComponentsInChildren(renderers);
  129. foreach (Renderer rendererComponents in renderers)
  130. {
  131. if (bounds.extents == Vector3.zero)
  132. bounds = rendererComponents.bounds;
  133. else if(rendererComponents.enabled)
  134. bounds.Encapsulate(rendererComponents.bounds);
  135. }
  136. return bounds;
  137. }
  138. public void Dispose()
  139. {
  140. if (m_Disposed)
  141. return;
  142. m_RenderUtility.Cleanup();
  143. UnityEngine.Object.DestroyImmediate(m_PreviewObject);
  144. m_PreviewObject = null;
  145. m_Disposed = true;
  146. }
  147. }
  148. }