浏览代码

Merge pull request #93 from Unity-Technologies/inspector-preview-mesh

Inspector preview mesh
/main
GitHub 7 年前
当前提交
a5b781b9
共有 8 个文件被更改,包括 166 次插入49 次删除
  1. 17
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/AbstractMaterialGraph.cs
  2. 73
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorView.cs
  3. 14
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Manipulators/Draggable.cs
  4. 15
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewManager.cs
  5. 40
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SerializableMesh.cs
  6. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SerializableMesh.cs.meta
  7. 34
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Manipulators/Scrollable.cs
  8. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Manipulators/Scrollable.cs.meta

17
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/AbstractMaterialGraph.cs


[NonSerialized]
List<Guid> m_RemovedProperties = new List<Guid>();
[NonSerialized]
public InspectorPreviewData previewData = new InspectorPreviewData();
public Mesh previewMesh
{
get { return previewData.mesh; }
set { previewData.mesh = value; }
}
public IEnumerable<IShaderProperty> properties
{
get { return m_Properties; }

configuredTextures = shaderProperties.GetConfiguredTexutres();
return finalShader.GetShaderString(0);
}
}
public class InspectorPreviewData
{
public Mesh mesh;
public Quaternion rotation = Quaternion.identity;
public float scale = 1f;
}
}

73
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorView.cs


