PreviewSceneResources.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System;
  2. using UnityEditor.SceneManagement;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. namespace UnityEditor.ShaderGraph.Drawing
  6. {
  7. class PreviewSceneResources : IDisposable
  8. {
  9. readonly Scene m_Scene;
  10. Camera m_Camera;
  11. public Light light0 { get; private set; }
  12. public Light light1 { get; private set; }
  13. Material m_CheckerboardMaterial;
  14. Material m_BlitNoAlphaMaterial;
  15. static readonly Mesh[] s_Meshes = { null, null, null, null, null };
  16. static readonly GUIContent[] s_MeshIcons = { null, null, null, null, null };
  17. static readonly GUIContent[] s_LightIcons = { null, null };
  18. static readonly GUIContent[] s_TimeIcons = { null, null };
  19. static GameObject CreateLight()
  20. {
  21. GameObject lightGO = EditorUtility.CreateGameObjectWithHideFlags("PreRenderLight", HideFlags.HideAndDontSave, typeof(Light));
  22. var light = lightGO.GetComponent<Light>();
  23. light.type = LightType.Directional;
  24. light.intensity = 1.0f;
  25. light.enabled = false;
  26. return lightGO;
  27. }
  28. public PreviewSceneResources()
  29. {
  30. m_Scene = EditorSceneManager.NewPreviewScene();
  31. var camGO = EditorUtility.CreateGameObjectWithHideFlags("Preview Scene Camera", HideFlags.HideAndDontSave, typeof(Camera));
  32. SceneManager.MoveGameObjectToScene(camGO, m_Scene);
  33. m_Camera = camGO.GetComponent<Camera>();
  34. EditorUtility.SetCameraAnimateMaterials(camera, true);
  35. camera.cameraType = CameraType.Preview;
  36. camera.enabled = false;
  37. camera.clearFlags = CameraClearFlags.Depth;
  38. camera.fieldOfView = 15;
  39. camera.farClipPlane = 10.0f;
  40. camera.nearClipPlane = 2.0f;
  41. camera.backgroundColor = new Color(49.0f / 255.0f, 49.0f / 255.0f, 49.0f / 255.0f, 1.0f);
  42. // Explicitly use forward rendering for all previews
  43. // (deferred fails when generating some static previews at editor launch; and we never want
  44. // vertex lit previews if that is chosen in the player settings)
  45. camera.renderingPath = RenderingPath.Forward;
  46. camera.useOcclusionCulling = false;
  47. camera.scene = m_Scene;
  48. var l0 = CreateLight();
  49. SceneManager.MoveGameObjectToScene(l0, m_Scene);
  50. //previewScene.AddGameObject(l0);
  51. light0 = l0.GetComponent<Light>();
  52. var l1 = CreateLight();
  53. SceneManager.MoveGameObjectToScene(l1, m_Scene);
  54. //previewScene.AddGameObject(l1);
  55. light1 = l1.GetComponent<Light>();
  56. light0.color = new Color(0.769f, 0.769f, 0.769f, 1); // SceneView.kSceneViewFrontLight
  57. light1.transform.rotation = Quaternion.Euler(340, 218, 177);
  58. light1.color = new Color(.4f, .4f, .45f, 0f) * .7f;
  59. m_CheckerboardMaterial = new Material(Shader.Find("Hidden/Checkerboard"));
  60. m_BlitNoAlphaMaterial = new Material(Shader.Find("Hidden/BlitNoAlpha"));
  61. checkerboardMaterial.hideFlags = HideFlags.HideAndDontSave;
  62. blitNoAlphaMaterial.hideFlags = HideFlags.HideAndDontSave;
  63. if (s_Meshes[0] == null)
  64. {
  65. var handleGo = (GameObject)EditorGUIUtility.LoadRequired("Previews/PreviewMaterials.fbx");
  66. // @TODO: temp workaround to make it not render in the scene
  67. handleGo.SetActive(false);
  68. foreach (Transform t in handleGo.transform)
  69. {
  70. var meshFilter = t.GetComponent<MeshFilter>();
  71. switch (t.name)
  72. {
  73. case "sphere":
  74. s_Meshes[0] = meshFilter.sharedMesh;
  75. break;
  76. case "cube":
  77. s_Meshes[1] = meshFilter.sharedMesh;
  78. break;
  79. case "cylinder":
  80. s_Meshes[2] = meshFilter.sharedMesh;
  81. break;
  82. case "torus":
  83. s_Meshes[3] = meshFilter.sharedMesh;
  84. break;
  85. default:
  86. Debug.LogWarning("Something is wrong, weird object found: " + t.name);
  87. break;
  88. }
  89. }
  90. s_MeshIcons[0] = EditorGUIUtility.IconContent("PreMatSphere");
  91. s_MeshIcons[1] = EditorGUIUtility.IconContent("PreMatCube");
  92. s_MeshIcons[2] = EditorGUIUtility.IconContent("PreMatCylinder");
  93. s_MeshIcons[3] = EditorGUIUtility.IconContent("PreMatTorus");
  94. s_MeshIcons[4] = EditorGUIUtility.IconContent("PreMatQuad");
  95. s_LightIcons[0] = EditorGUIUtility.IconContent("PreMatLight0");
  96. s_LightIcons[1] = EditorGUIUtility.IconContent("PreMatLight1");
  97. s_TimeIcons[0] = EditorGUIUtility.IconContent("PlayButton");
  98. s_TimeIcons[1] = EditorGUIUtility.IconContent("PauseButton");
  99. Mesh quadMesh = Resources.GetBuiltinResource(typeof(Mesh), "Quad.fbx") as Mesh;
  100. s_Meshes[4] = quadMesh;
  101. }
  102. }
  103. public Mesh sphere
  104. {
  105. get { return s_Meshes[0]; }
  106. }
  107. public Mesh quad
  108. {
  109. get { return s_Meshes[4]; }
  110. }
  111. public Material checkerboardMaterial
  112. {
  113. get { return m_CheckerboardMaterial; }
  114. }
  115. public Material blitNoAlphaMaterial
  116. {
  117. get { return m_BlitNoAlphaMaterial; }
  118. }
  119. public Camera camera
  120. {
  121. get { return m_Camera; }
  122. }
  123. public void Dispose()
  124. {
  125. if (light0 != null)
  126. {
  127. UnityEngine.Object.DestroyImmediate(light0.gameObject);
  128. light0 = null;
  129. }
  130. if (light1 != null)
  131. {
  132. UnityEngine.Object.DestroyImmediate(light1.gameObject);
  133. light1 = null;
  134. }
  135. if (camera != null)
  136. {
  137. UnityEngine.Object.DestroyImmediate(camera.gameObject);
  138. m_Camera = null;
  139. }
  140. if (checkerboardMaterial != null)
  141. {
  142. UnityEngine.Object.DestroyImmediate(checkerboardMaterial, true);
  143. m_CheckerboardMaterial = null;
  144. }
  145. if (blitNoAlphaMaterial != null)
  146. {
  147. UnityEngine.Object.DestroyImmediate(blitNoAlphaMaterial, true);
  148. m_BlitNoAlphaMaterial = null;
  149. }
  150. EditorSceneManager.ClosePreviewScene(m_Scene);
  151. }
  152. }
  153. }