浏览代码

Merge pull request #90 from Unity-Technologies/node-input-texture

Node input texture
/main
GitHub 7 年前
当前提交
5d9b28af
共有 27 个文件被更改,包括 614 次插入2 次删除
  1. 10
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MaterialSlot.cs
  2. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/AbstractMaterialNode.cs
  3. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/CodeFunctionNode.cs
  4. 31
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/Texture2DNode.cs
  5. 7
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/PropertyNode.cs
  6. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/SlotValue.cs
  7. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/AbstractSubGraphNode.cs
  8. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorView.cs
  9. 18
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/ShaderPropertyView.cs
  10. 16
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Manipulators/GraphDropTarget.cs
  11. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs
  12. 79
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/CubemapInputMaterialSlot.cs
  13. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/CubemapInputMaterialSlot.cs.meta
  14. 31
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/CubemapMaterialSlot.cs
  15. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/CubemapMaterialSlot.cs.meta
  16. 65
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/CubemapShaderProperty.cs
  17. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/CubemapShaderProperty.cs.meta
  18. 40
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SerializableCubemap.cs
  19. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SerializableCubemap.cs.meta
  20. 67
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/CubemapAssetNode.cs
  21. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/CubemapAssetNode.cs.meta
  22. 81
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/CubemapNode.cs
  23. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/CubemapNode.cs.meta
  24. 55
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/CubemapControl.cs
  25. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/CubemapControl.cs.meta
  26. 35
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/Slots/CubemapSlotControlView.cs
  27. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/Slots/CubemapSlotControlView.cs.meta

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


return "(SS)";
case ConcreteSlotValueType.Texture2D:
return "(T)";
case ConcreteSlotValueType.Cubemap:
return "(C)";
default:
return "(E)";
}

return slotType == SlotType.Input
? new Texture2DInputMaterialSlot(slotId, displayName, shaderOutputName, shaderStage, hidden)
: new Texture2DMaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden);
case SlotValueType.Cubemap:
return slotType == SlotType.Input
? new CubemapInputMaterialSlot(slotId, displayName, shaderOutputName, shaderStage, hidden)
: new CubemapMaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden);
case SlotValueType.Dynamic:
return new DynamicVectorMaterialSlot(slotId, displayName, shaderOutputName, slotType, defaultValue, shaderStage, hidden);
case SlotValueType.Vector4:

return inputType == SlotValueType.Matrix2;
case SlotValueType.Texture2D:
return inputType == SlotValueType.Texture2D;
case SlotValueType.Cubemap:
return inputType == SlotValueType.Cubemap;
case SlotValueType.Dynamic:
case SlotValueType.Vector4:
return inputType == SlotValueType.Vector4

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

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/AbstractMaterialNode.cs


return p + "4";
case ConcreteSlotValueType.Texture2D:
return "Texture2D";
case ConcreteSlotValueType.Cubemap:
return "Cubemap";
case ConcreteSlotValueType.Matrix2:
return "Matrix2x2";
case ConcreteSlotValueType.Matrix3:

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


{
return SlotValueType.Texture2D;
}
if (t == typeof(Cubemap))
{
return SlotValueType.Cubemap;
}
if (t == typeof(SamplerState))
{
return SlotValueType.SamplerState;

31
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/Texture2DNode.cs


using System.Reflection;
using UnityEngine;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing.Controls;
public enum TextureType
{
Default,
Normal
};
[Title("Input/Texture/Texture 2D")]
public class Texture2DNode : AbstractMaterialNode, IGeneratesBodyCode, IMayRequireMeshUV
{

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;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
}
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new Vector4MaterialSlot(OutputSlotRGBAId, kOutputSlotRGBAName, kOutputSlotRGBAName, SlotType.Output, Vector4.zero));

}
visitor.AddShaderChunk(result, true);
if (textureType == TextureType.Normal)
visitor.AddShaderChunk(string.Format("{0}.rgb = UnpackNormal({0});", GetVariableNameForSlot(OutputSlotRGBAId)), true);
visitor.AddShaderChunk(string.Format("{0} {1} = {2}.r;", precision, GetVariableNameForSlot(OutputSlotRId), GetVariableNameForSlot(OutputSlotRGBAId)), true);
visitor.AddShaderChunk(string.Format("{0} {1} = {2}.g;", precision, GetVariableNameForSlot(OutputSlotGId), GetVariableNameForSlot(OutputSlotRGBAId)), true);
visitor.AddShaderChunk(string.Format("{0} {1} = {2}.b;", precision, GetVariableNameForSlot(OutputSlotBId), GetVariableNameForSlot(OutputSlotRGBAId)), true);

