USDLoaderImplTinyusdz.cpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file USDLoader.cpp
  34. * @brief Implementation of the USD importer class
  35. */
  36. #ifndef ASSIMP_BUILD_NO_USD_IMPORTER
  37. #include <memory>
  38. #include <sstream>
  39. // internal headers
  40. #include <assimp/ai_assert.h>
  41. #include <assimp/anim.h>
  42. #include <assimp/CreateAnimMesh.h>
  43. #include <assimp/DefaultIOSystem.h>
  44. #include <assimp/DefaultLogger.hpp>
  45. #include <assimp/fast_atof.h>
  46. #include <assimp/Importer.hpp>
  47. #include <assimp/importerdesc.h>
  48. #include <assimp/IOStreamBuffer.h>
  49. #include <assimp/IOSystem.hpp>
  50. #include "assimp/MemoryIOWrapper.h"
  51. #include <assimp/StringUtils.h>
  52. #include <assimp/StreamReader.h>
  53. #include "io-util.hh" // namespace tinyusdz::io
  54. #include "tydra/scene-access.hh"
  55. #include "tydra/shader-network.hh"
  56. #include "USDLoaderImplTinyusdzHelper.h"
  57. #include "USDLoaderImplTinyusdz.h"
  58. #include "USDLoaderUtil.h"
  59. #include "USDPreprocessor.h"
  60. #include "../../../contrib/tinyusdz/assimp_tinyusdz_logging.inc"
  61. namespace {
  62. static constexpr char TAG[] = "tinyusdz loader";
  63. }
  64. namespace Assimp {
  65. using namespace std;
  66. void USDImporterImplTinyusdz::InternReadFile(
  67. const std::string &pFile,
  68. aiScene *pScene,
  69. IOSystem *pIOHandler) {
  70. // Grab filename for logging purposes
  71. size_t pos = pFile.find_last_of('/');
  72. string basePath = pFile.substr(0, pos);
  73. string nameWExt = pFile.substr(pos + 1);
  74. stringstream ss;
  75. ss.str("");
  76. ss << "InternReadFile(): model" << nameWExt;
  77. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  78. bool is_load_from_mem{ pFile.substr(0, AI_MEMORYIO_MAGIC_FILENAME_LENGTH) == AI_MEMORYIO_MAGIC_FILENAME };
  79. std::vector<uint8_t> in_mem_data;
  80. if (is_load_from_mem) {
  81. auto stream_closer = [pIOHandler](IOStream *pStream) {
  82. pIOHandler->Close(pStream);
  83. };
  84. std::unique_ptr<IOStream, decltype(stream_closer)> file_stream(pIOHandler->Open(pFile, "rb"), stream_closer);
  85. if (!file_stream) {
  86. throw DeadlyImportError("Failed to open file ", pFile, ".");
  87. }
  88. size_t file_size{ file_stream->FileSize() };
  89. in_mem_data.resize(file_size);
  90. file_stream->Read(in_mem_data.data(), 1, file_size);
  91. }
  92. bool ret{ false };
  93. tinyusdz::USDLoadOptions options;
  94. tinyusdz::Stage stage;
  95. std::string warn, err;
  96. bool is_usdz{ false };
  97. if (isUsdc(pFile)) {
  98. ret = is_load_from_mem ? LoadUSDCFromMemory(in_mem_data.data(), in_mem_data.size(), pFile, &stage, &warn, &err, options) :
  99. LoadUSDCFromFile(pFile, &stage, &warn, &err, options);
  100. ss.str("");
  101. ss << "InternReadFile(): LoadUSDCFromFile() result: " << ret;
  102. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  103. } else if (isUsda(pFile)) {
  104. ret = is_load_from_mem ? LoadUSDAFromMemory(in_mem_data.data(), in_mem_data.size(), pFile, &stage, &warn, &err, options) :
  105. LoadUSDAFromFile(pFile, &stage, &warn, &err, options);
  106. ss.str("");
  107. ss << "InternReadFile(): LoadUSDAFromFile() result: " << ret;
  108. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  109. } else if (isUsdz(pFile)) {
  110. ret = is_load_from_mem ? LoadUSDZFromMemory(in_mem_data.data(), in_mem_data.size(), pFile, &stage, &warn, &err, options) :
  111. LoadUSDZFromFile(pFile, &stage, &warn, &err, options);
  112. is_usdz = true;
  113. ss.str("");
  114. ss << "InternReadFile(): LoadUSDZFromFile() result: " << ret;
  115. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  116. } else if (isUsd(pFile)) {
  117. ret = is_load_from_mem ? LoadUSDFromMemory(in_mem_data.data(), in_mem_data.size(), pFile, &stage, &warn, &err, options) :
  118. LoadUSDFromFile(pFile, &stage, &warn, &err, options);
  119. ss.str("");
  120. ss << "InternReadFile(): LoadUSDFromFile() result: " << ret;
  121. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  122. }
  123. if (warn.empty() && err.empty()) {
  124. ss.str("");
  125. ss << "InternReadFile(): load free of warnings/errors";
  126. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  127. } else {
  128. if (!warn.empty()) {
  129. ss.str("");
  130. ss << "InternReadFile(): WARNING reported: " << warn;
  131. TINYUSDZLOGW(TAG, "%s", ss.str().c_str());
  132. }
  133. if (!err.empty()) {
  134. ss.str("");
  135. ss << "InternReadFile(): ERROR reported: " << err;
  136. TINYUSDZLOGE(TAG, "%s", ss.str().c_str());
  137. }
  138. }
  139. if (!ret) {
  140. ss.str("");
  141. ss << "InternReadFile(): ERROR: load failed! ret: " << ret;
  142. TINYUSDZLOGE(TAG, "%s", ss.str().c_str());
  143. return;
  144. }
  145. tinyusdz::tydra::RenderScene render_scene;
  146. tinyusdz::tydra::RenderSceneConverter converter;
  147. tinyusdz::tydra::RenderSceneConverterEnv env(stage);
  148. std::string usd_basedir = tinyusdz::io::GetBaseDir(pFile);
  149. env.set_search_paths({ usd_basedir }); // {} needed to convert to vector of char
  150. // NOTE: Pointer address of usdz_asset must be valid until the call of RenderSceneConverter::ConvertToRenderScene.
  151. tinyusdz::USDZAsset usdz_asset;
  152. if (is_usdz) {
  153. bool is_read_USDZ_asset = is_load_from_mem ? tinyusdz::ReadUSDZAssetInfoFromMemory(in_mem_data.data(), in_mem_data.size(), false, &usdz_asset, &warn, &err) :
  154. tinyusdz::ReadUSDZAssetInfoFromFile(pFile, &usdz_asset, &warn, &err);
  155. if (!is_read_USDZ_asset) {
  156. if (!warn.empty()) {
  157. ss.str("");
  158. ss << "InternReadFile(): ReadUSDZAssetInfoFromFile: WARNING reported: " << warn;
  159. TINYUSDZLOGW(TAG, "%s", ss.str().c_str());
  160. }
  161. if (!err.empty()) {
  162. ss.str("");
  163. ss << "InternReadFile(): ReadUSDZAssetInfoFromFile: ERROR reported: " << err;
  164. TINYUSDZLOGE(TAG, "%s", ss.str().c_str());
  165. }
  166. ss.str("");
  167. ss << "InternReadFile(): ReadUSDZAssetInfoFromFile: ERROR!";
  168. TINYUSDZLOGE(TAG, "%s", ss.str().c_str());
  169. } else {
  170. ss.str("");
  171. ss << "InternReadFile(): ReadUSDZAssetInfoFromFile: OK";
  172. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  173. }
  174. tinyusdz::AssetResolutionResolver arr;
  175. if (!tinyusdz::SetupUSDZAssetResolution(arr, &usdz_asset)) {
  176. ss.str("");
  177. ss << "InternReadFile(): SetupUSDZAssetResolution: ERROR: load failed! ret: " << ret;
  178. TINYUSDZLOGE(TAG, "%s", ss.str().c_str());
  179. } else {
  180. ss.str("");
  181. ss << "InternReadFile(): SetupUSDZAssetResolution: OK";
  182. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  183. env.asset_resolver = arr;
  184. }
  185. }
  186. ret = converter.ConvertToRenderScene(env, &render_scene);
  187. if (!ret) {
  188. ss.str("");
  189. ss << "InternReadFile(): ConvertToRenderScene() failed!";
  190. TINYUSDZLOGE(TAG, "%s", ss.str().c_str());
  191. return;
  192. }
  193. // sanityCheckNodesRecursive(pScene->mRootNode);
  194. animations(render_scene, pScene);
  195. meshes(render_scene, pScene, nameWExt);
  196. materials(render_scene, pScene, nameWExt);
  197. textures(render_scene, pScene, nameWExt);
  198. textureImages(render_scene, pScene, nameWExt);
  199. buffers(render_scene, pScene, nameWExt);
  200. pScene->mRootNode = nodesRecursive(nullptr, render_scene.nodes[0], render_scene.skeletons);
  201. setupBlendShapes(render_scene, pScene, nameWExt);
  202. }
  203. void USDImporterImplTinyusdz::animations(
  204. const tinyusdz::tydra::RenderScene& render_scene,
  205. aiScene* pScene) {
  206. if (render_scene.animations.empty()) {
  207. return;
  208. }
  209. pScene->mNumAnimations = unsigned(render_scene.animations.size());
  210. pScene->mAnimations = new aiAnimation *[pScene->mNumAnimations];
  211. for (unsigned animationIndex = 0; animationIndex < pScene->mNumAnimations; ++animationIndex) {
  212. const auto &animation = render_scene.animations[animationIndex];
  213. auto newAiAnimation = new aiAnimation();
  214. pScene->mAnimations[animationIndex] = newAiAnimation;
  215. newAiAnimation->mName = animation.abs_path;
  216. if (animation.channels_map.empty()) {
  217. newAiAnimation->mNumChannels = 0;
  218. continue;
  219. }
  220. // each channel affects a node (joint)
  221. newAiAnimation->mTicksPerSecond = render_scene.meta.framesPerSecond;
  222. newAiAnimation->mNumChannels = unsigned(animation.channels_map.size());
  223. newAiAnimation->mChannels = new aiNodeAnim *[newAiAnimation->mNumChannels];
  224. int channelIndex = 0;
  225. for (const auto &[jointName, animationChannelMap] : animation.channels_map) {
  226. auto newAiNodeAnim = new aiNodeAnim();
  227. newAiAnimation->mChannels[channelIndex] = newAiNodeAnim;
  228. newAiNodeAnim->mNodeName = jointName;
  229. newAiAnimation->mDuration = 0;
  230. std::vector<aiVectorKey> positionKeys;
  231. std::vector<aiQuatKey> rotationKeys;
  232. std::vector<aiVectorKey> scalingKeys;
  233. for (const auto &[channelType, animChannel] : animationChannelMap) {
  234. switch (channelType) {
  235. case tinyusdz::tydra::AnimationChannel::ChannelType::Rotation:
  236. if (animChannel.rotations.static_value.has_value()) {
  237. rotationKeys.emplace_back(0, tinyUsdzQuatToAiQuat(animChannel.rotations.static_value.value()));
  238. }
  239. for (const auto &rotationAnimSampler : animChannel.rotations.samples) {
  240. if (rotationAnimSampler.t > newAiAnimation->mDuration) {
  241. newAiAnimation->mDuration = rotationAnimSampler.t;
  242. }
  243. rotationKeys.emplace_back(rotationAnimSampler.t, tinyUsdzQuatToAiQuat(rotationAnimSampler.value));
  244. }
  245. break;
  246. case tinyusdz::tydra::AnimationChannel::ChannelType::Scale:
  247. if (animChannel.scales.static_value.has_value()) {
  248. scalingKeys.emplace_back(0, tinyUsdzScaleOrPosToAssimp(animChannel.scales.static_value.value()));
  249. }
  250. for (const auto &scaleAnimSampler : animChannel.scales.samples) {
  251. if (scaleAnimSampler.t > newAiAnimation->mDuration) {
  252. newAiAnimation->mDuration = scaleAnimSampler.t;
  253. }
  254. scalingKeys.emplace_back(scaleAnimSampler.t, tinyUsdzScaleOrPosToAssimp(scaleAnimSampler.value));
  255. }
  256. break;
  257. case tinyusdz::tydra::AnimationChannel::ChannelType::Transform:
  258. if (animChannel.transforms.static_value.has_value()) {
  259. aiVector3D position;
  260. aiVector3D scale;
  261. aiQuaternion rotation;
  262. tinyUsdzMat4ToAiMat4(animChannel.transforms.static_value.value().m).Decompose(scale, rotation, position);
  263. positionKeys.emplace_back(0, position);
  264. scalingKeys.emplace_back(0, scale);
  265. rotationKeys.emplace_back(0, rotation);
  266. }
  267. for (const auto &transformAnimSampler : animChannel.transforms.samples) {
  268. if (transformAnimSampler.t > newAiAnimation->mDuration) {
  269. newAiAnimation->mDuration = transformAnimSampler.t;
  270. }
  271. aiVector3D position;
  272. aiVector3D scale;
  273. aiQuaternion rotation;
  274. tinyUsdzMat4ToAiMat4(transformAnimSampler.value.m).Decompose(scale, rotation, position);
  275. positionKeys.emplace_back(transformAnimSampler.t, position);
  276. scalingKeys.emplace_back(transformAnimSampler.t, scale);
  277. rotationKeys.emplace_back(transformAnimSampler.t, rotation);
  278. }
  279. break;
  280. case tinyusdz::tydra::AnimationChannel::ChannelType::Translation:
  281. if (animChannel.translations.static_value.has_value()) {
  282. positionKeys.emplace_back(0, tinyUsdzScaleOrPosToAssimp(animChannel.translations.static_value.value()));
  283. }
  284. for (const auto &translationAnimSampler : animChannel.translations.samples) {
  285. if (translationAnimSampler.t > newAiAnimation->mDuration) {
  286. newAiAnimation->mDuration = translationAnimSampler.t;
  287. }
  288. positionKeys.emplace_back(translationAnimSampler.t, tinyUsdzScaleOrPosToAssimp(translationAnimSampler.value));
  289. }
  290. break;
  291. default:
  292. TINYUSDZLOGW(TAG, "Unsupported animation channel type (%s). Please update the USD importer to support this animation channel.", tinyusdzAnimChannelTypeFor(channelType).c_str());
  293. }
  294. }
  295. newAiNodeAnim->mNumPositionKeys = unsigned(positionKeys.size());
  296. newAiNodeAnim->mPositionKeys = new aiVectorKey[newAiNodeAnim->mNumPositionKeys];
  297. std::move(positionKeys.begin(), positionKeys.end(), newAiNodeAnim->mPositionKeys);
  298. newAiNodeAnim->mNumRotationKeys = unsigned(rotationKeys.size());
  299. newAiNodeAnim->mRotationKeys = new aiQuatKey[newAiNodeAnim->mNumRotationKeys];
  300. std::move(rotationKeys.begin(), rotationKeys.end(), newAiNodeAnim->mRotationKeys);
  301. newAiNodeAnim->mNumScalingKeys = unsigned(scalingKeys.size());
  302. newAiNodeAnim->mScalingKeys = new aiVectorKey[newAiNodeAnim->mNumScalingKeys];
  303. std::move(scalingKeys.begin(), scalingKeys.end(), newAiNodeAnim->mScalingKeys);
  304. ++channelIndex;
  305. }
  306. }
  307. }
  308. void USDImporterImplTinyusdz::meshes(
  309. const tinyusdz::tydra::RenderScene &render_scene,
  310. aiScene *pScene,
  311. const std::string &nameWExt) {
  312. stringstream ss;
  313. pScene->mNumMeshes = static_cast<unsigned int>(render_scene.meshes.size());
  314. pScene->mMeshes = new aiMesh *[pScene->mNumMeshes]();
  315. ss.str("");
  316. ss << "meshes(): pScene->mNumMeshes: " << pScene->mNumMeshes;
  317. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  318. // Export meshes
  319. for (size_t meshIdx = 0; meshIdx < pScene->mNumMeshes; meshIdx++) {
  320. pScene->mMeshes[meshIdx] = new aiMesh();
  321. pScene->mMeshes[meshIdx]->mName.Set(render_scene.meshes[meshIdx].prim_name);
  322. ss.str("");
  323. ss << " mesh[" << meshIdx << "]: " <<
  324. render_scene.meshes[meshIdx].joint_and_weights.jointIndices.size() << " jointIndices, " <<
  325. render_scene.meshes[meshIdx].joint_and_weights.jointWeights.size() << " jointWeights, elementSize: " <<
  326. render_scene.meshes[meshIdx].joint_and_weights.elementSize;
  327. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  328. ss.str("");
  329. ss << " skel_id: " << render_scene.meshes[meshIdx].skel_id;
  330. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  331. if (render_scene.meshes[meshIdx].material_id > -1) {
  332. pScene->mMeshes[meshIdx]->mMaterialIndex = render_scene.meshes[meshIdx].material_id;
  333. }
  334. verticesForMesh(render_scene, pScene, meshIdx, nameWExt);
  335. facesForMesh(render_scene, pScene, meshIdx, nameWExt);
  336. // Some models infer normals from faces, but others need them e.g.
  337. // - apple "toy car" canopy normals will be wrong
  338. // - human "untitled" model (tinyusdz issue #115) will be "splotchy"
  339. normalsForMesh(render_scene, pScene, meshIdx, nameWExt);
  340. materialsForMesh(render_scene, pScene, meshIdx, nameWExt);
  341. uvsForMesh(render_scene, pScene, meshIdx, nameWExt);
  342. }
  343. }
  344. void USDImporterImplTinyusdz::verticesForMesh(
  345. const tinyusdz::tydra::RenderScene &render_scene,
  346. aiScene *pScene,
  347. size_t meshIdx,
  348. const std::string &nameWExt) {
  349. UNUSED(nameWExt);
  350. const auto numVertices = static_cast<unsigned int>(render_scene.meshes[meshIdx].points.size());
  351. pScene->mMeshes[meshIdx]->mNumVertices = numVertices;
  352. pScene->mMeshes[meshIdx]->mVertices = new aiVector3D[pScene->mMeshes[meshIdx]->mNumVertices];
  353. // Check if this is a skinned mesh
  354. if (int skeleton_id = render_scene.meshes[meshIdx].skel_id; skeleton_id > -1) {
  355. // Recursively iterate to collect all the joints in the hierarchy into a flattened array
  356. std::vector<const tinyusdz::tydra::SkelNode *> skeletonNodes;
  357. skeletonNodes.push_back(&render_scene.skeletons[skeleton_id].root_node);
  358. for (int i = 0; i < skeletonNodes.size(); ++i) {
  359. for (const auto &child : skeletonNodes[i]->children) {
  360. skeletonNodes.push_back(&child);
  361. }
  362. }
  363. // Convert USD skeleton joints to Assimp bones
  364. const unsigned int numBones = unsigned(skeletonNodes.size());
  365. pScene->mMeshes[meshIdx]->mNumBones = numBones;
  366. pScene->mMeshes[meshIdx]->mBones = new aiBone *[numBones];
  367. for (unsigned int i = 0; i < numBones; ++i) {
  368. const tinyusdz::tydra::SkelNode *skeletonNode = skeletonNodes[i];
  369. const int boneIndex = skeletonNode->joint_id;
  370. // Sorted so that Assimp bone ids align with USD joint id
  371. auto outputBone = new aiBone();
  372. outputBone->mName = aiString(skeletonNode->joint_name);
  373. outputBone->mOffsetMatrix = tinyUsdzMat4ToAiMat4(skeletonNode->bind_transform.m).Inverse();
  374. pScene->mMeshes[meshIdx]->mBones[boneIndex] = outputBone;
  375. }
  376. // Vertex weights
  377. std::vector<std::vector<aiVertexWeight>> aiBonesVertexWeights;
  378. aiBonesVertexWeights.resize(numBones);
  379. const std::vector<int> &jointIndices = render_scene.meshes[meshIdx].joint_and_weights.jointIndices;
  380. const std::vector<float> &jointWeightIndices = render_scene.meshes[meshIdx].joint_and_weights.jointWeights;
  381. const int numWeightsPerVertex = render_scene.meshes[meshIdx].joint_and_weights.elementSize;
  382. for (unsigned int vertexIndex = 0; vertexIndex < numVertices; ++vertexIndex) {
  383. for (int weightIndex = 0; weightIndex < numWeightsPerVertex; ++weightIndex) {
  384. const unsigned int index = vertexIndex * numWeightsPerVertex + weightIndex;
  385. const float jointWeight = jointWeightIndices[index];
  386. if (jointWeight > 0) {
  387. const int jointIndex = jointIndices[index];
  388. aiBonesVertexWeights[jointIndex].emplace_back(vertexIndex, jointWeight);
  389. }
  390. }
  391. }
  392. for (unsigned boneIndex = 0; boneIndex < numBones; ++boneIndex) {
  393. const auto numWeightsForBone = unsigned(aiBonesVertexWeights[boneIndex].size());
  394. pScene->mMeshes[meshIdx]->mBones[boneIndex]->mWeights = new aiVertexWeight[numWeightsForBone];
  395. pScene->mMeshes[meshIdx]->mBones[boneIndex]->mNumWeights = numWeightsForBone;
  396. std::swap_ranges(aiBonesVertexWeights[boneIndex].begin(), aiBonesVertexWeights[boneIndex].end(), pScene->mMeshes[meshIdx]->mBones[boneIndex]->mWeights);
  397. }
  398. } // Skinned mesh end
  399. for (size_t j = 0; j < pScene->mMeshes[meshIdx]->mNumVertices; ++j) {
  400. pScene->mMeshes[meshIdx]->mVertices[j].x = render_scene.meshes[meshIdx].points[j][0];
  401. pScene->mMeshes[meshIdx]->mVertices[j].y = render_scene.meshes[meshIdx].points[j][1];
  402. pScene->mMeshes[meshIdx]->mVertices[j].z = render_scene.meshes[meshIdx].points[j][2];
  403. }
  404. }
  405. void USDImporterImplTinyusdz::facesForMesh(
  406. const tinyusdz::tydra::RenderScene &render_scene,
  407. aiScene *pScene,
  408. size_t meshIdx,
  409. const std::string &nameWExt) {
  410. UNUSED(nameWExt);
  411. pScene->mMeshes[meshIdx]->mNumFaces = static_cast<unsigned int>(render_scene.meshes[meshIdx].faceVertexCounts().size());
  412. pScene->mMeshes[meshIdx]->mFaces = new aiFace[pScene->mMeshes[meshIdx]->mNumFaces]();
  413. size_t faceVertIdxOffset = 0;
  414. for (size_t faceIdx = 0; faceIdx < pScene->mMeshes[meshIdx]->mNumFaces; ++faceIdx) {
  415. pScene->mMeshes[meshIdx]->mFaces[faceIdx].mNumIndices = render_scene.meshes[meshIdx].faceVertexCounts()[faceIdx];
  416. pScene->mMeshes[meshIdx]->mFaces[faceIdx].mIndices = new unsigned int[pScene->mMeshes[meshIdx]->mFaces[faceIdx].mNumIndices];
  417. for (size_t j = 0; j < pScene->mMeshes[meshIdx]->mFaces[faceIdx].mNumIndices; ++j) {
  418. pScene->mMeshes[meshIdx]->mFaces[faceIdx].mIndices[j] =
  419. render_scene.meshes[meshIdx].faceVertexIndices()[j + faceVertIdxOffset];
  420. }
  421. faceVertIdxOffset += pScene->mMeshes[meshIdx]->mFaces[faceIdx].mNumIndices;
  422. }
  423. }
  424. void USDImporterImplTinyusdz::normalsForMesh(
  425. const tinyusdz::tydra::RenderScene &render_scene,
  426. aiScene *pScene,
  427. size_t meshIdx,
  428. const std::string &nameWExt) {
  429. UNUSED(nameWExt);
  430. pScene->mMeshes[meshIdx]->mNormals = new aiVector3D[pScene->mMeshes[meshIdx]->mNumVertices];
  431. const float *floatPtr = reinterpret_cast<const float *>(render_scene.meshes[meshIdx].normals.get_data().data());
  432. for (size_t vertIdx = 0, fpj = 0; vertIdx < pScene->mMeshes[meshIdx]->mNumVertices; ++vertIdx, fpj += 3) {
  433. pScene->mMeshes[meshIdx]->mNormals[vertIdx].x = floatPtr[fpj];
  434. pScene->mMeshes[meshIdx]->mNormals[vertIdx].y = floatPtr[fpj + 1];
  435. pScene->mMeshes[meshIdx]->mNormals[vertIdx].z = floatPtr[fpj + 2];
  436. }
  437. }
  438. void USDImporterImplTinyusdz::materialsForMesh(
  439. const tinyusdz::tydra::RenderScene &render_scene,
  440. aiScene *pScene,
  441. size_t meshIdx,
  442. const std::string &nameWExt) {
  443. UNUSED(render_scene); UNUSED(pScene); UNUSED(meshIdx); UNUSED(nameWExt);
  444. }
  445. void USDImporterImplTinyusdz::uvsForMesh(
  446. const tinyusdz::tydra::RenderScene &render_scene,
  447. aiScene *pScene,
  448. size_t meshIdx,
  449. const std::string &nameWExt) {
  450. UNUSED(nameWExt);
  451. const size_t uvSlotsCount = render_scene.meshes[meshIdx].texcoords.size();
  452. if (uvSlotsCount < 1) {
  453. return;
  454. }
  455. pScene->mMeshes[meshIdx]->mTextureCoords[0] = new aiVector3D[pScene->mMeshes[meshIdx]->mNumVertices];
  456. pScene->mMeshes[meshIdx]->mNumUVComponents[0] = 2; // U and V stored in "x", "y" of aiVector3D.
  457. for (unsigned int uvSlotIdx = 0; uvSlotIdx < uvSlotsCount; ++uvSlotIdx) {
  458. const auto uvsForSlot = render_scene.meshes[meshIdx].texcoords.at(uvSlotIdx);
  459. if (uvsForSlot.get_data().size() == 0) {
  460. continue;
  461. }
  462. const float *floatPtr = reinterpret_cast<const float *>(uvsForSlot.get_data().data());
  463. for (size_t vertIdx = 0, fpj = 0; vertIdx < pScene->mMeshes[meshIdx]->mNumVertices; ++vertIdx, fpj += 2) {
  464. pScene->mMeshes[meshIdx]->mTextureCoords[uvSlotIdx][vertIdx].x = floatPtr[fpj];
  465. pScene->mMeshes[meshIdx]->mTextureCoords[uvSlotIdx][vertIdx].y = floatPtr[fpj + 1];
  466. }
  467. }
  468. }
  469. static aiColor3D *ownedColorPtrFor(const std::array<float, 3> &color) {
  470. aiColor3D *colorPtr = new aiColor3D();
  471. colorPtr->r = color[0];
  472. colorPtr->g = color[1];
  473. colorPtr->b = color[2];
  474. return colorPtr;
  475. }
  476. static std::string nameForTextureWithId(
  477. const tinyusdz::tydra::RenderScene &render_scene,
  478. const int targetId) {
  479. stringstream ss;
  480. std::string texName;
  481. for (const auto &image : render_scene.images) {
  482. if (image.buffer_id == targetId) {
  483. texName = image.asset_identifier;
  484. ss.str("");
  485. ss << "nameForTextureWithId(): found texture " << texName << " with target id " << targetId;
  486. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  487. break;
  488. }
  489. }
  490. ss.str("");
  491. ss << "nameForTextureWithId(): ERROR! Failed to find texture with target id " << targetId;
  492. TINYUSDZLOGE(TAG, "%s", ss.str().c_str());
  493. return texName;
  494. }
  495. static void assignTexture(
  496. const tinyusdz::tydra::RenderScene &render_scene,
  497. const tinyusdz::tydra::RenderMaterial &material,
  498. aiMaterial *mat,
  499. const int textureId,
  500. const int aiTextureType) {
  501. UNUSED(material);
  502. std::string name = nameForTextureWithId(render_scene, textureId);
  503. aiString *texName = new aiString();
  504. texName->Set(name);
  505. stringstream ss;
  506. ss.str("");
  507. ss << "assignTexture(): name: " << name;
  508. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  509. // TODO: verify hard-coded '0' index is correct
  510. mat->AddProperty(texName, _AI_MATKEY_TEXTURE_BASE, aiTextureType, 0);
  511. }
  512. void USDImporterImplTinyusdz::materials(
  513. const tinyusdz::tydra::RenderScene &render_scene,
  514. aiScene *pScene,
  515. const std::string &nameWExt) {
  516. const size_t numMaterials{render_scene.materials.size()};
  517. (void) numMaterials; // Ignore unused variable when -Werror enabled
  518. stringstream ss;
  519. ss.str("");
  520. ss << "materials(): model" << nameWExt << ", numMaterials: " << numMaterials;
  521. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  522. pScene->mNumMaterials = 0;
  523. if (render_scene.materials.empty()) {
  524. return;
  525. }
  526. pScene->mMaterials = new aiMaterial *[render_scene.materials.size()];
  527. for (const auto &material : render_scene.materials) {
  528. ss.str("");
  529. ss << " material[" << pScene->mNumMaterials << "]: name: |" << material.name << "|, disp name: |" << material.display_name << "|";
  530. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  531. aiMaterial *mat = new aiMaterial;
  532. aiString *materialName = new aiString();
  533. materialName->Set(material.name);
  534. mat->AddProperty(materialName, AI_MATKEY_NAME);
  535. mat->AddProperty(
  536. ownedColorPtrFor(material.surfaceShader.diffuseColor.value),
  537. 1, AI_MATKEY_COLOR_DIFFUSE);
  538. mat->AddProperty(
  539. ownedColorPtrFor(material.surfaceShader.specularColor.value),
  540. 1, AI_MATKEY_COLOR_SPECULAR);
  541. mat->AddProperty(
  542. ownedColorPtrFor(material.surfaceShader.emissiveColor.value),
  543. 1, AI_MATKEY_COLOR_EMISSIVE);
  544. ss.str("");
  545. if (material.surfaceShader.diffuseColor.is_texture()) {
  546. assignTexture(render_scene, material, mat, material.surfaceShader.diffuseColor.texture_id, aiTextureType_DIFFUSE);
  547. ss << " material[" << pScene->mNumMaterials << "]: diff tex id " << material.surfaceShader.diffuseColor.texture_id << "\n";
  548. }
  549. if (material.surfaceShader.specularColor.is_texture()) {
  550. assignTexture(render_scene, material, mat, material.surfaceShader.specularColor.texture_id, aiTextureType_SPECULAR);
  551. ss << " material[" << pScene->mNumMaterials << "]: spec tex id " << material.surfaceShader.specularColor.texture_id << "\n";
  552. }
  553. if (material.surfaceShader.normal.is_texture()) {
  554. assignTexture(render_scene, material, mat, material.surfaceShader.normal.texture_id, aiTextureType_NORMALS);
  555. ss << " material[" << pScene->mNumMaterials << "]: normal tex id " << material.surfaceShader.normal.texture_id << "\n";
  556. }
  557. if (material.surfaceShader.emissiveColor.is_texture()) {
  558. assignTexture(render_scene, material, mat, material.surfaceShader.emissiveColor.texture_id, aiTextureType_EMISSIVE);
  559. ss << " material[" << pScene->mNumMaterials << "]: emissive tex id " << material.surfaceShader.emissiveColor.texture_id << "\n";
  560. }
  561. if (material.surfaceShader.occlusion.is_texture()) {
  562. assignTexture(render_scene, material, mat, material.surfaceShader.occlusion.texture_id, aiTextureType_LIGHTMAP);
  563. ss << " material[" << pScene->mNumMaterials << "]: lightmap (occlusion) tex id " << material.surfaceShader.occlusion.texture_id << "\n";
  564. }
  565. if (material.surfaceShader.metallic.is_texture()) {
  566. assignTexture(render_scene, material, mat, material.surfaceShader.metallic.texture_id, aiTextureType_METALNESS);
  567. ss << " material[" << pScene->mNumMaterials << "]: metallic tex id " << material.surfaceShader.metallic.texture_id << "\n";
  568. }
  569. if (material.surfaceShader.roughness.is_texture()) {
  570. assignTexture(render_scene, material, mat, material.surfaceShader.roughness.texture_id, aiTextureType_DIFFUSE_ROUGHNESS);
  571. ss << " material[" << pScene->mNumMaterials << "]: roughness tex id " << material.surfaceShader.roughness.texture_id << "\n";
  572. }
  573. if (material.surfaceShader.clearcoat.is_texture()) {
  574. assignTexture(render_scene, material, mat, material.surfaceShader.clearcoat.texture_id, aiTextureType_CLEARCOAT);
  575. ss << " material[" << pScene->mNumMaterials << "]: clearcoat tex id " << material.surfaceShader.clearcoat.texture_id << "\n";
  576. }
  577. if (material.surfaceShader.opacity.is_texture()) {
  578. assignTexture(render_scene, material, mat, material.surfaceShader.opacity.texture_id, aiTextureType_OPACITY);
  579. ss << " material[" << pScene->mNumMaterials << "]: opacity tex id " << material.surfaceShader.opacity.texture_id << "\n";
  580. }
  581. if (material.surfaceShader.displacement.is_texture()) {
  582. assignTexture(render_scene, material, mat, material.surfaceShader.displacement.texture_id, aiTextureType_DISPLACEMENT);
  583. ss << " material[" << pScene->mNumMaterials << "]: displacement tex id " << material.surfaceShader.displacement.texture_id << "\n";
  584. }
  585. if (material.surfaceShader.clearcoatRoughness.is_texture()) {
  586. ss << " material[" << pScene->mNumMaterials << "]: clearcoatRoughness tex id " << material.surfaceShader.clearcoatRoughness.texture_id << "\n";
  587. }
  588. if (material.surfaceShader.opacityThreshold.is_texture()) {
  589. ss << " material[" << pScene->mNumMaterials << "]: opacityThreshold tex id " << material.surfaceShader.opacityThreshold.texture_id << "\n";
  590. }
  591. if (material.surfaceShader.ior.is_texture()) {
  592. ss << " material[" << pScene->mNumMaterials << "]: ior tex id " << material.surfaceShader.ior.texture_id << "\n";
  593. }
  594. if (!ss.str().empty()) {
  595. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  596. }
  597. pScene->mMaterials[pScene->mNumMaterials] = mat;
  598. ++pScene->mNumMaterials;
  599. }
  600. }
  601. void USDImporterImplTinyusdz::textures(
  602. const tinyusdz::tydra::RenderScene &render_scene,
  603. aiScene *pScene,
  604. const std::string &nameWExt) {
  605. UNUSED(pScene);
  606. const size_t numTextures{render_scene.textures.size()};
  607. UNUSED(numTextures); // Ignore unused variable when -Werror enabled
  608. stringstream ss;
  609. ss.str("");
  610. ss << "textures(): model" << nameWExt << ", numTextures: " << numTextures;
  611. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  612. size_t i{0};
  613. UNUSED(i);
  614. for (const auto &texture : render_scene.textures) {
  615. UNUSED(texture);
  616. ss.str("");
  617. ss << " texture[" << i << "]: id: " << texture.texture_image_id << ", disp name: |" << texture.display_name << "|, varname_uv: " <<
  618. texture.varname_uv << ", prim_name: |" << texture.prim_name << "|, abs_path: |" << texture.abs_path << "|";
  619. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  620. ++i;
  621. }
  622. }
  623. /**
  624. * "owned" as in, used "new" to allocate and aiScene now responsible for "delete"
  625. *
  626. * @param render_scene renderScene object
  627. * @param image textureImage object
  628. * @param nameWExt filename w/ext (use to extract file type hint)
  629. * @return aiTexture ptr
  630. */
  631. static aiTexture *ownedEmbeddedTextureFor(
  632. const tinyusdz::tydra::RenderScene &render_scene,
  633. const tinyusdz::tydra::TextureImage &image,
  634. const std::string &nameWExt) {
  635. UNUSED(nameWExt);
  636. stringstream ss;
  637. aiTexture *tex = new aiTexture();
  638. size_t pos = image.asset_identifier.find_last_of('/');
  639. string embTexName{image.asset_identifier.substr(pos + 1)};
  640. tex->mFilename.Set(image.asset_identifier.c_str());
  641. tex->mHeight = image.height;
  642. tex->mWidth = image.width;
  643. if (tex->mHeight == 0) {
  644. pos = embTexName.find_last_of('.');
  645. strncpy(tex->achFormatHint, embTexName.substr(pos + 1).c_str(), 3);
  646. const size_t imageBytesCount{render_scene.buffers[image.buffer_id].data.size()};
  647. tex->pcData = (aiTexel *) new char[imageBytesCount];
  648. memcpy(tex->pcData, &render_scene.buffers[image.buffer_id].data[0], imageBytesCount);
  649. } else {
  650. string formatHint{"rgba8888"};
  651. strncpy(tex->achFormatHint, formatHint.c_str(), 8);
  652. const size_t imageTexelsCount{tex->mWidth * tex->mHeight};
  653. tex->pcData = (aiTexel *) new char[imageTexelsCount * image.channels];
  654. const float *floatPtr = reinterpret_cast<const float *>(&render_scene.buffers[image.buffer_id].data[0]);
  655. ss.str("");
  656. ss << "ownedEmbeddedTextureFor(): manual fill...";
  657. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  658. for (size_t i = 0, fpi = 0; i < imageTexelsCount; ++i, fpi += 4) {
  659. tex->pcData[i].b = static_cast<uint8_t>(floatPtr[fpi] * 255);
  660. tex->pcData[i].g = static_cast<uint8_t>(floatPtr[fpi + 1] * 255);
  661. tex->pcData[i].r = static_cast<uint8_t>(floatPtr[fpi + 2] * 255);
  662. tex->pcData[i].a = static_cast<uint8_t>(floatPtr[fpi + 3] * 255);
  663. }
  664. ss.str("");
  665. ss << "ownedEmbeddedTextureFor(): imageTexelsCount: " << imageTexelsCount << ", channels: " << image.channels;
  666. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  667. }
  668. return tex;
  669. }
  670. void USDImporterImplTinyusdz::textureImages(
  671. const tinyusdz::tydra::RenderScene &render_scene,
  672. aiScene *pScene,
  673. const std::string &nameWExt) {
  674. stringstream ss;
  675. const size_t numTextureImages{render_scene.images.size()};
  676. UNUSED(numTextureImages); // Ignore unused variable when -Werror enabled
  677. ss.str("");
  678. ss << "textureImages(): model" << nameWExt << ", numTextureImages: " << numTextureImages;
  679. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  680. pScene->mTextures = nullptr; // Need to iterate over images before knowing if valid textures available
  681. pScene->mNumTextures = 0;
  682. for (const auto &image : render_scene.images) {
  683. ss.str("");
  684. ss << " image[" << pScene->mNumTextures << "]: |" << image.asset_identifier << "| w: " << image.width << ", h: " << image.height <<
  685. ", channels: " << image.channels << ", miplevel: " << image.miplevel << ", buffer id: " << image.buffer_id << "\n" <<
  686. " buffers.size(): " << render_scene.buffers.size() << ", data empty? " << render_scene.buffers[image.buffer_id].data.empty();
  687. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  688. if (image.buffer_id > -1 &&
  689. image.buffer_id < static_cast<long int>(render_scene.buffers.size()) &&
  690. !render_scene.buffers[image.buffer_id].data.empty()) {
  691. aiTexture *tex = ownedEmbeddedTextureFor(
  692. render_scene,
  693. image,
  694. nameWExt);
  695. if (pScene->mTextures == nullptr) {
  696. ss.str("");
  697. ss << " Init pScene->mTextures[" << render_scene.images.size() << "]";
  698. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  699. pScene->mTextures = new aiTexture *[render_scene.images.size()];
  700. }
  701. ss.str("");
  702. ss << " pScene->mTextures[" << pScene->mNumTextures << "] name: |" << tex->mFilename.C_Str() <<
  703. "|, w: " << tex->mWidth << ", h: " << tex->mHeight << ", hint: " << tex->achFormatHint;
  704. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  705. pScene->mTextures[pScene->mNumTextures++] = tex;
  706. }
  707. }
  708. }
  709. void USDImporterImplTinyusdz::buffers(
  710. const tinyusdz::tydra::RenderScene &render_scene,
  711. aiScene *pScene,
  712. const std::string &nameWExt) {
  713. const size_t numBuffers{render_scene.buffers.size()};
  714. UNUSED(pScene); UNUSED(numBuffers); // Ignore unused variable when -Werror enabled
  715. stringstream ss;
  716. ss.str("");
  717. ss << "buffers(): model" << nameWExt << ", numBuffers: " << numBuffers;
  718. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  719. size_t i = 0;
  720. for (const auto &buffer : render_scene.buffers) {
  721. ss.str("");
  722. ss << " buffer[" << i << "]: count: " << buffer.data.size() << ", type: " << to_string(buffer.componentType);
  723. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  724. ++i;
  725. }
  726. }
  727. using Assimp::tinyusdzNodeTypeFor;
  728. using Assimp::tinyUsdzMat4ToAiMat4;
  729. using tinyusdz::tydra::NodeType;
  730. aiNode *USDImporterImplTinyusdz::nodesRecursive(
  731. aiNode *pNodeParent,
  732. const tinyusdz::tydra::Node &node,
  733. const std::vector<tinyusdz::tydra::SkelHierarchy> &skeletons) {
  734. stringstream ss;
  735. aiNode *cNode = new aiNode();
  736. cNode->mParent = pNodeParent;
  737. cNode->mName.Set(node.prim_name);
  738. cNode->mTransformation = tinyUsdzMat4ToAiMat4(node.local_matrix.m);
  739. if (node.nodeType == NodeType::Mesh) {
  740. cNode->mNumMeshes = 1;
  741. cNode->mMeshes = new unsigned int[cNode->mNumMeshes];
  742. cNode->mMeshes[0] = node.id;
  743. }
  744. ss.str("");
  745. ss << "nodesRecursive(): node " << cNode->mName.C_Str() <<
  746. " type: |" << tinyusdzNodeTypeFor(node.nodeType) <<
  747. "|, disp " << node.display_name << ", abs " << node.abs_path;
  748. if (cNode->mParent != nullptr) {
  749. ss << " (parent " << cNode->mParent->mName.C_Str() << ")";
  750. }
  751. ss << " has " << node.children.size() << " children";
  752. if (node.nodeType == NodeType::Mesh) {
  753. ss << "\n node mesh id: " << node.id << " (node type: " << tinyusdzNodeTypeFor(node.nodeType) << ")";
  754. }
  755. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  756. unsigned int numChildren = unsigned(node.children.size());
  757. // Find any tinyusdz skeletons which might begin at this node
  758. // Add the skeleton bones as child nodes
  759. const tinyusdz::tydra::SkelNode *skelNode = nullptr;
  760. for (const auto &skeleton : skeletons) {
  761. if (skeleton.abs_path == node.abs_path) {
  762. // Add this skeleton's bones as child nodes
  763. ++numChildren;
  764. skelNode = &skeleton.root_node;
  765. break;
  766. }
  767. }
  768. cNode->mNumChildren = numChildren;
  769. // Done. No more children.
  770. if (numChildren == 0) {
  771. return cNode;
  772. }
  773. cNode->mChildren = new aiNode *[cNode->mNumChildren];
  774. size_t i{ 0 };
  775. for (const auto &childNode : node.children) {
  776. cNode->mChildren[i] = nodesRecursive(cNode, childNode, skeletons);
  777. ++i;
  778. }
  779. if (skelNode != nullptr) {
  780. // Convert USD skeleton into an Assimp node and make it the last child
  781. cNode->mChildren[cNode->mNumChildren-1] = skeletonNodesRecursive(cNode, *skelNode);
  782. }
  783. return cNode;
  784. }
  785. aiNode *USDImporterImplTinyusdz::skeletonNodesRecursive(
  786. aiNode* pNodeParent,
  787. const tinyusdz::tydra::SkelNode& joint) {
  788. auto *cNode = new aiNode(joint.joint_path);
  789. cNode->mParent = pNodeParent;
  790. cNode->mNumMeshes = 0; // not a mesh node
  791. cNode->mTransformation = tinyUsdzMat4ToAiMat4(joint.rest_transform.m);
  792. // Done. No more children.
  793. if (joint.children.empty()) {
  794. return cNode;
  795. }
  796. cNode->mNumChildren = static_cast<unsigned int>(joint.children.size());
  797. cNode->mChildren = new aiNode *[cNode->mNumChildren];
  798. for (unsigned i = 0; i < cNode->mNumChildren; ++i) {
  799. const tinyusdz::tydra::SkelNode &childJoint = joint.children[i];
  800. cNode->mChildren[i] = skeletonNodesRecursive(cNode, childJoint);
  801. }
  802. return cNode;
  803. }
  804. void USDImporterImplTinyusdz::sanityCheckNodesRecursive(
  805. aiNode *cNode) {
  806. stringstream ss;
  807. ss.str("");
  808. ss << "sanityCheckNodesRecursive(): node " << cNode->mName.C_Str();
  809. if (cNode->mParent != nullptr) {
  810. ss << " (parent " << cNode->mParent->mName.C_Str() << ")";
  811. }
  812. ss << " has " << cNode->mNumChildren << " children";
  813. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  814. for (size_t i = 0; i < cNode->mNumChildren; ++i) {
  815. sanityCheckNodesRecursive(cNode->mChildren[i]);
  816. }
  817. }
  818. void USDImporterImplTinyusdz::setupBlendShapes(
  819. const tinyusdz::tydra::RenderScene &render_scene,
  820. aiScene *pScene,
  821. const std::string &nameWExt) {
  822. stringstream ss;
  823. ss.str("");
  824. ss << "setupBlendShapes(): iterating over " << pScene->mNumMeshes << " meshes for model" << nameWExt;
  825. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  826. for (size_t meshIdx = 0; meshIdx < pScene->mNumMeshes; meshIdx++) {
  827. blendShapesForMesh(render_scene, pScene, meshIdx, nameWExt);
  828. }
  829. }
  830. void USDImporterImplTinyusdz::blendShapesForMesh(
  831. const tinyusdz::tydra::RenderScene &render_scene,
  832. aiScene *pScene,
  833. size_t meshIdx,
  834. const std::string &nameWExt) {
  835. UNUSED(nameWExt);
  836. stringstream ss;
  837. const unsigned int numBlendShapeTargets{static_cast<unsigned int>(render_scene.meshes[meshIdx].targets.size())};
  838. UNUSED(numBlendShapeTargets); // Ignore unused variable when -Werror enabled
  839. ss.str("");
  840. ss << " blendShapesForMesh(): mesh[" << meshIdx << "], numBlendShapeTargets: " << numBlendShapeTargets;
  841. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  842. if (numBlendShapeTargets > 0) {
  843. pScene->mMeshes[meshIdx]->mNumAnimMeshes = numBlendShapeTargets;
  844. pScene->mMeshes[meshIdx]->mAnimMeshes = new aiAnimMesh *[pScene->mMeshes[meshIdx]->mNumAnimMeshes];
  845. }
  846. auto mapIter = render_scene.meshes[meshIdx].targets.begin();
  847. size_t animMeshIdx{0};
  848. for (; mapIter != render_scene.meshes[meshIdx].targets.end(); ++mapIter) {
  849. const std::string name{mapIter->first};
  850. const tinyusdz::tydra::ShapeTarget shapeTarget{mapIter->second};
  851. pScene->mMeshes[meshIdx]->mAnimMeshes[animMeshIdx] = aiCreateAnimMesh(pScene->mMeshes[meshIdx]);
  852. ss.str("");
  853. ss << " mAnimMeshes[" << animMeshIdx << "]: mNumVertices: " << pScene->mMeshes[meshIdx]->mAnimMeshes[animMeshIdx]->mNumVertices <<
  854. ", target: " << shapeTarget.pointIndices.size() << " pointIndices, " << shapeTarget.pointOffsets.size() <<
  855. " pointOffsets, " << shapeTarget.normalOffsets.size() << " normalOffsets";
  856. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  857. for (size_t iVert = 0; iVert < shapeTarget.pointOffsets.size(); ++iVert) {
  858. pScene->mMeshes[meshIdx]->mAnimMeshes[animMeshIdx]->mVertices[shapeTarget.pointIndices[iVert]] +=
  859. tinyUsdzScaleOrPosToAssimp(shapeTarget.pointOffsets[iVert]);
  860. }
  861. for (size_t iVert = 0; iVert < shapeTarget.normalOffsets.size(); ++iVert) {
  862. pScene->mMeshes[meshIdx]->mAnimMeshes[animMeshIdx]->mNormals[shapeTarget.pointIndices[iVert]] +=
  863. tinyUsdzScaleOrPosToAssimp(shapeTarget.normalOffsets[iVert]);
  864. }
  865. ss.str("");
  866. ss << " target[" << animMeshIdx << "]: name: " << name << ", prim_name: " <<
  867. shapeTarget.prim_name << ", abs_path: " << shapeTarget.abs_path <<
  868. ", display_name: " << shapeTarget.display_name << ", " << shapeTarget.pointIndices.size() <<
  869. " pointIndices, " << shapeTarget.pointOffsets.size() << " pointOffsets, " <<
  870. shapeTarget.normalOffsets.size() << " normalOffsets, " << shapeTarget.inbetweens.size() <<
  871. " inbetweens";
  872. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  873. ++animMeshIdx;
  874. }
  875. }
  876. } // namespace Assimp
  877. #endif // !! ASSIMP_BUILD_NO_USD_IMPORTER