fix ResourceBuffer going out of scope before texture callbacks

This commit is contained in:
Nick Fisher
2023-10-03 12:46:14 +11:00
parent 4a9bf91fae
commit 4492a9fa8f
168 changed files with 36980 additions and 6 deletions

View File

@@ -0,0 +1,120 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef GLTFIO_ANIMATOR_H
#define GLTFIO_ANIMATOR_H
#include <gltfio/FilamentAsset.h>
#include <gltfio/FilamentInstance.h>
namespace filament::gltfio {
struct FFilamentAsset;
struct FFilamentInstance;
struct AnimatorImpl;
/**
* \class Animator Animator.h gltfio/Animator.h
* \brief Updates matrices according to glTF \c animation and \c skin definitions.
*
* Animator can be used for two things:
* - Updating matrices in filament::TransformManager components according to glTF \c animation definitions.
* - Updating bone matrices in filament::RenderableManager components according to glTF \c skin definitions.
*
* For a usage example, see the documentation for AssetLoader.
*/
class UTILS_PUBLIC Animator {
public:
/**
* Applies rotation, translation, and scale to entities that have been targeted by the given
* animation definition. Uses filament::TransformManager.
*
* @param animationIndex Zero-based index for the \c animation of interest.
* @param time Elapsed time of interest in seconds.
*/
void applyAnimation(size_t animationIndex, float time) const;
/**
* Computes root-to-node transforms for all bone nodes, then passes
* the results into filament::RenderableManager::setBones.
* Uses filament::TransformManager and filament::RenderableManager.
*
* NOTE: this operation is independent of \c animation.
*/
void updateBoneMatrices();
/**
* Applies a blended transform to the union of nodes affected by two animations.
* Used for cross-fading from a previous skinning-based animation or rigid body animation.
*
* First, this stashes the current transform hierarchy into a transient memory buffer.
*
* Next, this applies previousAnimIndex / previousAnimTime to the actual asset by internally
* calling applyAnimation().
*
* Finally, the stashed local transforms are lerped (via the scale / translation / rotation
* components) with their live counterparts, and the results are pushed to the asset.
*
* To achieve a cross fade effect with skinned models, clients will typically call animator
* methods in this order: (1) applyAnimation (2) applyCrossFade (3) updateBoneMatrices. The
* animation that clients pass to applyAnimation is the "current" animation corresponding to
* alpha=1, while the "previous" animation passed to applyCrossFade corresponds to alpha=0.
*/
void applyCrossFade(size_t previousAnimIndex, float previousAnimTime, float alpha);
/**
* Pass the identity matrix into all bone nodes, useful for returning to the T pose.
*
* NOTE: this operation is independent of \c animation.
*/
void resetBoneMatrices();
/** Returns the number of \c animation definitions in the glTF asset. */
size_t getAnimationCount() const;
/** Returns the duration of the specified glTF \c animation in seconds. */
float getAnimationDuration(size_t animationIndex) const;
/**
* Returns a weak reference to the string name of the specified \c animation, or an
* empty string if none was specified.
*/
const char* getAnimationName(size_t animationIndex) const;
// For internal use only.
void addInstance(FFilamentInstance* instance);
private:
/*! \cond PRIVATE */
friend struct FFilamentAsset;
friend struct FFilamentInstance;
/*! \endcond */
// If "instance" is null, then this is the primary animator.
Animator(FFilamentAsset const* asset, FFilamentInstance* instance);
~Animator();
Animator(const Animator& animator) = delete;
Animator(Animator&& animator) = delete;
Animator& operator=(const Animator&) = delete;
AnimatorImpl* mImpl;
};
} // namespace filament::gltfio
#endif // GLTFIO_ANIMATOR_H

View File

