Skip to content

Commit

Permalink
[animation] world space height for pivot based animated materials is …
Browse files Browse the repository at this point in the history
…now computed automatically
  • Loading branch information
PanosK92 committed Jan 17, 2025
1 parent d281bdc commit 95ce0ff
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 34 deletions.
4 changes: 2 additions & 2 deletions editor/Widgets/Properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -884,9 +884,9 @@ void Properties::ShowMaterial(Material* material) const
}

// wind animation
bool wind_animation = material->GetProperty(MaterialProperty::WindAnimation) != 0.0f;
bool wind_animation = material->GetProperty(MaterialProperty::AnimationFoliageWind) != 0.0f;
ImGui::Checkbox("Wind animation", &wind_animation);
material->SetProperty(MaterialProperty::WindAnimation, wind_animation ? 1.0f : 0.0f);
material->SetProperty(MaterialProperty::AnimationFoliageWind, wind_animation ? 1.0f : 0.0f);
}

//= MAP ===============================================================================
Expand Down
27 changes: 12 additions & 15 deletions runtime/Game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ namespace spartan
material->SetProperty(MaterialProperty::Normal, 0.1f);
material->SetProperty(MaterialProperty::TextureTilingX, 200.0f);
material->SetProperty(MaterialProperty::TextureTilingY, 200.0f);
material->SetProperty(MaterialProperty::VertexAnimateWater, 1.0f);
material->SetProperty(MaterialProperty::AnimationWaterFlow, 1.0f);
material->SetProperty(MaterialProperty::CullMode, static_cast<float>(RHI_CullMode::None));

// create a file path for this material (required for the material to be able to be cached by the resource cache)
Expand Down Expand Up @@ -646,9 +646,7 @@ namespace spartan
material->SetTexture(MaterialTextureType::Color, "project\\terrain\\vegetation_tree_2\\branches_color.png");
material->SetTexture(MaterialTextureType::Normal, "project\\terrain\\vegetation_tree_2\\branches_normal.png");
material->SetTexture(MaterialTextureType::Occlusion, "project\\terrain\\vegetation_tree_2\\branches_ao.png");
material->SetProperty(MaterialProperty::WindAnimation, 1.0f);
material->SetProperty(MaterialProperty::SubsurfaceScattering, 0.0f);
material->SetProperty(MaterialProperty::WorldSpaceHeight, renderable->GetBoundingBox(BoundingBoxType::Transformed).GetSize().y);
material->SetProperty(MaterialProperty::AnimationFoliageWind, 1.0f);
material->SetProperty(MaterialProperty::CullMode, static_cast<float>(RHI_CullMode::None));
renderable->SetMaterial(material);
}
Expand All @@ -673,8 +671,7 @@ namespace spartan
material->SetColor(Color::standard_white);
material->SetTexture(MaterialTextureType::Color, "project\\terrain\\vegetation_plant_1\\ormbunke.png");
material->SetProperty(MaterialProperty::SubsurfaceScattering, 0.0f);
material->SetProperty(MaterialProperty::WindAnimation, 1.0f);
material->SetProperty(MaterialProperty::WorldSpaceHeight, renderable->GetBoundingBox(BoundingBoxType::Transformed).GetSize().y);
material->SetProperty(MaterialProperty::AnimationFoliageWind, 1.0f);
material->SetProperty(MaterialProperty::CullMode, static_cast<float>(RHI_CullMode::None));
renderable->SetMaterial(material);

Expand Down Expand Up @@ -759,28 +756,28 @@ namespace spartan
// these are the ropes and the metal rings that hold them
if (Material* material = entity->GetDescendantByName("curtain_03_1")->GetComponent<Renderable>()->GetMaterial())
{
material->SetProperty(MaterialProperty::WindAnimation, 1.0f);
material->SetProperty(MaterialProperty::AnimationFoliageWind, 1.0f);
}

// this is fabric
if (Material* material = entity->GetDescendantByName("curtain_03_2")->GetComponent<Renderable>()->GetMaterial())
{
material->SetProperty(MaterialProperty::CullMode, static_cast<float>(RHI_CullMode::None));
material->SetProperty(MaterialProperty::WindAnimation, 1.0f);
material->SetProperty(MaterialProperty::CullMode, static_cast<float>(RHI_CullMode::None));
material->SetProperty(MaterialProperty::AnimationFoliageWind, 1.0f);
}

// this is fabric
if (Material* material = entity->GetDescendantByName("curtain_03_3")->GetComponent<Renderable>()->GetMaterial())
{
material->SetProperty(MaterialProperty::CullMode, static_cast<float>(RHI_CullMode::None));
material->SetProperty(MaterialProperty::WindAnimation, 1.0f);
material->SetProperty(MaterialProperty::CullMode, static_cast<float>(RHI_CullMode::None));
material->SetProperty(MaterialProperty::AnimationFoliageWind, 1.0f);
}

// this is fabric
if (Material* material = entity->GetDescendantByName("curtain_hanging_06_3")->GetComponent<Renderable>()->GetMaterial())
{
material->SetProperty(MaterialProperty::CullMode, static_cast<float>(RHI_CullMode::None));
material->SetProperty(MaterialProperty::WindAnimation, 1.0f);
material->SetProperty(MaterialProperty::CullMode, static_cast<float>(RHI_CullMode::None));
material->SetProperty(MaterialProperty::AnimationFoliageWind, 1.0f);
}
}
}
Expand All @@ -795,8 +792,8 @@ namespace spartan

