From e46ad93c2aaa3052aee1e84f5351621fa6fc44d9 Mon Sep 17 00:00:00 2001 From: Leslie Leigh Date: Tue, 2 Apr 2019 19:36:25 +0800 Subject: [PATCH] fix multi submodel skinning model component bug (#4127) --- .../3d/framework/skinning-model-component.ts | 2 +- cocos/renderer/models/skinning-model.ts | 46 +++++++++++-------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/cocos/3d/framework/skinning-model-component.ts b/cocos/3d/framework/skinning-model-component.ts index 329fbf1e18..e91ec02e78 100644 --- a/cocos/3d/framework/skinning-model-component.ts +++ b/cocos/3d/framework/skinning-model-component.ts @@ -146,7 +146,7 @@ export class SkinningModelComponent extends ModelComponent { const device = _getGlobalDevice(); const useJointTexture = !__FORCE_USE_UNIFORM_STORAGE__ && device !== null && device.hasFeature(GFXFeature.TEXTURE_FLOAT); - const mat = this.getMaterial(0, CC_EDITOR)!; + const mat = this.getMaterial(index, CC_EDITOR)!; const effectAsset = mat.effectAsset; mat.destroy(); mat.initialize({ effectAsset, defines: { CC_USE_SKINNING: true, CC_USE_JOINTS_TEXTURE: useJointTexture } }); diff --git a/cocos/renderer/models/skinning-model.ts b/cocos/renderer/models/skinning-model.ts index f2bc0ea78b..9572557da7 100644 --- a/cocos/renderer/models/skinning-model.ts +++ b/cocos/renderer/models/skinning-model.ts @@ -134,30 +134,38 @@ function _setJointMatrix (out: Float32Array, iMatrix: number, matrix: mat4) { out[16 * iMatrix + 15] = matrix.m15; } +const jointTextureCapacityTable = ((maxCapacity: number) => { + const pixelsPerJoint = 4; + const result: Array<{ textureExtent: number; capacity: number; }> = []; + for (let i = 0; ; ++i) { + const textureExtent = 1 << i; + const capacity = (textureExtent * textureExtent) / pixelsPerJoint; + if (capacity < 1) { + continue; + } + result.push({ textureExtent, capacity }); + if (capacity >= maxCapacity) { + break; + } + } + return result; +})(1024); + function _createJointsTexture (skinning: { joints: any[]; }) { const jointCount = skinning.joints.length; - - // Set jointsTexture. - // A squared texture with side length N(N > 1) multiples of 2 can store - // 2 ^ (2 * N - 2) matrices. - // We support most 1024 joints. - let size = 0; - if (jointCount > 1024) { - throw new Error('To many joints(more than 1024).'); - } else if (jointCount > 256) { - size = 64; - } else if (jointCount > 64) { - size = 32; - } else if (jointCount > 16) { - size = 16; - } else if (jointCount > 4) { - size = 8; - } else { - size = 4; + let textureExtent = -1; + for (const item of jointTextureCapacityTable) { + if (item.capacity >= jointCount) { + textureExtent = item.textureExtent; + break; + } + } + if (textureExtent < 0) { + throw new Error('To many joints.'); } const texture = new Texture2D(); - texture.create(size, size, PixelFormat.RGBA32F); + texture.create(textureExtent, textureExtent, PixelFormat.RGBA32F); texture.setFilters(Filter.NEAREST, Filter.NEAREST); texture.setWrapMode(WrapMode.CLAMP_TO_EDGE, WrapMode.CLAMP_TO_EDGE); return texture;