/src/assimp/code/Common/SkeletonMeshBuilder.cpp
Line | Count | Source |
1 | | /* |
2 | | Open Asset Import Library (assimp) |
3 | | ---------------------------------------------------------------------- |
4 | | |
5 | | Copyright (c) 2006-2026, assimp team |
6 | | |
7 | | All rights reserved. |
8 | | |
9 | | Redistribution and use of this software in source and binary forms, |
10 | | with or without modification, are permitted provided that the |
11 | | following conditions are met: |
12 | | |
13 | | * Redistributions of source code must retain the above |
14 | | copyright notice, this list of conditions and the |
15 | | following disclaimer. |
16 | | |
17 | | * Redistributions in binary form must reproduce the above |
18 | | copyright notice, this list of conditions and the |
19 | | following disclaimer in the documentation and/or other |
20 | | materials provided with the distribution. |
21 | | |
22 | | * Neither the name of the assimp team, nor the names of its |
23 | | contributors may be used to endorse or promote products |
24 | | derived from this software without specific prior |
25 | | written permission of the assimp team. |
26 | | |
27 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
28 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
29 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
30 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
31 | | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
32 | | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
33 | | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
34 | | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
35 | | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
36 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
37 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
38 | | |
39 | | ---------------------------------------------------------------------- |
40 | | */ |
41 | | |
42 | | /** @file SkeletonMeshBuilder.cpp |
43 | | * @brief Implementation of a little class to construct a dummy mesh for a skeleton |
44 | | */ |
45 | | |
46 | | #include <assimp/SkeletonMeshBuilder.h> |
47 | | #include <assimp/scene.h> |
48 | | |
49 | | using namespace Assimp; |
50 | | |
51 | | // ------------------------------------------------------------------------------------------------ |
52 | | // The constructor processes the given scene and adds a mesh there. |
53 | 0 | SkeletonMeshBuilder::SkeletonMeshBuilder(aiScene *pScene, aiNode *root, bool bKnobsOnly) { |
54 | | // nothing to do if there's mesh data already present at the scene |
55 | 0 | if (pScene->mNumMeshes > 0 || pScene->mRootNode == nullptr) { |
56 | 0 | return; |
57 | 0 | } |
58 | | |
59 | 0 | if (!root) { |
60 | 0 | root = pScene->mRootNode; |
61 | 0 | } |
62 | |
|
63 | 0 | mKnobsOnly = bKnobsOnly; |
64 | | |
65 | | // build some faces around each node |
66 | 0 | CreateGeometry(root); |
67 | | |
68 | | // create a mesh to hold all the generated faces |
69 | 0 | pScene->mNumMeshes = 1; |
70 | 0 | pScene->mMeshes = new aiMesh *[1]; |
71 | 0 | pScene->mMeshes[0] = CreateMesh(); |
72 | | // and install it at the root node |
73 | 0 | root->mNumMeshes = 1; |
74 | 0 | root->mMeshes = new unsigned int[1]; |
75 | 0 | root->mMeshes[0] = 0; |
76 | | |
77 | | // create a dummy material for the mesh |
78 | 0 | if (pScene->mNumMaterials == 0) { |
79 | 0 | pScene->mNumMaterials = 1; |
80 | 0 | pScene->mMaterials = new aiMaterial *[1]; |
81 | 0 | pScene->mMaterials[0] = CreateMaterial(); |
82 | 0 | } |
83 | 0 | } |
84 | | |
85 | | // ------------------------------------------------------------------------------------------------ |
86 | | // Recursively builds a simple mesh representation for the given node |
87 | 0 | void SkeletonMeshBuilder::CreateGeometry(const aiNode *pNode) { |
88 | | // add a joint entry for the node. |
89 | 0 | const unsigned int vertexStartIndex = static_cast<unsigned int>(mVertices.size()); |
90 | | |
91 | | // now build the geometry. |
92 | 0 | if (pNode->mNumChildren > 0 && !mKnobsOnly) { |
93 | | // If the node has children, we build little pointers to each of them |
94 | 0 | for (unsigned int a = 0; a < pNode->mNumChildren; a++) { |
95 | | // find a suitable coordinate system |
96 | 0 | const aiMatrix4x4 &childTransform = pNode->mChildren[a]->mTransformation; |
97 | 0 | aiVector3D childpos(childTransform.a4, childTransform.b4, childTransform.c4); |
98 | 0 | ai_real distanceToChild = childpos.Length(); |
99 | 0 | if (distanceToChild < ai_epsilon) { |
100 | 0 | continue; |
101 | 0 | } |
102 | 0 | aiVector3D up = aiVector3D(childpos).Normalize(); |
103 | 0 | aiVector3D orth(1.0, 0.0, 0.0); |
104 | 0 | if (std::fabs(orth * up) > 0.99) { |
105 | 0 | orth.Set(0.0, 1.0, 0.0); |
106 | 0 | } |
107 | |
|
108 | 0 | aiVector3D front = (up ^ orth).Normalize(); |
109 | 0 | aiVector3D side = (front ^ up).Normalize(); |
110 | |
|
111 | 0 | unsigned int localVertexStart = static_cast<unsigned int>(mVertices.size()); |
112 | 0 | mVertices.push_back(-front * distanceToChild * (ai_real)0.1); |
113 | 0 | mVertices.push_back(childpos); |
114 | 0 | mVertices.push_back(-side * distanceToChild * (ai_real)0.1); |
115 | 0 | mVertices.push_back(-side * distanceToChild * (ai_real)0.1); |
116 | 0 | mVertices.push_back(childpos); |
117 | 0 | mVertices.push_back(front * distanceToChild * (ai_real)0.1); |
118 | 0 | mVertices.push_back(front * distanceToChild * (ai_real)0.1); |
119 | 0 | mVertices.push_back(childpos); |
120 | 0 | mVertices.push_back(side * distanceToChild * (ai_real)0.1); |
121 | 0 | mVertices.push_back(side * distanceToChild * (ai_real)0.1); |
122 | 0 | mVertices.push_back(childpos); |
123 | 0 | mVertices.push_back(-front * distanceToChild * (ai_real)0.1); |
124 | |
|
125 | 0 | mFaces.emplace_back(localVertexStart + 0, localVertexStart + 1, localVertexStart + 2); |
126 | 0 | mFaces.emplace_back(localVertexStart + 3, localVertexStart + 4, localVertexStart + 5); |
127 | 0 | mFaces.emplace_back(localVertexStart + 6, localVertexStart + 7, localVertexStart + 8); |
128 | 0 | mFaces.emplace_back(localVertexStart + 9, localVertexStart + 10, localVertexStart + 11); |
129 | 0 | } |
130 | 0 | } else { |
131 | | // if the node has no children, it's an end node. Put a little knob there instead |
132 | 0 | aiVector3D ownpos(pNode->mTransformation.a4, pNode->mTransformation.b4, pNode->mTransformation.c4); |
133 | 0 | ai_real sizeEstimate = ownpos.Length() * ai_real(0.18); |
134 | 0 | const ai_real zero(0.0); |
135 | |
|
136 | 0 | mVertices.emplace_back(-sizeEstimate, zero, zero); |
137 | 0 | mVertices.emplace_back(zero, sizeEstimate, zero); |
138 | 0 | mVertices.emplace_back(zero, zero, -sizeEstimate); |
139 | 0 | mVertices.emplace_back(zero, sizeEstimate, zero); |
140 | 0 | mVertices.emplace_back(sizeEstimate, zero, zero); |
141 | 0 | mVertices.emplace_back(zero, zero, -sizeEstimate); |
142 | 0 | mVertices.emplace_back(sizeEstimate, zero, zero); |
143 | 0 | mVertices.emplace_back(zero, -sizeEstimate, zero); |
144 | 0 | mVertices.emplace_back(zero, zero, -sizeEstimate); |
145 | 0 | mVertices.emplace_back(zero, -sizeEstimate, zero); |
146 | 0 | mVertices.emplace_back(-sizeEstimate, zero, zero); |
147 | 0 | mVertices.emplace_back(zero, zero, -sizeEstimate); |
148 | |
|
149 | 0 | mVertices.emplace_back(-sizeEstimate, zero, zero); |
150 | 0 | mVertices.emplace_back(zero, zero, sizeEstimate); |
151 | 0 | mVertices.emplace_back(zero, sizeEstimate, zero); |
152 | 0 | mVertices.emplace_back(zero, sizeEstimate, zero); |
153 | 0 | mVertices.emplace_back(zero, zero, sizeEstimate); |
154 | 0 | mVertices.emplace_back(sizeEstimate, zero, zero); |
155 | 0 | mVertices.emplace_back(sizeEstimate, zero, zero); |
156 | 0 | mVertices.emplace_back(zero, zero, sizeEstimate); |
157 | 0 | mVertices.emplace_back(zero, -sizeEstimate, zero); |
158 | 0 | mVertices.emplace_back(zero, -sizeEstimate, zero); |
159 | 0 | mVertices.emplace_back(zero, zero, sizeEstimate); |
160 | 0 | mVertices.emplace_back(-sizeEstimate, zero, zero); |
161 | |
|
162 | 0 | mFaces.emplace_back(vertexStartIndex + 0, vertexStartIndex + 1, vertexStartIndex + 2); |
163 | 0 | mFaces.emplace_back(vertexStartIndex + 3, vertexStartIndex + 4, vertexStartIndex + 5); |
164 | 0 | mFaces.emplace_back(vertexStartIndex + 6, vertexStartIndex + 7, vertexStartIndex + 8); |
165 | 0 | mFaces.emplace_back(vertexStartIndex + 9, vertexStartIndex + 10, vertexStartIndex + 11); |
166 | 0 | mFaces.emplace_back(vertexStartIndex + 12, vertexStartIndex + 13, vertexStartIndex + 14); |
167 | 0 | mFaces.emplace_back(vertexStartIndex + 15, vertexStartIndex + 16, vertexStartIndex + 17); |
168 | 0 | mFaces.emplace_back(vertexStartIndex + 18, vertexStartIndex + 19, vertexStartIndex + 20); |
169 | 0 | mFaces.emplace_back(vertexStartIndex + 21, vertexStartIndex + 22, vertexStartIndex + 23); |
170 | 0 | } |
171 | |
|
172 | 0 | unsigned int numVertices = static_cast<unsigned int>(mVertices.size() - vertexStartIndex); |
173 | 0 | if (numVertices > 0) { |
174 | | // create a bone affecting all the newly created vertices |
175 | 0 | aiBone *bone = new aiBone; |
176 | 0 | mBones.push_back(bone); |
177 | 0 | bone->mName = pNode->mName; |
178 | | |
179 | | // calculate the bone offset matrix by concatenating the inverse transformations of all parents |
180 | 0 | bone->mOffsetMatrix = aiMatrix4x4(pNode->mTransformation).Inverse(); |
181 | 0 | for (aiNode *parent = pNode->mParent; parent != nullptr; parent = parent->mParent) |
182 | 0 | bone->mOffsetMatrix = aiMatrix4x4(parent->mTransformation).Inverse() * bone->mOffsetMatrix; |
183 | | |
184 | | // add all the vertices to the bone's influences |
185 | 0 | bone->mNumWeights = numVertices; |
186 | 0 | bone->mWeights = new aiVertexWeight[numVertices]; |
187 | 0 | for (unsigned int a = 0; a < numVertices; ++a) { |
188 | 0 | bone->mWeights[a] = aiVertexWeight(vertexStartIndex + a, 1.0); |
189 | 0 | } |
190 | | |
191 | | // HACK: (thom) transform all vertices to the bone's local space. Should be done before adding |
192 | | // them to the array, but I'm tired now and I'm annoyed. |
193 | 0 | aiMatrix4x4 boneToMeshTransform = aiMatrix4x4(bone->mOffsetMatrix).Inverse(); |
194 | 0 | for (unsigned int a = vertexStartIndex; a < mVertices.size(); a++) |
195 | 0 | mVertices[a] = boneToMeshTransform * mVertices[a]; |
196 | 0 | } |
197 | | |
198 | | // and finally recurse into the children list |
199 | 0 | for (unsigned int a = 0; a < pNode->mNumChildren; ++a) { |
200 | 0 | CreateGeometry(pNode->mChildren[a]); |
201 | 0 | } |
202 | 0 | } |
203 | | |
204 | | // ------------------------------------------------------------------------------------------------ |
205 | | // Creates the mesh from the internally accumulated stuff and returns it. |
206 | 0 | aiMesh *SkeletonMeshBuilder::CreateMesh() { |
207 | 0 | aiMesh *mesh = new aiMesh(); |
208 | | |
209 | | // add points |
210 | 0 | mesh->mNumVertices = static_cast<unsigned int>(mVertices.size()); |
211 | 0 | mesh->mVertices = new aiVector3D[mesh->mNumVertices]; |
212 | 0 | std::copy(mVertices.begin(), mVertices.end(), mesh->mVertices); |
213 | |
|
214 | 0 | mesh->mNormals = new aiVector3D[mesh->mNumVertices]; |
215 | | |
216 | | // add faces |
217 | 0 | mesh->mNumFaces = static_cast<unsigned int>(mFaces.size()); |
218 | 0 | mesh->mFaces = new aiFace[mesh->mNumFaces]; |
219 | 0 | for (unsigned int a = 0; a < mesh->mNumFaces; a++) { |
220 | 0 | const Face &inface = mFaces[a]; |
221 | 0 | aiFace &outface = mesh->mFaces[a]; |
222 | 0 | outface.mNumIndices = 3; |
223 | 0 | outface.mIndices = new unsigned int[3]; |
224 | 0 | outface.mIndices[0] = inface.mIndices[0]; |
225 | 0 | outface.mIndices[1] = inface.mIndices[1]; |
226 | 0 | outface.mIndices[2] = inface.mIndices[2]; |
227 | | |
228 | | // Compute per-face normals ... we don't want the bones to be smoothed ... they're built to visualize |
229 | | // the skeleton, so it's good if there's a visual difference to the rest of the geometry |
230 | 0 | aiVector3D nor = ((mVertices[inface.mIndices[2]] - mVertices[inface.mIndices[0]]) ^ |
231 | 0 | (mVertices[inface.mIndices[1]] - mVertices[inface.mIndices[0]])); |
232 | |
|
233 | 0 | if (nor.Length() < 1e-5) /* ensure that FindInvalidData won't remove us ...*/ |
234 | 0 | nor = aiVector3D(1.0, 0.0, 0.0); |
235 | |
|
236 | 0 | for (unsigned int n = 0; n < 3; ++n) |
237 | 0 | mesh->mNormals[inface.mIndices[n]] = nor; |
238 | 0 | } |
239 | | |
240 | | // add the bones |
241 | 0 | mesh->mNumBones = static_cast<unsigned int>(mBones.size()); |
242 | 0 | mesh->mBones = new aiBone *[mesh->mNumBones]; |
243 | 0 | std::copy(mBones.begin(), mBones.end(), mesh->mBones); |
244 | | |
245 | | // default |
246 | 0 | mesh->mMaterialIndex = 0; |
247 | |
|
248 | 0 | return mesh; |
249 | 0 | } |
250 | | |
251 | | // ------------------------------------------------------------------------------------------------ |
252 | | // Creates a dummy material and returns it. |
253 | 0 | aiMaterial *SkeletonMeshBuilder::CreateMaterial() { |
254 | 0 | aiMaterial *matHelper = new aiMaterial; |
255 | | |
256 | | // Name |
257 | 0 | aiString matName(std::string("SkeletonMaterial")); |
258 | 0 | matHelper->AddProperty(&matName, AI_MATKEY_NAME); |
259 | | |
260 | | // Prevent backface culling |
261 | 0 | const int no_cull = 1; |
262 | 0 | matHelper->AddProperty(&no_cull, 1, AI_MATKEY_TWOSIDED); |
263 | |
|
264 | 0 | return matHelper; |
265 | 0 | } |