浏览代码

Working remap node (wood wood) probably still needs a bunch of love. but something is working :)

/main
Tim Cooper 7 年前
当前提交
97cfbe75
共有 27 个文件被更改,包括 319 次插入186 次删除
  1. 10
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/AssetCallbacks/CreateRemapGraph.cs
  2. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/AbstractMaterialGraphEditWindow.cs
  3. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/MaterialGraphPresenter.cs
  4. 61
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/Nodes/RemapMasterNodePresenter.cs
  5. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewSystem.cs
  6. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers/MasterRemapGraphImporter.cs
  7. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightSubshaderPBR.template
  8. 17
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/AbstractMaterialGraph.cs
  9. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/AbstractShaderProperty.cs
  10. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/IShaderProperty.cs
  11. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/MaterialGraph.cs
  12. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/Vector2ShaderProperty.cs
  13. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/Vector3ShaderProperty.cs
  14. 74
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/LightweightPipeline/AbstractLightweightMasterNode.cs
  15. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/LightweightPipeline/AbstractLightweightPBRMasterNode.cs
  16. 17
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/LightweightPipeline/LightweightMetallicMasterNode.cs
  17. 15
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/LightweightPipeline/LightweightSpecularMasterNode.cs
  18. 23
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/MasterNode.cs
  19. 60
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/AbstractSubGraphNode.cs
  20. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/MaterialSubGraphAsset.cs
  21. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/SubGraphNode.cs
  22. 15
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Remapper/MasterRemapGraphAsset.cs
  23. 98
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/MasterRemapNode.cs
  24. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/MasterRemapNode.cs.meta
  25. 32
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Remapper/MasterRemapInputNode.cs
  26. 19
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Remapper/MaterialRemapAsset.cs
  27. 0
      /MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Remapper/MasterRemapGraphAsset.cs.meta

10
MaterialGraphProject/Assets/UnityShaderEditor/Editor/AssetCallbacks/CreateRemapGraph.cs


{
public class CreateRemapGraph : EndNameEditAction
{
[MenuItem("Assets/Create/Remap Graph", false, 209)]
[MenuItem("Assets/Create/Shader Remap Graph", false, 209)]
"New Remap-Graph.remapGraph", null, null);
"New Remap-Graph.ShaderRemapGraph", null, null);
/* var graph = CreateInstance<MaterialRemapAsset>();
graph.name = Path.GetFileName(pathName);
AssetDatabase.CreateAsset(graph, pathName);*/
var graph = new MasterRemapGraph();
File.WriteAllText(pathName, EditorJsonUtility.ToJson(graph));
AssetDatabase.Refresh();
}
}
}

1
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/AbstractMaterialGraphEditWindow.cs


