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