if (Material* material = entity->GetDescendantByName("IvySim_Leaves")->GetComponent<Renderable>()->GetMaterial())
{
material->SetProperty(MaterialProperty::CullMode, static_cast<float>(RHI_CullMode::None));
material->SetProperty(MaterialProperty::WindAnimation, 1.0f);
material->SetProperty(MaterialProperty::CullMode, static_cast<float>(RHI_CullMode::None));
material->SetProperty(MaterialProperty::AnimationFoliageWind, 1.0f);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions runtime/Rendering/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ namespace spartan
case MaterialProperty::TextureOffsetX: return "texture_offset_x";
case MaterialProperty::TextureOffsetY: return "texture_offset_y";
case MaterialProperty::TextureSlopeBased: return "texture_slope_based";
case MaterialProperty::WindAnimation: return "vertex_animate_wind";
case MaterialProperty::VertexAnimateWater: return "vertex_animate_water";
case MaterialProperty::AnimationFoliageWind: return "vertex_animate_wind";
case MaterialProperty::AnimationWaterFlow: return "vertex_animate_water";
case MaterialProperty::CullMode: return "cull_mode";
case MaterialProperty::Max: return "max";
default:
Expand Down
6 changes: 3 additions & 3 deletions runtime/Rendering/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ namespace spartan
TextureOffsetX, // offset of the texture along the X-axis
TextureOffsetY, // offset of the texture along the Y-axis
TextureSlopeBased, // applies texture mapping based on the mesh slope
WindAnimation, // applies vertex-based animation to simulate wind
VertexAnimateWater, // applies vertex-based animation to simulate water flow
AnimationFoliageWind, // applies vertex-based animation to simulate wind
AnimationWaterFlow, // applies vertex-based animation to simulate water flow
CullMode, // sets the culling mode based on RHI_CullMode enum values
Gltf, // indicates if the material was imported from a glTF file
Max // total number of properties, used to size arrays
Expand Down Expand Up @@ -122,7 +122,7 @@ namespace spartan
bool IsTessellated() const
{
// the gbuffer will tesselate in one of these cases
return HasTextureOfType(MaterialTextureType::Height) || GetProperty(MaterialProperty::VertexAnimateWater);
return HasTextureOfType(MaterialTextureType::Height) || GetProperty(MaterialProperty::AnimationWaterFlow);
}

// misc
Expand Down
24 changes: 12 additions & 12 deletions runtime/Rendering/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -917,18 +917,18 @@ namespace spartan
properties[index].ior = material->GetProperty(MaterialProperty::Ior);

// flags
properties[index].flags = material->HasTextureOfType(MaterialTextureType::Height) ? (1U << 0) : 0;
properties[index].flags |= material->HasTextureOfType(MaterialTextureType::Normal) ? (1U << 1) : 0;
properties[index].flags |= material->HasTextureOfType(MaterialTextureType::Color) ? (1U << 2) : 0;
properties[index].flags |= material->HasTextureOfType(MaterialTextureType::Roughness) ? (1U << 3) : 0;
properties[index].flags |= material->HasTextureOfType(MaterialTextureType::Metalness) ? (1U << 4) : 0;
properties[index].flags |= material->HasTextureOfType(MaterialTextureType::AlphaMask) ? (1U << 5) : 0;
properties[index].flags |= material->HasTextureOfType(MaterialTextureType::Emission) ? (1U << 6) : 0;
properties[index].flags |= material->HasTextureOfType(MaterialTextureType::Occlusion) ? (1U << 7) : 0;
properties[index].flags |= material->GetProperty(MaterialProperty::TextureSlopeBased) ? (1U << 8) : 0;
properties[index].flags |= material->GetProperty(MaterialProperty::WindAnimation) ? (1U << 9) : 0;
properties[index].flags |= material->GetProperty(MaterialProperty::VertexAnimateWater) ? (1U << 10) : 0;
properties[index].flags |= material->IsTessellated() ? (1U << 11) : 0;
properties[index].flags = material->HasTextureOfType(MaterialTextureType::Height) ? (1U << 0) : 0;
properties[index].flags |= material->HasTextureOfType(MaterialTextureType::Normal) ? (1U << 1) : 0;
properties[index].flags |= material->HasTextureOfType(MaterialTextureType::Color) ? (1U << 2) : 0;
properties[index].flags |= material->HasTextureOfType(MaterialTextureType::Roughness) ? (1U << 3) : 0;
properties[index].flags |= material->HasTextureOfType(MaterialTextureType::Metalness) ? (1U << 4) : 0;
properties[index].flags |= material->HasTextureOfType(MaterialTextureType::AlphaMask) ? (1U << 5) : 0;
properties[index].flags |= material->HasTextureOfType(MaterialTextureType::Emission) ? (1U << 6) : 0;
properties[index].flags |= material->HasTextureOfType(MaterialTextureType::Occlusion) ? (1U << 7) : 0;
properties[index].flags |= material->GetProperty(MaterialProperty::TextureSlopeBased) ? (1U << 8) : 0;
properties[index].flags |= material->GetProperty(MaterialProperty::AnimationFoliageWind) ? (1U << 9) : 0;
properties[index].flags |= material->GetProperty(MaterialProperty::AnimationWaterFlow) ? (1U << 10) : 0;
properties[index].flags |= material->IsTessellated() ? (1U << 11) : 0;
// when changing the bit flags, ensure that you also update the Surface struct in common_structs.hlsl, so that it reads those flags as expected
}

Expand Down
8 changes: 8 additions & 0 deletions runtime/World/Components/Renderable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ namespace spartan
m_mesh = nullptr;
}

void Renderable::OnStart()
{
if (Material* material = GetMaterial())
{
material->SetProperty(MaterialProperty::WorldSpaceHeight, GetBoundingBox(BoundingBoxType::Transformed).GetSize().y);
}
}

void Renderable::Serialize(FileStream* stream)
{
// mesh
Expand Down
1 change: 1 addition & 0 deletions runtime/World/Components/Renderable.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ namespace spartan
~Renderable();

// icomponent
void OnStart() override;
void Serialize(FileStream* stream) override;
void Deserialize(FileStream* stream) override;

Expand Down

0 comments on commit 95ce0ff

Please sign in to comment.