AssetDatabase.ImportAsset(path);
}
private void UpdateShaderGraphOnDisk(string path)
{
var graph = inMemoryAsset as UnityEngine.MaterialGraph.MaterialGraph;

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/MaterialGraphPresenter.cs


typeMapper[typeof(AACheckerboardNode)] = typeof(AnyNodePresenter); // anything derived from AnyNode should use the AnyNodePresenter
typeMapper[typeof(AACheckerboard3dNode)] = typeof(AnyNodePresenter); // anything derived from AnyNode should use the AnyNodePresenter*/
typeMapper[typeof(SubGraphNode)] = typeof(SubgraphNodePresenter);
typeMapper[typeof(MasterRemapNode)] = typeof(MasterRemapNodePresenter);
// typeMapper[typeof(RemapMasterNode)] = typeof(RemapMasterNodePresenter);
// typeMapper[typeof(MasterRemapInputNode)] = typeof(RemapInputNodePresenter);
typeMapper[typeof(AbstractSubGraphIONode)] = typeof(SubgraphIONodePresenter);
// typeMapper[typeof(AbstractSurfaceMasterNode)] = typeof(SurfaceMasterNodePresenter);

61
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/Nodes/RemapMasterNodePresenter.cs


using System;
using System.Collections.Generic;
using UnityEditor.Experimental.UIElements.GraphView;
/* [Serializable]
class RemapMasterControlPresenter : GraphControlPresenter
{
public override void OnGUIHandler()
{
base.OnGUIHandler();
[Serializable]
class RemapContolPresenter : GraphControlPresenter
{
public override void OnGUIHandler()
{
base.OnGUIHandler();
var remapNode = node as RemapMasterNode;
if (remapNode == null)
return;
var remapNode = node as MasterRemapNode;
if (remapNode == null)
return;
remapNode.remapAsset = (MaterialRemapAsset)EditorGUILayout.MiniThumbnailObjectField(
new GUIContent("Remap Asset"),
remapNode.remapAsset,
typeof(MaterialRemapAsset), null);
}
remapNode.remapGraphAsset = (MasterRemapGraphAsset)EditorGUILayout.MiniThumbnailObjectField(
new GUIContent("Remap"),
remapNode.remapGraphAsset,
typeof(MasterRemapGraphAsset), null);
}
public override float GetHeight()
{
return EditorGUIUtility.singleLineHeight + 2 * EditorGUIUtility.standardVerticalSpacing;
}
}
public override float GetHeight()
{
return EditorGUIUtility.singleLineHeight + 2 * EditorGUIUtility.standardVerticalSpacing;
}
}
[Serializable]
public class RemapMasterNodePresenter : MasterNodePresenter
{
protected override IEnumerable<GraphControlPresenter> GetControlData()
{
var instance = CreateInstance<RemapMasterControlPresenter>();
instance.Initialize(node);
var controls = new List<GraphControlPresenter>(base.GetControlData());
controls.Add(instance);
return controls;
}
}*/
[Serializable]
public class MasterRemapNodePresenter : MaterialNodePresenter
{
protected override IEnumerable<GraphControlPresenter> GetControlData()
{
var instance = CreateInstance<RemapContolPresenter>();
instance.Initialize(node);
return new List<GraphControlPresenter> { instance };
}
}
}

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


}
var node = m_Graph.GetNodeFromGuid(nodeGuid);
m_PreviewMaterial.shader = previewData.shader;
m_PreviewGenerator.DoRenderPreview(previewData.renderTexture, m_PreviewMaterial, previewData.previewMode, node is MasterNode, time, m_PreviewPropertyBlock);
m_PreviewGenerator.DoRenderPreview(previewData.renderTexture, m_PreviewMaterial, previewData.previewMode, node is IMasterNode, time, m_PreviewPropertyBlock);
previewData.texture = previewData.renderTexture;
}

if (!m_Previews.TryGetValue(nodeGuid, out previewData))
return;
if (node is MasterNode && node.owner is UnityEngine.MaterialGraph.MaterialGraph)
if (node is IMasterNode)
var masterNode = (MasterNode)node;
var materialGraph = (UnityEngine.MaterialGraph.MaterialGraph) node.owner;
var materialGraph = (AbstractMaterialGraph) node.owner;
previewData.shaderString = materialGraph.GetShader(node.guid + "_preview", GenerationMode.Preview, out defaultTextures);
previewData.previewMode = masterNode.has3DPreview() ? PreviewMode.Preview3D : PreviewMode.Preview2D;
PreviewMode pmode;
previewData.shaderString = materialGraph.GetShader(node, GenerationMode.Preview, node.guid + "_preview", out defaultTextures, out pmode);
previewData.previewMode = node.previewMode;
}
else if (!node.hasPreview || NodeUtils.FindEffectiveShaderStage(node, true) == ShaderStage.Vertex)
{

4
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers/MasterRemapGraphImporter.cs


using UnityEngine;
using UnityEngine.MaterialGraph;
[ScriptedImporter(1, "ShaderReampGraph")]
[ScriptedImporter(1, "ShaderRemapGraph")]
public class MasterRemapGraphImporter : ScriptedImporter
{
public override void OnImportAsset(AssetImportContext ctx)

ctx.AddObjectToAsset("MainAsset", graphAsset);
ctx.SetMainObject(graphAsset);
}
}
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightSubshaderPBR.template


