Files
o3de/Gems/WhiteBox/Code/Source/Rendering/Atom/WhiteBoxAtomRenderMesh.cpp
T
Esteban Papp 1f9b284de2 Merge branch 'development' into cmake/SPEC-7179
Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com>

# Conflicts:
#	Code/Editor/Plugins/ComponentEntityEditorPlugin/ComponentEntityEditorPlugin_precompiled.h
#	Code/Editor/Plugins/EditorCommon/EditorCommon_precompiled.h
#	Code/Editor/Plugins/EditorCommon/stdafx.cpp
#	Code/Editor/Plugins/FFMPEGPlugin/FFMPEGPlugin_precompiled.h
#	Code/Editor/Plugins/PerforcePlugin/PerforcePlugin_precompiled.h
#	Code/Editor/Plugins/ProjectSettingsTool/ProjectSettingsTool_precompiled.h
#	Code/Framework/AzToolsFramework/AzToolsFramework/AzToolsFramework_precompiled.h
#	Code/Tools/AssetProcessor/native/precompiled.h
#	Code/Tools/Standalone/StandaloneTools_precompiled.h
#	Gems/AssetMemoryAnalyzer/Code/Source/AssetMemoryAnalyzer_precompiled.h
#	Gems/Atom/Asset/ImageProcessingAtom/Code/Source/ImageProcessing_precompiled.h
#	Gems/Atom/RHI/DX12/Code/Source/RHI/Atom_RHI_DX12_precompiled.h
#	Gems/Atom/RHI/Metal/Code/Include/Platform/Mac/Atom_RHI_Metal_precompiled_Platform.h
#	Gems/Atom/RHI/Metal/Code/Include/Platform/iOS/Atom_RHI_Metal_precompiled_Platform.h
#	Gems/Atom/RHI/Metal/Code/Source/Atom_RHI_Metal_precompiled.h
#	Gems/Atom/RHI/Metal/Code/atom_rhi_metal_common_files.cmake
#	Gems/Atom/RHI/Null/Code/Source/Atom_RHI_Null_precompiled.h
#	Gems/Atom/RHI/Null/Code/atom_rhi_null_common_files.cmake
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Android/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Linux/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Mac/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Windows/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Source/Atom_RHI_Vulkan_precompiled.h
#	Gems/Atom/RHI/Vulkan/Code/Source/RHI/SwapChain.cpp
#	Gems/Atom/RHI/Vulkan/Code/atom_rhi_vulkan_common_files.cmake
#	Gems/AtomLyIntegration/AtomFont/Code/Include/AtomLyIntegration/AtomFont/AtomFont_precompiled.h
#	Gems/Blast/Code/Source/StdAfx.cpp
#	Gems/Camera/Code/Source/Camera_precompiled.h
#	Gems/EMotionFX/Code/Source/EMotionFX_precompiled.h
#	Gems/FastNoise/Code/Source/FastNoise_precompiled.h
#	Gems/Gestures/Code/Source/Gestures_precompiled.h
#	Gems/GradientSignal/Code/Source/GradientSignal_precompiled.h
#	Gems/GraphCanvas/Code/precompiled.h
#	Gems/ImGui/Code/Source/ImGui_precompiled.h
#	Gems/InAppPurchases/Code/Source/InAppPurchases_precompiled.h
#	Gems/LmbrCentral/Code/Source/LmbrCentral_precompiled.h
#	Gems/LmbrCentral/Code/Tests/ShapeGeometryUtilTest.cpp
#	Gems/LyShine/Code/Editor/UiCanvasEditor_precompiled.h
#	Gems/LyShine/Code/Source/Animation/LyShine_precompiled.h
#	Gems/LyShine/Code/Source/LyShine_precompiled.h
#	Gems/LyShineExamples/Code/Source/LyShineExamples_precompiled.h
#	Gems/Maestro/Code/Source/Cinematics/Maestro_precompiled.h
#	Gems/Maestro/Code/Source/Maestro_precompiled.h
#	Gems/MessagePopup/Code/Source/MessagePopup_precompiled.h
#	Gems/Metastream/Code/Source/Metastream_precompiled.h
#	Gems/Microphone/Code/Source/Microphone_precompiled.h
#	Gems/Multiplayer/Code/Source/Multiplayer_precompiled.h
#	Gems/PhysX/Code/NumericalMethods/Source/NumericalMethods_precompiled.h
#	Gems/PhysX/Code/Source/PhysXUnsupported_precompiled.h
#	Gems/PhysX/Code/Source/PhysX_precompiled.h
#	Gems/PhysX/Code/physx_unsupported_files.cmake
#	Gems/PhysXDebug/Code/Source/PhysXDebugUnsupported_precompiled.h
#	Gems/PhysXDebug/Code/Source/PhysXDebug_precompiled.h
#	Gems/ScriptCanvas/Code/Editor/precompiled.h
#	Gems/ScriptCanvas/Code/Source/precompiled.h
#	Gems/ScriptCanvasDeveloper/Code/Source/precompiled.h
#	Gems/ScriptCanvasPhysics/Code/Source/ScriptCanvasPhysics_precompiled.h
#	Gems/ScriptEvents/Code/Source/precompiled.h
#	Gems/ScriptEvents/Code/Tests/Editor/EditorTests.cpp
#	Gems/ScriptedEntityTweener/Code/Source/ScriptedEntityTweener_precompiled.h
#	Gems/SliceFavorites/Code/Source/SliceFavorites_precompiled.h
#	Gems/StartingPointCamera/Code/Source/StartingPointCamera_precompiled.h
#	Gems/StartingPointInput/Code/Source/StartingPointInput_precompiled.h
#	Gems/StartingPointMovement/Code/Source/StartingPointMovement_precompiled.h
#	Gems/SurfaceData/Code/Source/SurfaceData_precompiled.h
#	Gems/TextureAtlas/Code/Source/TextureAtlas_precompiled.h
#	Gems/TickBusOrderViewer/Code/Source/TickBusOrderViewer_precompiled.h
#	Gems/Twitch/Code/Source/Twitch_precompiled.h
#	Gems/VirtualGamepad/Code/Source/VirtualGamepad_precompiled.h
#	Gems/WhiteBox/Code/Source/WhiteBoxUnsupported_precompiled.h
#	Gems/WhiteBox/Code/Source/WhiteBox_precompiled.h
2021-07-16 15:42:37 -07:00

