diff --git a/ios/src/morph/GPUMorphHelper.cpp b/ios/src/morph/GPUMorphHelper.cpp index 0f8efb77..c094b57c 100644 --- a/ios/src/morph/GPUMorphHelper.cpp +++ b/ios/src/morph/GPUMorphHelper.cpp @@ -49,7 +49,7 @@ namespace gltfio { free(mem); }; - GPUMorphHelper::GPUMorphHelper(FFilamentAsset *asset, const char* meshName, const char* entityName, const char* materialInstanceName) : mAsset(asset) { + GPUMorphHelper::GPUMorphHelper(FFilamentAsset *asset, const char* meshName, const char* entityName, int primitiveIndex) : mAsset(asset) { cgltf_size num_primitives = 0; NodeMap &sourceNodes = asset->isInstanced() ? asset->mInstances[0]->nodeMap @@ -70,6 +70,14 @@ namespace gltfio { } } } + auto materialInstances = mAsset->getMaterialInstances(); + + std::cout << "Listing all material instances in asset" << std::endl; + for(int i = 0; i < mAsset->getMaterialInstanceCount(); i++) { + const char* name = materialInstances[i]->getName(); + std::cout << "Material : " << name << std::endl; + } + createTextures(); } @@ -85,8 +93,13 @@ namespace gltfio { auto materialInstances = mAsset->getMaterialInstances(); auto &engine = *(mAsset->mEngine); - for (auto &entry : mMorphTable) { - for (auto prim : entry.second.primitives) { + for (auto&& entry : mMorphTable) { + std::vector& prims = (std::vector&)entry.second.primitives; + for(int i = 0; i < prims.size(); i++) { +// prims.at(i).texture = nullptr; + auto& prim = prims.at(i); +// } +// for (auto&& prim : entry.second.primitives) { // for a single morph target, each vertex will be assigned 2 pixels, corresponding to a position vec3 and a normal vec3 // these two vectors will be laid out adjacent in memory // the total texture "width" is the total number of these pixels @@ -119,15 +132,17 @@ namespace gltfio { offset += int(target.bufferSize / sizeof(float)); } } - - prim.texture = Texture::Builder() - .width(textureWidth) // - .height(1) - .depth(prim.numTargets) - .sampler(Texture::Sampler::SAMPLER_2D_ARRAY) - .format(Texture::InternalFormat::RGB32F) - .levels(0x01) - .build(engine); + + Texture* texture = Texture::Builder() + .width(textureWidth) // + .height(1) + .depth(prim.numTargets) + .sampler(Texture::Sampler::SAMPLER_2D_ARRAY) + .format(Texture::InternalFormat::RGB32F) + .levels(0x01) + .build(engine); + + prim.texture = texture; //std::unique_ptr(texture); Texture::PixelBufferDescriptor descriptor( textureBuffer, @@ -140,9 +155,10 @@ namespace gltfio { for(int i = 0; i < mAsset->getMaterialInstanceCount(); i++) { const char* name = materialInstances[i]->getName(); - std::cout << name << std::endl; + if(strcmp(name, prim.materialName) == 0) { - prim.materialInstance = materialInstances[i]; + std::cout << "Found material instance for primitive under name : " << name << std::endl; + prim.materialInstance = materialInstances[i]; //std::unique_ptr(materialInstances[i]); break; } } @@ -162,9 +178,21 @@ namespace gltfio { } void GPUMorphHelper::applyWeights(float const *weights, size_t count, int primitiveIndex) noexcept { - auto materialInstance = mAsset->getMaterialInstances()[primitiveIndex]; - materialInstance->setParameter("morphTargetWeights", weights, count); -// assert(count <= numTargets); + std::cout << "Applying " << count << " weights to primitive index " << primitiveIndex << std::endl; + for (auto &entry : mMorphTable) { + int i = 0 ; + for (auto &prim : entry.second.primitives) { + if(i == primitiveIndex) { + if(!prim.materialInstance) { + std::cout << "Error, couldn't find material instance for primitive under name " << prim.materialName << std::endl; + } else { + prim.materialInstance->setParameter("morphTargetWeights", weights, count); + break; + } + } + i++; + } + } } void @@ -181,7 +209,7 @@ namespace gltfio { morphHelperPrim.materialName = prim.material->name; morphHelperPrim.numTargets = prim.targets_count; morphHelperPrim.numVertices = vertexBuffer->getVertexCount(); - cgltf_size maxIndex = 0; + cgltf_size maxIndex = 0; for(int i = 0; i < prim.indices->count; i++) { maxIndex = std::max(cgltf_accessor_read_index(prim.indices, i), maxIndex); } diff --git a/ios/src/morph/GPUMorphHelper.h b/ios/src/morph/GPUMorphHelper.h index 5c0fb586..cddea90f 100644 --- a/ios/src/morph/GPUMorphHelper.h +++ b/ios/src/morph/GPUMorphHelper.h @@ -58,12 +58,14 @@ namespace gltfio { struct GltfPrimitive { filament::VertexBuffer *vertexBuffer; +// const std::unique_ptr texture; Texture* texture; std::vector targets; // TODO: flatten this? const char* materialName; cgltf_size numTargets = 0; cgltf_size numVertices = 0; - MaterialInstance* materialInstance = nullptr; + //const std::unique_ptr materialInstance; + MaterialInstance* materialInstance; }; struct TableEntry {