123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Linq;
- using UnityEditor.Graphing;
- using UnityEngine;
- namespace UnityEditor.ShaderGraph.Internal
- {
- [Serializable]
- [FormerName("UnityEditor.ShaderGraph.FloatShaderProperty")]
- [FormerName("UnityEditor.ShaderGraph.Vector1ShaderProperty")]
- public sealed class Vector1ShaderProperty : AbstractShaderProperty<float>
- {
- internal Vector1ShaderProperty()
- {
- displayName = "Vector1";
- }
-
- public override PropertyType propertyType => PropertyType.Vector1;
-
- internal override bool isBatchable => true;
- internal override bool isExposable => true;
- internal override bool isRenamable => true;
- internal override bool isGpuInstanceable => true;
-
- string enumTagString
- {
- get
- {
- switch(enumType)
- {
- case EnumType.CSharpEnum:
- return $"[Enum({m_CSharpEnumType.ToString()})]";
- case EnumType.KeywordEnum:
- return $"[KeywordEnum({string.Join(", ", enumNames)})]";
- default:
- string enumValuesString = "";
- for (int i = 0; i < enumNames.Count; i++)
- {
- int value = (i < enumValues.Count) ? enumValues[i] : i;
- enumValuesString += (enumNames[i] + ", " + value + ((i != enumNames.Count - 1) ? ", " : ""));
- }
- return $"[Enum({enumValuesString})]";
- }
- }
- }
- internal override string GetPropertyBlockString()
- {
- switch(floatType)
- {
- case FloatType.Slider:
- return $"{hideTagString}{referenceName}(\"{displayName}\", Range({NodeUtils.FloatToShaderValue(m_RangeValues.x)}, {NodeUtils.FloatToShaderValue(m_RangeValues.y)})) = {NodeUtils.FloatToShaderValue(value)}";
- case FloatType.Integer:
- return $"{hideTagString}{referenceName}(\"{displayName}\", Int) = {NodeUtils.FloatToShaderValue(value)}";
- case FloatType.Enum:
- return $"{hideTagString}{enumTagString}{referenceName}(\"{displayName}\", Float) = {NodeUtils.FloatToShaderValue(value)}";
- default:
- return $"{hideTagString}{referenceName}(\"{displayName}\", Float) = {NodeUtils.FloatToShaderValue(value)}";
- }
- }
-
- [SerializeField]
- FloatType m_FloatType = FloatType.Default;
- public FloatType floatType
- {
- get => m_FloatType;
- set => m_FloatType = value;
- }
- [SerializeField]
- Vector2 m_RangeValues = new Vector2(0, 1);
- public Vector2 rangeValues
- {
- get => m_RangeValues;
- set => m_RangeValues = value;
- }
- EnumType m_EnumType = EnumType.Enum;
- public EnumType enumType
- {
- get => m_EnumType;
- set => m_EnumType = value;
- }
-
- Type m_CSharpEnumType;
- public Type cSharpEnumType
- {
- get => m_CSharpEnumType;
- set => m_CSharpEnumType = value;
- }
- List<string> m_EnumNames = new List<string>();
-
- public List<string> enumNames
- {
- get => m_EnumNames;
- set => m_EnumNames = value;
- }
- List<int> m_EnumValues = new List<int>();
- public List<int> enumValues
- {
- get => m_EnumValues;
- set => m_EnumValues = value;
- }
-
- internal override AbstractMaterialNode ToConcreteNode()
- {
- switch (m_FloatType)
- {
- case FloatType.Slider:
- return new SliderNode { value = new Vector3(value, m_RangeValues.x, m_RangeValues.y) };
- case FloatType.Integer:
- return new IntegerNode { value = (int)value };
- default:
- var node = new Vector1Node();
- node.FindInputSlot<Vector1MaterialSlot>(Vector1Node.InputSlotXId).value = value;
- return node;
- }
- }
- internal override PreviewProperty GetPreviewMaterialProperty()
- {
- return new PreviewProperty(propertyType)
- {
- name = referenceName,
- floatValue = value
- };
- }
- internal override ShaderInput Copy()
- {
- return new Vector1ShaderProperty()
- {
- displayName = displayName,
- hidden = hidden,
- value = value,
- floatType = floatType,
- rangeValues = rangeValues,
- enumType = enumType,
- enumNames = enumNames,
- enumValues = enumValues
- };
- }
- }
- public enum FloatType { Default, Slider, Integer, Enum }
- public enum EnumType { Enum, CSharpEnum, KeywordEnum, }
- }
|