A game about forced loneliness, made by TACStudios
at master 4.2 kB view raw
1using System; 2using UnityEditor.Graphing; 3using UnityEditor.ShaderGraph.Drawing.Slots; 4using UnityEditor.ShaderGraph.Internal; 5using UnityEngine; 6 7using UnityEngine.UIElements; 8 9namespace UnityEditor.ShaderGraph 10{ 11 [Serializable] 12 class DynamicMatrixMaterialSlot : MaterialSlot, IMaterialSlotHasValue<Matrix4x4> 13 { 14 [SerializeField] 15 private Matrix4x4 m_Value = Matrix4x4.identity; 16 17 [SerializeField] 18 private Matrix4x4 m_DefaultValue = Matrix4x4.identity; 19 20 private ConcreteSlotValueType m_ConcreteValueType = ConcreteSlotValueType.Matrix4; 21 22 public DynamicMatrixMaterialSlot() 23 { 24 } 25 26 public DynamicMatrixMaterialSlot( 27 int slotId, 28 string displayName, 29 string shaderOutputName, 30 SlotType slotType, 31 ShaderStageCapability stageCapability = ShaderStageCapability.All, 32 bool hidden = false) 33 : base(slotId, displayName, shaderOutputName, slotType, stageCapability, hidden) 34 { 35 m_Value = value; 36 } 37 38 public override VisualElement InstantiateControl() 39 { 40 return new LabelSlotControlView("Identity"); 41 } 42 43 public Matrix4x4 defaultValue { get { return m_DefaultValue; } } 44 45 public Matrix4x4 value 46 { 47 get { return m_Value; } 48 set { m_Value = value; } 49 } 50 51 public override bool isDefaultValue => value.Equals(defaultValue); 52 53 public override SlotValueType valueType { get { return SlotValueType.DynamicMatrix; } } 54 55 public override ConcreteSlotValueType concreteValueType 56 { 57 get { return m_ConcreteValueType; } 58 } 59 60 public void SetConcreteType(ConcreteSlotValueType valueType) 61 { 62 m_ConcreteValueType = valueType; 63 } 64 65 protected override string ConcreteSlotValueAsVariable() 66 { 67 var channelCount = (int)SlotValueHelper.GetMatrixDimension(concreteValueType); 68 var values = ""; 69 bool isFirst = true; 70 for (var r = 0; r < channelCount; r++) 71 { 72 for (var c = 0; c < channelCount; c++) 73 { 74 if (!isFirst) 75 values += ", "; 76 isFirst = false; 77 values += value.GetRow(r)[c]; 78 } 79 } 80 return string.Format("$precision{0}x{0}({1})", channelCount, values); 81 } 82 83 public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode) 84 { 85 if (!generationMode.IsPreview()) 86 return; 87 88 var matOwner = owner as AbstractMaterialNode; 89 if (matOwner == null) 90 throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode))); 91 92 AbstractShaderProperty property; 93 switch (concreteValueType) 94 { 95 case ConcreteSlotValueType.Matrix4: 96 property = new Matrix4ShaderProperty(); 97 break; 98 case ConcreteSlotValueType.Matrix3: 99 property = new Matrix3ShaderProperty(); 100 break; 101 case ConcreteSlotValueType.Matrix2: 102 property = new Matrix2ShaderProperty(); 103 break; 104 default: 105 throw new ArgumentOutOfRangeException(); 106 } 107 108 property.overrideReferenceName = matOwner.GetVariableNameForSlot(id); 109 property.generatePropertyBlock = false; 110 properties.AddShaderProperty(property); 111 } 112 113 public override void CopyValuesFrom(MaterialSlot foundSlot) 114 { 115 var slot = foundSlot as DynamicMatrixMaterialSlot; 116 if (slot != null) 117 value = slot.value; 118 } 119 120 public override void CopyDefaultValue(MaterialSlot other) 121 { 122 base.CopyDefaultValue(other); 123 if (other is IMaterialSlotHasValue<Matrix4x4> ms) 124 { 125 m_DefaultValue = ms.defaultValue; 126 } 127 } 128 } 129}