Fix World Triplanar Texture node to support displacement and float4 value

This commit is contained in:
Wojtek Figat
2024-08-05 14:37:31 +02:00
parent d6e99071aa
commit 979ab9f6ee
2 changed files with 11 additions and 23 deletions

View File

@@ -404,9 +404,9 @@ namespace FlaxEditor.Surface.Archetypes
Elements = new[] Elements = new[]
{ {
NodeElementArchetype.Factory.Input(0, "Texture", true, typeof(FlaxEngine.Object), 0), NodeElementArchetype.Factory.Input(0, "Texture", true, typeof(FlaxEngine.Object), 0),
NodeElementArchetype.Factory.Input(1, "Scale", true, typeof(Float3), 1, 0), NodeElementArchetype.Factory.Input(1, "Scale", true, typeof(Float4), 1, 0),
NodeElementArchetype.Factory.Input(2, "Blend", true, typeof(float), 2, 1), NodeElementArchetype.Factory.Input(2, "Blend", true, typeof(float), 2, 1),
NodeElementArchetype.Factory.Output(0, "Color", typeof(Float3), 3) NodeElementArchetype.Factory.Output(0, "Color", typeof(Float4), 3)
} }
}, },
new NodeArchetype new NodeArchetype

View File

@@ -687,51 +687,41 @@ void MaterialGenerator::ProcessGroupTextures(Box* box, Node* node, Value& value)
// World Triplanar Texture // World Triplanar Texture
case 16: case 16:
{ {
// Get input boxes
auto textureBox = node->GetBox(0); auto textureBox = node->GetBox(0);
auto scaleBox = node->GetBox(1); auto scaleBox = node->GetBox(1);
auto blendBox = node->GetBox(2); auto blendBox = node->GetBox(2);
if (!textureBox->HasConnection()) if (!textureBox->HasConnection())
{ {
// No texture to sample // No texture to sample
value = Value::Zero; value = Value::Zero;
break; break;
} }
const bool canUseSample = CanUseSample(_treeType);
if (!CanUseSample(_treeType))
{
// Must sample texture in pixel shader
value = Value::Zero;
break;
}
const auto texture = eatBox(textureBox->GetParent<Node>(), textureBox->FirstConnection()); const auto texture = eatBox(textureBox->GetParent<Node>(), textureBox->FirstConnection());
const auto scale = tryGetValue(scaleBox, node->Values[0]).AsFloat3(); const auto scale = tryGetValue(scaleBox, node->Values[0]).AsFloat4();
const auto blend = tryGetValue(blendBox, node->Values[1]).AsFloat(); const auto blend = tryGetValue(blendBox, node->Values[1]).AsFloat();
auto result = writeLocal(Value::InitForZero(ValueType::Float4), node); auto result = writeLocal(Value::InitForZero(ValueType::Float4), node);
const String triplanarTexture = String::Format(TEXT( const String triplanarTexture = String::Format(TEXT(
" {{\n" " {{\n"
" float3 worldPos = input.WorldPosition.xyz * ({1} * 0.001f);\n" " float3 worldPos = input.WorldPosition.xyz * ({1} * 0.001f);\n"
" float3 normal = abs(input.TBN[2]);\n" " float3 normal = abs(input.TBN[2]);\n"
" normal = pow(normal, {2});\n" " normal = pow(normal, {2});\n"
" normal = normal / (normal.x + normal.y + normal.z);\n" " normal = normal / (normal.x + normal.y + normal.z);\n"
" {3} += {0}.{4}(SamplerLinearWrap, worldPos.yz{5}) * normal.x;\n"
" {3} += {0}.Sample(SamplerLinearWrap, worldPos.yz) * normal.x;\n" " {3} += {0}.{4}(SamplerLinearWrap, worldPos.xz{5}) * normal.y;\n"
" {3} += {0}.Sample(SamplerLinearWrap, worldPos.xz) * normal.y;\n" " {3} += {0}.{4}(SamplerLinearWrap, worldPos.xy{5}) * normal.z;\n"
" {3} += {0}.Sample(SamplerLinearWrap, worldPos.xy) * normal.z;\n"
" }}\n" " }}\n"
), ),
texture.Value, // {0} texture.Value, // {0}
scale.Value, // {1} scale.Value, // {1}
blend.Value, // {2} blend.Value, // {2}
result.Value // {3} result.Value, // {3}
canUseSample ? TEXT("Sample") : TEXT("SampleLevel"), // {4}
canUseSample ? TEXT("") : TEXT(", 0") // {5}
); );
_writer.Write(*triplanarTexture); _writer.Write(*triplanarTexture);
value = result; value = result;
break;
} }
// Get Lightmap UV // Get Lightmap UV
case 18: case 18:
@@ -746,9 +736,7 @@ void MaterialGenerator::ProcessGroupTextures(Box* box, Node* node, Value& value)
"#endif\n" "#endif\n"
"}}\n" "}}\n"
), output.Value); ), output.Value);
_writer.Write(*lightmapUV); _writer.Write(*lightmapUV);
value = output; value = output;
break; break;
} }