7
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/PropertyNode.cs


AddSlot(new Texture2DMaterialSlot(OutputSlotId, "T", "T", SlotType.Output));
RemoveSlotsNameNotMatching(new[] {OutputSlotId});
}
else if (property is CubemapShaderProperty)
{
AddSlot(new CubemapMaterialSlot(OutputSlotId, "C", "C", SlotType.Output));
RemoveSlotsNameNotMatching(new[] { OutputSlotId });
}
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)

var graph = owner as AbstractMaterialGraph;
var property = graph.properties.FirstOrDefault(x => x.guid == propertyGuid);
if (!(property is TextureShaderProperty))
if (!(property is TextureShaderProperty) && !(property is CubemapShaderProperty))
return base.GetVariableNameForSlot(slotId);
return property.referenceName;

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


Matrix3,
Matrix2,
Texture2D,
Cubemap,
Dynamic,
Vector4,
Vector3,

Matrix3,
Matrix2,
Texture2D,
Cubemap,
Vector4,
Vector3,
Vector2,

"typeMatrix",
"typeMatrix",
"typeTexture2D",
"typeCubemap",
"typeFloat4",
"typeFloat3",
"typeFloat2",

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/AbstractSubGraphNode.cs


case PropertyType.Texture:
slotType = SlotValueType.Texture2D;
break;
case PropertyType.Cubemap:
slotType = SlotValueType.Cubemap;
break;
case PropertyType.Float:
slotType = SlotValueType.Vector1;
break;

var tProp = prop as TextureShaderProperty;
if (tSlot != null && tProp != null)
tSlot.texture = tProp.value.texture;
}
// copy default for cubemap for niceness
else if (slotType == SlotValueType.Cubemap && propType == PropertyType.Cubemap)
{
var tSlot = slot as CubemapInputMaterialSlot;
var tProp = prop as CubemapShaderProperty;
if (tSlot != null && tProp != null)
tSlot.cubemap = tProp.value.cubemap;
}
AddSlot(slot);
validNames.Add(id);

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


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

18
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/ShaderPropertyView.cs


objectField.OnValueChanged(OnTextureChanged);
Add(objectField);
}
else if (property is CubemapShaderProperty)
{
var fProp = (CubemapShaderProperty)property;
var objectField = new ObjectField { name = "value", objectType = typeof(Cubemap), value = fProp.value.cubemap };
objectField.OnValueChanged(OnCubemapChanged);
Add(objectField);
}
Add(new Button(OnClickRemove) { name = "remove", text = "Remove" });
}

if (newValue != fProp.value.texture)
{
fProp.value.texture = newValue;
NotifyNodes();
}
}
void OnCubemapChanged(ChangeEvent<Object> evt)
{
var fProp = (CubemapShaderProperty)property;
var newValue = (Cubemap)evt.newValue;
if (newValue != fProp.value.cubemap)
{
fProp.value.cubemap = newValue;
NotifyNodes();
}
}

16
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Manipulators/GraphDropTarget.cs


bool ValidateObject(Object obj)
{
return EditorUtility.IsPersistent(obj)
&& (obj is Texture2D || obj is MaterialSubGraphAsset);
&& (obj is Texture2D || obj is Cubemap || obj is MaterialSubGraphAsset);
}
void CreateNode(Object obj, Vector2 nodePosition)

