浏览代码

Add DynamicMatrixMaterialSlot and fix all Matrix input types

/main
Matt Dean 7 年前
当前提交
1a08b1b1
共有 17 个文件被更改,包括 354 次插入31 次删除
  1. 29
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/DynamicMatrixMaterialSlot.cs
  2. 10
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MaterialSlot.cs
  3. 50
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix2MaterialSlot.cs
  4. 47
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix3MaterialSlot.cs
  5. 47
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix4MaterialSlot.cs
  6. 47
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/AbstractMaterialNode.cs
  7. 15
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Matrix/Matrix2Node.cs
  8. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Matrix/Matrix3Node.cs
  9. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/ShaderGenerator.cs
  10. 29
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix2ShaderProperty.cs
  11. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix2ShaderProperty.cs.meta
  12. 29
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix3ShaderProperty.cs
  13. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix3ShaderProperty.cs.meta
  14. 29
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix4ShaderProperty.cs
  15. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix4ShaderProperty.cs.meta
  16. 25
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MatrixShaderProperty.cs
  17. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MatrixShaderProperty.cs.meta

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


public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{
if (!generationMode.IsPreview())
return;
var matOwner = owner as AbstractMaterialNode;
if (matOwner == null)
throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
IShaderProperty property;
switch (concreteValueType)
{
case ConcreteSlotValueType.Matrix4:
property = new Matrix4ShaderProperty();
break;
case ConcreteSlotValueType.Matrix3:
property = new Matrix3ShaderProperty();
break;
case ConcreteSlotValueType.Matrix2:
property = new Matrix2ShaderProperty();
break;
default:
throw new ArgumentOutOfRangeException();
}
property.overrideReferenceName = matOwner.GetVariableNameForSlot(id);
property.generatePropertyBlock = false;
properties.AddShaderProperty(property);
var slot = foundSlot as DynamicMatrixMaterialSlot;
if (slot != null)
value = slot.value;
}
}
}

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


{
case SlotValueType.SamplerState:
return inputType == SlotValueType.SamplerState;
case SlotValueType.DynamicMatrix:
|| inputType == SlotValueType.Matrix2;
|| inputType == SlotValueType.Matrix2
|| inputType == SlotValueType.DynamicMatrix;
|| inputType == SlotValueType.Matrix2;
|| inputType == SlotValueType.Matrix2
|| inputType == SlotValueType.DynamicMatrix;
return inputType == SlotValueType.Matrix2;
return inputType == SlotValueType.Matrix2
|| inputType == SlotValueType.DynamicMatrix;
case SlotValueType.Texture2D:
return inputType == SlotValueType.Texture2D;
case SlotValueType.Cubemap:

50
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix2MaterialSlot.cs


using System;
using UnityEngine;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph

{
[SerializeField]
private Matrix4x4 m_Value;
[SerializeField]
private Matrix4x4 m_DefaultValue;
public Matrix2MaterialSlot()
{
}

{
}
public Matrix4x4 defaultValue { get { return m_DefaultValue; } }
public Matrix4x4 value
{
get { return m_Value; }
set { m_Value = value; }
}
protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return precision + "2x2 (1,0,0,1)";

{}
{
if (!generationMode.IsPreview())
return;
public override void CopyValuesFrom(MaterialSlot foundSlot)
var matOwner = owner as AbstractMaterialNode;
if (matOwner == null)
throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
var property = new Matrix2ShaderProperty()
{
overrideReferenceName = matOwner.GetVariableNameForSlot(id),
generatePropertyBlock = false,
value = value
};
properties.AddShaderProperty(property);
}
public override PreviewProperty GetPreviewProperty(string name)
var pp = new PreviewProperty
{
name = name,
propType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
vector4Value = new Vector4(value.GetRow(0).x, value.GetRow(0).y, 0, 0),
floatValue = value.GetRow(0).x,
colorValue = new Vector4(value.GetRow(0).x, value.GetRow(0).x, 0, 0)
};
return pp;
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
var slot = foundSlot as Matrix2MaterialSlot;
if (slot != null)
value = slot.value;
}
}
}

47
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix3MaterialSlot.cs


using System;
using UnityEngine;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph

{
[SerializeField]
private Matrix4x4 m_Value;
[SerializeField]
private Matrix4x4 m_DefaultValue;
public Matrix3MaterialSlot()
{
}

{
}
public Matrix4x4 defaultValue { get { return m_DefaultValue; } }
public Matrix4x4 value
{
get { return m_Value; }
set { m_Value = value; }
}
protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return precision + "3x3 (1,0,0,0,1,0,0,0,1)";

{
if (!generationMode.IsPreview())
return;
var matOwner = owner as AbstractMaterialNode;
if (matOwner == null)
throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
var property = new Matrix3ShaderProperty()
{
overrideReferenceName = matOwner.GetVariableNameForSlot(id),
generatePropertyBlock = false,
value = value
};
properties.AddShaderProperty(property);
public override void CopyValuesFrom(MaterialSlot foundSlot)
public override PreviewProperty GetPreviewProperty(string name)
var pp = new PreviewProperty
{
name = name,
propType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
vector4Value = new Vector4(value.GetRow(0).x, value.GetRow(0).y, value.GetRow(0).z, 0),
floatValue = value.GetRow(0).x,
colorValue = new Vector4(value.GetRow(0).x, value.GetRow(0).x, value.GetRow(0).z, 0)
};
return pp;
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
var slot = foundSlot as Matrix3MaterialSlot;
if (slot != null)
value = slot.value;
}
}
}

