Tim Cooper
7 年前
当前提交
06b7010b
共有 18 个文件被更改,包括 87 次插入 和 1001 次删除
-
10MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/AbstractGraphEditWindow.cs
-
10MaterialGraphProject/Assets/Matt/AbstractAdvancedMasterNode.cs
-
83MaterialGraphProject/Assets/NewNodes/WIP/AACheckerBoard3dNode.cs
-
79MaterialGraphProject/Assets/NewNodes/WIP/AACheckerBoardNode.cs
-
4MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/NodePreviewPresenter.cs
-
4MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers/ShaderGraphImporter.cs
-
2MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/IntegrationTests/ShaderGenerationTest.cs
-
4MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Interfaces/IMasterNode.cs
-
6MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbstractMasterNode.cs
-
10MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Remapper/RemapMasterNode.cs
-
4MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SurfaceModel/AbstractSurfaceMasterNode.cs
-
6MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SurfaceModel/ExportTextureMasterNode.cs
-
12MaterialGraphProject/Assets/Eduardo/Editor/AddManyNodePresenter.cs.meta
-
50MaterialGraphProject/Assets/Eduardo/Editor/AddManyNodePresenter.cs
-
12MaterialGraphProject/Assets/NewNodes/WIP/AnyNode.cs.meta
-
670MaterialGraphProject/Assets/NewNodes/WIP/AnyNode.cs
-
12MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/AnyNodePresenter.cs.meta
-
110MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/AnyNodePresenter.cs
|
|||
using UnityEngine.Graphing; |
|||
using System.Reflection; |
|||
/*[Title("Procedural/AACheckerboard3d")] |
|||
public class AACheckerboard3dNode : AnyNode<AACheckerboard3dNode.Definition> |
|||
[Title("Procedural/AACheckerboard3d")] |
|||
public class AACheckerboard3dNode : CodeFunctionNode |
|||
public class Definition : IAnyNodeDefinition |
|||
protected override MethodInfo GetFunctionToConvert() |
|||
public string name { get { return "AACheckerboard3d"; } } |
|||
return GetType().GetMethod("Unity_AACheckerboard3d", BindingFlags.Static | BindingFlags.NonPublic); |
|||
} |
|||
public AnyNodeProperty[] properties |
|||
{ |
|||
get |
|||
{ |
|||
return new AnyNodeProperty[] |
|||
{ |
|||
// slotId is the 'immutable' value we used to connect things
|
|||
new AnyNodeProperty { slotId= 0, name = "inUVs", description = "Input UVW coords", propertyType = PropertyType.Vector3, value = Vector4.zero, state = AnyNodePropertyState.Slot }, |
|||
new AnyNodeProperty { slotId= 1, name = "A", description = "color A", propertyType = PropertyType.Vector4, value= new Vector4(0.2f, 0.2f, 0.2f, 0.2f), state = AnyNodePropertyState.Constant }, |
|||
new AnyNodeProperty { slotId= 2, name = "B", description = "color B", propertyType = PropertyType.Vector4, value= new Vector4(0.7f, 0.7f, 0.7f, 0.7f), state = AnyNodePropertyState.Constant }, |
|||
new AnyNodeProperty { slotId= 3, name = "aaTweak", description = "AA Tweak", propertyType = PropertyType.Vector3, value= new Vector4(0.05f, 3.0f, 0.0f, 0.0f),state = AnyNodePropertyState.Constant }, |
|||
new AnyNodeProperty { slotId= 4, name = "frequency", description = "Frequency", propertyType = PropertyType.Vector3, value = Vector4.one, state = AnyNodePropertyState.Constant }, |
|||
}; |
|||
} |
|||
} |
|||
|
|||
public AnyNodeSlot[] outputs |
|||
{ |
|||
get |
|||
{ |
|||
return new AnyNodeSlot[] |
|||
{ |
|||
new AnyNodeSlot { slotId= 5, name = "outColor", description = "Output color", slotValueType = SlotValueType.Vector4, value = Vector4.zero } |
|||
}; |
|||
} |
|||
} |
|||
public ShaderGlobal[] globals { get { return new ShaderGlobal[] { }; } } |
|||
|
|||
public string hlsl |
|||
{ |
|||
get |
|||
{ |
|||
return |
|||
"float3 dx = ddx(inUVs);\n" + |
|||
"float3 dy = ddy(inUVs);\n" + |
|||
"float du= sqrt(dx.x * dx.x + dy.x * dy.x);\n" + |
|||
"float dv= sqrt(dx.y * dx.y + dy.y * dy.y);\n" + |
|||
"float dw= sqrt(dx.z * dx.z + dy.z * dy.z);\n" + |
|||
"float3 distance3 = 2.0f * abs(frac((inUVs.xyz + 0.5f) * frequency.xyz) - 0.5f) - 0.5f;\n" + |
|||
"float3 scale = aaTweak.xxx / float3(du, dv, dw);\n" + |
|||
"float3 blend_out = saturate((scale - aaTweak.zzz) / (aaTweak.yyy - aaTweak.zzz));\n" + |
|||
"float3 vectorAlpha = clamp(distance3 * scale.xyz * blend_out.xyz, -1.0f, 1.0f);\n" + |
|||
"float alpha = saturate(0.5f + 0.5f * vectorAlpha.x * vectorAlpha.y * vectorAlpha.z);\n" + |
|||
"outColor= lerp(A, B, alpha.xxxx);"; |
|||
} |
|||
} |
|||
static string Unity_AACheckerboard3d( |
|||
[Slot(0, Binding.MeshUV0)] Vector2 uv, |
|||
[Slot(1, Binding.None, 0.2f, 0.2f, 0.2f, 0.2f)] Vector4 colorA, |
|||
[Slot(2, Binding.None, 0.7f, 0.7f, 0.7f, 0.7f)] Vector4 colorB, |
|||
[Slot(3, Binding.None, 0.5f, 3f, 0f, 0f)] Vector3 aaTweak, |
|||
[Slot(4, Binding.None, 1f, 1f, 1f, 1f)] Vector3 frequency, |
|||
[Slot(5, Binding.None)] out Vector4 result) |
|||
{ |
|||
result = Vector2.zero; |
|||
return |
|||
@"
|
|||
{ |
|||
float3 dx = ddx(inUVs); |
|||
float3 dy = ddy(inUVs); |
|||
float du= sqrt(dx.x * dx.x + dy.x * dy.x); |
|||
float dv= sqrt(dx.y * dx.y + dy.y * dy.y); |
|||
float dw= sqrt(dx.z * dx.z + dy.z * dy.z); |
|||
float3 distance3 = 2.0f * abs(frac((inUVs.xyz + 0.5f) * frequency.xyz) - 0.5f) - 0.5f; |
|||
float3 scale = aaTweak.xxx / float3(du, dv, dw); |
|||
float3 blend_out = saturate((scale - aaTweak.zzz) / (aaTweak.yyy - aaTweak.zzz)); |
|||
float3 vectorAlpha = clamp(distance3 * scale.xyz * blend_out.xyz, -1.0f, 1.0f); |
|||
float alpha = saturate(0.5f + 0.5f * vectorAlpha.x * vectorAlpha.y * vectorAlpha.z); |
|||
result= lerp(colorA, colorB, alpha.xxxx);; |
|||
}";
|
|||
}*/ |
|||
} |
|||
} |
|||
|
|
|||
using UnityEngine.Graphing; |
|||
using System.Reflection; |
|||
/* [Title("Procedural/AACheckerboard")] |
|||
public class AACheckerboardNode : AnyNode<AACheckerboardNode.Definition> |
|||
[Title("Procedural/AACheckerboard")] |
|||
public class AACheckerboardNode : CodeFunctionNode |
|||
public class Definition : IAnyNodeDefinition |
|||
protected override MethodInfo GetFunctionToConvert() |
|||
public string name { get { return "AACheckerboard"; } } |
|||
return GetType().GetMethod("Unity_AACheckerboard", BindingFlags.Static | BindingFlags.NonPublic); |
|||
} |
|||
public AnyNodeProperty[] properties |
|||
{ |
|||
get |
|||
{ |
|||
return new AnyNodeProperty[] |
|||
{ |
|||
// slotId is the 'immutable' value we used to connect things
|
|||
new AnyNodeProperty { slotId= 0, name = "inUVs", description = "Input UV coords", propertyType = PropertyType.Vector2, value = Vector4.zero, state = AnyNodePropertyState.Slot }, |
|||
new AnyNodeProperty { slotId= 1, name = "A", description = "color A", propertyType = PropertyType.Vector4, value= new Vector4(0.2f, 0.2f, 0.2f, 0.2f), state = AnyNodePropertyState.Constant }, |
|||
new AnyNodeProperty { slotId= 2, name = "B", description = "color B", propertyType = PropertyType.Vector4, value= new Vector4(0.7f, 0.7f, 0.7f, 0.7f), state = AnyNodePropertyState.Constant }, |
|||
new AnyNodeProperty { slotId= 3, name = "aaTweak", description = "AA Tweak", propertyType = PropertyType.Vector3, value= new Vector4(0.05f, 3.0f, 0.0f, 0.0f),state = AnyNodePropertyState.Constant }, |
|||
new AnyNodeProperty { slotId= 4, name = "frequency", description = "Frequency", propertyType = PropertyType.Vector2, value = Vector4.one, state = AnyNodePropertyState.Constant }, |
|||
}; |
|||
} |
|||
} |
|||
|
|||
public AnyNodeSlot[] outputs |
|||
{ |
|||
get |
|||
{ |
|||
return new AnyNodeSlot[] |
|||
{ |
|||
new AnyNodeSlot { slotId= 5, name = "outColor", description = "Output color", slotValueType = SlotValueType.Vector4, value = Vector4.zero } |
|||
}; |
|||
} |
|||
} |
|||
public ShaderGlobal[] globals { get { return new ShaderGlobal[] { }; } } |
|||
|
|||
public string hlsl |
|||
{ |
|||
get |
|||
{ |
|||
return |
|||
"float4 derivatives = float4(ddx(inUVs), ddy(inUVs));\n" + |
|||
"float2 duv_length = sqrt(float2(dot(derivatives.xz, derivatives.xz), dot(derivatives.yw, derivatives.yw)));\n" + |
|||
"float width = 0.5f;\n" + |
|||
"float2 distance3 = 2.0f * abs(frac(inUVs.xy * frequency) - 0.5f) - width;\n" + |
|||
"float2 scale = aaTweak.x / duv_length.xy;\n" + |
|||
"float2 blend_out = saturate((scale - aaTweak.zz) / (aaTweak.yy - aaTweak.zz));\n" + |
|||
"float2 vector_alpha = clamp(distance3 * scale.xy * blend_out.xy, -1.0f, 1.0f);\n" + |
|||
"float alpha = saturate(0.5f + 0.5f * vector_alpha.x * vector_alpha.y);\n" + |
|||
"outColor= lerp(A, B, alpha.xxxx);"; |
|||
} |
|||
} |
|||
static string Unity_AACheckerboard( |
|||
[Slot(0, Binding.MeshUV0)] Vector2 uv, |
|||
[Slot(1, Binding.None, 0.2f, 0.2f, 0.2f, 0.2f)] Vector4 colorA, |
|||
[Slot(2, Binding.None, 0.7f, 0.7f, 0.7f, 0.7f)] Vector4 colorB, |
|||
[Slot(3, Binding.None, 0.5f, 3f, 0f, 0f)] Vector3 aaTweak, |
|||
[Slot(4, Binding.None, 1f, 1f, 1f, 1f)] Vector2 frequency, |
|||
[Slot(5, Binding.None)] out Vector4 result) |
|||
{ |
|||
result = Vector2.zero; |
|||
return |
|||
@"
|
|||
{ |
|||
float4 derivatives = float4(ddx(inUVs), ddy(inUVs)); |
|||
float2 duv_length = sqrt(float2(dot(derivatives.xz, derivatives.xz), dot(derivatives.yw, derivatives.yw))); |
|||
float width = 0.5f; |
|||
float2 distance3 = 2.0f * abs(frac(inUVs.xy * frequency) - 0.5f) - width; |
|||
float2 scale = aaTweak.x / duv_length.xy; |
|||
float2 blend_out = saturate((scale - aaTweak.zz) / (aaTweak.yy - aaTweak.zz)); |
|||
float2 vector_alpha = clamp(distance3 * scale.xy * blend_out.xy, -1.0f, 1.0f); |
|||
float alpha = saturate(0.5f + 0.5f * vector_alpha.x * vector_alpha.y); |
|||
result= lerp(colorA, colorB, alpha.xxxx); |
|||
}";
|
|||
}*/ |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 0d89e5c0d48973c4ab16793131164a16 |
|||
timeCreated: 1495530357 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using UnityEngine.MaterialGraph; |
|||
using RMGUI.GraphView; |
|||
using UnityEditor.Graphing.Drawing; |
|||
using UnityEngine; |
|||
|
|||
namespace UnityEditor.MaterialGraph.Drawing |
|||
{ |
|||
/* [Serializable] |
|||
class AddManyContolPresenter : GraphControlPresenter |
|||
{ |
|||
|
|||
public override void OnGUIHandler() |
|||
{ |
|||
base.OnGUIHandler(); |
|||
|
|||
var addNode = node as UnityEngine.MaterialGraph.AddManyNode; |
|||
if (addNode == null) |
|||
return; |
|||
|
|||
if (GUILayout.Button("Add Input")) |
|||
{ |
|||
addNode.AddInputSlot(); |
|||
addNode.OnModified(); |
|||
} |
|||
if (GUILayout.Button("Remove Input")) |
|||
{ |
|||
addNode.RemoveInputSlot(); |
|||
addNode.OnModified(); |
|||
} |
|||
} |
|||
|
|||
public override float GetHeight() |
|||
{ |
|||
return EditorGUIUtility.singleLineHeight * 2 + 3 * EditorGUIUtility.standardVerticalSpacing; |
|||
} |
|||
} |
|||
|
|||
[Serializable] |
|||
public class AddManyNodePresenter : MaterialNodePresenter |
|||
{ |
|||
protected override IEnumerable<GraphElementPresenter> GetControlData() |
|||
{ |
|||
var instance = CreateInstance<AddManyContolPresenter>(); |
|||
instance.Initialize(node); |
|||
return new List<GraphElementPresenter> { instance }; |
|||
} |
|||
}*/ |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 38b06780b4d2b274b91e1c2c5627f95d |
|||
timeCreated: 1495627163 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
using System; |
|||
|
|||
|
|||
namespace UnityEngine.MaterialGraph |
|||
{ |
|||
/* public enum ShaderGlobal |
|||
{ |
|||
UV0, |
|||
UV1, |
|||
UV2, |
|||
UV3, |
|||
WorldSpaceNormal, |
|||
WorldSpaceBitangent, |
|||
WorldSpaceTangent, |
|||
WorldSpacePosition, |
|||
WorldSpaceViewDirection, |
|||
TangentSpaceViewDirection, |
|||
ScreenPosition, |
|||
VertexColor, |
|||
} |
|||
|
|||
[Serializable] |
|||
public class AnyNodeSlot |
|||
{ |
|||
public int slotId; |
|||
public string name; |
|||
public string description; |
|||
public SlotValueType slotValueType; |
|||
public Vector4 value; |
|||
|
|||
public MaterialSlot toMaterialSlot() |
|||
{ |
|||
return new MaterialSlot(slotId, name, name, Graphing.SlotType.Output, slotValueType, value); |
|||
} |
|||
|
|||
public string getTypeDecl(AbstractMaterialNode.OutputPrecision precision) |
|||
{ |
|||
switch (slotValueType) |
|||
{ |
|||
case SlotValueType.Sampler2D: |
|||
// TODO
|
|||
break; |
|||
case SlotValueType.Dynamic: |
|||
// TODO
|
|||
break; |
|||
case SlotValueType.Vector1: |
|||
return precision + ""; |
|||
case SlotValueType.Vector2: |
|||
return precision + "2"; |
|||
case SlotValueType.Vector3: |
|||
return precision + "3"; |
|||
case SlotValueType.Vector4: |
|||
return precision + "4"; |
|||
} |
|||
return null; |
|||
} |
|||
}; |
|||
|
|||
[Serializable] |
|||
public enum AnyNodePropertyState |
|||
{ |
|||
Constant = 0, |
|||
Exposed = 1, |
|||
Slot = 2, |
|||
}; |
|||
|
|||
|
|||
[Serializable] |
|||
public class AnyNodeProperty |
|||
{ |
|||
public int slotId; |
|||
public string name; |
|||
public string description; |
|||
public PropertyType propertyType; |
|||
public Vector4 value; |
|||
public AnyNodePropertyState state; |
|||
|
|||
public MaterialSlot toMaterialSlot() |
|||
{ |
|||
// convert property type to slotvaluetype...
|
|||
SlotValueType slotValueType = SlotValueType.Dynamic; |
|||
switch (propertyType) |
|||
{ |
|||
case PropertyType.Color: |
|||
slotValueType = SlotValueType.Vector4; |
|||
break; |
|||
case PropertyType.Texture: |
|||
slotValueType = SlotValueType.Sampler2D; |
|||
break; |
|||
case PropertyType.Cubemap: |
|||
slotValueType = SlotValueType.Sampler2D; |
|||
break; |
|||
case PropertyType.Float: |
|||
slotValueType = SlotValueType.Vector1; |
|||
break; |
|||
case PropertyType.Vector2: |
|||
slotValueType = SlotValueType.Vector2; |
|||
break; |
|||
case PropertyType.Vector3: |
|||
slotValueType = SlotValueType.Vector3; |
|||
break; |
|||
case PropertyType.Vector4: |
|||
slotValueType = SlotValueType.Vector4; |
|||
break; |
|||
case PropertyType.Matrix2: |
|||
slotValueType = SlotValueType.Matrix2; |
|||
break; |
|||
case PropertyType.Matrix3: |
|||
slotValueType = SlotValueType.Matrix3; |
|||
break; |
|||
case PropertyType.Matrix4: |
|||
slotValueType = SlotValueType.Matrix4; |
|||
break; |
|||
} |
|||
|
|||
return new MaterialSlot(slotId, name, name, Graphing.SlotType.Input, slotValueType, value); |
|||
} |
|||
|
|||
public PropertyChunk toPropertyChunk() |
|||
{ |
|||
switch (propertyType) |
|||
{ |
|||
case PropertyType.Color: |
|||
// TODO
|
|||
break; |
|||
case PropertyType.Texture: |
|||
// TODO
|
|||
break; |
|||
case PropertyType.Cubemap: |
|||
// TODO
|
|||
break; |
|||
case PropertyType.Float: |
|||
return new FloatPropertyChunk(name, description, value.x, PropertyChunk.HideState.Visible); |
|||
case PropertyType.Vector2: |
|||
case PropertyType.Vector3: |
|||
case PropertyType.Vector4: |
|||
return new VectorPropertyChunk(name, description, value, PropertyChunk.HideState.Visible); |
|||
case PropertyType.Matrix2: |
|||
case PropertyType.Matrix3: |
|||
case PropertyType.Matrix4: |
|||
// TODO
|
|||
break; |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
public string getTypeDecl(AbstractMaterialNode.OutputPrecision precision) |
|||
{ |
|||
switch (propertyType) |
|||
{ |
|||
case PropertyType.Color: |
|||
case PropertyType.Texture: |
|||
case PropertyType.Cubemap: |
|||
// TODO
|
|||
return null; |
|||
case PropertyType.Float: |
|||
return precision + ""; |
|||
case PropertyType.Vector2: |
|||
return precision + "2"; |
|||
case PropertyType.Vector3: |
|||
return precision + "3"; |
|||
case PropertyType.Vector4: |
|||
return precision + "4"; |
|||
case PropertyType.Matrix2: |
|||
case PropertyType.Matrix3: |
|||
case PropertyType.Matrix4: |
|||
// TODO
|
|||
break; |
|||
} |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
public interface IAnyNodeDefinition |
|||
{ |
|||
string name { get; } |
|||
AnyNodeProperty[] properties { get; } |
|||
AnyNodeSlot[] outputs { get; } |
|||
ShaderGlobal[] globals { get; } |
|||
string hlsl { get; } |
|||
} |
|||
|
|||
|
|||
public class AnyNodeBase : AbstractMaterialNode |
|||
{ |
|||
// local copy of the definition data -- some of it is state that gets modified by the user
|
|||
// TODO: really we should break the properties and slots into static definition data and mutable user data -- separate structs
|
|||
[SerializeField] |
|||
protected AnyNodeProperty[] m_properties; |
|||
|
|||
[SerializeField] |
|||
protected AnyNodeSlot[] m_outputSlots; |
|||
|
|||
[SerializeField] |
|||
protected ShaderGlobal[] m_globals; |
|||
|
|||
public IEnumerable<AnyNodeProperty> properties |
|||
{ |
|||
get |
|||
{ |
|||
return m_properties; |
|||
} |
|||
} |
|||
|
|||
public int propertyCount |
|||
{ |
|||
get { return m_properties.Length; } |
|||
} |
|||
|
|||
public void setPropertyState(AnyNodeProperty p, AnyNodePropertyState state) |
|||
{ |
|||
if (p.state != state) |
|||
{ |
|||
if ((p.state == AnyNodePropertyState.Slot) && (state != AnyNodePropertyState.Slot)) |
|||
{ |
|||
// Removing slot
|
|||
RemoveSlot(p.slotId); |
|||
} |
|||
else if ((p.state != AnyNodePropertyState.Slot) && (state == AnyNodePropertyState.Slot)) |
|||
{ |
|||
// Adding slot
|
|||
AddSlot(p.toMaterialSlot()); |
|||
} |
|||
|
|||
p.state = state; |
|||
} |
|||
} |
|||
} |
|||
|
|||
// [Title("Abstract Any Node")]
|
|||
public class AnyNode<DEFINITION> : |
|||
AnyNodeBase |
|||
, IGeneratesBodyCode |
|||
, IGeneratesFunction |
|||
// , IMayRequireMeshUV
|
|||
// , IMayRequireNormal // TODO
|
|||
// , IMayRequireTangent
|
|||
// , IMayRequireBitangent
|
|||
// , IMayRequireScreenPosition
|
|||
// , IMayRequireViewDirection
|
|||
// , IMayRequireWorldPosition
|
|||
// , IMayRequireVertexColor
|
|||
, IMayRequireViewDirectionTangentSpace |
|||
|
|||
where DEFINITION : IAnyNodeDefinition, new() |
|||
{ |
|||
private DEFINITION m_definition; |
|||
|
|||
public string node_name |
|||
{ |
|||
get |
|||
{ |
|||
return m_definition.name; |
|||
} |
|||
} |
|||
|
|||
public string node_hlsl |
|||
{ |
|||
get |
|||
{ |
|||
return m_definition.hlsl; |
|||
} |
|||
} |
|||
|
|||
public AnyNode() |
|||
{ |
|||
m_definition = new DEFINITION(); |
|||
|
|||
m_properties = m_definition.properties; |
|||
|
|||
m_outputSlots = m_definition.outputs; |
|||
|
|||
m_globals = m_definition.globals; |
|||
|
|||
UpdateNodeAfterDeserialization(); |
|||
} |
|||
|
|||
public sealed override void UpdateNodeAfterDeserialization() |
|||
{ |
|||
// update displayed name of the node
|
|||
name = node_name; |
|||
|
|||
// check new properties and slot definitions -- we want to forward data from the old ones
|
|||
// (just in case the definitions have changed)
|
|||
AnyNodeProperty[] new_properties= m_definition.properties; |
|||
AnyNodeSlot[] new_outputs= m_definition.outputs; |
|||
foreach (AnyNodeSlot s in new_outputs) |
|||
{ |
|||
// try to find matching slot
|
|||
AnyNodeSlot old_slot= Array.Find(m_outputSlots, x => x.slotId == s.slotId); |
|||
if (old_slot != null) |
|||
{ |
|||
s.value = old_slot.value; |
|||
} |
|||
} |
|||
foreach (AnyNodeProperty p in new_properties) |
|||
{ |
|||
// try to find matching old property
|
|||
AnyNodeProperty old_property = Array.Find(m_properties, x => x.slotId == p.slotId); |
|||
if (old_property != null) |
|||
{ |
|||
p.state = old_property.state; |
|||
p.value = old_property.value; |
|||
} |
|||
} |
|||
|
|||
// now that we've copied the old data into the new properties, start using the new properties
|
|||
m_properties = new_properties; |
|||
m_outputSlots = new_outputs; |
|||
m_globals = m_definition.globals; |
|||
|
|||
List<int> validSlotIds = new List<int>(); |
|||
|
|||
// add output slots first
|
|||
foreach (AnyNodeSlot s in m_outputSlots) |
|||
{ |
|||
// add slot to node
|
|||
AddSlot(s.toMaterialSlot()); |
|||
validSlotIds.Add(s.slotId); |
|||
} |
|||
|
|||
// add input slots
|
|||
foreach (AnyNodeProperty p in m_properties) |
|||
{ |
|||
// if this property is an input slot
|
|||
if (p.state == AnyNodePropertyState.Slot) |
|||
{ |
|||
// add slot to node
|
|||
AddSlot(p.toMaterialSlot()); |
|||
validSlotIds.Add(p.slotId); |
|||
} |
|||
} |
|||
|
|||
RemoveSlotsNameNotMatching(validSlotIds); |
|||
} |
|||
|
|||
public override void GeneratePropertyBlock(PropertyGenerator visitor, GenerationMode generationMode) |
|||
{ |
|||
// add uniform shader properties -- constants and textures
|
|||
foreach (AnyNodeProperty p in m_properties) |
|||
{ |
|||
// only exposed properties go in the property block
|
|||
if (p.state == AnyNodePropertyState.Exposed) |
|||
{ |
|||
PropertyChunk property = p.toPropertyChunk(); ; |
|||
if (property != null) |
|||
{ |
|||
visitor.AddShaderProperty(property); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode) |
|||
{ |
|||
foreach (AnyNodeProperty p in m_properties) |
|||
{ |
|||
// only exposed properties go in the property usage (hlsl declaration)
|
|||
if ((p.state == AnyNodePropertyState.Exposed) || |
|||
(p.state == AnyNodePropertyState.Constant && generationMode.IsPreview())) // constant properties are exposed in preview mode for fast iteration update
|
|||
{ |
|||
string typeDecl = p.getTypeDecl(precision); |
|||
if (typeDecl != null) |
|||
{ |
|||
visitor.AddShaderChunk(typeDecl + " " + p.name + ";", true); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
public bool RequiresViewDirectionTangentSpace() |
|||
{ |
|||
return (Array.FindIndex(m_globals, x => x == ShaderGlobal.TangentSpaceViewDirection) >= 0); |
|||
} |
|||
|
|||
public override void CollectPreviewMaterialProperties(List<PreviewProperty> property_list) |
|||
{ |
|||
base.CollectPreviewMaterialProperties(property_list); |
|||
|
|||
foreach (AnyNodeProperty p in m_properties) |
|||
{ |
|||
switch (p.propertyType) |
|||
{ |
|||
case PropertyType.Float: |
|||
case PropertyType.Vector2: |
|||
case PropertyType.Vector3: |
|||
case PropertyType.Vector4: |
|||
property_list.Add( |
|||
new PreviewProperty |
|||
{ |
|||
m_Name = p.name, |
|||
m_PropType = p.propertyType, |
|||
m_Vector4 = p.value |
|||
}); |
|||
break; |
|||
} |
|||
} |
|||
} |
|||
|
|||
|
|||
public string GetFunctionName() |
|||
{ |
|||
return "unity_any_" + node_name + "_" + precision; |
|||
} |
|||
|
|||
private string GetShaderGlobalName(ShaderGlobal g) |
|||
{ |
|||
string globalname = null; |
|||
switch (g) |
|||
{ |
|||
case ShaderGlobal.UV0: |
|||
globalname = ShaderGeneratorNames.GetUVName(UVChannel.uv0); |
|||
break; |
|||
case ShaderGlobal.UV1: |
|||
globalname = ShaderGeneratorNames.GetUVName(UVChannel.uv1); |
|||
break; |
|||
case ShaderGlobal.UV2: |
|||
globalname = ShaderGeneratorNames.GetUVName(UVChannel.uv2); |
|||
break; |
|||
case ShaderGlobal.UV3: |
|||
globalname = ShaderGeneratorNames.GetUVName(UVChannel.uv3); |
|||
break; |
|||
case ShaderGlobal.WorldSpaceNormal: |
|||
globalname = ShaderGeneratorNames.WorldSpaceNormal; |
|||
break; |
|||
case ShaderGlobal.WorldSpaceBitangent: |
|||
globalname = ShaderGeneratorNames.WorldSpaceBitangent; |
|||
break; |
|||
case ShaderGlobal.WorldSpaceTangent: |
|||
globalname = ShaderGeneratorNames.WorldSpaceTangent; |
|||
break; |
|||
case ShaderGlobal.WorldSpacePosition: |
|||
globalname = ShaderGeneratorNames.WorldSpacePosition; |
|||
break; |
|||
case ShaderGlobal.WorldSpaceViewDirection: |
|||
globalname = ShaderGeneratorNames.WorldSpaceViewDirection; |
|||
break; |
|||
case ShaderGlobal.TangentSpaceViewDirection: |
|||
globalname = ShaderGeneratorNames.TangentSpaceViewDirection; |
|||
break; |
|||
case ShaderGlobal.ScreenPosition: |
|||
globalname = ShaderGeneratorNames.ScreenPosition; |
|||
break; |
|||
case ShaderGlobal.VertexColor: |
|||
globalname = ShaderGeneratorNames.VertexColor; |
|||
break; |
|||
} |
|||
return globalname; |
|||
} |
|||
|
|||
private string GetShaderGlobalTypeDecl(ShaderGlobal g) |
|||
{ |
|||
string typeDecl = null; |
|||
switch (g) |
|||
{ |
|||
case ShaderGlobal.UV0: |
|||
case ShaderGlobal.UV1: |
|||
case ShaderGlobal.UV2: |
|||
case ShaderGlobal.UV3: |
|||
typeDecl = "float4"; |
|||
break; |
|||
case ShaderGlobal.WorldSpaceNormal: |
|||
case ShaderGlobal.WorldSpaceBitangent: |
|||
case ShaderGlobal.WorldSpaceTangent: |
|||
case ShaderGlobal.WorldSpacePosition: |
|||
case ShaderGlobal.WorldSpaceViewDirection: |
|||
case ShaderGlobal.TangentSpaceViewDirection: |
|||
typeDecl = "float3"; |
|||
break; |
|||
case ShaderGlobal.ScreenPosition: |
|||
typeDecl = "float2"; |
|||
break; |
|||
case ShaderGlobal.VertexColor: |
|||
typeDecl = "float4"; |
|||
break; |
|||
} |
|||
|
|||
return typeDecl; |
|||
} |
|||
|
|||
protected virtual string GetFunctionPrototype() |
|||
{ |
|||
string result = "inline void " + GetFunctionName() + "("; |
|||
|
|||
// inputs (slots & properties) first
|
|||
bool comma = false; |
|||
foreach (AnyNodeProperty p in m_properties) |
|||
{ |
|||
string typeDecl = p.getTypeDecl(precision); |
|||
result += "in " + typeDecl + " " + p.name + ", "; |
|||
comma = true; |
|||
} |
|||
|
|||
// then 'globals'
|
|||
foreach (ShaderGlobal g in m_globals) |
|||
{ |
|||
result += "in " + GetShaderGlobalTypeDecl(g) + " " + GetShaderGlobalName(g) + ", "; |
|||
} |
|||
|
|||
// then outputs
|
|||
foreach (AnyNodeSlot s in m_outputSlots) |
|||
{ |
|||
string typeDecl = s.getTypeDecl(precision); |
|||
result += "out " + typeDecl + " " + s.name + ", "; |
|||
comma = true; |
|||
} |
|||
|
|||
// remove last comma, if any
|
|||
if (comma) |
|||
{ |
|||
result= result.Remove(result.Length - 2); |
|||
} |
|||
|
|||
result += ")"; |
|||
|
|||
return result; |
|||
} |
|||
|
|||
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode) |
|||
{ |
|||
var outputString = new ShaderGenerator(); |
|||
outputString.AddShaderChunk(GetFunctionPrototype(), false); |
|||
outputString.AddShaderChunk("{", false); |
|||
outputString.Indent(); |
|||
outputString.AddShaderChunk(node_hlsl, false); |
|||
outputString.Deindent(); |
|||
outputString.AddShaderChunk("}", false); |
|||
|
|||
visitor.AddShaderChunk(outputString.GetShaderString(0), true); |
|||
} |
|||
|
|||
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode) |
|||
{ |
|||
var outputString = new ShaderGenerator(); |
|||
|
|||
outputString.AddShaderChunk("// AnyNode '" + node_name + "'", false); |
|||
|
|||
// declare and initialize output slot variables
|
|||
foreach (AnyNodeSlot s in m_outputSlots) |
|||
{ |
|||
var typeDecl = s.getTypeDecl(OutputPrecision.@float); // precision
|
|||
|
|||
if (typeDecl != null) |
|||
{ |
|||
outputString.AddShaderChunk( |
|||
typeDecl |
|||
+ " " |
|||
+ GetVariableNameForSlot(s.slotId) |
|||
+ " = 0;", false); // TODO non float type default value?
|
|||
} |
|||
} |
|||
|
|||
// open new context, in case our property names conflict with something
|
|||
outputString.AddShaderChunk("{", false); |
|||
outputString.Indent(); |
|||
|
|||
// add static declarations for contant properties
|
|||
foreach (AnyNodeProperty p in m_properties) |
|||
{ |
|||
if (p.state != AnyNodePropertyState.Constant || generationMode.IsPreview()) // except in preview mode...
|
|||
continue; |
|||
|
|||
string typeDecl= p.getTypeDecl(precision); |
|||
|
|||
if (typeDecl != null) |
|||
{ |
|||
switch (p.propertyType) |
|||
{ |
|||
case PropertyType.Color: |
|||
// TODO
|
|||
break; |
|||
case PropertyType.Texture: |
|||
// TODO
|
|||
break; |
|||
case PropertyType.Cubemap: |
|||
// TODO
|
|||
break; |
|||
case PropertyType.Float: |
|||
visitor.AddShaderChunk(typeDecl + " " + p.name + " = " + p.value.x + ";", true); |
|||
break; |
|||
case PropertyType.Vector2: |
|||
visitor.AddShaderChunk(typeDecl + " " + p.name + " = " + typeDecl + "(" + p.value.x + ", " + p.value.y + ");", true); |
|||
break; |
|||
case PropertyType.Vector3: |
|||
visitor.AddShaderChunk(typeDecl + " " + p.name + " = " + typeDecl + "(" + p.value.x + ", " + p.value.y + ", " + p.value.z + ");", true); |
|||
break; |
|||
case PropertyType.Vector4: |
|||
visitor.AddShaderChunk(typeDecl + " " + p.name + " = " + typeDecl + "(" + p.value.x + ", " + p.value.y + ", " + p.value.z + ", " + p.value.w + ");", true); |
|||
break; |
|||
case PropertyType.Matrix2: |
|||
case PropertyType.Matrix3: |
|||
case PropertyType.Matrix4: |
|||
// TODO
|
|||
|
|||
break; |
|||
} |
|||
} |
|||
} |
|||
|
|||
// call function
|
|||
outputString.AddShaderChunk(GetFunctionName() + "(", false); |
|||
outputString.Indent(); |
|||
outputString.AddShaderChunk("// input slots and properties", false); |
|||
|
|||
bool first = true; |
|||
foreach (AnyNodeProperty p in m_properties) |
|||
{ |
|||
string inputVariableName; |
|||
if (p.state == AnyNodePropertyState.Slot) |
|||
{ |
|||
inputVariableName = GetSlotValue(p.slotId, generationMode); |
|||
} |
|||
else |
|||
{ |
|||
// constant or property
|
|||
inputVariableName = p.name; |
|||
} |
|||
outputString.AddShaderChunk((first ? "" : ",") + inputVariableName, false); |
|||
first = false; |
|||
} |
|||
|
|||
outputString.AddShaderChunk("// global parameters", false); |
|||
foreach (ShaderGlobal g in m_globals) |
|||
{ |
|||
outputString.AddShaderChunk((first ? "" : ",") + GetShaderGlobalName(g), false); |
|||
first = false; |
|||
} |
|||
|
|||
outputString.AddShaderChunk("// output parameters", false); |
|||
foreach (AnyNodeSlot s in m_outputSlots) |
|||
{ |
|||
outputString.AddShaderChunk((first ? "" : ",") + GetVariableNameForSlot(s.slotId), false); |
|||
first = false; |
|||
} |
|||
|
|||
outputString.AddShaderChunk(");", false); // TODO: get rid of parameter hack
|
|||
outputString.Deindent(); |
|||
|
|||
outputString.Deindent(); |
|||
outputString.AddShaderChunk("}", false); |
|||
|
|||
// done splicing translated hlsl! yay
|
|||
visitor.AddShaderChunk(outputString.GetShaderString(0), true); |
|||
} |
|||
|
|||
public override bool hasPreview |
|||
{ |
|||
get { return true; } |
|||
} |
|||
|
|||
public override PreviewMode previewMode |
|||
{ |
|||
get |
|||
{ |
|||
return PreviewMode.Preview2D; |
|||
} |
|||
} |
|||
|
|||
bool RequiresMeshUV(UVChannel channel) |
|||
{ |
|||
// TODO
|
|||
return false; |
|||
} |
|||
|
|||
}*/ |
|||
} |
|||
|
|
|||
fileFormatVersion: 2 |
|||
guid: ce4472789956d684f89df3578d7d1d6d |
|||
timeCreated: 1495707441 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using RMGUI.GraphView; |
|||
using UnityEditor.Graphing.Drawing; |
|||
using UnityEditor; |
|||
using UnityEngine; |
|||
using UnityEngine.MaterialGraph; |
|||
|
|||
namespace UnityEditor.MaterialGraph.Drawing |
|||
{ |
|||
/* class AnyNodeControlPresenter : GraphControlPresenter |
|||
{ |
|||
public override void OnGUIHandler() |
|||
{ |
|||
base.OnGUIHandler(); |
|||
|
|||
var tNode = node as UnityEngine.MaterialGraph.AnyNodeBase; |
|||
if (tNode == null) |
|||
return; |
|||
|
|||
var properties = tNode.properties; |
|||
|
|||
EditorGUI.BeginChangeCheck(); |
|||
UnityEngine.Graphing.ModificationScope modificationScope = UnityEngine.Graphing.ModificationScope.Node; |
|||
|
|||
foreach (AnyNodeProperty p in properties) |
|||
{ |
|||
AnyNodePropertyState newState = (AnyNodePropertyState) EditorGUILayout.EnumPopup(p.name, p.state); |
|||
if (newState != p.state) |
|||
{ |
|||
tNode.setPropertyState(p, newState); |
|||
|
|||
modificationScope = UnityEngine.Graphing.ModificationScope.Graph; |
|||
} |
|||
|
|||
bool disabled = p.state == AnyNodePropertyState.Slot; |
|||
EditorGUI.BeginDisabledGroup(disabled); |
|||
{ |
|||
switch (p.propertyType) |
|||
{ |
|||
case PropertyType.Color: |
|||
// TODO
|
|||
break; |
|||
case PropertyType.Texture: |
|||
// TODO
|
|||
break; |
|||
case PropertyType.Cubemap: |
|||
// TODO
|
|||
break; |
|||
case PropertyType.Float: |
|||
p.value.x = EditorGUILayout.FloatField("", p.value.x); |
|||
break; |
|||
case PropertyType.Vector2: |
|||
{ |
|||
Vector2 result = EditorGUILayout.Vector2Field("", new Vector2(p.value.x, p.value.y)); |
|||
p.value.x = result.x; |
|||
p.value.y = result.y; |
|||
} |
|||
break; |
|||
case PropertyType.Vector3: |
|||
p.value = EditorGUILayout.Vector3Field("", p.value); |
|||
break; |
|||
case PropertyType.Vector4: |
|||
p.value = EditorGUILayout.Vector4Field("", p.value); |
|||
break; |
|||
case PropertyType.Matrix2: |
|||
// p.value = EditorGUILayout.Matrix2Field("", p.value);
|
|||
break; |
|||
case PropertyType.Matrix3: |
|||
// p.value = EditorGUILayout.Matrix3Field("", p.value);
|
|||
break; |
|||
case PropertyType.Matrix4: |
|||
// p.value = EditorGUILayout.Matrix4Field("", p.value);
|
|||
break; |
|||
} |
|||
} |
|||
EditorGUI.EndDisabledGroup(); |
|||
} |
|||
|
|||
bool changed= EditorGUI.EndChangeCheck(); |
|||
|
|||
if (changed) |
|||
{ |
|||
if (tNode.onModified != null) |
|||
tNode.onModified(tNode, modificationScope); |
|||
} |
|||
} |
|||
|
|||
public override float GetHeight() |
|||
{ |
|||
var tNode = node as UnityEngine.MaterialGraph.AnyNodeBase; |
|||
if (tNode == null) |
|||
return EditorGUIUtility.standardVerticalSpacing; |
|||
|
|||
return (EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing) * tNode.propertyCount * 2 |
|||
+ EditorGUIUtility.standardVerticalSpacing; |
|||
} |
|||
} |
|||
|
|||
[Serializable] |
|||
public class AnyNodePresenter : PropertyNodePresenter |
|||
{ |
|||
protected override IEnumerable<GraphElementPresenter> GetControlData() |
|||
{ |
|||
var instance = CreateInstance<AnyNodeControlPresenter>(); |
|||
instance.Initialize(node); |
|||
return new List<GraphElementPresenter>(base.GetControlData()) { instance }; |
|||
} |
|||
}*/ |
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue