浏览代码

[mat graph]More remapping

/main
Tim Cooper 8 年前
当前提交
57c58904
共有 3 个文件被更改,包括 267 次插入57 次删除
  1. 20
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Remapper/MasterRemapGraph.cs
  2. 125
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Remapper/MasterRemapInputNode.cs
  3. 179
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Remapper/RemapMasterNode.cs

20
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Remapper/MasterRemapGraph.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Graphing;

if (!amn.allowedInRemapGraph)
Debug.LogWarningFormat("Attempting to add {0} to Remap Graph. This is not allowed.", amn.GetType());
}
base.AddNode(node);
}
if (node is IGenerateProperties)
public List<string> GetSubShadersFor(RemapMasterNode rmn, GenerationMode mode)
{
var subShaders = new List<string>();
try
{
inputNode.m_RemapTarget = rmn;
foreach (var node in GetNodes<IMasterNode>())
subShaders.Add(node.GetSubShader(mode));
}
catch (Exception e)
Debug.LogWarning("Attempting to add second SubGraphInputNode to SubGraph. This is not allowed.");
return;
Debug.LogException(e);
base.AddNode(node);
inputNode.m_RemapTarget = null;
return subShaders;
}
}
}

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


using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Graphing;

[Title("Remapper/Remap Input Node")]
public class MasterRemapInputNode : AbstractSubGraphIONode
, IGeneratesBodyCode
, IGeneratesFunction
, IMayRequireNormal
, IMayRequireTangent
, IMayRequireBitangent
, IMayRequireMeshUV
, IMayRequireScreenPosition
, IMayRequireViewDirection
, IMayRequireWorldPosition
, IMayRequireVertexColor
[NonSerialized]
internal RemapMasterNode m_RemapTarget;
public MasterRemapInputNode()
{
name = "Inputs";

}
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
{
if (generationMode == GenerationMode.ForReals)
return;
foreach (var slot in GetOutputSlots<MaterialSlot>())
{
var outDimension = ConvertConcreteSlotValueTypeToString(slot.concreteValueType);
visitor.AddShaderChunk("float" + outDimension + " " + GetVariableNameForSlot(slot.id) + ";", true);
}
}
public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
{
base.CollectPreviewMaterialProperties(properties);

}
);
}
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
if (m_RemapTarget != null)
m_RemapTarget.GenerateNodeCode(visitor, generationMode);
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{
if (m_RemapTarget != null)
m_RemapTarget.GenerateNodeFunction(visitor, generationMode);
}
public override void GeneratePropertyBlock(PropertyGenerator visitor, GenerationMode generationMode)
{
if (m_RemapTarget != null)
m_RemapTarget.GeneratePropertyBlock(visitor, generationMode);
}
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
{
if (m_RemapTarget == null)
{
foreach (var slot in GetOutputSlots<MaterialSlot>())
{
var outDimension = ConvertConcreteSlotValueTypeToString(slot.concreteValueType);
visitor.AddShaderChunk("float" + outDimension + " " + GetVariableNameForSlot(slot.id) + ";", true);
}
}
else
{
if (m_RemapTarget != null)
m_RemapTarget.GeneratePropertyUsages(visitor, generationMode);
}
}
public bool RequiresNormal()
{
if (m_RemapTarget == null)
return false;
return m_RemapTarget.RequiresNormal();
}
public bool RequiresTangent()
{
if (m_RemapTarget == null)
return false;
return m_RemapTarget.RequiresTangent();
}
public bool RequiresBitangent()
{
if (m_RemapTarget == null)
return false;
return m_RemapTarget.RequiresBitangent();
}
public bool RequiresMeshUV()
{
if (m_RemapTarget == null)
return false;
return m_RemapTarget.RequiresMeshUV();
}
public bool RequiresScreenPosition()
{
if (m_RemapTarget == null)
return false;
return m_RemapTarget.RequiresScreenPosition();
}
public bool RequiresViewDirection()
{
if (m_RemapTarget == null)
return false;
return m_RemapTarget.RequiresViewDirection();
}
public bool RequiresWorldPosition()
{
if (m_RemapTarget == null)
return false;
return m_RemapTarget.RequiresWorldPosition();
}
public bool RequiresVertexColor()
{
if (m_RemapTarget == null)
return false;
return m_RemapTarget.RequiresVertexColor();
}
}
}