{
m_Graph.owner.RegisterCompleteObjectUndo("Drag Texture");
var property = new TextureShaderProperty { displayName = texture2D.name, value = { texture = texture2D } };
m_Graph.AddShaderProperty(property);
var node = new PropertyNode();
var drawState = node.drawState;
drawState.position = new Rect(nodePosition, drawState.position.size);
node.drawState = drawState;
m_Graph.AddNode(node);
node.propertyGuid = property.guid;
}
var cubemap = obj as Cubemap;
if (cubemap != null)
{
m_Graph.owner.RegisterCompleteObjectUndo("Drag Cubemap");
var property = new CubemapShaderProperty { displayName = cubemap.name, value = { cubemap = cubemap } };
m_Graph.AddShaderProperty(property);
var node = new PropertyNode();
var drawState = node.drawState;

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


case ConcreteSlotValueType.Texture2D:
prop = new TextureShaderProperty();
break;
case ConcreteSlotValueType.Cubemap:
prop = new CubemapShaderProperty();
break;
case ConcreteSlotValueType.Vector4:
prop = new Vector4ShaderProperty();
break;

79
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/CubemapInputMaterialSlot.cs


using System;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing.Slots;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class CubemapInputMaterialSlot : CubemapMaterialSlot
{
[SerializeField]
private SerializableCubemap m_Cubemap = new SerializableCubemap();
public Cubemap cubemap
{
get { return m_Cubemap.cubemap; }
set { m_Cubemap.cubemap = value; }
}
public CubemapInputMaterialSlot()
{}
public CubemapInputMaterialSlot(
int slotId,
string displayName,
string shaderOutputName,
ShaderStage shaderStage = ShaderStage.Dynamic,
bool hidden = false)
: base(slotId, displayName, shaderOutputName, SlotType.Input, shaderStage, hidden)
{}
public override VisualElement InstantiateControl()
{
return new CubemapSlotControlView(this);
}
public override string GetDefaultValue(GenerationMode generationMode)
{
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)));
return matOwner.GetVariableNameForSlot(id);
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{
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 prop = new CubemapShaderProperty();
prop.overrideReferenceName = matOwner.GetVariableNameForSlot(id);
prop.modifiable = false;
prop.generatePropertyBlock = true;
prop.value.cubemap = cubemap;
properties.AddShaderProperty(prop);
}
public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
{
m_Name = name,
m_PropType = PropertyType.Cubemap,
m_Cubemap = cubemap
};
return pp;
}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
var slot = foundSlot as CubemapInputMaterialSlot;
if (slot != null)
m_Cubemap = slot.m_Cubemap;
}
}
}

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


fileFormatVersion: 2
guid: 7b961f6f7e2617446afb6a8e329d84d4
timeCreated: 1509276977

31
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/CubemapMaterialSlot.cs


using System;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class CubemapMaterialSlot : MaterialSlot
{
public CubemapMaterialSlot()
{}
public CubemapMaterialSlot(
int slotId,
string displayName,
string shaderOutputName,
SlotType slotType,
ShaderStage shaderStage = ShaderStage.Dynamic,
bool hidden = false)
: base(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden)
{}
public override SlotValueType valueType { get { return SlotValueType.Cubemap; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Cubemap; } }
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{}
}
}

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


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

65
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/CubemapShaderProperty.cs


using System;
using System.Text;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class CubemapShaderProperty : AbstractShaderProperty<SerializableCubemap>
{
[SerializeField]
private bool m_Modifiable = true;
public CubemapShaderProperty()
{
value = new SerializableCubemap();
}
public override PropertyType propertyType
{
get { return PropertyType.Cubemap; }
}
public bool modifiable
{
get { return m_Modifiable; }
set { m_Modifiable = value; }
}
public override Vector4 defaultValue
{
get { return new Vector4(); }
}
public override string GetPropertyBlockString()
{
var result = new StringBuilder();
if (!m_Modifiable)
{
result.Append("[NonModifiableTextureData] ");
}
result.Append("[NoScaleOffset] ");
result.Append(referenceName);
result.Append("(\"");
result.Append(displayName);
result.Append("\", CUBE) = \"\" {}");
return result.ToString();
}
public override string GetPropertyDeclarationString()
{
return "samplerCUBE " + referenceName + ";";
}
public override PreviewProperty GetPreviewMaterialProperty()
{
return new PreviewProperty()
{
m_Name = referenceName,
m_PropType = PropertyType.Cubemap,
m_Cubemap = value.cubemap
};
}
}
}

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


fileFormatVersion: 2
guid: ca5ddce766d5ae24f8be282b4f7a21bd
timeCreated: 1505346949

40
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SerializableCubemap.cs


