diff --git a/docs/docs/guides/SKYBOX.mdx b/docs/docs/guides/SKYBOX.mdx index 99fdac57..efd0f5ef 100644 --- a/docs/docs/guides/SKYBOX.mdx +++ b/docs/docs/guides/SKYBOX.mdx @@ -24,7 +24,7 @@ import { Skybox } from 'react-native-filament'; ```tsx function Scene() { - const cubemap = useBuffer(require("./skybox_cubemap.ktx")) + const cubemap = require("./skybox_cubemap.ktx") return ( diff --git a/package/cpp/core/RNFEngineImpl.Skybox.cpp b/package/cpp/core/RNFEngineImpl.Skybox.cpp index a75915c8..a1157340 100644 --- a/package/cpp/core/RNFEngineImpl.Skybox.cpp +++ b/package/cpp/core/RNFEngineImpl.Skybox.cpp @@ -3,11 +3,15 @@ // #include "RNFEngineImpl.h" + #include "RNFReferences.h" #include "utils/RNFConverter.h" + #include #include +#include + namespace margelo { void EngineImpl::createAndSetSkybox(std::string hexColor, std::optional showSun, std::optional envIntensity) { Skybox::Builder builder = Skybox::Builder(); @@ -29,9 +33,42 @@ void EngineImpl::createAndSetSkybox(std::string hexColor, std::optional sh _scene->setSkybox(_skybox.get()); } -void EngineImpl::createAndSetSkybox(std::optional> textureBuffer, std::optional showSun, +void EngineImpl::createAndSetSkybox(std::shared_ptr textureBuffer, std::optional showSun, std::optional envIntensity) { - throw std::runtime_error("Not implemented yet"); + Skybox::Builder builder = Skybox::Builder(); + if (showSun.has_value()) { + builder.showSun(showSun.value()); + } + if (envIntensity.has_value()) { + builder.intensity(envIntensity.value()); + } + + if (!textureBuffer) { + throw std::runtime_error("Texture buffer is null"); + } + auto buffer = textureBuffer->getBuffer(); + if (buffer->getSize() == 0) { + throw std::runtime_error("Texture buffer is empty"); + } + + auto* bundle = new image::Ktx1Bundle(buffer->getData(), buffer->getSize()); + Texture* cubemap = ktxreader::Ktx1Reader::createTexture( + _engine.get(), *bundle, false, + [](void* userdata) { + auto* bundle = (image::Ktx1Bundle*)userdata; + delete bundle; + }, + bundle); + + builder.environment(cubemap); + + Skybox* skybox = builder.build(*_engine); + _skybox = References::adoptEngineRef(_engine, skybox, [cubemap](std::shared_ptr engine, Skybox* skybox) { + Logger::log(TAG, "Destroying Skybox..."); + engine->destroy(skybox); + engine->destroy(cubemap); + }); + _scene->setSkybox(_skybox.get()); } void EngineImpl::clearSkybox() { diff --git a/package/cpp/core/RNFEngineImpl.h b/package/cpp/core/RNFEngineImpl.h index 60f67b6b..4a42677a 100644 --- a/package/cpp/core/RNFEngineImpl.h +++ b/package/cpp/core/RNFEngineImpl.h @@ -63,7 +63,7 @@ class EngineImpl : public std::enable_shared_from_this { std::shared_ptr createNameComponentManager(); std::shared_ptr createMaterial(std::shared_ptr materialBuffer); void createAndSetSkybox(std::string hexColor, std::optional showSun, std::optional envIntensity); - void createAndSetSkybox(std::optional> textureBuffer, std::optional showSun, + void createAndSetSkybox(std::shared_ptr textureBuffer, std::optional showSun, std::optional envIntensity); void clearSkybox(); void setAutomaticInstancingEnabled(bool enabled); diff --git a/package/example/Shared/src/App.tsx b/package/example/Shared/src/App.tsx index ba2d4411..977659c0 100644 --- a/package/example/Shared/src/App.tsx +++ b/package/example/Shared/src/App.tsx @@ -18,6 +18,7 @@ import { FadeOut } from './FadeOut' import { CastShadow } from './CastShadow' import { ScaleEffect } from './ScaleEffect' import { ChangeMaterials } from './ChangeMaterials' +import { SkyboxExample } from './SkyboxExample' function NavigationItem(props: { name: string; route: string }) { const navigation = useNavigation() @@ -62,6 +63,7 @@ function HomeScreen() { + ) } @@ -103,6 +105,7 @@ function App() { + diff --git a/package/example/Shared/src/SkyboxExample.tsx b/package/example/Shared/src/SkyboxExample.tsx new file mode 100644 index 00000000..8528dfc6 --- /dev/null +++ b/package/example/Shared/src/SkyboxExample.tsx @@ -0,0 +1,30 @@ +import * as React from 'react' + +import { StyleSheet } from 'react-native' +import { FilamentView, Camera, FilamentScene, Model, Light, Skybox } from 'react-native-filament' + +function Renderer() { + return ( + + + + + + + + ) +} + +export function SkyboxExample() { + return ( + + + + ) +} + +const styles = StyleSheet.create({ + container: { + flex: 1, + }, +})