942 lines
38 KiB
C++
942 lines
38 KiB
C++
#include "MetaCoreRender/MetaCoreFilamentSceneBridge.h"
|
||
#include "MetaCorePlatform/MetaCoreWindow.h"
|
||
#include "MetaCoreScene/MetaCoreScene.h"
|
||
#include "MetaCoreScene/MetaCoreComponents.h"
|
||
#include "MetaCoreRender/MetaCoreRenderTypes.h"
|
||
#include "MetaCoreRender/MetaCoreImGuiHelper.h"
|
||
#include "MetaCoreRender/MetaCoreSceneRenderSync.h"
|
||
#include <imgui.h>
|
||
#include "MetaCoreFoundation/MetaCoreAssetRegistry.h"
|
||
|
||
#include <filament/Engine.h>
|
||
#include <filament/Scene.h>
|
||
#include <filament/View.h>
|
||
#include <filament/Camera.h>
|
||
#include <filament/SwapChain.h>
|
||
#include <filament/Renderer.h>
|
||
#include <filament/Viewport.h>
|
||
#include <filament/TransformManager.h>
|
||
#include <filament/Texture.h>
|
||
#include <filament/RenderTarget.h>
|
||
#include <filament/LightManager.h>
|
||
|
||
#include <gltfio/AssetLoader.h>
|
||
#include <gltfio/FilamentAsset.h>
|
||
#include <gltfio/ResourceLoader.h>
|
||
#include <gltfio/MaterialProvider.h>
|
||
|
||
#include <utils/Entity.h>
|
||
#include <utils/EntityManager.h>
|
||
#include <utils/NameComponentManager.h>
|
||
#include "MetaCoreScene/MetaCoreTransformUtils.h"
|
||
|
||
#define GLM_ENABLE_EXPERIMENTAL
|
||
#include <glm/gtc/type_ptr.hpp>
|
||
#include <glm/geometric.hpp>
|
||
#include <glm/gtc/matrix_transform.hpp>
|
||
#include <glm/gtx/matrix_decompose.hpp>
|
||
#include <glm/gtc/quaternion.hpp>
|
||
|
||
#include <iostream>
|
||
#include <fstream>
|
||
#include <algorithm>
|
||
#include <unordered_map>
|
||
#include <unordered_set>
|
||
#include <map>
|
||
#include <vector>
|
||
|
||
// 引入 Windows 和 OpenGL 头文件以创建纹理
|
||
#define WIN32_LEAN_AND_MEAN
|
||
#include <windows.h>
|
||
#include <GL/gl.h>
|
||
|
||
namespace MetaCore {
|
||
|
||
class MetaCoreFilamentSceneBridge::MetaCoreFilamentSceneBridgeImpl {
|
||
public:
|
||
MetaCoreFilamentSceneBridgeImpl() = default;
|
||
|
||
~MetaCoreFilamentSceneBridgeImpl() {
|
||
Shutdown();
|
||
}
|
||
|
||
// 提供公有接口用于构建渲染同步快照
|
||
MetaCoreSceneRenderSyncSnapshot BuildSnapshot(const MetaCoreScene& scene) const {
|
||
return RenderSync_.BuildSnapshot(scene);
|
||
}
|
||
|
||
bool Initialize(MetaCoreWindow& window) {
|
||
std::cout << "FilamentSceneBridge: Initializing..." << std::endl;
|
||
|
||
// 创建 Filament Engine (不共享上下文,避免闪退)
|
||
Engine_ = filament::Engine::create();
|
||
|
||
if (!Engine_) {
|
||
std::cerr << "Failed to create Filament Engine with shared context!" << std::endl;
|
||
return false;
|
||
}
|
||
|
||
// 创建绑定真实窗口的交换链
|
||
SwapChain_ = Engine_->createSwapChain((void*)window.GetNativeWindowHandle());
|
||
if (!SwapChain_) {
|
||
std::cerr << "Failed to create Filament SwapChain with window handle!" << std::endl;
|
||
return false;
|
||
}
|
||
|
||
// 创建渲染器
|
||
Renderer_ = Engine_->createRenderer();
|
||
|
||
// 创建场景
|
||
Scene_ = Engine_->createScene();
|
||
|
||
// 创建默认灯光
|
||
Light_ = utils::EntityManager::get().create();
|
||
filament::LightManager::Builder(filament::LightManager::Type::DIRECTIONAL)
|
||
.color(filament::Color::toLinear<filament::ACCURATE>({ 1.0f, 1.0f, 1.0f }))
|
||
.intensity(100000.0f)
|
||
.direction({ 0.5f, 0.5f, -1.0f }) // Z-up 下 Z 向下
|
||
.castShadows(true)
|
||
.build(*Engine_, Light_);
|
||
Scene_->addEntity(Light_);
|
||
|
||
// 创建相机
|
||
utils::Entity cameraEntity = utils::EntityManager::get().create();
|
||
Camera_ = Engine_->createCamera(cameraEntity);
|
||
|
||
// 创建视图
|
||
View_ = Engine_->createView();
|
||
View_->setScene(Scene_);
|
||
View_->setCamera(Camera_);
|
||
|
||
// 创建 UI 视图
|
||
UIView_ = Engine_->createView();
|
||
|
||
// 创建 ImGuiHelper
|
||
ImGuiHelper_ = new MetaCoreImGuiHelper(Engine_, UIView_, "", ImGui::GetCurrentContext());
|
||
|
||
// 初始化离屏渲染纹理
|
||
auto [width, height] = window.GetFramebufferSize();
|
||
|
||
// 1. 创建 Filament 颜色纹理
|
||
FilamentTexture_ = filament::Texture::Builder()
|
||
.width(static_cast<uint32_t>(width))
|
||
.height(static_cast<uint32_t>(height))
|
||
.usage(filament::Texture::Usage::COLOR_ATTACHMENT | filament::Texture::Usage::SAMPLEABLE)
|
||
.format(filament::Texture::InternalFormat::RGBA8)
|
||
.build(*Engine_);
|
||
|
||
// 2. 创建深度纹理
|
||
DepthTexture_ = filament::Texture::Builder()
|
||
.width(static_cast<uint32_t>(width))
|
||
.height(static_cast<uint32_t>(height))
|
||
.usage(filament::Texture::Usage::DEPTH_ATTACHMENT)
|
||
.format(filament::Texture::InternalFormat::DEPTH24)
|
||
.build(*Engine_);
|
||
|
||
// 3. 创建 RenderTarget 并绑定
|
||
RenderTarget_ = filament::RenderTarget::Builder()
|
||
.texture(filament::RenderTarget::AttachmentPoint::COLOR, FilamentTexture_)
|
||
.texture(filament::RenderTarget::AttachmentPoint::DEPTH, DepthTexture_)
|
||
.build(*Engine_);
|
||
|
||
// 4. 设置到 View
|
||
View_->setRenderTarget(RenderTarget_);
|
||
View_->setViewport({0, 0, static_cast<uint32_t>(width), static_cast<uint32_t>(height)});
|
||
|
||
// 确保开启后处理(为了HDR)
|
||
View_->setPostProcessingEnabled(true);
|
||
|
||
// 初始化 gltfio
|
||
MaterialProvider_ = filament::gltfio::createJitShaderProvider(Engine_);
|
||
NameManager_ = new utils::NameComponentManager(utils::EntityManager::get());
|
||
AssetLoader_ = filament::gltfio::AssetLoader::create({ Engine_, MaterialProvider_, NameManager_ });
|
||
|
||
std::cout << "FilamentSceneBridge: Initialized with Offscreen Texture ID: " << GLTextureId_ << std::endl;
|
||
return true;
|
||
}
|
||
|
||
void Shutdown() {
|
||
if (Engine_) {
|
||
std::cout << "FilamentSceneBridge: Shutting down..." << std::endl;
|
||
|
||
// 销毁所有加载的资产
|
||
for (auto& [id, asset] : LoadedAssets_) {
|
||
AssetLoader_->destroyAsset(asset);
|
||
}
|
||
LoadedAssets_.clear();
|
||
ObjectToFilamentEntity_.clear();
|
||
ObjectWorldMatrices_.clear();
|
||
|
||
for (auto& [id, entity] : SceneLightEntities_) {
|
||
if (Scene_) {
|
||
Scene_->remove(entity);
|
||
}
|
||
Engine_->destroy(entity);
|
||
utils::EntityManager::get().destroy(entity);
|
||
}
|
||
SceneLightEntities_.clear();
|
||
|
||
// 销毁 gltfio 资源
|
||
if (AssetLoader_) {
|
||
filament::gltfio::AssetLoader::destroy(&AssetLoader_);
|
||
AssetLoader_ = nullptr;
|
||
}
|
||
if (MaterialProvider_) {
|
||
MaterialProvider_->destroyMaterials();
|
||
delete MaterialProvider_;
|
||
MaterialProvider_ = nullptr;
|
||
}
|
||
|
||
// 销毁离屏渲染资源
|
||
if (RenderTarget_) {
|
||
Engine_->destroy(RenderTarget_);
|
||
RenderTarget_ = nullptr;
|
||
}
|
||
if (FilamentTexture_) {
|
||
Engine_->destroy(FilamentTexture_);
|
||
FilamentTexture_ = nullptr;
|
||
}
|
||
if (DepthTexture_) {
|
||
Engine_->destroy(DepthTexture_);
|
||
DepthTexture_ = nullptr;
|
||
}
|
||
if (GLTextureId_) {
|
||
glDeleteTextures(1, &GLTextureId_);
|
||
GLTextureId_ = 0;
|
||
}
|
||
|
||
// 销毁 View
|
||
Engine_->destroy(View_);
|
||
|
||
// 销毁相机组件和实体
|
||
if (Camera_) {
|
||
utils::Entity cameraEntity = Camera_->getEntity();
|
||
Engine_->destroyCameraComponent(cameraEntity);
|
||
utils::EntityManager::get().destroy(cameraEntity);
|
||
}
|
||
|
||
// 销毁其他资源
|
||
Engine_->destroy(Scene_);
|
||
Engine_->destroy(Renderer_);
|
||
Engine_->destroy(SwapChain_);
|
||
|
||
delete NameManager_;
|
||
NameManager_ = nullptr;
|
||
|
||
// 最后销毁 Engine
|
||
filament::Engine::destroy(&Engine_);
|
||
Engine_ = nullptr;
|
||
}
|
||
}
|
||
|
||
void SetProjectRootPath(const std::filesystem::path& projectRootPath) {
|
||
ProjectRootPath_ = projectRootPath;
|
||
}
|
||
|
||
void ParseModelHierarchyToEcs(MetaCoreScene& scene, MetaCoreGameObject& parentObject, filament::gltfio::FilamentAsset* asset) {
|
||
if (!asset) return;
|
||
|
||
const utils::Entity* entities = asset->getEntities();
|
||
size_t entityCount = asset->getEntityCount();
|
||
|
||
auto& tm = Engine_->getTransformManager();
|
||
|
||
// 建立 Filament Entity 到 MetaCore GameObject 的映射
|
||
std::map<utils::Entity, MetaCoreGameObject> entityToObj;
|
||
entityToObj[asset->getRoot()] = parentObject;
|
||
|
||
// 【超级核心】:绑定顶级父 GameObject 标识到 Filament Asset 根 Entity,彻底打通顶级 Gizmo 的同步!
|
||
ObjectToFilamentEntity_[parentObject.GetId()] = { asset, asset->getRoot() };
|
||
|
||
// 预先收集并缓存 parentObject 子树下的所有已有 GameObject,用于高精度的三步复用匹配
|
||
std::vector<MetaCoreGameObject> subtreeObjects;
|
||
std::vector<MetaCoreId> subtreeIds = scene.GetSubtreeObjectIds(parentObject.GetId());
|
||
for (MetaCoreId subId : subtreeIds) {
|
||
auto subObj = scene.FindGameObject(subId);
|
||
if (subObj && subObj.GetId() != parentObject.GetId()) {
|
||
subtreeObjects.push_back(subObj);
|
||
}
|
||
}
|
||
|
||
// 第一遍:创建或复用所有对象
|
||
for (size_t i = 0; i < entityCount; i++) {
|
||
utils::Entity entity = entities[i];
|
||
if (entity == asset->getRoot()) continue;
|
||
|
||
const char* nodeName = asset->getName(entity);
|
||
std::string name = nodeName ? nodeName : ("Node_" + std::to_string(i));
|
||
|
||
MetaCoreGameObject childObj;
|
||
bool isExisting = false;
|
||
|
||
// 1. 优先通过 ModelNodeIndex 进行精确匹配(针对带有网格的节点)
|
||
for (auto& sceneObj : subtreeObjects) {
|
||
if (sceneObj.HasComponent<MetaCoreMeshRendererComponent>()) {
|
||
auto& mesh = sceneObj.GetComponent<MetaCoreMeshRendererComponent>();
|
||
if (mesh.ModelNodeIndex == static_cast<std::int32_t>(i)) {
|
||
childObj = sceneObj;
|
||
isExisting = true;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
// 2. 如果没找到(针对没有网格的空节点),通过名字进行匹配复用
|
||
if (!isExisting) {
|
||
for (auto& sceneObj : subtreeObjects) {
|
||
if (sceneObj.GetName() == name) {
|
||
// 且该节点不能已经被其他实体映射占用
|
||
bool alreadyMapped = false;
|
||
for (auto& [ent, obj] : entityToObj) {
|
||
if (obj.GetId() == sceneObj.GetId()) {
|
||
alreadyMapped = true;
|
||
break;
|
||
}
|
||
}
|
||
if (!alreadyMapped) {
|
||
childObj = sceneObj;
|
||
isExisting = true;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// 3. 实在没有找到,才进行动态创建(仅作为降级兜底)
|
||
if (!isExisting) {
|
||
childObj = scene.CreateGameObject(name, parentObject.GetId());
|
||
auto& meshRenderer = childObj.AddComponent<MetaCoreMeshRendererComponent>();
|
||
meshRenderer.MeshSource = MetaCoreMeshSourceKind::Asset;
|
||
meshRenderer.ModelNodeIndex = static_cast<std::int32_t>(i);
|
||
}
|
||
|
||
entityToObj[entity] = childObj;
|
||
ObjectToFilamentEntity_[childObj.GetId()] = { asset, entity };
|
||
|
||
// 获取并设置 Transform
|
||
auto instance = tm.getInstance(entity);
|
||
if (instance) {
|
||
filament::math::mat4f localTransform = tm.getTransform(instance);
|
||
|
||
glm::mat4 filamentMatrix;
|
||
std::memcpy(glm::value_ptr(filamentMatrix), &localTransform[0][0], sizeof(float) * 16);
|
||
|
||
glm::mat4 R_minus90X = glm::rotate(glm::mat4(1.0f), glm::radians(-90.0f), {1, 0, 0});
|
||
glm::mat4 R_plus90X = glm::rotate(glm::mat4(1.0f), glm::radians(90.0f), {1, 0, 0});
|
||
glm::mat4 matrix = R_minus90X * filamentMatrix * R_plus90X;
|
||
|
||
// 拆解矩阵
|
||
glm::vec3 scale;
|
||
glm::quat rotation;
|
||
glm::vec3 translation;
|
||
glm::vec3 skew;
|
||
glm::vec4 perspective;
|
||
glm::decompose(matrix, scale, rotation, translation, skew, perspective);
|
||
|
||
auto& transform = childObj.GetComponent<MetaCoreTransformComponent>();
|
||
transform.Position = translation;
|
||
transform.RotationEulerDegrees = glm::degrees(glm::eulerAngles(rotation));
|
||
transform.Scale = scale;
|
||
}
|
||
}
|
||
|
||
// 第二遍:修正父子关系
|
||
for (size_t i = 0; i < entityCount; i++) {
|
||
utils::Entity entity = entities[i];
|
||
if (entity == asset->getRoot()) continue;
|
||
|
||
auto instance = tm.getInstance(entity);
|
||
if (instance) {
|
||
utils::Entity parentEntity = tm.getParent(instance);
|
||
if (parentEntity && entityToObj.count(parentEntity)) {
|
||
MetaCoreGameObject currentObj = entityToObj[entity];
|
||
MetaCoreGameObject parentObj = entityToObj[parentEntity];
|
||
currentObj.SetParentId(parentObj.GetId());
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
static std::string NormalizePath(std::string path) {
|
||
std::replace(path.begin(), path.end(), '\\', '/');
|
||
return path;
|
||
}
|
||
|
||
void SyncScene(const MetaCoreSceneRenderSyncSnapshot& snapshot, MetaCoreScene* scene, bool compatibilityMeshOnly, bool useScenePrimaryCamera) {
|
||
if (!AssetLoader_) return;
|
||
(void)compatibilityMeshOnly;
|
||
|
||
LastSyncSnapshot_ = snapshot;
|
||
ObjectWorldMatrices_ = LastSyncSnapshot_.WorldMatrices;
|
||
SyncSceneLights(LastSyncSnapshot_);
|
||
if (useScenePrimaryCamera) {
|
||
MetaCoreSceneView primarySceneView;
|
||
if (MetaCoreSceneRenderSync::TryBuildSceneViewFromPrimaryCamera(LastSyncSnapshot_, primarySceneView)) {
|
||
ApplySceneView(primarySceneView);
|
||
}
|
||
}
|
||
|
||
for (const auto& renderable : snapshot.Renderables) {
|
||
if (renderable.MeshSource != MetaCoreMeshSourceKind::Asset &&
|
||
renderable.MeshSource != MetaCoreMeshSourceKind::Builtin) {
|
||
continue;
|
||
}
|
||
|
||
std::string modelPath;
|
||
if (renderable.SourceModelAssetGuid.IsValid()) {
|
||
modelPath = MetaCoreAssetRegistry::Get().ResolveGuidToPath(renderable.SourceModelAssetGuid).generic_string();
|
||
}
|
||
if (modelPath.empty() && !renderable.SourceModelPath.empty()) {
|
||
modelPath = renderable.SourceModelPath;
|
||
}
|
||
if (modelPath.empty() && renderable.MeshSource == MetaCoreMeshSourceKind::Builtin && renderable.BuiltinMesh == MetaCoreBuiltinMeshType::Cube) {
|
||
modelPath = "Assets/Models/box1.glb";
|
||
}
|
||
if (modelPath.empty()) {
|
||
continue;
|
||
}
|
||
|
||
std::filesystem::path absolutePath = ProjectRootPath_ / modelPath;
|
||
|
||
MetaCoreId hostRootId = renderable.HostRootId;
|
||
std::string hostRootName = renderable.HostRootName;
|
||
|
||
// 如果该模型的顶级根宿主已经被加载过,则不需要重复加载,直接更新变换
|
||
if (LoadedAssets_.contains(hostRootId)) {
|
||
continue;
|
||
}
|
||
|
||
// 加载新模型,以顶级根宿主 hostRootId 进行注册存储
|
||
std::cout << "FilamentSceneBridge: Loading GLTF: " << absolutePath << " (Host: " << hostRootName << ")" << std::endl;
|
||
|
||
std::ifstream file(absolutePath, std::ios::binary);
|
||
if (!file.is_open()) {
|
||
std::cerr << "Failed to open file: " << absolutePath << std::endl;
|
||
continue;
|
||
}
|
||
|
||
std::vector<char> buffer((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
||
|
||
filament::gltfio::FilamentAsset* asset = AssetLoader_->createAsset(
|
||
reinterpret_cast<const uint8_t*>(buffer.data()),
|
||
static_cast<uint32_t>(buffer.size())
|
||
);
|
||
|
||
if (!asset) {
|
||
std::cerr << "Failed to create asset!" << std::endl;
|
||
continue;
|
||
}
|
||
|
||
// 加载贴图等资源
|
||
filament::gltfio::ResourceLoader resourceLoader({ Engine_ });
|
||
resourceLoader.loadResources(asset);
|
||
|
||
// 添加到场景
|
||
Scene_->addEntities(asset->getEntities(), asset->getEntityCount());
|
||
|
||
LoadedAssets_[hostRootId] = asset;
|
||
|
||
if (scene != nullptr) {
|
||
// 有 ECS 实例模式下同步层级结构并补挂 Tag
|
||
auto hostRootObj = scene->FindGameObject(hostRootId);
|
||
if (hostRootObj) {
|
||
MetaCoreGameObject nonConstHostRoot = hostRootObj;
|
||
|
||
// 现场补挂 Tag,确保后续帧和拖拽时 100% 命中 Tag 快速通道,绝不走 Fallback
|
||
if (!nonConstHostRoot.HasComponent<MetaCoreModelRootTag>()) {
|
||
nonConstHostRoot.AddComponent<MetaCoreModelRootTag>(MetaCoreModelRootTag{ modelPath });
|
||
}
|
||
|
||
// 如果名字是默认的 "Cube",自动改为模型文件名
|
||
if (nonConstHostRoot.GetName() == "Cube") {
|
||
std::string filename = std::filesystem::path(modelPath).filename().string();
|
||
nonConstHostRoot.GetName() = filename;
|
||
}
|
||
|
||
ParseModelHierarchyToEcs(*scene, nonConstHostRoot, asset);
|
||
}
|
||
} else {
|
||
// 如果 scene 为 nullptr,代表纯快照渲染模式。我们需要直接在 ObjectToFilamentEntity_ 中建立子实体的映射。
|
||
// 顶级父 ID 对应 asset->getRoot()
|
||
ObjectToFilamentEntity_[hostRootId] = { asset, asset->getRoot() };
|
||
|
||
// 遍历新资产中的 entities 映射到 snapshot 中的 renderable 节点
|
||
const utils::Entity* entities = asset->getEntities();
|
||
size_t entityCount = asset->getEntityCount();
|
||
|
||
std::vector<MetaCoreRenderSyncRenderable> candidates;
|
||
for (const auto& r : snapshot.Renderables) {
|
||
if (r.HostRootId == hostRootId && r.ObjectId != hostRootId) {
|
||
candidates.push_back(r);
|
||
}
|
||
}
|
||
|
||
std::map<utils::Entity, MetaCoreId> entityToId;
|
||
entityToId[asset->getRoot()] = hostRootId;
|
||
|
||
for (size_t i = 0; i < entityCount; i++) {
|
||
utils::Entity entity = entities[i];
|
||
if (entity == asset->getRoot()) continue;
|
||
|
||
const char* nodeName = asset->getName(entity);
|
||
std::string name = nodeName ? nodeName : ("Node_" + std::to_string(i));
|
||
|
||
MetaCoreId childObjectId = 0;
|
||
bool isExisting = false;
|
||
|
||
// 1. 优先通过 ModelNodeIndex 进行匹配
|
||
for (const auto& cand : candidates) {
|
||
if (cand.ModelNodeIndex == static_cast<std::int32_t>(i)) {
|
||
childObjectId = cand.ObjectId;
|
||
isExisting = true;
|
||
break;
|
||
}
|
||
}
|
||
|
||
// 2. 其次通过名字匹配
|
||
if (!isExisting) {
|
||
for (const auto& cand : candidates) {
|
||
if (cand.Name == name) {
|
||
bool alreadyMapped = false;
|
||
for (auto& [ent, id] : entityToId) {
|
||
if (id == cand.ObjectId) {
|
||
alreadyMapped = true;
|
||
break;
|
||
}
|
||
}
|
||
if (!alreadyMapped) {
|
||
childObjectId = cand.ObjectId;
|
||
isExisting = true;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
if (isExisting && childObjectId != 0) {
|
||
entityToId[entity] = childObjectId;
|
||
ObjectToFilamentEntity_[childObjectId] = { asset, entity };
|
||
}
|
||
}
|
||
}
|
||
|
||
// 创建中间 Pivot 实体用于坐标系转换 (glTF Y-up -> MetaCore Z-up)
|
||
utils::Entity pivotEntity = utils::EntityManager::get().create();
|
||
auto& tm = Engine_->getTransformManager();
|
||
tm.create(pivotEntity);
|
||
|
||
// 将资产根节点挂载到 Pivot 下
|
||
utils::Entity assetRoot = asset->getRoot();
|
||
tm.setParent(tm.getInstance(assetRoot), tm.getInstance(pivotEntity));
|
||
|
||
// 获取原始局部变换并叠加旋转补偿
|
||
filament::math::mat4f originalLocalTransform = tm.getTransform(tm.getInstance(assetRoot));
|
||
glm::mat4 originalMatrix;
|
||
std::memcpy(glm::value_ptr(originalMatrix), &originalLocalTransform[0][0], sizeof(float) * 16);
|
||
|
||
glm::mat4 compensationMatrix = glm::rotate(glm::mat4(1.0f), glm::radians(-90.0f), {1, 0, 0});
|
||
glm::mat4 finalRootMatrix = compensationMatrix * originalMatrix;
|
||
|
||
tm.setTransform(tm.getInstance(assetRoot), *reinterpret_cast<const filament::math::mat4f*>(glm::value_ptr(finalRootMatrix)));
|
||
|
||
// 【关键一步】:注册 hostRootId(顶级根宿主)的 ID 与 pivotEntity,彻底打通顶级宿主的 Gizmo 坐标变换!
|
||
ObjectToFilamentEntity_[hostRootId] = { asset, pivotEntity };
|
||
|
||
Scene_->addEntity(pivotEntity);
|
||
|
||
// 更新初始位置
|
||
glm::mat4 initLocalMat{1.0f};
|
||
auto itMat = snapshot.LocalMatrices.find(renderable.ObjectId);
|
||
if (itMat != snapshot.LocalMatrices.end()) {
|
||
initLocalMat = itMat->second;
|
||
}
|
||
UpdateTransform(renderable.ObjectId, initLocalMat);
|
||
}
|
||
|
||
// 全量同步所有已映射对象的 Transform
|
||
for (const auto& [objectId, assetEntity] : ObjectToFilamentEntity_) {
|
||
auto itMat = snapshot.LocalMatrices.find(objectId);
|
||
if (itMat != snapshot.LocalMatrices.end()) {
|
||
UpdateTransform(objectId, itMat->second);
|
||
}
|
||
}
|
||
}
|
||
|
||
void ApplySceneView(const MetaCoreSceneView& sceneView) {
|
||
if (!Camera_ || !View_) return;
|
||
|
||
// 1. 视图矩阵同步
|
||
const glm::mat4 viewMatrix = glm::lookAt(sceneView.CameraPosition, sceneView.CameraTarget, sceneView.CameraUp);
|
||
const glm::mat4 cameraModelMatrix = glm::inverse(viewMatrix);
|
||
Camera_->setModelMatrix(*reinterpret_cast<const filament::math::mat4f*>(glm::value_ptr(cameraModelMatrix)));
|
||
|
||
// 2. 投影矩阵:回归 Filament 原生设置
|
||
const auto& viewport = View_->getViewport();
|
||
float aspect = (viewport.height > 0) ? (static_cast<float>(viewport.width) / static_cast<float>(viewport.height)) : 1.0f;
|
||
|
||
Camera_->setProjection(
|
||
sceneView.VerticalFieldOfViewDegrees,
|
||
aspect,
|
||
sceneView.NearClip,
|
||
sceneView.FarClip,
|
||
filament::Camera::Fov::VERTICAL
|
||
);
|
||
}
|
||
|
||
void RenderAll() {
|
||
if (!Renderer_ || !SwapChain_ || !View_ || !UIView_ || !ImGuiHelper_) {
|
||
return;
|
||
}
|
||
|
||
if (Renderer_->beginFrame(SwapChain_)) {
|
||
// 1. 渲染 3D 离屏视口 (输出到 RenderTarget)
|
||
filament::Renderer::ClearOptions options;
|
||
options.clearColor = { 0.11f, 0.12f, 0.14f, 1.0f }; // 深灰色
|
||
options.clear = true;
|
||
Renderer_->setClearOptions(options);
|
||
|
||
Renderer_->render(View_);
|
||
|
||
// 2. 准备并渲染 UI 视口 (输出到 SwapChain)
|
||
ImGuiIO& io = ImGui::GetIO();
|
||
ImGuiHelper_->setDisplaySize(io.DisplaySize.x, io.DisplaySize.y);
|
||
UIView_->setViewport({0, 0, static_cast<uint32_t>(io.DisplaySize.x), static_cast<uint32_t>(io.DisplaySize.y)});
|
||
|
||
options.clearColor = { 0.11f, 0.12f, 0.14f, 1.0f }; // 灰色
|
||
options.clear = true;
|
||
Renderer_->setClearOptions(options);
|
||
|
||
ImGuiHelper_->processImGuiCommands(ImGui::GetDrawData(), io);
|
||
|
||
Renderer_->render(UIView_);
|
||
|
||
Renderer_->endFrame();
|
||
|
||
// 在帧结束、指令提交后,安全地销毁上一帧遗留的旧纹理
|
||
for (auto* tex : OldTextures_) {
|
||
Engine_->destroy(tex);
|
||
}
|
||
OldTextures_.clear();
|
||
}
|
||
}
|
||
|
||
uint32_t GetGLTextureId() const {
|
||
return GLTextureId_;
|
||
}
|
||
|
||
void Resize(int width, int height) {
|
||
if (width <= 0 || height <= 0) return;
|
||
|
||
// 【关键修复】:如果大小没有改变,不要重新创建纹理!
|
||
// 否则会导致上一帧传给 ImGui 的纹理指针在这一帧被提前销毁,引发 Use-after-free 崩溃。
|
||
if (FilamentTexture_ &&
|
||
FilamentTexture_->getWidth() == static_cast<uint32_t>(width) &&
|
||
FilamentTexture_->getHeight() == static_cast<uint32_t>(height)) {
|
||
return;
|
||
}
|
||
|
||
if (RenderTarget_) Engine_->destroy(RenderTarget_);
|
||
if (DepthTexture_) Engine_->destroy(DepthTexture_);
|
||
|
||
// 【关键修复】:推迟销毁旧纹理,避免当前帧 ImGui 的 DrawList 还在引用它导致崩溃。
|
||
if (FilamentTexture_) {
|
||
OldTextures_.push_back(FilamentTexture_);
|
||
}
|
||
|
||
FilamentTexture_ = filament::Texture::Builder()
|
||
.width(static_cast<uint32_t>(width))
|
||
.height(static_cast<uint32_t>(height))
|
||
.usage(filament::Texture::Usage::COLOR_ATTACHMENT | filament::Texture::Usage::SAMPLEABLE)
|
||
.format(filament::Texture::InternalFormat::RGBA8)
|
||
.build(*Engine_);
|
||
|
||
DepthTexture_ = filament::Texture::Builder()
|
||
.width(static_cast<uint32_t>(width))
|
||
.height(static_cast<uint32_t>(height))
|
||
.usage(filament::Texture::Usage::DEPTH_ATTACHMENT)
|
||
.format(filament::Texture::InternalFormat::DEPTH24)
|
||
.build(*Engine_);
|
||
|
||
RenderTarget_ = filament::RenderTarget::Builder()
|
||
.texture(filament::RenderTarget::AttachmentPoint::COLOR, FilamentTexture_)
|
||
.texture(filament::RenderTarget::AttachmentPoint::DEPTH, DepthTexture_)
|
||
.build(*Engine_);
|
||
|
||
View_->setRenderTarget(RenderTarget_);
|
||
View_->setViewport({0, 0, static_cast<uint32_t>(width), static_cast<uint32_t>(height)});
|
||
}
|
||
|
||
void* GetFilamentTexturePointer() const {
|
||
return FilamentTexture_;
|
||
}
|
||
|
||
bool TryGetObjectWorldMatrix(MetaCoreId objectId, glm::mat4& worldMatrix) const {
|
||
const auto worldIt = ObjectWorldMatrices_.find(objectId);
|
||
if (worldIt == ObjectWorldMatrices_.end()) {
|
||
return false;
|
||
}
|
||
|
||
worldMatrix = worldIt->second;
|
||
return true;
|
||
}
|
||
|
||
bool HasRuntimeSyncFailure() const { return false; }
|
||
const std::string& GetLastRuntimeSyncFailure() const { return EmptyString_; }
|
||
|
||
bool VerifyTransformForTesting(MetaCoreId objectId, const glm::mat4& expectedMatrix) const {
|
||
auto it = ObjectToFilamentEntity_.find(objectId);
|
||
if (it == ObjectToFilamentEntity_.end()) return false;
|
||
|
||
utils::Entity entity = it->second.second;
|
||
auto& tm = Engine_->getTransformManager();
|
||
auto instance = tm.getInstance(entity);
|
||
if (!instance) return false;
|
||
|
||
filament::math::mat4f currentMat = tm.getTransform(instance);
|
||
glm::mat4 actualMatrix;
|
||
std::memcpy(glm::value_ptr(actualMatrix), ¤tMat[0][0], sizeof(float) * 16);
|
||
|
||
glm::mat4 targetMatrix = expectedMatrix;
|
||
if (LoadedAssets_.count(objectId) == 0) {
|
||
glm::mat4 R_plus90X = glm::rotate(glm::mat4(1.0f), glm::radians(90.0f), {1, 0, 0});
|
||
glm::mat4 R_minus90X = glm::rotate(glm::mat4(1.0f), glm::radians(-90.0f), {1, 0, 0});
|
||
targetMatrix = R_plus90X * targetMatrix * R_minus90X;
|
||
}
|
||
|
||
// 浮点数比对,精度 0.0001
|
||
for (int c = 0; c < 4; ++c) {
|
||
for (int r = 0; r < 4; ++r) {
|
||
if (std::abs(actualMatrix[c][r] - targetMatrix[c][r]) > 0.0001f) {
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
bool VerifyLightExistsForTesting(MetaCoreId objectId) const {
|
||
return SceneLightEntities_.contains(objectId);
|
||
}
|
||
|
||
float GetDefaultLightIntensityForTesting() const {
|
||
if (!Engine_) return 0.0F;
|
||
auto& lightManager = Engine_->getLightManager();
|
||
const auto defaultLightInstance = lightManager.getInstance(Light_);
|
||
if (defaultLightInstance) {
|
||
return lightManager.getIntensity(defaultLightInstance);
|
||
}
|
||
return 0.0F;
|
||
}
|
||
|
||
private:
|
||
static filament::math::float3 ToFilamentFloat3(const glm::vec3& value) {
|
||
return filament::math::float3{ value.x, value.y, value.z };
|
||
}
|
||
|
||
static filament::math::float3 BuildFilamentLightDirection(const glm::mat4& worldMatrix) {
|
||
glm::vec3 direction = glm::vec3(worldMatrix * glm::vec4(0.0F, 0.0F, -1.0F, 0.0F));
|
||
if (glm::length(direction) <= 0.0001F) {
|
||
direction = glm::vec3(0.5F, 0.5F, -1.0F);
|
||
}
|
||
direction = glm::normalize(direction);
|
||
return ToFilamentFloat3(direction);
|
||
}
|
||
|
||
static filament::LinearColor BuildFilamentLightColor(const glm::vec3& color) {
|
||
return filament::Color::toLinear<filament::ACCURATE>({ color.x, color.y, color.z });
|
||
}
|
||
|
||
static float BuildFilamentLightIntensity(float intensity) {
|
||
return (intensity > 0.0F ? intensity : 0.0F) * 10000.0F;
|
||
}
|
||
|
||
void SyncSceneLights(const MetaCoreSceneRenderSyncSnapshot& snapshot) {
|
||
if (!Engine_ || !Scene_) {
|
||
return;
|
||
}
|
||
|
||
std::unordered_set<MetaCoreId> activeLightIds;
|
||
auto& lightManager = Engine_->getLightManager();
|
||
const auto defaultLightInstance = lightManager.getInstance(Light_);
|
||
|
||
// 统计当前是否有处于启用状态的光源
|
||
bool hasActiveLight = false;
|
||
for (const auto& light : snapshot.Lights) {
|
||
if (light.Enabled) {
|
||
hasActiveLight = true;
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (defaultLightInstance) {
|
||
// 如果场景中没有任何起作用的灯光,则自动亮起默认灯光作为兜底
|
||
lightManager.setIntensity(defaultLightInstance, hasActiveLight ? 0.0F : 100000.0F);
|
||
}
|
||
|
||
for (const MetaCoreRenderSyncLight& light : snapshot.Lights) {
|
||
if (!light.Enabled) {
|
||
continue;
|
||
}
|
||
activeLightIds.insert(light.ObjectId);
|
||
auto lightIt = SceneLightEntities_.find(light.ObjectId);
|
||
if (lightIt == SceneLightEntities_.end()) {
|
||
utils::Entity lightEntity = utils::EntityManager::get().create();
|
||
filament::LightManager::Builder(filament::LightManager::Type::DIRECTIONAL)
|
||
.color(BuildFilamentLightColor(light.Color))
|
||
.intensity(BuildFilamentLightIntensity(light.Intensity))
|
||
.direction(BuildFilamentLightDirection(light.WorldMatrix))
|
||
.castShadows(true)
|
||
.build(*Engine_, lightEntity);
|
||
Scene_->addEntity(lightEntity);
|
||
lightIt = SceneLightEntities_.emplace(light.ObjectId, lightEntity).first;
|
||
}
|
||
|
||
const auto lightInstance = lightManager.getInstance(lightIt->second);
|
||
if (lightInstance) {
|
||
lightManager.setColor(lightInstance, BuildFilamentLightColor(light.Color));
|
||
lightManager.setIntensity(lightInstance, BuildFilamentLightIntensity(light.Intensity));
|
||
lightManager.setDirection(lightInstance, BuildFilamentLightDirection(light.WorldMatrix));
|
||
}
|
||
}
|
||
|
||
for (auto it = SceneLightEntities_.begin(); it != SceneLightEntities_.end();) {
|
||
if (activeLightIds.contains(it->first)) {
|
||
++it;
|
||
continue;
|
||
}
|
||
|
||
Scene_->remove(it->second);
|
||
Engine_->destroy(it->second);
|
||
utils::EntityManager::get().destroy(it->second);
|
||
it = SceneLightEntities_.erase(it);
|
||
}
|
||
}
|
||
|
||
void UpdateTransform(MetaCoreId objectId, const glm::mat4& localMatrix) {
|
||
auto it = ObjectToFilamentEntity_.find(objectId);
|
||
if (it == ObjectToFilamentEntity_.end()) return;
|
||
|
||
utils::Entity entity = it->second.second;
|
||
|
||
auto& tm = Engine_->getTransformManager();
|
||
auto instance = tm.getInstance(entity);
|
||
if (instance) {
|
||
glm::mat4 matrix = localMatrix;
|
||
|
||
// 【关键修复】:如果当前 entity 是子节点(即不是顶级映射的 pivotEntity)
|
||
// 由于它的父级(assetRoot)带有了 -90X 旋转,我们必须对子节点应用基变换来抵消这个旋转。
|
||
// 公式:L_filament = R(90X) * L_ecs * R(-90X)
|
||
if (LoadedAssets_.count(objectId) == 0) {
|
||
glm::mat4 R_plus90X = glm::rotate(glm::mat4(1.0f), glm::radians(90.0f), {1, 0, 0});
|
||
glm::mat4 R_minus90X = glm::rotate(glm::mat4(1.0f), glm::radians(-90.0f), {1, 0, 0});
|
||
matrix = R_plus90X * matrix * R_minus90X;
|
||
}
|
||
|
||
tm.setTransform(instance, *reinterpret_cast<const filament::math::mat4f*>(glm::value_ptr(matrix)));
|
||
}
|
||
}
|
||
|
||
filament::Engine* Engine_ = nullptr;
|
||
filament::SwapChain* SwapChain_ = nullptr;
|
||
filament::Renderer* Renderer_ = nullptr;
|
||
filament::Scene* Scene_ = nullptr;
|
||
filament::Camera* Camera_ = nullptr;
|
||
filament::View* View_ = nullptr;
|
||
|
||
filament::gltfio::AssetLoader* AssetLoader_ = nullptr;
|
||
filament::gltfio::MaterialProvider* MaterialProvider_ = nullptr;
|
||
utils::NameComponentManager* NameManager_ = nullptr;
|
||
|
||
MetaCoreSceneRenderSync RenderSync_{};
|
||
MetaCoreSceneRenderSyncSnapshot LastSyncSnapshot_{};
|
||
std::unordered_map<MetaCoreId, glm::mat4> ObjectWorldMatrices_{};
|
||
std::unordered_map<MetaCoreId, std::pair<filament::gltfio::FilamentAsset*, utils::Entity>> ObjectToFilamentEntity_;
|
||
std::unordered_map<MetaCoreId, filament::gltfio::FilamentAsset*> LoadedAssets_;
|
||
std::unordered_map<MetaCoreId, utils::Entity> SceneLightEntities_;
|
||
|
||
filament::View* UIView_ = nullptr;
|
||
MetaCoreImGuiHelper* ImGuiHelper_ = nullptr;
|
||
|
||
GLuint GLTextureId_ = 0;
|
||
filament::Texture* FilamentTexture_ = nullptr;
|
||
filament::Texture* DepthTexture_ = nullptr;
|
||
filament::RenderTarget* RenderTarget_ = nullptr;
|
||
std::vector<filament::Texture*> OldTextures_;
|
||
utils::Entity Light_;
|
||
|
||
std::filesystem::path ProjectRootPath_{};
|
||
std::string EmptyString_{};
|
||
};
|
||
|
||
MetaCoreFilamentSceneBridge::MetaCoreFilamentSceneBridge()
|
||
: Impl_(std::make_unique<MetaCoreFilamentSceneBridgeImpl>()) {}
|
||
|
||
MetaCoreFilamentSceneBridge::~MetaCoreFilamentSceneBridge() = default;
|
||
|
||
bool MetaCoreFilamentSceneBridge::Initialize(MetaCoreWindow& window) {
|
||
return Impl_->Initialize(window);
|
||
}
|
||
|
||
void MetaCoreFilamentSceneBridge::Shutdown() {
|
||
Impl_->Shutdown();
|
||
}
|
||
|
||
void MetaCoreFilamentSceneBridge::SetProjectRootPath(const std::filesystem::path& projectRootPath) {
|
||
Impl_->SetProjectRootPath(projectRootPath);
|
||
}
|
||
|
||
void MetaCoreFilamentSceneBridge::SyncScene(MetaCoreScene& scene, bool compatibilityMeshOnly, bool useScenePrimaryCamera) {
|
||
auto snapshot = Impl_->BuildSnapshot(scene);
|
||
Impl_->SyncScene(snapshot, &scene, compatibilityMeshOnly, useScenePrimaryCamera);
|
||
}
|
||
|
||
void MetaCoreFilamentSceneBridge::SyncScene(const MetaCoreSceneRenderSyncSnapshot& snapshot, MetaCoreScene* scene, bool compatibilityMeshOnly, bool useScenePrimaryCamera) {
|
||
Impl_->SyncScene(snapshot, scene, compatibilityMeshOnly, useScenePrimaryCamera);
|
||
}
|
||
|
||
void MetaCoreFilamentSceneBridge::ApplySceneView(const MetaCoreSceneView& sceneView) {
|
||
Impl_->ApplySceneView(sceneView);
|
||
}
|
||
|
||
void MetaCoreFilamentSceneBridge::RenderAll() {
|
||
Impl_->RenderAll();
|
||
}
|
||
|
||
uint32_t MetaCoreFilamentSceneBridge::GetGLTextureId() const {
|
||
return Impl_->GetGLTextureId();
|
||
}
|
||
|
||
void MetaCoreFilamentSceneBridge::Resize(int width, int height) {
|
||
Impl_->Resize(width, height);
|
||
}
|
||
|
||
void* MetaCoreFilamentSceneBridge::GetFilamentTexturePointer() const {
|
||
return Impl_->GetFilamentTexturePointer();
|
||
}
|
||
|
||
bool MetaCoreFilamentSceneBridge::TryGetObjectWorldMatrix(MetaCoreId objectId, glm::mat4& worldMatrix) const {
|
||
return Impl_->TryGetObjectWorldMatrix(objectId, worldMatrix);
|
||
}
|
||
|
||
bool MetaCoreFilamentSceneBridge::HasRuntimeSyncFailure() const {
|
||
return Impl_->HasRuntimeSyncFailure();
|
||
}
|
||
|
||
const std::string& MetaCoreFilamentSceneBridge::GetLastRuntimeSyncFailure() const {
|
||
return Impl_->GetLastRuntimeSyncFailure();
|
||
}
|
||
|
||
bool MetaCoreFilamentSceneBridge::VerifyTransformForTesting(MetaCoreId objectId, const glm::mat4& expectedMatrix) const {
|
||
return Impl_->VerifyTransformForTesting(objectId, expectedMatrix);
|
||
}
|
||
|
||
bool MetaCoreFilamentSceneBridge::VerifyLightExistsForTesting(MetaCoreId objectId) const {
|
||
return Impl_->VerifyLightExistsForTesting(objectId);
|
||
}
|
||
|
||
float MetaCoreFilamentSceneBridge::GetDefaultLightIntensityForTesting() const {
|
||
return Impl_->GetDefaultLightIntensityForTesting();
|
||
}
|
||
|
||
} // namespace MetaCore
|