diff --git a/cocos/3d/models/baked-skinning-model.ts b/cocos/3d/models/baked-skinning-model.ts index d0cd13740d..9a35fdc223 100644 --- a/cocos/3d/models/baked-skinning-model.ts +++ b/cocos/3d/models/baked-skinning-model.ts @@ -106,6 +106,13 @@ export class BakedSkinningModel extends MorphModel { UBOSkinningTexture.SIZE, )); } + + // The animation buffer belongs to the skinning root. Refresh its binding without + // rebuilding the instanced attribute block, which would erase custom attribute values. + for (let i = 0; i < this._subModels.length; i++) { + this._updateLocalDescriptors(i, this._subModels[i].descriptorSet); + } + this.updateInstancedJointTextureInfo(); } // Override diff --git a/cocos/3d/models/skinning-model.ts b/cocos/3d/models/skinning-model.ts index 192342bfd5..fa7b4de795 100644 --- a/cocos/3d/models/skinning-model.ts +++ b/cocos/3d/models/skinning-model.ts @@ -142,6 +142,7 @@ export class SkinningModel extends MorphModel { } this._bufferIndices = null; this._joints.length = 0; if (!skeleton || !skinningRoot || !mesh) { return; } + const prevRealTimeTextureMode = this._realTimeTextureMode; this._realTimeTextureMode = false; if (UBOSkinning.JOINT_UNIFORM_CAPACITY < skeleton.joints.length) { this._realTimeTextureMode = true; } this.transform = skinningRoot; @@ -161,6 +162,15 @@ export class SkinningModel extends MorphModel { if (!jointMaps) { indices.push(index); buffers.push(0); } else { getRelevantBuffers(indices, buffers, jointMaps, index); } this._joints.push({ indices, buffers, bound, target, bindpose, transform }); } + + // Rebind recreated joint resources without resetting user-provided instanced attributes. + const subModelCount = Math.min(this._subModels.length, this._bufferIndices.length); + if (subModelCount && prevRealTimeTextureMode !== this._realTimeTextureMode) { + this.onMacroPatchesStateChanged(); + } + for (let i = 0; i < subModelCount; i++) { + this._updateLocalDescriptors(i, this._subModels[i].descriptorSet); + } } /** diff --git a/native/cocos/3d/models/BakedSkinningModel.cpp b/native/cocos/3d/models/BakedSkinningModel.cpp index 4783185c16..4e4d74bcc0 100644 --- a/native/cocos/3d/models/BakedSkinningModel.cpp +++ b/native/cocos/3d/models/BakedSkinningModel.cpp @@ -86,6 +86,12 @@ void BakedSkinningModel::bindSkeleton(Skeleton *skeleton, Node *skinningRoot, Me pipeline::UBOSkinning::size, }); } + + // JSB synchronizes the new root's animation buffer before calling bindSkeleton. + for (index_t i = 0; i < _subModels.size(); ++i) { + updateLocalDescriptors(i, _subModels[i]->getDescriptorSet()); + } + updateInstancedJointTextureInfo(); } void BakedSkinningModel::updateTransform(uint32_t stamp) { diff --git a/native/cocos/3d/models/SkinningModel.cpp b/native/cocos/3d/models/SkinningModel.cpp index 28d1e5b977..6eaaf69464 100644 --- a/native/cocos/3d/models/SkinningModel.cpp +++ b/native/cocos/3d/models/SkinningModel.cpp @@ -23,6 +23,8 @@ ****************************************************************************/ #include "3d/models/SkinningModel.h" +#include + #include #include "3d/assets/Mesh.h" @@ -80,6 +82,7 @@ void SkinningModel::bindSkeleton(Skeleton *skeleton, Node *skinningRoot, Mesh *m _joints.clear(); if (!skeleton || !skinningRoot || !mesh) return; + const bool prevRealTimeTextureMode = _realTimeTextureMode; auto jointCount = static_cast(skeleton->getJoints().size()); _realTimeTextureMode = pipeline::SkinningJointCapacity::jointUniformCapacity < jointCount; setTransform(skinningRoot); @@ -113,6 +116,15 @@ void SkinningModel::bindSkeleton(Skeleton *skeleton, Node *skinningRoot, Mesh *m jointInfo.indices = std::move(indices); _joints.emplace_back(std::move(jointInfo)); } + + // Rebind recreated resources without resetting custom instanced attributes. + const auto subModelCount = std::min(_subModels.size(), _bufferIndices.size()); + if (subModelCount && prevRealTimeTextureMode != _realTimeTextureMode) { + onMacroPatchesStateChanged(); + } + for (index_t i = 0; i < subModelCount; ++i) { + updateLocalDescriptors(i, _subModels[i]->getDescriptorSet()); + } } void SkinningModel::updateTransform(uint32_t stamp) {