浏览代码

SphericalIndentation node, and adding ability to source shader globals in Any nodes

/main
ChrisTchou 7 年前
当前提交
189f30e4
共有 10 个文件被更改,包括 224 次插入18 次删除
  1. 1
      MaterialGraphProject/Assets/NewNodes/WIP/AACheckerBoardNode.cs
  2. 144
      MaterialGraphProject/Assets/NewNodes/WIP/AnyNode.cs
  3. 1
      MaterialGraphProject/Assets/NewNodes/WIP/RadialShearNode.cs
  4. 2
      MaterialGraphProject/Assets/NewNodes/WIP/ScaleOffsetNode.cs
  5. 2
      MaterialGraphProject/Assets/NewNodes/WIP/SphereWarpNode.cs
  6. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/MaterialGraphPresenter.cs
  7. 69
      MaterialGraphProject/Assets/NewNodes/WIP/SphericalIndentationNode.cs
  8. 12
      MaterialGraphProject/Assets/NewNodes/WIP/SphericalIndentationNode.cs.meta
  9. 1
      MaterialGraphProject/Assets/SphereDent.ShaderGraph
  10. 9
      MaterialGraphProject/Assets/SphereDent.ShaderGraph.meta

1
MaterialGraphProject/Assets/NewNodes/WIP/AACheckerBoardNode.cs


};
}
}
public ShaderGlobal[] globals { get { return new ShaderGlobal[] { }; } }
public string hlsl
{

144
MaterialGraphProject/Assets/NewNodes/WIP/AnyNode.cs


namespace UnityEngine.MaterialGraph
{
public enum ShaderGlobal
{
UV0,
UV1,
UV2,
UV3,
WorldSpaceNormal,
WorldSpaceBitangent,
WorldSpaceTangent,
WorldSpacePosition,
WorldSpaceViewDirection,
TangentSpaceViewDirection,
ScreenPosition,
VertexColor,
}
[Serializable]
public class AnyNodeSlot
{

{
switch (slotValueType)
{
case SlotValueType.sampler2D:
case SlotValueType.Sampler2D:
// TODO
break;
case SlotValueType.Dynamic:

slotValueType = SlotValueType.Vector4;
break;
case PropertyType.Texture:
slotValueType = SlotValueType.sampler2D;
slotValueType = SlotValueType.Sampler2D;
slotValueType = SlotValueType.sampler2D;
slotValueType = SlotValueType.Sampler2D;
break;
case PropertyType.Float:
slotValueType = SlotValueType.Vector1;

string name { get; }
AnyNodeProperty[] properties { get; }
AnyNodeSlot[] outputs { get; }
ShaderGlobal[] globals { get; }
string hlsl { get; }
}

[SerializeField]
protected AnyNodeSlot[] m_outputSlots;
[SerializeField]
protected ShaderGlobal[] m_globals;
public IEnumerable<AnyNodeProperty> properties
{

AnyNodeBase
, IGeneratesBodyCode
, IGeneratesFunction
// , IMayRequireMeshUV
// , IMayRequireNormal // TODO
// , IMayRequireTangent
// , IMayRequireBitangent
// , IMayRequireScreenPosition
// , IMayRequireViewDirection
// , IMayRequireWorldPosition
// , IMayRequireVertexColor
, IMayRequireViewDirectionTangentSpace
/* , IMayRequireMeshUV
, IOnAssetEnabled // TODO
, IMayRequireNormal // TODO
, IMayRequireTangent
, IMayRequireBitangent
, IMayRequireScreenPosition
, IMayRequireViewDirection
, IMayRequireWorldPosition
, IMayRequireVertexColor
, IMayRequireViewDirectionTangentSpace
*/
where DEFINITION : IAnyNodeDefinition, new()
{
private DEFINITION m_definition;

m_properties = m_definition.properties;
m_outputSlots = m_definition.outputs;
m_globals = m_definition.globals;
UpdateNodeAfterDeserialization();
}

// 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>();

}
}
}
}
public bool RequiresViewDirectionTangentSpace()
{
return (Array.FindIndex(m_globals, x => x == ShaderGlobal.TangentSpaceViewDirection) >= 0);
}
public override void CollectPreviewMaterialProperties(List<PreviewProperty> property_list)

// properties.AddRange(subGraph.GetPreviewProperties()); // ???
switch (p.propertyType)
{
case PropertyType.Float:

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() + "(";

}
// then 'globals'
// TODO
foreach (ShaderGlobal g in m_globals)
{
result += "in " + GetShaderGlobalTypeDecl(g) + " " + GetShaderGlobalName(g) + ", ";
}
// then outputs
foreach (AnyNodeSlot s in m_outputSlots)

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;
}

