Tim Cooper
7 年前
当前提交
9e0273b3
共有 9 个文件被更改,包括 164 次插入 和 276 次删除
-
24MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorPresenter.cs
-
122MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorView.cs
-
4MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/GraphEditorPresenter.cs
-
10MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewSystem.cs
-
9MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/Presenters.meta
-
9MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/Views.meta
-
262MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/NodePreviewPresenter.cs
|
|||
fileFormatVersion: 2 |
|||
guid: 2d08dfb360644031a5e64558d4a93263 |
|||
folderAsset: yes |
|||
timeCreated: 1503663071 |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 0969ff891b874694aa732e1240ee159b |
|||
folderAsset: yes |
|||
timeCreated: 1503663067 |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Reflection; |
|||
using UnityEditor.Experimental.UIElements.GraphView; |
|||
using UnityEngine; |
|||
using UnityEngine.Graphing; |
|||
using UnityEngine.MaterialGraph; |
|||
using Object = UnityEngine.Object; |
|||
using UnityEditor.Experimental.UIElements.GraphView; |
|||
|
|||
namespace UnityEditor.MaterialGraph.Drawing |
|||
{ |
|||
[Serializable] |
|||
public class NodePreviewPresenter : ScriptableObject |
|||
{ |
|||
protected NodePreviewPresenter() |
|||
{} |
|||
|
|||
MaterialGraphPreviewGenerator m_PreviewGenerator; |
|||
|
|||
[NonSerialized] |
|||
int m_LastShaderVersion = -1; |
|||
|
|||
[NonSerialized] |
|||
Material m_PreviewMaterial; |
|||
|
|||
[NonSerialized] |
|||
Shader m_PreviewShader; |
|||
|
|||
AbstractMaterialNode m_Node; |
|||
|
|||
[SerializeField] |
|||
ModificationScope m_ModificationScope; |
|||
|
|||
// Null means no modification is currently in progress.
|
|||
public ModificationScope modificationScope |
|||
{ |
|||
get |
|||
{ |
|||
return m_ModificationScope; |
|||
} |
|||
set |
|||
{ |
|||
// External changes can only set the modification scope higher, to prevent missing out on a previously set shader regeneration.
|
|||
if (m_ModificationScope >= value) |
|||
return; |
|||
m_ModificationScope = value; |
|||
} |
|||
} |
|||
|
|||
[NonSerialized] |
|||
Texture m_Texture; |
|||
|
|||
public Texture texture |
|||
{ |
|||
get { return m_Texture; } |
|||
} |
|||
|
|||
PreviewMode m_GeneratedShaderMode = PreviewMode.Preview2D; |
|||
|
|||
public Material previewMaterial |
|||
{ |
|||
get |
|||
{ |
|||
if (m_PreviewMaterial == null) |
|||
{ |
|||
m_PreviewMaterial = new Material(Shader.Find("Unlit/Color")) {hideFlags = HideFlags.HideInHierarchy}; |
|||
m_PreviewMaterial.hideFlags = HideFlags.HideInHierarchy; |
|||
} |
|||
return m_PreviewMaterial; |
|||
} |
|||
} |
|||
|
|||
MaterialGraphPreviewGenerator previewGenerator |
|||
{ |
|||
get |
|||
{ |
|||
if (m_PreviewGenerator == null) |
|||
{ |
|||
m_PreviewGenerator = new MaterialGraphPreviewGenerator(m_Node is MasterNode); |
|||
} |
|||
return m_PreviewGenerator; |
|||
} |
|||
} |
|||
|
|||
public void Initialize(AbstractMaterialNode node) |
|||
{ |
|||
m_Node = node; |
|||
m_ModificationScope = ModificationScope.Graph; |
|||
} |
|||
|
|||
public void UpdateTexture() |
|||
{ |
|||
if (m_Node == null) |
|||
{ |
|||
m_Texture = null; |
|||
return; |
|||
} |
|||
|
|||
if (m_Node.hasPreview == false) |
|||
{ |
|||
m_Texture = null; |
|||
return; |
|||
} |
|||
|
|||
var scope = m_ModificationScope; |
|||
m_ModificationScope = ModificationScope.Nothing; |
|||
|
|||
if (scope != ModificationScope.Nothing) |
|||
{ |
|||
if (scope >= ModificationScope.Graph) |
|||
{ |
|||
var stage = NodeUtils.FindEffectiveShaderStage(m_Node, true); |
|||
/* if (!(m_Node is AbstractSurfaceMasterNode) && stage == ShaderStage.Vertex) |
|||
{ |
|||
// Debug.Log(m_Node.name + ":" + stage);
|
|||
m_Texture = null; |
|||
return; |
|||
}*/ |
|||
// TODO: Handle shader regeneration error
|
|||
var status = UpdatePreviewShader(); |
|||
} |
|||
m_Texture = RenderPreview(new Vector2(256, 256)); |
|||
} |
|||
else if (texture == null) |
|||
{ |
|||
m_Texture = RenderPreview(new Vector2(256, 256)); |
|||
} |
|||
} |
|||
|
|||
void OnDisable() |
|||
{ |
|||
if (m_PreviewGenerator != null) |
|||
m_PreviewGenerator.Dispose(); |
|||
} |
|||
|
|||
protected virtual string GetPreviewShaderString() |
|||
{ |
|||
// TODO: this is a workaround right now.
|
|||
/* if (m_Node is IMasterNode) |
|||
{ |
|||
var localNode = (IMasterNode)m_Node; |
|||
if (localNode == null) |
|||
return string.Empty; |
|||
|
|||
List<PropertyCollector.TextureInfo> defaultTextures; |
|||
var resultShader = ((IMasterNode)m_Node).GetFullShader(GenerationMode.Preview, m_Node.guid + "_preview", out defaultTextures); |
|||
|
|||
if (((IMasterNode)m_Node).has3DPreview()) |
|||
{ |
|||
m_GeneratedShaderMode = PreviewMode.Preview3D; |
|||
} |
|||
|
|||
return resultShader; |
|||
}*/ |
|||
return ShaderGenerator.GeneratePreviewShader(m_Node, out m_GeneratedShaderMode); |
|||
} |
|||
|
|||
bool UpdatePreviewShader() |
|||
{ |
|||
if (m_Node == null || m_Node.hasError) |
|||
return false; |
|||
|
|||
var resultShader = GetPreviewShaderString(); |
|||
Debug.Log("RecreateShaderAndMaterial : " + m_Node.GetVariableNameForNode() + Environment.NewLine + resultShader); |
|||
string shaderOuputString = resultShader.Replace("UnityEngine.MaterialGraph", "Generated"); |
|||
System.IO.File.WriteAllText(Application.dataPath + "/../GeneratedShader.shader", shaderOuputString); |
|||
|
|||
if (string.IsNullOrEmpty(resultShader)) |
|||
return false; |
|||
|
|||
// workaround for some internal shader compiler weirdness
|
|||
// if we are in error we sometimes to not properly clean
|
|||
// out the error flags and will stay in error, even
|
|||
// if we are now valid
|
|||
if (m_PreviewShader && MaterialGraphAsset.ShaderHasError(m_PreviewShader)) |
|||
{ |
|||
Object.DestroyImmediate(m_PreviewShader, true); |
|||
m_PreviewShader = null; |
|||
} |
|||
|
|||
if (m_PreviewShader == null) |
|||
{ |
|||
m_PreviewShader = ShaderUtil.CreateShaderAsset(resultShader); |
|||
m_PreviewShader.hideFlags = HideFlags.HideAndDontSave; |
|||
} |
|||
else |
|||
{ |
|||
ShaderUtil.UpdateShaderAsset(m_PreviewShader, resultShader); |
|||
} |
|||
|
|||
return !MaterialGraphAsset.ShaderHasError(m_PreviewShader); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// RenderPreview gets called in OnPreviewGUI. Nodes can override
|
|||
/// RenderPreview and do their own rendering to the render texture
|
|||
/// </summary>
|
|||
Texture RenderPreview(Vector2 targetSize) |
|||
{ |
|||
previewMaterial.shader = m_PreviewShader; |
|||
UpdateMaterialProperties(m_Node, previewMaterial); |
|||
return previewGenerator.DoRenderPreview(previewMaterial, m_GeneratedShaderMode, new Rect(0, 0, targetSize.x, targetSize.y)); |
|||
} |
|||
|
|||
static void SetPreviewMaterialProperty(PreviewProperty previewProperty, Material mat) |
|||
{ |
|||
switch (previewProperty.m_PropType) |
|||
{ |
|||
case PropertyType.Texture: |
|||
mat.SetTexture(previewProperty.m_Name, previewProperty.m_Texture); |
|||
break; |
|||
case PropertyType.Cubemap: |
|||
mat.SetTexture(previewProperty.m_Name, previewProperty.m_Cubemap); |
|||
break; |
|||
case PropertyType.Color: |
|||
mat.SetColor(previewProperty.m_Name, previewProperty.m_Color); |
|||
break; |
|||
case PropertyType.Vector2: |
|||
mat.SetVector(previewProperty.m_Name, previewProperty.m_Vector4); |
|||
break; |
|||
case PropertyType.Vector3: |
|||
mat.SetVector(previewProperty.m_Name, previewProperty.m_Vector4); |
|||
break; |
|||
case PropertyType.Vector4: |
|||
mat.SetVector(previewProperty.m_Name, previewProperty.m_Vector4); |
|||
break; |
|||
case PropertyType.Float: |
|||
mat.SetFloat(previewProperty.m_Name, previewProperty.m_Float); |
|||
break; |
|||
} |
|||
} |
|||
|
|||
public static void UpdateMaterialProperties(AbstractMaterialNode target, Material material) |
|||
{ |
|||
var childNodes = ListPool<INode>.Get(); |
|||
NodeUtils.DepthFirstCollectNodesFromNode(childNodes, target); |
|||
|
|||
var pList = ListPool<PreviewProperty>.Get(); |
|||
for (var index = 0; index < childNodes.Count; index++) |
|||
{ |
|||
var node = childNodes[index] as AbstractMaterialNode; |
|||
if (node == null) |
|||
continue; |
|||
|
|||
node.CollectPreviewMaterialProperties(pList); |
|||
} |
|||
|
|||
var graph = target.owner as AbstractMaterialGraph; |
|||
foreach (var prop in graph.properties) |
|||
{ |
|||
SetPreviewMaterialProperty(prop.GetPreviewMaterialProperty(), material); |
|||
} |
|||
|
|||
foreach (var prop in pList) |
|||
SetPreviewMaterialProperty(prop, material); |
|||
|
|||
ListPool<INode>.Release(childNodes); |
|||
ListPool<PreviewProperty>.Release(pList); |
|||
} |
|||
} |
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue