浏览代码
Added Normal Derive Nodes (#1550)
Added Normal Derive Nodes (#1550)
* adding normal derive nodes * updates to changelog and renaming normal nodes for clarity * updated file names * changed float to precision on normal reconstruct z node/main
Matt Dean
7 年前
当前提交
cef782d1
共有 9 个文件被更改,包括 210 次插入 和 6 次删除
-
8com.unity.shadergraph/CHANGELOG.md
-
13com.unity.shadergraph/Editor/Data/Nodes/Artistic/Normal/NormalFromTextureNode.cs
-
3com.unity.shadergraph/.data/normal_derive_nodes.PNG
-
131com.unity.shadergraph/Editor/Data/Nodes/Artistic/Normal/NormalFromHeightNode.cs
-
11com.unity.shadergraph/Editor/Data/Nodes/Artistic/Normal/NormalFromHeightNode.cs.meta
-
39com.unity.shadergraph/Editor/Data/Nodes/Artistic/Normal/NormalReconstructZNode.cs
-
11com.unity.shadergraph/Editor/Data/Nodes/Artistic/Normal/NormalReconstructZNode.cs.meta
-
0/com.unity.shadergraph/Editor/Data/Nodes/Artistic/Normal/NormalFromTextureNode.cs.meta
-
0/com.unity.shadergraph/Editor/Data/Nodes/Artistic/Normal/NormalFromTextureNode.cs
|
|||
version https://git-lfs.github.com/spec/v1 |
|||
oid sha256:2c14e830338ddf74ff5479bb56e8aa4a4d0f9274e96390d9897d97228f281d8e |
|||
size 54715 |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using UnityEditor.Graphing; |
|||
using UnityEditor.ShaderGraph.Drawing.Controls; |
|||
using UnityEngine; |
|||
|
|||
namespace UnityEditor.ShaderGraph |
|||
{ |
|||
|
|||
public enum OutputSpace |
|||
{ |
|||
Tangent, |
|||
World |
|||
}; |
|||
|
|||
[Title("Artistic", "Normal", "Normal From Height")] |
|||
public class NormalFromHeightNode : AbstractMaterialNode, IGeneratesBodyCode, IGeneratesFunction, IMayRequireTangent, IMayRequireBitangent, IMayRequireNormal, IMayRequirePosition |
|||
{ |
|||
public NormalFromHeightNode() |
|||
{ |
|||
name = "Normal From Height"; |
|||
UpdateNodeAfterDeserialization(); |
|||
} |
|||
|
|||
public override string documentationURL |
|||
{ |
|||
get { return "https://github.com/Unity-Technologies/ShaderGraph/wiki/Normal-From-Heightmap-Node"; } |
|||
} |
|||
|
|||
[SerializeField] |
|||
private OutputSpace m_OutputSpace = OutputSpace.Tangent; |
|||
|
|||
[EnumControl("Output Space")] |
|||
public OutputSpace outputSpace |
|||
{ |
|||
get { return m_OutputSpace; } |
|||
set |
|||
{ |
|||
if (m_OutputSpace == value) |
|||
return; |
|||
|
|||
m_OutputSpace = value; |
|||
Dirty(ModificationScope.Graph); |
|||
} |
|||
} |
|||
|
|||
const int InputSlotId = 0; |
|||
const int OutputSlotId = 1; |
|||
const string kInputSlotName = "In"; |
|||
const string kOutputSlotName = "Out"; |
|||
|
|||
public override bool hasPreview |
|||
{ |
|||
get { return true; } |
|||
} |
|||
|
|||
string GetFunctionName() |
|||
{ |
|||
return string.Format("Unity_NormalFromHeight_{0}", outputSpace.ToString()); |
|||
} |
|||
|
|||
public sealed override void UpdateNodeAfterDeserialization() |
|||
{ |
|||
AddSlot(new Vector1MaterialSlot(InputSlotId, kInputSlotName, kInputSlotName, SlotType.Input, 0)); |
|||
AddSlot(new Vector3MaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, Vector4.zero)); |
|||
RemoveSlotsNameNotMatching(new[] { InputSlotId, OutputSlotId }); |
|||
} |
|||
|
|||
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode) |
|||
{ |
|||
var sb = new ShaderStringBuilder(); |
|||
|
|||
var inputValue = GetSlotValue(InputSlotId, generationMode); |
|||
var outputValue = GetSlotValue(OutputSlotId, generationMode); |
|||
sb.AppendLine("{0} {1};", FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType.ToString(precision), GetVariableNameForSlot(OutputSlotId)); |
|||
sb.AppendLine("{0}3x3 _{1}_TangentMatrix = {0}3x3(IN.{2}SpaceTangent, IN.{2}SpaceBiTangent, IN.{2}SpaceNormal);", precision, GetVariableNameForNode(), NeededCoordinateSpace.World.ToString()); |
|||
sb.AppendLine("{0}3 _{1}_Position = IN.{2}SpacePosition;", precision, GetVariableNameForNode(), NeededCoordinateSpace.World.ToString()); |
|||
|
|||
sb.AppendLine("{0}({1},_{2}_Position,_{2}_TangentMatrix, {3});", GetFunctionName(), inputValue, GetVariableNameForNode(), outputValue); |
|||
|
|||
visitor.AddShaderChunk(sb.ToString(), false); |
|||
} |
|||
|
|||
public void GenerateNodeFunction(FunctionRegistry registry, GraphContext graphContext, GenerationMode generationMode) |
|||
{ |
|||
registry.ProvideFunction(GetFunctionName(), s => |
|||
{ |
|||
s.AppendLine("void {0}({2} In, {1}3 Position, {1}3x3 TangentMatrix, out {3} Out)", |
|||
GetFunctionName(), |
|||
precision, |
|||
FindInputSlot<MaterialSlot>(InputSlotId).concreteValueType.ToString(precision), |
|||
FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType.ToString(precision)); |
|||
using (s.BlockScope()) |
|||
{ |
|||
s.AppendLine("{0}3 worldDirivativeX = ddx(Position * 100);", precision); |
|||
s.AppendLine("{0}3 worldDirivativeY = ddy(Position * 100);", precision); |
|||
s.AppendNewLine(); |
|||
s.AppendLine("{0}3 crossX = cross(TangentMatrix[2].xyz, worldDirivativeX);", precision); |
|||
s.AppendLine("{0}3 crossY = cross(TangentMatrix[2].xyz, worldDirivativeY);", precision); |
|||
s.AppendLine("{0}3 d = abs(dot(crossY, worldDirivativeX));", precision); |
|||
s.AppendLine("{0}3 inToNormal = ((((In + ddx(In)) - In) * crossY) + (((In + ddy(In)) - In) * crossX)) * sign(d);", precision); |
|||
s.AppendLine("inToNormal.y *= -1.0;", precision); |
|||
s.AppendNewLine(); |
|||
s.AppendLine("Out = normalize((d * TangentMatrix[2].xyz) - inToNormal);", precision); |
|||
|
|||
if(outputSpace == OutputSpace.Tangent) |
|||
s.AppendLine("Out = TransformWorldToTangent(Out, TangentMatrix);"); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
public NeededCoordinateSpace RequiresTangent(ShaderStageCapability stageCapability) |
|||
{ |
|||
return NeededCoordinateSpace.World; |
|||
} |
|||
|
|||
public NeededCoordinateSpace RequiresBitangent(ShaderStageCapability stageCapability) |
|||
{ |
|||
return NeededCoordinateSpace.World; |
|||
} |
|||
|
|||
public NeededCoordinateSpace RequiresNormal(ShaderStageCapability stageCapability) |
|||
{ |
|||
return NeededCoordinateSpace.World; |
|||
} |
|||
public NeededCoordinateSpace RequiresPosition(ShaderStageCapability stageCapability) |
|||
{ |
|||
return NeededCoordinateSpace.World; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 3bf617c1fe220684696e5bf3850bc423 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System.Reflection; |
|||
using UnityEngine; |
|||
|
|||
namespace UnityEditor.ShaderGraph |
|||
{ |
|||
[Title("Artistic", "Normal", "Normal Reconstruct Z")] |
|||
public class NormalReconstructZNode : CodeFunctionNode |
|||
{ |
|||
public NormalReconstructZNode() |
|||
{ |
|||
name = "Normal Reconstruct Z"; |
|||
} |
|||
|
|||
public override string documentationURL |
|||
{ |
|||
get { return "https://github.com/Unity-Technologies/ShaderGraph/wiki/Normal-Reconstruct-Z-Node"; } |
|||
} |
|||
|
|||
protected override MethodInfo GetFunctionToConvert() |
|||
{ |
|||
return GetType().GetMethod("NormalReconstructZ", BindingFlags.Static | BindingFlags.NonPublic); |
|||
} |
|||
|
|||
static string NormalReconstructZ( |
|||
[Slot(0, Binding.None)] Vector2 In, |
|||
[Slot(2, Binding.None, ShaderStageCapability.Fragment)] out Vector3 Out) |
|||
|
|||
{ |
|||
Out = Vector3.zero; |
|||
return |
|||
@"
|
|||
{ |
|||
{precision} reconstructZ = sqrt(1 - ( In.x * In.x + In.y * In.y)); |
|||
{precision}3 normalVector = {precision}3(In.x, In.y, reconstructZ); |
|||
Out = normalize(normalVector); |
|||
}";
|
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 27615ef3e2e760b45b15895bd39d0954 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue