浏览代码

Add WorldBitangentNode (with IMayRequireBitangent behavior)

/main
Paul Demeulenaere 8 年前
当前提交
c1f9ae7d
共有 4 个文件被更改,包括 82 次插入5 次删除
  1. 9
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/SubGraphNode.cs
  2. 18
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Util/ShaderGenerator.cs
  3. 48
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/WorldSpaceBitangentNode.cs
  4. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/WorldSpaceBitangentNode.cs.meta

9
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/SubGraphNode.cs


, IOnAssetEnabled
, IMayRequireNormal
, IMayRequireTangent
, IMayRequireBitangent
, IMayRequireMeshUV
, IMayRequireScreenPosition
, IMayRequireViewDirection

return false;
return subGraph.activeNodes.OfType<IMayRequireTangent>().Any(x => x.RequiresTangent());
}
public bool RequiresBitangent()
{
if (subGraph == null)
return false;
return subGraph.activeNodes.OfType<IMayRequireBitangent>().Any(x => x.RequiresBitangent());
}
}
}

18
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Util/ShaderGenerator.cs


public static class ShaderGeneratorNames
{
public const string WorldSpaceNormal = "worldSpaceNormal";
public const string WorldSpaceBitangent = "worldSpaceBitangent";
public const string WorldSpaceTangent = "worldSpaceTangent";
public const string WorldSpacePosition = "worldPosition";
public const string WorldSpaceViewDirection = "worldSpaceViewDirection";

vertexShaderBlock.AddShaderChunk("float4 screenPos = ComputeScreenPos(UnityObjectToClipPos(v.vertex));", true);
vertexShaderBlock.AddShaderChunk("float3 worldNormal = UnityObjectToWorldNormal(v.normal);", true);
bool bRequireBitangent = activeNodeList.OfType<IMayRequireBitangent>().Any(x => x.RequiresBitangent());
bool needsWorldPos = activeNodeList.OfType<IMayRequireViewDirection>().Any(x => x.RequiresViewDirection());
if (needsWorldPos || activeNodeList.OfType<IMayRequireWorldPosition>().Any(x => x.RequiresWorldPosition()))
{

}
if (activeNodeList.OfType<IMayRequireNormal>().Any(x => x.RequiresNormal()))
if (bRequireBitangent || activeNodeList.OfType<IMayRequireNormal>().Any(x => x.RequiresNormal()))
{
shaderInputVisitor.AddShaderChunk("float3 worldNormal : TEXCOORD1;", true);
vertexShaderBlock.AddShaderChunk("o.worldNormal = worldNormal;", true);

shaderBodyVisitor.AddShaderChunk("half4 " + ShaderGeneratorNames.ScreenPosition + " = IN.screenPos;", true);
}
if (activeNodeList.OfType<IMayRequireTangent>().Any(x => x.RequiresTangent()))
if (bRequireBitangent || activeNodeList.OfType<IMayRequireTangent>().Any(x => x.RequiresTangent()))
{
shaderInputVisitor.AddShaderChunk("float4 worldTangent : TEXCOORD4;", true);
vertexShaderBlock.AddShaderChunk("o.worldTangent = float4(UnityObjectToWorldDir(v.tangent.xyz), v.tangent.w);", true);
shaderBodyVisitor.AddShaderChunk("float3 " + ShaderGeneratorNames.WorldSpaceTangent + " = normalize(IN.worldTangent.xyz);", true);
}
if (bRequireBitangent)
shaderInputVisitor.AddShaderChunk("float3 worldTangent : TEXCOORD4;", true);
vertexShaderBlock.AddShaderChunk("o.worldTangent = UnityObjectToWorldDir(v.tangent.xyz);", true);
shaderBodyVisitor.AddShaderChunk("float3 " + ShaderGeneratorNames.WorldSpaceTangent + " = normalize(IN.worldTangent);", true);
shaderBodyVisitor.AddShaderChunk(string.Format("float3 {0} = cross({1}, {2}) * IN.worldTangent.w;", ShaderGeneratorNames.WorldSpaceBitangent, ShaderGeneratorNames.WorldSpaceNormal, ShaderGeneratorNames.WorldSpaceTangent), true);
}
var generationMode = GenerationMode.Preview;

48
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/WorldSpaceBitangentNode.cs


using UnityEngine.Graphing;
namespace UnityEngine.MaterialGraph
{
public interface IMayRequireBitangent
{
bool RequiresBitangent();
}
[Title("Input/World Bitangent Node")]
public class WorldSpaceBitangentNode : AbstractMaterialNode, IMayRequireBitangent
{
public const int kOutputSlotId = 0;
public const string kOutputSlotName = "Bitangent";
public WorldSpaceBitangentNode()
{
name = "World Bitangent";
UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new MaterialSlot(kOutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, SlotValueType.Vector3, new Vector4(0, 0, 1, 1)));
RemoveSlotsNameNotMatching(new[] { kOutputSlotId });
}
public override bool hasPreview
{
get { return true; }
}
public override PreviewMode previewMode
{
get { return PreviewMode.Preview3D; }
}
public override string GetVariableNameForSlot(int slotId)
{
return ShaderGeneratorNames.WorldSpaceBitangent;
}
public bool RequiresBitangent()
{
return true;
}
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/WorldSpaceBitangentNode.cs.meta


fileFormatVersion: 2
guid: 25ded6c13e7523d48935f8bd1b9afcb6
timeCreated: 1481041579
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存