|
|
|
|
|
|
using System.Collections.Generic; |
|
|
|
using UnityEditor.MaterialGraph.Drawing.Controls; |
|
|
|
using UnityEngine.Graphing; |
|
|
|
|
|
|
|
namespace UnityEngine.MaterialGraph |
|
|
|
{ |
|
|
|
public enum ConstantType |
|
|
|
{ |
|
|
|
PI, |
|
|
|
TAU, |
|
|
|
PHI, |
|
|
|
E, |
|
|
|
SQRT2 |
|
|
|
}; |
|
|
|
|
|
|
|
[Title("Input/Basic/Constant")] |
|
|
|
public class ConstantNode : AbstractMaterialNode, IGeneratesBodyCode |
|
|
|
{ |
|
|
|
static Dictionary<ConstantType, float> m_constantList = new Dictionary<ConstantType, float> |
|
|
|
{ |
|
|
|
{ConstantType.PI, 3.1415926f }, |
|
|
|
{ConstantType.TAU, 6.28318530f}, |
|
|
|
{ConstantType.PHI, 1.618034f}, |
|
|
|
{ConstantType.E, 2.718282f}, |
|
|
|
{ConstantType.SQRT2, 1.414214f}, |
|
|
|
}; |
|
|
|
|
|
|
|
[SerializeField] |
|
|
|
private ConstantType m_constant = ConstantType.PI; |
|
|
|
|
|
|
|
private const int kOutputSlotId = 0; |
|
|
|
private const string kOutputSlotName = "Out"; |
|
|
|
|
|
|
|
[EnumControl("")] |
|
|
|
public ConstantType constant |
|
|
|
{ |
|
|
|
get { return m_constant; } |
|
|
|
set |
|
|
|
{ |
|
|
|
if (m_constant == value) |
|
|
|
return; |
|
|
|
|
|
|
|
m_constant = value; |
|
|
|
if (onModified != null) |
|
|
|
{ |
|
|
|
onModified(this, ModificationScope.Graph); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public ConstantNode() |
|
|
|
{ |
|
|
|
name = "Constant"; |
|
|
|
UpdateNodeAfterDeserialization(); |
|
|
|
} |
|
|
|
|
|
|
|
public sealed override void UpdateNodeAfterDeserialization() |
|
|
|
{ |
|
|
|
AddSlot(new MaterialSlot(kOutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, SlotValueType.Vector1, Vector4.zero)); |
|
|
|
RemoveSlotsNameNotMatching(new[] { kOutputSlotId }); |
|
|
|
} |
|
|
|
|
|
|
|
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode) |
|
|
|
{ |
|
|
|
visitor.AddShaderChunk(precision + " " + GetVariableNameForNode() + " = " + m_constantList[constant] + ";", true); |
|
|
|
} |
|
|
|
|
|
|
|
public override string GetVariableNameForSlot(int slotId) |
|
|
|
{ |
|
|
|
return GetVariableNameForNode(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |