USDLoaderImplTinyusdz.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2024, 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. setupNodes(render_scene, pScene, nameWExt);
  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 = render_scene.animations.size();
  210. pScene->mAnimations = new aiAnimation *[pScene->mNumAnimations];
  211. for (int 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->mNumChannels = animation.channels_map.size();
  222. newAiAnimation->mChannels = new aiNodeAnim *[newAiAnimation->mNumChannels];
  223. int channelIndex = 0;
  224. for (const auto &[jointName, animationChannelMap] : animation.channels_map) {
  225. auto newAiNodeAnim = new aiNodeAnim();
  226. newAiAnimation->mChannels[channelIndex] = newAiNodeAnim;
  227. newAiNodeAnim->mNodeName = jointName;
  228. newAiAnimation->mDuration = 0;
  229. std::vector<aiVectorKey> positionKeys;
  230. std::vector<aiQuatKey> rotationKeys;
  231. std::vector<aiVectorKey> scalingKeys;
  232. for (const auto &[channelType, animChannel] : animationChannelMap) {
  233. switch (channelType) {
  234. case tinyusdz::tydra::AnimationChannel::ChannelType::Rotation:
  235. if (animChannel.rotations.static_value.has_value()) {
  236. rotationKeys.emplace_back(0, tinyUsdzQuatToAiQuat(animChannel.rotations.static_value.value()));
  237. }
  238. for (const auto &rotationAnimSampler : animChannel.rotations.samples) {
  239. if (rotationAnimSampler.t > newAiAnimation->mDuration) {
  240. newAiAnimation->mDuration = rotationAnimSampler.t;
  241. }
  242. rotationKeys.emplace_back(rotationAnimSampler.t, tinyUsdzQuatToAiQuat(rotationAnimSampler.value));
  243. }
  244. break;
  245. case tinyusdz::tydra::AnimationChannel::ChannelType::Scale:
  246. if (animChannel.scales.static_value.has_value()) {
  247. scalingKeys.emplace_back(0, tinyUsdzScaleOrPosToAssimp(animChannel.scales.static_value.value()));
  248. }
  249. for (const auto &scaleAnimSampler : animChannel.scales.samples) {
  250. if (scaleAnimSampler.t > newAiAnimation->mDuration) {
  251. newAiAnimation->mDuration = scaleAnimSampler.t;
  252. }
  253. scalingKeys.emplace_back(scaleAnimSampler.t, tinyUsdzScaleOrPosToAssimp(scaleAnimSampler.value));
  254. }
  255. break;
  256. case tinyusdz::tydra::AnimationChannel::ChannelType::Transform:
  257. if (animChannel.transforms.static_value.has_value()) {
  258. aiVector3D position;
  259. aiVector3D scale;
  260. aiQuaternion rotation;
  261. tinyUsdzMat4ToAiMat4(animChannel.transforms.static_value.value().m).Decompose(scale, rotation, position);
  262. positionKeys.emplace_back(0, position);
  263. scalingKeys.emplace_back(0, scale);
  264. rotationKeys.emplace_back(0, rotation);
  265. }
  266. for (const auto &transformAnimSampler : animChannel.transforms.samples) {
  267. if (transformAnimSampler.t > newAiAnimation->mDuration) {
  268. newAiAnimation->mDuration = transformAnimSampler.t;
  269. }
  270. aiVector3D position;
  271. aiVector3D scale;
  272. aiQuaternion rotation;
  273. tinyUsdzMat4ToAiMat4(transformAnimSampler.value.m).Decompose(scale, rotation, position);
  274. positionKeys.emplace_back(transformAnimSampler.t, position);
  275. scalingKeys.emplace_back(transformAnimSampler.t, scale);
  276. rotationKeys.emplace_back(transformAnimSampler.t, rotation);
  277. }
  278. break;
  279. case tinyusdz::tydra::AnimationChannel::ChannelType::Translation:
  280. if (animChannel.translations.static_value.has_value()) {
  281. positionKeys.emplace_back(0, tinyUsdzScaleOrPosToAssimp(animChannel.translations.static_value.value()));
  282. }
  283. for (const auto &translationAnimSampler : animChannel.translations.samples) {
  284. if (translationAnimSampler.t > newAiAnimation->mDuration) {
  285. newAiAnimation->mDuration = translationAnimSampler.t;
  286. }
  287. positionKeys.emplace_back(translationAnimSampler.t, tinyUsdzScaleOrPosToAssimp(translationAnimSampler.value));
  288. }
  289. break;
  290. default:
  291. TINYUSDZLOGW(TAG, "Unsupported animation channel type (%s). Please update the USD importer to support this animation channel.", tinyusdzAnimChannelTypeFor(channelType).c_str());
  292. }
  293. }
  294. newAiNodeAnim->mNumPositionKeys = positionKeys.size();
  295. newAiNodeAnim->mPositionKeys = new aiVectorKey[newAiNodeAnim->mNumPositionKeys];
  296. std::move(positionKeys.begin(), positionKeys.end(), newAiNodeAnim->mPositionKeys);
  297. newAiNodeAnim->mNumRotationKeys = rotationKeys.size();
  298. newAiNodeAnim->mRotationKeys = new aiQuatKey[newAiNodeAnim->mNumRotationKeys];
  299. std::move(rotationKeys.begin(), rotationKeys.end(), newAiNodeAnim->mRotationKeys);
  300. newAiNodeAnim->mNumScalingKeys = scalingKeys.size();
  301. newAiNodeAnim->mScalingKeys = new aiVectorKey[newAiNodeAnim->mNumScalingKeys];
  302. std::move(scalingKeys.begin(), scalingKeys.end(), newAiNodeAnim->mScalingKeys);
  303. ++channelIndex;
  304. }
  305. }
  306. }
  307. void USDImporterImplTinyusdz::meshes(
  308. const tinyusdz::tydra::RenderScene &render_scene,
  309. aiScene *pScene,
  310. const std::string &nameWExt) {
  311. stringstream ss;
  312. pScene->mNumMeshes = static_cast<unsigned int>(render_scene.meshes.size());
  313. pScene->mMeshes = new aiMesh *[pScene->mNumMeshes]();
  314. ss.str("");
  315. ss << "meshes(): pScene->mNumMeshes: " << pScene->mNumMeshes;
  316. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  317. // Export meshes
  318. for (size_t meshIdx = 0; meshIdx < pScene->mNumMeshes; meshIdx++) {
  319. pScene->mMeshes[meshIdx] = new aiMesh();
  320. pScene->mMeshes[meshIdx]->mName.Set(render_scene.meshes[meshIdx].prim_name);
  321. ss.str("");
  322. ss << " mesh[" << meshIdx << "]: " <<
  323. render_scene.meshes[meshIdx].joint_and_weights.jointIndices.size() << " jointIndices, " <<
  324. render_scene.meshes[meshIdx].joint_and_weights.jointWeights.size() << " jointWeights, elementSize: " <<
  325. render_scene.meshes[meshIdx].joint_and_weights.elementSize;
  326. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  327. ss.str("");
  328. ss << " skel_id: " << render_scene.meshes[meshIdx].skel_id;
  329. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  330. if (render_scene.meshes[meshIdx].material_id > -1) {
  331. pScene->mMeshes[meshIdx]->mMaterialIndex = render_scene.meshes[meshIdx].material_id;
  332. }
  333. verticesForMesh(render_scene, pScene, meshIdx, nameWExt);
  334. facesForMesh(render_scene, pScene, meshIdx, nameWExt);
  335. // Some models infer normals from faces, but others need them e.g.
  336. // - apple "toy car" canopy normals will be wrong
  337. // - human "untitled" model (tinyusdz issue #115) will be "splotchy"
  338. normalsForMesh(render_scene, pScene, meshIdx, nameWExt);
  339. materialsForMesh(render_scene, pScene, meshIdx, nameWExt);
  340. uvsForMesh(render_scene, pScene, meshIdx, nameWExt);
  341. }
  342. }
  343. void USDImporterImplTinyusdz::verticesForMesh(
  344. const tinyusdz::tydra::RenderScene &render_scene,
  345. aiScene *pScene,
  346. size_t meshIdx,
  347. const std::string &nameWExt) {
  348. UNUSED(nameWExt);
  349. const auto numVertices = static_cast<unsigned int>(render_scene.meshes[meshIdx].points.size());
  350. pScene->mMeshes[meshIdx]->mNumVertices = numVertices;
  351. pScene->mMeshes[meshIdx]->mVertices = new aiVector3D[pScene->mMeshes[meshIdx]->mNumVertices];
  352. // Check if this is a skinned mesh
  353. if (int skeleton_id = render_scene.meshes[meshIdx].skel_id; skeleton_id > -1) {
  354. // Recursively iterate to collect all the joints in the hierarchy into a flattened array
  355. std::vector<const tinyusdz::tydra::SkelNode *> skeletonNodes;
  356. skeletonNodes.push_back(&render_scene.skeletons[skeleton_id].root_node);
  357. for (int i = 0; i < skeletonNodes.size(); ++i) {
  358. for (const auto &child : skeletonNodes[i]->children) {
  359. skeletonNodes.push_back(&child);
  360. }
  361. }
  362. // Convert USD skeleton joints to Assimp bones
  363. const unsigned int numBones = skeletonNodes.size();
  364. pScene->mMeshes[meshIdx]->mNumBones = numBones;
  365. pScene->mMeshes[meshIdx]->mBones = new aiBone *[numBones];
  366. for (unsigned int i = 0; i < numBones; ++i) {
  367. const tinyusdz::tydra::SkelNode *skeletonNode = skeletonNodes[i];
  368. const int boneIndex = skeletonNode->joint_id;
  369. // Sorted so that Assimp bone ids align with USD joint id
  370. auto outputBone = new aiBone();
  371. outputBone->mName = aiString(skeletonNode->joint_name);
  372. outputBone->mOffsetMatrix = tinyUsdzMat4ToAiMat4(skeletonNode->bind_transform.m).Inverse();
  373. pScene->mMeshes[meshIdx]->mBones[boneIndex] = outputBone;
  374. }
  375. // Vertex weights
  376. std::vector<std::vector<aiVertexWeight>> aiBonesVertexWeights;
  377. aiBonesVertexWeights.resize(numBones);
  378. const std::vector<int> &jointIndices = render_scene.meshes[meshIdx].joint_and_weights.jointIndices;
  379. const std::vector<float> &jointWeightIndices = render_scene.meshes[meshIdx].joint_and_weights.jointWeights;
  380. const int numWeightsPerVertex = render_scene.meshes[meshIdx].joint_and_weights.elementSize;
  381. for (unsigned int vertexIndex = 0; vertexIndex < numVertices; ++vertexIndex) {
  382. for (int weightIndex = 0; weightIndex < numWeightsPerVertex; ++weightIndex) {
  383. const unsigned int index = vertexIndex * numWeightsPerVertex + weightIndex;
  384. const float jointWeight = jointWeightIndices[index];
  385. if (jointWeight > 0) {
  386. const int jointIndex = jointIndices[index];
  387. aiBonesVertexWeights[jointIndex].emplace_back(vertexIndex, jointWeight);
  388. }
  389. }
  390. }
  391. for (int boneIndex = 0; boneIndex < numBones; ++boneIndex) {
  392. const unsigned int numWeightsForBone = aiBonesVertexWeights[boneIndex].size();
  393. pScene->mMeshes[meshIdx]->mBones[boneIndex]->mWeights = new aiVertexWeight[numWeightsForBone];
  394. pScene->mMeshes[meshIdx]->mBones[boneIndex]->mNumWeights = numWeightsForBone;
  395. std::swap_ranges(aiBonesVertexWeights[boneIndex].begin(), aiBonesVertexWeights[boneIndex].end(), pScene->mMeshes[meshIdx]->mBones[boneIndex]->mWeights);
  396. }
  397. } // Skinned mesh end
  398. for (size_t j = 0; j < pScene->mMeshes[meshIdx]->mNumVertices; ++j) {
  399. pScene->mMeshes[meshIdx]->mVertices[j].x = render_scene.meshes[meshIdx].points[j][0];
  400. pScene->mMeshes[meshIdx]->mVertices[j].y = render_scene.meshes[meshIdx].points[j][1];
  401. pScene->mMeshes[meshIdx]->mVertices[j].z = render_scene.meshes[meshIdx].points[j][2];
  402. }
  403. }
  404. void USDImporterImplTinyusdz::facesForMesh(
  405. const tinyusdz::tydra::RenderScene &render_scene,
  406. aiScene *pScene,
  407. size_t meshIdx,
  408. const std::string &nameWExt) {
  409. UNUSED(nameWExt);
  410. pScene->mMeshes[meshIdx]->mNumFaces = static_cast<unsigned int>(render_scene.meshes[meshIdx].faceVertexCounts().size());
  411. pScene->mMeshes[meshIdx]->mFaces = new aiFace[pScene->mMeshes[meshIdx]->mNumFaces]();
  412. size_t faceVertIdxOffset = 0;
  413. for (size_t faceIdx = 0; faceIdx < pScene->mMeshes[meshIdx]->mNumFaces; ++faceIdx) {
  414. pScene->mMeshes[meshIdx]->mFaces[faceIdx].mNumIndices = render_scene.meshes[meshIdx].faceVertexCounts()[faceIdx];
  415. pScene->mMeshes[meshIdx]->mFaces[faceIdx].mIndices = new unsigned int[pScene->mMeshes[meshIdx]->mFaces[faceIdx].mNumIndices];
  416. for (size_t j = 0; j < pScene->mMeshes[meshIdx]->mFaces[faceIdx].mNumIndices; ++j) {
  417. pScene->mMeshes[meshIdx]->mFaces[faceIdx].mIndices[j] =
  418. render_scene.meshes[meshIdx].faceVertexIndices()[j + faceVertIdxOffset];
  419. }
  420. faceVertIdxOffset += pScene->mMeshes[meshIdx]->mFaces[faceIdx].mNumIndices;
  421. }
  422. }
  423. void USDImporterImplTinyusdz::normalsForMesh(
  424. const tinyusdz::tydra::RenderScene &render_scene,
  425. aiScene *pScene,
  426. size_t meshIdx,
  427. const std::string &nameWExt) {
  428. UNUSED(nameWExt);
  429. pScene->mMeshes[meshIdx]->mNormals = new aiVector3D[pScene->mMeshes[meshIdx]->mNumVertices];
  430. const float *floatPtr = reinterpret_cast<const float *>(render_scene.meshes[meshIdx].normals.get_data().data());
  431. for (size_t vertIdx = 0, fpj = 0; vertIdx < pScene->mMeshes[meshIdx]->mNumVertices; ++vertIdx, fpj += 3) {
  432. pScene->mMeshes[meshIdx]->mNormals[vertIdx].x = floatPtr[fpj];
  433. pScene->mMeshes[meshIdx]->mNormals[vertIdx].y = floatPtr[fpj + 1];
  434. pScene->mMeshes[meshIdx]->mNormals[vertIdx].z = floatPtr[fpj + 2];
  435. }
  436. }
  437. void USDImporterImplTinyusdz::materialsForMesh(
  438. const tinyusdz::tydra::RenderScene &render_scene,
  439. aiScene *pScene,
  440. size_t meshIdx,
  441. const std::string &nameWExt) {
  442. UNUSED(render_scene); UNUSED(pScene); UNUSED(meshIdx); UNUSED(nameWExt);
  443. }
  444. void USDImporterImplTinyusdz::uvsForMesh(
  445. const tinyusdz::tydra::RenderScene &render_scene,
  446. aiScene *pScene,
  447. size_t meshIdx,
  448. const std::string &nameWExt) {
  449. UNUSED(nameWExt);
  450. const size_t uvSlotsCount = render_scene.meshes[meshIdx].texcoords.size();
  451. if (uvSlotsCount < 1) {
  452. return;
  453. }
  454. pScene->mMeshes[meshIdx]->mTextureCoords[0] = new aiVector3D[pScene->mMeshes[meshIdx]->mNumVertices];
  455. pScene->mMeshes[meshIdx]->mNumUVComponents[0] = 2; // U and V stored in "x", "y" of aiVector3D.
  456. for (unsigned int uvSlotIdx = 0; uvSlotIdx < uvSlotsCount; ++uvSlotIdx) {
  457. const auto uvsForSlot = render_scene.meshes[meshIdx].texcoords.at(uvSlotIdx);
  458. if (uvsForSlot.get_data().size() == 0) {
  459. continue;
  460. }
  461. const float *floatPtr = reinterpret_cast<const float *>(uvsForSlot.get_data().data());
  462. for (size_t vertIdx = 0, fpj = 0; vertIdx < pScene->mMeshes[meshIdx]->mNumVertices; ++vertIdx, fpj += 2) {
  463. pScene->mMeshes[meshIdx]->mTextureCoords[uvSlotIdx][vertIdx].x = floatPtr[fpj];
  464. pScene->mMeshes[meshIdx]->mTextureCoords[uvSlotIdx][vertIdx].y = floatPtr[fpj + 1];
  465. }
  466. }
  467. }
  468. static aiColor3D *ownedColorPtrFor(const std::array<float, 3> &color) {
  469. aiColor3D *colorPtr = new aiColor3D();
  470. colorPtr->r = color[0];
  471. colorPtr->g = color[1];
  472. colorPtr->b = color[2];
  473. return colorPtr;
  474. }
  475. static std::string nameForTextureWithId(
  476. const tinyusdz::tydra::RenderScene &render_scene,
  477. const int targetId) {
  478. stringstream ss;
  479. std::string texName;
  480. for (const auto &image : render_scene.images) {
  481. if (image.buffer_id == targetId) {
  482. texName = image.asset_identifier;
  483. ss.str("");
  484. ss << "nameForTextureWithId(): found texture " << texName << " with target id " << targetId;
  485. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  486. break;
  487. }
  488. }
  489. ss.str("");
  490. ss << "nameForTextureWithId(): ERROR! Failed to find texture with target id " << targetId;
  491. TINYUSDZLOGE(TAG, "%s", ss.str().c_str());
  492. return texName;
  493. }
  494. static void assignTexture(
  495. const tinyusdz::tydra::RenderScene &render_scene,
  496. const tinyusdz::tydra::RenderMaterial &material,
  497. aiMaterial *mat,
  498. const int textureId,
  499. const int aiTextureType) {
  500. UNUSED(material);
  501. std::string name = nameForTextureWithId(render_scene, textureId);
  502. aiString *texName = new aiString();
  503. texName->Set(name);
  504. stringstream ss;
  505. ss.str("");
  506. ss << "assignTexture(): name: " << name;
  507. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  508. // TODO: verify hard-coded '0' index is correct
  509. mat->AddProperty(texName, _AI_MATKEY_TEXTURE_BASE, aiTextureType, 0);
  510. }
  511. void USDImporterImplTinyusdz::materials(
  512. const tinyusdz::tydra::RenderScene &render_scene,
  513. aiScene *pScene,
  514. const std::string &nameWExt) {
  515. const size_t numMaterials{render_scene.materials.size()};
  516. (void) numMaterials; // Ignore unused variable when -Werror enabled
  517. stringstream ss;
  518. ss.str("");
  519. ss << "materials(): model" << nameWExt << ", numMaterials: " << numMaterials;
  520. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  521. pScene->mNumMaterials = 0;
  522. if (render_scene.materials.empty()) {
  523. return;
  524. }
  525. pScene->mMaterials = new aiMaterial *[render_scene.materials.size()];
  526. for (const auto &material : render_scene.materials) {
  527. ss.str("");
  528. ss << " material[" << pScene->mNumMaterials << "]: name: |" << material.name << "|, disp name: |" << material.display_name << "|";
  529. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  530. aiMaterial *mat = new aiMaterial;
  531. aiString *materialName = new aiString();
  532. materialName->Set(material.name);
  533. mat->AddProperty(materialName, AI_MATKEY_NAME);
  534. mat->AddProperty(
  535. ownedColorPtrFor(material.surfaceShader.diffuseColor.value),
  536. 1, AI_MATKEY_COLOR_DIFFUSE);
  537. mat->AddProperty(
  538. ownedColorPtrFor(material.surfaceShader.specularColor.value),
  539. 1, AI_MATKEY_COLOR_SPECULAR);
  540. mat->AddProperty(
  541. ownedColorPtrFor(material.surfaceShader.emissiveColor.value),
  542. 1, AI_MATKEY_COLOR_EMISSIVE);
  543. ss.str("");
  544. if (material.surfaceShader.diffuseColor.is_texture()) {
  545. assignTexture(render_scene, material, mat, material.surfaceShader.diffuseColor.texture_id, aiTextureType_DIFFUSE);
  546. ss << " material[" << pScene->mNumMaterials << "]: diff tex id " << material.surfaceShader.diffuseColor.texture_id << "\n";
  547. }
  548. if (material.surfaceShader.specularColor.is_texture()) {
  549. assignTexture(render_scene, material, mat, material.surfaceShader.specularColor.texture_id, aiTextureType_SPECULAR);
  550. ss << " material[" << pScene->mNumMaterials << "]: spec tex id " << material.surfaceShader.specularColor.texture_id << "\n";
  551. }
  552. if (material.surfaceShader.normal.is_texture()) {
  553. assignTexture(render_scene, material, mat, material.surfaceShader.normal.texture_id, aiTextureType_NORMALS);
  554. ss << " material[" << pScene->mNumMaterials << "]: normal tex id " << material.surfaceShader.normal.texture_id << "\n";
  555. }
  556. if (material.surfaceShader.emissiveColor.is_texture()) {
  557. assignTexture(render_scene, material, mat, material.surfaceShader.emissiveColor.texture_id, aiTextureType_EMISSIVE);
  558. ss << " material[" << pScene->mNumMaterials << "]: emissive tex id " << material.surfaceShader.emissiveColor.texture_id << "\n";
  559. }
  560. if (material.surfaceShader.occlusion.is_texture()) {
  561. assignTexture(render_scene, material, mat, material.surfaceShader.occlusion.texture_id, aiTextureType_LIGHTMAP);
  562. ss << " material[" << pScene->mNumMaterials << "]: lightmap (occlusion) tex id " << material.surfaceShader.occlusion.texture_id << "\n";
  563. }
  564. if (material.surfaceShader.metallic.is_texture()) {
  565. assignTexture(render_scene, material, mat, material.surfaceShader.metallic.texture_id, aiTextureType_METALNESS);
  566. ss << " material[" << pScene->mNumMaterials << "]: metallic tex id " << material.surfaceShader.metallic.texture_id << "\n";
  567. }
  568. if (material.surfaceShader.roughness.is_texture()) {
  569. assignTexture(render_scene, material, mat, material.surfaceShader.roughness.texture_id, aiTextureType_DIFFUSE_ROUGHNESS);
  570. ss << " material[" << pScene->mNumMaterials << "]: roughness tex id " << material.surfaceShader.roughness.texture_id << "\n";
  571. }
  572. if (material.surfaceShader.clearcoat.is_texture()) {
  573. assignTexture(render_scene, material, mat, material.surfaceShader.clearcoat.texture_id, aiTextureType_CLEARCOAT);
  574. ss << " material[" << pScene->mNumMaterials << "]: clearcoat tex id " << material.surfaceShader.clearcoat.texture_id << "\n";
  575. }
  576. if (material.surfaceShader.opacity.is_texture()) {
  577. assignTexture(render_scene, material, mat, material.surfaceShader.opacity.texture_id, aiTextureType_OPACITY);
  578. ss << " material[" << pScene->mNumMaterials << "]: opacity tex id " << material.surfaceShader.opacity.texture_id << "\n";
  579. }
  580. if (material.surfaceShader.displacement.is_texture()) {
  581. assignTexture(render_scene, material, mat, material.surfaceShader.displacement.texture_id, aiTextureType_DISPLACEMENT);
  582. ss << " material[" << pScene->mNumMaterials << "]: displacement tex id " << material.surfaceShader.displacement.texture_id << "\n";
  583. }
  584. if (material.surfaceShader.clearcoatRoughness.is_texture()) {
  585. ss << " material[" << pScene->mNumMaterials << "]: clearcoatRoughness tex id " << material.surfaceShader.clearcoatRoughness.texture_id << "\n";
  586. }
  587. if (material.surfaceShader.opacityThreshold.is_texture()) {
  588. ss << " material[" << pScene->mNumMaterials << "]: opacityThreshold tex id " << material.surfaceShader.opacityThreshold.texture_id << "\n";
  589. }
  590. if (material.surfaceShader.ior.is_texture()) {
  591. ss << " material[" << pScene->mNumMaterials << "]: ior tex id " << material.surfaceShader.ior.texture_id << "\n";
  592. }
  593. if (!ss.str().empty()) {
  594. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  595. }
  596. pScene->mMaterials[pScene->mNumMaterials] = mat;
  597. ++pScene->mNumMaterials;
  598. }
  599. }
  600. void USDImporterImplTinyusdz::textures(
  601. const tinyusdz::tydra::RenderScene &render_scene,
  602. aiScene *pScene,
  603. const std::string &nameWExt) {
  604. UNUSED(pScene);
  605. const size_t numTextures{render_scene.textures.size()};
  606. UNUSED(numTextures); // Ignore unused variable when -Werror enabled
  607. stringstream ss;
  608. ss.str("");
  609. ss << "textures(): model" << nameWExt << ", numTextures: " << numTextures;
  610. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  611. size_t i{0};
  612. UNUSED(i);
  613. for (const auto &texture : render_scene.textures) {
  614. UNUSED(texture);
  615. ss.str("");
  616. ss << " texture[" << i << "]: id: " << texture.texture_image_id << ", disp name: |" << texture.display_name << "|, varname_uv: " <<
  617. texture.varname_uv << ", prim_name: |" << texture.prim_name << "|, abs_path: |" << texture.abs_path << "|";
  618. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  619. ++i;
  620. }
  621. }
  622. /**
  623. * "owned" as in, used "new" to allocate and aiScene now responsible for "delete"
  624. *
  625. * @param render_scene renderScene object
  626. * @param image textureImage object
  627. * @param nameWExt filename w/ext (use to extract file type hint)
  628. * @return aiTexture ptr
  629. */
  630. static aiTexture *ownedEmbeddedTextureFor(
  631. const tinyusdz::tydra::RenderScene &render_scene,
  632. const tinyusdz::tydra::TextureImage &image,
  633. const std::string &nameWExt) {
  634. UNUSED(nameWExt);
  635. stringstream ss;
  636. aiTexture *tex = new aiTexture();
  637. size_t pos = image.asset_identifier.find_last_of('/');
  638. string embTexName{image.asset_identifier.substr(pos + 1)};
  639. tex->mFilename.Set(image.asset_identifier.c_str());
  640. tex->mHeight = image.height;
  641. // const size_t imageBytesCount{render_scene.buffers[image.buffer_id].data.size() / image.channels};
  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. void USDImporterImplTinyusdz::setupNodes(
  728. const tinyusdz::tydra::RenderScene &render_scene,
  729. aiScene *pScene,
  730. const std::string &nameWExt) {
  731. stringstream ss;
  732. pScene->mRootNode = nodes(render_scene, nameWExt);
  733. if (pScene->mRootNode == nullptr) {
  734. return;
  735. }
  736. pScene->mRootNode->mNumMeshes = pScene->mNumMeshes;
  737. pScene->mRootNode->mMeshes = new unsigned int[pScene->mRootNode->mNumMeshes];
  738. ss.str("");
  739. ss << "setupNodes(): pScene->mNumMeshes: " << pScene->mNumMeshes;
  740. ss << ", mRootNode->mNumMeshes: " << pScene->mRootNode->mNumMeshes;
  741. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  742. for (unsigned int meshIdx = 0; meshIdx < pScene->mNumMeshes; meshIdx++) {
  743. pScene->mRootNode->mMeshes[meshIdx] = meshIdx;
  744. }
  745. }
  746. aiNode *USDImporterImplTinyusdz::nodes(
  747. const tinyusdz::tydra::RenderScene &render_scene,
  748. const std::string &nameWExt) {
  749. const size_t numNodes{render_scene.nodes.size()};
  750. (void) numNodes; // Ignore unused variable when -Werror enabled
  751. stringstream ss;
  752. ss.str("");
  753. ss << "nodes(): model" << nameWExt << ", numNodes: " << numNodes;
  754. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  755. aiNode *rootNode = nodesRecursive(nullptr, render_scene.nodes[0], render_scene.skeletons);
  756. return rootNode;
  757. }
  758. using Assimp::tinyusdzNodeTypeFor;
  759. using Assimp::tinyUsdzMat4ToAiMat4;
  760. using tinyusdz::tydra::NodeType;
  761. aiNode *USDImporterImplTinyusdz::nodesRecursive(
  762. aiNode *pNodeParent,
  763. const tinyusdz::tydra::Node &node,
  764. const std::vector<tinyusdz::tydra::SkelHierarchy> &skeletons) {
  765. stringstream ss;
  766. aiNode *cNode = new aiNode();
  767. cNode->mParent = pNodeParent;
  768. cNode->mName.Set(node.prim_name);
  769. cNode->mTransformation = tinyUsdzMat4ToAiMat4(node.local_matrix.m);
  770. ss.str("");
  771. ss << "nodesRecursive(): node " << cNode->mName.C_Str() <<
  772. " type: |" << tinyusdzNodeTypeFor(node.nodeType) <<
  773. "|, disp " << node.display_name << ", abs " << node.abs_path;
  774. if (cNode->mParent != nullptr) {
  775. ss << " (parent " << cNode->mParent->mName.C_Str() << ")";
  776. }
  777. ss << " has " << node.children.size() << " children";
  778. if (node.id != -1) {
  779. ss << "\n node mesh id: " << node.id << " (node type: " << tinyusdzNodeTypeFor(node.nodeType) << ")";
  780. }
  781. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  782. unsigned int numChildren = node.children.size();
  783. // Find any tinyusdz skeletons which might begin at this node
  784. // Add the skeleton bones as child nodes
  785. const tinyusdz::tydra::SkelNode *skelNode = nullptr;
  786. for (const auto &skeleton : skeletons) {
  787. if (skeleton.abs_path == node.abs_path) {
  788. // Add this skeleton's bones as child nodes
  789. ++numChildren;
  790. skelNode = &skeleton.root_node;
  791. break;
  792. }
  793. }
  794. cNode->mNumChildren = numChildren;
  795. // Done. No more children.
  796. if (numChildren == 0) {
  797. return cNode;
  798. }
  799. cNode->mChildren = new aiNode *[cNode->mNumChildren];
  800. size_t i{ 0 };
  801. for (const auto &childNode : node.children) {
  802. cNode->mChildren[i] = nodesRecursive(cNode, childNode, skeletons);
  803. ++i;
  804. }
  805. if (skelNode != nullptr) {
  806. // Convert USD skeleton into an Assimp node and make it the last child
  807. cNode->mChildren[cNode->mNumChildren-1] = skeletonNodesRecursive(cNode, *skelNode);
  808. }
  809. return cNode;
  810. }
  811. aiNode *USDImporterImplTinyusdz::skeletonNodesRecursive(
  812. aiNode* pNodeParent,
  813. const tinyusdz::tydra::SkelNode& joint) {
  814. auto *cNode = new aiNode(joint.joint_path);
  815. cNode->mParent = pNodeParent;
  816. cNode->mNumMeshes = 0; // not a mesh node
  817. cNode->mTransformation = tinyUsdzMat4ToAiMat4(joint.rest_transform.m);
  818. // Done. No more children.
  819. if (joint.children.empty()) {
  820. return cNode;
  821. }
  822. cNode->mNumChildren = static_cast<unsigned int>(joint.children.size());
  823. cNode->mChildren = new aiNode *[cNode->mNumChildren];
  824. for (int i = 0; i < cNode->mNumChildren; ++i) {
  825. const tinyusdz::tydra::SkelNode &childJoint = joint.children[i];
  826. cNode->mChildren[i] = skeletonNodesRecursive(cNode, childJoint);
  827. }
  828. return cNode;
  829. }
  830. void USDImporterImplTinyusdz::sanityCheckNodesRecursive(
  831. aiNode *cNode) {
  832. stringstream ss;
  833. ss.str("");
  834. ss << "sanityCheckNodesRecursive(): node " << cNode->mName.C_Str();
  835. if (cNode->mParent != nullptr) {
  836. ss << " (parent " << cNode->mParent->mName.C_Str() << ")";
  837. }
  838. ss << " has " << cNode->mNumChildren << " children";
  839. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  840. for (size_t i = 0; i < cNode->mNumChildren; ++i) {
  841. sanityCheckNodesRecursive(cNode->mChildren[i]);
  842. }
  843. }
  844. void USDImporterImplTinyusdz::setupBlendShapes(
  845. const tinyusdz::tydra::RenderScene &render_scene,
  846. aiScene *pScene,
  847. const std::string &nameWExt) {
  848. stringstream ss;
  849. ss.str("");
  850. ss << "setupBlendShapes(): iterating over " << pScene->mNumMeshes << " meshes for model" << nameWExt;
  851. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  852. for (size_t meshIdx = 0; meshIdx < pScene->mNumMeshes; meshIdx++) {
  853. blendShapesForMesh(render_scene, pScene, meshIdx, nameWExt);
  854. }
  855. }
  856. void USDImporterImplTinyusdz::blendShapesForMesh(
  857. const tinyusdz::tydra::RenderScene &render_scene,
  858. aiScene *pScene,
  859. size_t meshIdx,
  860. const std::string &nameWExt) {
  861. UNUSED(nameWExt);
  862. stringstream ss;
  863. const unsigned int numBlendShapeTargets{static_cast<unsigned int>(render_scene.meshes[meshIdx].targets.size())};
  864. UNUSED(numBlendShapeTargets); // Ignore unused variable when -Werror enabled
  865. ss.str("");
  866. ss << " blendShapesForMesh(): mesh[" << meshIdx << "], numBlendShapeTargets: " << numBlendShapeTargets;
  867. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  868. if (numBlendShapeTargets > 0) {
  869. pScene->mMeshes[meshIdx]->mNumAnimMeshes = numBlendShapeTargets;
  870. pScene->mMeshes[meshIdx]->mAnimMeshes = new aiAnimMesh *[pScene->mMeshes[meshIdx]->mNumAnimMeshes];
  871. }
  872. auto mapIter = render_scene.meshes[meshIdx].targets.begin();
  873. size_t animMeshIdx{0};
  874. for (; mapIter != render_scene.meshes[meshIdx].targets.end(); ++mapIter) {
  875. const std::string name{mapIter->first};
  876. const tinyusdz::tydra::ShapeTarget shapeTarget{mapIter->second};
  877. pScene->mMeshes[meshIdx]->mAnimMeshes[animMeshIdx] = aiCreateAnimMesh(pScene->mMeshes[meshIdx]);
  878. ss.str("");
  879. ss << " mAnimMeshes[" << animMeshIdx << "]: mNumVertices: " << pScene->mMeshes[meshIdx]->mAnimMeshes[animMeshIdx]->mNumVertices <<
  880. ", target: " << shapeTarget.pointIndices.size() << " pointIndices, " << shapeTarget.pointOffsets.size() <<
  881. " pointOffsets, " << shapeTarget.normalOffsets.size() << " normalOffsets";
  882. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  883. for (size_t iVert = 0; iVert < shapeTarget.pointOffsets.size(); ++iVert) {
  884. pScene->mMeshes[meshIdx]->mAnimMeshes[animMeshIdx]->mVertices[shapeTarget.pointIndices[iVert]] +=
  885. tinyUsdzScaleOrPosToAssimp(shapeTarget.pointOffsets[iVert]);
  886. }
  887. for (size_t iVert = 0; iVert < shapeTarget.normalOffsets.size(); ++iVert) {
  888. pScene->mMeshes[meshIdx]->mAnimMeshes[animMeshIdx]->mNormals[shapeTarget.pointIndices[iVert]] +=
  889. tinyUsdzScaleOrPosToAssimp(shapeTarget.normalOffsets[iVert]);
  890. }
  891. ss.str("");
  892. ss << " target[" << animMeshIdx << "]: name: " << name << ", prim_name: " <<
  893. shapeTarget.prim_name << ", abs_path: " << shapeTarget.abs_path <<
  894. ", display_name: " << shapeTarget.display_name << ", " << shapeTarget.pointIndices.size() <<
  895. " pointIndices, " << shapeTarget.pointOffsets.size() << " pointOffsets, " <<
  896. shapeTarget.normalOffsets.size() << " normalOffsets, " << shapeTarget.inbetweens.size() <<
  897. " inbetweens";
  898. TINYUSDZLOGD(TAG, "%s", ss.str().c_str());
  899. ++animMeshIdx;
  900. }
  901. }
  902. } // namespace Assimp
  903. #endif // !! ASSIMP_BUILD_NO_USD_IMPORTER