浏览代码

Preliminary work on Dynamic Matrix

- Add other matrix slot value types
/main
Matt Dean 7 年前
当前提交
8697ac36
共有 6 个文件被更改,包括 138 次插入91 次删除
  1. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MaterialSlot.cs
  2. 30
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/CodeFunctionNode.cs
  3. 107
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Matrix/MatrixMultiplyNode.cs
  4. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/SlotValue.cs
  5. 78
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/DynamicMatrixMaterialSlot.cs
  6. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/DynamicMatrixMaterialSlot.cs.meta

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MaterialSlot.cs


{
case SlotValueType.SamplerState:
return new SamplerStateMaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden);
case SlotValueType.DynamicMatrix:
return new DynamicMatrixMaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden);
case SlotValueType.Matrix4:
return new Matrix4MaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden);
case SlotValueType.Matrix3:

30
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/CodeFunctionNode.cs


protected struct DynamicDimensionVector
{}
protected struct ColorRGBA
{}
protected struct ColorRGB
{}
protected struct Matrix3x3
{}
protected struct Matrix2x2
{}
protected struct DynamicDimensionMatrix
{ }
protected enum Binding
{
None,

protected abstract MethodInfo GetFunctionToConvert();
public class ColorRGBA { };
public class ColorRGB { };
private static SlotValueType ConvertTypeToSlotValueType(ParameterInfo p)
{
Type t = p.ParameterType;

if (t == typeof(Matrix4x4))
{
return SlotValueType.Matrix4;
}
if (t == typeof(Matrix3x3))
{
return SlotValueType.Matrix3;
}
if (t == typeof(Matrix2x2))
{
return SlotValueType.Matrix2;
}
if (t == typeof(DynamicDimensionMatrix))
{
return SlotValueType.DynamicMatrix;
}
throw new ArgumentException("Unsupported type " + t);
}

107
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Matrix/MatrixMultiplyNode.cs


using UnityEditor.Graphing;
using System.Reflection;
using UnityEngine;
[Title("Math", "Matrix", "Multiply Matrix")]
public class MatrixMultiplyNode : AbstractMaterialNode, IGeneratesBodyCode, IGeneratesFunction
[Title("Math", "Matrix", "Matrix Multiply")]
public class MatrixMultiplyNode : CodeFunctionNode
protected const string kInputSlot1ShaderName = "Input1";
protected const string kInputSlot2ShaderName = "Input2";
protected const string kOutputSlotShaderName = "Output";
public const int InputSlot1Id = 0;
public const int InputSlot2Id = 1;
public const int OutputSlotId = 2;
public override bool hasPreview
{
get { return false; }
}
name = "Multiply Matrix";
UpdateNodeAfterDeserialization();
}
protected string GetFunctionName()
{
return "unity_matrix_multiply_" + precision;
name = "Matrix Multiply";
public sealed override void UpdateNodeAfterDeserialization()
public override bool hasPreview
AddSlot(GetInputSlot1());
AddSlot(GetInputSlot2());
AddSlot(GetOutputSlot());
RemoveSlotsNameNotMatching(validSlots);
get { return false; }
protected int[] validSlots
protected override MethodInfo GetFunctionToConvert()
get { return new[] { InputSlot1Id, InputSlot2Id, OutputSlotId }; }
return GetType().GetMethod("Unity_MatrixMultiply", BindingFlags.Static | BindingFlags.NonPublic);
protected MaterialSlot GetInputSlot1()
static string Unity_MatrixMultiply(
[Slot(0, Binding.None)] DynamicDimensionMatrix A,
[Slot(1, Binding.None)] DynamicDimensionMatrix B,
[Slot(2, Binding.None)] out DynamicDimensionMatrix Out)
return new Matrix4MaterialSlot(InputSlot1Id, GetInputSlot1Name(), kInputSlot1ShaderName, SlotType.Input);
}
protected MaterialSlot GetInputSlot2()
{
return new Matrix4MaterialSlot(InputSlot2Id, GetInputSlot2Name(), kInputSlot2ShaderName, SlotType.Input);
}
protected MaterialSlot GetOutputSlot()
{
return new Matrix4MaterialSlot(OutputSlotId, GetOutputSlotName(), kOutputSlotShaderName, SlotType.Output);
}
protected virtual string GetInputSlot1Name()
{
return "Input1";
}
protected virtual string GetInputSlot2Name()
{
return "Input2";
}
protected string GetOutputSlotName()
{
return "Output";
}
protected string GetFunctionPrototype(string arg1Name, string arg2Name)
{
return string.Format("inline {0} {1} ({2} {3}, {4} {5})", ConvertConcreteSlotValueTypeToString(precision, FindInputSlot<MaterialSlot>(InputSlot2Id).concreteValueType), GetFunctionName(), ConvertConcreteSlotValueTypeToString(precision, FindInputSlot<MaterialSlot>(InputSlot1Id).concreteValueType), arg1Name, ConvertConcreteSlotValueTypeToString(precision, FindInputSlot<MaterialSlot>(InputSlot2Id).concreteValueType), arg2Name);
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
NodeUtils.SlotConfigurationExceptionIfBadConfiguration(this, new[] { InputSlot1Id, InputSlot2Id }, new[] { OutputSlotId });
string input1Value = GetSlotValue(InputSlot1Id, generationMode);
string input2Value = GetSlotValue(InputSlot2Id, generationMode);
visitor.AddShaderChunk(string.Format("{0} {1} = {2};", ConvertConcreteSlotValueTypeToString(precision, FindInputSlot<MaterialSlot>(InputSlot2Id).concreteValueType), GetVariableNameForSlot(OutputSlotId), GetFunctionCallBody(input1Value, input2Value)), true);
}
protected string GetFunctionCallBody(string input1Value, string input2Value)
{
return GetFunctionName() + " (" + input1Value + ", " + input2Value + ")";
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{
var outputString = new ShaderGenerator();
outputString.AddShaderChunk(GetFunctionPrototype("arg1", "arg2"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk("return mul(arg1, arg2);", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
visitor.AddShaderChunk(outputString.GetShaderString(0), true);
return
@"
{
Out = A * B;
}
";
}
}
}

1
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/SlotValue.cs


public enum SlotValueType
{
SamplerState,
DynamicMatrix,
Matrix4,
Matrix3,
Matrix2,

78
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/DynamicMatrixMaterialSlot.cs


using System;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing.Slots;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class DynamicMatrixMaterialSlot : MaterialSlot, IMaterialSlotHasVaule<Matrix4x4>
{
[SerializeField]
private Matrix4x4 m_Value;
[SerializeField]
private Matrix4x4 m_DefaultValue;
private ConcreteSlotValueType m_ConcreteValueType = ConcreteSlotValueType.Matrix4;
public DynamicMatrixMaterialSlot()
{
}
public DynamicMatrixMaterialSlot(
int slotId,
string displayName,
string shaderOutputName,
SlotType slotType,
ShaderStage shaderStage = ShaderStage.Dynamic,
bool hidden = false)
: base(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden)
{
m_Value = value;
}
public Matrix4x4 defaultValue { get { return m_DefaultValue; } }
public Matrix4x4 value
{
get { return m_Value; }
set { m_Value = value; }
}
public override SlotValueType valueType { get { return SlotValueType.DynamicMatrix; } }
public override ConcreteSlotValueType concreteValueType
{
get { return m_ConcreteValueType; }
}
public void SetConcreteType(ConcreteSlotValueType valueType)
{
m_ConcreteValueType = valueType;
}
protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
var channelCount = (int)SlotValueHelper.GetChannelCount(concreteValueType);
var values = "";
for (var r = 0; r < channelCount; r++)
{
for (var i = 0; i < channelCount; i++)
{
values += ", " + value.GetRow(r)[i];
}
}
return string.Format("{0}{1}({2})", precision, channelCount, values);
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{
}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
}
}
}

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


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