float3 Albedo = float3(0.5, 0.5, 0.5);
float3 Specular = float3(0, 0, 0);
float Metallic = 0;
float3 Normal = float3(.5, .5, 1);
float3 Normal = float3(0, 0, 1);
float3 Emission = 0;
float Smoothness = 0;
float Occlusion = 1;

17
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/AbstractMaterialGraph.cs


return GetShader(node, GenerationMode.Preview, string.Format("hidden/preview/{0}", node.GetVariableNameForNode()), out configuredTextures, out previewMode);
}
protected string GetShader(AbstractMaterialNode node, GenerationMode mode, string name, out List<PropertyCollector.TextureInfo> configuredTextures, out PreviewMode previewMode)
public string GetShader(AbstractMaterialNode node, GenerationMode mode, string name, out List<PropertyCollector.TextureInfo> configuredTextures, out PreviewMode previewMode)
if (node == null)
throw new ArgumentNullException(nameof(node));
var vertexShader = new ShaderGenerator();
var sufraceDescriptionFunction = new ShaderGenerator();
var surfaceDescriptionStruct = new ShaderGenerator();

surfaceDescriptionStruct.AddShaderChunk("struct SurfaceDescription{", false);
surfaceDescriptionStruct.Indent();
if (node is MasterNode)
if (node is IMasterNode)
{
foreach (var slot in node.GetInputSlots<MaterialSlot>())
surfaceDescriptionStruct.AddShaderChunk(AbstractMaterialNode.ConvertConcreteSlotValueTypeToString(AbstractMaterialNode.OutputPrecision.@float, slot.concreteValueType) + " " + slot.shaderOutputName + ";", false);

}
sufraceDescriptionFunction.AddShaderChunk("SurfaceDescription surface = (SurfaceDescription)0;", false);
if (node is MasterNode)
if (node is IMasterNode)
{
foreach (var input in node.GetInputSlots<MaterialSlot>())
{

finalShader.AddShaderChunk(sufraceDescriptionFunction.GetShaderString(2), false);
finalShader.AddShaderChunk("ENDCG", false);
if (node is MasterNode)
var masterNode = node as IMasterNode;
if (masterNode != null)
var master = (MasterNode) node;
finalShader.AddShaderChunk(master.GetSubShader(requirements), false);
var subShaders = masterNode.GetSubshader(requirements, null);
foreach (var ss in subShaders)
finalShader.AddShaderChunk(ss, false);
}
else
{

8
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/AbstractShaderProperty.cs


}
set { m_Name = value; }
}
public string referenceName
{
get

public abstract Vector4 defaultValue { get; }
public abstract string GetPropertyBlockString();
public abstract string GetPropertyDeclarationString();
public virtual string GetInlinePropertyDeclarationString()
{
return GetPropertyDeclarationString();
}
public abstract PreviewProperty GetPreviewMaterialProperty();
public virtual void OnBeforeSerialize()

1
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/IShaderProperty.cs


string GetPropertyBlockString();
string GetPropertyDeclarationString();
string GetInlinePropertyDeclarationString();
PreviewProperty GetPreviewMaterialProperty();
}
}

6
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/MaterialGraph.cs


[Serializable]
public class MaterialGraph : AbstractMaterialGraph
{
public MasterNode masterNode
public IMasterNode masterNode
get { return GetNodes<MasterNode>().FirstOrDefault(); }
get { return GetNodes<INode>().OfType<IMasterNode>().FirstOrDefault(); }
return GetShader(masterNode, mode, name, out configuredTextures, out pmode);
return GetShader(masterNode as AbstractMaterialNode, mode, name, out configuredTextures, out pmode);
}
}

5
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/Vector2ShaderProperty.cs


