From b6f82ad6ffd998c3e3b5279da056d27ede99d37e Mon Sep 17 00:00:00 2001 From: jerrywwl Date: Fri, 26 Apr 2019 10:27:02 +0800 Subject: [PATCH] fix compressed texture creation bug --- cocos/3d/assets/mesh.ts | 2 +- cocos/3d/framework/avatar-model-component.ts | 41 ++++++++++++++------ cocos/3d/ui/assembler/label/letter-font.ts | 2 + cocos/gfx/webgl/webgl-commands.ts | 15 ++++--- cocos/gfx/webgl2/webgl2-commands.ts | 13 +++++-- 5 files changed, 51 insertions(+), 22 deletions(-) diff --git a/cocos/3d/assets/mesh.ts b/cocos/3d/assets/mesh.ts index d83465b22d..73615afada 100644 --- a/cocos/3d/assets/mesh.ts +++ b/cocos/3d/assets/mesh.ts @@ -281,7 +281,7 @@ export class Mesh extends Asset { srcOffset = bundle.view.offset; dstOffset = dstBundle.view.offset; - vertStride = bundle.view.stride; + vertStride = bundle.view.stride + 4; vertCount = bundle.view.count + dstBundle.view.count; vb = new ArrayBuffer(vertCount * vertStride); diff --git a/cocos/3d/framework/avatar-model-component.ts b/cocos/3d/framework/avatar-model-component.ts index c58c9a1c9c..31df3fb5ee 100644 --- a/cocos/3d/framework/avatar-model-component.ts +++ b/cocos/3d/framework/avatar-model-component.ts @@ -23,16 +23,16 @@ ****************************************************************************/ import { Texture2D } from '../../assets'; +import { Filter, PixelFormat } from '../../assets/asset-enum'; import { Rect } from '../../core'; import { ccclass, executeInEditMode, executionOrder, menu, property } from '../../core/data/class-decorator'; +import { GFXBufferTextureCopy } from '../../gfx/define'; import { GFXDevice } from '../../gfx/device'; import { Mesh } from '../assets'; import { SkinningModelComponent } from './skinning-model-component'; -import { GFXTextureCopy } from '../../gfx/define'; -import { Filter, PixelFormat } from '../../assets/asset-enum'; export interface IAvatarUnit { - mesh: Mesh; + mesh: Mesh | null; region: Rect; albedoMap: Texture2D | null; alphaMap: Texture2D | null; @@ -71,6 +71,8 @@ export class AvatarModelComponent extends SkinningModelComponent { public onLoad () { super.onLoad(); + this._mesh = new Mesh(); + this._combinedTex = new Texture2D(); this._combinedTex.setFilters(Filter.LINEAR, Filter.LINEAR); this.resizeCombiendTexture(); @@ -90,20 +92,33 @@ export class AvatarModelComponent extends SkinningModelComponent { public combine () { const texImages: TexImageSource[] = []; - const texImageRegions: GFXTextureCopy[] = []; + const texImageRegions: GFXBufferTextureCopy[] = []; const texBuffers: ArrayBuffer[] = []; - const texBufferRegions: GFXTextureCopy[] = []; + const texBufferRegions: GFXBufferTextureCopy[] = []; + + this._mesh for (const unit of this._units) { if (unit) { + + // merge mesh + if (unit.mesh) { + for (let i = 0; i < unit.mesh.subMeshCount; ++i) { + const subMesh = unit.mesh.getSubMesh(i); + } + } + + // merge textures const isValid = (unit.region.x > 0 && unit.region.y > 0) && (unit.region.width > 0 && unit.region.height > 0) && (unit.region.x + unit.region.width) <= this._combinedTexSize && (unit.region.y + unit.region.height) <= this._combinedTexSize; - if (isValid && unit.albedoMap && unit.albedoMap.image && unit.albedoMap.image.data) { - - const region = new GFXTextureCopy(); + const region = new GFXBufferTextureCopy(); + region.texOffset.x = unit.region.x; + region.texOffset.y = unit.region.y; + region.texExtent.width = unit.region.width; + region.texExtent.height = unit.region.height; const data = unit.albedoMap.image.data; if (data instanceof HTMLCanvasElement || data instanceof HTMLImageElement) { @@ -119,10 +134,14 @@ export class AvatarModelComponent extends SkinningModelComponent { const gfxTex = this._combinedTex!.getGFXTexture(); const device: GFXDevice = cc.director.root.device; - for (const buff of texBuffers) { - // device.copyBuffersToTexture(texBuffers, gfxTex!, ); + + if (texBuffers.length > 0) { + device.copyTexImagesToTexture(texImages, gfxTex!, texImageRegions); + } + + if (texBuffers.length > 0) { + device.copyBuffersToTexture(texBuffers, gfxTex!, texBufferRegions); } - // device.copyTexImagesToTexture(); } private resizeCombiendTexture () { diff --git a/cocos/3d/ui/assembler/label/letter-font.ts b/cocos/3d/ui/assembler/label/letter-font.ts index 4bffdcdbfd..22ab4e279c 100644 --- a/cocos/3d/ui/assembler/label/letter-font.ts +++ b/cocos/3d/ui/assembler/label/letter-font.ts @@ -309,11 +309,13 @@ export class LetterAtlas { this._letterDefinitions.set(letterTexture.hash, letterDefinition); + /* const region = new GFXBufferTextureCopy(); region.texOffset.x = letterDefinition.offsetX; region.texOffset.y = letterDefinition.offsetY; region.texExtent.width = letterDefinition.w; region.texExtent.height = letterDefinition.h; + */ return letterDefinition; } diff --git a/cocos/gfx/webgl/webgl-commands.ts b/cocos/gfx/webgl/webgl-commands.ts index 27700195dd..b9573e0f2b 100644 --- a/cocos/gfx/webgl/webgl-commands.ts +++ b/cocos/gfx/webgl/webgl-commands.ts @@ -24,6 +24,7 @@ import { IGFXFormatInfo, IGFXRect, IGFXViewport, + GFXFormatSize, } from '../define'; import { WebGLGFXCommandAllocator } from './webgl-command-allocator'; import { @@ -794,8 +795,9 @@ export function WebGLCmdFuncCreateTexture (device: WebGLGFXDevice, gpuTexture: W h = Math.max(1, h >> 1); } } else { - const view: ArrayBufferView = { buffer: new ArrayBuffer(0), byteLength: 0, byteOffset: 0 }; for (let i = 0; i < gpuTexture.mipLevel; ++i) { + const imgSize = GFXFormatSize(gpuTexture.format, w, h, 1); + const view: ArrayBufferView = { buffer: new ArrayBuffer(imgSize), byteLength: imgSize, byteOffset: 0 }; gl.compressedTexImage2D(WebGLRenderingContext.TEXTURE_2D, i, gpuTexture.glInternelFmt, w, h, 0, view); w = Math.max(1, w >> 1); h = Math.max(1, h >> 1); @@ -847,12 +849,12 @@ export function WebGLCmdFuncCreateTexture (device: WebGLGFXDevice, gpuTexture: W } } } else { - const view: ArrayBufferView = { buffer: new ArrayBuffer(0), byteLength: 0, byteOffset: 0 }; - for (let f = 0; f < 6; ++f) { let w = gpuTexture.width; let h = gpuTexture.height; for (let i = 0; i < gpuTexture.mipLevel; ++i) { + const imgSize = GFXFormatSize(gpuTexture.format, w, h, 1); + const view: ArrayBufferView = { buffer: new ArrayBuffer(imgSize), byteLength: imgSize, byteOffset: 0 }; gl.compressedTexImage2D(WebGLRenderingContext.TEXTURE_CUBE_MAP_POSITIVE_X + f, i, gpuTexture.glInternelFmt, w, h, 0, view); w = Math.max(1, w >> 1); h = Math.max(1, h >> 1); @@ -929,8 +931,9 @@ export function WebGLCmdFuncResizeTexture (device: WebGLGFXDevice, gpuTexture: W h = Math.max(1, h >> 1); } } else { - const view: ArrayBufferView = { buffer: new ArrayBuffer(0), byteLength: 0, byteOffset: 0 }; for (let i = 0; i < gpuTexture.mipLevel; ++i) { + const imgSize = GFXFormatSize(gpuTexture.format, w, h, 1); + const view: ArrayBufferView = { buffer: new ArrayBuffer(imgSize), byteLength: imgSize, byteOffset: 0 }; gl.compressedTexImage2D(gl.TEXTURE_2D, i, gpuTexture.glInternelFmt, w, h, 0, view); w = Math.max(1, w >> 1); h = Math.max(1, h >> 1); @@ -961,12 +964,12 @@ export function WebGLCmdFuncResizeTexture (device: WebGLGFXDevice, gpuTexture: W } } } else { - const view: ArrayBufferView = { buffer: new ArrayBuffer(0), byteLength: 0, byteOffset: 0 }; - for (let f = 0; f < 6; ++f) { let w = gpuTexture.width; let h = gpuTexture.height; for (let i = 0; i < gpuTexture.mipLevel; ++i) { + const imgSize = GFXFormatSize(gpuTexture.format, w, h, 1); + const view: ArrayBufferView = { buffer: new ArrayBuffer(imgSize), byteLength: imgSize, byteOffset: 0 }; gl.compressedTexImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + f, i, gpuTexture.glInternelFmt, w, h, 0, view); w = Math.max(1, w >> 1); h = Math.max(1, h >> 1); diff --git a/cocos/gfx/webgl2/webgl2-commands.ts b/cocos/gfx/webgl2/webgl2-commands.ts index 4f88e646a9..5b3bc74231 100644 --- a/cocos/gfx/webgl2/webgl2-commands.ts +++ b/cocos/gfx/webgl2/webgl2-commands.ts @@ -25,6 +25,7 @@ import { IGFXRect, IGFXViewport, GFXFormatType, + GFXFormatSize, } from '../define'; import { WebGL2GFXCommandAllocator } from './webgl2-command-allocator'; import { @@ -972,7 +973,8 @@ export function WebGL2CmdFuncCreateTexture (device: WebGL2GFXDevice, gpuTexture: } else { const view: ArrayBufferView = { buffer: new ArrayBuffer(0), byteLength: 0, byteOffset: 0 }; for (let i = 0; i < gpuTexture.mipLevel; ++i) { - gl.compressedTexImage2D(WebGL2RenderingContext.TEXTURE_2D, i, gpuTexture.glInternelFmt, w, h, 0, view); + const imgSize = GFXFormatSize(gpuTexture.format, w, h, 1); + gl.compressedTexImage2D(WebGL2RenderingContext.TEXTURE_2D, i, gpuTexture.glInternelFmt, w, h, 0, imgSize, 0); w = Math.max(1, w >> 1); h = Math.max(1, h >> 1); } @@ -1038,7 +1040,8 @@ export function WebGL2CmdFuncCreateTexture (device: WebGL2GFXDevice, gpuTexture: let w = gpuTexture.width; let h = gpuTexture.height; for (let i = 0; i < gpuTexture.mipLevel; ++i) { - gl.compressedTexImage2D(WebGL2RenderingContext.TEXTURE_CUBE_MAP_POSITIVE_X + f, i, gpuTexture.glInternelFmt, w, h, 0, view); + const imgSize = GFXFormatSize(gpuTexture.format, w, h, 1); + gl.compressedTexImage2D(WebGL2RenderingContext.TEXTURE_CUBE_MAP_POSITIVE_X + f, i, gpuTexture.glInternelFmt, w, h, 0, imgSize, 0); w = Math.max(1, w >> 1); h = Math.max(1, h >> 1); } @@ -1117,7 +1120,8 @@ export function WebGL2CmdFuncResizeTexture (device: WebGL2GFXDevice, gpuTexture: } else { const view: ArrayBufferView = { buffer: new ArrayBuffer(0), byteLength: 0, byteOffset: 0 }; for (let i = 0; i < gpuTexture.mipLevel; ++i) { - gl.compressedTexImage2D(WebGL2RenderingContext.TEXTURE_2D, i, gpuTexture.glInternelFmt, w, h, 0, view); + const imgSize = GFXFormatSize(gpuTexture.format, w, h, 1); + gl.compressedTexImage2D(WebGL2RenderingContext.TEXTURE_2D, i, gpuTexture.glInternelFmt, w, h, 0, imgSize, 0); w = Math.max(1, w >> 1); h = Math.max(1, h >> 1); } @@ -1164,7 +1168,8 @@ export function WebGL2CmdFuncResizeTexture (device: WebGL2GFXDevice, gpuTexture: let w = gpuTexture.width; let h = gpuTexture.height; for (let i = 0; i < gpuTexture.mipLevel; ++i) { - gl.compressedTexImage2D(WebGL2RenderingContext.TEXTURE_CUBE_MAP_POSITIVE_X + f, i, gpuTexture.glInternelFmt, w, h, 0, view); + const imgSize = GFXFormatSize(gpuTexture.format, w, h, 1); + gl.compressedTexImage2D(WebGL2RenderingContext.TEXTURE_CUBE_MAP_POSITIVE_X + f, i, gpuTexture.glInternelFmt, w, h, 0, imgSize, 0); w = Math.max(1, w >> 1); h = Math.max(1, h >> 1); }