diff --git a/cocos/3d/assets/mesh.ts b/cocos/3d/assets/mesh.ts index 4efb6e549e..747c1c3418 100644 --- a/cocos/3d/assets/mesh.ts +++ b/cocos/3d/assets/mesh.ts @@ -125,10 +125,7 @@ export interface IRenderingSubmesh { export class RenderingMesh { public constructor ( - private _subMeshes: IRenderingSubmesh[], - private _vertexBuffers: GFXBuffer[], - private _indexBuffers: GFXBuffer[]) { - + private _subMeshes: IRenderingSubmesh[]) { } public get subMeshes (): IRenderingSubmesh[] { @@ -143,16 +140,20 @@ export class RenderingMesh { return this._subMeshes[index]; } - public destroy () { - this._vertexBuffers.forEach((vertexBuffer) => { - vertexBuffer.destroy(); - }); - this._vertexBuffers.length = 0; + public clearSubMeshes () { + for (const subMesh of this._subMeshes) { + for (const vb of subMesh.vertexBuffers) { + vb.destroy(); + } + if (subMesh.indexBuffer) { + subMesh.indexBuffer.destroy(); + } + } + this._subMeshes.splice(0); + } - this._indexBuffers.forEach((indexBuffer) => { - indexBuffer.destroy(); - }); - this._indexBuffers.length = 0; + public destroy () { + this.clearSubMeshes(); this._subMeshes.length = 0; } } @@ -217,30 +218,108 @@ export class Mesh extends Asset { super(); } + public initialize () { + if (this._initialized) { + return; + } + + this._initialized = true; + + if (this._data === null) { + return; + } + + const buffer = this._data.buffer; + const gfxDevice: GFXDevice = cc.director.root.device; + const vertexBuffers = this._createVertexBuffers(gfxDevice, buffer); + const indexBuffers: GFXBuffer[] = []; + const submeshes: IRenderingSubmesh[] = []; + + for (const prim of this._struct.primitives) { + if (prim.vertexBundelIndices.length === 0) { + continue; + } + + let indexBuffer: GFXBuffer | null = null; + let ib: any = null; + if (prim.indexView) { + const idxView = prim.indexView; + + indexBuffer = gfxDevice.createBuffer({ + usage: GFXBufferUsageBit.INDEX | GFXBufferUsageBit.TRANSFER_DST, + memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE, + size: idxView.length, + stride: idxView.stride, + }); + indexBuffers.push(indexBuffer); + + ib = new (getIndexStrideCtor(idxView.stride))(buffer, idxView.offset, idxView.count); + indexBuffer.update(ib); + } + + const vbReference = prim.vertexBundelIndices.map((i) => vertexBuffers[i]); + + let gfxAttributes: IGFXAttribute[] = []; + if (prim.vertexBundelIndices.length > 0) { + const idx = prim.vertexBundelIndices[0]; + const vertexBundle = this._struct.vertexBundles[idx]; + gfxAttributes = vertexBundle.attributes; + } + + const subMesh: IRenderingSubmesh = { + primitiveMode: prim.primitiveMode, + vertexBuffers: vbReference, + indexBuffer, + attributes: gfxAttributes, + }; + + if (prim.geometricInfo) { + const info = prim.geometricInfo; + subMesh.geometricInfo = { + indices: ib, + positions: new Float32Array(buffer, info.view.offset, info.view.length / 4), + }; + } + + submeshes.push(subMesh); + } + + this._renderingMesh = new RenderingMesh(submeshes); + } + /** * Destory this mesh and immediately release its GPU resources. */ public destroy () { - this._tryDestroyRenderingMesh(); + this.destroyRenderingMesh(); return super.destroy(); } + public destroyRenderingMesh () { + if (this._renderingMesh) { + this._renderingMesh.destroy(); + this._renderingMesh = null; + this._data = null; + this._initialized = false; + } + } + /** * Assigns new mesh struct to this. * @param struct The new mesh's struct. * @param data The new mesh's data. */ public assign (struct: IMeshStruct, data: Uint8Array) { + this.destroyRenderingMesh(); this._struct = struct; this._data = data; - this._tryDestroyRenderingMesh(); } /** * Gets the rendering mesh. */ public get renderingMesh (): RenderingMesh { - this._init(); + this.initialize(); return this._renderingMesh!; } @@ -261,7 +340,7 @@ export class Mesh extends Asset { } } // if - if (!this._data && mesh._data) { + if (this.subMeshCount === 0 && mesh._data) { this._struct.vertexBundles = new Array(mesh._struct.vertexBundles.length); for (let i = 0; i < mesh._struct.vertexBundles.length; ++i) { const bundle = mesh._struct.vertexBundles[i]; @@ -334,7 +413,8 @@ export class Mesh extends Asset { } this._data = mesh._data.slice(); - this._init (); + this._initialized = false; + this.initialize (); return true; } @@ -350,6 +430,12 @@ export class Mesh extends Asset { let vbView: Uint8Array; let srcVBView: Uint8Array; let dstVBView: Uint8Array; + let srcAttrOffset = 0; + let srcVBOffset = 0; + let dstVBOffset = 0; + let attrSize = 0; + let dstAttrView: Uint8Array; + let hasAttr = false; const vertexBundles = new Array(this._struct.vertexBundles.length); for (let i = 0; i < this._struct.vertexBundles.length; ++i) { @@ -370,7 +456,33 @@ export class Mesh extends Asset { dstOffset += dstVBView.length; vbView.set(srcVBView); - vbView.set(dstVBView, bundle.view.length); + + if (bundle.view.stride === dstBundle.view.stride) { + vbView.set(dstVBView, bundle.view.length); + } else { + srcAttrOffset = 0; + for (const attr of bundle.attributes) { + dstVBOffset = 0; + for (const dstAttr of dstBundle.attributes) { + if (attr.name === dstAttr.name && attr.format === dstAttr.format) { + hasAttr = true; + break; + } + dstVBOffset += GFXFormatInfos[dstAttr.format].size; + } + if (hasAttr) { + attrSize = GFXFormatInfos[attr.format].size; + srcVBOffset = bundle.view.length + srcAttrOffset; + for (let v = 0; v < dstBundle.view.count; ++v) { + dstAttrView = dstVBView.subarray(dstVBOffset, attrSize); + vbView.set(dstAttrView, srcVBOffset); + srcVBOffset += bundle.view.stride; + dstVBOffset += dstBundle.view.stride; + } + } + srcAttrOffset += GFXFormatInfos[attr.format].size; + } + } vertexBundles[i] = { attributes: bundle.attributes, @@ -517,7 +629,7 @@ export class Mesh extends Asset { // Create mesh. this.assign(meshStruct, new Uint8Array(bufferBlob.getCombined())); - this._init (); + this.initialize (); return true; } @@ -617,7 +729,7 @@ export class Mesh extends Asset { return null; } - public copyAttribute (primitiveIndex: number, attributeName: GFXAttributeName, buffer: ArrayBuffer, stride: number, offset: number) { + public copyAttribute (primitiveIndex: number, attributeName: GFXAttributeName, buffer: ArrayBuffer, stride: number, offset: number) { if (!this._data || primitiveIndex >= this._struct.primitives.length) { return null; @@ -692,75 +804,6 @@ export class Mesh extends Asset { } } - private _init () { - if (this._initialized) { - return; - } - - this._initialized = true; - - if (this._data === null) { - return; - } - - const buffer = this._data.buffer; - const gfxDevice: GFXDevice = cc.director.root.device; - const vertexBuffers = this._createVertexBuffers(gfxDevice, buffer); - const indexBuffers: GFXBuffer[] = []; - const submeshes: IRenderingSubmesh[] = []; - - for (const prim of this._struct.primitives) { - if (prim.vertexBundelIndices.length === 0) { - continue; - } - - let indexBuffer: GFXBuffer | null = null; - let ib: any = null; - if (prim.indexView) { - const idxView = prim.indexView; - - indexBuffer = gfxDevice.createBuffer({ - usage: GFXBufferUsageBit.INDEX | GFXBufferUsageBit.TRANSFER_DST, - memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE, - size: idxView.length, - stride: idxView.stride, - }); - indexBuffers.push(indexBuffer); - - ib = new (getIndexStrideCtor(idxView.stride))(buffer, idxView.offset, idxView.count); - indexBuffer.update(ib); - } - - const vbReference = prim.vertexBundelIndices.map((i) => vertexBuffers[i]); - - let gfxAttributes: IGFXAttribute[] = []; - if (prim.vertexBundelIndices.length > 0) { - const idx = prim.vertexBundelIndices[0]; - const vertexBundle = this._struct.vertexBundles[idx]; - gfxAttributes = vertexBundle.attributes; - } - - const subMesh: IRenderingSubmesh = { - primitiveMode: prim.primitiveMode, - vertexBuffers: vbReference, - indexBuffer, - attributes: gfxAttributes, - }; - - if (prim.geometricInfo) { - const info = prim.geometricInfo; - subMesh.geometricInfo = { - indices: ib, - positions: new Float32Array(buffer, info.view.offset, info.view.length / 4), - }; - } - - submeshes.push(subMesh); - } - - this._renderingMesh = new RenderingMesh(submeshes, vertexBuffers, indexBuffers); - } - private _createVertexBuffers (gfxDevice: GFXDevice, data: ArrayBuffer): GFXBuffer[] { return this._struct.vertexBundles.map((vertexBundle) => { const vertexBuffer = gfxDevice.createBuffer({ @@ -774,14 +817,6 @@ export class Mesh extends Asset { return vertexBuffer; }); } - - private _tryDestroyRenderingMesh () { - if (this._renderingMesh) { - this._renderingMesh.destroy(); - this._renderingMesh = null; - this._initialized = false; - } - } } cc.Mesh = Mesh; diff --git a/cocos/3d/framework/avatar-model-component.ts b/cocos/3d/framework/avatar-model-component.ts index e4d84cad81..254e1d5719 100644 --- a/cocos/3d/framework/avatar-model-component.ts +++ b/cocos/3d/framework/avatar-model-component.ts @@ -26,16 +26,79 @@ import { Texture2D } from '../../assets'; import { Filter, PixelFormat } from '../../assets/asset-enum'; import { Vec2 } from '../../core'; import { ccclass, executeInEditMode, executionOrder, menu, property } from '../../core/data/class-decorator'; -import { GFXAttributeName, GFXBufferTextureCopy, GFXFormatInfos } from '../../gfx/define'; +import { GFXAttributeName, GFXBufferTextureCopy, GFXFormatInfos, GFXFormatSize, GFXFormat } from '../../gfx/define'; import { GFXDevice } from '../../gfx/device'; import { Mesh } from '../assets'; import { SkinningModelComponent } from './skinning-model-component'; -export interface IAvatarUnit { - mesh: Mesh | null; - offset: Vec2; - albedoMap: Texture2D | null; - alphaMap: Texture2D | null; +const _vec2 = new Vec2(); + +@ccclass('cc.AvatarUnit') +export class AvatarUnit { + + @property({ type: Mesh }) + get mesh (): Mesh | null { + return this._mesh; + } + + set mesh (mesh: Mesh | null) { + if (this._mesh !== mesh) { + this._mesh = mesh; + } + } + + @property({ type: cc.Vec2 }) + get atlasSize (): Vec2 { + return this._atlasSize; + } + + set atlasSize (atlasSize: Vec2) { + if (this._atlasSize.x !== atlasSize.x || + this._atlasSize.y !== atlasSize.y) { + this._atlasSize = atlasSize; + } + } + + @property({ type: cc.Vec2 }) + get offset (): Vec2 { + return this._offset; + } + + set offset (offset: Vec2) { + if (this._offset.x !== offset.x || + this._offset.y !== offset.y) { + this._offset = offset; + } + } + + @property({ type: Texture2D }) + get albedoMap (): Texture2D | null { + return this._albedoMap; + } + + set albedoMap (albedoMap: Texture2D | null) { + if (this._albedoMap !== albedoMap) { + this._albedoMap = albedoMap; + + if (this._albedoMap) { + _vec2.x = this._albedoMap.width; + _vec2.y = this._albedoMap.height; + this.atlasSize = _vec2; + } + } + } + + @property + private _mesh: Mesh | null = null; + + @property + private _offset: Vec2 = new Vec2(0, 0); + + @property + private _atlasSize: Vec2 = new Vec2(256, 256); + + @property + private _albedoMap: Texture2D | null = null; } /** @@ -50,18 +113,46 @@ export class AvatarModelComponent extends SkinningModelComponent { @property private _combinedTexSize: number = 1024; - private _combinedTex: Texture2D | null = null; - private _units: Array = []; + @property + private _albedoMapName: string = ''; + + @property + private _avatarUnits: AvatarUnit[] = []; @property({ override: true, visible: false }) get mesh (): Mesh | null { return this._mesh; } - get units (): Array { - return this._units; + @property({ type: Number }) + get combinedTexSize (): number { + return this._combinedTexSize; + } + + set combinedTexSize (size: number) { + this._combinedTexSize = size; + } + + @property({ type: String }) + get albedoMapName (): string { + return this._albedoMapName; + } + + set albedoMapName (name: string) { + if (this._albedoMapName !== name) { + this._albedoMapName = name; + } + } + + @property({ type: [AvatarUnit] }) + get avatarUnits (): AvatarUnit[] { + return this._avatarUnits; + } + + set avatarUnits (units: AvatarUnit[]) { + this._avatarUnits = units; } constructor () { @@ -94,8 +185,8 @@ export class AvatarModelComponent extends SkinningModelComponent { } } - public addUnit (unit: IAvatarUnit) { - this._units.push(unit); + public addAvatarUnit (unit: AvatarUnit) { + this._avatarUnits.push(unit); } public clear () { @@ -104,47 +195,55 @@ export class AvatarModelComponent extends SkinningModelComponent { } } + public bindTextures () { + if (this._albedoMapName.length > 0 && this._materials.length > 0) { + const mtrl = this._materials[0]; + if (mtrl) { + mtrl.setProperty(this._albedoMapName, this._combinedTex); + } + } + } + public combine () { + this.combineTextures(); + this.combineMeshes(); + this.bindTextures(); + } + + public combineTextures () { + let isValid = false; + for (const unit of this._avatarUnits) { + if (unit.albedoMap) { + isValid = true; + break; + } + } + + if (!isValid) { + return; + } + const texImages: TexImageSource[] = []; const texImageRegions: GFXBufferTextureCopy[] = []; const texBuffers: ArrayBuffer[] = []; const texBufferRegions: GFXBufferTextureCopy[] = []; - let uvOffset = 0; - let dataView: DataView; - let uv_x: number; - let uv_y: number; - const isLittleEndian = cc.sys.isLittleEndian; - for (const unit of this._units) { + /* + const buffSize = GFXFormatSize(GFXFormat.RGBA8, this._combinedTexSize, this._combinedTexSize, 1); + const clearBuff = new ArrayBuffer(buffSize); + texBuffers.push(clearBuff); + + let region = new GFXBufferTextureCopy(); + region.texExtent.width = this._combinedTexSize; + region.texExtent.height = this._combinedTexSize; + texBufferRegions.push(region); + */ + + for (const unit of this._avatarUnits) { if (unit) { const offset = unit.offset; - const isValid = (offset.x >= 0 && offset.y >= 0); + isValid = (offset.x >= 0 && offset.y >= 0); if (isValid && unit.albedoMap && unit.albedoMap.image && unit.albedoMap.image.data) { - - // merge textures - if (unit.mesh && unit.mesh.data) { - dataView = new DataView(unit.mesh.data); - const struct = unit.mesh.struct; - for (const bundle of struct.vertexBundles) { - uvOffset = bundle.view.offset; - for (const attr of bundle.attributes) { - if (attr.name.indexOf(GFXAttributeName.ATTR_TEX_COORD) >= 0) { - break; - } - uvOffset += GFXFormatInfos[attr.format].size; - } - for (let v = 0; v < bundle.view.count; ++v) { - uv_x = dataView.getFloat32(uvOffset, isLittleEndian) + offset.x; - uv_y = dataView.getFloat32(uvOffset + 4, isLittleEndian) + offset.y; - dataView.setFloat32(uvOffset, uv_x, isLittleEndian); - dataView.setFloat32(uvOffset + 4, uv_y, isLittleEndian); - uvOffset += bundle.view.stride; - } - } - - this._mesh!.merge(unit.mesh); - } - // merge textures const region = new GFXBufferTextureCopy(); region.texOffset.x = offset.x; @@ -167,13 +266,87 @@ export class AvatarModelComponent extends SkinningModelComponent { const gfxTex = this._combinedTex!.getGFXTexture(); const device: GFXDevice = cc.director.root.device; - if (texBuffers.length > 0) { - device.copyTexImagesToTexture(texImages, gfxTex!, texImageRegions); - } - if (texBuffers.length > 0) { device.copyBuffersToTexture(texBuffers, gfxTex!, texBufferRegions); } + + if (texImages.length > 0) { + device.copyTexImagesToTexture(texImages, gfxTex!, texImageRegions); + } + } + + public combineMeshes () { + let isValid = false; + for (const unit of this._avatarUnits) { + if (unit.mesh) { + isValid = true; + break; + } + } + + if (this._mesh) { + this._mesh.destroyRenderingMesh(); + this._onMeshChanged(this._mesh); + this._updateModels(); + } + + if (!isValid) { + return; + } + + let uvOffset = 0; + let hasUV = false; + let dataView: DataView; + let uv_x: number; + let uv_y: number; + const isLittleEndian = cc.sys.isLittleEndian; + + for (const unit of this._avatarUnits) { + if (unit) { + // merge textures + if (unit.mesh && unit.mesh.data) { + const offset = unit.offset; + + const meshData = unit.mesh.data.slice(0); + const newMesh = new Mesh(); + newMesh.assign(unit.mesh.struct, meshData); + + dataView = new DataView(meshData.buffer); + const struct = unit.mesh.struct; + for (const bundle of struct.vertexBundles) { + uvOffset = bundle.view.offset; + hasUV = false; + for (const attr of bundle.attributes) { + if (attr.name.indexOf(GFXAttributeName.ATTR_TEX_COORD) >= 0) { + hasUV = true; + break; + } + uvOffset += GFXFormatInfos[attr.format].size; + } + + if (hasUV) { + const fx = unit.atlasSize.x / this._combinedTexSize; + const fy = unit.atlasSize.y / this._combinedTexSize; + for (let v = 0; v < bundle.view.count; ++v) { + uv_x = dataView.getFloat32(uvOffset, isLittleEndian); + uv_y = dataView.getFloat32(uvOffset + 4, isLittleEndian); + uv_y = Math.abs(uv_y); + uv_x = uv_x * fx + offset.x; + uv_y = (1.0 - uv_y) * fy + offset.y; + dataView.setFloat32(uvOffset, uv_x, isLittleEndian); + dataView.setFloat32(uvOffset + 4, uv_y, isLittleEndian); + uvOffset += bundle.view.stride; + } + } + } + + this._mesh!.merge(newMesh); + } + } + } + + this._onMeshChanged(this._mesh); + this._updateModels(); } private resizeCombiendTexture () { diff --git a/cocos/renderer/scene/model.ts b/cocos/renderer/scene/model.ts index babd7792ae..e460e95f38 100644 --- a/cocos/renderer/scene/model.ts +++ b/cocos/renderer/scene/model.ts @@ -201,7 +201,7 @@ export class Model { this._uboLocal.view.set(_temp_floatx16, UBOLocal.MAT_WORLD_IT_OFFSET); const commonLocal = this._localBindings.get(UBOLocal.BLOCK.name); - if (commonLocal) { + if (commonLocal && commonLocal.buffer) { commonLocal.buffer!.update(this._uboLocal.view); }