get { return new Vector4(value.x, value.y, 0, 0); }
}
public override string GetInlinePropertyDeclarationString()
{
return "float2 " + referenceName + ";";
}
public override PreviewProperty GetPreviewMaterialProperty()
{
return new PreviewProperty()

5
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/Vector3ShaderProperty.cs


get { return new Vector4(value.x, value.y, value.z, 0); }
}
public override string GetInlinePropertyDeclarationString()
{
return "float3 " + referenceName + ";";
}
public override PreviewProperty GetPreviewMaterialProperty()
{
return new PreviewProperty()

74
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/LightweightPipeline/AbstractLightweightMasterNode.cs


protected abstract IEnumerable<int> masterVertexInputs { get; }
protected abstract string GetTemplateName();
protected virtual void GetLightweightDefinesAndRemap(ShaderGenerator defines, ShaderGenerator surfaceOutputRemap)
protected virtual void GetLightweightDefinesAndRemap(ShaderGenerator defines, ShaderGenerator surfaceOutputRemap, MasterRemapGraph remapper)
// Step 1: configure slot defaults
var edge = owner.GetEdges(slot.slotReference).FirstOrDefault();
if (edge == null)
continue;
surfaceOutputRemap.AddShaderChunk( slot.shaderOutputName
+ " = "
+ slot.GetDefaultValue(GenerationMode.ForReals)
+ ";", true);
}
surfaceOutputRemap.AddShaderChunk(slot.shaderOutputName
+ " = surf."
+ slot.shaderOutputName + ";", true);
// Step 2: no remapper, working with raw master node..
if (remapper == null)
{
foreach (var slot in GetInputSlots<MaterialSlot>())
{
var edge = owner.GetEdges(slot.slotReference).FirstOrDefault();
if (edge == null)
continue;
surfaceOutputRemap.AddShaderChunk(slot.shaderOutputName
+ " = surf."
+ slot.shaderOutputName + ";", true);
}
// Step 3: remapper present... complex workflow time
else
{
surfaceOutputRemap.AddShaderChunk("{", false);
surfaceOutputRemap.Indent();
foreach (var prop in remapper.properties)
{
surfaceOutputRemap.AddShaderChunk(prop.GetInlinePropertyDeclarationString(), true);
surfaceOutputRemap.AddShaderChunk(string.Format("{0} = surf.{0};", prop.referenceName), true);
}
List<INode> nodes = new List<INode>();
NodeUtils.DepthFirstCollectNodesFromNode(nodes, this, NodeUtils.IncludeSelf.Exclude);
foreach (var activeNode in nodes.OfType<AbstractMaterialNode>())
{
if (activeNode is IGeneratesBodyCode)
(activeNode as IGeneratesBodyCode).GenerateNodeCode(surfaceOutputRemap, GenerationMode.ForReals);
}
foreach (var input in GetInputSlots<MaterialSlot>())
{
foreach (var edge in owner.GetEdges(input.slotReference))
{
var outputRef = edge.outputSlot;
var fromNode = owner.GetNodeFromGuid<AbstractMaterialNode>(outputRef.nodeGuid);
if (fromNode == null)
continue;
surfaceOutputRemap.AddShaderChunk(string.Format("{0} = {1};", input.shaderOutputName, fromNode.GetVariableNameForSlot(outputRef.slotId)), true);
}
}
surfaceOutputRemap.Deindent();
surfaceOutputRemap.AddShaderChunk("}", false);
}
public override string GetSubShader(ShaderGraphRequirements externalGraphRequiements)
public override IEnumerable<string> GetSubshader(ShaderGraphRequirements graphRequirements, MasterRemapGraph remapper)
{
var tagsVisitor = new ShaderGenerator();
var blendingVisitor = new ShaderGenerator();

vertexShader,
localPixelShader,
surfaceInputs,
externalGraphRequiements,
graphRequirements,
GetLightweightDefinesAndRemap(defines, surfaceOutputRemap);
GetLightweightDefinesAndRemap(defines, surfaceOutputRemap, remapper);
return string.Empty;
return new string[] {};
var subShaderTemplate = File.ReadAllText(templateLocation);
var resultShader = subShaderTemplate.Replace("${Defines}", defines.GetShaderString(3));

resultShader = resultShader.Replace("${ZTest}", zTestVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${ZWrite}", zWriteVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${LOD}", "" + m_MaterialOptions.lod);
return resultShader;
return new[] {resultShader};
}
protected abstract int GetInterpolatorStartIndex();

5
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/LightweightPipeline/AbstractLightweightPBRMasterNode.cs


public const int SmoothnessSlotId = 4;
public const int OcclusionSlotId = 5;
public const int AlphaSlotId = 6;
public const int VertexOffsetId = 7;
protected override void GetLightweightDefinesAndRemap(ShaderGenerator defines, ShaderGenerator surfaceOutputRemap)
protected override void GetLightweightDefinesAndRemap(ShaderGenerator defines, ShaderGenerator surfaceOutputRemap, MasterRemapGraph remapper)
base.GetLightweightDefinesAndRemap(defines, surfaceOutputRemap);
base.GetLightweightDefinesAndRemap(defines, surfaceOutputRemap, remapper);
defines.AddShaderChunk("#define _GLOSSYREFLECTIONS_ON", true);
defines.AddShaderChunk("#define _SPECULARHIGHLIGHTS_ON", true);

17
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/LightweightPipeline/LightweightMetallicMasterNode.cs


public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new MaterialSlot(VertexOffsetId, VertexOffsetName, VertexOffsetName, SlotType.Input, SlotValueType.Vector3, Vector4.zero, ShaderStage.Vertex));
AddSlot(new MaterialSlot(AlbedoSlotId, AlbedoSlotName, AlbedoSlotName, SlotType.Input, SlotValueType.Vector3, Vector4.zero, ShaderStage.Fragment));
AddSlot(new MaterialSlot(NormalSlotId, NormalSlotName, NormalSlotName, SlotType.Input, SlotValueType.Vector3, Vector4.zero, ShaderStage.Fragment));
AddSlot(new MaterialSlot(AlbedoSlotId, AlbedoSlotName, AlbedoSlotName, SlotType.Input, SlotValueType.Vector3, new Vector4(0.5f, 0.5f, 0.5f), ShaderStage.Fragment));
AddSlot(new MaterialSlot(NormalSlotId, NormalSlotName, NormalSlotName, SlotType.Input, SlotValueType.Vector3, new Vector4(0,0,1), ShaderStage.Fragment));
AddSlot(new MaterialSlot(AlphaSlotId, AlphaSlotName, AlphaSlotName, SlotType.Input, SlotValueType.Vector1, Vector4.zero, ShaderStage.Fragment));
AddSlot(new MaterialSlot(AlphaSlotId, AlphaSlotName, AlphaSlotName, SlotType.Input, SlotValueType.Vector1, Vector4.one, ShaderStage.Fragment));
// clear out slot names that do not match the slots
// we support

