浏览代码

Maths Part 3

- Cleanup after merge
- Started on Maths/Vector
- Fixed Log and Exponential outputs
- Partial Derivative is WIP
/main
Matt Dean 7 年前
当前提交
d085dca3
共有 17 个文件被更改,包括 494 次插入389 次删除
  1. 8
      MaterialGraphProject/Assets/NewNodes/Editor/CommonMatrixType.cs
  2. 30
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/ConstantNode.cs
  3. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/ConstantNode.cs.meta
  4. 43
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Advanced/LogNode.cs
  5. 34
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Basic/ExponentialNode.cs
  6. 48
      MaterialGraphProject/GeneratedShader.shader
  7. 31
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Vector/CrossProductNode.cs
  8. 156
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Vector/PartialDerivativeNode.cs
  9. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Vector/PartialDerivativeNode.cs.meta
  10. 199
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Vector/TransformNode.cs
  11. 60
      MaterialGraphProject/Assets/NewNodes/Editor/Keep/ConstantsNode.cs
  12. 227
      MaterialGraphProject/Assets/NewNodes/Editor/Keep/TransformNode.cs
  13. 31
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Vector/CrossNode.cs
  14. 0
      /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Vector/CrossProductNode.cs.meta
  15. 0
      /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Vector/TangentToWorldNode.cs
  16. 0
      /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Vector/TangentToWorldNode.cs.meta
  17. 0
      /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Vector/TransformNode.cs.meta

8
MaterialGraphProject/Assets/NewNodes/Editor/CommonMatrixType.cs


ObjectToWorld,
WorldToObject
};
public enum SimpleMatrixType
{
World,
Local,
Tangent,
View
};
}

30
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/ConstantNode.cs


using System.Collections.Generic;
using UnityEditor.MaterialGraph.Drawing.Controls;
using UnityEngine.Graphing;
[Title("Math/Constants")]
public class ConstantsNode : AbstractMaterialNode, IGeneratesBodyCode
public enum ConstantType
{
PI,
TAU,
PHI,
E,
SQRT2
};
[Title("Input/Basic/Constant")]
public class ConstantNode : AbstractMaterialNode, IGeneratesBodyCode
{
static Dictionary<ConstantType, float> m_constantList = new Dictionary<ConstantType, float>
{

private ConstantType m_constant = ConstantType.PI;
private const int kOutputSlotId = 0;
private const string kOutputSlotName = "Constant";
private const string kOutputSlotName = "Out";
[EnumControl("")]
public ConstantType constant

}
}
public ConstantsNode()
public ConstantNode()
name = "MathConstant";
name = "Constant";
UpdateNodeAfterDeserialization();
}

{
visitor.AddShaderChunk(precision + " " + GetVariableNameForNode() + " = " + m_constantList[constant] + ";", true);
}
public override string GetVariableNameForSlot(int slotId)
{
return GetVariableNameForNode();
}
}
}

5
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/ConstantNode.cs.meta


fileFormatVersion: 2
guid: fea1e18ab449c174d91a3f41a220aa4e
timeCreated: 1495448829
licenseType: Pro
guid: f48f68efc30ae334098ff30e88fbf9db
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0

43
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Advanced/LogNode.cs


name = "Log";
}
static Dictionary<LogType, string> m_LogTypes = new Dictionary<LogType, string>
{
{LogType.Log, "" },
{LogType.Log2, "2" },
{LogType.Log10, "10" },
};
private static LogType m_LogType = LogType.Log;
private LogType m_LogType = LogType.Log;
[EnumControl("")]
public LogType logType

}
}
string GetCurrentType()
{
return System.Enum.GetName(typeof(LogType), m_LogType);
}
return GetType().GetMethod("Unity_Log", BindingFlags.Static | BindingFlags.NonPublic);
return GetType().GetMethod(string.Format("Unity_{0}", GetCurrentType()),
BindingFlags.Static | BindingFlags.NonPublic);
}
static string Unity_Log(

return
@"
{
Out = log"+ m_LogTypes[m_LogType] + @"(In);
Out = log(In);
}
";
}
static string Unity_Log2(
[Slot(0, Binding.None)] DynamicDimensionVector In,
[Slot(1, Binding.None)] out DynamicDimensionVector Out)
{
return
@"
{
Out = log2(In);
}
";
}
static string Unity_Log10(
[Slot(0, Binding.None)] DynamicDimensionVector In,
[Slot(1, Binding.None)] out DynamicDimensionVector Out)
{
return
@"
{
Out = log10(In);
}
";
}

