1 #ifndef DALI_SCENE3D_LOADER_MESH_DEFINITION_H
2 #define DALI_SCENE3D_LOADER_MESH_DEFINITION_H
4 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 #include <dali/public-api/common/vector-wrapper.h>
25 #include <dali-scene3d/public-api/api.h>
26 #include <dali-scene3d/public-api/loader/blend-shape-details.h>
27 #include <dali-scene3d/public-api/loader/buffer-definition.h>
28 #include <dali-scene3d/public-api/loader/index.h>
29 #include <dali-scene3d/public-api/loader/mesh-geometry.h>
30 #include <dali-scene3d/public-api/loader/utils.h>
32 namespace Dali::Scene3D::Loader
35 * @brief Defines a mesh with its attributes, the primitive type to render it as,
36 * and the file to load it from with the offset and length information for the
37 * individual attribute buffers.
39 struct DALI_SCENE3D_API MeshDefinition
41 using Vector = std::vector<std::pair<MeshDefinition, MeshGeometry>>;
45 INVALID = std::numeric_limits<uint32_t>::max()
50 MAX_NUMBER_OF_JOINT_SETS = 4
55 FLIP_UVS_VERTICAL = NthBit(0),
56 U32_INDICES = NthBit(1), // default is unsigned short
57 U8_INDICES = NthBit(2), // default is unsigned short
58 U16_JOINT_IDS = NthBit(3), // default is floats
59 U8_JOINT_IDS = NthBit(4),
60 U16_WEIGHT = NthBit(5), // default is floats
61 U8_WEIGHT = NthBit(6),
62 S8_POSITION = NthBit(7), // default is floats
63 U8_POSITION = NthBit(8), // default is floats
64 S16_POSITION = NthBit(9), // default is floats
65 U16_POSITION = NthBit(10), // default is floats
66 S8_NORMAL = NthBit(11), // default is floats
67 S16_NORMAL = NthBit(12), // default is floats
68 S8_TANGENT = NthBit(13), // default is floats
69 S16_TANGENT = NthBit(14), // default is floats
70 S8_TEXCOORD = NthBit(15), // default is floats
71 U8_TEXCOORD = NthBit(16), // default is floats
72 S16_TEXCOORD = NthBit(17), // default is floats
73 U16_TEXCOORD = NthBit(18), // default is floats
76 enum FlagMasks : uint32_t
78 POSITIONS_MASK = 0x780,
79 NORMALS_MASK = 0x1800,
80 TANGENTS_MASK = 0x6000,
81 TEXCOORDS_MASK = 0x78000,
87 POSITIONS = NthBit(1),
89 TEX_COORDS = NthBit(3),
91 LEGACY_BITANGENTS = NthBit(5), // these are ignored; we're calculating them in the (PBR) shader.
93 WEIGHTS_0 = NthBit(7),
97 * @brief Describes raw data in terms of its position and size in a buffer.
102 uint32_t mOffset = INVALID; // the default means that the blob is undefined.
103 uint32_t mLength = 0; // if the blob is undefined, its data may still be generated. This is enabled by setting length to some non-0 value. Refer to MeshDefinition for details.
104 uint16_t mStride = 0; // ignore if 0
105 uint16_t mElementSizeHint = 0; // ignore if 0 or stride == 0
106 std::vector<float> mMin;
107 std::vector<float> mMax;
109 static void ComputeMinMax(std::vector<float>& min, std::vector<float>& max, uint32_t numComponents, uint32_t count, const float* values);
111 static void ApplyMinMax(const std::vector<float>& min, const std::vector<float>& max, uint32_t count, float* values, std::vector<uint32_t>* sparseIndices = nullptr);
115 Blob(const Blob&) = default;
116 Blob& operator=(const Blob&) = default;
118 Blob(Blob&&) = default;
119 Blob& operator=(Blob&&) = default;
121 Blob(uint32_t offset, uint32_t length, uint16_t stride = 0, uint16_t elementSizeHint = 0, const std::vector<float>& min = {}, const std::vector<float>& max = {});
124 * @brief Calculates the size of a tightly-packed buffer for the elements from the blob.
126 uint32_t GetBufferSize() const;
129 * @brief Convenience method to tell whether a Blob has meaningful data.
131 bool IsDefined() const
133 return mOffset != INVALID;
137 * @brief Convenience method to tell whether the elements stored in the blob follow each
138 * other tightly. The opposite would be interleaving.
140 bool IsConsecutive() const
142 return mStride == 0 || mStride == mElementSizeHint;
146 * @brief Computes the min / max of the input value data.
147 * The min and max are stored in mMin and mMax.
149 * @param[in] numComponents number of components of data type. e.g., 3 for Vector3.
150 * @param[in] count The number of data.
151 * @param[in] values Data for the mesh.
153 void ComputeMinMax(uint32_t numComponents, uint32_t count, float* values);
156 * @brief Applies the min / max values, if they're defined in the model
158 * @param[in] count The number of data.
159 * @param[in] values Data for the mesh that min / max values will be applied.
160 * @param[in] sparseIndices Pointer to array of sparse indices (or nullptr if not provided)
163 void ApplyMinMax(uint32_t count, float* values, std::vector<uint32_t>* sparseIndices = nullptr) const;
167 * @brief A sparse blob describes a change in a reference Blob.
168 * @p indices describe what positions of the reference Blob change and
169 * @p values describe the new values.
173 SparseBlob() = default;
175 SparseBlob(const SparseBlob&) = default;
176 SparseBlob& operator=(const SparseBlob&) = default;
178 SparseBlob(SparseBlob&&) = default;
179 SparseBlob& operator=(SparseBlob&&) = default;
181 SparseBlob(const Blob& indices, const Blob& values, uint32_t count);
182 SparseBlob(Blob&& indices, Blob&& values, uint32_t count);
186 uint32_t mCount = 0u;
192 std::unique_ptr<SparseBlob> mSparse;
193 Index mBufferIdx = INVALID_INDEX;
194 bool mNormalized{false};
196 Accessor() = default;
198 Accessor(const Accessor&) = delete;
199 Accessor& operator=(const Accessor&) = delete;
201 Accessor(Accessor&&) = default;
202 Accessor& operator=(Accessor&&) = default;
204 Accessor(const MeshDefinition::Blob& blob,
205 const MeshDefinition::SparseBlob& sparse,
206 Index bufferIndex = INVALID_INDEX,
207 bool normalized = false);
209 Accessor(MeshDefinition::Blob&& blob,
210 MeshDefinition::SparseBlob&& sparse,
211 Index bufferIndex = INVALID_INDEX,
212 bool normalized = false);
214 bool IsDefined() const
216 return mBlob.IsDefined() || (mSparse && (mSparse->mIndices.IsDefined() && mSparse->mValues.IsDefined()));
221 * @brief Stores a blend shape.
230 uint32_t mFlags = 0x0;
238 Property::Type mType;
239 uint32_t mNumElements;
240 std::vector<uint8_t> mData;
242 void AttachBuffer(Geometry& g) const;
245 std::vector<uint16_t> mIndices;
246 std::vector<Attrib> mAttribs;
248 unsigned int mBlendShapeBufferOffset{0};
249 Dali::Vector<float> mBlendShapeUnnormalizeFactor;
250 PixelData mBlendShapeData;
253 MeshDefinition() = default;
255 MeshDefinition(const MeshDefinition&) = delete;
256 MeshDefinition& operator=(const MeshDefinition&) = delete;
258 MeshDefinition(MeshDefinition&&) = default;
259 MeshDefinition& operator=(MeshDefinition&&) = default;
262 * @brief Determines whether the mesh definition is that of a quad.
267 * @brief Determines whether the mesh is used for skeletal animation.
269 bool IsSkinned() const;
272 * @brief Determines if the mesh has any vertex colors
274 bool HasVertexColor() const;
277 * @brief Returns the number of joint sets defined by the mesh
279 * @note Clamped to 4 to minimise GPU attrs.
281 uint32_t GetNumberOfJointSets() const;
284 * @brief Whether the mesh has blend shapes.
286 bool HasBlendShapes() const;
289 * @brief Requests normals to be generated.
290 * @note Generation happens in LoadRaw().
291 * @note Must have Vector3 positions defined.
293 void RequestNormals();
296 * @brief Requests tangents to be generated.
297 * @note Generation happens in LoadRaw().
298 * @note Must have Vector3 normals defined.
300 void RequestTangents();
303 * @brief Loads raw geometry data, which includes index (optional) and
304 * attribute buffers, as well as blend shape data. This is then returned.
305 * @note This can be done on any thread.
307 RawData LoadRaw(const std::string& modelsPath, BufferDefinition::Vector& buffers);
310 * @brief Creates a MeshGeometry based firstly on the value of the uri member:
311 * if it is "quad", a textured quad is created; otherwise it uses the
312 * attribute (and index) buffers and blend shape information (if available)
314 * If mFlipVertical was set, the UVs are flipped in Y, i.e. v = 1.0 - v.
316 MeshGeometry Load(RawData&& raw) const;
319 * @brief Retrieves what Components information is in this mesh's BlendShape.
321 * @param[out] hasPositions True if the BlendShape has position components
322 * @param[out] hasNormals True if the BlendShape has normal components
323 * @param[out] hasTangents True if the BlendShape has tangent components
325 void RetrieveBlendShapeComponents(bool& hasPositions, bool& hasNormals, bool& hasTangents) const;
328 std::shared_ptr<RawData> mRawData;
329 uint32_t mFlags = 0x0;
330 Geometry::Type mPrimitiveType = Geometry::TRIANGLES;
331 std::string mUri; // When the mesh data is loaded from embedded resources, this URI is used as a data stream.
334 Accessor mNormals; // data can be generated based on positions
335 Accessor mTangents; // data can be generated based on normals and texCoords (the latter isn't mandatory; the results will be better if available)
336 std::vector<Accessor> mTexCoords;
337 std::vector<Accessor> mColors;
338 std::vector<Accessor> mJoints;
339 std::vector<Accessor> mWeights;
340 Property::Type mTangentType{Property::VECTOR3};
342 Blob mBlendShapeHeader;
343 std::vector<BlendShape> mBlendShapes;
344 BlendShapes::Version mBlendShapeVersion = BlendShapes::Version::INVALID;
346 Index mSkeletonIdx = INVALID_INDEX;
347 ModelPrimitive mModelPrimitive;
350 } // namespace Dali::Scene3D::Loader
352 #endif //DALI_SCENE3D_LOADER_MESH_DEFINITION_H