MetallicSlotId,
SmoothnessSlotId,
OcclusionSlotId,
AlphaSlotId,
VertexOffsetId
AlphaSlotId
});
}

}
protected override void GetLightweightDefinesAndRemap(ShaderGenerator defines, ShaderGenerator surfaceOutputRemap)
protected override void GetLightweightDefinesAndRemap(ShaderGenerator defines, ShaderGenerator surfaceOutputRemap, MasterRemapGraph remapper)
base.GetLightweightDefinesAndRemap(defines, surfaceOutputRemap);
base.GetLightweightDefinesAndRemap(defines, surfaceOutputRemap, remapper);
defines.AddShaderChunk("#define _METALLIC_SETUP 1", true);
}

{
get
{
return new[]
return new int[]
VertexOffsetId
};
}
}

15
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/LightweightPipeline/LightweightSpecularMasterNode.cs


public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new MaterialSlot(VertexOffsetId, VertexOffsetName, VertexOffsetName, SlotType.Input, SlotValueType.Vector3, Vector4.zero, ShaderStage.Vertex));
AddSlot(new MaterialSlot(AlbedoSlotId, AlbedoSlotName, AlbedoSlotName, SlotType.Input, SlotValueType.Vector3, Vector4.zero, ShaderStage.Fragment));
AddSlot(new MaterialSlot(NormalSlotId, NormalSlotName, NormalSlotName, SlotType.Input, SlotValueType.Vector3, Vector4.zero, ShaderStage.Fragment));
AddSlot(new MaterialSlot(AlbedoSlotId, AlbedoSlotName, AlbedoSlotName, SlotType.Input, SlotValueType.Vector3, new Vector4(0.5f, 0.5f, 0.5f, 0.5f), ShaderStage.Fragment));
AddSlot(new MaterialSlot(NormalSlotId, NormalSlotName, NormalSlotName, SlotType.Input, SlotValueType.Vector3, new Vector4(0,0,1), ShaderStage.Fragment));
AddSlot(new MaterialSlot(SpecularSlotId, SpecularSlotName, SpecularSlotName, SlotType.Input, SlotValueType.Vector1, Vector4.zero, ShaderStage.Fragment));
AddSlot(new MaterialSlot(SpecularSlotId, SpecularSlotName, SpecularSlotName, SlotType.Input, SlotValueType.Vector3, Vector4.zero, ShaderStage.Fragment));
AddSlot(new MaterialSlot(AlphaSlotId, AlphaSlotName, AlphaSlotName, SlotType.Input, SlotValueType.Vector1, Vector4.zero, ShaderStage.Fragment));
AddSlot(new MaterialSlot(AlphaSlotId, AlphaSlotName, AlphaSlotName, SlotType.Input, SlotValueType.Vector1, Vector4.one, ShaderStage.Fragment));
// clear out slot names that do not match the slots
// we support