namespace UnityEditor.ShaderGraph.Drawing.Inspector
{
[Serializable]
class PersistentMesh
{
[SerializeField]
Mesh m_Mesh;
public Mesh mesh
{
get { return m_Mesh; }
set { m_Mesh = value; }
}
}
public class GraphInspectorView : VisualElement, IDisposable
{
int m_SelectionHash;

List<INode> m_SelectedNodes;
PersistentMesh m_PersistentMasterNodePreviewMesh;
Vector2 m_PreviewScrollPosition;
public Action onUpdateAssetClick { get; set; }
public Action onShowInProjectClick { get; set; }

persistenceKey = "GraphInspector";
m_Graph = graph;
m_SelectedNodes = new List<INode>();

}
bottomContainer.Add(propertiesContainer);
m_PreviewTextureView = new PreviewTextureView {name = "preview", image = Texture2D.blackTexture};
m_PreviewTextureView = new PreviewTextureView { name = "preview", image = Texture2D.blackTexture };
m_PreviewTextureView.AddManipulator(new Draggable(OnMouseDrag, true));
m_PreviewTextureView.AddManipulator(new Scrollable(OnMouseScroll));
m_PreviewScrollPosition = new Vector2(0f, 0f);
m_PreviewMeshPicker = new ObjectField() { objectType = typeof(Mesh) };
m_PreviewMeshPicker.OnValueChanged(OnPreviewMeshChanged);

m_PreviewRenderHandle = previewManager.masterRenderData;
m_PreviewRenderHandle.onPreviewChanged += OnPreviewChanged;
m_PreviewMeshPicker.SetValueAndNotify(m_Graph.previewMesh);
// Nodes missing custom editors:
// - PropertyNode
// - SubGraphInputNode

get { return m_PreviewRenderHandle.shaderData.node as MasterNode; }
}
void OnMouseDrag(Vector2 deltaMouse)
{
Vector2 previewSize = m_PreviewTextureView.contentRect.size;
m_PreviewScrollPosition -= deltaMouse * (Event.current.shift ? 3f : 1f) / Mathf.Min(previewSize.x, previewSize.y) * 140f;
m_PreviewScrollPosition.y = Mathf.Clamp(m_PreviewScrollPosition.y, -90f, 90f);
Quaternion previewRotation = Quaternion.Euler(m_PreviewScrollPosition.y, 0, 0) * Quaternion.Euler(0, m_PreviewScrollPosition.x, 0);
m_Graph.previewData.rotation = previewRotation;
}
void OnMouseScroll(float scrollDelta)
{
m_Graph.previewData.scale -= scrollDelta * .01f;
m_Graph.previewData.scale = Mathf.Clamp(m_Graph.previewData.scale, .1f, 4f);
}
void OnAddProperty()
{
var gm = new GenericMenu();

void OnPreviewMeshChanged(ChangeEvent<UnityEngine.Object> changeEvent)
{
if (changeEvent.newValue == null)
{
m_PreviewRenderHandle.mesh = null;
m_PersistentMasterNodePreviewMesh.mesh = null;
}
if (changedMesh)
{
m_PreviewRenderHandle.mesh = changedMesh;
m_PersistentMasterNodePreviewMesh.mesh = changedMesh;
}
SavePersistentData();
}
public override void OnPersistentDataReady()
{
base.OnPersistentDataReady();
string key = GetFullHierarchicalPersistenceKey();
if (m_Graph.previewMesh != changedMesh)
{
m_Graph.previewData.rotation = Quaternion.identity;
m_Graph.previewData.scale = 1f;
}
m_PersistentMasterNodePreviewMesh = GetOrCreatePersistentData<PersistentMesh>(m_PersistentMasterNodePreviewMesh, key);
m_PreviewMeshPicker.SetValueAndNotify(m_PersistentMasterNodePreviewMesh.mesh);
m_Graph.previewMesh = changedMesh;
}
public void UpdateSelection(IEnumerable<INode> nodes)

14
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Manipulators/Draggable.cs


bool m_Active;
public Draggable(Action<Vector2> handler)
bool m_OutputDeltaMovement;
public Draggable(Action<Vector2> handler, bool outputDeltaMovement = false)
m_OutputDeltaMovement = outputDeltaMovement;
activators.Add(new ManipulatorActivationFilter()
{
button = MouseButton.LeftMouse

{
if (m_Active)
{
m_Handler(evt.localMousePosition);
if (m_OutputDeltaMovement)
{
m_Handler(evt.mouseDelta);
}
else
{
m_Handler(evt.localMousePosition);
}
}
}

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


if (masterRenderData.shaderData != null && m_DirtyPreviews.Contains(masterRenderData.shaderData.node.guid))
m_RenderList3D.Add(masterRenderData);
m_RenderList3D.Sort((data1, data2) => data1.shaderData.shader.GetInstanceID().CompareTo(data2.shaderData.shader.GetInstanceID()));
m_RenderList2D.Sort((data1, data2) => data1.shaderData.shader.GetInstanceID().CompareTo(data2.shaderData.shader.GetInstanceID()));
m_SceneResources.light0.enabled = true;
m_SceneResources.light0.intensity = 1.0f;
m_SceneResources.light0.transform.rotation = Quaternion.Euler(50f, 50f, 0);

m_SceneResources.camera.transform.rotation = Quaternion.identity;
m_SceneResources.camera.orthographicSize = 1;
m_SceneResources.camera.orthographic = true;
m_RenderList2D.Sort((data1, data2) => data1.shaderData.shader.GetInstanceID().CompareTo(data2.shaderData.shader.GetInstanceID()));
foreach (var renderData in m_RenderList2D)
{
int outputId;

m_SceneResources.camera.transform.position = -Vector3.forward * 5;
m_SceneResources.camera.transform.rotation = Quaternion.identity;
m_SceneResources.camera.orthographic = false;
m_RenderList3D.Sort((data1, data2) => data1.shaderData.shader.GetInstanceID().CompareTo(data2.shaderData.shader.GetInstanceID()));
foreach (var renderData in m_RenderList3D)
{
int outputId;

RenderTexture.active = renderData.renderTexture;
GL.Clear(true, true, Color.black);
Graphics.Blit(Texture2D.whiteTexture, renderData.renderTexture, m_SceneResources.checkerboardMaterial);
var mesh = renderData.mesh ?? m_SceneResources.sphere;
Graphics.DrawMesh(mesh, Matrix4x4.TRS(-mesh.bounds.center, Quaternion.identity, Vector3.one), m_PreviewMaterial, 1, m_SceneResources.camera, 0, m_PreviewPropertyBlock, ShadowCastingMode.Off, false, null, false);
var mesh = (renderData == masterRenderData && m_Graph.previewMesh) ? m_Graph.previewMesh : m_SceneResources.sphere;
Quaternion rotation = (renderData == masterRenderData) ? m_Graph.previewData.rotation : Quaternion.identity;
float scale = (renderData == masterRenderData) ? m_Graph.previewData.scale : 1f;
Graphics.DrawMesh(mesh, Matrix4x4.TRS(-mesh.bounds.center, rotation, Vector3.one * scale), m_PreviewMaterial, 1, m_SceneResources.camera, 0, m_PreviewPropertyBlock, ShadowCastingMode.Off, false, null, false);
var previousUseSRP = Unsupported.useScriptableRenderPipeline;
Unsupported.useScriptableRenderPipeline = renderData.shaderData.node is IMasterNode;
m_SceneResources.camera.Render();

public class PreviewRenderData
{
public PreviewShaderData shaderData { get; set; }
public Mesh mesh { get; set; }
public RenderTexture renderTexture { get; set; }
public Texture texture { get; set; }
public OnPreviewChanged onPreviewChanged;

40
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SerializableMesh.cs


using System;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class SerializableMesh
{
[SerializeField]
private string m_SerializedMesh;
[Serializable]
private class MeshHelper
{
public Mesh mesh;
}
public Mesh mesh
{
get
{
if (string.IsNullOrEmpty(m_SerializedMesh))
return null;
var meshHelper = new MeshHelper();
EditorJsonUtility.FromJsonOverwrite(m_SerializedMesh, meshHelper);
return meshHelper.mesh;
}
set
{
if (mesh == value)
return;
var meshHelper = new MeshHelper();
meshHelper.mesh = value;
m_SerializedMesh = EditorJsonUtility.ToJson(meshHelper, true);
}
}
}
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SerializableMesh.cs.meta


fileFormatVersion: 2
guid: 944c4fc9d004e7845adb3f86843ab742
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

34
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Manipulators/Scrollable.cs


using System;
using UnityEngine.Experimental.UIElements;
namespace UnityEditor.ShaderGraph.Drawing
{
public class Scrollable : MouseManipulator
{
Action<float> m_Handler;
public Scrollable(Action<float> handler)
{
m_Handler = handler;
activators.Add(new ManipulatorActivationFilter()
{
button = MouseButton.LeftMouse
});
}
protected override void RegisterCallbacksOnTarget()
{
target.RegisterCallback(new EventCallback<WheelEvent>(OnMouseWheel), Capture.NoCapture);
}
protected override void UnregisterCallbacksFromTarget()
{
target.UnregisterCallback(new EventCallback<WheelEvent>(OnMouseWheel), Capture.NoCapture);
}
void OnMouseWheel(WheelEvent evt)
{
m_Handler(evt.delta.y);
}
}
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Manipulators/Scrollable.cs.meta


fileFormatVersion: 2
guid: 1fde0872f0455304bb082dbee731fabf
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存