1
MaterialGraphProject/Assets/NewNodes/WIP/RadialShearNode.cs


};
}
}
public ShaderGlobal[] globals { get { return new ShaderGlobal[] { }; } }
public string hlsl
{

2
MaterialGraphProject/Assets/NewNodes/WIP/ScaleOffsetNode.cs


}
}
public ShaderGlobal[] globals { get { return new ShaderGlobal[] { }; } }
public string hlsl
{
get

2
MaterialGraphProject/Assets/NewNodes/WIP/SphereWarpNode.cs


}
}
public ShaderGlobal[] globals { get { return new ShaderGlobal[]{ }; } }
public string hlsl
{
get

1
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/MaterialGraphPresenter.cs


typeMapper[typeof(ScaleOffsetNode)] = typeof(AnyNodePresenter); // anything derived from AnyNode should use the AnyNodePresenter
typeMapper[typeof(RadialShearNode)] = typeof(AnyNodePresenter); // anything derived from AnyNode should use the AnyNodePresenter
typeMapper[typeof(SphereWarpNode)] = typeof(AnyNodePresenter); // anything derived from AnyNode should use the AnyNodePresenter
typeMapper[typeof(SphericalIndentationNode)] = typeof(AnyNodePresenter); // anything derived from AnyNode should use the AnyNodePresenter
typeMapper[typeof(AACheckerboardNode)] = typeof(AnyNodePresenter); // anything derived from AnyNode should use the AnyNodePresenter
typeMapper[typeof(SubGraphNode)] = typeof(SubgraphNodePresenter);
typeMapper[typeof(RemapMasterNode)] = typeof(RemapMasterNodePresenter);

69
MaterialGraphProject/Assets/NewNodes/WIP/SphericalIndentationNode.cs


using UnityEngine.Graphing;
namespace UnityEngine.MaterialGraph
{
[Title("UV/SphericalIndentation")]
public class SphericalIndentationNode : AnyNode<SphericalIndentationNode.Definition>
{
public class Definition : IAnyNodeDefinition
{
public string name { get { return "SphericalIndentation"; } }
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 = "center", description = "UV center point", propertyType = PropertyType.Vector2, value= new Vector4(0.5f, 0.5f, 0.5f, 0.5f), state = AnyNodePropertyState.Constant },
new AnyNodeProperty { slotId= 2, name = "height", description = "Height off surface", propertyType = PropertyType.Float, value= Vector4.zero, state = AnyNodePropertyState.Constant },
new AnyNodeProperty { slotId= 3, name = "radius", description = "Radius", propertyType = PropertyType.Float, value= Vector4.one, state = AnyNodePropertyState.Constant },
};
}
}
public AnyNodeSlot[] outputs
{
get
{
return new AnyNodeSlot[]
{
new AnyNodeSlot { slotId= 4, name = "outUVs", description = "Output UV texture coordinates", slotValueType = SlotValueType.Vector2, value = Vector4.zero },
new AnyNodeSlot { slotId= 5, name = "outNormal", description = "Output Normal in tangent space", slotValueType = SlotValueType.Vector3, value = Vector4.zero }
};
}
}
public ShaderGlobal[] globals { get { return new ShaderGlobal[] { ShaderGlobal.TangentSpaceViewDirection }; } }
public string hlsl
{
get
{
return
"float radius2= radius*radius;\n" +
"float3 cur= float3(inUVs.xy, 0.0f);\n" +
"float3 sphereCenter = float3(center, height);\n" +
"float3 edgeA = sphereCenter - cur;\n" +
"float a2 = dot(edgeA, edgeA);\n" +
"outUVs= inUVs;\n" +
"outNormal= float3(0.0f, 0.0f, 1.0f);\n" +
"if (a2 < radius2)\n" +
"{\n" +
" float a = sqrt(a2);\n" +
" edgeA = edgeA / a;\n" +
" float cosineR = dot(edgeA, tangentSpaceViewDirection.xyz);\n" +
" float x = cosineR * a - sqrt(-a2 + radius2 + a2 * cosineR * cosineR);\n" +
" float3 intersectedEdge = cur + tangentSpaceViewDirection * x;\n" +
" outNormal= normalize(sphereCenter - intersectedEdge);\n" +
" outUVs = intersectedEdge.xy;\n" +
"}\n";
}
}
}
}
}

12
MaterialGraphProject/Assets/NewNodes/WIP/SphericalIndentationNode.cs.meta


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

1
MaterialGraphProject/Assets/SphereDent.ShaderGraph
文件差异内容过多而无法显示
查看文件

9
MaterialGraphProject/Assets/SphereDent.ShaderGraph.meta


fileFormatVersion: 2
guid: b63da350811f7ba48914e852a299204c
timeCreated: 1495747830
licenseType: Pro
ScriptedImporter:
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 11500000, guid: 625f186215c104763be7675aa2d941aa, type: 3}
正在加载...
取消
保存