SpecularSlotId,
SmoothnessSlotId,
OcclusionSlotId,
AlphaSlotId,
VertexOffsetId
AlphaSlotId
});
}
protected override string GetTemplateName()

{
get
{
return new[]
return new int[]
VertexOffsetId
};
}
}

23
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/MasterNode.cs


namespace UnityEngine.MaterialGraph
{
public interface IMasterNode
{
SurfaceMaterialOptions options { get; }
IEnumerable<string> GetSubshader(ShaderGraphRequirements graphRequirements, MasterRemapGraph remapper);
}
[Title("Master/Master")]
public abstract class MasterNode : AbstractMaterialNode
public abstract class MasterNode : AbstractMaterialNode, IMasterNode
{
[SerializeField]
protected SurfaceMaterialOptions m_MaterialOptions = new SurfaceMaterialOptions();

get { return false; }
}
public virtual bool has3DPreview()
public override PreviewMode previewMode
return true;
get { return PreviewMode.Preview3D; }
public abstract string GetSubShader(ShaderGraphRequirements shaderGraphRequirements);
public SurfaceMaterialOptions options
{
get { return m_MaterialOptions; }
}
public abstract IEnumerable<string> GetSubshader(ShaderGraphRequirements graphRequirements, MasterRemapGraph remapper);
}
}

60
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/AbstractSubGraphNode.cs


, IMayRequireTime
{
protected virtual AbstractSubGraph subGraph { get; }
protected virtual AbstractSubGraph referencedGraph { get; }
get { return subGraph != null; }
get { return referencedGraph != null; }
}
public override PreviewMode previewMode

if (subGraph == null)
if (referencedGraph == null)
return PreviewMode.Preview2D;
return PreviewMode.Preview3D;

public virtual void OnEnable()
{
var validNames = new List<int>();
if (subGraph == null)
if (referencedGraph == null)
var props = subGraph.properties;
var props = referencedGraph.properties;
foreach (var prop in props)
{
var propType = prop.propertyType;

{
base.CollectShaderProperties(visitor, generationMode);
if (subGraph == null)
if (referencedGraph == null)
subGraph.CollectShaderProperties(visitor, GenerationMode.ForReals);
referencedGraph.CollectShaderProperties(visitor, GenerationMode.ForReals);
}
public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)

if (subGraph == null)
if (referencedGraph == null)
properties.AddRange(subGraph.GetPreviewProperties());
properties.AddRange(referencedGraph.GetPreviewProperties());
if (subGraph == null)
if (referencedGraph == null)
subGraph.GenerateNodeFunction(visitor, GenerationMode.ForReals);
referencedGraph.GenerateNodeFunction(visitor, GenerationMode.ForReals);
if (subGraph == null)
if (referencedGraph == null)
return subGraph.activeNodes.OfType<IMayRequireNormal>().Aggregate(NeededCoordinateSpace.None, (mask, node) =>
return referencedGraph.activeNodes.OfType<IMayRequireNormal>().Aggregate(NeededCoordinateSpace.None, (mask, node) =>
{
mask |= node.RequiresNormal();
return mask;

public bool RequiresMeshUV(UVChannel channel)
{
if (subGraph == null)
if (referencedGraph == null)
return subGraph.activeNodes.OfType<IMayRequireMeshUV>().Any(x => x.RequiresMeshUV(channel));
return referencedGraph.activeNodes.OfType<IMayRequireMeshUV>().Any(x => x.RequiresMeshUV(channel));
if (subGraph == null)
if (referencedGraph == null)
return subGraph.activeNodes.OfType<IMayRequireScreenPosition>().Any(x => x.RequiresScreenPosition());
return referencedGraph.activeNodes.OfType<IMayRequireScreenPosition>().Any(x => x.RequiresScreenPosition());
if (subGraph == null)
if (referencedGraph == null)
return subGraph.activeNodes.OfType<IMayRequireViewDirection>().Aggregate(NeededCoordinateSpace.None, (mask, node) =>
return referencedGraph.activeNodes.OfType<IMayRequireViewDirection>().Aggregate(NeededCoordinateSpace.None, (mask, node) =>
{
mask |= node.RequiresViewDirection();
return mask;

public NeededCoordinateSpace RequiresPosition()
{
if (subGraph == null)
if (referencedGraph == null)
return subGraph.activeNodes.OfType<IMayRequirePosition>().Aggregate(NeededCoordinateSpace.None, (mask, node) =>
return referencedGraph.activeNodes.OfType<IMayRequirePosition>().Aggregate(NeededCoordinateSpace.None, (mask, node) =>
{
mask |= node.RequiresPosition();
return mask;

public NeededCoordinateSpace RequiresTangent()
{
if (subGraph == null)
if (referencedGraph == null)
return subGraph.activeNodes.OfType<IMayRequireTangent>().Aggregate(NeededCoordinateSpace.None, (mask, node) =>
return referencedGraph.activeNodes.OfType<IMayRequireTangent>().Aggregate(NeededCoordinateSpace.None, (mask, node) =>
{
mask |= node.RequiresTangent();
return mask;

public bool RequiresTime()
{
if (subGraph == null)
if (referencedGraph == null)
return subGraph.activeNodes.OfType<IMayRequireTime>().Any(x => x.RequiresTime());
return referencedGraph.activeNodes.OfType<IMayRequireTime>().Any(x => x.RequiresTime());
if (subGraph == null)
if (referencedGraph == null)
return subGraph.activeNodes.OfType<IMayRequireBitangent>().Aggregate(NeededCoordinateSpace.None, (mask, node) =>
return referencedGraph.activeNodes.OfType<IMayRequireBitangent>().Aggregate(NeededCoordinateSpace.None, (mask, node) =>
{
mask |= node.RequiresBitangent();
return mask;

public bool RequiresVertexColor()
{
if (subGraph == null)
if (referencedGraph == null)
return subGraph.activeNodes.OfType<IMayRequireVertexColor>().Any(x => x.RequiresVertexColor());
return referencedGraph.activeNodes.OfType<IMayRequireVertexColor>().Any(x => x.RequiresVertexColor());
}
}

5
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/MaterialSubGraphAsset.cs


{
public class MaterialSubGraphAsset : ScriptableObject
{
[SerializeField]
private SubGraph m_MaterialSubGraph = new SubGraph();
[SerializeField] private SubGraph m_MaterialSubGraph = new SubGraph();
public SubGraph subGraph
{

}
}

5
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/SubGraphNode.cs


public MaterialSubGraphAsset subGraph;
}
protected override AbstractSubGraph subGraph
protected override AbstractSubGraph referencedGraph
{
get
{

public void GenerateNodeCode(ShaderGenerator shaderBodyVisitor, GenerationMode generationMode)
{
if (subGraph == null)
if (referencedGraph == null)
return;
var outputString = new ShaderGenerator();

// find out which output slots are actually used
//TODO: Be smarter about this and only output ones that are actually USED, not just connected
//var validOutputSlots = NodeUtils.GetSlotsThatOutputToNodeRecurse(this, (graph as BaseMaterialGraph).masterNode);
foreach (var slot in GetOutputSlots<MaterialSlot>())
{

15
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Remapper/MasterRemapGraphAsset.cs


namespace UnityEngine.MaterialGraph
{
public class MasterRemapGraphAsset : ScriptableObject
{
[SerializeField]
private MasterRemapGraph m_RemapGraph = new MasterRemapGraph();
public MasterRemapGraph remapGraph
{
get { return m_RemapGraph; }
set { m_RemapGraph = value; }
}
}
}

98
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/MasterRemapNode.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine.Graphing;
namespace UnityEngine.MaterialGraph
{
[Title("Master/Remap-Node")]
public class MasterRemapNode : AbstractSubGraphNode, IMasterNode
{
[SerializeField]
protected SurfaceMaterialOptions m_MaterialOptions = new SurfaceMaterialOptions();
public SurfaceMaterialOptions options
{
get { return m_MaterialOptions; }
}
public virtual PreviewMode previewMode
{
get { return PreviewMode.Preview3D; }
}
[SerializeField]
private string m_SerializedRemapGraph = string.Empty;
[Serializable]
private class RemapGraphHelper
{
public MasterRemapGraphAsset remapGraph;
}
protected override AbstractSubGraph referencedGraph
{
get
{
if (remapGraphAsset == null)
return null;
return remapGraphAsset.remapGraph;
}
}
#if UNITY_EDITOR
public MasterRemapGraphAsset remapGraphAsset
{
get
{
if (string.IsNullOrEmpty(m_SerializedRemapGraph))
return null;
var helper = new RemapGraphHelper();
EditorJsonUtility.FromJsonOverwrite(m_SerializedRemapGraph, helper);
return helper.remapGraph;
}
set
{
if (remapGraphAsset == value)
return;
var helper = new RemapGraphHelper();
helper.remapGraph = value;
m_SerializedRemapGraph = EditorJsonUtility.ToJson(helper, true);
OnEnable();
if (onModified != null)
onModified(this, ModificationScope.Topological);
}
}
#else
public MaterialSubGraphAsset subGraphAsset {get; set; }
#endif
public MasterRemapNode()
{
name = "RemapMaster";
}
public IEnumerable<string> GetSubshader(ShaderGraphRequirements graphRequirements, MasterRemapGraph remapper)
{
if (referencedGraph == null)
return new string[]{};
var masterNodes = referencedGraph.activeNodes.OfType<IMasterNode>().ToList();
if (masterNodes.Count == 0)
return new string[]{};
var results = new List<string>();
foreach (var master in masterNodes)
results.AddRange(master.GetSubshader(graphRequirements, referencedGraph as MasterRemapGraph));
return results;
}
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/MasterRemapNode.cs.meta


fileFormatVersion: 2
guid: 861139fc6f60415cae2eb9a9625aaeb8
timeCreated: 1506873607

32
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Remapper/MasterRemapInputNode.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Graphing;
namespace UnityEngine.MaterialGraph
{
[Title("Remapper/Remap Input Node")]
public class MasterRemapInputNode : AbstractSubGraphIONode
{
public MasterRemapInputNode()
{
name = "Inputs";
}
public override int AddSlot()
{
var index = GetInputSlots<ISlot>().Count() + 1;
AddSlot(new MaterialSlot(index, "Output " + index, "Output" + index, SlotType.Output, SlotValueType.Vector4, Vector4.zero));
return index;
}
public override void RemoveSlot()
{
var index = GetInputSlots<ISlot>().Count();
if (index == 0)
return;
RemoveSlot(index);
}
}
}

19
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Remapper/MaterialRemapAsset.cs


using System.Collections.Generic;
using System.Linq;
using UnityEngine.Graphing;
namespace UnityEngine.MaterialGraph
{
public class MasterRemapGraphAsset : ScriptableObject
{
[SerializeField]
private MasterRemapGraph m_RemapGraph = new MasterRemapGraph();
public MasterRemapGraph remapGraph
{
get { return m_RemapGraph; }
set { m_RemapGraph = value; }
}
}
}

/MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Remapper/MaterialRemapAsset.cs.meta → /MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Remapper/MasterRemapGraphAsset.cs.meta

正在加载...
取消
保存