@@ -0,0 +1,249 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef GLTFIO_ASSETLOADER_H
#define GLTFIO_ASSETLOADER_H
#include <filament/Engine.h>
#include <filament/Material.h>
#include <gltfio/FilamentAsset.h>
#include <gltfio/FilamentInstance.h>
#include <gltfio/MaterialProvider.h>
#include <utils/compiler.h>
namespace utils {
class EntityManager;
class NameComponentManager;
}
/**
* Loader and pipeline for glTF 2.0 assets.
*/
namespace filament::gltfio {
class NodeManager;
/**
* \struct AssetConfiguration AssetLoader.h gltfio/AssetLoader.h
* \brief Construction parameters for AssetLoader.
*/
struct AssetConfiguration {
//! The engine that the loader should pass to builder objects (e.g.
//! filament::VertexBuffer::Builder).
class filament::Engine* engine;
//! Controls whether the loader uses filamat to generate materials on the fly, or loads a small
//! set of precompiled ubershader materials. Deleting the MaterialProvider is the client's
//! responsibility. See createJitShaderProvider() and createUbershaderProvider().
MaterialProvider* materials;
//! Optional manager for associating string names with entities in the transform hierarchy.
utils::NameComponentManager* names = nullptr;
//! Overrides the factory used for creating entities in the transform hierarchy. If this is not
//! specified, AssetLoader will use the singleton EntityManager associated with the current
//! process.
utils::EntityManager* entities = nullptr;
//! Optional default node name for anonymous nodes
char* defaultNodeName = nullptr;
};
/**
* \class AssetLoader AssetLoader.h gltfio/AssetLoader.h
* \brief Consumes glTF content and produces FilamentAsset objects.
*
* AssetLoader consumes a blob of glTF 2.0 content (either JSON or GLB) and produces a FilamentAsset
* object, which is a bundle of Filament textures, vertex buffers, index buffers, etc. An asset is
* composed of 1 or more FilamentInstance objects which contain entities and components.
*
* Clients must use AssetLoader to create and destroy FilamentAsset objects. This is similar to
* how filament::Engine is used to create and destroy core objects like VertexBuffer.
*
* AssetLoader does not fetch external buffer data or create textures on its own. Clients can use
* ResourceLoader for this, which obtains the URI list from the asset. This is demonstrated in the
* code snippet below.
*
* AssetLoader also owns a cache of filament::Material objects that may be re-used across multiple
* loads.
*
* Example usage:
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* auto engine = Engine::create();
* auto materials = createJitShaderProvider(engine);
* auto decoder = createStbProvider(engine);
* auto loader = AssetLoader::create({engine, materials});
*
* // Parse the glTF content and create Filament entities.
* std::vector<uint8_t> content(...);
* FilamentAsset* asset = loader->createAsset(content.data(), content.size());
* content.clear();
*
* // Load buffers and textures from disk.
* ResourceLoader resourceLoader({engine, ".", true});
* resourceLoader.addTextureProvider("image/png", decoder)
* resourceLoader.addTextureProvider("image/jpeg", decoder)
* resourceLoader.loadResources(asset);
*
* // Free the glTF hierarchy as it is no longer needed.
* asset->releaseSourceData();
*
* // Add renderables to the scene.
* scene->addEntities(asset->getEntities(), asset->getEntityCount());
*
* // Extract the animator interface from the FilamentInstance.
* auto animator = asset->getInstance()->getAnimator();
*
* // Execute the render loop and play the first animation.
* do {
* animator->applyAnimation(0, time);
* animator->updateBoneMatrices();
* if (renderer->beginFrame(swapChain)) {
* renderer->render(view);
* renderer->endFrame();
* }
* } while (!quit);
*
* scene->removeEntities(asset->getEntities(), asset->getEntityCount());
* loader->destroyAsset(asset);
* materials->destroyMaterials();
* delete materials;
* delete decoder;
* AssetLoader::destroy(&loader);
* Engine::destroy(&engine);
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
class UTILS_PUBLIC AssetLoader {
public:
/**
* Creates an asset loader for the given configuration, which specifies the Filament engine.
*
* The engine is held weakly, used only for the creation and destruction of Filament objects.
* The optional name component manager can be used to assign names to renderables.
* The material source specifies whether to use filamat to generate materials on the fly, or to
* load a small set of precompiled ubershader materials.
*/
static AssetLoader* create(const AssetConfiguration& config);
/**
* Frees the loader.
*
* This does not not automatically free the cache of materials, nor
* does it free the entities for created assets (see destroyAsset).
*/
static void destroy(AssetLoader** loader);
/**
* Takes a pointer to the contents of a GLB or a JSON-based glTF 2.0 file and returns an asset
* with one instance, or null on failure.
*/
FilamentAsset* createAsset(const uint8_t* bytes, uint32_t nbytes);
/**
* Consumes the contents of a glTF 2.0 file and produces a primary asset with one or more
* instances. The primary asset has ownership over the instances.
*
* The returned instances share their textures, materials, and vertex buffers with the primary
* asset. However each instance has its own unique set of entities, transform components,
* material instances, and renderable components. Instances are freed when the primary asset is
* freed.
*
* Light components are not instanced, they belong only to the primary asset.
*
* Clients must use ResourceLoader to load resources on the primary asset.
*
* The entity accessor and renderable stack API in the primary asset can be used to control the
* union of all instances. The individual FilamentInstance objects can be used to access each
* instance's partition of entities. Similarly, the Animator in the primary asset controls all
* instances. To animate instances individually, use FilamentInstance::getAnimator().
*
* @param bytes the contents of a glTF 2.0 file (JSON or GLB)
* @param numBytes the number of bytes in "bytes"
* @param instances destination pointer, to be populated by the requested number of instances
* @param numInstances requested number of instances
* @return the primary asset that has ownership over all instances
*/
FilamentAsset* createInstancedAsset(const uint8_t* bytes, uint32_t numBytes,
FilamentInstance** instances, size_t numInstances);
/**
* Adds a new instance to the asset.
*
* Use this with caution. It is more efficient to pre-allocate a max number of instances, and
* gradually add them to the scene as needed. Instances can also be "recycled" by removing and
* re-adding them to the scene.
*
* NOTE: destroyInstance() does not exist because gltfio favors flat arrays for storage of
* entity lists and instance lists, which would be slow to shift. We also wish to discourage
* create/destroy churn, as noted above.
*
* This cannot be called after FilamentAsset::releaseSourceData().
* See also AssetLoader::createInstancedAsset().
*/
FilamentInstance* createInstance(FilamentAsset* primary);
/**
* Allows clients to enable diagnostic shading on newly-loaded assets.
*/
void enableDiagnostics(bool enable = true);
/**
* Destroys the given asset, all of its associated Filament objects, and all associated
* FilamentInstance objects.
*
* This destroys entities, components, material instances, vertex buffers, index buffers,
* and textures. This does not necessarily immediately free all source data, since
* texture decoding or GPU uploading might be underway.
*/
void destroyAsset(const FilamentAsset* asset);
/**
* Gets a weak reference to an array of cached materials, used internally to create material
* instances for assets.
*/
const filament::Material* const* getMaterials() const noexcept;
/**
* Gets the number of cached materials.
*/
size_t getMaterialsCount() const noexcept;
utils::NameComponentManager* getNames() const noexcept;
NodeManager& getNodeManager() noexcept;
MaterialProvider& getMaterialProvider() noexcept;
/*! \cond PRIVATE */
protected:
AssetLoader() noexcept = default;
~AssetLoader() = default;
public:
AssetLoader(AssetLoader const&) = delete;
AssetLoader(AssetLoader&&) = delete;
AssetLoader& operator=(AssetLoader const&) = delete;
AssetLoader& operator=(AssetLoader&&) = delete;
/*! \endcond */
};
} // namespace filament::gltfio
#endif // GLTFIO_ASSETLOADER_H

View File

@@ -0,0 +1,303 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef GLTFIO_FILAMENTASSET_H
#define GLTFIO_FILAMENTASSET_H
#include <filament/Box.h>
#include <filament/TextureSampler.h>
#include <gltfio/NodeManager.h>
#include <utils/compiler.h>
#include <utils/Entity.h>
namespace filament {
class Camera;
class Engine;
class MaterialInstance;
class Scene;
}
namespace filament::gltfio {
class Animator;
class FilamentInstance;
/**
* \class FilamentAsset FilamentAsset.h gltfio/FilamentAsset.h
* \brief Owns a bundle of Filament objects that have been created by AssetLoader.
*
* For usage instructions, see the documentation for AssetLoader.
*
* This class owns a hierarchy of entities that have been loaded from a glTF asset. Every entity has
* a filament::TransformManager component, and some entities also have \c Name, \c Renderable,
* \c Light, \c Camera, or \c Node components.
*
* In addition to the aforementioned entities, an asset has strong ownership over a list of
* filament::VertexBuffer, filament::IndexBuffer, filament::Texture,
* and, optionally, a simple animation engine (gltfio::Animator).
*
* Clients must use ResourceLoader to create filament::Texture objects, compute tangent quaternions,
* and upload data into vertex buffers and index buffers.
*
* \todo Only the default glTF scene is loaded, other glTF scenes are ignored.
*/
class UTILS_PUBLIC FilamentAsset {
public:
using Entity = utils::Entity;
using SceneMask = NodeManager::SceneMask;
/**
* Gets the list of entities, one for each glTF node. All of these have a Transform component.
* Some of the returned entities may also have a Renderable component and/or a Light component.
*/
const Entity* getEntities() const noexcept;
/**
* Gets the number of entities returned by getEntities().
*/
size_t getEntityCount() const noexcept;
/**
* Gets the list of entities in the scene representing lights. All of these have a Light component.
*/
const Entity* getLightEntities() const noexcept;
/**
* Gets the number of entities returned by getLightEntities().
*/
size_t getLightEntityCount() const noexcept;
/**
* Gets the list of entities in the asset that have renderable components.
*/
const utils::Entity* getRenderableEntities() const noexcept;
/**
* Gets the number of entities returned by getRenderableEntities().
*/
size_t getRenderableEntityCount() const noexcept;
/**
* Gets the list of entities in the scene representing cameras. All of these have a \c Camera
* component.
*
* Note about aspect ratios:
* gltfio always uses an aspect ratio of 1.0 when setting the projection matrix for perspective
* cameras. gltfio then sets the camera's scaling matrix with the aspect ratio specified in the
* glTF file (if present).
*
* The camera's scaling matrix allows clients to adjust the aspect ratio independently from the
* camera's projection.
*
* To change the aspect ratio of the glTF camera:
*
* camera->setScaling(double4 {1.0 / newAspectRatio, 1.0, 1.0, 1.0});
*
* @see filament::Camera::setScaling
*/
const Entity* getCameraEntities() const noexcept;
/**
* Gets the number of entities returned by getCameraEntities().
*/
size_t getCameraEntityCount() const noexcept;
/**
* Gets the transform root for the asset, which has no matching glTF node.
*
* This node exists for convenience, allowing users to transform the entire asset. For instanced
* assets, this is a "super root" where each of its children is a root in a particular instance.
* This allows users to transform all instances en masse if they wish to do so.
*/
Entity getRoot() const noexcept;
/**
* Pops a ready renderable off the queue, or returns 0 if no renderables have become ready.
*
* NOTE: To determine the progress percentage or completion status, please use
* ResourceLoader#asyncGetLoadProgress. To get the number of ready renderables,
* please use popRenderables().
*
* This method allows clients to progressively add the asset's renderables to the scene as
* textures gradually become ready through asynchronous loading. For example, on every frame
* progressive applications can do something like this:
*
* while (Entity e = popRenderable()) { scene.addEntity(e); }
*
* Progressive reveal is not supported for dynamically added instances.
*
* \see ResourceLoader#asyncBeginLoad
* \see popRenderables()
*/
Entity popRenderable() noexcept;
/**
* Pops up to "count" ready renderables off the queue, or returns the available number.
*
* The given pointer should either be null or point to memory that can hold up to count
* entities. If the pointer is null, returns the number of available renderables. Otherwise
* returns the number of entities that have been written.
*
* \see ResourceLoader#asyncBeginLoad
*/
size_t popRenderables(Entity* entities, size_t count) noexcept;
/** Gets resource URIs for all externally-referenced buffers. */
const char* const* getResourceUris() const noexcept;
/** Gets the number of resource URIs returned by getResourceUris(). */
size_t getResourceUriCount() const noexcept;
/**
* Gets the bounding box computed from the supplied min / max values in glTF accessors.
*
* This does not return a bounding box over all FilamentInstance, it's just a straightforward
* AAAB that can be determined at load time from the asset data.
*/
filament::Aabb getBoundingBox() const noexcept;
/** Gets the NameComponentManager label for the given entity, if it exists. */
const char* getName(Entity) const noexcept;
/** Returns the first entity with the given name, or 0 if none exist. */
Entity getFirstEntityByName(const char* name) noexcept;
/**
* Gets a list of entities with the given name.
*
* @param name Null-terminated string to match.
* @param entities Pointer to an array to populate.
* @param maxCount Maximum number of entities to retrieve.
*
* @return If entities is non-null, the number of entities written to the entity pointer.
* Otherwise this returns the number of entities with the given name.
*/
size_t getEntitiesByName(const char* name, Entity* entities,
size_t maxCount) const noexcept;
/**
* Gets a list of entities whose names start with the given prefix.
*
* @param prefix Null-terminated prefix string to match.
* @param entities Pointer to an array to populate.
* @param maxCount Maximum number of entities to retrieve.
*
* @return If entities is non-null, the number of entities written to the entity pointer.
* Otherwise this returns the number of entities with the given prefix.
*/
size_t getEntitiesByPrefix(const char* prefix, Entity* entities,
size_t maxCount) const noexcept;
/** Gets the glTF extras string for a specific node, or for the asset, if it exists. */
const char* getExtras(Entity entity = {}) const noexcept;
/**
* Gets the morph target name at the given index in the given entity.
*/
const char* getMorphTargetNameAt(Entity entity, size_t targetIndex) const noexcept;
/**
* Returns the number of morph targets in the given entity.
*/
size_t getMorphTargetCountAt(Entity entity) const noexcept;
/**
* Lazily creates a single LINES renderable that draws the transformed bounding-box hierarchy
* for diagnostic purposes. The wireframe is owned by the asset so clients should not delete it.
*/
Entity getWireframe() noexcept;
/**
* Returns the Filament engine associated with the AssetLoader that created this asset.
*/
filament::Engine* getEngine() const noexcept;
/**
* Reclaims CPU-side memory for URI strings, binding lists, and raw animation data.
*
* This should only be called after ResourceLoader::loadResources().
* If this is an instanced asset, this prevents creation of new instances.
*/
void releaseSourceData() noexcept;
/**
* Returns a weak reference to the underlying cgltf hierarchy. This becomes invalid after
* calling releaseSourceData().
*/
const void* getSourceAsset() noexcept;
/**
* Returns the number of scenes in the asset.
*/
size_t getSceneCount() const noexcept;
/**
* Returns the name of the given scene.
*
* Returns null if the given scene does not have a name or is out of bounds.
*/
const char* getSceneName(size_t sceneIndex) const noexcept;
/**
* Adds entities to a Filament scene only if they belong to at least one of the given glTF
* scenes.
*
* This is just a helper that provides an alternative to directly calling scene->addEntities()
* and provides filtering functionality.
*/
void addEntitiesToScene(filament::Scene& targetScene, const Entity* entities, size_t count,
SceneMask sceneFilter) const;
/**
* Releases ownership of entities and their Filament components.
*
* This makes the client take responsibility for destroying Filament
* components (e.g. Renderable, TransformManager component) as well as
* the underlying entities.
*/
void detachFilamentComponents() noexcept;
bool areFilamentComponentsDetached() const noexcept;
/**
* Convenience function to get the first instance, or null if it doesn't exist.
*/
FilamentInstance* getInstance() noexcept {
return getAssetInstanceCount() > 0 ? getAssetInstances()[0] : nullptr;
}
/*! \cond PRIVATE */
FilamentInstance** getAssetInstances() noexcept;
size_t getAssetInstanceCount() const noexcept;
protected:
FilamentAsset() noexcept = default;
~FilamentAsset() = default;
public:
FilamentAsset(FilamentAsset const&) = delete;
FilamentAsset(FilamentAsset&&) = delete;
FilamentAsset& operator=(FilamentAsset const&) = delete;
FilamentAsset& operator=(FilamentAsset&&) = delete;
/*! \endcond */
};
} // namespace filament::gltfio
#endif // GLTFIO_FILAMENTASSET_H

