浏览代码

Move actual preview rendering into PreviewManager

/main
Peter Bay Bastian 7 年前
当前提交
95cfcc60
共有 3 个文件被更改,包括 263 次插入5 次删除
  1. 63
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewManager.cs
  2. 202
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/PreviewSceneResources.cs
  3. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/PreviewSceneResources.cs.meta

63
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewManager.cs


using UnityEngine;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph;
using UnityEngine.Rendering;
using Object = UnityEngine.Object;
namespace UnityEditor.ShaderGraph.Drawing

HashSet<Guid> m_TimeDependentPreviews = new HashSet<Guid>();
Material m_PreviewMaterial;
MaterialPropertyBlock m_PreviewPropertyBlock;
MaterialGraphPreviewGenerator m_PreviewGenerator = new MaterialGraphPreviewGenerator();
PreviewSceneResources m_SceneResources;
Texture2D m_ErrorTexture;
DateTime m_LastUpdate;

m_ErrorTexture.SetPixel(1, 1, Color.magenta);
m_ErrorTexture.filterMode = FilterMode.Point;
m_ErrorTexture.Apply();
m_SceneResources = new PreviewSceneResources();
foreach (var node in m_Graph.GetNodes<INode>())
AddPreview(node);

}
var node = m_Graph.GetNodeFromGuid(nodeGuid);
m_PreviewMaterial.shader = previewData.shader;
m_PreviewGenerator.DoRenderPreview(previewData.renderTexture, m_PreviewMaterial, previewData.mesh, previewData.previewMode, node is IMasterNode, time, m_PreviewPropertyBlock);
if (previewData.previewMode == PreviewMode.Preview3D)
{
m_SceneResources.camera.transform.position = -Vector3.forward * 5;
m_SceneResources.camera.transform.rotation = Quaternion.identity;
m_SceneResources.camera.orthographic = false;
}
else
{
m_SceneResources.camera.transform.position = -Vector3.forward * 2;
m_SceneResources.camera.transform.rotation = Quaternion.identity;
m_SceneResources.camera.orthographicSize = 1;
m_SceneResources.camera.orthographic = true;
}
m_SceneResources.camera.targetTexture = previewData.renderTexture;
var previousRenderTexure = RenderTexture.active;
RenderTexture.active = previewData.renderTexture;
GL.Clear(true, true, Color.black);
Graphics.Blit(Texture2D.whiteTexture, previewData.renderTexture, m_SceneResources.checkerboardMaterial);
EditorUtility.SetCameraAnimateMaterialsTime(m_SceneResources.camera, time);
m_SceneResources.light0.enabled = true;
m_SceneResources.light0.intensity = 1.0f;
m_SceneResources.light0.transform.rotation = Quaternion.Euler(50f, 50f, 0);
m_SceneResources.light1.enabled = true;
m_SceneResources.light1.intensity = 1.0f;
m_SceneResources.camera.clearFlags = CameraClearFlags.Depth;
Mesh mesh = previewData.previewMode == PreviewMode.Preview2D ? m_SceneResources.quad : (previewData.mesh ?? m_SceneResources.sphere);
Graphics.DrawMesh(
mesh,
previewData.previewMode == PreviewMode.Preview3D ? Matrix4x4.TRS(-mesh.bounds.center, Quaternion.identity, Vector3.one) : Matrix4x4.identity,
m_PreviewMaterial,
1,
m_SceneResources.camera,
0,
m_PreviewPropertyBlock,
ShadowCastingMode.Off,
false,
null,
false);
var previousUseSRP = Unsupported.useScriptableRenderPipeline;
Unsupported.useScriptableRenderPipeline = node is IMasterNode;
m_SceneResources.camera.Render();
Unsupported.useScriptableRenderPipeline = previousUseSRP;
RenderTexture.active = previousRenderTexure;
m_SceneResources.light0.enabled = false;
m_SceneResources.light1.enabled = false;
previewData.texture = previewData.renderTexture;
}

if (m_PreviewMaterial != null)
Object.DestroyImmediate(m_PreviewMaterial, true);
m_PreviewMaterial = null;
if (m_PreviewGenerator != null)
m_PreviewGenerator.Dispose();
m_PreviewGenerator = null;
if (m_SceneResources != null)
m_SceneResources.Dispose();
m_SceneResources = null;
var previews = m_Previews.ToList();
foreach (var kvp in previews)
DestroyPreview(kvp.Key, kvp.Value);

202
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/PreviewSceneResources.cs


