using System; using System.Linq; using System.Collections.Generic; using UnityEditor.Graphing; using UnityEditor.ShaderGraph.Drawing.Controls; using UnityEngine; namespace UnityEditor.ShaderGraph { [Serializable] [Title("Master", "PBR")] public class PBRMasterNode : MasterNode, IMayRequireNormal { public const string AlbedoSlotName = "Albedo"; public const string NormalSlotName = "Normal"; public const string EmissionSlotName = "Emission"; public const string MetallicSlotName = "Metallic"; public const string SpecularSlotName = "Specular"; public const string SmoothnessSlotName = "Smoothness"; public const string OcclusionSlotName = "Occlusion"; public const string AlphaSlotName = "Alpha"; public const string AlphaClipThresholdSlotName = "AlphaClipThreshold"; public const string VertexOffsetName = "VertexPosition"; public const int AlbedoSlotId = 0; public const int NormalSlotId = 1; public const int MetallicSlotId = 2; public const int SpecularSlotId = 3; public const int EmissionSlotId = 4; public const int SmoothnessSlotId = 5; public const int OcclusionSlotId = 6; public const int AlphaSlotId = 7; public const int AlphaThresholdSlotId = 8; public enum Model { Specular, Metallic } [SerializeField] private Model m_Model = Model.Metallic; [EnumControl("Workflow")] public Model model { get { return m_Model; } set { if (m_Model == value) return; m_Model = value; UpdateNodeAfterDeserialization(); Dirty(ModificationScope.Topological); } } [SerializeField] private SurfaceType m_SurfaceType; [EnumControl("Surface")] public SurfaceType surfaceType { get { return m_SurfaceType; } set { if (m_SurfaceType == value) return; m_SurfaceType = value; Dirty(ModificationScope.Graph); } } [SerializeField] private AlphaMode m_AlphaMode; [EnumControl("Blend")] public AlphaMode alphaMode { get { return m_AlphaMode; } set { if (m_AlphaMode == value) return; m_AlphaMode = value; Dirty(ModificationScope.Graph); } } [SerializeField] private bool m_TwoSided; [ToggleControl("Two Sided")] public Toggle twoSided { get { return new Toggle(m_TwoSided); } set { if (m_TwoSided == value.isOn) return; m_TwoSided = value.isOn; Dirty(ModificationScope.Graph); } } public PBRMasterNode() { UpdateNodeAfterDeserialization(); } public override string documentationURL { get { return "https://github.com/Unity-Technologies/ShaderGraph/wiki/PBR-Master-Node"; } } public sealed override void UpdateNodeAfterDeserialization() { base.UpdateNodeAfterDeserialization(); name = "PBR Master"; AddSlot(new ColorRGBMaterialSlot(AlbedoSlotId, AlbedoSlotName, AlbedoSlotName, SlotType.Input, Color.grey, ShaderStage.Fragment)); AddSlot(new NormalMaterialSlot(NormalSlotId, NormalSlotName, NormalSlotName, CoordinateSpace.Tangent, ShaderStage.Fragment)); AddSlot(new ColorRGBMaterialSlot(EmissionSlotId, EmissionSlotName, EmissionSlotName, SlotType.Input, Color.black, ShaderStage.Fragment)); if (model == Model.Metallic) AddSlot(new Vector1MaterialSlot(MetallicSlotId, MetallicSlotName, MetallicSlotName, SlotType.Input, 0, ShaderStage.Fragment)); else AddSlot(new ColorRGBMaterialSlot(SpecularSlotId, SpecularSlotName, SpecularSlotName, SlotType.Input, Color.grey, ShaderStage.Fragment)); AddSlot(new Vector1MaterialSlot(SmoothnessSlotId, SmoothnessSlotName, SmoothnessSlotName, SlotType.Input, 0.5f, ShaderStage.Fragment)); AddSlot(new Vector1MaterialSlot(OcclusionSlotId, OcclusionSlotName, OcclusionSlotName, SlotType.Input, 1f, ShaderStage.Fragment)); AddSlot(new Vector1MaterialSlot(AlphaSlotId, AlphaSlotName, AlphaSlotName, SlotType.Input, 1f, ShaderStage.Fragment)); AddSlot(new Vector1MaterialSlot(AlphaThresholdSlotId, AlphaClipThresholdSlotName, AlphaClipThresholdSlotName, SlotType.Input, 0f, ShaderStage.Fragment)); // clear out slot names that do not match the slots // we support RemoveSlotsNameNotMatching( new[] { AlbedoSlotId, NormalSlotId, EmissionSlotId, model == Model.Metallic ? MetallicSlotId : SpecularSlotId, SmoothnessSlotId, OcclusionSlotId, AlphaSlotId, AlphaThresholdSlotId }, true); } public NeededCoordinateSpace RequiresNormal() { List slots = new List(); GetSlots(slots); return slots.OfType().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresNormal()); } } }