34
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Basic/ExponentialNode.cs


{
public enum ExponentialType
{
Exp,
Exp2
Exponential,
Exponential2
};
[Title("Math/Basic/Exponential")]

name = "Exponential";
}
static Dictionary<ExponentialType, string> m_ExponentialTypes = new Dictionary<ExponentialType, string>
{
{ExponentialType.Exp, "" },
{ExponentialType.Exp2, "2" },
};
private static ExponentialType m_ExponentialType = ExponentialType.Exp;
private ExponentialType m_ExponentialType = ExponentialType.Exponential;
[EnumControl("")]
public ExponentialType exponentialType

onModified(this, ModificationScope.Graph);
}
}
}
string GetCurrentType()
{
return System.Enum.GetName(typeof(ExponentialType), m_ExponentialType);
return GetType().GetMethod("Unity_Exponential", BindingFlags.Static | BindingFlags.NonPublic);
return GetType().GetMethod(string.Format("Unity_{0}", GetCurrentType()),
BindingFlags.Static | BindingFlags.NonPublic);
}
static string Unity_Exponential(

return
@"
{
Out = exp"+m_ExponentialTypes[m_ExponentialType] +@"(In);
Out = exp(In);
}
";
}
static string Unity_Exponential2(
[Slot(0, Binding.None)] DynamicDimensionVector In,
[Slot(1, Binding.None)] out DynamicDimensionVector Out)
{
return
@"
{
Out = exp2(In);
}
";
}

48
MaterialGraphProject/GeneratedShader.shader


