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-scene3d/public-api/api.h"
22 #include "dali-scene3d/public-api/loader/blend-shape-details.h"
23 #include "dali-scene3d/public-api/loader/index.h"
24 #include "dali-scene3d/public-api/loader/mesh-geometry.h"
25 #include "dali-scene3d/public-api/loader/utils.h"
29 #include "dali/public-api/common/vector-wrapper.h"
38 * @brief Defines a mesh with its attributes, the primitive type to render it as,
39 * and the file to load it from with the offset and length information for the
40 * individual attribute buffers.
42 struct DALI_SCENE3D_API MeshDefinition
44 using Vector = std::vector<std::pair<MeshDefinition, MeshGeometry>>;
48 INVALID = std::numeric_limits<uint32_t>::max()
53 FLIP_UVS_VERTICAL = NthBit(0),
54 U32_INDICES = NthBit(1), // default is unsigned short
55 U16_JOINT_IDS = NthBit(2), // default is floats
56 U8_JOINT_IDS = NthBit(3),
62 POSITIONS = NthBit(1),
64 TEX_COORDS = NthBit(3),
66 LEGACY_BITANGENTS = NthBit(5), // these are ignored; we're calculating them in the (PBR) shader.
68 WEIGHTS_0 = NthBit(7),
72 * @brief Describes raw data in terms of its position and size in a buffer.
77 uint32_t mOffset = INVALID; // the default means that the blob is undefined.
78 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.
79 uint16_t mStride = 0; // ignore if 0
80 uint16_t mElementSizeHint = 0; // ignore if 0 or stride == 0
81 std::vector<float> mMin;
82 std::vector<float> mMax;
84 static void ComputeMinMax(std::vector<float>& min, std::vector<float>& max, uint32_t numComponents, uint32_t count, const float* values);
86 static void ApplyMinMax(const std::vector<float>& min, const std::vector<float>& max, uint32_t count, float* values);
90 Blob(const Blob&) = default;
91 Blob& operator=(const Blob&) = default;
93 Blob(Blob&&) = default;
94 Blob& operator=(Blob&&) = default;
96 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 = {});
99 * @brief Calculates the size of a tightly-packed buffer for the elements from the blob.
101 uint32_t GetBufferSize() const;
104 * @brief Convenience method to tell whether a Blob has meaningful data.
106 bool IsDefined() const
108 return mOffset != INVALID;
112 * @brief Convenience method to tell whether the elements stored in the blob follow each
113 * other tightly. The opposite would be interleaving.
115 bool IsConsecutive() const
117 return mStride == 0 || mStride == mElementSizeHint;
121 * @brief Computes the min / max of the input value data.
122 * The min and max are stored in mMin and mMax.
124 * @param[in] numComponents number of components of data type. e.g., 3 for Vector3.
125 * @param[in] count The number of data.
126 * @param[in] values Data for the mesh.
128 void ComputeMinMax(uint32_t numComponents, uint32_t count, float* values);
131 * @brief Applies the min / max values, if they're defined in the model
133 * @param[in] count The number of data.
134 * @param[in] values Data for the mesh that min / max values will be applied.
136 void ApplyMinMax(uint32_t count, float* values) const;
140 * @brief A sparse blob describes a change in a reference Blob.
141 * @p indices describe what positions of the reference Blob change and
142 * @p values describe the new values.
146 SparseBlob() = default;
148 SparseBlob(const SparseBlob&) = default;
149 SparseBlob& operator=(const SparseBlob&) = default;
151 SparseBlob(SparseBlob&&) = default;
152 SparseBlob& operator=(SparseBlob&&) = default;
154 SparseBlob(const Blob& indices, const Blob& values, uint32_t count);
155 SparseBlob(Blob&& indices, Blob&& values, uint32_t count);
159 uint32_t mCount = 0u;
165 std::unique_ptr<SparseBlob> mSparse;
167 Accessor() = default;
169 Accessor(const Accessor&) = delete;
170 Accessor& operator=(const Accessor&) = delete;
172 Accessor(Accessor&&) = default;
173 Accessor& operator=(Accessor&&) = default;
175 Accessor(const MeshDefinition::Blob& blob,
176 const MeshDefinition::SparseBlob& sparse);
177 Accessor(MeshDefinition::Blob&& blob,
178 MeshDefinition::SparseBlob&& sparse);
180 bool IsDefined() const
182 return mBlob.IsDefined() || (mSparse && (mSparse->mIndices.IsDefined() && mSparse->mValues.IsDefined()));
187 * @brief Stores a blend shape.
203 Property::Type mType;
204 uint32_t mNumElements;
205 std::vector<uint8_t> mData;
207 void AttachBuffer(Geometry& g) const;
210 std::vector<uint16_t> mIndices;
211 std::vector<Attrib> mAttribs;
213 unsigned int mBlendShapeBufferOffset{0};
214 Dali::Vector<float> mBlendShapeUnnormalizeFactor;
215 PixelData mBlendShapeData;
218 MeshDefinition() = default;
220 MeshDefinition(const MeshDefinition&) = delete;
221 MeshDefinition& operator=(const MeshDefinition&) = delete;
223 MeshDefinition(MeshDefinition&&) = default;
224 MeshDefinition& operator=(MeshDefinition&&) = default;
227 * @brief Determines whether the mesh definition is that of a quad.
232 * @brief Determines whether the mesh is used for skeletal animation.
234 bool IsSkinned() const;
237 * @brief Whether the mesh has blend shapes.
239 bool HasBlendShapes() const;
242 * @brief Requests normals to be generated.
243 * @note Generation happens in LoadRaw().
244 * @note Must have Vector3 positions defined.
246 void RequestNormals();
249 * @brief Requests tangents to be generated.
250 * @note Generation happens in LoadRaw().
251 * @note Must have Vector3 normals defined.
253 void RequestTangents();
256 * @brief Loads raw geometry data, which includes index (optional) and
257 * attribute buffers, as well as blend shape data. This is then returned.
258 * @note This can be done on any thread.
260 RawData LoadRaw(const std::string& modelsPath);
263 * @brief Creates a MeshGeometry based firstly on the value of the uri member:
264 * if it is "quad", a textured quad is created; otherwise it uses the
265 * attribute (and index) buffers and blend shape information (if available)
267 * If mFlipVertical was set, the UVs are flipped in Y, i.e. v = 1.0 - v.
269 MeshGeometry Load(RawData&& raw) const;
272 std::shared_ptr<RawData> mRawData;
273 uint32_t mFlags = 0x0;
274 Geometry::Type mPrimitiveType = Geometry::TRIANGLES;
278 Accessor mNormals; // data can be generated based on positions
281 Accessor mTangents; // data can be generated based on normals and texCoords (the latter isn't mandatory; the results will be better if available)
284 Property::Type mTangentType{Property::VECTOR3};
286 Blob mBlendShapeHeader;
287 std::vector<BlendShape> mBlendShapes;
288 BlendShapes::Version mBlendShapeVersion = BlendShapes::Version::INVALID;
290 Index mSkeletonIdx = INVALID_INDEX;
293 } // namespace Loader
294 } // namespace Scene3D
297 #endif //DALI_SCENE3D_LOADER_MESH_DEFINITION_H