| 1 |
/* |
| 2 |
* Copyright (C) 2021 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "FFilamentAsset.h" |
| 18 |
#include "FFilamentInstance.h" |
| 19 |
|
| 20 |
#include <math/vec4.h> |
| 21 |
|
| 22 |
#include <tsl/robin_map.h> |
| 23 |
|
| 24 |
#include <vector> |
| 25 |
|
| 26 |
struct cgltf_node; |
| 27 |
struct cgltf_mesh; |
| 28 |
struct cgltf_primitive; |
| 29 |
|
| 30 |
namespace gltfio { |
| 31 |
|
| 32 |
/** |
| 33 |
* Internal class that partitions lists of morph weights and maintains a cache of BufferObject |
| 34 |
* instances. This allows gltfio to support up to 255 morph targets. |
| 35 |
* |
| 36 |
* Each partition is associated with an unordered set of 4 (or fewer) morph target indices, which |
| 37 |
* we call the "primary indices" for that time slice. |
| 38 |
* |
| 39 |
* Animator has ownership over a single instance of MorphHelper, thus it is 1:1 with FilamentAsset. |
| 40 |
*/ |
| 41 |
class MorphHelper { |
| 42 |
public: |
| 43 |
using Entity = utils::Entity; |
| 44 |
MorphHelper(FFilamentAsset* asset, FFilamentInstance* inst); |
| 45 |
~MorphHelper(); |
| 46 |
|
| 47 |
/** |
| 48 |
* Picks the 4 most influential weights and applies them to the target entity. |
| 49 |
*/ |
| 50 |
void applyWeights(Entity targetEntity, float const* weights, size_t count) noexcept; |
| 51 |
|
| 52 |
private: |
| 53 |
struct GltfTarget { |
| 54 |
filament::BufferObject* bufferObject; |
| 55 |
int morphTargetIndex; |
| 56 |
cgltf_attribute_type type; |
| 57 |
}; |
| 58 |
|
| 59 |
struct GltfPrimitive { |
| 60 |
filament::VertexBuffer* vertexBuffer; |
| 61 |
int baseSlot; |
| 62 |
std::vector<GltfTarget> targets; // TODO: flatten this? |
| 63 |
}; |
| 64 |
|
| 65 |
struct TableEntry { |
| 66 |
std::vector<GltfPrimitive> primitives; // TODO: flatten this? |
| 67 |
}; |
| 68 |
|
| 69 |
void addPrimitive(cgltf_mesh const* mesh, int primitiveIndex, TableEntry* entry); |
| 70 |
int determineBaseSlot(const cgltf_primitive& prim) const; |
| 71 |
|
| 72 |
std::vector<float> mPartiallySortedWeights; |
| 73 |
tsl::robin_map<Entity, TableEntry> mMorphTable; |
| 74 |
const FFilamentAsset* mAsset; |
| 75 |
const FFilamentInstance* mInstance; |
| 76 |
}; |
| 77 |
|
| 78 |
} // namespace gltfio |