47
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix4MaterialSlot.cs


using System;
using UnityEngine;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph

{
[SerializeField]
private Matrix4x4 m_Value;
[SerializeField]
private Matrix4x4 m_DefaultValue;
public Matrix4MaterialSlot()
{
}

{
}
public Matrix4x4 defaultValue { get { return m_DefaultValue; } }
public Matrix4x4 value
{
get { return m_Value; }
set { m_Value = value; }
}
protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return precision + "4x4 (1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1)";

{
if (!generationMode.IsPreview())
return;
var matOwner = owner as AbstractMaterialNode;
if (matOwner == null)
throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
var property = new Matrix4ShaderProperty()
{
overrideReferenceName = matOwner.GetVariableNameForSlot(id),
generatePropertyBlock = false,
value = value
};
properties.AddShaderProperty(property);
public override void CopyValuesFrom(MaterialSlot foundSlot)
public override PreviewProperty GetPreviewProperty(string name)
var pp = new PreviewProperty
{
name = name,
propType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
vector4Value = new Vector4(value.GetRow(0).x, value.GetRow(0).y, value.GetRow(0).z, value.GetRow(0).w),
floatValue = value.GetRow(0).x,
colorValue = new Vector4(value.GetRow(0).x, value.GetRow(0).x, value.GetRow(0).z, value.GetRow(0).w)
};
return pp;
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
var slot = foundSlot as Matrix4MaterialSlot;
if (slot != null)
value = slot.value;
}
}
}

47
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/AbstractMaterialNode.cs


return ConcreteSlotValueType.Vector1;
}
private ConcreteSlotValueType ConvertDynamicMatrixInputTypeToConcrete(IEnumerable<ConcreteSlotValueType> inputTypes)
{
var concreteSlotValueTypes = inputTypes as IList<ConcreteSlotValueType> ?? inputTypes.ToList();
var inputTypesDistinct = concreteSlotValueTypes.Distinct().ToList();
switch (inputTypesDistinct.Count)
{
case 0:
return ConcreteSlotValueType.Matrix2;
case 1:
return inputTypesDistinct.FirstOrDefault();
default:
var ordered = inputTypesDistinct.OrderByDescending(x => x);
if (ordered.Any())
return ordered.FirstOrDefault();
break;
}
return ConcreteSlotValueType.Matrix2;
}
public override void ValidateNode()
{
var isInError = false;

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
s_TempSlots.Clear();
GetInputSlots(s_TempSlots);

{
if (inputSlot is DynamicVectorMaterialSlot)
skippedDynamicSlots.Add(inputSlot as DynamicVectorMaterialSlot);
if (inputSlot is DynamicMatrixMaterialSlot)
skippedDynamicMatrixSlots.Add(inputSlot as DynamicMatrixMaterialSlot);
continue;
}

dynamicInputSlotsToCompare.Add((DynamicVectorMaterialSlot)inputSlot, outputConcreteType);
continue;
}
else if (inputSlot is DynamicMatrixMaterialSlot)
{
dynamicMatrixInputSlotsToCompare.Add((DynamicMatrixMaterialSlot)inputSlot, outputConcreteType);
continue;
}
// if we have a standard connection... just check the types work!
if (!ImplicitConversionExists(outputConcreteType, inputSlot.concreteValueType))

foreach (var skippedSlot in skippedDynamicSlots)
skippedSlot.SetConcreteType(dynamicType);
// 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);
s_TempSlots.Clear();
GetInputSlots(s_TempSlots);
var inputError = s_TempSlots.Any(x => x.hasError);

if (outputSlot is DynamicVectorMaterialSlot)
{
(outputSlot as DynamicVectorMaterialSlot).SetConcreteType(dynamicType);
continue;
}
else if (outputSlot is DynamicMatrixMaterialSlot)
{
(outputSlot as DynamicMatrixMaterialSlot).SetConcreteType(dynamicMatrixType);
continue;
}
}

ListPool<DynamicVectorMaterialSlot>.Release(skippedDynamicSlots);
DictionaryPool<DynamicVectorMaterialSlot, ConcreteSlotValueType>.Release(dynamicInputSlotsToCompare);
ListPool<DynamicMatrixMaterialSlot>.Release(skippedDynamicMatrixSlots);
DictionaryPool<DynamicMatrixMaterialSlot, ConcreteSlotValueType>.Release(dynamicMatrixInputSlotsToCompare);
}
public int version { get; private set; }

