浏览代码

Fix preview rendering bugs (causing re-use of previous texture and disapperance of checkerboard background) and persistence (remembers zoom etc. between assembly reloads, scene changes, even play mode!)

/main
Peter Bay Bastian 7 年前
当前提交
b6a4d98d
共有 5 个文件被更改,包括 17 次插入15 次删除
  1. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs
  2. 14
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphPreviewGenerator.cs
  3. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewSystem.cs
  4. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/GraphEditorView.cs
  5. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Resources/Shaders/Checkerboard.shader

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs


m_GraphEditorView.onUpdateAssetClick += UpdateAsset;
m_GraphEditorView.onConvertToSubgraphClick += ToSubGraph;
m_GraphEditorView.onShowInProjectClick += PingAsset;
m_GraphEditorView.RegisterCallback<PostLayoutEvent>(OnPostLayout);
rootVisualContainer.Add(graphEditorView);
}
}

if (materialGraph == null)
return;
if (graphEditorView == null)
graphEditorView = new GraphEditorView(materialGraph, selected);
graphEditorView = new GraphEditorView(materialGraph, selected) { persistenceKey = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(selected)) };
if (graphEditorView != null)
graphEditorView.previewSystem.Update();
}

graphObject.graph.OnEnable();
graphObject.graph.ValidateGraph();
graphEditorView = new GraphEditorView(m_GraphObject.graph as AbstractMaterialGraph, selected);
graphEditorView = new GraphEditorView(m_GraphObject.graph as AbstractMaterialGraph, selected) { persistenceKey = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(selected)) };
graphEditorView.RegisterCallback<PostLayoutEvent>(OnPostLayout);
titleContent = new GUIContent(selected.name);
Repaint();

14
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphPreviewGenerator.cs


Light1.color = new Color(.4f, .4f, .45f, 0f) * .7f;
m_CheckerboardMaterial = new Material(Shader.Find("Hidden/Checkerboard"));
m_CheckerboardMaterial.SetFloat("_X", 32);
m_CheckerboardMaterial.SetFloat("_Y", 32);
m_CheckerboardMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
m_CheckerboardMaterial.hideFlags = HideFlags.HideAndDontSave;
if (s_Meshes[0] == null)
{

RenderSettings.ambientProbe = ambientProbe;
m_Camera.targetTexture = renderTexture;
var previousRenderTexure = RenderTexture.active;
RenderTexture.active = renderTexture;
GL.Clear(true, true, Color.black);
Graphics.Blit(Texture2D.whiteTexture, renderTexture, m_CheckerboardMaterial);

m_Camera.Render();
Unsupported.useScriptableRenderPipeline = oldAllowPipes;
RenderTexture.active = previousRenderTexure;
Unsupported.RestoreOverrideRenderSettings();
Light0.enabled = false;

public void Dispose()
{
if (Light0 == null)
if (Light0 != null)
if (Light1 == null)
if (Light1 != null)
if (m_Camera == null)
if (m_Camera != null)
if (m_CheckerboardMaterial == null)
if (m_CheckerboardMaterial != null)
{
UnityEngine.Object.DestroyImmediate(m_CheckerboardMaterial);
m_CheckerboardMaterial = null;

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewSystem.cs


{
m_Graph = graph;
m_PreviewMaterial = new Material(Shader.Find("Unlit/Color")) { hideFlags = HideFlags.HideInHierarchy };
m_PreviewMaterial.hideFlags = HideFlags.HideInHierarchy;
m_PreviewMaterial.hideFlags = HideFlags.HideAndDontSave;
m_PreviewPropertyBlock = new MaterialPropertyBlock();
m_ErrorTexture = new Texture2D(2, 2);
m_ErrorTexture.SetPixel(0, 0, Color.magenta);

4
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/GraphEditorView.cs


var content = new VisualElement { name = "content" };
{
m_GraphView = new MaterialGraphView(graph) { name = "GraphView" };
m_GraphView.persistenceKey = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(asset));
m_GraphView = new MaterialGraphView(graph) { name = "GraphView", persistenceKey = "MaterialGraphView" };
m_GraphView.SetupZoom(ContentZoomer.DefaultMinScale, ContentZoomer.DefaultMaxScale);
m_GraphView.AddManipulator(new ContentDragger());
m_GraphView.AddManipulator(new RectangleSelector());

onUpdateAssetClick = null;
onConvertToSubgraphClick = null;
onShowInProjectClick = null;
m_Graph.onChange -= OnGraphChange;
if (m_GraphInspectorView != null) m_GraphInspectorView.Dispose();
if (previewSystem != null)
{

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Resources/Shaders/Checkerboard.shader


#include "UnityCG.cginc"
uniform float _X;
uniform float _Y;
static const float rows = 32;
static const float columns = 32;
static const float4 col1 = float4(0.15, 0.15, 0.15, 1.0);
static const float4 col2 = float4(0.3, 0.3, 0.3, 1.0);

float total = floor(i.uv.x * _X) + floor(i.uv.y * _Y);
float total = floor(i.uv.x * rows) + floor(i.uv.y * columns);
bool isEven = total % 2.0 == 0.0;
return isEven ? col1 : col2;
}

正在加载...
取消
保存