291 lines
9.5 KiB
C++

/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#include "WhiteBoxAtomRenderMesh.h"
#include <Rendering/Atom/WhiteBoxMeshAtomData.h>
#include <Rendering/WhiteBoxRenderData.h>
#include <Util/WhiteBoxMathUtil.h>
#include <Viewport/WhiteBoxViewportConstants.h>
#include <Atom/RPI.Public/Model/Model.h>
#include <Atom/RPI.Public/Scene.h>
#include <Atom/RPI.Reflect/Asset/AssetUtils.h>
#include <Atom/RPI.Reflect/Model/ModelAssetCreator.h>
#include <Atom/RPI.Reflect/Model/ModelLodAssetCreator.h>
#include <Atom/RPI.Reflect/ResourcePoolAssetCreator.h>
#include <AtomLyIntegration/CommonFeatures/Material/MaterialComponentBus.h>
#include <AzCore/Math/PackedVector3.h>
namespace WhiteBox
{
bool AtomRenderMesh::AreAttributesValid() const
{
bool attributesAreValid = true;
for (const auto& attribute : m_attributes)
{
AZStd::visit(
[&attributesAreValid](const auto& att)
{
if (!att->IsValid())
{
attributesAreValid = false;
}
},
attribute);
}
return attributesAreValid;
}
bool AtomRenderMesh::CreateMeshBuffers(const WhiteBoxMeshAtomData& meshData)
{
m_indexBuffer = AZStd::make_unique<IndexBuffer>(meshData.GetIndices());
CreateAttributeBuffer<AttributeType::Position>(meshData.GetPositions());
CreateAttributeBuffer<AttributeType::Normal>(meshData.GetNormals());
CreateAttributeBuffer<AttributeType::Tangent>(meshData.GetTangents());
CreateAttributeBuffer<AttributeType::Bitangent>(meshData.GetBitangents());
CreateAttributeBuffer<AttributeType::UV>(meshData.GetUVs());
CreateAttributeBuffer<AttributeType::Color>(meshData.GetColors());
return AreAttributesValid();
}
bool AtomRenderMesh::UpdateMeshBuffers(const WhiteBoxMeshAtomData& meshData)
{
UpdateAttributeBuffer<AttributeType::Position>(meshData.GetPositions());
UpdateAttributeBuffer<AttributeType::Normal>(meshData.GetNormals());
UpdateAttributeBuffer<AttributeType::Tangent>(meshData.GetTangents());
UpdateAttributeBuffer<AttributeType::Bitangent>(meshData.GetBitangents());
UpdateAttributeBuffer<AttributeType::UV>(meshData.GetUVs());
UpdateAttributeBuffer<AttributeType::Color>(meshData.GetColors());
return AreAttributesValid();
}
void AtomRenderMesh::AddLodBuffers(AZ::RPI::ModelLodAssetCreator& modelLodCreator)
{
modelLodCreator.SetLodIndexBuffer(m_indexBuffer->GetBuffer());
for (auto& attribute : m_attributes)
{
AZStd::visit(
[&modelLodCreator](auto& att)
{
att->AddLodStreamBuffer(modelLodCreator);
},
attribute);
}
}
void AtomRenderMesh::AddMeshBuffers(AZ::RPI::ModelLodAssetCreator& modelLodCreator)
{
modelLodCreator.SetMeshIndexBuffer(m_indexBuffer->GetBufferAssetView());
for (auto& attribute : m_attributes)
{
AZStd::visit(
[&modelLodCreator](auto&& att)
{
att->AddMeshStreamBuffer(modelLodCreator);
},
attribute);
}
}
bool AtomRenderMesh::CreateLodAsset(const WhiteBoxMeshAtomData& meshData)
{
if (!CreateMeshBuffers(meshData))
{
return false;
}
AZ::RPI::ModelLodAssetCreator modelLodCreator;
modelLodCreator.Begin(AZ::Data::AssetId(AZ::Uuid::CreateRandom()));
AddLodBuffers(modelLodCreator);
modelLodCreator.BeginMesh();
modelLodCreator.SetMeshAabb(meshData.GetAabb());
// set the default material
if (auto materialAsset = AZ::RPI::AssetUtils::LoadAssetByProductPath<AZ::RPI::MaterialAsset>(TexturedMaterialPath.data()))
{
modelLodCreator.SetMeshMaterialAsset(materialAsset);
}
else
{
AZ_Error("CreateLodAsset", false, "Could not load material.");
return false;
}
AddMeshBuffers(modelLodCreator);
modelLodCreator.EndMesh();
if (!modelLodCreator.End(m_lodAsset))
{
AZ_Error("CreateLodAsset", false, "Couldn't create LoD asset.");
return false;
}
if (!m_lodAsset.IsReady())
{
AZ_Error("CreateLodAsset", false, "LoD asset is not ready.");
return false;
}
if (!m_lodAsset.Get())
{
AZ_Error("CreateLodAsset", false, "LoD asset is nullptr.");
return false;
}
return true;
}
void AtomRenderMesh::CreateModelAsset()
{
AZ::RPI::ModelAssetCreator modelCreator;
modelCreator.Begin(AZ::Data::AssetId(AZ::Uuid::CreateRandom()));
modelCreator.SetName(ModelName);
modelCreator.AddLodAsset(AZStd::move(m_lodAsset));
modelCreator.End(m_modelAsset);
}
bool AtomRenderMesh::CreateModel(AZ::EntityId entityId)
{
m_model = AZ::RPI::Model::FindOrCreate(m_modelAsset);
m_meshFeatureProcessor =
AZ::RPI::Scene::GetFeatureProcessorForEntity<AZ::Render::MeshFeatureProcessorInterface>(entityId);
if (!m_meshFeatureProcessor)
{
AZ_Error(
"MeshComponentController", m_meshFeatureProcessor,
"Unable to find a MeshFeatureProcessorInterface on the entityId.");
return false;
}
m_meshFeatureProcessor->ReleaseMesh(m_meshHandle);
m_meshHandle = m_meshFeatureProcessor->AcquireMesh(AZ::Render::MeshHandleDescriptor{ m_modelAsset });
return true;
}
bool AtomRenderMesh::MeshRequiresFullRebuild([[maybe_unused]] const WhiteBoxMeshAtomData& meshData) const
{
return meshData.VertexCount() != m_vertexCount;
}
bool AtomRenderMesh::CreateMesh(const WhiteBoxMeshAtomData& meshData, AZ::EntityId entityId)
{
if (!CreateLodAsset(meshData))
{
// TODO: LYN-808
return false;
}
CreateModelAsset();
if (!CreateModel(entityId))
{
// TODO: LYN-808
return false;
}
m_vertexCount = meshData.VertexCount();
return true;
}
bool AtomRenderMesh::DoesMeshRequireFullRebuild([[maybe_unused]] const WhiteBoxMeshAtomData& meshData) const
{
// this has been disabled due to a some recent updates with Atom that a) cause visual artefacts
// when updating the buffers and b) have a big performance boost when rebuilding from scratch anyway.
//
// this method for building the mesh will probably be replace anyway when the Atom DynamicDraw support
// comes online.
return true; // meshData.VertexCount() != m_vertexCount;
}
void AtomRenderMesh::BuildMesh(
const WhiteBoxRenderData& renderData, const AZ::Transform& worldFromLocal, AZ::EntityId entityId)
{
const WhiteBoxFaces culledFaceList = BuildCulledWhiteBoxFaces(renderData.m_faces);
const WhiteBoxMeshAtomData meshData(culledFaceList);
if (DoesMeshRequireFullRebuild(meshData))
{
if (!CreateMesh(meshData, entityId))
{
// TODO: LYN-808
return;
}
}
else
{
if (!UpdateMeshBuffers(meshData))
{
// TODO: LYN-808
return;
}
}
UpdateTransform(worldFromLocal);
}
void AtomRenderMesh::UpdateTransform(const AZ::Transform& worldFromLocal)
{
m_meshFeatureProcessor->SetTransform(m_meshHandle, worldFromLocal);
}
void AtomRenderMesh::UpdateMaterial([[maybe_unused]] const WhiteBoxMaterial& material)
{
// TODO: LYN-784
// colors: vertex colors probs not used.
// (use constant color for material -> material editor)
//
// m_meshFeatureProcessor->SetMaterialAssignmentMap() (not in spectra mainline yet)
//
// for now:
// m_meshFeatureProcessor->ReleaseMesh(m_meshHandle);
// MaterialAssignmentMap materialMap;
// materialMap[DefaultMaterialAssignmentId] = (see below)
// m_meshHandle = m_meshFeatureProcessor->AcquireMesh(m_modelAsset, materialMap)
// from tommy (SkinnedMeshContainer.cpp)
/*
auto materialAsset =
AZ::RPI::AssetUtils::LoadAssetByProductPath<AZ::RPI::MaterialAsset>(SkinnedMeshMaterial); auto
materialOverrideInstance = AZ::RPI::Material::FindOrCreate(materialAsset);
AZ::Render::MaterialAssignmentMap materialMap;
materialMap[AZ::Render::DefaultMaterialAssignmentId].m_materialAsset = materialAsset;
materialMap[AZ::Render::DefaultMaterialAssignmentId].m_materialInstance = materialOverrideInstance;
*/
/*
changing material properties:
(todo)
*/
}
bool AtomRenderMesh::IsVisible() const
{
// TODO: LYN-788
return true;
}
void AtomRenderMesh::SetVisiblity([[maybe_unused]] bool visibility)
{
// TODO: LYN-788
// hide: m_meshFeatureProcessor->ReleaseMesh(m_meshHandle);
// show: m_meshHandle = m_meshFeatureProcessor->AcquireMesh(m_modelAsset);
}
} // namespace WhiteBox