浏览代码

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 次删除
  1. 8
      com.unity.shadergraph/CHANGELOG.md
  2. 13
      com.unity.shadergraph/Editor/Data/Nodes/Artistic/Normal/NormalFromTextureNode.cs
  3. 3
      com.unity.shadergraph/.data/normal_derive_nodes.PNG
  4. 131
      com.unity.shadergraph/Editor/Data/Nodes/Artistic/Normal/NormalFromHeightNode.cs
  5. 11
      com.unity.shadergraph/Editor/Data/Nodes/Artistic/Normal/NormalFromHeightNode.cs.meta
  6. 39
      com.unity.shadergraph/Editor/Data/Nodes/Artistic/Normal/NormalReconstructZNode.cs
  7. 11
      com.unity.shadergraph/Editor/Data/Nodes/Artistic/Normal/NormalReconstructZNode.cs.meta
  8. 0
      /com.unity.shadergraph/Editor/Data/Nodes/Artistic/Normal/NormalFromTextureNode.cs.meta
  9. 0
      /com.unity.shadergraph/Editor/Data/Nodes/Artistic/Normal/NormalFromTextureNode.cs

8
com.unity.shadergraph/CHANGELOG.md


This adds gradient functionality via two new nodes. The Sample Gradient node samples a gradient given a Time parameter. You can define this gradient on the Gradient slot control view. The Gradient Asset node defines a gradient that can be sampled by multiple Sample Gradient nodes using different Time parameters.
## Normal Derive Nodes
![](.data/normal_derive_nodes.png)
There are two Normal Derive Nodes: `Normal From Height` and `Normal Reconstruct Z`.
`Normal From Height` uses Vector1 input to derive a normal map. `Normal Reconstruct Z` uses the X and Y components in Vector2 input to derive the proper Z value for a normal map.
### Sphere Mask Node
![](.data/sphereMask.png)

- If the current render pipeline is not compatible, master nodes now display an error badge.
- The preview shader now only considers the current render pipeline. Because of this there is less code to compile, and therefore the preview shader will compile faster.
- When you rename a shader graph or sub shader graph locally on your disk, the title of the Shader Graph window, black board, and preview also updates.
- `Normal Create` node has been renamed to `Normal From Texture`.

13
com.unity.shadergraph/Editor/Data/Nodes/Artistic/Normal/NormalFromTextureNode.cs


namespace UnityEditor.ShaderGraph
{
[Title("Artistic", "Normal", "Normal Create")]
public class NormalCreateNode : AbstractMaterialNode, IGeneratesBodyCode, IGeneratesFunction, IGenerateProperties, IMayRequireMeshUV
[FormerName("UnityEditor.ShaderGraph.NormalCreateNode")]
[Title("Artistic", "Normal", "Normal From Texture")]
public class NormalFromTextureNode : AbstractMaterialNode, IGeneratesBodyCode, IGeneratesFunction, IGenerateProperties, IMayRequireMeshUV
{
public const int TextureInputId = 0;
public const int UVInputId = 1;

const string k_StrengthInputName = "Strength";
const string k_OutputSlotName = "Out";
public NormalCreateNode()
public NormalFromTextureNode()
name = "Normal Create";
name = "Normal From Texture";
get { return "https://github.com/Unity-Technologies/ShaderGraph/wiki/Normal-Create-Node"; }
get { return "https://github.com/Unity-Technologies/ShaderGraph/wiki/Normal-From-Texture-Node"; }
return string.Format("Unity_NormalCreate_{0}", precision);
return string.Format("Unity_NormalFromTexture_{0}", precision);
}
public override bool hasPreview { get { return true; } }

3
com.unity.shadergraph/.data/normal_derive_nodes.PNG


version https://git-lfs.github.com/spec/v1
oid sha256:2c14e830338ddf74ff5479bb56e8aa4a4d0f9274e96390d9897d97228f281d8e
size 54715

131
com.unity.shadergraph/Editor/Data/Nodes/Artistic/Normal/NormalFromHeightNode.cs


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

11
com.unity.shadergraph/Editor/Data/Nodes/Artistic/Normal/NormalFromHeightNode.cs.meta


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

39
com.unity.shadergraph/Editor/Data/Nodes/Artistic/Normal/NormalReconstructZNode.cs


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

11
com.unity.shadergraph/Editor/Data/Nodes/Artistic/Normal/NormalReconstructZNode.cs.meta


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

/com.unity.shadergraph/Editor/Data/Nodes/Artistic/Normal/NormalCreateNode.cs.meta → /com.unity.shadergraph/Editor/Data/Nodes/Artistic/Normal/NormalFromTextureNode.cs.meta

/com.unity.shadergraph/Editor/Data/Nodes/Artistic/Normal/NormalCreateNode.cs → /com.unity.shadergraph/Editor/Data/Nodes/Artistic/Normal/NormalFromTextureNode.cs

正在加载...
取消
保存