using System;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace UnityEditor.ShaderGraph.Drawing
{
public class PreviewSceneResources : IDisposable
{
readonly Scene m_Scene;
static Mesh s_Quad;
Camera m_Camera;
public Light light0 { get; private set; }
public Light light1 { get; private set; }
Material m_CheckerboardMaterial;
static readonly Mesh[] s_Meshes = { null, null, null, null, null };
static readonly GUIContent[] s_MeshIcons = { null, null, null, null, null };
static readonly GUIContent[] s_LightIcons = { null, null };
static readonly GUIContent[] s_TimeIcons = { null, null };
static GameObject CreateLight()
{
GameObject lightGO = EditorUtility.CreateGameObjectWithHideFlags("PreRenderLight", HideFlags.HideAndDontSave, typeof(Light));
var light = lightGO.GetComponent<Light>();
light.type = LightType.Directional;
light.intensity = 1.0f;
light.enabled = false;
return lightGO;
}
public PreviewSceneResources()
{
m_Scene = EditorSceneManager.NewPreviewScene();
var camGO = EditorUtility.CreateGameObjectWithHideFlags("Preview Scene Camera", HideFlags.HideAndDontSave, typeof(Camera));
SceneManager.MoveGameObjectToScene(camGO, m_Scene);
m_Camera = camGO.GetComponent<Camera>();
EditorUtility.SetCameraAnimateMaterials(camera, true);
camera.cameraType = CameraType.Preview;
camera.enabled = false;
camera.clearFlags = CameraClearFlags.Depth;
camera.fieldOfView = 15;
camera.farClipPlane = 10.0f;
camera.nearClipPlane = 2.0f;
camera.backgroundColor = new Color(49.0f / 255.0f, 49.0f / 255.0f, 49.0f / 255.0f, 1.0f);
// Explicitly use forward rendering for all previews
// (deferred fails when generating some static previews at editor launch; and we never want
// vertex lit previews if that is chosen in the player settings)
camera.renderingPath = RenderingPath.Forward;
camera.useOcclusionCulling = false;
camera.scene = m_Scene;
var l0 = CreateLight();
SceneManager.MoveGameObjectToScene(l0, m_Scene);
//previewScene.AddGameObject(l0);
light0 = l0.GetComponent<Light>();
var l1 = CreateLight();
SceneManager.MoveGameObjectToScene(l1, m_Scene);
//previewScene.AddGameObject(l1);
light1 = l1.GetComponent<Light>();
light0.color = new Color(0.769f, 0.769f, 0.769f, 1); // SceneView.kSceneViewFrontLight
light1.transform.rotation = Quaternion.Euler(340, 218, 177);
light1.color = new Color(.4f, .4f, .45f, 0f) * .7f;
m_CheckerboardMaterial = new Material(Shader.Find("Hidden/Checkerboard"));
checkerboardMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
checkerboardMaterial.hideFlags = HideFlags.HideAndDontSave;
if (s_Meshes[0] == null)
{
var handleGo = (GameObject)EditorGUIUtility.LoadRequired("Previews/PreviewMaterials.fbx");
// @TODO: temp workaround to make it not render in the scene
handleGo.SetActive(false);
foreach (Transform t in handleGo.transform)
{
var meshFilter = t.GetComponent<MeshFilter>();
switch (t.name)
{
case "sphere":
s_Meshes[0] = meshFilter.sharedMesh;
break;
case "cube":
s_Meshes[1] = meshFilter.sharedMesh;
break;
case "cylinder":
s_Meshes[2] = meshFilter.sharedMesh;
break;
case "torus":
s_Meshes[3] = meshFilter.sharedMesh;
break;
default:
Debug.Log("Something is wrong, weird object found: " + t.name);
break;
}
}
s_MeshIcons[0] = EditorGUIUtility.IconContent("PreMatSphere");
s_MeshIcons[1] = EditorGUIUtility.IconContent("PreMatCube");
s_MeshIcons[2] = EditorGUIUtility.IconContent("PreMatCylinder");
s_MeshIcons[3] = EditorGUIUtility.IconContent("PreMatTorus");
s_MeshIcons[4] = EditorGUIUtility.IconContent("PreMatQuad");
s_LightIcons[0] = EditorGUIUtility.IconContent("PreMatLight0");
s_LightIcons[1] = EditorGUIUtility.IconContent("PreMatLight1");
s_TimeIcons[0] = EditorGUIUtility.IconContent("PlayButton");
s_TimeIcons[1] = EditorGUIUtility.IconContent("PauseButton");
Mesh quadMesh = Resources.GetBuiltinResource(typeof(Mesh), "Quad.fbx") as Mesh;
s_Meshes[4] = quadMesh;
}
if (s_Quad == null)
{
var vertices = new[]
{
new Vector3(-1f, -1f, 0f),
new Vector3(1f, 1f, 0f),
new Vector3(1f, -1f, 0f),
new Vector3(-1f, 1f, 0f)
};
var uvs = new[]
{
new Vector2(0f, 0f),
new Vector2(1f, 1f),
new Vector2(1f, 0f),
new Vector2(0f, 1f)
};
var indices = new[] { 0, 1, 2, 1, 0, 3 };
s_Quad = new Mesh
{
vertices = vertices,
uv = uvs,
triangles = indices
};
s_Quad.RecalculateNormals();
s_Quad.RecalculateBounds();
}
}
public Mesh sphere
{
get { return s_Meshes[0]; }
}
public Mesh quad
{
get { return s_Quad; }
}
public Material checkerboardMaterial
{
get { return m_CheckerboardMaterial; }
}
public Camera camera
{
get { return m_Camera; }
}
public void Dispose()
{
if (light0 != null)
{
UnityEngine.Object.DestroyImmediate(light0.gameObject);
light0 = null;
}
if (light1 != null)
{
UnityEngine.Object.DestroyImmediate(light1.gameObject);
light1 = null;
}
if (camera != null)
{
UnityEngine.Object.DestroyImmediate(camera.gameObject);
m_Camera = null;
}
if (checkerboardMaterial != null)
{
UnityEngine.Object.DestroyImmediate(checkerboardMaterial);
m_CheckerboardMaterial = null;
}
EditorSceneManager.ClosePreviewScene(m_Scene);
}
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/PreviewSceneResources.cs.meta


fileFormatVersion: 2
guid: 37873c3b010e40d1ac1e6aace3c0fb10
timeCreated: 1510662606
正在加载...
取消
保存