using System;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class SerializableCubemap
{
[SerializeField]
private string m_SerializedCubemap;
[Serializable]
private class CubemapHelper
{
public Cubemap cubemap;
}
public Cubemap cubemap
{
get
{
if (string.IsNullOrEmpty(m_SerializedCubemap))
return null;
var cube = new CubemapHelper();
EditorJsonUtility.FromJsonOverwrite(m_SerializedCubemap, cube);
return cube.cubemap;
}
set
{
if (cubemap == value)
return;
var cube = new CubemapHelper();
cube.cubemap = value;
m_SerializedCubemap = EditorJsonUtility.ToJson(cube, true);
}
}
}
}

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


fileFormatVersion: 2
guid: 01e80e004e94f5e48ad1cb83d5701300
timeCreated: 1505346945

67
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/CubemapAssetNode.cs


using System.Collections.Generic;
using UnityEditor.ShaderGraph.Drawing.Controls;
using UnityEngine;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
[Title("Input/Texture/Cubemap Asset")]
public class CubemapAssetNode : AbstractMaterialNode
{
public const int OutputSlotId = 0;
const string kOutputSlotName = "Out";
public CubemapAssetNode()
{
name = "Cubemap Asset";
UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new CubemapMaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output));
RemoveSlotsNameNotMatching(new[] { OutputSlotId });
}
[SerializeField]
private SerializableCubemap m_Cubemap = new SerializableCubemap();
[CubemapControl("")]
public Cubemap cubemap
{
get { return m_Cubemap.cubemap; }
set
{
if (m_Cubemap.cubemap == value)
return;
m_Cubemap.cubemap = value;
if (onModified != null)
{
onModified(this, ModificationScope.Node);
}
}
}
public override void CollectShaderProperties(PropertyCollector properties, GenerationMode generationMode)
{
properties.AddShaderProperty(new CubemapShaderProperty()
{
overrideReferenceName = GetVariableNameForSlot(OutputSlotId),
generatePropertyBlock = true,
value = m_Cubemap,
modifiable = false
});
}
public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
{
properties.Add(new PreviewProperty
{
m_Name = GetVariableNameForSlot(OutputSlotId),
m_PropType = PropertyType.Cubemap,
m_Cubemap = cubemap
});
}
}
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/CubemapAssetNode.cs.meta


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

81
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/CubemapNode.cs


using System.Linq;
using System.Reflection;
using UnityEngine;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing.Controls;
namespace UnityEditor.ShaderGraph
{
[Title("Input/Texture/Cubemap")]
public class CubemapNode : AbstractMaterialNode, IGeneratesBodyCode, IMayRequireViewDirection, IMayRequireNormal
{
public const int OutputSlotId = 0;
public const int CubemapInputId = 1;
public const int ViewDirInputId = 2;
public const int NormalInputId = 3;
public const int LODInputId = 4;
const string kOutputSlotName = "Out";
const string kCubemapInputName = "Cube";
const string kViewDirInputName = "ViewDir";
const string kNormalInputName = "Normal";
const string kLODInputName = "LOD";
public override bool hasPreview { get { return true; } }
public CubemapNode()
{
name = "Cubemap";
UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new Vector4MaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, Vector4.zero));
AddSlot(new CubemapInputMaterialSlot(CubemapInputId, kCubemapInputName, kCubemapInputName));
AddSlot(new ViewDirectionMaterialSlot(ViewDirInputId, kViewDirInputName, kViewDirInputName, CoordinateSpace.Object));
AddSlot(new NormalMaterialSlot(NormalInputId, kNormalInputName, kNormalInputName, CoordinateSpace.Object));
AddSlot(new Vector1MaterialSlot(LODInputId, kLODInputName, kLODInputName, SlotType.Input, 0));
RemoveSlotsNameNotMatching(new[] { OutputSlotId, CubemapInputId, ViewDirInputId, NormalInputId, LODInputId });
}
public override PreviewMode previewMode
{
get { return PreviewMode.Preview3D; }
}
// Node generations
public virtual void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
string result = string.Format("{0}4 {1} = texCUBElod ({2}, {0}4(reflect(-{3}, {4}), {5}));"
, precision
, GetVariableNameForSlot(OutputSlotId)
, GetSlotValue(CubemapInputId, generationMode)
, GetSlotValue(ViewDirInputId, generationMode)
, GetSlotValue(NormalInputId, generationMode)
, GetSlotValue(LODInputId, generationMode));
visitor.AddShaderChunk(result, true);
}
public NeededCoordinateSpace RequiresViewDirection()
{
var viewDirSlot = FindInputSlot<MaterialSlot>(ViewDirInputId);
var edgesViewDir = owner.GetEdges(viewDirSlot.slotReference);
if (!edgesViewDir.Any())
return CoordinateSpace.Object.ToNeededCoordinateSpace();
else
return NeededCoordinateSpace.None;
}
public NeededCoordinateSpace RequiresNormal()
{
var normalSlot = FindInputSlot<MaterialSlot>(NormalInputId);
var edgesNormal = owner.GetEdges(normalSlot.slotReference);
if (!edgesNormal.Any())
return CoordinateSpace.Object.ToNeededCoordinateSpace();
else
return NeededCoordinateSpace.None;
}
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/CubemapNode.cs.meta


fileFormatVersion: 2
guid: 6eaeb9c846408f74daeadcc3314810e4
timeCreated: 1495637741
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

55
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/CubemapControl.cs


using System;
using System.Reflection;
using UnityEditor.Experimental.UIElements;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
using UnityEngine.Experimental.UIElements.StyleSheets;
using UnityEditor.ShaderGraph;
namespace UnityEditor.ShaderGraph.Drawing.Controls
{
[AttributeUsage(AttributeTargets.Property)]
public class CubemapControlAttribute : Attribute, IControlAttribute
{
string m_Label;
public CubemapControlAttribute(string label = null)
{
m_Label = label;
}
public VisualElement InstantiateControl(AbstractMaterialNode node, PropertyInfo propertyInfo)
{
return new CubemapControlView(m_Label, node, propertyInfo);
}
}
public class CubemapControlView : VisualElement
{
AbstractMaterialNode m_Node;
PropertyInfo m_PropertyInfo;
public CubemapControlView(string label, AbstractMaterialNode node, PropertyInfo propertyInfo)
{
m_Node = node;
m_PropertyInfo = propertyInfo;
if (propertyInfo.PropertyType != typeof(Cubemap))
throw new ArgumentException("Property must be of type Texture.", "propertyInfo");
label = label ?? ObjectNames.NicifyVariableName(propertyInfo.Name);
if (!string.IsNullOrEmpty(label))
Add(new Label(label));
var cubemapField = new ObjectField { value = (Cubemap) m_PropertyInfo.GetValue(m_Node, null), objectType = typeof(Cubemap) };
cubemapField.OnValueChanged(OnChange);
Add(cubemapField);
}
void OnChange(ChangeEvent<UnityEngine.Object> evt)
{
m_Node.owner.owner.RegisterCompleteObjectUndo("Cubemap Change");
m_PropertyInfo.SetValue(m_Node, evt.newValue, null);
Dirty(ChangeType.Repaint);
}
}
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/CubemapControl.cs.meta


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

35
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/Slots/CubemapSlotControlView.cs


using System;
using UnityEditor.Experimental.UIElements;
using UnityEditor.Graphing;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
using UnityEngine.Experimental.UIElements.StyleSheets;
using Object = UnityEngine.Object;
namespace UnityEditor.ShaderGraph.Drawing.Slots
{
public class CubemapSlotControlView : VisualElement
{
CubemapInputMaterialSlot m_Slot;
public CubemapSlotControlView(CubemapInputMaterialSlot slot)
{
m_Slot = slot;
var objectField = new ObjectField { objectType = typeof(Cubemap), value = m_Slot.cubemap };
objectField.OnValueChanged(OnValueChanged);
Add(objectField);
}
void OnValueChanged(ChangeEvent<Object> evt)
{
var cubemap = evt.newValue as Cubemap;
if (cubemap != m_Slot.cubemap)
{
m_Slot.owner.owner.owner.RegisterCompleteObjectUndo("Change Cubemap");
m_Slot.cubemap = cubemap;
if (m_Slot.owner.onModified != null)
m_Slot.owner.onModified(m_Slot.owner, ModificationScope.Node);
}
}
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/Slots/CubemapSlotControlView.cs.meta


fileFormatVersion: 2
guid: b54f542a9d5f7b84e974b54d625d73ea
timeCreated: 1509718979
正在加载...
取消
保存