refactoring

This commit is contained in:
Nick Fisher
2025-03-19 12:27:13 +08:00
parent d5bffd5ad2
commit 627447f8b0
65 changed files with 2304 additions and 3005 deletions

View File

@@ -1,5 +1,3 @@
#if __APPLE__
#include "TargetConditionals.h"
#endif
@@ -48,57 +46,39 @@ namespace thermion
using std::string;
static constexpr filament::math::float4 sFullScreenTriangleVertices[3] = {
{-1.0f, -1.0f, 1.0f, 1.0f},
{3.0f, -1.0f, 1.0f, 1.0f},
{-1.0f, 3.0f, 1.0f, 1.0f}};
static const uint16_t sFullScreenTriangleIndices[3] = {0, 1, 2};
void RenderTicker::setRenderable(SwapChain *swapChain, View **views, uint8_t numViews) {
{
std::lock_guard lock(mMutex);
auto swapChainViews = mRenderable[swapChain];
// Find if this swapChain already exists in our collection
auto it = std::find_if(mRenderable.begin(), mRenderable.end(),
[swapChain](const auto& pair) { return pair.first == swapChain; });
swapChainViews.clear();
// Prepare the vector of views
std::vector<View*> swapChainViews;
for(int i = 0; i < numViews; i++) {
swapChainViews.push_back(views[i]);
}
mRenderable[swapChain] = swapChainViews;
// Keep track of the swapchains, so we can iterate them in the render method.
bool found = false;
for (auto existingSwapChain : mSwapChains) {
if (existingSwapChain == swapChain) {
found = true;
break;
}
}
if (!found) {
mSwapChains.push_back(swapChain);
if (it != mRenderable.end()) {
// Update existing entry
it->second = swapChainViews;
} else {
// Add new entry
mRenderable.emplace_back(swapChain, swapChainViews);
}
}
}
void RenderTicker::render(uint64_t frameTimeInNanos)
{
std::lock_guard lock(mMutex);
// Update all animation managers
for (auto animationManager : mAnimationManagers) {
if (animationManager) { // Check for nullptr just in case
animationManager->update(frameTimeInNanos * 1e-9);
}
animationManager->update(frameTimeInNanos * 1e-9);
}
for (auto swapChain : mSwapChains)
for (const auto& [swapChain, views] : mRenderable)
{
auto views = mRenderable[swapChain];
if (views.size() > 0)
if (!views.empty())
{
bool beginFrame = mRenderer->beginFrame(swapChain, frameTimeInNanos);
if (beginFrame)
@@ -116,19 +96,19 @@ namespace thermion
#endif
}
void RenderTicker::addAnimationManager(AnimationManager* animationManager) {
std::lock_guard<std::mutex> lock(mMutex);
mAnimationManagers.push_back(animationManager);
}
void RenderTicker::addAnimationManager(AnimationManager* animationManager) {
std::lock_guard<std::mutex> lock(mMutex);
mAnimationManagers.push_back(animationManager);
}
void RenderTicker::removeAnimationManager(AnimationManager* animationManager) {
std::lock_guard<std::mutex> lock(mMutex);
auto it = std::find(mAnimationManagers.begin(), mAnimationManagers.end(), animationManager);
if (it != mAnimationManagers.end()) {
mAnimationManagers.erase(it);
}
void RenderTicker::removeAnimationManager(AnimationManager* animationManager) {
std::lock_guard<std::mutex> lock(mMutex);
auto it = std::find(mAnimationManagers.begin(), mAnimationManagers.end(), animationManager);
if (it != mAnimationManagers.end()) {
mAnimationManagers.erase(it);
}
}
RenderTicker::~RenderTicker() {}
RenderTicker::~RenderTicker() {}
} // namespace thermion

View File

@@ -47,6 +47,16 @@ namespace thermion
camera->setCustomProjection(convert_double4x4_to_mat4(projectionMatrix), near, far);
}
EMSCRIPTEN_KEEPALIVE double Camera_getFocusDistance(TCamera *camera) {
auto *camera = reinterpret_cast<Camera *>(tCamera);
return camera->getFocusDistance();
}
EMSCRIPTEN_KEEPALIVE void Camera_setFocusDistance(TCamera *camera, float distance) {
auto *camera = reinterpret_cast<Camera *>(tCamera);
return camera->setFocusDistance(distance);
}
EMSCRIPTEN_KEEPALIVE double4x4 Camera_getModelMatrix(TCamera *tCamera)
{
auto *camera = reinterpret_cast<Camera *>(tCamera);

View File

@@ -7,6 +7,7 @@
#include <math/vec2.h>
#include "Log.hpp"
#include "materials/image.h"
#include "c_api/TMaterialInstance.h"
#ifdef __cplusplus
@@ -23,6 +24,26 @@ namespace thermion
return reinterpret_cast<TMaterialInstance *>(instance);
}
EMSCRIPTEN_KEEPALIVE TMaterial *Material_createImageMaterial(TEngine *tEngine) {
auto *engine = reinterpret_cast<filament::Engine *>(tEngine);
auto *material = Material::Builder()
.package(IMAGE_IMAGE_DATA, IMAGE_IMAGE_SIZE)
.build(*engine);
return reinterpret_cast<TMaterial *>(material);
}
EMSCRIPTEN_KEEPALIVE TMaterial *Material_createGridMaterial(TEngine *tEngine) {
auto *engine = reinterpret_cast<filament::Engine *>(tEngine);
auto *material = Material::Builder()
.package(GRID_GRID_DATA, GRID_GRID_SIZE)
.build(*engine);
return reinterpret_cast<TMaterial *>(material);
}
EMSCRIPTEN_KEEPALIVE bool Material_hasParameter(TMaterial *tMaterial, const char *propertyName) {
auto *material = reinterpret_cast<filament::Material *>(tMaterial);
return material->hasParameter(propertyName);

View File

@@ -6,6 +6,7 @@
#include "c_api/TGltfAssetLoader.h"
#include "c_api/TSceneAsset.h"
#include "scene/GridOverlay.hpp"
#include "scene/SceneAsset.hpp"
#include "scene/GltfSceneAsset.hpp"
#include "scene/GeometrySceneAssetBuilder.hpp"
@@ -89,6 +90,13 @@ extern "C"
return reinterpret_cast<TSceneAsset *>(sceneAsset);
}
EMSCRIPTEN_KEEPALIVE TSceneAsset *SceneAsset_createGrid(TEngine *tEngine, TMaterial* tMaterial) {
auto *engine = reinterpret_cast<filament::Engine *>(tEngine);
auto *material = reinterpret_cast<filament::Material *>(tMaterial);
auto *asset = new GridOverlay(*engine, material);
return reinterpret_cast<TSceneAsset *>(asset);
}
EMSCRIPTEN_KEEPALIVE void SceneAsset_destroy(TSceneAsset *tSceneAsset) {
auto *asset = reinterpret_cast<SceneAsset*>(tSceneAsset);

View File

@@ -67,8 +67,8 @@ extern "C"
return;
}
auto center = aabb.center();
auto halfExtent = aabb.extent();
auto center = filament::math::float3 { boundingBox.centerX, boundingBox.centerY, boundingBox.centerZ };
auto halfExtent = filament::math::float3 { boundingBox.halfExtentX, boundingBox.halfExtentY, boundingBox.halfExtentZ };
auto maxExtent = max(halfExtent) * 2;
auto scaleFactor = 2.0f / maxExtent;
auto transform = math::mat4f::scaling(scaleFactor) * math::mat4f::translation(-center);