浏览代码

Boolean node and type (part 1)

/main
Matt Dean 7 年前
当前提交
8f22e3a7
共有 13 个文件被更改,包括 300 次插入3 次删除
  1. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MaterialSlot.cs
  2. 18
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/PreviewProperty.cs
  3. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/CodeFunctionNode.cs
  4. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/PropertyType.cs
  5. 9
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/SlotValue.cs
  6. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorView.cs
  7. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs
  8. 91
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/BooleanMaterialSlot.cs
  9. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/BooleanMaterialSlot.cs.meta
  10. 58
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/BooleanShaderProperty.cs
  11. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/BooleanShaderProperty.cs.meta
  12. 84
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/BooleanNode.cs
  13. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/BooleanNode.cs.meta

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


return "(3)";
case ConcreteSlotValueType.Vector4:
return "(4)";
case ConcreteSlotValueType.Boolean:
return "(B)";
case ConcreteSlotValueType.Matrix2:
return "(2x2)";
case ConcreteSlotValueType.Matrix3:

return new Vector2MaterialSlot(slotId, displayName, shaderOutputName, slotType, defaultValue, shaderStage, hidden);
case SlotValueType.Vector1:
return new Vector1MaterialSlot(slotId, displayName, shaderOutputName, slotType, defaultValue.x, shaderStage, hidden);
case SlotValueType.Boolean:
return new BooleanMaterialSlot(slotId, displayName, shaderOutputName, slotType, false, shaderStage, hidden);
}
throw new ArgumentOutOfRangeException("type", type, null);

|| inputType == SlotValueType.Vector2
|| inputType == SlotValueType.Vector1
|| inputType == SlotValueType.Dynamic;
case SlotValueType.Boolean:
return inputType == SlotValueType.Vector1;
}
return false;
}

return PropertyType.Texture;
case ConcreteSlotValueType.Cubemap:
return PropertyType.Cubemap;
case ConcreteSlotValueType.Boolean:
return PropertyType.Boolean;
case ConcreteSlotValueType.Vector1:
return PropertyType.Float;
case ConcreteSlotValueType.Vector2:

18
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/PreviewProperty.cs


public Vector4 vector4Value;
[FieldOffset(0)]
public float floatValue;
[FieldOffset(0)]
public bool booleanValue;
}
Data m_Data;

if (propType != PropertyType.Float)
throw new ArgumentException(string.Format(k_SetErrorMessage, PropertyType.Float, propType));
m_Data.floatValue = value;
}
}
public bool booleanValue
{
get
{
if (propType != PropertyType.Boolean)
throw new ArgumentException(string.Format(k_GetErrorMessage, PropertyType.Boolean, propType));
return m_Data.booleanValue;
}
set
{
if (propType != PropertyType.Boolean)
throw new ArgumentException(string.Format(k_SetErrorMessage, PropertyType.Boolean, propType));
m_Data.booleanValue = value;
}
}

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


if (p.ParameterType.IsByRef)
t = p.ParameterType.GetElementType();
if (t == typeof(Boolean))
{
return SlotValueType.Boolean;
}
if (t == typeof(Vector1))
{
return SlotValueType.Vector1;

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


Color,
Texture,
Cubemap,
Boolean,
Float,
Vector2,
Vector3,

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


Vector4,
Vector3,
Vector2,
Vector1
Vector1,
Boolean
}
public enum ConcreteSlotValueType

Vector4,
Vector3,
Vector2,
Vector1
Vector1,
Boolean
}
public static class SlotValueHelper

"typeFloat4",
"typeFloat3",
"typeFloat2",
"typeFloat1"
"typeFloat1",
"typeBoolean"
};
public static string ToClassName(this ConcreteSlotValueType type)

1
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorView.cs


gm.AddItem(new GUIContent("Vector3"), false, () => AddProperty(new Vector3ShaderProperty()));
gm.AddItem(new GUIContent("Vector4"), false, () => AddProperty(new Vector4ShaderProperty()));
gm.AddItem(new GUIContent("Color"), false, () => AddProperty(new ColorShaderProperty()));
gm.AddItem(new GUIContent("Boolean"), false, () => AddProperty(new BooleanShaderProperty()));
gm.AddItem(new GUIContent("Texture"), false, () => AddProperty(new TextureShaderProperty()));
gm.AddItem(new GUIContent("Cubemap"), false, () => AddProperty(new CubemapShaderProperty()));
gm.ShowAsContext();

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs


case ConcreteSlotValueType.Vector1:
prop = new FloatShaderProperty();
break;
case ConcreteSlotValueType.Boolean:
prop = new BooleanShaderProperty();
break;
default:
throw new ArgumentOutOfRangeException();
}

91
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/BooleanMaterialSlot.cs


using System;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing.Slots;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class BooleanMaterialSlot : MaterialSlot, IMaterialSlotHasValue<bool>
{
[SerializeField]
private bool m_Value;
[SerializeField]
private bool m_DefaultValue;
public BooleanMaterialSlot()
{}
public BooleanMaterialSlot(
int slotId,
string displayName,
string shaderOutputName,
SlotType slotType,
bool value,
ShaderStage shaderStage = ShaderStage.Dynamic,
bool hidden = false)
: base(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden)
{
m_DefaultValue = value;
m_Value = value;
}
public bool defaultValue { get { return m_DefaultValue; } }
public bool value
{
get { return m_Value; }
set { m_Value = value; }
}
/*public override VisualElement InstantiateControl()
{
return new MultiFloatSlotControlView(owner, 1, () => new Vector4(value, 0f, 0f, 0f), (newValue) => value = newValue.x);
}*/
protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return value.ToString();
}
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)));
var property = new BooleanShaderProperty()
{
overrideReferenceName = matOwner.GetVariableNameForSlot(id),
generatePropertyBlock = false,
value = value
};
properties.AddShaderProperty(property);
}
public override SlotValueType valueType { get { return SlotValueType.Boolean; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Boolean; } }
public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty(PropertyType.Boolean)
{
name = name,
booleanValue = value
};
return pp;
}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
var slot = foundSlot as BooleanMaterialSlot;
if (slot != null)
value = slot.value;
}
}
}

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


fileFormatVersion: 2
guid: efad93f5b8245a54fb4fee128fc0f3f6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

58
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/BooleanShaderProperty.cs


using System;
using System.Text;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing.Controls;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class BooleanShaderProperty : AbstractShaderProperty<bool>
{
public BooleanShaderProperty()
{
displayName = "Bool";
}
public override PropertyType propertyType
{
get { return PropertyType.Boolean; }
}
public override Vector4 defaultValue
{
get { return new Vector4(); }
}
public override string GetPropertyBlockString()
{
var result = new StringBuilder();
result.Append("[Toggle] ");
result.Append(referenceName);
result.Append("(\"");
result.Append(displayName);
result.Append("\", Float) = ");
result.Append(value == true ? 1 : 0);
return result.ToString();
}
public override string GetPropertyDeclarationString(string delimiter = ";")
{
return string.Format("float {0}{1}", referenceName, delimiter);
}
public override PreviewProperty GetPreviewMaterialProperty()
{
return new PreviewProperty(PropertyType.Boolean)
{
name = referenceName,
booleanValue = value
};
}
public override INode ToConcreteNode()
{
return new BooleanNode { value = new Toggle(value) };
}
}
}

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


fileFormatVersion: 2
guid: 8891f9466e6b01c498c3f4b2c6abc0eb
timeCreated: 1505346922

84
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/BooleanNode.cs


using System.Collections.Generic;
using UnityEditor.ShaderGraph.Drawing.Controls;
using UnityEngine;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
[Title("Input", "Basic", "Boolean")]
public class BooleanNode : AbstractMaterialNode, IGeneratesBodyCode, IPropertyFromNode
{
[SerializeField]
private bool m_Value;
public const int OutputSlotId = 0;
private const string kOutputSlotName = "Out";
public BooleanNode()
{
name = "Boolean";
UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new BooleanMaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, false));
RemoveSlotsNameNotMatching(new[] { OutputSlotId });
}
[ToggleControl("")]
public Toggle value
{
get { return new Toggle(m_Value); }
set
{
if (m_Value == value.isOn)
return;
m_Value = value.isOn;
Dirty(ModificationScope.Node);
}
}
public override void CollectShaderProperties(PropertyCollector properties, GenerationMode generationMode)
{
if (!generationMode.IsPreview())
return;
properties.AddShaderProperty(new BooleanShaderProperty()
{
overrideReferenceName = GetVariableNameForNode(),
generatePropertyBlock = false,
value = m_Value
});
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
if (generationMode.IsPreview())
return;
visitor.AddShaderChunk(precision + " " + GetVariableNameForNode() + " = " + (m_Value ? 1 : 0) + ";", true);
}
public override string GetVariableNameForSlot(int slotId)
{
return GetVariableNameForNode();
}
public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
{
properties.Add(new PreviewProperty(PropertyType.Boolean)
{
name = GetVariableNameForNode(),
booleanValue = m_Value
});
}
public IShaderProperty AsShaderProperty()
{
return new BooleanShaderProperty { value = m_Value };
}
public int outputSlotId { get { return OutputSlotId; } }
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/BooleanNode.cs.meta


fileFormatVersion: 2
guid: a6bd4d4655c0b9046a14e6ab0caebdc3
timeCreated: 1445864587
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存