case ConcreteSlotValueType.Cubemap:
return "Cubemap";
case ConcreteSlotValueType.Matrix2:
return p + "x2x2";
return p + "2x2";
case ConcreteSlotValueType.Matrix3:
return p + "3x3";
case ConcreteSlotValueType.Matrix4:

15
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Matrix/Matrix2Node.cs


RemoveSlotsNameNotMatching(new[] { kOutputSlotId });
}
public string propertyName
{
get
{
return string.Format("{0}_{1}_Uniform", name, GetVariableNameForNode());
}
}
return propertyName;
return GetVariableNameForNode();
//if (exposedState == ExposedState.Exposed || generationMode.IsPreview())
// return;
visitor.AddShaderChunk(precision + "2 " + name + " = " + precision + "2x2 (" + m_Row0.x + ", " + m_Row0.y + ", " + m_Row1.x + ", " + m_Row1.y + ");", true);
visitor.AddShaderChunk(precision + "2x2 " + GetVariableNameForNode() + " = " + precision + "2x2 (" + m_Row0.x + ", " + m_Row0.y + ", " + m_Row1.x + ", " + m_Row1.y + ");", true);
}
[SerializeField]

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Matrix/Matrix3Node.cs


RemoveSlotsNameNotMatching(new[] { kOutputSlotId });
}
public string propertyName
{
get { return string.Format("{0}_{1}_Uniform", name, GetVariableNameForNode()); }
}
return propertyName;
return GetVariableNameForNode();
//if (exposedState == ExposedState.Exposed || generationMode.IsPreview())
// return;
visitor.AddShaderChunk(precision + "3x3 " + propertyName + " = " + precision + "3x3 (" + m_Row0.x + ", " + m_Row0.y + ", " + m_Row0.z + ", " + m_Row1.x + ", " + m_Row1.y + ", " + m_Row1.z + ", " + m_Row2.x + ", " + m_Row2.y + ", " + m_Row2.z + ");", true);
visitor.AddShaderChunk(precision + "3x3 " + GetVariableNameForNode() + " = " + precision + "3x3 (" + m_Row0.x + ", " + m_Row0.y + ", " + m_Row0.z + ", " + m_Row1.x + ", " + m_Row1.y + ", " + m_Row1.z + ", " + m_Row2.x + ", " + m_Row2.y + ", " + m_Row2.z + ");", true);
}
[SerializeField]

4
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/ShaderGenerator.cs


default:
return kErrorString;
}
case ConcreteSlotValueType.Matrix3:
return rawOutput;
case ConcreteSlotValueType.Matrix2:
return rawOutput;
default:
return kErrorString;
}

29
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix2ShaderProperty.cs


using System;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class Matrix2ShaderProperty : MatrixShaderProperty
{
public Matrix2ShaderProperty()
{
displayName = "Matrix2";
}
public override PropertyType propertyType
{
get { return PropertyType.Matrix2; }
}
public override Vector4 defaultValue
{
get { return new Vector4(); }
}
public override PreviewProperty GetPreviewMaterialProperty()
{
return default(PreviewProperty);
}
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix2ShaderProperty.cs.meta


fileFormatVersion: 2
guid: 0755d7f7a7e11cb4294563bbd0d434cc
timeCreated: 1505346935

29
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix3ShaderProperty.cs


using System;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class Matrix3ShaderProperty : MatrixShaderProperty
{
public Matrix3ShaderProperty()
{
displayName = "Matrix3";
}
public override PropertyType propertyType
{
get { return PropertyType.Matrix3; }
}
public override Vector4 defaultValue
{
get { return new Vector4(); }
}
public override PreviewProperty GetPreviewMaterialProperty()
{
return default(PreviewProperty);
}
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix3ShaderProperty.cs.meta


fileFormatVersion: 2
guid: 95c9c50f50c70c8479180b855a3bd255
timeCreated: 1505346935

29
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix4ShaderProperty.cs


using System;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class Matrix4ShaderProperty : MatrixShaderProperty
{
public Matrix4ShaderProperty()
{
displayName = "Matrix4";
}
public override PropertyType propertyType
{
get { return PropertyType.Matrix4; }
}
public override Vector4 defaultValue
{
get { return new Vector4(); }
}
public override PreviewProperty GetPreviewMaterialProperty()
{
return default(PreviewProperty);
}
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix4ShaderProperty.cs.meta


fileFormatVersion: 2
guid: 6379ce3fb15957343ba875d18f87efbc
timeCreated: 1505346935

25
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MatrixShaderProperty.cs


using System;
using System.Text;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public abstract class MatrixShaderProperty : AbstractShaderProperty<Matrix4x4>
{
public override string GetPropertyBlockString()
{
return string.Empty;
}
public override string GetPropertyDeclarationString()
{
return "float4x4 " + referenceName + ";";
}
public override PreviewProperty GetPreviewMaterialProperty()
{
return default(PreviewProperty);
}
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MatrixShaderProperty.cs.meta


fileFormatVersion: 2
guid: 79075442664faac49b00b4c93d28e08b
timeCreated: 1505408377
正在加载...
取消
保存