179
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Remapper/RemapMasterNode.cs


using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
#if UNITY_EDITOR
using UnityEditor;
#endif

[Serializable]
[Title("Master/Remapper")]
public class RemapMasterNode : AbstractMasterNode
, IGeneratesBodyCode
, IGeneratesFunction
, IOnAssetEnabled
, IMayRequireNormal
, IMayRequireTangent
, IMayRequireBitangent
, IMayRequireMeshUV
, IMayRequireScreenPosition
, IMayRequireViewDirection
, IMayRequireWorldPosition
, IMayRequireVertexColor
{
[SerializeField]
private string m_SerialziedRemapGraph = string.Empty;

public override string GetFullShader(GenerationMode mode, out List<PropertyGenerator.TextureInfo> configuredTextures)
{
throw new NotImplementedException();
var shaderTemplateLocation = ShaderGenerator.GetTemplatePath("shader.template");
if (remapAsset == null || !File.Exists(shaderTemplateLocation))

// Step 1: Generate properties from this node
// remap graphs are not allowed to have subgraphs
// or property nodes, so this is okay :)
var shaderPropertiesVisitor = new ShaderGenerator();
var shaderPropertiesVisitor = new PropertyGenerator();
node.GeneratePropertyUsages(shaderPropertiesVisitor, mode);
node.GeneratePropertyBlock(shaderPropertiesVisitor, mode);
var subShaders = new List<string>();
foreach (var node in activeNodeList.OfType<IMasterNode>())
subShaders.Add(node.GetSubShader(mode));
}
public void GenerateNodeCode(ShaderGenerator shaderBody, GenerationMode generationMode)
{
var nodes = ListPool<INode>.Get();
//Get the rest of the nodes for all the slots
NodeUtils.DepthFirstCollectNodesFromNode(nodes, this, null, NodeUtils.IncludeSelf.Exclude);
for (var i = 0; i < nodes.Count; i++)
{
var node = nodes[i];
if (node is IGeneratesBodyCode)
(node as IGeneratesBodyCode).GenerateNodeCode(shaderBody, generationMode);
}
ListPool<INode>.Release(nodes);
foreach (var slot in GetInputSlots<MaterialSlot>())
{
var edge = owner.GetEdges(slot.slotReference).FirstOrDefault();
if (edge != null)
{
var outputRef = edge.outputSlot;
var fromNode = owner.GetNodeFromGuid<AbstractMaterialNode>(outputRef.nodeGuid);
if (fromNode == null)
continue;
// Step 2: Set this node as the remap target
var subShaders = remapAsset.masterRemapGraph.GetSubShadersFor(this, mode);
shaderBody.AddShaderChunk("float4 reamapper_" + slot.shaderOutputName + " = " + fromNode.GetVariableNameForSlot(outputRef.slotId) + ";", true);
}
else
{
shaderBody.AddShaderChunk("float4 reamapper_" + slot.shaderOutputName + " = 0;", true);
}
}
var templateText = File.ReadAllText(shaderTemplateLocation);
var resultShader = templateText.Replace("${ShaderName}", GetType() + guid.ToString());
resultShader = resultShader.Replace("${ShaderPropertiesHeader}", shaderPropertiesVisitor.GetShaderString(2));
resultShader = resultShader.Replace("${SubShader}", subShaders.Aggregate((i, j) => i + Environment.NewLine + j));
configuredTextures = shaderPropertiesVisitor.GetConfiguredTexutres();
return Regex.Replace(resultShader, @"\r\n|\n\r|\n|\r", Environment.NewLine);
}
public override string GetSubShader(GenerationMode mode)

validNames.Add(slot.id);
}
RemoveSlotsNameNotMatching(validNames);
}
public void GenerateNodeCode(ShaderGenerator shaderBody, GenerationMode generationMode)
{
var nodes = ListPool<INode>.Get();
NodeUtils.DepthFirstCollectNodesFromNode(nodes, this, null, NodeUtils.IncludeSelf.Exclude);
for (var i = 0; i < nodes.Count; i++)
{
var node = nodes[i];
if (node is IGeneratesBodyCode)
(node as IGeneratesBodyCode).GenerateNodeCode(shaderBody, generationMode);
}
ListPool<INode>.Release(nodes);
if (remapAsset == null)
return;
var inputNode = remapAsset.masterRemapGraph.inputNode;
foreach (var mappedSlot in inputNode.GetOutputSlots<MaterialSlot>())
{
var edge = owner.GetEdges(new SlotReference(guid, mappedSlot.id)).FirstOrDefault();
if (edge != null)
{
var outputRef = edge.outputSlot;
var fromNode = owner.GetNodeFromGuid<AbstractMaterialNode>(outputRef.nodeGuid);
if (fromNode == null)
continue;
shaderBody.AddShaderChunk("float4 " + inputNode.GetVariableNameForSlot(mappedSlot.id) + " = " + fromNode.GetVariableNameForSlot(outputRef.slotId) + ";", true);
}
else
{
shaderBody.AddShaderChunk("float4 " + inputNode.GetVariableNameForSlot(mappedSlot.id) + " = 0;", true);
}
}
}
public override void GeneratePropertyBlock(PropertyGenerator visitor, GenerationMode generationMode)
{
var activeNodeList = new List<INode>();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, this, null, NodeUtils.IncludeSelf.Exclude);
foreach (var node in activeNodeList.OfType<IGenerateProperties>())
node.GeneratePropertyBlock(visitor, generationMode);
}
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
{
var activeNodeList = new List<INode>();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, this, null, NodeUtils.IncludeSelf.Exclude);
foreach (var node in activeNodeList.OfType<IGenerateProperties>())
node.GeneratePropertyUsages(visitor, generationMode);
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{
var activeNodeList = new List<INode>();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, this, null, NodeUtils.IncludeSelf.Exclude);
foreach (var node in activeNodeList.OfType<IGeneratesFunction>())
node.GenerateNodeFunction(visitor, generationMode);
}
public bool RequiresNormal()
{
var activeNodeList = new List<INode>();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, this, null, NodeUtils.IncludeSelf.Exclude);
return activeNodeList.OfType<IMayRequireNormal>().Any(x => x.RequiresNormal());
}
public bool RequiresTangent()
{
var activeNodeList = new List<INode>();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, this, null, NodeUtils.IncludeSelf.Exclude);
return activeNodeList.OfType<IMayRequireTangent>().Any(x => x.RequiresTangent());
}
public bool RequiresBitangent()
{
var activeNodeList = new List<INode>();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, this, null, NodeUtils.IncludeSelf.Exclude);
return activeNodeList.OfType<IMayRequireBitangent>().Any(x => x.RequiresBitangent());
}
public bool RequiresMeshUV()
{
var activeNodeList = new List<INode>();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, this, null, NodeUtils.IncludeSelf.Exclude);
return activeNodeList.OfType<IMayRequireMeshUV>().Any(x => x.RequiresMeshUV());
}
public bool RequiresScreenPosition()
{
var activeNodeList = new List<INode>();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, this, null, NodeUtils.IncludeSelf.Exclude);
return activeNodeList.OfType<IMayRequireScreenPosition>().Any(x => x.RequiresScreenPosition());
}
public bool RequiresViewDirection()
{
var activeNodeList = new List<INode>();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, this, null, NodeUtils.IncludeSelf.Exclude);
return activeNodeList.OfType<IMayRequireViewDirection>().Any(x => x.RequiresViewDirection());
}
public bool RequiresWorldPosition()
{
var activeNodeList = new List<INode>();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, this, null, NodeUtils.IncludeSelf.Exclude);
return activeNodeList.OfType<IMayRequireWorldPosition>().Any(x => x.RequiresWorldPosition());
}
public bool RequiresVertexColor()
{
var activeNodeList = new List<INode>();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, this, null, NodeUtils.IncludeSelf.Exclude);
return activeNodeList.OfType<IMayRequireVertexColor>().Any(x => x.RequiresVertexColor());
}
}
}
正在加载...
取消
保存