网格更新
This commit is contained in:
parent
7729a21fe9
commit
74416a6b60
@ -100,6 +100,18 @@ if(NOT METACORE_BUILD_CORE_ONLY)
|
||||
else()
|
||||
set(METACORE_FILAMENT_MATC "${METACORE_FILAMENT_ROOT}/bin/matc")
|
||||
endif()
|
||||
set(METACORE_FILAMENT_MATC_RUNNABLE OFF)
|
||||
if(EXISTS "${METACORE_FILAMENT_MATC}")
|
||||
execute_process(
|
||||
COMMAND "${METACORE_FILAMENT_MATC}" --help
|
||||
RESULT_VARIABLE METACORE_FILAMENT_MATC_HELP_RESULT
|
||||
OUTPUT_QUIET
|
||||
ERROR_QUIET
|
||||
)
|
||||
if(METACORE_FILAMENT_MATC_HELP_RESULT EQUAL 0)
|
||||
set(METACORE_FILAMENT_MATC_RUNNABLE ON)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
set(METACORE_UI_BLIT_MATERIAL_PREBUILT "")
|
||||
foreach(candidate IN ITEMS
|
||||
@ -151,6 +163,61 @@ if(NOT METACORE_BUILD_CORE_ONLY)
|
||||
VERBATIM
|
||||
)
|
||||
endfunction()
|
||||
|
||||
set(METACORE_EDITOR_GRID_MATERIAL_SOURCE "${CMAKE_SOURCE_DIR}/Source/MetaCoreRender/Private/Materials/grid.mat")
|
||||
set(METACORE_EDITOR_GRID_MATERIAL_OUTPUT "${CMAKE_BINARY_DIR}/grid.filamat")
|
||||
set(METACORE_EDITOR_GRID_MATERIAL_PREBUILT "")
|
||||
foreach(candidate IN ITEMS
|
||||
"${CMAKE_SOURCE_DIR}/Source/MetaCoreRender/Private/Materials/grid.filamat"
|
||||
"${CMAKE_SOURCE_DIR}/build_filament/libs/filamentapp/generated/material/transparentColor.filamat"
|
||||
)
|
||||
if(EXISTS "${candidate}")
|
||||
set(METACORE_EDITOR_GRID_MATERIAL_PREBUILT "${candidate}")
|
||||
break()
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
if(METACORE_FILAMENT_MATC_RUNNABLE AND EXISTS "${METACORE_EDITOR_GRID_MATERIAL_SOURCE}")
|
||||
add_custom_command(
|
||||
OUTPUT "${METACORE_EDITOR_GRID_MATERIAL_OUTPUT}"
|
||||
COMMAND "${METACORE_FILAMENT_MATC}"
|
||||
--platform desktop
|
||||
--api opengl
|
||||
--output "${METACORE_EDITOR_GRID_MATERIAL_OUTPUT}"
|
||||
"${METACORE_EDITOR_GRID_MATERIAL_SOURCE}"
|
||||
DEPENDS
|
||||
"${METACORE_EDITOR_GRID_MATERIAL_SOURCE}"
|
||||
"${METACORE_FILAMENT_MATC}"
|
||||
VERBATIM
|
||||
)
|
||||
add_custom_target(MetaCoreEditorGridMaterial
|
||||
DEPENDS "${METACORE_EDITOR_GRID_MATERIAL_OUTPUT}"
|
||||
)
|
||||
elseif(METACORE_EDITOR_GRID_MATERIAL_PREBUILT)
|
||||
add_custom_command(
|
||||
OUTPUT "${METACORE_EDITOR_GRID_MATERIAL_OUTPUT}"
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
"${METACORE_EDITOR_GRID_MATERIAL_PREBUILT}"
|
||||
"${METACORE_EDITOR_GRID_MATERIAL_OUTPUT}"
|
||||
DEPENDS "${METACORE_EDITOR_GRID_MATERIAL_PREBUILT}"
|
||||
VERBATIM
|
||||
)
|
||||
add_custom_target(MetaCoreEditorGridMaterial
|
||||
DEPENDS "${METACORE_EDITOR_GRID_MATERIAL_OUTPUT}"
|
||||
)
|
||||
endif()
|
||||
|
||||
function(metacore_stage_editor_grid_material target_name)
|
||||
if(TARGET MetaCoreEditorGridMaterial)
|
||||
add_dependencies(${target_name} MetaCoreEditorGridMaterial)
|
||||
add_custom_command(TARGET ${target_name} POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
"${METACORE_EDITOR_GRID_MATERIAL_OUTPUT}"
|
||||
"$<TARGET_FILE_DIR:${target_name}>/grid.filamat"
|
||||
VERBATIM
|
||||
)
|
||||
endif()
|
||||
endfunction()
|
||||
endif()
|
||||
|
||||
add_executable(MetaCoreHeaderTool
|
||||
@ -546,6 +613,7 @@ target_link_libraries(MetaCoreEditorApp
|
||||
MetaCoreEditor
|
||||
)
|
||||
metacore_stage_ui_blit_material(MetaCoreEditorApp)
|
||||
metacore_stage_editor_grid_material(MetaCoreEditorApp)
|
||||
|
||||
|
||||
set(METACORE_LAUNCHER_SOURCES
|
||||
|
||||
Binary file not shown.
@ -829,59 +829,6 @@ void MetaCoreDrawWorldOriginOverlay(
|
||||
return;
|
||||
}
|
||||
|
||||
void MetaCoreDrawViewportGridOverlay(
|
||||
const MetaCoreEditorContext& editorContext,
|
||||
const MetaCoreSceneViewportState& viewportState
|
||||
) {
|
||||
if (!editorContext.GetShowViewportGrid()) {
|
||||
return;
|
||||
}
|
||||
if (viewportState.Width <= 1.0F || viewportState.Height <= 1.0F) {
|
||||
return;
|
||||
}
|
||||
|
||||
ImDrawList* drawList = ImGui::GetForegroundDrawList();
|
||||
const ImVec2 topLeft(viewportState.Left, viewportState.Top);
|
||||
const ImVec2 bottomRight(viewportState.Left + viewportState.Width, viewportState.Top + viewportState.Height);
|
||||
const ImVec2 center((topLeft.x + bottomRight.x) * 0.5F, (topLeft.y + bottomRight.y) * 0.5F);
|
||||
|
||||
const float minorSpacing = 32.0F;
|
||||
const int verticalMinorCount = static_cast<int>(viewportState.Width / minorSpacing) + 2;
|
||||
const int horizontalMinorCount = static_cast<int>(viewportState.Height / minorSpacing) + 2;
|
||||
const ImU32 minorColor = IM_COL32(110, 120, 135, 40);
|
||||
const ImU32 majorColor = IM_COL32(150, 165, 185, 70);
|
||||
const ImU32 axisXColor = IM_COL32(220, 80, 80, 95);
|
||||
const ImU32 axisYColor = IM_COL32(120, 210, 100, 95);
|
||||
|
||||
for (int lineIndex = -verticalMinorCount; lineIndex <= verticalMinorCount; ++lineIndex) {
|
||||
const float x = center.x + static_cast<float>(lineIndex) * minorSpacing;
|
||||
if (x < topLeft.x || x > bottomRight.x) {
|
||||
continue;
|
||||
}
|
||||
const bool isMajor = (lineIndex % 4) == 0;
|
||||
drawList->AddLine(
|
||||
ImVec2(x, topLeft.y),
|
||||
ImVec2(x, bottomRight.y),
|
||||
lineIndex == 0 ? axisXColor : (isMajor ? majorColor : minorColor),
|
||||
lineIndex == 0 ? 1.8F : (isMajor ? 1.2F : 1.0F)
|
||||
);
|
||||
}
|
||||
|
||||
for (int lineIndex = -horizontalMinorCount; lineIndex <= horizontalMinorCount; ++lineIndex) {
|
||||
const float y = center.y + static_cast<float>(lineIndex) * minorSpacing;
|
||||
if (y < topLeft.y || y > bottomRight.y) {
|
||||
continue;
|
||||
}
|
||||
const bool isMajor = (lineIndex % 4) == 0;
|
||||
drawList->AddLine(
|
||||
ImVec2(topLeft.x, y),
|
||||
ImVec2(bottomRight.x, y),
|
||||
lineIndex == 0 ? axisYColor : (isMajor ? majorColor : minorColor),
|
||||
lineIndex == 0 ? 1.8F : (isMajor ? 1.2F : 1.0F)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void MetaCoreDrawSelectionBoundsOverlay(
|
||||
const MetaCoreEditorContext& editorContext,
|
||||
const MetaCoreScene& scene,
|
||||
@ -1654,6 +1601,7 @@ void MetaCoreEditorApp::DrawEditorFrame() {
|
||||
}
|
||||
|
||||
// 6. 驱动 Filament 渲染
|
||||
ViewportRenderer_.SetEditorGridVisible(!isGameView && EditorContext_->GetShowViewportGrid());
|
||||
if (isGameView) {
|
||||
if (hasGameCamera) {
|
||||
ViewportRenderer_.RenderSnapshotToViewport(gameViewSnapshot, &Scene_, sceneView);
|
||||
@ -1740,7 +1688,6 @@ void MetaCoreEditorApp::DrawEditorFrame() {
|
||||
|
||||
// 绘制 Overlays
|
||||
if (!isGameView) {
|
||||
MetaCoreDrawViewportGridOverlay(*EditorContext_, viewportState);
|
||||
MetaCoreDrawWorldOriginOverlay(*EditorContext_, sceneView, viewportState);
|
||||
MetaCoreDrawSelectionBoundsOverlay(*EditorContext_, Scene_, sceneView, viewportState);
|
||||
MetaCoreDrawSelectionHierarchyOverlay(*EditorContext_, Scene_, sceneView, viewportState);
|
||||
@ -1781,10 +1728,12 @@ void MetaCoreEditorApp::DrawEditorFrame() {
|
||||
// --- 补丁三重构结束 ---
|
||||
} else {
|
||||
SceneInteractionService_.ResetFrameState();
|
||||
ViewportRenderer_.SetEditorGridVisible(false);
|
||||
ViewportRenderer_.SetViewportRect(MetaCoreViewportRect{});
|
||||
}
|
||||
} else {
|
||||
SceneInteractionService_.ResetFrameState();
|
||||
ViewportRenderer_.SetEditorGridVisible(false);
|
||||
ViewportRenderer_.SetViewportRect(MetaCoreViewportRect{});
|
||||
}
|
||||
}
|
||||
|
||||
20
Source/MetaCoreRender/Private/Materials/grid.mat
Normal file
20
Source/MetaCoreRender/Private/Materials/grid.mat
Normal file
@ -0,0 +1,20 @@
|
||||
material {
|
||||
name : MetaCoreEditorGrid,
|
||||
shadingModel : unlit,
|
||||
blending : transparent,
|
||||
transparency : default,
|
||||
depthWrite : false,
|
||||
depthCulling : true,
|
||||
doubleSided : true,
|
||||
parameters : [
|
||||
{ type : float4, name : baseColorFactor }
|
||||
],
|
||||
}
|
||||
|
||||
fragment {
|
||||
void material(inout MaterialInputs material) {
|
||||
prepareMaterial(material);
|
||||
material.baseColor = materialParams.baseColorFactor;
|
||||
material.baseColor.rgb *= material.baseColor.a;
|
||||
}
|
||||
}
|
||||
@ -127,6 +127,10 @@ void MetaCoreEditorViewportRenderer::SetProjectRootPath(const std::filesystem::p
|
||||
FilamentSceneBridge_.SetProjectRootPath(projectRootPath);
|
||||
}
|
||||
|
||||
void MetaCoreEditorViewportRenderer::SetEditorGridVisible(bool visible) {
|
||||
FilamentSceneBridge_.SetEditorGridVisible(visible);
|
||||
}
|
||||
|
||||
uint32_t MetaCoreEditorViewportRenderer::GetFilamentGLTextureId() const {
|
||||
return FilamentSceneBridge_.GetGLTextureId();
|
||||
}
|
||||
|
||||
@ -27,6 +27,9 @@
|
||||
#include <filament/TextureSampler.h>
|
||||
#include <filament/Skybox.h>
|
||||
#include <filament/IndirectLight.h>
|
||||
#include <filament/VertexBuffer.h>
|
||||
#include <filament/IndexBuffer.h>
|
||||
#include <filament/Color.h>
|
||||
|
||||
#include <gltfio/AssetLoader.h>
|
||||
#include <gltfio/FilamentAsset.h>
|
||||
@ -52,8 +55,10 @@
|
||||
#include <fstream>
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
@ -63,6 +68,8 @@
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
|
||||
// 引入平台和 OpenGL 头文件以创建纹理
|
||||
#if defined(_WIN32)
|
||||
@ -377,6 +384,35 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
void SetEditorGridVisible(bool visible) {
|
||||
EditorGridVisibleRequested_ = visible;
|
||||
if (Engine_ == nullptr || Scene_ == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (visible) {
|
||||
if (!EnsureEditorGridRenderable()) {
|
||||
return;
|
||||
}
|
||||
if (!EditorGridEntityInScene_) {
|
||||
Scene_->addEntity(EditorGridEntity_);
|
||||
EntitiesInScene_.insert(EditorGridEntity_);
|
||||
EditorGridEntityInScene_ = true;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (EditorGridEntityInScene_) {
|
||||
Scene_->remove(EditorGridEntity_);
|
||||
EntitiesInScene_.erase(EditorGridEntity_);
|
||||
EditorGridEntityInScene_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] bool VerifyEditorGridVisibleForTesting() const {
|
||||
return EditorGridEntityInScene_;
|
||||
}
|
||||
|
||||
void Shutdown() {
|
||||
if (RuntimeAssetSubscriptionId_ != 0) {
|
||||
MetaCoreRuntimeAssetRegistry::Get().Unsubscribe(RuntimeAssetSubscriptionId_);
|
||||
@ -385,6 +421,8 @@ public:
|
||||
if (Engine_) {
|
||||
std::cout << "FilamentSceneBridge: Shutting down..." << std::endl;
|
||||
DebugRenderTargetState("Shutdown begin");
|
||||
|
||||
DestroyEditorGridRenderable();
|
||||
|
||||
// 1. 销毁所有加载的资产,切断其内部 Renderable 对克隆材质实例的占用引用
|
||||
std::cout << "[DEBUG] LoadedAssets_ size: " << LoadedAssets_.size() << std::endl;
|
||||
@ -2120,6 +2158,321 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
struct MetaCoreEditorGridVertex {
|
||||
filament::math::float3 Position{};
|
||||
};
|
||||
|
||||
struct MetaCoreEditorGridPrimitiveRange {
|
||||
std::uint32_t Offset = 0;
|
||||
std::uint32_t Count = 0;
|
||||
};
|
||||
|
||||
[[nodiscard]] filament::Material* LoadEditorGridMaterial() {
|
||||
if (Engine_ == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const std::array<std::filesystem::path, 8> candidatePaths{
|
||||
std::filesystem::path("grid.filamat"),
|
||||
std::filesystem::path("../grid.filamat"),
|
||||
std::filesystem::path("build/grid.filamat"),
|
||||
std::filesystem::path("build/linux-full-clang-libcxx-debug/grid.filamat"),
|
||||
std::filesystem::path("build_filament/libs/filamentapp/generated/material/transparentColor.filamat"),
|
||||
std::filesystem::path("../build_filament/libs/filamentapp/generated/material/transparentColor.filamat"),
|
||||
std::filesystem::path("../../build_filament/libs/filamentapp/generated/material/transparentColor.filamat"),
|
||||
std::filesystem::path("../../../build_filament/libs/filamentapp/generated/material/transparentColor.filamat")
|
||||
};
|
||||
|
||||
for (const std::filesystem::path& candidate : candidatePaths) {
|
||||
std::ifstream file(candidate, std::ios::binary);
|
||||
if (!file.is_open()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
std::vector<char> buffer(
|
||||
(std::istreambuf_iterator<char>(file)),
|
||||
std::istreambuf_iterator<char>()
|
||||
);
|
||||
if (buffer.empty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
filament::Material* material = filament::Material::Builder()
|
||||
.package(buffer.data(), buffer.size())
|
||||
.build(*Engine_);
|
||||
if (material != nullptr) {
|
||||
MetaCoreFilamentDebugLog("Editor grid material loaded path=\"" + candidate.generic_string() + "\"");
|
||||
return material;
|
||||
}
|
||||
}
|
||||
|
||||
MetaCoreFilamentDebugLog("Editor grid material not found; using Filament default material fallback");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void SetEditorGridMaterialColor(
|
||||
filament::MaterialInstance& materialInstance,
|
||||
filament::math::float4 color
|
||||
) const {
|
||||
materialInstance.setColorWrite(true);
|
||||
materialInstance.setDepthWrite(false);
|
||||
materialInstance.setDepthCulling(true);
|
||||
materialInstance.setCullingMode(filament::backend::CullingMode::NONE);
|
||||
|
||||
const filament::Material* material = materialInstance.getMaterial();
|
||||
if (material == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto setColorIfPresent = [&](const char* parameterName) -> bool {
|
||||
if (!material->hasParameter(parameterName)) {
|
||||
return false;
|
||||
}
|
||||
materialInstance.setParameter(parameterName, filament::RgbaType::LINEAR, color);
|
||||
return true;
|
||||
};
|
||||
|
||||
if (setColorIfPresent("baseColor") ||
|
||||
setColorIfPresent("baseColorFactor") ||
|
||||
setColorIfPresent("color") ||
|
||||
setColorIfPresent("albedo")) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] bool EnsureEditorGridRenderable() {
|
||||
if (EditorGridBuilt_) {
|
||||
return true;
|
||||
}
|
||||
if (Engine_ == nullptr || Scene_ == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
EditorGridMaterial_ = LoadEditorGridMaterial();
|
||||
const filament::Material* material =
|
||||
EditorGridMaterial_ != nullptr ? EditorGridMaterial_ : Engine_->getDefaultMaterial();
|
||||
if (material == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
constexpr int gridExtent = 100;
|
||||
constexpr int majorStep = 10;
|
||||
enum class GridLineKind : std::size_t {
|
||||
Minor = 0,
|
||||
Major = 1,
|
||||
AxisX = 2,
|
||||
AxisY = 3,
|
||||
Count = 4
|
||||
};
|
||||
|
||||
std::array<std::vector<MetaCoreEditorGridVertex>, static_cast<std::size_t>(GridLineKind::Count)> groupedVertices;
|
||||
const auto appendLine = [&](
|
||||
GridLineKind kind,
|
||||
float x0,
|
||||
float y0,
|
||||
float x1,
|
||||
float y1
|
||||
) {
|
||||
auto& vertices = groupedVertices[static_cast<std::size_t>(kind)];
|
||||
vertices.push_back(MetaCoreEditorGridVertex{filament::math::float3{x0, y0, 0.0F}});
|
||||
vertices.push_back(MetaCoreEditorGridVertex{filament::math::float3{x1, y1, 0.0F}});
|
||||
};
|
||||
|
||||
for (int coordinate = -gridExtent; coordinate <= gridExtent; ++coordinate) {
|
||||
const bool isMajor = coordinate != 0 && (coordinate % majorStep) == 0;
|
||||
const GridLineKind verticalKind = coordinate == 0
|
||||
? GridLineKind::AxisY
|
||||
: (isMajor ? GridLineKind::Major : GridLineKind::Minor);
|
||||
appendLine(
|
||||
verticalKind,
|
||||
static_cast<float>(coordinate),
|
||||
static_cast<float>(-gridExtent),
|
||||
static_cast<float>(coordinate),
|
||||
static_cast<float>(gridExtent)
|
||||
);
|
||||
|
||||
const GridLineKind horizontalKind = coordinate == 0
|
||||
? GridLineKind::AxisX
|
||||
: (isMajor ? GridLineKind::Major : GridLineKind::Minor);
|
||||
appendLine(
|
||||
horizontalKind,
|
||||
static_cast<float>(-gridExtent),
|
||||
static_cast<float>(coordinate),
|
||||
static_cast<float>(gridExtent),
|
||||
static_cast<float>(coordinate)
|
||||
);
|
||||
}
|
||||
|
||||
std::vector<MetaCoreEditorGridVertex> vertices;
|
||||
std::vector<std::uint16_t> indices;
|
||||
vertices.reserve((gridExtent * 2 + 1) * 4);
|
||||
indices.reserve((gridExtent * 2 + 1) * 4);
|
||||
|
||||
std::array<MetaCoreEditorGridPrimitiveRange, static_cast<std::size_t>(GridLineKind::Count)> ranges{};
|
||||
for (std::size_t kindIndex = 0; kindIndex < groupedVertices.size(); ++kindIndex) {
|
||||
ranges[kindIndex].Offset = static_cast<std::uint32_t>(indices.size());
|
||||
for (const MetaCoreEditorGridVertex& vertex : groupedVertices[kindIndex]) {
|
||||
if (vertices.size() > std::numeric_limits<std::uint16_t>::max()) {
|
||||
return false;
|
||||
}
|
||||
vertices.push_back(vertex);
|
||||
indices.push_back(static_cast<std::uint16_t>(vertices.size() - 1));
|
||||
}
|
||||
ranges[kindIndex].Count = static_cast<std::uint32_t>(indices.size() - ranges[kindIndex].Offset);
|
||||
}
|
||||
|
||||
if (vertices.empty() || indices.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
EditorGridVertexBuffer_ = filament::VertexBuffer::Builder()
|
||||
.vertexCount(vertices.size())
|
||||
.bufferCount(1)
|
||||
.attribute(
|
||||
filament::VertexAttribute::POSITION,
|
||||
0,
|
||||
filament::VertexBuffer::AttributeType::FLOAT3,
|
||||
offsetof(MetaCoreEditorGridVertex, Position),
|
||||
sizeof(MetaCoreEditorGridVertex)
|
||||
)
|
||||
.build(*Engine_);
|
||||
EditorGridIndexBuffer_ = filament::IndexBuffer::Builder()
|
||||
.indexCount(indices.size())
|
||||
.bufferType(filament::IndexBuffer::IndexType::USHORT)
|
||||
.build(*Engine_);
|
||||
if (EditorGridVertexBuffer_ == nullptr || EditorGridIndexBuffer_ == nullptr) {
|
||||
DestroyEditorGridRenderable();
|
||||
return false;
|
||||
}
|
||||
|
||||
const size_t vertexDataSize = vertices.size() * sizeof(MetaCoreEditorGridVertex);
|
||||
void* vertexData = std::malloc(vertexDataSize);
|
||||
if (vertexData == nullptr) {
|
||||
DestroyEditorGridRenderable();
|
||||
return false;
|
||||
}
|
||||
std::memcpy(vertexData, vertices.data(), vertexDataSize);
|
||||
EditorGridVertexBuffer_->setBufferAt(
|
||||
*Engine_,
|
||||
0,
|
||||
filament::VertexBuffer::BufferDescriptor(
|
||||
vertexData,
|
||||
vertexDataSize,
|
||||
[](void* buffer, size_t, void*) {
|
||||
std::free(buffer);
|
||||
},
|
||||
nullptr
|
||||
)
|
||||
);
|
||||
|
||||
const size_t indexDataSize = indices.size() * sizeof(std::uint16_t);
|
||||
void* indexData = std::malloc(indexDataSize);
|
||||
if (indexData == nullptr) {
|
||||
DestroyEditorGridRenderable();
|
||||
return false;
|
||||
}
|
||||
std::memcpy(indexData, indices.data(), indexDataSize);
|
||||
EditorGridIndexBuffer_->setBuffer(
|
||||
*Engine_,
|
||||
filament::IndexBuffer::BufferDescriptor(
|
||||
indexData,
|
||||
indexDataSize,
|
||||
[](void* buffer, size_t, void*) {
|
||||
std::free(buffer);
|
||||
},
|
||||
nullptr
|
||||
)
|
||||
);
|
||||
|
||||
const std::array<filament::math::float4, static_cast<std::size_t>(GridLineKind::Count)> colors{
|
||||
filament::math::float4{0.48F, 0.52F, 0.60F, 0.22F},
|
||||
filament::math::float4{0.62F, 0.68F, 0.78F, 0.36F},
|
||||
filament::math::float4{0.86F, 0.22F, 0.22F, 0.58F},
|
||||
filament::math::float4{0.42F, 0.82F, 0.26F, 0.58F}
|
||||
};
|
||||
for (std::size_t kindIndex = 0; kindIndex < EditorGridMaterialInstances_.size(); ++kindIndex) {
|
||||
EditorGridMaterialInstances_[kindIndex] = material->createInstance();
|
||||
if (EditorGridMaterialInstances_[kindIndex] == nullptr) {
|
||||
DestroyEditorGridRenderable();
|
||||
return false;
|
||||
}
|
||||
SetEditorGridMaterialColor(*EditorGridMaterialInstances_[kindIndex], colors[kindIndex]);
|
||||
}
|
||||
|
||||
EditorGridEntity_ = utils::EntityManager::get().create();
|
||||
auto builder = filament::RenderableManager::Builder(static_cast<std::size_t>(GridLineKind::Count));
|
||||
builder
|
||||
.boundingBox({{0.0F, 0.0F, 0.0F}, {static_cast<float>(gridExtent), static_cast<float>(gridExtent), 0.05F}})
|
||||
.culling(false)
|
||||
.castShadows(false)
|
||||
.receiveShadows(false);
|
||||
|
||||
for (std::size_t kindIndex = 0; kindIndex < ranges.size(); ++kindIndex) {
|
||||
builder
|
||||
.geometry(
|
||||
kindIndex,
|
||||
filament::RenderableManager::PrimitiveType::LINES,
|
||||
EditorGridVertexBuffer_,
|
||||
EditorGridIndexBuffer_,
|
||||
ranges[kindIndex].Offset,
|
||||
ranges[kindIndex].Count
|
||||
)
|
||||
.material(kindIndex, EditorGridMaterialInstances_[kindIndex])
|
||||
.blendOrder(kindIndex, static_cast<uint16_t>(kindIndex));
|
||||
}
|
||||
|
||||
if (static_cast<int>(builder.build(*Engine_, EditorGridEntity_)) != 0) {
|
||||
DestroyEditorGridRenderable();
|
||||
return false;
|
||||
}
|
||||
|
||||
EditorGridBuilt_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void DestroyEditorGridRenderable() {
|
||||
if (Engine_ == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (EditorGridEntityInScene_ && Scene_ != nullptr) {
|
||||
Scene_->remove(EditorGridEntity_);
|
||||
EntitiesInScene_.erase(EditorGridEntity_);
|
||||
EditorGridEntityInScene_ = false;
|
||||
}
|
||||
|
||||
if (EditorGridEntity_) {
|
||||
auto& renderableManager = Engine_->getRenderableManager();
|
||||
if (renderableManager.getInstance(EditorGridEntity_)) {
|
||||
renderableManager.destroy(EditorGridEntity_);
|
||||
}
|
||||
Engine_->destroy(EditorGridEntity_);
|
||||
utils::EntityManager::get().destroy(EditorGridEntity_);
|
||||
EditorGridEntity_ = {};
|
||||
}
|
||||
|
||||
for (filament::MaterialInstance*& materialInstance : EditorGridMaterialInstances_) {
|
||||
if (materialInstance != nullptr) {
|
||||
Engine_->destroy(materialInstance);
|
||||
materialInstance = nullptr;
|
||||
}
|
||||
}
|
||||
if (EditorGridVertexBuffer_ != nullptr) {
|
||||
Engine_->destroy(EditorGridVertexBuffer_);
|
||||
EditorGridVertexBuffer_ = nullptr;
|
||||
}
|
||||
if (EditorGridIndexBuffer_ != nullptr) {
|
||||
Engine_->destroy(EditorGridIndexBuffer_);
|
||||
EditorGridIndexBuffer_ = nullptr;
|
||||
}
|
||||
if (EditorGridMaterial_ != nullptr) {
|
||||
Engine_->destroy(EditorGridMaterial_);
|
||||
EditorGridMaterial_ = nullptr;
|
||||
}
|
||||
|
||||
EditorGridBuilt_ = false;
|
||||
}
|
||||
|
||||
bool EntityBelongsToAsset(filament::gltfio::FilamentAsset* asset, utils::Entity entity) const {
|
||||
if (asset == nullptr || entity.isNull()) {
|
||||
return false;
|
||||
@ -2518,6 +2871,15 @@ private:
|
||||
std::unordered_map<MetaCoreId, utils::Entity> SceneLightEntities_;
|
||||
std::unordered_set<utils::Entity, utils::Entity::Hasher> EntitiesInScene_;
|
||||
|
||||
bool EditorGridVisibleRequested_ = false;
|
||||
bool EditorGridBuilt_ = false;
|
||||
bool EditorGridEntityInScene_ = false;
|
||||
utils::Entity EditorGridEntity_{};
|
||||
filament::VertexBuffer* EditorGridVertexBuffer_ = nullptr;
|
||||
filament::IndexBuffer* EditorGridIndexBuffer_ = nullptr;
|
||||
filament::Material* EditorGridMaterial_ = nullptr;
|
||||
std::array<filament::MaterialInstance*, 4> EditorGridMaterialInstances_{};
|
||||
|
||||
filament::View* UIView_ = nullptr;
|
||||
MetaCoreImGuiHelper* ImGuiHelper_ = nullptr;
|
||||
|
||||
@ -2571,6 +2933,10 @@ void MetaCoreFilamentSceneBridge::ApplySceneView(const MetaCoreSceneView& sceneV
|
||||
Impl_->ApplySceneView(sceneView);
|
||||
}
|
||||
|
||||
void MetaCoreFilamentSceneBridge::SetEditorGridVisible(bool visible) {
|
||||
Impl_->SetEditorGridVisible(visible);
|
||||
}
|
||||
|
||||
void MetaCoreFilamentSceneBridge::RenderAll() {
|
||||
Impl_->RenderAll();
|
||||
}
|
||||
@ -2615,4 +2981,8 @@ bool MetaCoreFilamentSceneBridge::VerifyEntityInSceneForTesting(MetaCoreId objec
|
||||
return Impl_->VerifyEntityInSceneForTesting(objectId);
|
||||
}
|
||||
|
||||
bool MetaCoreFilamentSceneBridge::VerifyEditorGridVisibleForTesting() const {
|
||||
return Impl_->VerifyEditorGridVisibleForTesting();
|
||||
}
|
||||
|
||||
} // namespace MetaCore
|
||||
|
||||
@ -23,6 +23,7 @@ public:
|
||||
void RenderSnapshotToViewport(const MetaCoreSceneRenderSyncSnapshot& snapshot, MetaCoreScene* scene, const MetaCoreSceneView& sceneView);
|
||||
void RenderAll();
|
||||
void SetProjectRootPath(const std::filesystem::path& projectRootPath);
|
||||
void SetEditorGridVisible(bool visible);
|
||||
[[nodiscard]] uint32_t GetFilamentGLTextureId() const;
|
||||
[[nodiscard]] void* GetFilamentTexturePointer() const;
|
||||
|
||||
|
||||
@ -30,6 +30,7 @@ public:
|
||||
void SyncScene(MetaCoreScene& scene, bool compatibilityMeshOnly = false, bool useScenePrimaryCamera = false);
|
||||
void SyncScene(const MetaCoreSceneRenderSyncSnapshot& snapshot, MetaCoreScene* scene = nullptr, bool compatibilityMeshOnly = false, bool useScenePrimaryCamera = false);
|
||||
void ApplySceneView(const MetaCoreSceneView& sceneView);
|
||||
void SetEditorGridVisible(bool visible);
|
||||
void RenderAll();
|
||||
[[nodiscard]] uint32_t GetGLTextureId() const;
|
||||
[[nodiscard]] void* GetFilamentTexturePointer() const;
|
||||
@ -43,6 +44,7 @@ public:
|
||||
[[nodiscard]] bool VerifyLightExistsForTesting(MetaCoreId objectId) const;
|
||||
[[nodiscard]] float GetDefaultLightIntensityForTesting() const;
|
||||
[[nodiscard]] bool VerifyEntityInSceneForTesting(MetaCoreId objectId) const;
|
||||
[[nodiscard]] bool VerifyEditorGridVisibleForTesting() const;
|
||||
|
||||
private:
|
||||
class MetaCoreFilamentSceneBridgeImpl;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user