Shader "hidden/preview/TangentToWorld_C47CCB6C"
Shader "hidden/preview/PartialDerivative_CA98ACC1"
{
Properties
{

void Unity_TangentToWorld_float(float3 inVector, out float3 result, float3 tangent, float3 biTangent, float3 normal)
void Unity_DDX_Coarse_float(float In, out float Out)
float3x3 tangentToWorld = transpose(float3x3(tangent, biTangent, normal));
result= saturate(mul(tangentToWorld, normalize(inVector)));
Out = ddx_coarse(In);
}
struct GraphVertexInput
{

UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct SurfaceInputs{
float3 WorldSpaceNormal;
float3 WorldSpaceTangent;
float3 WorldSpaceBiTangent;
float3 TangentToWorld_C47CCB6C_result;
float _PartialDerivative_CA98ACC1_Out;
surface.TangentToWorld_C47CCB6C_result = scale * surface.TangentToWorld_C47CCB6C_result;
surface._PartialDerivative_CA98ACC1_Out = scale * surface._PartialDerivative_CA98ACC1_Out;
base.TangentToWorld_C47CCB6C_result = base.TangentToWorld_C47CCB6C_result + add.TangentToWorld_C47CCB6C_result;
base._PartialDerivative_CA98ACC1_Out = base._PartialDerivative_CA98ACC1_Out + add._PartialDerivative_CA98ACC1_Out;
float4 TangentToWorld_C47CCB6C_inVector;
float4 TangentToWorld_C47CCB6C_tangent;
float4 TangentToWorld_C47CCB6C_biTangent;
float4 TangentToWorld_C47CCB6C_normal;
float Vector1_13AB6CC;
float3 WorldSpaceNormal = IN.WorldSpaceNormal;
float3 WorldSpaceTangent = IN.WorldSpaceTangent;
float3 WorldSpaceBiTangent = IN.WorldSpaceBiTangent;
float3 TangentToWorld_C47CCB6C_result;
Unity_TangentToWorld_float(TangentToWorld_C47CCB6C_inVector, TangentToWorld_C47CCB6C_result, WorldSpaceTangent, WorldSpaceBiTangent, WorldSpaceNormal);
float _PartialDerivative_CA98ACC1_Out;
Unity_DDX_Coarse_float(Vector1_13AB6CC, _PartialDerivative_CA98ACC1_Out);
surface.TangentToWorld_C47CCB6C_result = TangentToWorld_C47CCB6C_result;
surface._PartialDerivative_CA98ACC1_Out = _PartialDerivative_CA98ACC1_Out;
return surface;
}
ENDCG

struct GraphVertexOutput
{
float4 position : POSITION;
float3 WorldSpaceNormal : TEXCOORD0;
float3 WorldSpaceTangent : TEXCOORD1;
float3 WorldSpaceBiTangent : TEXCOORD2;
};
GraphVertexOutput vert (GraphVertexInput v)
{

o.WorldSpaceNormal = mul(v.normal,(float3x3)unity_WorldToObject);
o.WorldSpaceTangent = mul((float3x3)unity_ObjectToWorld,v.tangent);
o.WorldSpaceBiTangent = normalize(cross(o.WorldSpaceNormal, o.WorldSpaceTangent.xyz) * v.tangent.w);
float3 WorldSpaceNormal = normalize(IN.WorldSpaceNormal);
float3 WorldSpaceTangent = IN.WorldSpaceTangent;
float3 WorldSpaceBiTangent = IN.WorldSpaceBiTangent;
surfaceInput.WorldSpaceNormal = WorldSpaceNormal;
surfaceInput.WorldSpaceTangent = WorldSpaceTangent;
surfaceInput.WorldSpaceBiTangent = WorldSpaceBiTangent;
return half4(surf.TangentToWorld_C47CCB6C_result.x, surf.TangentToWorld_C47CCB6C_result.y, surf.TangentToWorld_C47CCB6C_result.z, 1.0);
return half4(surf._PartialDerivative_CA98ACC1_Out, surf._PartialDerivative_CA98ACC1_Out, surf._PartialDerivative_CA98ACC1_Out, 1.0);
}
ENDCG
}

31
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Vector/CrossProductNode.cs


using System.Reflection;
namespace UnityEngine.MaterialGraph
{
[Title("Math/Vector/Cross Product")]
public class CrossProductNode : CodeFunctionNode
{
public CrossProductNode()
{
name = "Cross Product";
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_CrossProduct", BindingFlags.Static | BindingFlags.NonPublic);
}
static string Unity_CrossProduct(
[Slot(0, Binding.None)] DynamicDimensionVector A,
[Slot(1, Binding.None)] DynamicDimensionVector B,
[Slot(2, Binding.None)] out DynamicDimensionVector Out)
{
return
@"
{
Out = cross(A, B);
}
";
}
}
}

156
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Vector/PartialDerivativeNode.cs


using System.Reflection;
using UnityEditor.MaterialGraph.Drawing.Controls;
using UnityEngine.Graphing;
namespace UnityEngine.MaterialGraph
{
[Title("Math/Vector/Partial Derivative")]
public class PartialDerivativeNode : CodeFunctionNode
{
public enum Precision
{
Coarse,
Default,
Fine
};
public enum Coordinate
{
X,
Y
};
public PartialDerivativeNode()
{
name = "Partial Derivative";
}
[SerializeField]
private Precision m_Precision = Precision.Default;
[EnumControl("Precision")]
public Precision _precision
{
get { return m_Precision; }
set
{
if (m_Precision == value)
return;
m_Precision = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
}
}
string GetCurrentPrecision()
{
return System.Enum.GetName(typeof(Precision), m_Precision);
}
[SerializeField]
private Coordinate m_Coordinate = Coordinate.X;
[EnumControl("Respect Coordinate")]
public Coordinate coordinate
{
get { return m_Coordinate; }
set
{
if (m_Coordinate == value)
return;
m_Coordinate = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
}
}
string GetCurrentCoordinate()
{
return System.Enum.GetName(typeof(Coordinate), m_Coordinate);
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod(string.Format("Unity_DD{0}_{1}", GetCurrentCoordinate(), GetCurrentPrecision()),
BindingFlags.Static | BindingFlags.NonPublic);
}
static string Unity_DDX_Default(
[Slot(0, Binding.None)] Vector1 In,
[Slot(1, Binding.None)] out Vector1 Out)
{
return
@"
{
Out = ddx(In);
}
";
}
static string Unity_DDX_Coarse(
[Slot(0, Binding.None)] Vector1 In,
[Slot(1, Binding.None)] out Vector1 Out)
{
return
@"
{
Out = ddx_coarse(In);
}
";
}
static string Unity_DDX_Fine(
[Slot(0, Binding.None)] Vector1 In,
[Slot(1, Binding.None)] out Vector1 Out)
{
return
@"
{
Out = ddx_fine(In);
}
";
}
static string Unity_DDY_Default(
[Slot(0, Binding.None)] Vector1 In,
[Slot(1, Binding.None)] out Vector1 Out)
{
return
@"
{
Out = ddy(In);
}
";
}
static string Unity_DDY_Coarse(
[Slot(0, Binding.None)] Vector1 In,
[Slot(1, Binding.None)] out Vector1 Out)
{
return
@"
{
Out = ddy_coarse(In);
}
";
}
static string Unity_DDY_Fine(
[Slot(0, Binding.None)] Vector1 In,
[Slot(1, Binding.None)] out Vector1 Out)
{
return
@"
{
Out = ddy_fine(In);
}
";
}
}
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Vector/PartialDerivativeNode.cs.meta


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

199
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Vector/TransformNode.cs


using UnityEngine.Graphing;
using System.Collections.Generic;
using UnityEditor.MaterialGraph.Drawing.Controls;
namespace UnityEngine.MaterialGraph
{
[Title("Math/Vector/Transform")]
public class TransformNode : AbstractMaterialNode, IGeneratesBodyCode, IMayRequireTangent, IMayRequireBitangent, IMayRequireNormal
{
[SerializeField]
private CoordinateSpace m_spaceListFrom;
[SerializeField]
private CoordinateSpace m_spaceListTo;
private const int InputSlotId = 0;
private const int OutputSlotId = 1;
private const string kInputSlotName = "In";
private const string kOutputSlotName = "Out";
public TransformNode()
{
name = "Transform";
UpdateNodeAfterDeserialization();
}
[EnumControl("From")]
public CoordinateSpace spaceFrom
{
get { return m_spaceListFrom; }
set
{
if (m_spaceListFrom == value)
return;
m_spaceListFrom = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
}
}
[EnumControl("To")]
public CoordinateSpace spaceTo
{
get { return m_spaceListTo; }
set
{
if (m_spaceListTo == value)
return;
m_spaceListTo = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
}
}
public override bool hasPreview
{
get { return true; }
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(GetInputSlot());
AddSlot(GetOutputSlot());
RemoveSlotsNameNotMatching(validSlots);
}
protected int[] validSlots
{
get { return new[] {InputSlotId, OutputSlotId}; }
}
protected virtual MaterialSlot GetInputSlot()
{
return new Vector3MaterialSlot(InputSlotId, kInputSlotName, kInputSlotName, SlotType.Input, Vector3.zero);
}
protected virtual MaterialSlot GetOutputSlot()
{
return new Vector3MaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, Vector4.zero);
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
NodeUtils.SlotConfigurationExceptionIfBadConfiguration(this, new[] { InputSlotId }, new[] { OutputSlotId });
string inputValue = GetSlotValue(InputSlotId, generationMode);
string transformString = "";
bool requiresTangentTransform = false;
if (spaceFrom == CoordinateSpace.World)
{
if (spaceTo == CoordinateSpace.World)
{
transformString = inputValue;
}
else if (spaceTo == CoordinateSpace.Object)
{
transformString = "mul(unity_WorldToObject, float4(" + inputValue + ", 0)).xyz";
}
else if (spaceTo == CoordinateSpace.Tangent)
{
requiresTangentTransform = true;
transformString = "mul(tangentTransform, " + inputValue + ").xyz";
}
else if (spaceTo == CoordinateSpace.View)
{
transformString = "mul( UNITY_MATRIX_V, float4(" + inputValue + ", 0)).xyz";
}
}
else if (spaceFrom == CoordinateSpace.Object)
{
if (spaceTo == CoordinateSpace.World)
{
transformString = "mul(unity_ObjectToWorld, float4(" + inputValue + ", 0)).xyz";
}
else if (spaceTo == CoordinateSpace.Object)
{
transformString = inputValue;
}
else if (spaceTo == CoordinateSpace.Tangent)
{
requiresTangentTransform = true;
transformString = "mul( tangentTransform, mul( unity_ObjectToWorld, float4(" + inputValue + ", 0) ).xyz).xyz";
}
else if (spaceTo == CoordinateSpace.View)
{
transformString = "mul( UNITY_MATRIX_MV, float4(" + inputValue + ", 0)).xyz";
}
}
else if (spaceFrom == CoordinateSpace.Tangent)
{
requiresTangentTransform = true;
if (spaceTo == CoordinateSpace.World)
{
transformString = "mul( " + inputValue + ", tangentTransform ).xyz";
}
else if (spaceTo == CoordinateSpace.Object)
{
transformString = "mul( unity_WorldToObject, float4(mul(" + inputValue + ", tangentTransform ),0) ).xyz";
}
else if (spaceTo == CoordinateSpace.Tangent)
{
transformString = inputValue;
}
else if (spaceTo == CoordinateSpace.View)
{
transformString = "mul( UNITY_MATRIX_V, float4(mul(" + inputValue + ", tangentTransform ),0) ).xyz";
}
}
else if (spaceFrom == CoordinateSpace.View)
{
if (spaceTo == CoordinateSpace.World)
{
transformString = "mul( float4(" + inputValue + ", 0), UNITY_MATRIX_V ).xyz";
}
else if (spaceTo == CoordinateSpace.Object)
{
transformString = "mul( float4(" + inputValue + ", 0), UNITY_MATRIX_MV ).xyz";
}
else if (spaceTo == CoordinateSpace.Tangent)
{
requiresTangentTransform = true;
transformString = "mul( tangentTransform, mul( float4(" + inputValue + ", 0), UNITY_MATRIX_V ).xyz ).xyz";
}
else if (spaceTo == CoordinateSpace.View)
{
transformString = inputValue;
}
}
if (requiresTangentTransform)
visitor.AddShaderChunk("float3x3 tangentTransform = float3x3("+ spaceFrom.ToString() + "SpaceTangent, "+ spaceFrom.ToString() + "SpaceBiTangent, "+ spaceFrom.ToString() + "SpaceNormal);", false);
visitor.AddShaderChunk(string.Format("{0} {1} = {2};",
ConvertConcreteSlotValueTypeToString(precision, FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType),
GetVariableNameForSlot(OutputSlotId),
transformString), true);
}
public NeededCoordinateSpace RequiresTangent()
{
return spaceFrom.ToNeededCoordinateSpace();
}
public NeededCoordinateSpace RequiresBitangent()
{
return spaceFrom.ToNeededCoordinateSpace();
}
public NeededCoordinateSpace RequiresNormal()
{
return spaceFrom.ToNeededCoordinateSpace();
}
}
}

60
MaterialGraphProject/Assets/NewNodes/Editor/Keep/ConstantsNode.cs


using System.Collections.Generic;
using UnityEditor.MaterialGraph.Drawing.Controls;
using UnityEngine.MaterialGraph;
using UnityEngine.Graphing;
namespace UnityEngine.MaterialGraph
{
[Title("Math/Constants")]
public class ConstantsNode : AbstractMaterialNode, IGeneratesBodyCode
{
static Dictionary<ConstantType, float> m_constantList = new Dictionary<ConstantType, float>
{
{ConstantType.PI, 3.1415926f },
{ConstantType.TAU, 6.28318530f},
{ConstantType.PHI, 1.618034f},
{ConstantType.E, 2.718282f},
{ConstantType.SQRT2, 1.414214f},
};
[SerializeField]
private ConstantType m_constant = ConstantType.PI;
private const int kOutputSlotId = 0;
private const string kOutputSlotName = "Constant";
[EnumControl("")]
public ConstantType constant
{
get { return m_constant; }
set
{
if (m_constant == value)
return;
m_constant = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
}
}
public ConstantsNode()
{
name = "MathConstant";
UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new Vector1MaterialSlot(kOutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, 0));
RemoveSlotsNameNotMatching(new[] { kOutputSlotId });
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
visitor.AddShaderChunk(precision + " " + GetVariableNameForNode() + " = " + m_constantList[constant] + ";", true);
}
}
}

