浏览代码

Adding Heightmap to Normalmap node. Fixing some bugs with function N

/main
Eduardo Chaves 8 年前
当前提交
e2223047
共有 5 个文件被更改,包括 90 次插入6 次删除
  1. 2
      MaterialGraphProject/Assets/Eduardo/EduardoTestGraph.ShaderGraph
  2. 7
      MaterialGraphProject/Assets/Eduardo/FunctionNInNOut.cs
  3. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbstractMaterialNode.cs
  4. 63
      MaterialGraphProject/Assets/Eduardo/HeightToNormalNode.cs
  5. 12
      MaterialGraphProject/Assets/Eduardo/HeightToNormalNode.cs.meta

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

7
MaterialGraphProject/Assets/Eduardo/FunctionNInNOut.cs


else
nextSlotId = GetInputSlots<MaterialSlot>().Count() + 1;
AddSlot(new MaterialSlot(nextSlotId, displayName, nameInShader, slotType, valueType, defaultValue, true));
bool useDefaultValue = (valueType != SlotValueType.sampler2D);
AddSlot(new MaterialSlot(nextSlotId, displayName, nameInShader, slotType, valueType, defaultValue, useDefaultValue));
return nextSlotId;
}

if (inSlot.isOutputSlot)
param += "out ";
param += precision + GetSlotTypeName(inSlot.id) + " ";
if (FindSlot<MaterialSlot>(inSlot.id).concreteValueType != ConcreteSlotValueType.sampler2D)
param += precision;
param += GetSlotTypeName(inSlot.id) + " ";
param += GetShaderOutputName(inSlot.id);
if (remainingParams > 1)

12
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbstractMaterialNode.cs


if (inputSlot == null)
return string.Empty;
string sufix = "";
var edges = owner.GetEdges(inputSlot.slotReference).ToArray();
if (edges.Any())

if (slot == null)
return string.Empty;
return ShaderGenerator.AdaptNodeOutput(fromNode, slot.id, slot.concreteValueType);
if (slot.concreteValueType == ConcreteSlotValueType.sampler2D)
sufix += "_Uniform";
return ShaderGenerator.AdaptNodeOutput(fromNode, slot.id, slot.concreteValueType) + sufix;
return inputSlot.GetDefaultValue(generationMode);
if (inputSlot.concreteValueType == ConcreteSlotValueType.sampler2D)
sufix += "_Uniform";
return inputSlot.GetDefaultValue(generationMode) + sufix;
}
private ConcreteSlotValueType FindCommonChannelType(ConcreteSlotValueType from, ConcreteSlotValueType to)

case ConcreteSlotValueType.Vector4:
return "4";
case ConcreteSlotValueType.sampler2D:
return "5";
return "sampler2D";
default:
return "Error";
}

63
MaterialGraphProject/Assets/Eduardo/HeightToNormalNode.cs


using UnityEngine.Graphing;
using System.Linq;
using System.Collections;
namespace UnityEngine.MaterialGraph
{
[Title("HeightToNormal")]
public class HeightToNormalNode : FunctionNInNOut, IGeneratesFunction
{
public HeightToNormalNode()
{
name = "HeightToNormal";
AddSlot("HeightMap", "heightmap", Graphing.SlotType.Input, SlotValueType.sampler2D, Vector4.zero);
AddSlot("UV", "texCoord", Graphing.SlotType.Input, SlotValueType.Vector2, Vector4.zero);
AddSlot("Offset", "texOffset", Graphing.SlotType.Input, SlotValueType.Vector1, new Vector4(0.005f, 0,0,0));
AddSlot("Strength", "strength", Graphing.SlotType.Input, SlotValueType.Vector1, new Vector4(8,0,0,0));
AddSlot("Normal", "normalRes", Graphing.SlotType.Output, SlotValueType.Vector3, Vector4.zero);
UpdateNodeAfterDeserialization();
}
protected override string GetFunctionName()
{
return "unity_ObjectScale";
}
public override bool hasPreview
{
get { return true; }
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{
var outputString = new ShaderGenerator();
outputString.AddShaderChunk(GetFunctionPrototype(), false);
outputString.AddShaderChunk("{", false);
outputString.AddShaderChunk("float2 offsetU = float2(texCoord.x + texOffset, texCoord.y);", false);
outputString.AddShaderChunk("float2 offsetV = float2(texCoord.x, texCoord.y + texOffset);", false);
outputString.AddShaderChunk("float normalSample = tex2D(heightmap, texCoord).r;", false);
outputString.AddShaderChunk("float uSample = tex2D(heightmap, offsetU).r;", false);
outputString.AddShaderChunk("float vSample = tex2D(heightmap, offsetV).r;", false);
outputString.AddShaderChunk("float uMinusNormal = uSample - normalSample;", false);
outputString.AddShaderChunk("float vMinusNormal = vSample - normalSample;", false);
outputString.AddShaderChunk("uMinusNormal = uMinusNormal * strength;", false);
outputString.AddShaderChunk("vMinusNormal = vMinusNormal * strength;", false);
outputString.AddShaderChunk("float3 va = float3(1, 0, uMinusNormal);", false);
outputString.AddShaderChunk("float3 vb = float3(0, 1, vMinusNormal);", false);
outputString.AddShaderChunk("normalRes = cross(va, vb);", false);
outputString.AddShaderChunk("}", false);
visitor.AddShaderChunk(outputString.GetShaderString(0), true);
}
}
}

12
MaterialGraphProject/Assets/Eduardo/HeightToNormalNode.cs.meta


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