initial commit
This commit is contained in:
parent
6715289efe
commit
788c3389af
37645 changed files with 2526849 additions and 80 deletions
|
@ -0,0 +1,116 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEditor.Graphing;
|
||||
using UnityEditor.ShaderGraph.Drawing.Controls;
|
||||
|
||||
namespace UnityEditor.ShaderGraph
|
||||
{
|
||||
[Title("Math", "Matrix", "Matrix Construction")]
|
||||
class MatrixConstructionNode : AbstractMaterialNode, IGeneratesBodyCode, IGeneratesFunction
|
||||
{
|
||||
const string kInputSlotM0Name = "M0";
|
||||
const string kInputSlotM1Name = "M1";
|
||||
const string kInputSlotM2Name = "M2";
|
||||
const string kInputSlotM3Name = "M3";
|
||||
const string kOutput4x4SlotName = "4x4";
|
||||
const string kOutput3x3SlotName = "3x3";
|
||||
const string kOutput2x2SlotName = "2x2";
|
||||
|
||||
public const int InputSlotM0Id = 0;
|
||||
public const int InputSlotM1Id = 1;
|
||||
public const int InputSlotM2Id = 2;
|
||||
public const int InputSlotM3Id = 3;
|
||||
public const int Output4x4SlotId = 4;
|
||||
public const int Output3x3SlotId = 5;
|
||||
public const int Output2x2SlotId = 6;
|
||||
|
||||
public MatrixConstructionNode()
|
||||
{
|
||||
name = "Matrix Construction";
|
||||
UpdateNodeAfterDeserialization();
|
||||
}
|
||||
|
||||
[SerializeField]
|
||||
MatrixAxis m_Axis;
|
||||
|
||||
[EnumControl("")]
|
||||
MatrixAxis axis
|
||||
{
|
||||
get { return m_Axis; }
|
||||
set
|
||||
{
|
||||
if (m_Axis.Equals(value))
|
||||
return;
|
||||
m_Axis = value;
|
||||
Dirty(ModificationScope.Graph);
|
||||
}
|
||||
}
|
||||
|
||||
string GetFunctionName()
|
||||
{
|
||||
return $"Unity_MatrixConstruction_{axis}_$precision";
|
||||
}
|
||||
|
||||
public sealed override void UpdateNodeAfterDeserialization()
|
||||
{
|
||||
AddSlot(new Vector4MaterialSlot(InputSlotM0Id, kInputSlotM0Name, kInputSlotM0Name, SlotType.Input, Vector4.zero));
|
||||
AddSlot(new Vector4MaterialSlot(InputSlotM1Id, kInputSlotM1Name, kInputSlotM1Name, SlotType.Input, Vector4.zero));
|
||||
AddSlot(new Vector4MaterialSlot(InputSlotM2Id, kInputSlotM2Name, kInputSlotM2Name, SlotType.Input, Vector4.zero));
|
||||
AddSlot(new Vector4MaterialSlot(InputSlotM3Id, kInputSlotM3Name, kInputSlotM3Name, SlotType.Input, Vector4.zero));
|
||||
AddSlot(new Matrix4MaterialSlot(Output4x4SlotId, kOutput4x4SlotName, kOutput4x4SlotName, SlotType.Output));
|
||||
AddSlot(new Matrix3MaterialSlot(Output3x3SlotId, kOutput3x3SlotName, kOutput3x3SlotName, SlotType.Output));
|
||||
AddSlot(new Matrix2MaterialSlot(Output2x2SlotId, kOutput2x2SlotName, kOutput2x2SlotName, SlotType.Output));
|
||||
RemoveSlotsNameNotMatching(new int[] { InputSlotM0Id, InputSlotM1Id, InputSlotM2Id, InputSlotM3Id, Output4x4SlotId, Output3x3SlotId, Output2x2SlotId });
|
||||
}
|
||||
|
||||
public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
|
||||
{
|
||||
var inputM0Value = GetSlotValue(InputSlotM0Id, generationMode);
|
||||
var inputM1Value = GetSlotValue(InputSlotM1Id, generationMode);
|
||||
var inputM2Value = GetSlotValue(InputSlotM2Id, generationMode);
|
||||
var inputM3Value = GetSlotValue(InputSlotM3Id, generationMode);
|
||||
|
||||
sb.AppendLine("{0} {1};", FindOutputSlot<MaterialSlot>(Output4x4SlotId).concreteValueType.ToShaderString(), GetVariableNameForSlot(Output4x4SlotId));
|
||||
sb.AppendLine("{0} {1};", FindOutputSlot<MaterialSlot>(Output3x3SlotId).concreteValueType.ToShaderString(), GetVariableNameForSlot(Output3x3SlotId));
|
||||
sb.AppendLine("{0} {1};", FindOutputSlot<MaterialSlot>(Output2x2SlotId).concreteValueType.ToShaderString(), GetVariableNameForSlot(Output2x2SlotId));
|
||||
sb.AppendLine("{0}({1}, {2}, {3}, {4}, {5}, {6}, {7});",
|
||||
GetFunctionName(),
|
||||
inputM0Value,
|
||||
inputM1Value,
|
||||
inputM2Value,
|
||||
inputM3Value,
|
||||
GetVariableNameForSlot(Output4x4SlotId),
|
||||
GetVariableNameForSlot(Output3x3SlotId),
|
||||
GetVariableNameForSlot(Output2x2SlotId));
|
||||
}
|
||||
|
||||
public void GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
|
||||
{
|
||||
registry.ProvideFunction(GetFunctionName(), s =>
|
||||
{
|
||||
s.AppendLine("void {0} ({1} M0, {1} M1, {1} M2, {1} M3, out {2} Out4x4, out {3} Out3x3, out {4} Out2x2)",
|
||||
GetFunctionName(),
|
||||
FindInputSlot<MaterialSlot>(InputSlotM0Id).concreteValueType.ToShaderString(),
|
||||
FindOutputSlot<MaterialSlot>(Output4x4SlotId).concreteValueType.ToShaderString(),
|
||||
FindOutputSlot<MaterialSlot>(Output3x3SlotId).concreteValueType.ToShaderString(),
|
||||
FindOutputSlot<MaterialSlot>(Output2x2SlotId).concreteValueType.ToShaderString());
|
||||
using (s.BlockScope())
|
||||
{
|
||||
switch (m_Axis)
|
||||
{
|
||||
case MatrixAxis.Column:
|
||||
s.AppendLine("Out4x4 = $precision4x4(M0.x, M1.x, M2.x, M3.x, M0.y, M1.y, M2.y, M3.y, M0.z, M1.z, M2.z, M3.z, M0.w, M1.w, M2.w, M3.w);");
|
||||
s.AppendLine("Out3x3 = $precision3x3(M0.x, M1.x, M2.x, M0.y, M1.y, M2.y, M0.z, M1.z, M2.z);");
|
||||
s.AppendLine("Out2x2 = $precision2x2(M0.x, M1.x, M0.y, M1.y);");
|
||||
break;
|
||||
default:
|
||||
s.AppendLine("Out4x4 = $precision4x4(M0.x, M0.y, M0.z, M0.w, M1.x, M1.y, M1.z, M1.w, M2.x, M2.y, M2.z, M2.w, M3.x, M3.y, M3.z, M3.w);");
|
||||
s.AppendLine("Out3x3 = $precision3x3(M0.x, M0.y, M0.z, M1.x, M1.y, M1.z, M2.x, M2.y, M2.z);");
|
||||
s.AppendLine("Out2x2 = $precision2x2(M0.x, M0.y, M1.x, M1.y);");
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 7f3857db61fc34eaabd871901ae5a2b6
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
|
@ -0,0 +1,33 @@
|
|||
using System.Reflection;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityEditor.ShaderGraph
|
||||
{
|
||||
[Title("Math", "Matrix", "Matrix Determinant")]
|
||||
class MatrixDeterminantNode : CodeFunctionNode
|
||||
{
|
||||
public MatrixDeterminantNode()
|
||||
{
|
||||
name = "Matrix Determinant";
|
||||
}
|
||||
|
||||
public override bool hasPreview => false;
|
||||
|
||||
protected override MethodInfo GetFunctionToConvert()
|
||||
{
|
||||
return GetType().GetMethod("Unity_MatrixDeterminant", BindingFlags.Static | BindingFlags.NonPublic);
|
||||
}
|
||||
|
||||
static string Unity_MatrixDeterminant(
|
||||
[Slot(0, Binding.None)] DynamicDimensionMatrix In,
|
||||
[Slot(1, Binding.None)] out Vector1 Out)
|
||||
{
|
||||
return
|
||||
@"
|
||||
{
|
||||
Out = determinant(In);
|
||||
}
|
||||
";
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 41d69945fb0fa45ec8e14292dfb9074d
|
||||
timeCreated: 1495542985
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,249 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEditor.Graphing;
|
||||
using UnityEditor.ShaderGraph.Drawing.Controls;
|
||||
using UnityEditor.Graphing.Util;
|
||||
using UnityEngine.Pool;
|
||||
|
||||
namespace UnityEditor.ShaderGraph
|
||||
{
|
||||
enum MatrixAxis
|
||||
{
|
||||
Row,
|
||||
Column
|
||||
}
|
||||
|
||||
[Title("Math", "Matrix", "Matrix Split")]
|
||||
class MatrixSplitNode : AbstractMaterialNode, IGeneratesBodyCode
|
||||
{
|
||||
const string kInputSlotName = "In";
|
||||
const string kOutputSlotM0Name = "M0";
|
||||
const string kOutputSlotM1Name = "M1";
|
||||
const string kOutputSlotM2Name = "M2";
|
||||
const string kOutputSlotM3Name = "M3";
|
||||
|
||||
public const int InputSlotId = 0;
|
||||
public const int OutputSlotRId = 1;
|
||||
public const int OutputSlotGId = 2;
|
||||
public const int OutputSlotBId = 3;
|
||||
public const int OutputSlotAId = 4;
|
||||
|
||||
public MatrixSplitNode()
|
||||
{
|
||||
name = "Matrix Split";
|
||||
UpdateNodeAfterDeserialization();
|
||||
}
|
||||
|
||||
[SerializeField]
|
||||
MatrixAxis m_Axis;
|
||||
|
||||
[EnumControl("")]
|
||||
MatrixAxis axis
|
||||
{
|
||||
get { return m_Axis; }
|
||||
set
|
||||
{
|
||||
if (m_Axis.Equals(value))
|
||||
return;
|
||||
m_Axis = value;
|
||||
Dirty(ModificationScope.Graph);
|
||||
}
|
||||
}
|
||||
|
||||
static string[] s_ComponentList = new string[4] { "r", "g", "b", "a" };
|
||||
|
||||
public sealed override void UpdateNodeAfterDeserialization()
|
||||
{
|
||||
AddSlot(new DynamicMatrixMaterialSlot(InputSlotId, kInputSlotName, kInputSlotName, SlotType.Input));
|
||||
AddSlot(new DynamicVectorMaterialSlot(OutputSlotRId, kOutputSlotM0Name, kOutputSlotM0Name, SlotType.Output, Vector4.zero));
|
||||
AddSlot(new DynamicVectorMaterialSlot(OutputSlotGId, kOutputSlotM1Name, kOutputSlotM1Name, SlotType.Output, Vector4.zero));
|
||||
AddSlot(new DynamicVectorMaterialSlot(OutputSlotBId, kOutputSlotM2Name, kOutputSlotM2Name, SlotType.Output, Vector4.zero));
|
||||
AddSlot(new DynamicVectorMaterialSlot(OutputSlotAId, kOutputSlotM3Name, kOutputSlotM3Name, SlotType.Output, Vector4.zero));
|
||||
RemoveSlotsNameNotMatching(new int[] { InputSlotId, OutputSlotRId, OutputSlotGId, OutputSlotBId, OutputSlotAId });
|
||||
}
|
||||
|
||||
static int[] s_OutputSlots = { OutputSlotRId, OutputSlotGId, OutputSlotBId, OutputSlotAId };
|
||||
|
||||
public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
|
||||
{
|
||||
var inputValue = GetSlotValue(InputSlotId, generationMode);
|
||||
|
||||
var inputSlot = FindInputSlot<MaterialSlot>(InputSlotId);
|
||||
var numInputRows = 0;
|
||||
bool useIndentity = false;
|
||||
|
||||
if (inputSlot != null)
|
||||
{
|
||||
numInputRows = SlotValueHelper.GetMatrixDimension(inputSlot.concreteValueType);
|
||||
if (numInputRows > 4)
|
||||
numInputRows = 0;
|
||||
|
||||
if (!owner.GetEdges(inputSlot.slotReference).Any())
|
||||
{
|
||||
numInputRows = 0;
|
||||
useIndentity = true;
|
||||
}
|
||||
}
|
||||
|
||||
int concreteRowCount = useIndentity ? 2 : numInputRows;
|
||||
|
||||
for (var r = 0; r < 4; r++)
|
||||
{
|
||||
string outputValue;
|
||||
if (r >= numInputRows)
|
||||
{
|
||||
outputValue = string.Format("$precision{0}(", concreteRowCount);
|
||||
for (int c = 0; c < concreteRowCount; c++)
|
||||
{
|
||||
if (c != 0)
|
||||
outputValue += ", ";
|
||||
outputValue += Matrix4x4.identity.GetRow(r)[c];
|
||||
}
|
||||
outputValue += ")";
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (m_Axis)
|
||||
{
|
||||
case MatrixAxis.Column:
|
||||
outputValue = string.Format("$precision{0}(", numInputRows);
|
||||
for (int c = 0; c < numInputRows; c++)
|
||||
{
|
||||
if (c != 0)
|
||||
outputValue += ", ";
|
||||
outputValue += string.Format("{0}[{1}].{2}", inputValue, c, s_ComponentList[r]);
|
||||
}
|
||||
outputValue += ")";
|
||||
break;
|
||||
default:
|
||||
outputValue = string.Format("{0}[{1}]", inputValue, r);
|
||||
break;
|
||||
}
|
||||
}
|
||||
sb.AppendLine(string.Format("$precision{0} {1} = {2};", concreteRowCount, GetVariableNameForSlot(s_OutputSlots[r]), outputValue));
|
||||
}
|
||||
}
|
||||
|
||||
public override void EvaluateDynamicMaterialSlots(List<MaterialSlot> inputSlots, List<MaterialSlot> outputSlots)
|
||||
{
|
||||
var dynamicInputSlotsToCompare = DictionaryPool<DynamicVectorMaterialSlot, ConcreteSlotValueType>.Get();
|
||||
var skippedDynamicSlots = ListPool<DynamicVectorMaterialSlot>.Get();
|
||||
|
||||
var dynamicMatrixInputSlotsToCompare = DictionaryPool<DynamicMatrixMaterialSlot, ConcreteSlotValueType>.Get();
|
||||
var skippedDynamicMatrixSlots = ListPool<DynamicMatrixMaterialSlot>.Get();
|
||||
|
||||
// iterate the input slots
|
||||
{
|
||||
foreach (var inputSlot in inputSlots)
|
||||
{
|
||||
inputSlot.hasError = false;
|
||||
|
||||
// if there is a connection
|
||||
var edges = owner.GetEdges(inputSlot.slotReference).ToList();
|
||||
if (!edges.Any())
|
||||
{
|
||||
if (inputSlot is DynamicVectorMaterialSlot)
|
||||
skippedDynamicSlots.Add(inputSlot as DynamicVectorMaterialSlot);
|
||||
if (inputSlot is DynamicMatrixMaterialSlot)
|
||||
skippedDynamicMatrixSlots.Add(inputSlot as DynamicMatrixMaterialSlot);
|
||||
continue;
|
||||
}
|
||||
|
||||
// get the output details
|
||||
var outputSlotRef = edges[0].outputSlot;
|
||||
var outputNode = outputSlotRef.node;
|
||||
if (outputNode == null)
|
||||
continue;
|
||||
|
||||
var outputSlot = outputNode.FindOutputSlot<MaterialSlot>(outputSlotRef.slotId);
|
||||
if (outputSlot == null)
|
||||
continue;
|
||||
|
||||
if (outputSlot.hasError)
|
||||
{
|
||||
inputSlot.hasError = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
var outputConcreteType = outputSlot.concreteValueType;
|
||||
// dynamic input... depends on output from other node.
|
||||
// we need to compare ALL dynamic inputs to make sure they
|
||||
// are compatable.
|
||||
if (inputSlot is DynamicVectorMaterialSlot)
|
||||
{
|
||||
dynamicInputSlotsToCompare.Add((DynamicVectorMaterialSlot)inputSlot, outputConcreteType);
|
||||
continue;
|
||||
}
|
||||
else if (inputSlot is DynamicMatrixMaterialSlot)
|
||||
{
|
||||
dynamicMatrixInputSlotsToCompare.Add((DynamicMatrixMaterialSlot)inputSlot, outputConcreteType);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// and now dynamic matrices
|
||||
var dynamicMatrixType = ConvertDynamicMatrixInputTypeToConcrete(dynamicMatrixInputSlotsToCompare.Values);
|
||||
foreach (var dynamicKvP in dynamicMatrixInputSlotsToCompare)
|
||||
dynamicKvP.Key.SetConcreteType(dynamicMatrixType);
|
||||
foreach (var skippedSlot in skippedDynamicMatrixSlots)
|
||||
skippedSlot.SetConcreteType(dynamicMatrixType);
|
||||
|
||||
// we can now figure out the dynamic slotType
|
||||
// from here set all the
|
||||
var dynamicType = SlotValueHelper.ConvertMatrixToVectorType(dynamicMatrixType);
|
||||
foreach (var dynamicKvP in dynamicInputSlotsToCompare)
|
||||
dynamicKvP.Key.SetConcreteType(dynamicType);
|
||||
foreach (var skippedSlot in skippedDynamicSlots)
|
||||
skippedSlot.SetConcreteType(dynamicType);
|
||||
|
||||
bool inputError = inputSlots.Any(x => x.hasError);
|
||||
if (inputError)
|
||||
{
|
||||
owner.AddConcretizationError(objectId, string.Format("Node {0} had input error", objectId));
|
||||
hasError = true;
|
||||
}
|
||||
// configure the output slots now
|
||||
// their slotType will either be the default output slotType
|
||||
// or the above dynanic slotType for dynamic nodes
|
||||
// or error if there is an input error
|
||||
foreach (var outputSlot in outputSlots)
|
||||
{
|
||||
outputSlot.hasError = false;
|
||||
|
||||
if (inputError)
|
||||
{
|
||||
outputSlot.hasError = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (outputSlot is DynamicVectorMaterialSlot)
|
||||
{
|
||||
(outputSlot as DynamicVectorMaterialSlot).SetConcreteType(dynamicType);
|
||||
continue;
|
||||
}
|
||||
else if (outputSlot is DynamicMatrixMaterialSlot)
|
||||
{
|
||||
(outputSlot as DynamicMatrixMaterialSlot).SetConcreteType(dynamicMatrixType);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (outputSlots.Any(x => x.hasError))
|
||||
{
|
||||
owner.AddConcretizationError(objectId, string.Format("Node {0} had output error", objectId));
|
||||
hasError = true;
|
||||
}
|
||||
}
|
||||
|
||||
CalculateNodeHasError();
|
||||
|
||||
ListPool<DynamicVectorMaterialSlot>.Release(skippedDynamicSlots);
|
||||
DictionaryPool<DynamicVectorMaterialSlot, ConcreteSlotValueType>.Release(dynamicInputSlotsToCompare);
|
||||
|
||||
ListPool<DynamicMatrixMaterialSlot>.Release(skippedDynamicMatrixSlots);
|
||||
DictionaryPool<DynamicMatrixMaterialSlot, ConcreteSlotValueType>.Release(dynamicMatrixInputSlotsToCompare);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 03599e40507c24caba0dd9b596f3b5dc
|
||||
timeCreated: 1490896965
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,36 @@
|
|||
using System.Reflection;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityEditor.ShaderGraph
|
||||
{
|
||||
[Title("Math", "Matrix", "Matrix Transpose")]
|
||||
class MatrixTransposeNode : CodeFunctionNode
|
||||
{
|
||||
public MatrixTransposeNode()
|
||||
{
|
||||
name = "Matrix Transpose";
|
||||
}
|
||||
|
||||
public override bool hasPreview
|
||||
{
|
||||
get { return false; }
|
||||
}
|
||||
|
||||
protected override MethodInfo GetFunctionToConvert()
|
||||
{
|
||||
return GetType().GetMethod("Unity_MatrixTranspose", BindingFlags.Static | BindingFlags.NonPublic);
|
||||
}
|
||||
|
||||
static string Unity_MatrixTranspose(
|
||||
[Slot(0, Binding.None)] DynamicDimensionMatrix In,
|
||||
[Slot(1, Binding.None)] out DynamicDimensionMatrix Out)
|
||||
{
|
||||
return
|
||||
@"
|
||||
{
|
||||
Out = transpose(In);
|
||||
}
|
||||
";
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 19da2635a60154a6793334e535a0a856
|
||||
timeCreated: 1495542985
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Add table
Add a link
Reference in a new issue