View File

@@ -0,0 +1,174 @@
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef GLTFIO_FILAMENTINSTANCE_H
#define GLTFIO_FILAMENTINSTANCE_H
#include <utils/compiler.h>
#include <utils/Entity.h>
#include <filament/Box.h>
namespace filament {
class MaterialInstance;
}
namespace filament::gltfio {
class Animator;
class FilamentAsset;
/**
* \class FilamentInstance FilamentInstance.h gltfio/FilamentInstance.h
* \brief Provides access to a hierarchy of entities that have been instanced from a glTF asset.
*
* Every entity has a TransformManager component, and some entities also have \c Name or
* \c Renderable components.
*
* \see AssetLoader::createInstancedAsset()
*/
class UTILS_PUBLIC FilamentInstance {
public:
/**
* Gets the owner of this instance.
*/
FilamentAsset const* getAsset() const noexcept;
/**
* Gets the list of entities in this instance, one for each glTF node. All of these have a
* Transform component. Some of the returned entities may also have a Renderable component or
* Name component.
*/
const utils::Entity* getEntities() const noexcept;
/**
* Gets the number of entities returned by getEntities().
*/
size_t getEntityCount() const noexcept;
/** Gets the transform root for the instance, which has no matching glTF node. */
utils::Entity getRoot() const noexcept;
/**
* Applies the given material variant to all primitives in this instance.
*
* Ignored if variantIndex is out of bounds.
*/
void applyMaterialVariant(size_t variantIndex) noexcept;
/**
* Returns the number of material variants in the asset.
*/
size_t getMaterialVariantCount() const noexcept;
/**
* Returns the name of the given material variant, or null if it is out of bounds.
*/
const char* getMaterialVariantName(size_t variantIndex) const noexcept;
/**
* Returns the animation engine for the instance.
*
* Note that an animator can be obtained either from an individual instance, or from the
* originating FilamentAsset. In the latter case, the animation frame is shared amongst all
* instances. If individual control is desired, users must obtain the animator from the
* individual instances.
*
* The animator is owned by the asset and should not be manually deleted.
*/
Animator* getAnimator() noexcept;
/**
* Gets the number of skins.
*/
size_t getSkinCount() const noexcept;
/**
* Gets the skin name at skin index.
*/
const char* getSkinNameAt(size_t skinIndex) const noexcept;
/**
* Gets the number of joints at skin index.
*/
size_t getJointCountAt(size_t skinIndex) const noexcept;
/**
* Gets joints at skin index.
*/
const utils::Entity* getJointsAt(size_t skinIndex) const noexcept;
/**
* Attaches the given skin to the given node, which must have an associated mesh with
* BONE_INDICES and BONE_WEIGHTS attributes.
*
* This is a no-op if the given skin index or target is invalid.
*/
void attachSkin(size_t skinIndex, utils::Entity target) noexcept;
/**
* Detaches the given skin from the given node.
*
* This is a no-op if the given skin index or target is invalid.
*/
void detachSkin(size_t skinIndex, utils::Entity target) noexcept;
/**
* Gets inverse bind matrices for all joints at the given skin index.
*
* See getJointCountAt for determining the number of matrices returned (i.e. the number of joints).
*/
math::mat4f const* getInverseBindMatricesAt(size_t skinIndex) const;
/**
* Resets the AABB on all renderables by manually computing the bounding box.
*
* THIS IS ONLY USEFUL FOR MALFORMED ASSETS THAT DO NOT HAVE MIN/MAX SET UP CORRECTLY.
*
* Does not affect the return value of getBoundingBox() on the owning asset.
* Cannot be called after releaseSourceData() on the owning asset.
* Can only be called after loadResources() or asyncBeginLoad().
*/
void recomputeBoundingBoxes();
/**
* Gets the axis-aligned bounding box from the supplied min / max values in glTF accessors.
*
* If recomputeBoundingBoxes() has been called, then this returns the recomputed AABB.
*/
Aabb getBoundingBox() const noexcept;
/** Gets all material instances. These are already bound to renderables. */
const MaterialInstance* const* getMaterialInstances() const noexcept;
/** Gets all material instances (non-const). These are already bound to renderables. */
MaterialInstance* const* getMaterialInstances() noexcept;
/** Gets the number of materials returned by getMaterialInstances(). */
size_t getMaterialInstanceCount() const noexcept;
/**
* Releases ownership of material instances.
*
* This makes the client take responsibility for destroying MaterialInstance
* objects. The getMaterialInstances query becomes invalid after detachment.
*/
void detachMaterialInstances();
};
} // namespace filament::gltfio
#endif // GLTFIO_FILAMENTINSTANCE_H

View File

@@ -0,0 +1,214 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef GLTFIO_MATERIALPROVIDER_H
#define GLTFIO_MATERIALPROVIDER_H
#include <filament/Engine.h>
#include <filament/Material.h>
#include <filament/MaterialInstance.h>
#include <utils/compiler.h>
#include <array>
#include <string>
namespace filament::gltfio {
enum class AlphaMode : uint8_t {
OPAQUE,
MASK,
BLEND
};
// The following struct gets hashed so all padding bits should be explicit.
// Tell the compiler to emit a warning if it adds any padding.
UTILS_WARNING_PUSH
UTILS_WARNING_ENABLE_PADDED
/**
* \struct MaterialKey MaterialProvider.h gltfio/MaterialProvider.h
* \brief Small POD structure that specifies the requirements for a glTF material.
* \note This key is processed by MurmurHashFn so please make padding explicit.
*/
struct alignas(4) MaterialKey {
// -- 32 bit boundary --
bool doubleSided : 1;
bool unlit : 1;
bool hasVertexColors : 1;
bool hasBaseColorTexture : 1;
bool hasNormalTexture : 1;
bool hasOcclusionTexture : 1;
bool hasEmissiveTexture : 1;
bool useSpecularGlossiness : 1;
AlphaMode alphaMode : 4;
bool enableDiagnostics : 4;
union {
struct {
bool hasMetallicRoughnessTexture : 1;
uint8_t metallicRoughnessUV : 7;
};
struct {
bool hasSpecularGlossinessTexture : 1;
uint8_t specularGlossinessUV : 7;
};
};
uint8_t baseColorUV;
// -- 32 bit boundary --
bool hasClearCoatTexture : 1;
uint8_t clearCoatUV : 7;
bool hasClearCoatRoughnessTexture : 1;
uint8_t clearCoatRoughnessUV : 7;
bool hasClearCoatNormalTexture : 1;
uint8_t clearCoatNormalUV : 7;
bool hasClearCoat : 1;
bool hasTransmission : 1;
bool hasTextureTransforms : 6;
// -- 32 bit boundary --
uint8_t emissiveUV;
uint8_t aoUV;
uint8_t normalUV;
bool hasTransmissionTexture : 1;
uint8_t transmissionUV : 7;
// -- 32 bit boundary --
bool hasSheenColorTexture : 1;
uint8_t sheenColorUV : 7;
bool hasSheenRoughnessTexture : 1;
uint8_t sheenRoughnessUV : 7;
bool hasVolumeThicknessTexture : 1;
uint8_t volumeThicknessUV : 7;
bool hasSheen : 1;
bool hasIOR : 1;
bool hasVolume : 1;
uint8_t padding : 5;
};
static_assert(sizeof(MaterialKey) == 16, "MaterialKey has unexpected size.");
UTILS_WARNING_POP
bool operator==(const MaterialKey& k1, const MaterialKey& k2);
// Define a mapping from a uv set index in the source asset to one of Filament's uv sets.
enum UvSet : uint8_t { UNUSED, UV0, UV1 };
constexpr int UvMapSize = 8;
using UvMap = std::array<UvSet, UvMapSize>;
inline uint8_t getNumUvSets(const UvMap& uvmap) {
return std::max({
uvmap[0], uvmap[1], uvmap[2], uvmap[3],
uvmap[4], uvmap[5], uvmap[6], uvmap[7],
});
};
/**
* \class MaterialProvider MaterialProvider.h gltfio/MaterialProvider.h
* \brief Interface to a provider of glTF materials (has two implementations).
*
* - The \c JitShaderProvider implementation generates materials at run time (which can be slow) and
* requires the filamat library, but produces streamlined shaders. See createJitShaderProvider().
*
* - The \c UbershaderProvider implementation uses a small number of pre-built materials with complex
* fragment shaders, but does not require any run time work or usage of filamat. See
* createUbershaderProvider().
*
* Both implementations of MaterialProvider maintain a small cache of materials which must be
* explicitly freed using destroyMaterials(). These materials are not freed automatically when the
* MaterialProvider is destroyed, which allows clients to take ownership if desired.
*
*/
class UTILS_PUBLIC MaterialProvider {
public:
virtual ~MaterialProvider() {}
/**
* Creates or fetches a compiled Filament material, then creates an instance from it.
*
* @param config Specifies requirements; might be mutated due to resource constraints.
* @param uvmap Output argument that gets populated with a small table that maps from a glTF uv
* index to a Filament uv index.
* @param label Optional tag that is not a part of the cache key.
* @param extras Optional extras as stringified JSON (not a part of the cache key).
* Does not store the pointer.
*/
virtual MaterialInstance* createMaterialInstance(MaterialKey* config, UvMap* uvmap,
const char* label = "material", const char* extras = nullptr) = 0;
/**
* Creates or fetches a compiled Filament material corresponding to the given config.
*/
virtual Material* getMaterial(MaterialKey* config, UvMap* uvmap,
const char* label = "material") { return nullptr; }
/**
* Gets a weak reference to the array of cached materials.
*/
virtual const Material* const* getMaterials() const noexcept = 0;
/**
* Gets the number of cached materials.
*/
virtual size_t getMaterialsCount() const noexcept = 0;
/**
* Destroys all cached materials.
*
* This is not called automatically when MaterialProvider is destroyed, which allows
* clients to take ownership of the cache if desired.
*/
virtual void destroyMaterials() = 0;
/**
* Returns true if the presence of the given vertex attribute is required.
*
* Some types of providers (e.g. ubershader) require dummy attribute values
* if the glTF model does not provide them.
*/
virtual bool needsDummyData(VertexAttribute attrib) const noexcept = 0;
};
void constrainMaterial(MaterialKey* key, UvMap* uvmap);
void processShaderString(std::string* shader, const UvMap& uvmap,
const MaterialKey& config);
/**
* Creates a material provider that builds materials on the fly, composing GLSL at run time.
*
* @param optimizeShaders Optimizes shaders, but at significant cost to construction time.
* @return New material provider that can build materials at run time.
*
* Requires \c libfilamat to be linked in. Not available in \c libgltfio_core.
*
* @see createUbershaderProvider
*/
UTILS_PUBLIC
MaterialProvider* createJitShaderProvider(Engine* engine, bool optimizeShaders = false);
/**
* Creates a material provider that loads a small set of pre-built materials.
*
* @return New material provider that can quickly load a material from a cache.
*
* @see createJitShaderProvider
*/
UTILS_PUBLIC
MaterialProvider* createUbershaderProvider(Engine* engine, const void* archive,
size_t archiveByteCount);
} // namespace filament::gltfio
#endif // GLTFIO_MATERIALPROVIDER_H

View File

@@ -0,0 +1,110 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef GLTFIO_NODEMANAGER_H
#define GLTFIO_NODEMANAGER_H
#include <filament/FilamentAPI.h>
#include <utils/bitset.h>
#include <utils/compiler.h>
#include <utils/CString.h>
#include <utils/EntityInstance.h>
#include <utils/FixedCapacityVector.h>
namespace utils {
class Entity;
} // namespace utils
namespace filament::gltfio {
class FNodeManager;
/**
* NodeManager is used to add annotate entities with glTF-specific information.
*
* Node components are created by gltfio and exposed to users to allow inspection.
*
* Nodes do not store the glTF hierarchy or names; see TransformManager and NameComponentManager.
*/
class UTILS_PUBLIC NodeManager {
public:
using Instance = utils::EntityInstance<NodeManager>;
using Entity = utils::Entity;
using CString = utils::CString;
using SceneMask = utils::bitset32;
static constexpr size_t MAX_SCENE_COUNT = 32;
/**
* Returns whether a particular Entity is associated with a component of this NodeManager
* @param e An Entity.
* @return true if this Entity has a component associated with this manager.
*/
bool hasComponent(Entity e) const noexcept;
/**
* Gets an Instance representing the node component associated with the given Entity.
* @param e An Entity.
* @return An Instance object, which represents the node component associated with the Entity e.
* @note Use Instance::isValid() to make sure the component exists.
* @see hasComponent()
*/
Instance getInstance(Entity e) const noexcept;
/**
* Creates a node component and associates it with the given entity.
* @param entity An Entity to associate a node component with.
*
* If this component already exists on the given entity, it is first destroyed as if
* destroy(Entity e) was called.
*
* @see destroy()
*/
void create(Entity entity);
/**
* Destroys this component from the given entity.
* @param e An entity.
*
* @see create()
*/
void destroy(Entity e) noexcept;
void setMorphTargetNames(Instance ci, utils::FixedCapacityVector<CString> names) noexcept;
const utils::FixedCapacityVector<CString>& getMorphTargetNames(Instance ci) const noexcept;
void setExtras(Instance ci, CString extras) noexcept;
const CString& getExtras(Instance ci) const noexcept;
void setSceneMembership(Instance ci, SceneMask scenes) noexcept;
SceneMask getSceneMembership(Instance ci) const noexcept;
protected:
NodeManager() noexcept = default;
~NodeManager() = default;
public:
NodeManager(NodeManager const&) = delete;
NodeManager(NodeManager&&) = delete;
NodeManager& operator=(NodeManager const&) = delete;
NodeManager& operator=(NodeManager&&) = delete;
};
} // namespace filament::gltfio
#endif // GLTFIO_NODEMANAGER_H

View File

@@ -0,0 +1,165 @@
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef GLTFIO_RESOURCELOADER_H
#define GLTFIO_RESOURCELOADER_H
#include <gltfio/FilamentAsset.h>
#include <filament/VertexBuffer.h>
#include <utils/compiler.h>
namespace filament {
class Engine;
}
namespace filament::gltfio {
struct FFilamentAsset;
class AssetPool;
class TextureProvider;
/**
* \struct ResourceConfiguration ResourceLoader.h gltfio/ResourceLoader.h
* \brief Construction parameters for ResourceLoader.
*/
struct ResourceConfiguration {
//! The engine that the loader should pass to builder objects (e.g.
//! filament::Texture::Builder).
class filament::Engine* engine;
//! Optional path or URI that points to the base glTF file. This is used solely
//! to resolve relative paths. The string pointer is not retained.
const char* gltfPath;
//! If true, adjusts skinning weights to sum to 1. Well formed glTF files do not need this,
//! but it is useful for robustness.
bool normalizeSkinningWeights;
};
/**
* \class ResourceLoader ResourceLoader.h gltfio/ResourceLoader.h
* \brief Prepares and uploads vertex buffers and textures to the GPU.
*
* For a usage example, see the documentation for AssetLoader.
*
* ResourceLoader must be destroyed on the same thread that calls filament::Renderer::render()
* because it listens to filament::backend::BufferDescriptor callbacks in order to determine when to
* free CPU-side data blobs.
*
* \todo If clients persist their ResourceLoader, Filament textures are currently re-created upon
* subsequent re-loads of the same asset. To fix this, we would need to enable shared ownership
* of Texture objects between ResourceLoader and FilamentAsset.
*/
class UTILS_PUBLIC ResourceLoader {
public:
using BufferDescriptor = filament::backend::BufferDescriptor;
ResourceLoader(const ResourceConfiguration& config);
~ResourceLoader();
/**
* Feeds the binary content of an external resource into the loader's URI cache.
*
* On some platforms, `ResourceLoader` does not know how to download external resources on its
* own (external resources might come from a filesystem, a database, or the internet) so this
* method allows clients to download external resources and push them to the loader.
*
* Every resource should be passed in before calling #loadResources or #asyncBeginLoad. See
* also FilamentAsset#getResourceUris.
*
* When loading GLB files (as opposed to JSON-based glTF files), clients typically do not
* need to call this method.
*/
void addResourceData(const char* uri, BufferDescriptor&& buffer);
/**
* Register a plugin that can consume PNG / JPEG content and produce filament::Texture objects.
*
* Destruction of the given provider is the client's responsibility.
*/
void addTextureProvider(const char* mimeType, TextureProvider* provider);
/**
* Checks if the given resource has already been added to the URI cache.
*/
bool hasResourceData(const char* uri) const;
/**
* Frees memory by evicting the URI cache that was populated via addResourceData.
*
* This can be called only after a model is fully loaded or after loading has been cancelled.
*/
void evictResourceData();
/**
* Loads resources for the given asset from the filesystem or data cache and "finalizes" the
* asset by transforming the vertex data format if necessary, decoding image files, supplying
* tangent data, etc.
*
* Returns false if resources have already been loaded, or if one or more resources could not
* be loaded.
*
* Note: this method is synchronous and blocks until all textures have been decoded.
* For an asynchronous alternative, see #asyncBeginLoad.
*/
bool loadResources(FilamentAsset* asset);
/**
* Starts an asynchronous resource load.
*
* Returns false if the loading process was unable to start.
*
* This is an alternative to #loadResources and requires periodic calls to #asyncUpdateLoad.
* On multi-threaded systems this creates threads for texture decoding.
*/
bool asyncBeginLoad(FilamentAsset* asset);
/**
* Gets the status of an asynchronous resource load as a percentage in [0,1].
*/
float asyncGetLoadProgress() const;
/**
* Updates an asynchronous load by performing any pending work that must take place
* on the main thread.
*
* Clients must periodically call this until #asyncGetLoadProgress returns 100%.
* After progress reaches 100%, calling this is harmless; it just does nothing.
*/
void asyncUpdateLoad();
/**
* Cancels pending decoder jobs, frees all CPU-side texel data, and flushes the Engine.
*
* Calling this is only necessary if the asyncBeginLoad API was used
* and cancellation is required before progress reaches 100%.
*/
void asyncCancelLoad();
private:
bool loadResources(FFilamentAsset* asset, bool async);
void normalizeSkinningWeights(FFilamentAsset* asset) const;
AssetPool* mPool;
struct Impl;
Impl* pImpl;
};
} // namespace filament::gltfio
#endif // GLTFIO_RESOURCELOADER_H

View File

@@ -0,0 +1,187 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef GLTFIO_TEXTUREPROVIDER_H
#define GLTFIO_TEXTUREPROVIDER_H
#include <stddef.h>
#include <stdint.h>
#include <utils/compiler.h>
#include <utils/BitmaskEnum.h>
namespace filament {
class Engine;
class Texture;
}
namespace filament::gltfio {
/**
* TextureProvider is an interface that allows clients to implement their own texture decoding
* facility for JPEG, PNG, or KTX2 content. It constructs Filament Texture objects synchronously,
* but populates their miplevels asynchronously.
*
* gltfio calls all public methods from the foreground thread, i.e. the thread that the Filament
* engine was created with. However the implementation may create 0 or more background threads to
* perform decoding work.
*
* The following pseudocode illustrates how this interface could be used, but in practice the only
* client is the gltfio ResourceLoader.
*
* filament::Engine* engine = ...;
* TextureProvider* provider = createStbProvider(engine);
*
* for (auto filename : textureFiles) {
* std::vector<uint8_t> buf = readEntireFile(filename);
* Texture* texture = provider->pushTexture(buf.data(), buf.size(), "image/png", 0);
* if (texture == nullptr) { puts(provider->getPushMessage()); exit(1); }
* }
*
* // At this point, the returned textures can be bound to material instances, but none of their
* // miplevel images have been populated yet.
*
* while (provider->getPoppedCount() < provider->getPushedCount()) {
* sleep(200);
*
* // The following call gives the provider an opportunity to reap the results of any
* // background decoder work that has been completed (e.g. by calling Texture::setImage).
* provider->updateQueue();
*
* // Check for textures that now have all their miplevels initialized.
* while (Texture* texture = provider->popTexture()) {
* printf("%p has all its miplevels ready.\n", texture);
* }
* }
*
* delete provider;
*/
class UTILS_PUBLIC TextureProvider {
public:
using Texture = filament::Texture;
enum class TextureFlags : uint64_t {
NONE = 0,
sRGB = 1 << 0,
};
/**
* Creates a Filament texture and pushes it to the asynchronous decoding queue.
*
* The provider synchronously determines the texture dimensions in order to create a Filament
* texture object, then populates the miplevels asynchronously.
*
* If construction fails, nothing is pushed to the queue and null is returned. The failure
* reason can be obtained with getPushMessage(). The given buffer pointer is not held, so the
* caller can free it immediately. It is also the caller's responsibility to free the returned
* Texture object, but it is only safe to do so after it has been popped from the queue.
*/
virtual Texture* pushTexture(const uint8_t* data, size_t byteCount,
const char* mimeType, TextureFlags flags) = 0;
/**
* Checks if any texture is ready to be removed from the asynchronous decoding queue, and if so
* pops it off.
*
* Unless an error or cancellation occurred during the decoding process, the returned texture
* should have all its miplevels populated. If the texture is not complete, the reason can be
* obtained with getPopMessage().
*
* Due to concurrency, textures are not necessarily popped off in the same order they were
* pushed. Returns null if there are no textures that are ready to be popped.
*/
virtual Texture* popTexture() = 0;
/**
* Polls textures in the queue and uploads mipmap images if any have emerged from the decoder.
*
* This gives the provider an opportunity to call Texture::setImage() on the foreground thread.
* If needed, it can also call Texture::generateMipmaps() here.
*
* Items in the decoding queue can become "poppable" only during this call.
*/
virtual void updateQueue() = 0;
/**
* Returns a failure message for the most recent call to pushTexture(), or null for success.
*
* Note that this method does not pertain to the decoding process. If decoding fails, clients to
* can pop the incomplete texture off the queue and obtain a failure message using the
* getPopFailure() method.
*
* The returned string is owned by the provider and becomes invalid after the next call to
* pushTexture().
*/
virtual const char* getPushMessage() const = 0;
/**
* Returns a failure message for the most recent call to popTexture(), or null for success.
*
* If the most recent call to popTexture() returned null, then no error occurred and this
* returns null. If the most recent call to popTexture() returned a "complete" texture (i.e.
* all miplevels present), then this returns null. This returns non-null only if an error or
* cancellation occurred while decoding the popped texture.
*
* The returned string is owned by the provider and becomes invalid after the next call to
* popTexture().
*/
virtual const char* getPopMessage() const = 0;
/**
* Waits for all outstanding decoding jobs to complete.
*
* Clients should call updateQueue() afterwards if they wish to update the push / pop queue.
*/
virtual void waitForCompletion() = 0;
/**
* Cancels all not-yet-started decoding jobs and waits for all other jobs to complete.
*
* Jobs that have already started cannot be canceled. Textures whose decoding process has
* been cancelled will be made poppable on the subsequent call to updateQueue().
*/
virtual void cancelDecoding() = 0;
/** Total number of successful push calls since the provider was created. */
virtual size_t getPushedCount() const = 0;
/** Total number of successful pop calls since the provider was created. */
virtual size_t getPoppedCount() const = 0;
/** Total number of textures that have become ready-to-pop since the provider was created. */
virtual size_t getDecodedCount() const = 0;
virtual ~TextureProvider() = default;
};
/**
* Creates a simple decoder based on stb_image that can handle "image/png" and "image/jpeg".
* This works only if your build configuration includes STB.
*/
TextureProvider* createStbProvider(filament::Engine* engine);
/**
* Creates a decoder that can handle certain types of "image/ktx2" content as specified in
* the KHR_texture_basisu specification.
*/
TextureProvider* createKtx2Provider(filament::Engine* engine);
} // namespace filament::gltfio
template<> struct utils::EnableBitMaskOperators<filament::gltfio::TextureProvider::TextureFlags>
: public std::true_type {};
#endif // GLTFIO_TEXTUREPROVIDER_H

View File

@@ -0,0 +1,13 @@
#ifndef UBERARCHIVE_H_
#define UBERARCHIVE_H_
#include <stdint.h>
extern "C" {
extern const uint8_t UBERARCHIVE_PACKAGE[];
extern int UBERARCHIVE_DEFAULT_OFFSET;
extern int UBERARCHIVE_DEFAULT_SIZE;
}
#define UBERARCHIVE_DEFAULT_DATA (UBERARCHIVE_PACKAGE + UBERARCHIVE_DEFAULT_OFFSET)
#endif

View File

@@ -0,0 +1,128 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef GLTFIO_MATH_H
#define GLTFIO_MATH_H
#include <math/quat.h>
#include <math/vec3.h>
#include <math/mat3.h>
#include <math/mat4.h>
#include <math/TVecHelpers.h>
#include <utils/compiler.h>
namespace filament::gltfio {
template <typename T>
UTILS_PUBLIC T cubicSpline(const T& vert0, const T& tang0, const T& vert1, const T& tang1, float t) {
float tt = t * t, ttt = tt * t;
float s2 = -2 * ttt + 3 * tt, s3 = ttt - tt;
float s0 = 1 - s2, s1 = s3 - tt + t;
T p0 = vert0;
T m0 = tang0;
T p1 = vert1;
T m1 = tang1;
return s0 * p0 + s1 * m0 * t + s2 * p1 + s3 * m1 * t;
}
UTILS_PUBLIC inline void decomposeMatrix(const filament::math::mat4f& mat, filament::math::float3* translation,
filament::math::quatf* rotation, filament::math::float3* scale) {
using namespace filament::math;
// Extract translation.
*translation = mat[3].xyz;
// Extract upper-left for determinant computation.
const float a = mat[0][0];
const float b = mat[0][1];
const float c = mat[0][2];
const float d = mat[1][0];
const float e = mat[1][1];
const float f = mat[1][2];
const float g = mat[2][0];
const float h = mat[2][1];
const float i = mat[2][2];
const float A = e * i - f * h;
const float B = f * g - d * i;
const float C = d * h - e * g;
// Extract scale.
const float det(a * A + b * B + c * C);
float scalex = length(float3({a, b, c}));
float scaley = length(float3({d, e, f}));
float scalez = length(float3({g, h, i}));
float3 s = { scalex, scaley, scalez };
if (det < 0) {
s = -s;
}
*scale = s;
// Remove scale from the matrix if it is not close to zero.
mat4f clone = mat;
if (std::abs(det) > std::numeric_limits<float>::epsilon()) {
clone[0] /= s.x;
clone[1] /= s.y;
clone[2] /= s.z;
// Extract rotation
*rotation = clone.toQuaternion();
} else {
// Set to identity if close to zero
*rotation = quatf(1.0f);
}
}
UTILS_PUBLIC inline filament::math::mat4f composeMatrix(const filament::math::float3& translation,
const filament::math::quatf& rotation, const filament::math::float3& scale) {
float tx = translation[0];
float ty = translation[1];
float tz = translation[2];
float qx = rotation[0];
float qy = rotation[1];
float qz = rotation[2];
float qw = rotation[3];
float sx = scale[0];
float sy = scale[1];
float sz = scale[2];
return filament::math::mat4f(
(1 - 2 * qy*qy - 2 * qz*qz) * sx,
(2 * qx*qy + 2 * qz*qw) * sx,
(2 * qx*qz - 2 * qy*qw) * sx,
0.f,
(2 * qx*qy - 2 * qz*qw) * sy,
(1 - 2 * qx*qx - 2 * qz*qz) * sy,
(2 * qy*qz + 2 * qx*qw) * sy,
0.f,
(2 * qx*qz + 2 * qy*qw) * sz,
(2 * qy*qz - 2 * qx*qw) * sz,
(1 - 2 * qx*qx - 2 * qy*qy) * sz,
0.f, tx, ty, tz, 1.f);
}
inline filament::math::mat3f matrixFromUvTransform(const float offset[2], float rotation,
const float scale[2]) {
float tx = offset[0];
float ty = offset[1];
float sx = scale[0];
float sy = scale[1];
float c = cos(rotation);
float s = sin(rotation);
return filament::math::mat3f(sx * c, sx * s, tx, -sy * s, sy * c, ty, 0.0f, 0.0f, 1.0f);
};
} // namespace filament::gltfio
#endif // GLTFIO_MATH_H