mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-03 01:12:42 +08:00
fix empty mesh merging
This commit is contained in:
+82
-1
@@ -261,6 +261,83 @@ export class Mesh extends Asset {
|
||||
}
|
||||
} // if
|
||||
|
||||
if (!this._data && mesh._data) {
|
||||
this._struct.vertexBundles = new Array<IVertexBundle>(mesh._struct.vertexBundles.length);
|
||||
for (let i = 0; i < mesh._struct.vertexBundles.length; ++i) {
|
||||
const bundle = mesh._struct.vertexBundles[i];
|
||||
|
||||
const attributes = new Array<IGFXAttribute>(bundle.attributes.length);
|
||||
for (let a = 0; a < bundle.attributes.length; ++a) {
|
||||
const attr = bundle.attributes[a];
|
||||
attributes[a] = {
|
||||
name: attr.name,
|
||||
format: attr.format,
|
||||
isNormalized: attr.isNormalized,
|
||||
stream: attr.stream,
|
||||
isInstanced: attr.isInstanced,
|
||||
};
|
||||
}
|
||||
this._struct.vertexBundles[i] = {
|
||||
view: {
|
||||
offset: bundle.view.offset,
|
||||
length: bundle.view.length,
|
||||
count: bundle.view.count,
|
||||
stride: bundle.view.stride,
|
||||
},
|
||||
attributes,
|
||||
};
|
||||
}
|
||||
|
||||
this._struct.primitives = new Array<IPrimitive>(mesh._struct.primitives.length);
|
||||
for (let p = 0; p < mesh._struct.primitives.length; ++p) {
|
||||
const prim = mesh._struct.primitives[p];
|
||||
this._struct.primitives[p] = {
|
||||
vertexBundelIndices: prim.vertexBundelIndices.slice(),
|
||||
primitiveMode: prim.primitiveMode,
|
||||
};
|
||||
|
||||
const srcPrim = this._struct.primitives[p];
|
||||
|
||||
if (prim.indexView) {
|
||||
srcPrim.indexView = {
|
||||
offset: prim.indexView.offset,
|
||||
length: prim.indexView.length,
|
||||
count: prim.indexView.count,
|
||||
stride: prim.indexView.stride,
|
||||
};
|
||||
}
|
||||
|
||||
if (prim.geometricInfo) {
|
||||
srcPrim.geometricInfo = {
|
||||
doubleSided: prim.geometricInfo.doubleSided,
|
||||
view: {
|
||||
offset: prim.geometricInfo.view.offset,
|
||||
length: prim.geometricInfo.view.length,
|
||||
count: prim.geometricInfo.view.count,
|
||||
stride: prim.geometricInfo.view.stride,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (mesh._struct.minPosition) {
|
||||
this._struct.minPosition = new Vec3();
|
||||
this._struct.minPosition.x = mesh._struct.minPosition.x;
|
||||
this._struct.minPosition.y = mesh._struct.minPosition.y;
|
||||
this._struct.minPosition.z = mesh._struct.minPosition.z;
|
||||
}
|
||||
if (mesh._struct.maxPosition) {
|
||||
this._struct.maxPosition = new Vec3();
|
||||
this._struct.maxPosition.x = mesh._struct.maxPosition.x;
|
||||
this._struct.maxPosition.y = mesh._struct.maxPosition.y;
|
||||
this._struct.maxPosition.z = mesh._struct.maxPosition.z;
|
||||
}
|
||||
|
||||
this._data = mesh._data.slice();
|
||||
this._init ();
|
||||
return true;
|
||||
}
|
||||
|
||||
// merge buffer
|
||||
const bufferBlob = new BufferBlob();
|
||||
|
||||
@@ -281,7 +358,7 @@ export class Mesh extends Asset {
|
||||
|
||||
srcOffset = bundle.view.offset;
|
||||
dstOffset = dstBundle.view.offset;
|
||||
vertStride = bundle.view.stride + 4;
|
||||
vertStride = bundle.view.stride;
|
||||
vertCount = bundle.view.count + dstBundle.view.count;
|
||||
|
||||
vb = new ArrayBuffer(vertCount * vertStride);
|
||||
@@ -446,6 +523,10 @@ export class Mesh extends Asset {
|
||||
}
|
||||
|
||||
public validateMergingMesh (mesh: Mesh) {
|
||||
if (!this._data && mesh._data) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// validate vertex bundles
|
||||
if (this._struct.vertexBundles.length !== mesh._struct.vertexBundles.length) {
|
||||
return false;
|
||||
|
||||
@@ -24,16 +24,16 @@
|
||||
|
||||
import { Texture2D } from '../../assets';
|
||||
import { Filter, PixelFormat } from '../../assets/asset-enum';
|
||||
import { Rect } from '../../core';
|
||||
import { Vec2 } from '../../core';
|
||||
import { ccclass, executeInEditMode, executionOrder, menu, property } from '../../core/data/class-decorator';
|
||||
import { GFXBufferTextureCopy } from '../../gfx/define';
|
||||
import { GFXAttributeName, GFXBufferTextureCopy, GFXFormatInfos } from '../../gfx/define';
|
||||
import { GFXDevice } from '../../gfx/device';
|
||||
import { Mesh } from '../assets';
|
||||
import { SkinningModelComponent } from './skinning-model-component';
|
||||
|
||||
export interface IAvatarUnit {
|
||||
mesh: Mesh | null;
|
||||
region: Rect;
|
||||
offset: Vec2;
|
||||
albedoMap: Texture2D | null;
|
||||
alphaMap: Texture2D | null;
|
||||
}
|
||||
@@ -60,7 +60,7 @@ export class AvatarModelComponent extends SkinningModelComponent {
|
||||
return this._mesh;
|
||||
}
|
||||
|
||||
get parts (): Array<IAvatarUnit | null> {
|
||||
get units (): Array<IAvatarUnit | null> {
|
||||
return this._units;
|
||||
}
|
||||
|
||||
@@ -87,29 +87,70 @@ export class AvatarModelComponent extends SkinningModelComponent {
|
||||
this._combinedTex.destroy();
|
||||
this._combinedTex = null;
|
||||
}
|
||||
|
||||
if (this._mesh) {
|
||||
this._mesh.destroy();
|
||||
this._mesh = null;
|
||||
}
|
||||
}
|
||||
|
||||
public addUnit (unit: IAvatarUnit) {
|
||||
this._units.push(unit);
|
||||
}
|
||||
|
||||
public clear () {
|
||||
if (this._mesh) {
|
||||
this._mesh.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
public combine () {
|
||||
|
||||
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) {
|
||||
if (unit) {
|
||||
|
||||
// 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;
|
||||
const offset = unit.offset;
|
||||
const 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 = unit.region.x;
|
||||
region.texOffset.y = unit.region.y;
|
||||
region.texExtent.width = unit.region.width;
|
||||
region.texExtent.height = unit.region.height;
|
||||
region.texOffset.x = offset.x;
|
||||
region.texOffset.y = offset.y;
|
||||
region.texExtent.width = unit.albedoMap.image.width;
|
||||
region.texExtent.height = unit.albedoMap.image.height;
|
||||
|
||||
const data = unit.albedoMap.image.data;
|
||||
if (data instanceof HTMLCanvasElement || data instanceof HTMLImageElement) {
|
||||
|
||||
Reference in New Issue
Block a user