|
|
|
|
|
|
using System; |
|
|
|
using UnityEditor.Graphing; |
|
|
|
using UnityEditor.ShaderGraph.Drawing.Controls; |
|
|
|
using UnityEngine; |
|
|
|
|
|
|
[Serializable] |
|
|
|
public struct CoordinateSpaceConversion : IEnumConversion |
|
|
|
{ |
|
|
|
public CoordinateSpace from; |
|
|
|
public CoordinateSpace to; |
|
|
|
|
|
|
|
public CoordinateSpaceConversion(CoordinateSpace from, CoordinateSpace to) |
|
|
|
{ |
|
|
|
this.from = from; |
|
|
|
this.to = to; |
|
|
|
} |
|
|
|
|
|
|
|
Enum IEnumConversion.from |
|
|
|
{ |
|
|
|
get { return from; } |
|
|
|
set { from = (CoordinateSpace)value; } |
|
|
|
} |
|
|
|
|
|
|
|
Enum IEnumConversion.to |
|
|
|
{ |
|
|
|
get { return to; } |
|
|
|
set { to = (CoordinateSpace)value; } |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
[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"; |
|
|
|
|
|
|
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); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
[SerializeField] |
|
|
|
CoordinateSpaceConversion m_Conversion = new CoordinateSpaceConversion(CoordinateSpace.Object, CoordinateSpace.World); |
|
|
|
[EnumControl("To")] |
|
|
|
public CoordinateSpace spaceTo |
|
|
|
[EnumConversionControl] |
|
|
|
public CoordinateSpaceConversion conversion |
|
|
|
get { return m_spaceListTo; } |
|
|
|
get { return m_Conversion; } |
|
|
|
if (m_spaceListTo == value) |
|
|
|
if (Equals(m_Conversion, value)) |
|
|
|
|
|
|
|
m_spaceListTo = value; |
|
|
|
m_Conversion = value; |
|
|
|
{ |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected int[] validSlots |
|
|
|
{ |
|
|
|
get { return new[] {InputSlotId, OutputSlotId}; } |
|
|
|
get { return new[] { InputSlotId, OutputSlotId }; } |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual MaterialSlot GetInputSlot() |
|
|
|
|
|
|
{ |
|
|
|
NodeUtils.SlotConfigurationExceptionIfBadConfiguration(this, new[] { InputSlotId }, new[] { OutputSlotId }); |
|
|
|
string inputValue = GetSlotValue(InputSlotId, generationMode); |
|
|
|
string targetTransformString = "tangentTransform_" + spaceFrom.ToString(); |
|
|
|
if (spaceFrom == CoordinateSpace.World) |
|
|
|
if (conversion.from == CoordinateSpace.World) |
|
|
|
if (spaceTo == CoordinateSpace.World) |
|
|
|
if (conversion.to == CoordinateSpace.World) |
|
|
|
else if (spaceTo == CoordinateSpace.Object) |
|
|
|
else if (conversion.to == CoordinateSpace.Object) |
|
|
|
else if (spaceTo == CoordinateSpace.Tangent) |
|
|
|
else if (conversion.to == CoordinateSpace.Tangent) |
|
|
|
transformString = "mul(" + targetTransformString + ", " + inputValue + ").xyz"; |
|
|
|
transformString = "mul(tangentTransform, " + inputValue + ").xyz"; |
|
|
|
else if (spaceTo == CoordinateSpace.View) |
|
|
|
else if (conversion.to == CoordinateSpace.View) |
|
|
|
else if (spaceFrom == CoordinateSpace.Object) |
|
|
|
else if (conversion.from == CoordinateSpace.Object) |
|
|
|
if (spaceTo == CoordinateSpace.World) |
|
|
|
if (conversion.to == CoordinateSpace.World) |
|
|
|
else if (spaceTo == CoordinateSpace.Object) |
|
|
|
else if (conversion.to == CoordinateSpace.Object) |
|
|
|
else if (spaceTo == CoordinateSpace.Tangent) |
|
|
|
else if (conversion.to == CoordinateSpace.Tangent) |
|
|
|
transformString = "mul( " + targetTransformString + ", mul( unity_ObjectToWorld, float4(" + inputValue + ", 0) ).xyz).xyz"; |
|
|
|
transformString = "mul( tangentTransform, mul( unity_ObjectToWorld, float4(" + inputValue + ", 0) ).xyz).xyz"; |
|
|
|
else if (spaceTo == CoordinateSpace.View) |
|
|
|
else if (conversion.to == CoordinateSpace.View) |
|
|
|
else if (spaceFrom == CoordinateSpace.Tangent) |
|
|
|
else if (conversion.from == CoordinateSpace.Tangent) |
|
|
|
if (spaceTo == CoordinateSpace.World) |
|
|
|
if (conversion.to == CoordinateSpace.World) |
|
|
|
transformString = "mul( " + inputValue + ", " + targetTransformString + " ).xyz"; |
|
|
|
transformString = "mul( " + inputValue + ", tangentTransform ).xyz"; |
|
|
|
else if (spaceTo == CoordinateSpace.Object) |
|
|
|
else if (conversion.to == CoordinateSpace.Object) |
|
|
|
transformString = "mul( unity_WorldToObject, float4(mul(" + inputValue + ", " + targetTransformString + " ),0) ).xyz"; |
|
|
|
transformString = "mul( unity_WorldToObject, float4(mul(" + inputValue + ", tangentTransform ),0) ).xyz"; |
|
|
|
else if (spaceTo == CoordinateSpace.Tangent) |
|
|
|
else if (conversion.to == CoordinateSpace.Tangent) |
|
|
|
else if (spaceTo == CoordinateSpace.View) |
|
|
|
else if (conversion.to == CoordinateSpace.View) |
|
|
|
transformString = "mul( UNITY_MATRIX_V, float4(mul(" + inputValue + ", " + targetTransformString + " ),0) ).xyz"; |
|
|
|
transformString = "mul( UNITY_MATRIX_V, float4(mul(" + inputValue + ", tangentTransform ),0) ).xyz"; |
|
|
|
else if (spaceFrom == CoordinateSpace.View) |
|
|
|
else if (conversion.from == CoordinateSpace.View) |
|
|
|
if (spaceTo == CoordinateSpace.World) |
|
|
|
if (conversion.to == CoordinateSpace.World) |
|
|
|
else if (spaceTo == CoordinateSpace.Object) |
|
|
|
else if (conversion.to == CoordinateSpace.Object) |
|
|
|
else if (spaceTo == CoordinateSpace.Tangent) |
|
|
|
else if (conversion.to == CoordinateSpace.Tangent) |
|
|
|
transformString = "mul( " + targetTransformString + ", mul( float4(" + inputValue + ", 0), UNITY_MATRIX_V ).xyz ).xyz"; |
|
|
|
transformString = "mul( tangentTransform, mul( float4(" + inputValue + ", 0), UNITY_MATRIX_V ).xyz ).xyz"; |
|
|
|
else if (spaceTo == CoordinateSpace.View) |
|
|
|
else if (conversion.to == CoordinateSpace.View) |
|
|
|
{ |
|
|
|
transformString = inputValue; |
|
|
|
} |
|
|
|
|
|
|
visitor.AddShaderChunk("float3x3 " + targetTransformString + " = float3x3(" + spaceFrom.ToString() + "SpaceTangent, " + spaceFrom.ToString() + "SpaceBiTangent, " + spaceFrom.ToString() + "SpaceNormal);", true); |
|
|
|
visitor.AddShaderChunk("float3x3 tangentTransform = float3x3(" + conversion.from.ToString() + "SpaceTangent, " + conversion.from.ToString() + "SpaceBiTangent, " + conversion.from.ToString() + "SpaceNormal);", false); |
|
|
|
|
|
|
|
visitor.AddShaderChunk(string.Format("{0} {1} = {2};", |
|
|
|
ConvertConcreteSlotValueTypeToString(precision, FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType), |
|
|
|
|
|
|
|
|
|
|
public NeededCoordinateSpace RequiresTangent() |
|
|
|
{ |
|
|
|
return spaceFrom.ToNeededCoordinateSpace(); |
|
|
|
return conversion.from.ToNeededCoordinateSpace(); |
|
|
|
return spaceFrom.ToNeededCoordinateSpace(); |
|
|
|
return conversion.from.ToNeededCoordinateSpace(); |
|
|
|
return spaceFrom.ToNeededCoordinateSpace(); |
|
|
|
return conversion.from.ToNeededCoordinateSpace(); |
|
|
|
} |
|
|
|
} |