GitHub
7 年前
当前提交
758a90e4
共有 16 个文件被更改,包括 746 次插入 和 0 次删除
-
3MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/ScreenPositionMaterialSlot.cs
-
55MaterialGraphProject/Assets/UnityShaderEditor/Editor/Resources/Styles/MaterialGraph.uss
-
8MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Filter.meta
-
8MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/PBR.meta
-
123MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/UV/TriplanarNode.cs
-
12MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/UV/TriplanarNode.cs.meta
-
219MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/DielectricSpecularControl.cs
-
3MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/DielectricSpecularControl.cs.meta
-
39MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Filter/DitherNode.cs
-
11MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Filter/DitherNode.cs.meta
-
8MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/PBR/MetalReflectanceNode.cs.meta
-
8MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/PBR/DielectricSpecularNode.cs.meta
-
166MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/PBR/DielectricSpecularNode.cs
-
83MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/PBR/MetalReflectanceNode.cs
|
|||
fileFormatVersion: 2 |
|||
guid: e005d19f5315e44bc9f50b3f6f8e50b2 |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: bc2d9fef9660c574dad26112f3061731 |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System.Linq; |
|||
using UnityEngine; |
|||
using UnityEditor.Graphing; |
|||
using UnityEditor.ShaderGraph.Drawing.Controls; |
|||
|
|||
namespace UnityEditor.ShaderGraph |
|||
{ |
|||
[Title("UV", "Triplanar")] |
|||
public class TriplanarNode : AbstractMaterialNode, IGeneratesBodyCode, IMayRequirePosition, IMayRequireNormal |
|||
{ |
|||
public const int OutputSlotId = 0; |
|||
public const int TextureInputId = 1; |
|||
public const int SamplerInputId = 2; |
|||
public const int PositionInputId = 3; |
|||
public const int NormalInputId = 4; |
|||
public const int TileInputId = 5; |
|||
public const int BlendInputId = 6; |
|||
const string kOutputSlotName = "Out"; |
|||
const string kTextureInputName = "Texture"; |
|||
const string kSamplerInputName = "Sampler"; |
|||
const string kPositionInputName = "Position"; |
|||
const string kNormalInputName = "Normal"; |
|||
const string kTileInputName = "Tile"; |
|||
const string kBlendInputName = "Blend"; |
|||
|
|||
public override bool hasPreview { get { return true; } } |
|||
public override PreviewMode previewMode |
|||
{ |
|||
get { return PreviewMode.Preview3D; } |
|||
} |
|||
|
|||
public TriplanarNode() |
|||
{ |
|||
name = "Triplanar"; |
|||
UpdateNodeAfterDeserialization(); |
|||
} |
|||
|
|||
[SerializeField] |
|||
private TextureType m_TextureType = TextureType.Default; |
|||
|
|||
[EnumControl("Type")] |
|||
public TextureType textureType |
|||
{ |
|||
get { return m_TextureType; } |
|||
set |
|||
{ |
|||
if (m_TextureType == value) |
|||
return; |
|||
|
|||
m_TextureType = value; |
|||
Dirty(ModificationScope.Graph); |
|||
} |
|||
} |
|||
|
|||
public sealed override void UpdateNodeAfterDeserialization() |
|||
{ |
|||
AddSlot(new Vector4MaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, Vector4.zero)); |
|||
AddSlot(new Texture2DInputMaterialSlot(TextureInputId, kTextureInputName, kTextureInputName)); |
|||
AddSlot(new SamplerStateMaterialSlot(SamplerInputId, kSamplerInputName, kSamplerInputName, SlotType.Input)); |
|||
AddSlot(new PositionMaterialSlot(PositionInputId, kPositionInputName, kPositionInputName, CoordinateSpace.World)); |
|||
AddSlot(new NormalMaterialSlot(NormalInputId, kNormalInputName, kNormalInputName, CoordinateSpace.World)); |
|||
AddSlot(new Vector1MaterialSlot(TileInputId, kTileInputName, kTileInputName, SlotType.Input, 1)); |
|||
AddSlot(new Vector1MaterialSlot(BlendInputId, kBlendInputName, kBlendInputName, SlotType.Input, 1)); |
|||
RemoveSlotsNameNotMatching(new[] { OutputSlotId, TextureInputId, SamplerInputId, PositionInputId, NormalInputId, TileInputId, BlendInputId }); |
|||
} |
|||
|
|||
// Node generations
|
|||
public virtual void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode) |
|||
{ |
|||
var sb = new ShaderStringBuilder(); |
|||
sb.AppendLine("{0}3 {1}_UV = {2} * {3};", precision, GetVariableNameForNode(), |
|||
GetSlotValue(PositionInputId, generationMode), GetSlotValue(TileInputId, generationMode)); |
|||
|
|||
sb.AppendLine("{0}3 {1}_Blend = pow(abs({2}), {3});", precision, GetVariableNameForNode(), |
|||
GetSlotValue(NormalInputId, generationMode), GetSlotValue(BlendInputId, generationMode)); |
|||
|
|||
sb.AppendLine("{0}_Blend /= dot({0}_Blend, 1.0);", GetVariableNameForNode()); |
|||
|
|||
//Sampler input slot
|
|||
var samplerSlot = FindInputSlot<MaterialSlot>(SamplerInputId); |
|||
var edgesSampler = owner.GetEdges(samplerSlot.slotReference); |
|||
|
|||
var id = GetSlotValue(TextureInputId, generationMode); |
|||
sb.AppendLine("{0}4 {1}_X = SAMPLE_TEXTURE2D({2}, {3}, {1}_UV.yz);" |
|||
, precision |
|||
, GetVariableNameForNode() |
|||
, id |
|||
, edgesSampler.Any() ? GetSlotValue(SamplerInputId, generationMode) : "sampler" + id); |
|||
|
|||
sb.AppendLine("{0}4 {1}_Y = SAMPLE_TEXTURE2D({2}, {3}, {1}_UV.xz);" |
|||
, precision |
|||
, GetVariableNameForNode() |
|||
, id |
|||
, edgesSampler.Any() ? GetSlotValue(SamplerInputId, generationMode) : "sampler" + id); |
|||
|
|||
sb.AppendLine("{0}4 {1}_Z = SAMPLE_TEXTURE2D({2}, {3}, {1}_UV.xy);" |
|||
, precision |
|||
, GetVariableNameForNode() |
|||
, id |
|||
, edgesSampler.Any() ? GetSlotValue(SamplerInputId, generationMode) : "sampler" + id); |
|||
|
|||
sb.AppendLine("{0}4 {1} = {2}_X * {2}_Blend.x + {2}_Y * {2}_Blend.y + {2}_Z * {2}_Blend.z;" |
|||
, precision |
|||
, GetVariableNameForSlot(OutputSlotId) |
|||
, GetVariableNameForNode()); |
|||
|
|||
if (textureType == TextureType.Normal) |
|||
sb.AppendLine("{0}.rgb = UnpackNormalmapRGorAG({0});", GetVariableNameForSlot(OutputSlotId)); |
|||
|
|||
visitor.AddShaderChunk(sb.ToString(), false); |
|||
} |
|||
|
|||
public NeededCoordinateSpace RequiresPosition() |
|||
{ |
|||
return CoordinateSpace.World.ToNeededCoordinateSpace(); |
|||
} |
|||
|
|||
public NeededCoordinateSpace RequiresNormal() |
|||
{ |
|||
return CoordinateSpace.World.ToNeededCoordinateSpace(); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 2210eee393fa07449a1b51817fdcb679 |
|||
timeCreated: 1495637741 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Reflection; |
|||
using UnityEditor.Experimental.UIElements; |
|||
using UnityEngine; |
|||
using UnityEngine.Experimental.UIElements; |
|||
|
|||
namespace UnityEditor.ShaderGraph.Drawing.Controls |
|||
{ |
|||
[AttributeUsage(AttributeTargets.Property)] |
|||
public class DielectricSpecularControlAttribute : Attribute, IControlAttribute |
|||
{ |
|||
public DielectricSpecularControlAttribute() |
|||
{ |
|||
} |
|||
|
|||
public VisualElement InstantiateControl(AbstractMaterialNode node, PropertyInfo propertyInfo) |
|||
{ |
|||
return new DielectricSpecularControlView(node, propertyInfo); |
|||
} |
|||
} |
|||
|
|||
public class DielectricSpecularControlView : VisualElement |
|||
{ |
|||
AbstractMaterialNode m_Node; |
|||
PropertyInfo m_PropertyInfo; |
|||
DielectricSpecularNode.DielectricMaterial m_DielectricMaterial; |
|||
|
|||
VisualElement m_RangePanel; |
|||
Slider m_RangeSlider; |
|||
FloatField m_RangeField; |
|||
VisualElement m_IORPanel; |
|||
Slider m_IORSlider; |
|||
FloatField m_IORField; |
|||
|
|||
int m_UndoGroup = -1; |
|||
|
|||
public DielectricSpecularControlView(AbstractMaterialNode node, PropertyInfo propertyInfo) |
|||
{ |
|||
m_Node = node; |
|||
m_PropertyInfo = propertyInfo; |
|||
m_DielectricMaterial = (DielectricSpecularNode.DielectricMaterial)m_PropertyInfo.GetValue(m_Node, null); |
|||
|
|||
if (propertyInfo.PropertyType != typeof(DielectricSpecularNode.DielectricMaterial)) |
|||
throw new ArgumentException("Property must be of type DielectricMaterial.", "propertyInfo"); |
|||
|
|||
var enumPanel = new VisualElement { name = "enumPanel" }; |
|||
enumPanel.Add(new Label("Material")); |
|||
var enumField = new EnumField(m_DielectricMaterial.type); |
|||
enumField.OnValueChanged(OnEnumChanged); |
|||
enumPanel.Add(enumField); |
|||
Add(enumPanel); |
|||
|
|||
m_RangePanel = new VisualElement { name = "sliderPanel" }; |
|||
m_RangePanel.Add(new Label("Range")); |
|||
Action<float> changedRangeSlider = (s) => { OnChangeRangeSlider(s); }; |
|||
m_RangeSlider = new Slider(0.01f, 1, changedRangeSlider); |
|||
m_RangeSlider.value = 0.5f; |
|||
m_RangePanel.Add(m_RangeSlider); |
|||
m_RangeField = AddField(m_RangePanel, m_RangeSlider, 0, m_DielectricMaterial); |
|||
m_RangePanel.SetEnabled(true); |
|||
Add(m_RangePanel); |
|||
|
|||
m_IORPanel = new VisualElement { name = "sliderPanel" }; |
|||
m_IORPanel.Add(new Label("IOR")); |
|||
Action<float> changedIORSlider = (s) => { OnChangeIORSlider(s); }; |
|||
m_IORSlider = new Slider(1, 5, changedIORSlider); |
|||
m_IORSlider.value = 1; |
|||
m_IORPanel.Add(m_IORSlider); |
|||
m_IORField = AddField(m_IORPanel, m_IORSlider, 1, m_DielectricMaterial); |
|||
m_IORPanel.SetEnabled(false); |
|||
Add(m_IORPanel); |
|||
} |
|||
|
|||
void OnEnumChanged(ChangeEvent<Enum> evt) |
|||
{ |
|||
if (!evt.newValue.Equals(m_DielectricMaterial.type)) |
|||
{ |
|||
m_Node.owner.owner.RegisterCompleteObjectUndo("Change " + m_Node.name); |
|||
m_DielectricMaterial.type = (DielectricMaterialType)evt.newValue; |
|||
m_PropertyInfo.SetValue(m_Node, m_DielectricMaterial, null); |
|||
|
|||
switch(m_DielectricMaterial.type) |
|||
{ |
|||
case DielectricMaterialType.Common: |
|||
m_RangePanel.SetEnabled(true); |
|||
m_IORPanel.SetEnabled(false); |
|||
break; |
|||
case DielectricMaterialType.Custom: |
|||
m_RangePanel.SetEnabled(false); |
|||
m_IORPanel.SetEnabled(true); |
|||
break; |
|||
default: |
|||
m_RangePanel.SetEnabled(false); |
|||
m_IORPanel.SetEnabled(false); |
|||
break; |
|||
} |
|||
} |
|||
} |
|||
|
|||
void OnChangeRangeSlider(float newValue) |
|||
{ |
|||
m_Node.owner.owner.RegisterCompleteObjectUndo("Slider Change"); |
|||
m_DielectricMaterial.range = newValue; |
|||
m_PropertyInfo.SetValue(m_Node, m_DielectricMaterial, null); |
|||
if(m_RangeField != null) |
|||
m_RangeField.value = newValue; |
|||
Dirty(ChangeType.Repaint); |
|||
} |
|||
|
|||
void OnChangeIORSlider(float newValue) |
|||
{ |
|||
m_Node.owner.owner.RegisterCompleteObjectUndo("Slider Change"); |
|||
m_DielectricMaterial.indexOfRefraction = newValue; |
|||
m_PropertyInfo.SetValue(m_Node, m_DielectricMaterial, null); |
|||
if(m_IORField != null) |
|||
m_IORField.value = newValue; |
|||
Dirty(ChangeType.Repaint); |
|||
} |
|||
|
|||
FloatField AddField(VisualElement panel, Slider slider, int index, DielectricSpecularNode.DielectricMaterial initMaterial) |
|||
{ |
|||
float initValue; |
|||
if(index == 1) |
|||
initValue = initMaterial.indexOfRefraction; |
|||
else |
|||
initValue = initMaterial.range; |
|||
|
|||
var field = new FloatField { userData = index, value = initValue }; |
|||
|
|||
field.RegisterCallback<MouseDownEvent>(Repaint); |
|||
field.RegisterCallback<MouseMoveEvent>(Repaint); |
|||
field.OnValueChanged(evt => |
|||
{ |
|||
var value = (DielectricSpecularNode.DielectricMaterial)m_PropertyInfo.GetValue(m_Node, null); |
|||
var fieldValue = (float)evt.newValue; |
|||
if(index == 1) |
|||
{ |
|||
value.indexOfRefraction = fieldValue; |
|||
RedrawIORControls(fieldValue); |
|||
} |
|||
else |
|||
{ |
|||
value.range = fieldValue; |
|||
RedrawRangeControls(fieldValue); |
|||
} |
|||
m_PropertyInfo.SetValue(m_Node, value, null); |
|||
m_UndoGroup = -1; |
|||
Dirty(ChangeType.Repaint); |
|||
}); |
|||
field.RegisterCallback<InputEvent>(evt => |
|||
{ |
|||
if (m_UndoGroup == -1) |
|||
{ |
|||
m_UndoGroup = Undo.GetCurrentGroup(); |
|||
m_Node.owner.owner.RegisterCompleteObjectUndo("Change " + m_Node.name); |
|||
} |
|||
float newValue; |
|||
if (!float.TryParse(evt.newData, out newValue)) |
|||
newValue = 0f; |
|||
var value = (DielectricSpecularNode.DielectricMaterial)m_PropertyInfo.GetValue(m_Node, null); |
|||
if(index == 1) |
|||
value.indexOfRefraction = newValue; |
|||
else |
|||
value.range = newValue; |
|||
m_PropertyInfo.SetValue(m_Node, value, null); |
|||
Dirty(ChangeType.Repaint); |
|||
}); |
|||
field.RegisterCallback<KeyDownEvent>(evt => |
|||
{ |
|||
if (evt.keyCode == KeyCode.Escape && m_UndoGroup > -1) |
|||
{ |
|||
Undo.RevertAllDownToGroup(m_UndoGroup); |
|||
m_UndoGroup = -1; |
|||
var value = (DielectricSpecularNode.DielectricMaterial)m_PropertyInfo.GetValue(m_Node, null); |
|||
if(index == 1) |
|||
RedrawIORControls(value.indexOfRefraction); |
|||
else |
|||
RedrawRangeControls(value.range); |
|||
evt.StopPropagation(); |
|||
} |
|||
Dirty(ChangeType.Repaint); |
|||
}); |
|||
panel.Add(field); |
|||
return field; |
|||
} |
|||
|
|||
void RedrawRangeControls(float value) |
|||
{ |
|||
value = Mathf.Max(Mathf.Min(value, 1), 0.01f); |
|||
m_RangePanel.Remove(m_RangeSlider); |
|||
Action<float> changedSlider = (s) => { OnChangeRangeSlider(s); }; |
|||
m_RangeSlider = new Slider(0.01f, 1, changedSlider); |
|||
m_RangeSlider.value = value; |
|||
m_RangePanel.Add(m_RangeSlider); |
|||
m_RangePanel.Remove(m_RangeField); |
|||
m_RangeField.value = value; |
|||
m_RangePanel.Add(m_RangeField); |
|||
} |
|||
|
|||
void RedrawIORControls(float value) |
|||
{ |
|||
value = Mathf.Max(Mathf.Min(value, 5), 1); |
|||
m_IORPanel.Remove(m_IORSlider); |
|||
Action<float> changedSlider = (s) => { OnChangeIORSlider(s); }; |
|||
m_IORSlider = new Slider(1, 5, changedSlider); |
|||
m_IORSlider.value = value; |
|||
m_IORPanel.Add(m_IORSlider); |
|||
m_IORPanel.Remove(m_IORField); |
|||
m_IORField.value = value; |
|||
m_IORPanel.Add(m_IORField); |
|||
} |
|||
|
|||
void Repaint<T>(MouseEventBase<T> evt) where T : MouseEventBase<T>, new() |
|||
{ |
|||
evt.StopPropagation(); |
|||
Dirty(ChangeType.Repaint); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 20b1492eed8154d8f9a454e3563d9c01 |
|||
timeCreated: 1507817885 |
|
|||
using System.Reflection; |
|||
using UnityEngine; |
|||
using UnityEditor.Graphing; |
|||
namespace UnityEditor.ShaderGraph |
|||
{ |
|||
[Title("Artistic", "Filter", "Dither")] |
|||
public class DitherNode : CodeFunctionNode |
|||
{ |
|||
public DitherNode() |
|||
{ |
|||
name = "Dither"; |
|||
UpdateNodeAfterDeserialization(); |
|||
} |
|||
protected override MethodInfo GetFunctionToConvert() |
|||
{ |
|||
return GetType().GetMethod("Unity_Dither", BindingFlags.Static | BindingFlags.NonPublic); |
|||
} |
|||
static string Unity_Dither( |
|||
[Slot(0, Binding.None)] DynamicDimensionVector In, |
|||
[Slot(1, Binding.ScreenPosition)] Vector2 UV, |
|||
[Slot(2, Binding.None)] out DynamicDimensionVector Out) |
|||
{ |
|||
return |
|||
@"
|
|||
{ |
|||
{precision}2 uv = (UV.xy / UV.w) * _ScreenParams.xy; |
|||
{precision} DITHER_THRESHOLDS[16] = |
|||
{ |
|||
1.0 / 17.0, 9.0 / 17.0, 3.0 / 17.0, 11.0 / 17.0, |
|||
13.0 / 17.0, 5.0 / 17.0, 15.0 / 17.0, 7.0 / 17.0, |
|||
4.0 / 17.0, 12.0 / 17.0, 2.0 / 17.0, 10.0 / 17.0, |
|||
16.0 / 17.0, 8.0 / 17.0, 14.0 / 17.0, 6.0 / 17.0 |
|||
}; |
|||
uint index = (uint(uv.x) % 4) * 4 + uint(uv.y) % 4; |
|||
Out = In - DITHER_THRESHOLDS[index]; |
|||
}";
|
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 40b83fe632985494f96d2211c1963835 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: ba3db6b7663a26244a3efba1203bad6e |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|
|||
fileFormatVersion: 2 |
|||
guid: f33e635e375c6b841bf3452a8da24be6 |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using UnityEditor.Graphing; |
|||
using UnityEditor.ShaderGraph.Drawing.Controls; |
|||
using UnityEngine; |
|||
|
|||
namespace UnityEditor.ShaderGraph |
|||
{ |
|||
public enum DielectricMaterialType |
|||
{ |
|||
Common, |
|||
RustedMetal, |
|||
Water, |
|||
Ice, |
|||
Glass, |
|||
Custom |
|||
}; |
|||
|
|||
[Title("Input", "PBR", "Dielectric Specular")] |
|||
public class DielectricSpecularNode : AbstractMaterialNode, IGeneratesBodyCode |
|||
{ |
|||
public DielectricSpecularNode() |
|||
{ |
|||
name = "Dielectric Specular"; |
|||
UpdateNodeAfterDeserialization(); |
|||
} |
|||
|
|||
[SerializeField] |
|||
DielectricMaterial m_Material = new DielectricMaterial(DielectricMaterialType.Common, 0.5f, 1.0f); |
|||
|
|||
[Serializable] |
|||
public struct DielectricMaterial |
|||
{ |
|||
public DielectricMaterialType type; |
|||
public float range; |
|||
public float indexOfRefraction; |
|||
|
|||
public DielectricMaterial(DielectricMaterialType type, float range, float indexOfRefraction) |
|||
{ |
|||
this.type = type; |
|||
this.range = range; |
|||
this.indexOfRefraction = indexOfRefraction; |
|||
} |
|||
} |
|||
|
|||
[DielectricSpecularControl()] |
|||
public DielectricMaterial material |
|||
{ |
|||
get { return m_Material; } |
|||
set |
|||
{ |
|||
if ((value.type == m_Material.type) && (value.range == m_Material.range) && (value.indexOfRefraction == m_Material.indexOfRefraction)) |
|||
return; |
|||
DielectricMaterialType previousType = m_Material.type; |
|||
m_Material = value; |
|||
if(value.type != previousType) |
|||
Dirty(ModificationScope.Graph); |
|||
else |
|||
Dirty(ModificationScope.Node); |
|||
} |
|||
} |
|||
|
|||
static Dictionary<DielectricMaterialType, string> m_MaterialList = new Dictionary<DielectricMaterialType, string> |
|||
{ |
|||
{DielectricMaterialType.RustedMetal, "(0.030, 0.030, 0.030)"}, |
|||
{DielectricMaterialType.Water, "(0.020, 0.020, 0.020)"}, |
|||
{DielectricMaterialType.Ice, "(0.018, 0.018, 0.018)"}, |
|||
{DielectricMaterialType.Glass, "(0.040, 0.040, 0.040)"} |
|||
}; |
|||
|
|||
private const int kOutputSlotId = 0; |
|||
private const string kOutputSlotName = "Out"; |
|||
|
|||
public override bool hasPreview { get { return true; } } |
|||
public override PreviewMode previewMode |
|||
{ |
|||
get { return PreviewMode.Preview2D; } |
|||
} |
|||
|
|||
|
|||
public sealed override void UpdateNodeAfterDeserialization() |
|||
{ |
|||
AddSlot(new Vector3MaterialSlot(kOutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, Vector3.zero)); |
|||
RemoveSlotsNameNotMatching(new[] { kOutputSlotId }); |
|||
} |
|||
|
|||
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode) |
|||
{ |
|||
var sb = new ShaderStringBuilder(); |
|||
if (!generationMode.IsPreview()) |
|||
{ |
|||
switch (material.type) |
|||
{ |
|||
case DielectricMaterialType.Custom: |
|||
sb.AppendLine("{0} _{1}_IOR = {2};", precision, GetVariableNameForNode(), material.indexOfRefraction); |
|||
break; |
|||
case DielectricMaterialType.Common: |
|||
sb.AppendLine("{0} _{1}_Range = {2};", precision, GetVariableNameForNode(), material.range); |
|||
break; |
|||
default: |
|||
break; |
|||
} |
|||
} |
|||
switch (material.type) |
|||
{ |
|||
case DielectricMaterialType.Common: |
|||
sb.AppendLine("{0}3 {1} = lerp(0.034, 0.048, _{2}_Range);", precision, GetVariableNameForSlot(kOutputSlotId), GetVariableNameForNode()); |
|||
break; |
|||
case DielectricMaterialType.Custom: |
|||
sb.AppendLine("{0}3 {1} = pow(_{2}_IOR - 1, 2) / pow(_{2}_IOR + 1, 2);", precision, GetVariableNameForSlot(kOutputSlotId), GetVariableNameForNode()); |
|||
break; |
|||
default: |
|||
sb.AppendLine("{0}3 {1} = {0}3{2};", precision, GetVariableNameForSlot(kOutputSlotId), m_MaterialList[material.type]); |
|||
break; |
|||
} |
|||
visitor.AddShaderChunk(sb.ToString(), false); |
|||
} |
|||
|
|||
public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties) |
|||
{ |
|||
base.CollectPreviewMaterialProperties(properties); |
|||
|
|||
if(material.type == DielectricMaterialType.Common) |
|||
{ |
|||
properties.Add(new PreviewProperty(PropertyType.Float) |
|||
{ |
|||
name = string.Format("_{0}_Range", GetVariableNameForNode()), |
|||
floatValue = material.range |
|||
}); |
|||
} |
|||
else if (material.type == DielectricMaterialType.Custom) |
|||
{ |
|||
properties.Add(new PreviewProperty(PropertyType.Float) |
|||
{ |
|||
name = string.Format("_{0}_IOR", GetVariableNameForNode()), |
|||
floatValue = material.indexOfRefraction |
|||
}); |
|||
} |
|||
} |
|||
|
|||
public override void CollectShaderProperties(PropertyCollector properties, GenerationMode generationMode) |
|||
{ |
|||
if (!generationMode.IsPreview()) |
|||
return; |
|||
|
|||
base.CollectShaderProperties(properties, generationMode); |
|||
|
|||
if (material.type == DielectricMaterialType.Common) |
|||
{ |
|||
properties.AddShaderProperty(new FloatShaderProperty() |
|||
{ |
|||
overrideReferenceName = string.Format("_{0}_Range", GetVariableNameForNode()), |
|||
generatePropertyBlock = false |
|||
}); |
|||
} |
|||
else if (material.type == DielectricMaterialType.Custom) |
|||
{ |
|||
properties.AddShaderProperty(new FloatShaderProperty() |
|||
{ |
|||
overrideReferenceName = string.Format("_{0}_IOR", GetVariableNameForNode()), |
|||
generatePropertyBlock = false |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
} |
|
|||
using System.Reflection; |
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
using UnityEditor.Graphing; |
|||
using UnityEditor.ShaderGraph.Drawing.Controls; |
|||
|
|||
namespace UnityEditor.ShaderGraph |
|||
{ |
|||
public enum MetalMaterialType |
|||
{ |
|||
Iron, |
|||
Silver, |
|||
Aluminium, |
|||
Gold, |
|||
Copper, |
|||
Chromium, |
|||
Nickel, |
|||
Titanium, |
|||
Cobalt, |
|||
Platinum |
|||
}; |
|||
|
|||
[Title("Input", "PBR", "Metal Reflectance")] |
|||
public class MetalReflectanceNode : AbstractMaterialNode, IGeneratesBodyCode |
|||
{ |
|||
public MetalReflectanceNode() |
|||
{ |
|||
name = "Metal Reflectance"; |
|||
UpdateNodeAfterDeserialization(); |
|||
} |
|||
|
|||
[SerializeField] |
|||
private MetalMaterialType m_Material = MetalMaterialType.Iron; |
|||
|
|||
[EnumControl("Material")] |
|||
public MetalMaterialType material |
|||
{ |
|||
get { return m_Material; } |
|||
set |
|||
{ |
|||
if (m_Material == value) |
|||
return; |
|||
|
|||
m_Material = value; |
|||
Dirty(ModificationScope.Graph); |
|||
} |
|||
} |
|||
|
|||
static Dictionary<MetalMaterialType, string> m_MaterialList = new Dictionary<MetalMaterialType, string> |
|||
{ |
|||
{MetalMaterialType.Iron, "(0.560, 0.570, 0.580)"}, |
|||
{MetalMaterialType.Silver, "(0.972, 0.960, 0.915)"}, |
|||
{MetalMaterialType.Aluminium, "(0.913, 0.921, 0.925)"}, |
|||
{MetalMaterialType.Gold, "(1.000, 0.766, 0.336)"}, |
|||
{MetalMaterialType.Copper, "(0.955, 0.637, 0.538)"}, |
|||
{MetalMaterialType.Chromium, "(0.550, 0.556, 0.554)"}, |
|||
{MetalMaterialType.Nickel, "(0.660, 0.609, 0.526)"}, |
|||
{MetalMaterialType.Titanium, "(0.542, 0.497, 0.449)"}, |
|||
{MetalMaterialType.Cobalt, "(0.662, 0.655, 0.634)"}, |
|||
{MetalMaterialType.Platinum, "(0.672, 0.637, 0.585)"} |
|||
}; |
|||
|
|||
private const int kOutputSlotId = 0; |
|||
private const string kOutputSlotName = "Out"; |
|||
|
|||
public override bool hasPreview { get { return true; } } |
|||
public override PreviewMode previewMode |
|||
{ |
|||
get { return PreviewMode.Preview2D; } |
|||
} |
|||
|
|||
public sealed override void UpdateNodeAfterDeserialization() |
|||
{ |
|||
AddSlot(new Vector3MaterialSlot(kOutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, Vector3.zero)); |
|||
RemoveSlotsNameNotMatching(new[] { kOutputSlotId }); |
|||
} |
|||
|
|||
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode) |
|||
{ |
|||
visitor.AddShaderChunk(string.Format("{0}3 {1} = {0}3{2};", precision, GetVariableNameForSlot(kOutputSlotId), m_MaterialList[material]), true); |
|||
} |
|||
} |
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue