-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Asset loading #13
Asset loading #13
Conversation
…feat/first-render-api
// Allocate a *direct* ByteBuffer and put the bytes into it. | ||
ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.length); | ||
buffer.put(bytes); | ||
// Reset position to 0 to be ready for reading | ||
buffer.flip(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mrousavy I had to do this for the buffer to work as I am using fbjni ByteBuffer implementation which only supports direct(?) buffers. And I think in this step I am copying the buffer again.
In general I am wondering whether I am doing this here the most efficient way (I doubt it). Would appreciate if you could take a stab at making this as copy-less as possible 😄
std::shared_ptr<FilamentBuffer> JFilamentProxy::getAssetByteBuffer(const std::string& path) { | ||
static const auto method = javaClassLocal()->getMethod<jni::alias_ref<jni::JByteBuffer>(jni::alias_ref<jstring>)>("getAssetByteBuffer"); | ||
jni::local_ref<jni::JByteBuffer> localRef = method(_javaPart, jni::make_jstring(path)); | ||
jni::global_ref<jni::JByteBuffer> globalRef = jni::make_global(localRef); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
memory leak!
return 13; | ||
ByteBuffer getAssetByteBuffer(String assetName) throws IOException { | ||
InputStream input = reactContext.getAssets().open(assetName); | ||
byte[] bytes = new byte[input.available()]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
allocates buffer twice!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I found a better solution for it, please have a look
package/cpp/core/EngineWrapper.cpp
Outdated
@@ -24,6 +35,13 @@ EngineWrapper::EngineWrapper(std::shared_ptr<Choreographer> choreographer) { | |||
_engine = References<Engine>::adoptRef(Engine::create(), [](Engine* engine) { Engine::destroy(&engine); }); | |||
_materialProvider = gltfio::createUbershaderProvider(_engine.get(), UBERARCHIVE_DEFAULT_DATA, UBERARCHIVE_DEFAULT_SIZE); | |||
_assetLoader = gltfio::AssetLoader::create(filament::gltfio::AssetConfiguration{.engine = _engine.get(), .materials = _materialProvider}); | |||
_resourceLoader = new filament::gltfio::ResourceLoader({.engine = _engine.get(), .normalizeSkinningWeights = true}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
memory leak!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
References<T>::
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Converted _materialProvider
, _assetLoader
and _resourceLoader
to be shared_ptrs using the references library now!
void EngineWrapper::createDefaultLight() { | ||
// Create default directional light (In ModelViewer this is the default, so we use it here as well) | ||
void EngineWrapper::loadAsset(std::shared_ptr<FilamentBuffer> modelBuffer) { | ||
filament::gltfio::FilamentAsset* asset = _assetLoader->createAsset(modelBuffer->getData(), modelBuffer->getSize()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how does cleanup work here? it's a pointer, does it need to be delete
'd?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To use the References I feel like we'd need a AdoptAssetLoader reference 🤔
Texture* cubemap = ktxreader::Ktx1Reader::createTexture( | ||
_engine.get(), *iblBundle, false, | ||
[](void* userdata) { | ||
auto* bundle = (image::Ktx1Bundle*)userdata; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use dynamic_cast
and check for null (sucessful cast)
buffer.put(bytes); | ||
// Create a channel from the input stream | ||
ReadableByteChannel channel = Channels.newChannel(input); | ||
int estimatedSize = input.available(); // This is not always accurate for the actual size |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what? 😄 you cannot estimate an array size - this needs to be the exact number.
>[!WARNING] > Based on the following PR which should be merged first > - #13 **Goal:** To provide an API from JS that can be used to setup the scene and control the transform of the elements. (Most important) **Changes:** - **Camera:** - Renamed `lookAt` -> `lookAtCameraManipulator` Can be used to make camera look at camera manipulator - New `lookAt` function, which can be used to manually control the position, target and up vector of the camera - New `setLensProjection` & `setProjection` function to Control the lens configuration from JS - **⚠️ Note:** @mrousavy The user would also need to call this when the surface size changes (as we need to pass the aspect ratio to this function). Lets discuss in a quick meeting how to solve this best - **Engine:** - Renamed `createDefaultLight` -> `setIndirectLight`, only sets the indirect light from an FilamentBuffer now (decouples the function from setting a default directional light, which can be now configured by the user) - New `createLightEntity` Function to create a light with the configuration you want - Exposed `transformToUnitCube`. Pass an asset and it will transform the asset to fit into a unit cube - New `setEntityPosition`, `setEntityRotation` and `setEntityScale` to set or update an entities transform - `loadAsset` now returns a `FilamentAsset`. The asset can be used for various operations in the scene. --------- Co-authored-by: Marc Rousavy <me@mrousavy.com>
This reverts commit 9dde746.
>[!WARNING] > Based on the following PR which should be merged first > - #13 **Goal:** To provide an API from JS that can be used to setup the scene and control the transform of the elements. (Most important) **Changes:** - **Camera:** - Renamed `lookAt` -> `lookAtCameraManipulator` Can be used to make camera look at camera manipulator - New `lookAt` function, which can be used to manually control the position, target and up vector of the camera - New `setLensProjection` & `setProjection` function to Control the lens configuration from JS - **⚠️ Note:** @mrousavy The user would also need to call this when the surface size changes (as we need to pass the aspect ratio to this function). Lets discuss in a quick meeting how to solve this best - **Engine:** - Renamed `createDefaultLight` -> `setIndirectLight`, only sets the indirect light from an FilamentBuffer now (decouples the function from setting a default directional light, which can be now configured by the user) - New `createLightEntity` Function to create a light with the configuration you want - Exposed `transformToUnitCube`. Pass an asset and it will transform the asset to fit into a unit cube - New `setEntityPosition`, `setEntityRotation` and `setEntityScale` to set or update an entities transform - `loadAsset` now returns a `FilamentAsset`. The asset can be used for various operations in the scene. --------- Co-authored-by: Marc Rousavy <me@mrousavy.com>
>[!WARNING] > Based on the following PR which should be merged first > - #13 **Goal:** To provide an API from JS that can be used to setup the scene and control the transform of the elements. (Most important) **Changes:** - **Camera:** - Renamed `lookAt` -> `lookAtCameraManipulator` Can be used to make camera look at camera manipulator - New `lookAt` function, which can be used to manually control the position, target and up vector of the camera - New `setLensProjection` & `setProjection` function to Control the lens configuration from JS - **⚠️ Note:** @mrousavy The user would also need to call this when the surface size changes (as we need to pass the aspect ratio to this function). Lets discuss in a quick meeting how to solve this best - **Engine:** - Renamed `createDefaultLight` -> `setIndirectLight`, only sets the indirect light from an FilamentBuffer now (decouples the function from setting a default directional light, which can be now configured by the user) - New `createLightEntity` Function to create a light with the configuration you want - Exposed `transformToUnitCube`. Pass an asset and it will transform the asset to fit into a unit cube - New `setEntityPosition`, `setEntityRotation` and `setEntityScale` to set or update an entities transform - `loadAsset` now returns a `FilamentAsset`. The asset can be used for various operations in the scene. --------- --------- Co-authored-by: Hanno J. Gödecke <hanno@margelo.io> Co-authored-by: Hanno J. Gödecke <die.drei99@yahoo.de>
>[!WARNING] > Based on the following PR which should be merged first > - #13 **Goal:** To provide an API from JS that can be used to setup the scene and control the transform of the elements. (Most important) **Changes:** - **Camera:** - Renamed `lookAt` -> `lookAtCameraManipulator` Can be used to make camera look at camera manipulator - New `lookAt` function, which can be used to manually control the position, target and up vector of the camera - New `setLensProjection` & `setProjection` function to Control the lens configuration from JS - **⚠️ Note:** @mrousavy The user would also need to call this when the surface size changes (as we need to pass the aspect ratio to this function). Lets discuss in a quick meeting how to solve this best - **Engine:** - Renamed `createDefaultLight` -> `setIndirectLight`, only sets the indirect light from an FilamentBuffer now (decouples the function from setting a default directional light, which can be now configured by the user) - New `createLightEntity` Function to create a light with the configuration you want - Exposed `transformToUnitCube`. Pass an asset and it will transform the asset to fit into a unit cube - New `setEntityPosition`, `setEntityRotation` and `setEntityScale` to set or update an entities transform - `loadAsset` now returns a `FilamentAsset`. The asset can be used for various operations in the scene. --------- Co-authored-by: Marc Rousavy <me@mrousavy.com>
Todo
Changes
Goal of this PR is to make it possible to load assets and add them to the scene.
Note that we also need assets to load ktx files for lighting.
Right now the assets need to be bundled within the app's assets / bundle (there is no loading from JS code yet).