Add World Triplanar Texture node to shader editor
This commit is contained in:
@@ -385,6 +385,24 @@ namespace FlaxEditor.Surface.Archetypes
|
||||
NodeElementArchetype.Factory.Input(0, "World Position", true, typeof(Float3), 1),
|
||||
}
|
||||
},
|
||||
new NodeArchetype
|
||||
{
|
||||
TypeID = 16,
|
||||
Title = "World Triplanar Texture",
|
||||
Description = "Projects a texture using world-space coordinates instead of UVs.",
|
||||
Flags = NodeFlags.MaterialGraph,
|
||||
Size = new Float2(240, 60),
|
||||
DefaultValues = new object[]
|
||||
{
|
||||
1.0f
|
||||
},
|
||||
Elements = new[]
|
||||
{
|
||||
NodeElementArchetype.Factory.Input(0, "Texture", true, typeof(FlaxEngine.Object), 0),
|
||||
NodeElementArchetype.Factory.Input(1, "Scale", true, typeof(float), 1, 0),
|
||||
NodeElementArchetype.Factory.Output(0, "Color", typeof(Float3), 2)
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -444,6 +444,48 @@ void MaterialGenerator::ProcessGroupTextures(Box* box, Node* node, Value& value)
|
||||
value = box == gradientBox ? gradient : distance;
|
||||
break;
|
||||
}
|
||||
// World Triplanar Texture
|
||||
case 16:
|
||||
{
|
||||
// Get input boxes
|
||||
auto textureBox = node->GetBox(0);
|
||||
auto scaleBox = node->GetBox(1);
|
||||
|
||||
if (!textureBox->HasConnection())
|
||||
{
|
||||
// No texture to sample
|
||||
value = Value::Zero;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!CanUseSample(_treeType))
|
||||
{
|
||||
// Must sample texture in pixel shader
|
||||
value = Value::Zero;
|
||||
break;
|
||||
}
|
||||
|
||||
const auto texture = eatBox(textureBox->GetParent<Node>(), textureBox->FirstConnection());
|
||||
const auto scale = tryGetValue(scaleBox, node->Values[0]);
|
||||
|
||||
auto result = writeLocal(Value::InitForZero(ValueType::Float4), node);
|
||||
|
||||
const String triplanarTexture = String::Format(TEXT(
|
||||
" float3 worldPos = input.WorldPosition.xyz * {1} / 1000;\n"
|
||||
" float3 normal = input.TBN[2];\n"
|
||||
|
||||
" {2} += {0}.Sample(SamplerLinearWrap, worldPos.yz) * abs(dot(normal, float3(1,0,0)));\n"
|
||||
" {2} += {0}.Sample(SamplerLinearWrap, worldPos.xz) * abs(dot(normal, float3(0,1,0)));\n"
|
||||
" {2} += {0}.Sample(SamplerLinearWrap, worldPos.xy) * abs(dot(normal, float3(0,0,1)));\n"
|
||||
),
|
||||
texture.Value, // {0}
|
||||
scale.Value, // {1}
|
||||
result.Value // {2}
|
||||
);
|
||||
|
||||
_writer.Write(*triplanarTexture);
|
||||
value = result;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user