浏览代码

Rewrote triplanar to work with the Sampler Asset (some master node issue where Texture2D would fail with no errors). Graphs which don't seem to work should have TriPlanar, SamplerAsset, and TextureAsset deleted and re-added.

/main
vlad 7 年前
当前提交
d0c30f9a
共有 2 个文件被更改,包括 117 次插入1 次删除
  1. 3
      MaterialGraphProject/Assets/Eduardo/FunctionNInNOut.cs
  2. 115
      MaterialGraphProject/Assets/Vlad/UVTriPlanar.cs

3
MaterialGraphProject/Assets/Eduardo/FunctionNInNOut.cs


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

if (FindSlot<MaterialSlot>(inSlot.id).concreteValueType != ConcreteSlotValueType.Texture2D
&& FindSlot<MaterialSlot>(inSlot.id).concreteValueType != ConcreteSlotValueType.SamplerState
&& FindSlot<MaterialSlot>(inSlot.id).concreteValueType != ConcreteSlotValueType.Sampler2D
)
param += precision;
param += GetSlotTypeName(inSlot.id) + " ";

115
MaterialGraphProject/Assets/Vlad/UVTriPlanar.cs


private string slot0Name = "texRef";
private string slot1Name = "tileFactor";
private string slot2Name = "blendFactor";
private string slot4Name = "normalRef";
private string slot5Name = "posRef";
private string slot6Name = "outputRef";
protected override string GetFunctionName()
{
return "unity_triplanar_" + precision;
}
public UVTriPlanar()
{
name = "UVTriPlanar";
slot0 = AddSlot("Texture", slot0Name, Graphing.SlotType.Input, SlotValueType.Sampler2D, Vector4.zero);
slot1 = AddSlot("Tile", slot1Name, Graphing.SlotType.Input, SlotValueType.Vector1, Vector4.zero);
slot2 = AddSlot("Blend", slot2Name, Graphing.SlotType.Input, SlotValueType.Vector1, Vector4.zero);
slot4 = AddSlot("Normals", slot4Name, Graphing.SlotType.Input, SlotValueType.Vector3, Vector3.one);
slot5 = AddSlot("Position", slot5Name, Graphing.SlotType.Input, SlotValueType.Vector3, Vector3.one);
slot6 = AddSlot("RGBA ", slot6Name, Graphing.SlotType.Output, SlotValueType.Vector4, Vector4.zero);
UpdateNodeAfterDeserialization();
}
public override bool hasPreview
{
get { return true; }
}
public override PreviewMode previewMode
{
get
{
return PreviewMode.Preview3D;
}
}
//TODO:Externalize
//Reference code from:http://www.chilliant.com/rgb2hsv.html
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{
var outputString = new ShaderGenerator();
// outputString.AddShaderChunk(GetFunctionPrototype("arg1", "arg2", "arg3", samplerName), false);
outputString.AddShaderChunk(GetFunctionPrototype(), false);
outputString.Indent();
outputString.AddShaderChunk("{", false);
// create UVs from position
outputString.AddShaderChunk("float3 uvs = " + slot5Name + "*" + slot1Name + ";", false);
// use absolute value of normal as texture weights
outputString.AddShaderChunk("half3 blend = pow(abs(" + slot4Name + ")," + slot2Name + ");", false);
// make sure the weights sum up to 1 (divide by sum of x+y+z)
outputString.AddShaderChunk("blend /= dot(blend, 1.0);", false);
// read the three texture projections, for x,y,z axes
outputString.AddShaderChunk("float4 cx = " + "tex2D(" + slot0Name + ", uvs.yz);", false);
outputString.AddShaderChunk("float4 cy = " + "tex2D(" + slot0Name + ", uvs.xz);", false);
outputString.AddShaderChunk("float4 cz = " + "tex2D(" + slot0Name + ", uvs.xy);", false);
// blend the textures based on weights
outputString.AddShaderChunk(slot6Name + " = cx * blend.x + cy * blend.y + cz * blend.z;", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
visitor.AddShaderChunk(outputString.GetShaderString(0), true);
}
//prevent validation errors when a sampler2D input is missing
//use on any input requiring a TextureAssetNode
public override void ValidateNode()
{
base.ValidateNode();
var slot = FindInputSlot<MaterialSlot>(0);
if (slot == null)
return;
var edges = owner.GetEdges(slot.slotReference).ToList();
hasError |= edges.Count == 0;
}
public bool RequiresNormal()
{
return true;
}
public bool RequiresWorldPosition()
{
return true;
}
}
}
///////////////// TEXTURE2D version below. Works fine, but the master nodes don't jive with it //////////////////////////
/*
namespace UnityEngine.MaterialGraph
{
[Title("UV/Tri-Planar Mapping")]
public class UVTriPlanar : FunctionNInNOut, IGeneratesFunction, IMayRequireNormal, IMayRequireWorldPosition
{
private int slot0, slot1, slot2, slot3, slot4, slot5, slot6 = 0;
private string slot0Name = "texRef";
private string slot1Name = "tileFactor";
private string slot2Name = "blendFactor";
private string slot3Name = "samplerRef";
private string slot4Name = "normalRef";
private string slot5Name = "posRef";

}
}
}
*/
正在加载...
取消
保存