227
MaterialGraphProject/Assets/NewNodes/Editor/Keep/TransformNode.cs


using UnityEngine.Graphing;
using System.Collections.Generic;
using UnityEditor.MaterialGraph.Drawing.Controls;
namespace UnityEngine.MaterialGraph
{
[Title("Math/Vector/Transform")]
public class TransformNode : AbstractMaterialNode, IGeneratesBodyCode, IMayRequireTangent, IMayRequireBitangent, IMayRequireNormal
{
[SerializeField]
private SimpleMatrixType m_spaceListFrom;
[SerializeField]
private SimpleMatrixType m_spaceListTo;
private const int InputSlotId = 0;
private const int OutputSlotId = 1;
private const string kInputSlotName = "Input";
private const string kOutputSlotName = "Output";
[EnumControl("From")]
public SimpleMatrixType spaceFrom
{
get { return m_spaceListFrom; }
set
{
if (m_spaceListFrom == value)
return;
m_spaceListFrom = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
}
}
public override bool hasPreview
{
get { return false; }
}
[EnumControl("To")]
public SimpleMatrixType spaceTo
{
get { return m_spaceListTo; }
set
{
if (m_spaceListTo == value)
return;
m_spaceListTo = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
}
}
public TransformNode()
{
name = "Transform";
UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(GetInputSlot());
AddSlot(GetOutputSlot());
RemoveSlotsNameNotMatching(validSlots);
}
protected int[] validSlots
{
get { return new[] {InputSlotId, OutputSlotId}; }
}
protected virtual MaterialSlot GetInputSlot()
{
return new Vector3MaterialSlot(InputSlotId, GetInputSlotName(), kInputSlotName, SlotType.Input, Vector3.zero);
}
protected virtual MaterialSlot GetOutputSlot()
{
return new Vector3MaterialSlot(OutputSlotId, GetOutputSlotName(), kOutputSlotName, SlotType.Output, Vector4.zero);
}
protected virtual string GetInputSlotName()
{
return "Input";
}
protected virtual string GetOutputSlotName()
{
return "Output";
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
NodeUtils.SlotConfigurationExceptionIfBadConfiguration(this, new[] { InputSlotId }, new[] { OutputSlotId });
string inputValue = GetSlotValue(InputSlotId, generationMode);
string transformString = "";
bool requiresTangentTransform = false;
if (spaceFrom == SimpleMatrixType.World)
{
if (spaceTo == SimpleMatrixType.World)
{
transformString = inputValue;
}
else if (spaceTo == SimpleMatrixType.Local)
{
transformString = "mul(unity_WorldToObject, float4(" + inputValue + ", 0)).xyz";
}
else if (spaceTo == SimpleMatrixType.Tangent)
{
requiresTangentTransform = true;
transformString = "mul(tangentTransform, " + inputValue + ").xyz";
}
else if (spaceTo == SimpleMatrixType.View)
{
transformString = "mul( UNITY_MATRIX_V, float4(" + inputValue + ", 0)).xyz";
}
}
else if (spaceFrom == SimpleMatrixType.Local)
{
if (spaceTo == SimpleMatrixType.World)
{
transformString = "mul(unity_ObjectToWorld, float4(" + inputValue + ", 0)).xyz";
}
else if (spaceTo == SimpleMatrixType.Local)
{
transformString = inputValue;
}
else if (spaceTo == SimpleMatrixType.Tangent)
{
requiresTangentTransform = true;
transformString = "mul( tangentTransform, mul( unity_ObjectToWorld, float4(" + inputValue + ", 0) ).xyz).xyz";
}
else if (spaceTo == SimpleMatrixType.View)
{
transformString = "mul( UNITY_MATRIX_MV, float4(" + inputValue + ", 0)).xyz";
}
}
else if (spaceFrom == SimpleMatrixType.Tangent)
{
requiresTangentTransform = true;
if (spaceTo == SimpleMatrixType.World)
{
transformString = "mul( " + inputValue + ", tangentTransform ).xyz";
}
else if (spaceTo == SimpleMatrixType.Local)
{
transformString = "mul( unity_WorldToObject, float4(mul(" + inputValue + ", tangentTransform ),0) ).xyz";
}
else if (spaceTo == SimpleMatrixType.Tangent)
{
transformString = inputValue;
}
else if (spaceTo == SimpleMatrixType.View)
{
transformString = "mul( UNITY_MATRIX_V, float4(mul(" + inputValue + ", tangentTransform ),0) ).xyz";
}
}
else if (spaceFrom == SimpleMatrixType.View)
{
if (spaceTo == SimpleMatrixType.World)
{
transformString = "mul( float4(" + inputValue + ", 0), UNITY_MATRIX_V ).xyz";
}
else if (spaceTo == SimpleMatrixType.Local)
{
transformString = "mul( float4(" + inputValue + ", 0), UNITY_MATRIX_MV ).xyz";
}
else if (spaceTo == SimpleMatrixType.Tangent)
{
requiresTangentTransform = true;
transformString = "mul( tangentTransform, mul( float4(" + inputValue + ", 0), UNITY_MATRIX_V ).xyz ).xyz";
}
else if (spaceTo == SimpleMatrixType.View)
{
transformString = inputValue;
}
}
if (requiresTangentTransform)
visitor.AddShaderChunk("float3x3 tangentTransform = float3x3( worldSpaceTangent, worldSpaceBitangent, worldSpaceNormal);", false);
visitor.AddShaderChunk(string.Format("{0} {1} = {2};",
ConvertConcreteSlotValueTypeToString(precision, FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType),
GetVariableNameForSlot(OutputSlotId),
transformString), true);
}
//float3x3 tangentTransform = float3x3( i.tangentDir, i.bitangentDir, i.normalDir);------
//mul(unity_WorldToObject, float4(i.posWorld.rgb,0) ).xyz - world to local---------
//mul( tangentTransform, i.posWorld.rgb ).xyz - world to tangent-----------------
//mul( UNITY_MATRIX_V, float4(i.posWorld.rgb,0) ).xyz - world to view-------------
//mul( unity_ObjectToWorld, float4(i.posWorld.rgb,0) ).xyz - local to world--------
//mul( tangentTransform, mul( unity_ObjectToWorld, float4(i.posWorld.rgb,0) ).xyz - local to tangent------------
//mul( UNITY_MATRIX_MV, float4(i.posWorld.rgb,0) ).xyz - local to view--------------
//mul( i.posWorld.rgb, tangentTransform ).xyz - tangent to world---------
//mul( unity_WorldToObject, float4(mul( i.posWorld.rgb, tangentTransform ),0) ).xyz - tangent to local-----
//mul( UNITY_MATRIX_V, float4(mul( i.posWorld.rgb, tangentTransform ),0) ).xyz - tangent to view-------
//mul( float4(i.posWorld.rgb,0), UNITY_MATRIX_V ).xyz - view to world
//mul( float4(i.posWorld.rgb,0), UNITY_MATRIX_MV ).xyz - view to local
//mul( tangentTransform, mul( float4(i.posWorld.rgb,0), UNITY_MATRIX_V ).xyz ).xyz - view to tangent
public NeededCoordinateSpace RequiresTangent()
{
return NeededCoordinateSpace.World;
}
public NeededCoordinateSpace RequiresBitangent()
{
return NeededCoordinateSpace.World;
}
public NeededCoordinateSpace RequiresNormal()
{
return NeededCoordinateSpace.World;
}
}
}

31
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Vector/CrossNode.cs


using System.Reflection;
namespace UnityEngine.MaterialGraph
{
[Title("Math/Vector/Cross Product")]
public class CrossNode : CodeFunctionNode
{
public CrossNode()
{
name = "CrossProduct";
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_CrossProduct", BindingFlags.Static | BindingFlags.NonPublic);
}
static string Unity_CrossProduct(
[Slot(0, Binding.None)] DynamicDimensionVector first,
[Slot(1, Binding.None)] DynamicDimensionVector second,
[Slot(2, Binding.None)] out DynamicDimensionVector result)
{
return
@"
{
result = cross(first, second);
}
";
}
}
}

/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Vector/CrossNode.cs.meta → /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Vector/CrossProductNode.cs.meta

/MaterialGraphProject/Assets/NewNodes/Editor/Keep/TangentToWorldNode.cs → /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Vector/TangentToWorldNode.cs

/MaterialGraphProject/Assets/NewNodes/Editor/Keep/TangentToWorldNode.cs.meta → /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Vector/TangentToWorldNode.cs.meta

/MaterialGraphProject/Assets/NewNodes/Editor/Keep/TransformNode.cs.meta → /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Vector/TransformNode.cs.meta

正在加载...
取消
保存