|
|
|
|
|
|
//using System.Reflection;
|
|
|
|
//using UnityEngine;
|
|
|
|
//using System.Collections.Generic;
|
|
|
|
|
|
|
|
[Title("Math", "Matrix", "Multiply Matrix By Vector")] |
|
|
|
public class MatrixMultiplyByVectorNode : AbstractMaterialNode, IGeneratesBodyCode, IGeneratesFunction |
|
|
|
[Title("Math", "Matrix", "Matrix Multiply Vector")] |
|
|
|
public class MatrixMultiplyVectorNode : /*CodeFunctionNode*/ AbstractMaterialNode, IGeneratesBodyCode, IGeneratesFunction |
|
|
|
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 |
|
|
|
/*public MatrixMultiplyVectorNode() |
|
|
|
get { return false; } |
|
|
|
name = "Matrix Multiply Vector"; |
|
|
|
public MatrixMultiplyByVectorNode() |
|
|
|
protected override MethodInfo GetFunctionToConvert() |
|
|
|
name = "Multiply Matrix By Vector"; |
|
|
|
UpdateNodeAfterDeserialization(); |
|
|
|
List<MaterialSlot> outSlots = new List<MaterialSlot>(); |
|
|
|
GetOutputSlots(outSlots); |
|
|
|
switch (outSlots[0].concreteValueType) |
|
|
|
{ |
|
|
|
case ConcreteSlotValueType.Vector1: |
|
|
|
return GetType().GetMethod("Unity_MatrixMultiplyVector1", BindingFlags.Static | BindingFlags.NonPublic); |
|
|
|
case ConcreteSlotValueType.Vector2: |
|
|
|
return GetType().GetMethod("Unity_MatrixMultiplyVector2", BindingFlags.Static | BindingFlags.NonPublic); |
|
|
|
case ConcreteSlotValueType.Vector3: |
|
|
|
return GetType().GetMethod("Unity_MatrixMultiplyVector3", BindingFlags.Static | BindingFlags.NonPublic); |
|
|
|
default: |
|
|
|
return GetType().GetMethod("Unity_MatrixMultiplyVector4", BindingFlags.Static | BindingFlags.NonPublic); |
|
|
|
} |
|
|
|
protected string GetFunctionName() |
|
|
|
static string Unity_MatrixMultiplyVector1( |
|
|
|
[Slot(0, Binding.None)] DynamicDimensionVector Vector, |
|
|
|
[Slot(1, Binding.None)] DynamicDimensionMatrix Matrix, |
|
|
|
[Slot(2, Binding.None)] out DynamicDimensionVector Out) |
|
|
|
return "unity_matrix_multiplybyvector_" + precision; |
|
|
|
return |
|
|
|
@"
|
|
|
|
{ |
|
|
|
Out = mul(Vector, Matrix); |
|
|
|
} |
|
|
|
";
|
|
|
|
public sealed override void UpdateNodeAfterDeserialization() |
|
|
|
static string Unity_MatrixMultiplyVector2( |
|
|
|
[Slot(0, Binding.None)] DynamicDimensionVector Vector, |
|
|
|
[Slot(1, Binding.None)] DynamicDimensionMatrix Matrix, |
|
|
|
[Slot(2, Binding.None)] out DynamicDimensionVector Out) |
|
|
|
AddSlot(GetInputSlot1()); |
|
|
|
AddSlot(GetInputSlot2()); |
|
|
|
AddSlot(GetOutputSlot()); |
|
|
|
RemoveSlotsNameNotMatching(validSlots); |
|
|
|
return |
|
|
|
@"
|
|
|
|
{ |
|
|
|
Out = mul(Vector, Matrix); |
|
|
|
} |
|
|
|
";
|
|
|
|
protected int[] validSlots |
|
|
|
static string Unity_MatrixMultiplyVector3( |
|
|
|
[Slot(0, Binding.None)] DynamicDimensionVector Vector, |
|
|
|
[Slot(1, Binding.None)] DynamicDimensionMatrix Matrix, |
|
|
|
[Slot(2, Binding.None)] out DynamicDimensionVector Out) |
|
|
|
get { return new[] { InputSlot1Id, InputSlot2Id, OutputSlotId }; } |
|
|
|
return |
|
|
|
@"
|
|
|
|
{ |
|
|
|
Out = mul(Vector, Matrix); |
|
|
|
} |
|
|
|
";
|
|
|
|
protected MaterialSlot GetInputSlot1() |
|
|
|
static string Unity_MatrixMultiplyVector4( |
|
|
|
[Slot(0, Binding.None)] DynamicDimensionVector Vector, |
|
|
|
[Slot(1, Binding.None)] DynamicDimensionMatrix Matrix, |
|
|
|
[Slot(2, Binding.None)] out DynamicDimensionVector Out) |
|
|
|
return new Matrix4MaterialSlot(InputSlot1Id, GetInputSlot1Name(), kInputSlot1ShaderName, SlotType.Input); |
|
|
|
} |
|
|
|
return |
|
|
|
@"
|
|
|
|
{ |
|
|
|
Out = mul(Vector, Matrix); |
|
|
|
} |
|
|
|
";
|
|
|
|
}*/ |
|
|
|
protected MaterialSlot GetInputSlot2() |
|
|
|
{ |
|
|
|
return new Vector4MaterialSlot(InputSlot2Id, GetInputSlot2Name(), kInputSlot2ShaderName, SlotType.Input, Vector4.zero); |
|
|
|
} |
|
|
|
protected const string k_InputSlot1Name = "Vector"; |
|
|
|
protected const string k_InputSlot2Name = "Matrix"; |
|
|
|
protected const string k_OutputSlotName = "Out"; |
|
|
|
protected MaterialSlot GetOutputSlot() |
|
|
|
public const int InputSlot1Id = 0; |
|
|
|
public const int InputSlot2Id = 1; |
|
|
|
public const int OutputSlotId = 2; |
|
|
|
|
|
|
|
public override bool hasPreview |
|
|
|
return new Vector4MaterialSlot(OutputSlotId, GetOutputSlotName(), kOutputSlotShaderName, SlotType.Output, Vector4.zero); |
|
|
|
get { return true; } |
|
|
|
protected virtual string GetInputSlot1Name() |
|
|
|
public MatrixMultiplyVectorNode() |
|
|
|
return "Input1"; |
|
|
|
name = "Matrix Multiply Vector"; |
|
|
|
UpdateNodeAfterDeserialization(); |
|
|
|
protected virtual string GetInputSlot2Name() |
|
|
|
protected string GetFunctionName() |
|
|
|
return "Input2"; |
|
|
|
int channelCount = (int)SlotValueHelper.GetChannelCount(FindSlot<MaterialSlot>(OutputSlotId).concreteValueType); |
|
|
|
return "Unity_MatrixMultiplyVector_" + precision + channelCount; |
|
|
|
protected string GetOutputSlotName() |
|
|
|
public sealed override void UpdateNodeAfterDeserialization() |
|
|
|
return "Output"; |
|
|
|
AddSlot(new DynamicVectorMaterialSlot(InputSlot1Id, k_InputSlot1Name, k_InputSlot1Name, SlotType.Input, Vector4.zero)); |
|
|
|
AddSlot(new DynamicMatrixMaterialSlot(InputSlot2Id, k_InputSlot2Name, k_InputSlot2Name, SlotType.Input)); |
|
|
|
AddSlot(new DynamicVectorMaterialSlot(OutputSlotId, k_OutputSlotName, k_OutputSlotName, SlotType.Output, Vector4.zero)); |
|
|
|
RemoveSlotsNameNotMatching(new[] { InputSlot1Id, InputSlot2Id, OutputSlotId }); |
|
|
|
protected string GetFunctionPrototype(string arg1Name, string arg2Name) |
|
|
|
protected string GetFunctionPrototype(string argIn1, string argIn2, string argOut) |
|
|
|
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); |
|
|
|
return string.Format("inline void {0} ({1} {2}, {3} {4}, out {5} {6})", GetFunctionName(), |
|
|
|
ConvertConcreteSlotValueTypeToString(precision, FindInputSlot<MaterialSlot>(InputSlot1Id).concreteValueType), argIn1, |
|
|
|
ConvertConcreteSlotValueTypeToString(precision, FindInputSlot<MaterialSlot>(InputSlot2Id).concreteValueType), argIn2, |
|
|
|
ConvertConcreteSlotValueTypeToString(precision, FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType), argOut); |
|
|
|
} |
|
|
|
|
|
|
|
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode 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); |
|
|
|
string outputValue = GetSlotValue(OutputSlotId, generationMode); |
|
|
|
visitor.AddShaderChunk(string.Format("{0} {1};", ConvertConcreteSlotValueTypeToString(precision, FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType), GetVariableNameForSlot(OutputSlotId)), true); |
|
|
|
visitor.AddShaderChunk(GetFunctionCallBody(input1Value, input2Value, outputValue), true); |
|
|
|
protected string GetFunctionCallBody(string input1Value, string input2Value) |
|
|
|
protected string GetFunctionCallBody(string input1Value, string input2Value, string outputValue) |
|
|
|
return string.Format("{0} ({1}, {2})", GetFunctionName(), input1Value, input2Value); |
|
|
|
return string.Format("{0} ({1}, {2}, {3});", GetFunctionName(), input1Value, input2Value, outputValue); |
|
|
|
string vectorString = "Vector"; |
|
|
|
int inputChannelCount = (int)SlotValueHelper.GetChannelCount(FindSlot<MaterialSlot>(InputSlot1Id).concreteValueType); |
|
|
|
if (inputChannelCount == 1) |
|
|
|
{ |
|
|
|
int outputChannelCount = (int)SlotValueHelper.GetChannelCount(FindSlot<MaterialSlot>(OutputSlotId).concreteValueType); |
|
|
|
vectorString += "."; |
|
|
|
for(int i = 0; i < outputChannelCount; i++) |
|
|
|
vectorString += "x"; |
|
|
|
} |
|
|
|
|
|
|
|
outputString.AddShaderChunk(GetFunctionPrototype("arg1", "arg2"), false); |
|
|
|
outputString.AddShaderChunk(GetFunctionPrototype("Vector", "Matrix", "Out"), false); |
|
|
|
outputString.AddShaderChunk("return mul(arg1, arg2);", false); |
|
|
|
outputString.AddShaderChunk(string.Format("Out = mul({0}, Matrix);", vectorString), false); |
|
|
|
outputString.Deindent(); |
|
|
|
outputString.AddShaderChunk("}", false); |
|
|
|
|
|
|
|