From 6681e9c770c915075d7195969529dba7d25500f9 Mon Sep 17 00:00:00 2001 From: Leslie Date: Thu, 10 Jan 2019 11:28:35 +0800 Subject: [PATCH 1/5] rename, tslint --- cocos/3d/assets/{mesh.js => mesh.ts} | 75 ++++++++++++++-------------- 1 file changed, 37 insertions(+), 38 deletions(-) rename cocos/3d/assets/{mesh.js => mesh.ts} (85%) diff --git a/cocos/3d/assets/mesh.js b/cocos/3d/assets/mesh.ts similarity index 85% rename from cocos/3d/assets/mesh.js rename to cocos/3d/assets/mesh.ts index 16f7f76ddf..8b9ec9d4d0 100644 --- a/cocos/3d/assets/mesh.js +++ b/cocos/3d/assets/mesh.ts @@ -24,22 +24,22 @@ ****************************************************************************/ // @ts-check -import { _decorator } from "../../core/data"; +import { _decorator } from '../../core/data'; const { ccclass, property } = _decorator; -import { Asset } from "../../assets/asset"; -import gfx from "../../renderer/gfx"; -import { enums as gfxEnums } from "../../renderer/gfx/enums"; -import InputAssembler from "../../renderer/core/input-assembler"; -import BufferRange from "./utils/buffer-range"; -import { Vec3 } from "../../core/value-types"; -import { GFXBufferUsageBit, GFXMemoryUsageBit, GFXFormat } from "../../gfx/define"; +import { Asset } from '../../assets/asset'; +import { Vec3 } from '../../core/value-types'; +import { GFXBufferUsageBit, GFXFormat, GFXMemoryUsageBit } from '../../gfx/define'; +import InputAssembler from '../../renderer/core/input-assembler'; +import gfx from '../../renderer/gfx'; +import { enums as gfxEnums } from '../../renderer/gfx/enums'; +import BufferRange from './utils/buffer-range'; /** * A vertex bundle describes a serials of vertex attributes. * These vertex attributes occupy a range of the buffer and * are interleaved, no padding bytes, in the range. */ -@ccclass("cc.VertexBundle") +@ccclass('cc.VertexBundle') export class VertexBundle { /** * The data range of this bundle. @@ -47,21 +47,21 @@ export class VertexBundle { * @type {BufferRange} */ @property(BufferRange) - _data = null; + public _data = null; /** * This bundle's vertices count. * @type {number} */ @property(Number) - _verticesCount = 0; + public _verticesCount = 0; /** * The attributes's formats. * @type {{name: string, type: number, num: number, normalize: boolean}[]} */ @property - _formats = []; + public _formats = []; } cc.VertexBundle = VertexBundle; @@ -69,7 +69,7 @@ cc.VertexBundle = VertexBundle; * A primitive is a geometry constituted with a list of * same topology primitive graphic(such as points, lines or triangles). */ -@ccclass("cc.Primitive") +@ccclass('cc.Primitive') export class Primitive { /** @@ -77,28 +77,28 @@ export class Primitive { * @type {number[]} */ @property([Number]) - _vertexBundelIndices = []; + public _vertexBundelIndices = []; /** * The indices data range of this primitive. * @type {BufferRange} */ @property(BufferRange) - _indices = null; + public _indices = null; /** * The type of this primitive's indices. * @type {number} */ @property(Number) - _indexUnit = gfxEnums.INDEX_FMT_UINT16; + public _indexUnit = gfxEnums.INDEX_FMT_UINT16; /** * This primitive's topology. * @type {number} */ @property(Number) - _topology = gfxEnums.PT_TRIANGLES; + public _topology = gfxEnums.PT_TRIANGLES; } cc.Primitive = Primitive; @@ -109,27 +109,27 @@ export default class Mesh extends Asset { * @type {VertexBundle[]} */ @property([VertexBundle]) - _vertexBundles = []; + public _vertexBundles = []; /** * @type {Primitive[]} */ @property([Primitive]) - _primitives = []; + public _primitives = []; /** * The min position of this mesh's vertices. */ @property(Vec3) - _minPosition = null; + public _minPosition = null; /** * The max position of this mesh's vertices. */ @property(Vec3) - _maxPosition = null; + public _maxPosition = null; - constructor() { + constructor () { super(); /** @@ -143,18 +143,18 @@ export default class Mesh extends Asset { this._data = null; } - get _nativeAsset() { + get _nativeAsset () { return this._data; } - set _nativeAsset(value) { + set _nativeAsset (value) { this._data = value; } /** * */ - _lazyInitRenderResources() { + public _lazyInitRenderResources () { if (this._subMeshes != null) { return; } @@ -166,7 +166,7 @@ export default class Mesh extends Asset { } const buffer = this._getBuffer(); const vertexBuffers = this._vertexBundles.map((vertexBundle) => { - let vb = cc.director.root.device.createBuffer({ + const vb = cc.director.root.device.createBuffer({ usage: GFXBufferUsageBit.VERTEX, memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE, size: vertexBundle._data._length, @@ -188,8 +188,7 @@ export default class Mesh extends Asset { // Currently, an IA only has one vertex buffer. const vertexBuffer = vertexBuffers[primitive._vertexBundelIndices[0]]; - - let ib = cc.director.root.device.createBuffer({ + const ib = cc.director.root.device.createBuffer({ usage: GFXBufferUsageBit.INDEX, memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE, size: primitive._indices._length, @@ -202,11 +201,11 @@ export default class Mesh extends Asset { ib.update(new DataView(buffer, primitive._indices._offset, primitive._indices._length)); - let attributes = []; + const attributes = []; - let vertexBundle = this._vertexBundles[primitive._vertexBundelIndices[0]]; + const vertexBundle = this._vertexBundles[primitive._vertexBundelIndices[0]]; for (let i = 0; i < vertexBundle._formats.length; i++) { - let gfxAttr = {}; + const gfxAttr = {}; gfxAttr.name = vertexBundle._formats[i].name; let gfxFormat; switch (vertexBundle._formats[i].num) { @@ -263,7 +262,7 @@ export default class Mesh extends Asset { * !#en * Destory this mesh and immediately release its video memory. */ - destroy() { + public destroy () { if (this._subMeshes !== null) { // Destroy vertex buffer this._subMeshes[0]._vertexBuffer.destroy(); @@ -282,7 +281,7 @@ export default class Mesh extends Asset { * Submeshes count of this mesh. * @type {number} */ - get subMeshCount() { + get subMeshCount () { this._lazyInitRenderResources(); return this._subMeshes.length; } @@ -292,16 +291,16 @@ export default class Mesh extends Asset { * Gets the specified submesh. * @param {number} index Index of the specified submesh. */ - getSubMesh(index) { + public getSubMesh (index) { this._lazyInitRenderResources(); return this._subMeshes[index]; } - get minPosition() { + get minPosition () { return this._minPosition; } - get maxPosition() { + get maxPosition () { return this._maxPosition; } @@ -320,11 +319,11 @@ export default class Mesh extends Asset { /** * @return {ArrayBuffer} */ - _getBuffer() { + public _getBuffer () { return this._data.buffer; } - _getIndexUnitSize(indexUnit) { + public _getIndexUnitSize (indexUnit) { switch (indexUnit) { case gfxEnums.INDEX_FMT_UINT8: return 1; From 06797ddb05c03ff9bdedf37af8dd941ca571eb79 Mon Sep 17 00:00:00 2001 From: Leslie Date: Thu, 10 Jan 2019 11:48:45 +0800 Subject: [PATCH 2/5] rename; tslint --- .../utils/{buffer-range.js => buffer-range.ts} | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) rename cocos/3d/assets/utils/{buffer-range.js => buffer-range.ts} (90%) diff --git a/cocos/3d/assets/utils/buffer-range.js b/cocos/3d/assets/utils/buffer-range.ts similarity index 90% rename from cocos/3d/assets/utils/buffer-range.js rename to cocos/3d/assets/utils/buffer-range.ts index bbd720b2df..fdd98c48f9 100644 --- a/cocos/3d/assets/utils/buffer-range.js +++ b/cocos/3d/assets/utils/buffer-range.ts @@ -24,32 +24,32 @@ ****************************************************************************/ // @ts-check -import { _decorator } from "../../../core/data"; +import { _decorator } from '../../../core/data'; const { ccclass, property } = _decorator; /** * The class BufferRange denotes a range of the buffer. */ -@ccclass("cc.BufferRange") +@ccclass('cc.BufferRange') export default class BufferRange { /** * The offset of the range. * @type {Number} */ @property(Number) - _offset = 0; + public _offset = 0; /** * The length of the range, in bytes. * @type {Number} */ @property(Number) - _length = 0; + public _length = 0; - constructor(offset, length) { + constructor (offset, length) { this._offset = offset; this._length = length; } } -cc.BufferRange = BufferRange; \ No newline at end of file +cc.BufferRange = BufferRange; From 14fd6888c61aee629279ef3ab768140b7cac2e2f Mon Sep 17 00:00:00 2001 From: Leslie Date: Thu, 10 Jan 2019 16:07:39 +0800 Subject: [PATCH 3/5] restruct mesh --- cocos/3d/assets/mesh.ts | 599 ++++++++++++++++---------- cocos/3d/assets/utils/buffer-range.ts | 32 +- 2 files changed, 364 insertions(+), 267 deletions(-) diff --git a/cocos/3d/assets/mesh.ts b/cocos/3d/assets/mesh.ts index 8b9ec9d4d0..5aab6e00d5 100644 --- a/cocos/3d/assets/mesh.ts +++ b/cocos/3d/assets/mesh.ts @@ -25,123 +25,242 @@ // @ts-check import { _decorator } from '../../core/data'; +import { ccenum } from '../../core/value-types/enum'; const { ccclass, property } = _decorator; import { Asset } from '../../assets/asset'; import { Vec3 } from '../../core/value-types'; +import { GFXBuffer } from '../../gfx/buffer'; import { GFXBufferUsageBit, GFXFormat, GFXMemoryUsageBit } from '../../gfx/define'; -import InputAssembler from '../../renderer/core/input-assembler'; -import gfx from '../../renderer/gfx'; -import { enums as gfxEnums } from '../../renderer/gfx/enums'; -import BufferRange from './utils/buffer-range'; +import { GFXDevice } from '../../gfx/device'; +import { GFXInputAssembler, IGFXInputAttribute } from '../../gfx/input-assembler'; +import { IBufferRange } from './utils/buffer-range'; -/** - * A vertex bundle describes a serials of vertex attributes. - * These vertex attributes occupy a range of the buffer and - * are interleaved, no padding bytes, in the range. - */ -@ccclass('cc.VertexBundle') -export class VertexBundle { +export enum AttributeBaseType { + /** + * 8 bits signed integer. + */ + INT8, + + /** + * 8 bits unsigned integer. + */ + UINT8, + + /** + * 16 bits signed integer. + */ + INT16, + + /** + * 16 bits unsigned integer. + */ + UINT16, + + /** + * 32 bits signed integer. + */ + INT32, + + /** + * 32 bits unsigned integer. + */ + UINT32, + + /** + * 32 bits floating number. + */ + FLOAT32, +} + +ccenum(AttributeBaseType); + +export enum AttributeType { + /** + * Scalar. + */ + SCALAR, + + /** + * 2 components vector. + */ + VEC2, + + /** + * 3 components vector. + */ + VEC3, + + /** + * 4 components vector. + */ + VEC4, +} + +ccenum(AttributeType); + +export enum Topology { + /** + * Point list. + */ + POINT_LIST, + + /** + * Line list. + */ + LINE_LIST, + + /** + * Triangle list. + */ + TRIANGLE_LIST, +} + +ccenum(Topology); + +export enum IndexUnit { + /** + * 8 bits unsigned integer. + */ + UINT8, + + /** + * 8 bits unsigned integer. + */ + UINT16, + + /** + * 8 bits unsigned integer. + */ + UINT32, +} + +ccenum(IndexUnit); + +function getIndexUnitStride (indexUnit: IndexUnit) { + switch (indexUnit) { + case IndexUnit.UINT8: return 1; + case IndexUnit.UINT16: return 2; + case IndexUnit.UINT32: return 3; + } + return 1; +} + +export interface IVertexAttribute { + /** + * Attribute Name. + */ + name: string; + + /** + * Attribute base type. + */ + baseType: AttributeBaseType; + + /** + * Attribute type. + */ + type: AttributeType; + + /** + * Whether normalize. + */ + normalize: boolean; +} + +function toGfxAttributeType (vertexAttribute: IVertexAttribute) { + let formatName = ''; + switch (vertexAttribute.type) { + case AttributeType.SCALAR: + formatName = 'R'; + break; + case AttributeType.VEC2: + formatName = 'RG'; + break; + case AttributeType.VEC3: + formatName = 'RGB'; + break; + case AttributeType.VEC4: + formatName = 'RGBA'; + break; + } + switch (vertexAttribute.baseType) { + case AttributeBaseType.INT8: + formatName += '8I'; + break; + case AttributeBaseType.UINT8: + formatName += '8UI'; + break; + case AttributeBaseType.INT16: + formatName += '16I'; + break; + case AttributeBaseType.UINT16: + formatName += '16UI'; + break; + case AttributeBaseType.INT32: + formatName += '32I'; + break; + case AttributeBaseType.UINT32: + formatName += '32UI'; + break; + case AttributeBaseType.FLOAT32: + formatName += '32F'; + break; + } + + const resultFormat = GFXFormat[formatName]; + if (resultFormat !== undefined) { + return resultFormat; + } + + return GFXFormat.R8UI; +} + +export interface IVertexBundle { /** * The data range of this bundle. * This range of data is essentially mapped to a GPU vertex buffer. - * @type {BufferRange} */ - @property(BufferRange) - public _data = null; + data: IBufferRange; /** * This bundle's vertices count. - * @type {number} */ - @property(Number) - public _verticesCount = 0; + verticesCount: number; /** - * The attributes's formats. - * @type {{name: string, type: number, num: number, normalize: boolean}[]} + * Attributes. */ - @property - public _formats = []; + attributes: IVertexAttribute[]; } -cc.VertexBundle = VertexBundle; /** * A primitive is a geometry constituted with a list of * same topology primitive graphic(such as points, lines or triangles). */ -@ccclass('cc.Primitive') -export class Primitive { - +export interface IPrimitive { /** * The vertex bundles that this primitive use. - * @type {number[]} */ - @property([Number]) - public _vertexBundelIndices = []; + vertexBundelIndices: number[]; /** * The indices data range of this primitive. - * @type {BufferRange} */ - @property(BufferRange) - public _indices = null; + indices: IBufferRange; /** * The type of this primitive's indices. - * @type {number} */ - @property(Number) - public _indexUnit = gfxEnums.INDEX_FMT_UINT16; + indexUnit: IndexUnit; /** * This primitive's topology. - * @type {number} */ - @property(Number) - public _topology = gfxEnums.PT_TRIANGLES; + topology: Topology; } -cc.Primitive = Primitive; @ccclass('cc.Mesh') export default class Mesh extends Asset { - /** - * The vertex bundles that this mesh owns. - * @type {VertexBundle[]} - */ - @property([VertexBundle]) - public _vertexBundles = []; - - /** - * @type {Primitive[]} - */ - @property([Primitive]) - public _primitives = []; - - /** - * The min position of this mesh's vertices. - */ - @property(Vec3) - public _minPosition = null; - - /** - * The max position of this mesh's vertices. - */ - @property(Vec3) - public _maxPosition = null; - - constructor () { - super(); - - /** - * @type {GFXInputAssembler[]} - */ - this._subMeshes = null; - - /** - * @type {Uint8Array} - */ - this._data = null; - } get _nativeAsset () { return this._data; @@ -152,148 +271,11 @@ export default class Mesh extends Asset { } /** - * - */ - public _lazyInitRenderResources () { - if (this._subMeshes != null) { - return; - } - - this._subMeshes = []; - - if (this._data === null) { - return; - } - const buffer = this._getBuffer(); - const vertexBuffers = this._vertexBundles.map((vertexBundle) => { - const vb = cc.director.root.device.createBuffer({ - usage: GFXBufferUsageBit.VERTEX, - memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE, - size: vertexBundle._data._length, - stride: vertexBundle._data._length / vertexBundle._verticesCount, - }); - - if (!vb) { - return null; - } - - vb.update(new Uint8Array(buffer, vertexBundle._data._offset, vertexBundle._data._length)); - return vb; - }); - this._subMeshes = this._primitives.map((primitive) => { - if (primitive._vertexBundelIndices.length === 0) { - return null; - } - - // Currently, an IA only has one vertex buffer. - const vertexBuffer = vertexBuffers[primitive._vertexBundelIndices[0]]; - - const ib = cc.director.root.device.createBuffer({ - usage: GFXBufferUsageBit.INDEX, - memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE, - size: primitive._indices._length, - stride: this._getIndexUnitSize(primitive._indexUnit), - }); - - if (!ib) { - return null; - } - - ib.update(new DataView(buffer, primitive._indices._offset, primitive._indices._length)); - - const attributes = []; - - const vertexBundle = this._vertexBundles[primitive._vertexBundelIndices[0]]; - for (let i = 0; i < vertexBundle._formats.length; i++) { - const gfxAttr = {}; - gfxAttr.name = vertexBundle._formats[i].name; - let gfxFormat; - switch (vertexBundle._formats[i].num) { - case 1: - gfxFormat = 'R'; - break; - case 2: - gfxFormat = 'RG'; - break; - case 3: - gfxFormat = 'RGB'; - break; - case 4: - gfxFormat = 'RGBA'; - break; - } - switch (vertexBundle._formats[i].type) { - case gfx.ATTR_TYPE_INT8: - gfxFormat += '8I'; - break; - case gfx.ATTR_TYPE_UINT8: - gfxFormat += '8UI'; - break; - case gfx.ATTR_TYPE_INT16: - gfxFormat += '16I'; - break; - case gfx.ATTR_TYPE_UINT16: - gfxFormat += '16UI'; - break; - case gfx.ATTR_TYPE_INT32: - gfxFormat += '32I'; - break; - case gfx.ATTR_TYPE_UINT32: - gfxFormat += '32UI'; - break; - case gfx.ATTR_TYPE_FLOAT32: - gfxFormat += '32F'; - break; - } - gfxAttr.format = GFXFormat[gfxFormat]; - gfxAttr.normalize = vertexBundle._formats[i].normalize; - attributes.push(gfxAttr); - } - - return cc.director.root.device.createInputAssembler({ - attributes, - vertexBuffers: [vertexBuffer], - indexBuffer: ib, - }); - }); - } - - /** - * !#en - * Destory this mesh and immediately release its video memory. - */ - public destroy () { - if (this._subMeshes !== null) { - // Destroy vertex buffer - this._subMeshes[0]._vertexBuffer.destroy(); - // Destroy index buffers - for (let i = 0; i < this._subMeshes.length; ++i) { - this._subMeshes[i]._indexBuffer.destroy(); - } - this._subMeshes = null; - } - - return super.destroy(); - } - - /** - * !#en * Submeshes count of this mesh. - * @type {number} */ get subMeshCount () { this._lazyInitRenderResources(); - return this._subMeshes.length; - } - - /** - * !#en - * Gets the specified submesh. - * @param {number} index Index of the specified submesh. - */ - public getSubMesh (index) { - this._lazyInitRenderResources(); - return this._subMeshes[index]; + return this._subMeshes!.length; } get minPosition () { @@ -303,36 +285,177 @@ export default class Mesh extends Asset { get maxPosition () { return this._maxPosition; } - - // TODO - // updateData () { - // // store the data - // if (this._persist) { - // if (this._data) { - // this._data.set(data, offset); - // } else { - // this._data = data; - // } - // } - // } + /** + * The vertex bundles that this mesh owns. + */ + @property + public _vertexBundles: IVertexBundle[] = []; /** - * @return {ArrayBuffer} + * The primitives that this mesh owns. */ - public _getBuffer () { - return this._data.buffer; + public _primitives: IPrimitive[] = []; + + /** + * The min position of this mesh's vertices. + */ + @property(Vec3) + public _minPosition: Vec3 = new Vec3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE); + + /** + * The max position of this mesh's vertices. + */ + @property(Vec3) + public _maxPosition: Vec3 = new Vec3(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE); + + private _data: Uint8Array | null = null; + + private _initialized = false; + + private _subMeshes: Array = []; + + private _vertexBuffers: GFXBuffer[] = []; + + private _indexBuffers: GFXBuffer[] = []; + + constructor () { + super(); } - public _getIndexUnitSize (indexUnit) { - switch (indexUnit) { - case gfxEnums.INDEX_FMT_UINT8: - return 1; - case gfxEnums.INDEX_FMT_UINT16: - return 2; - case gfxEnums.INDEX_FMT_UINT32: - return 3; + /** + * Destory this mesh and immediately release its video memory. + */ + public destroy () { + this._vertexBuffers.forEach((vertexBuffer) => { + vertexBuffer.destroy(); + }); + this._vertexBuffers.length = 0; + + this._indexBuffers.forEach((indexBuffer) => { + indexBuffer.destroy(); + }); + this._indexBuffers.length = 0; + + this._subMeshes.forEach((subMesh) => { + if (subMesh) { + subMesh.destroy(); + } + }); + this._subMeshes.length = 0; + + this._initialized = false; + + return super.destroy(); + } + + /** + * !#en + * Gets the specified submesh. + * @param {number} index Index of the specified submesh. + */ + public getSubMesh (index) { + this._lazyInitRenderResources(); + return this._subMeshes![index]; + } + + /** + * + */ + private _lazyInitRenderResources () { + if (this._initialized) { + return; } - return 1; + + this._initialized = true; + + if (this._data === null) { + return; + } + + const buffer = this._data.buffer; + + const gfxDevice = cc.director.root.device as GFXDevice; + + this._createVertexBuffers(gfxDevice, buffer); + + const submeshes = this._primitives.map((primitive) => { + if (primitive.vertexBundelIndices.length === 0) { + return null; + } + + const indexBuffer = gfxDevice.createBuffer({ + usage: GFXBufferUsageBit.INDEX, + memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE, + size: primitive.indices.length, + stride: getIndexUnitStride(primitive.indexUnit), + }); + + if (!indexBuffer) { + return null; + } + + indexBuffer.update(buffer, primitive.indices.offset, primitive.indices.length); + + const gfxAttributes: IGFXInputAttribute[] = []; + primitive.vertexBundelIndices.forEach((iVertexBundle) => { + const vertexBundle = this._vertexBundles[iVertexBundle]; + vertexBundle.attributes.forEach((attribute) => { + const gfxAttribute: IGFXInputAttribute = { + name: attribute.name, + format: toGfxAttributeType(attribute), + stream: iVertexBundle, + }; + if ('normalize' in attribute) { + gfxAttribute.isNormalized = attribute.normalize; + } + gfxAttributes.push(gfxAttribute); + }); + }); + + const referedVertexBuffers = primitive.vertexBundelIndices.map( + (i) => this._vertexBuffers[i]); + + return gfxDevice.createInputAssembler({ + attributes: gfxAttributes, + vertexBuffers: referedVertexBuffers, + indexBuffer, + }); + }); + + this._subMeshes = submeshes; + } + + private _createVertexBuffers (gfxDevice: GFXDevice, data: ArrayBuffer) { + let nullish = false; + + const vertexBuffers = this._vertexBundles.map((vertexBundle) => { + const vertexBuffer = gfxDevice.createBuffer({ + usage: GFXBufferUsageBit.VERTEX, + memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE, + size: vertexBundle.data.length, + stride: vertexBundle.data.length / vertexBundle.verticesCount, + }); + + if (!vertexBuffer) { + nullish = true; + return null; + } + + vertexBuffer.update(new Uint8Array(data, vertexBundle.data.offset, vertexBundle.data.length)); + return vertexBuffer; + }); + + if (!nullish) { + this._vertexBuffers = vertexBuffers as GFXBuffer[]; + } else { + vertexBuffers.forEach((vertexBuffer) => { + if (vertexBuffer) { + vertexBuffer.destroy(); + } + }); + } + + return !nullish; } } cc.Mesh = Mesh; diff --git a/cocos/3d/assets/utils/buffer-range.ts b/cocos/3d/assets/utils/buffer-range.ts index fdd98c48f9..25f2f386ed 100644 --- a/cocos/3d/assets/utils/buffer-range.ts +++ b/cocos/3d/assets/utils/buffer-range.ts @@ -23,33 +23,7 @@ THE SOFTWARE. ****************************************************************************/ -// @ts-check -import { _decorator } from '../../../core/data'; -const { ccclass, property } = _decorator; - -/** - * The class BufferRange denotes a range of the buffer. - */ -@ccclass('cc.BufferRange') -export default class BufferRange { - /** - * The offset of the range. - * @type {Number} - */ - @property(Number) - public _offset = 0; - - /** - * The length of the range, in bytes. - * @type {Number} - */ - @property(Number) - public _length = 0; - - constructor (offset, length) { - this._offset = offset; - this._length = length; - } +export interface IBufferRange { + offset: number; + length: number; } - -cc.BufferRange = BufferRange; From 934bc477139b5bb93d787b984ad58039912b29ab Mon Sep 17 00:00:00 2001 From: Leslie Date: Thu, 10 Jan 2019 16:55:27 +0800 Subject: [PATCH 4/5] topology --- cocos/3d/assets/mesh.ts | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/cocos/3d/assets/mesh.ts b/cocos/3d/assets/mesh.ts index 5aab6e00d5..090b2dd4c9 100644 --- a/cocos/3d/assets/mesh.ts +++ b/cocos/3d/assets/mesh.ts @@ -30,7 +30,7 @@ const { ccclass, property } = _decorator; import { Asset } from '../../assets/asset'; import { Vec3 } from '../../core/value-types'; import { GFXBuffer } from '../../gfx/buffer'; -import { GFXBufferUsageBit, GFXFormat, GFXMemoryUsageBit } from '../../gfx/define'; +import { GFXBufferUsageBit, GFXFormat, GFXMemoryUsageBit, GFXPrimitiveMode } from '../../gfx/define'; import { GFXDevice } from '../../gfx/device'; import { GFXInputAssembler, IGFXInputAttribute } from '../../gfx/input-assembler'; import { IBufferRange } from './utils/buffer-range'; @@ -102,21 +102,25 @@ export enum Topology { /** * Point list. */ - POINT_LIST, + POINT_LIST = GFXPrimitiveMode.POINT_LIST, /** * Line list. */ - LINE_LIST, + LINE_LIST = GFXPrimitiveMode.LINE_LIST, /** * Triangle list. */ - TRIANGLE_LIST, + TRIANGLE_LIST = GFXPrimitiveMode.TRIANGLE_LIST, } ccenum(Topology); +function toGFXTopology (topology: Topology) { + return topology as unknown as GFXPrimitiveMode; +} + export enum IndexUnit { /** * 8 bits unsigned integer. @@ -167,7 +171,7 @@ export interface IVertexAttribute { normalize: boolean; } -function toGfxAttributeType (vertexAttribute: IVertexAttribute) { +function toGFXAttributeType (vertexAttribute: IVertexAttribute) { let formatName = ''; switch (vertexAttribute.type) { case AttributeType.SCALAR: @@ -358,6 +362,10 @@ export default class Mesh extends Asset { return this._subMeshes![index]; } + public getGFXPrimitiveMode (index: number) { + return toGFXTopology(this._primitives[index].topology); + } + /** * */ @@ -402,7 +410,7 @@ export default class Mesh extends Asset { vertexBundle.attributes.forEach((attribute) => { const gfxAttribute: IGFXInputAttribute = { name: attribute.name, - format: toGfxAttributeType(attribute), + format: toGFXAttributeType(attribute), stream: iVertexBundle, }; if ('normalize' in attribute) { From 2cf99a2ad171fd236b97ad9f8cf8df1176a818ba Mon Sep 17 00:00:00 2001 From: Leslie Date: Thu, 10 Jan 2019 17:43:31 +0800 Subject: [PATCH 5/5] restruct mesh.ts --- cocos/3d/assets/mesh.ts | 160 +++++++++++++++++++--------------- cocos/3d/misc/utils.js | 6 +- cocos/3d/primitive/box.ts | 2 +- cocos/3d/primitive/defines.ts | 2 +- 4 files changed, 94 insertions(+), 76 deletions(-) diff --git a/cocos/3d/assets/mesh.ts b/cocos/3d/assets/mesh.ts index 090b2dd4c9..3fb1a594b0 100644 --- a/cocos/3d/assets/mesh.ts +++ b/cocos/3d/assets/mesh.ts @@ -263,6 +263,47 @@ export interface IPrimitive { topology: Topology; } +export interface IRenderingSubmesh { + inputAssembler: GFXInputAssembler; + primitiveMode: GFXPrimitiveMode; +} + +export class RenderingMesh { + public constructor ( + private _subMeshes: Array, + private _vertexBuffers: GFXBuffer[], + private _indexBuffers: GFXBuffer[]) { + + } + + public get subMeshCount () { + return this._subMeshes.length; + } + + public getSubmesh (index: number) { + return this._subMeshes[index]; + } + + public destroy () { + this._vertexBuffers.forEach((vertexBuffer) => { + vertexBuffer.destroy(); + }); + this._vertexBuffers.length = 0; + + this._indexBuffers.forEach((indexBuffer) => { + indexBuffer.destroy(); + }); + this._indexBuffers.length = 0; + + this._subMeshes.forEach((subMesh) => { + if (subMesh) { + subMesh.inputAssembler.destroy(); + } + }); + this._subMeshes.length = 0; + } +} + @ccclass('cc.Mesh') export default class Mesh extends Asset { @@ -276,19 +317,27 @@ export default class Mesh extends Asset { /** * Submeshes count of this mesh. + * @deprecated Use this.renderingMesh.subMeshCount instead. */ get subMeshCount () { - this._lazyInitRenderResources(); - return this._subMeshes!.length; + const renderingMesh = this.renderingMesh; + return renderingMesh ? renderingMesh.subMeshCount : 0; } + /** + * Min position of this mesh. + */ get minPosition () { return this._minPosition; } + /** + * Max position of this mesh. + */ get maxPosition () { return this._maxPosition; } + /** * The vertex bundles that this mesh owns. */ @@ -298,6 +347,7 @@ export default class Mesh extends Asset { /** * The primitives that this mesh owns. */ + @property public _primitives: IPrimitive[] = []; /** @@ -316,11 +366,7 @@ export default class Mesh extends Asset { private _initialized = false; - private _subMeshes: Array = []; - - private _vertexBuffers: GFXBuffer[] = []; - - private _indexBuffers: GFXBuffer[] = []; + private _renderingMesh: RenderingMesh | null = null; constructor () { super(); @@ -330,45 +376,33 @@ export default class Mesh extends Asset { * Destory this mesh and immediately release its video memory. */ public destroy () { - this._vertexBuffers.forEach((vertexBuffer) => { - vertexBuffer.destroy(); - }); - this._vertexBuffers.length = 0; - - this._indexBuffers.forEach((indexBuffer) => { - indexBuffer.destroy(); - }); - this._indexBuffers.length = 0; - - this._subMeshes.forEach((subMesh) => { - if (subMesh) { - subMesh.destroy(); - } - }); - this._subMeshes.length = 0; - - this._initialized = false; - + if (this._renderingMesh) { + this._renderingMesh.destroy(); + this._renderingMesh = null; + this._initialized = false; + } return super.destroy(); } + /** + * Gets the rendering mesh. + */ + public get renderingMesh () { + this._lazyInitRenderResources(); + return this._renderingMesh; + } + /** * !#en * Gets the specified submesh. - * @param {number} index Index of the specified submesh. + * @param index Index of the specified submesh. + * @deprecated Using this.renderingMesh() instead. */ - public getSubMesh (index) { - this._lazyInitRenderResources(); - return this._subMeshes![index]; + public getSubMesh (index: number) { + const renderingMesh = this.renderingMesh; + return renderingMesh ? renderingMesh.getSubmesh(index) : null; } - public getGFXPrimitiveMode (index: number) { - return toGFXTopology(this._primitives[index].topology); - } - - /** - * - */ private _lazyInitRenderResources () { if (this._initialized) { return; @@ -384,9 +418,11 @@ export default class Mesh extends Asset { const gfxDevice = cc.director.root.device as GFXDevice; - this._createVertexBuffers(gfxDevice, buffer); + const vertexBuffers = this._createVertexBuffers(gfxDevice, buffer); - const submeshes = this._primitives.map((primitive) => { + const indexBuffers: GFXBuffer[] = []; + + const renderingSubmeshes = this._primitives.map((primitive) => { if (primitive.vertexBundelIndices.length === 0) { return null; } @@ -397,10 +433,7 @@ export default class Mesh extends Asset { size: primitive.indices.length, stride: getIndexUnitStride(primitive.indexUnit), }); - - if (!indexBuffer) { - return null; - } + indexBuffers.push(indexBuffer); indexBuffer.update(buffer, primitive.indices.offset, primitive.indices.length); @@ -421,49 +454,32 @@ export default class Mesh extends Asset { }); const referedVertexBuffers = primitive.vertexBundelIndices.map( - (i) => this._vertexBuffers[i]); + (i) => vertexBuffers[i]); - return gfxDevice.createInputAssembler({ - attributes: gfxAttributes, - vertexBuffers: referedVertexBuffers, - indexBuffer, - }); + return { + primitiveMode: toGFXTopology(primitive.topology), + inputAssembler: gfxDevice.createInputAssembler({ + attributes: gfxAttributes, + vertexBuffers: referedVertexBuffers, + indexBuffer, + }), + } as IRenderingSubmesh ; }); - this._subMeshes = submeshes; + this._renderingMesh = new RenderingMesh(renderingSubmeshes, vertexBuffers, indexBuffers); } - private _createVertexBuffers (gfxDevice: GFXDevice, data: ArrayBuffer) { - let nullish = false; - - const vertexBuffers = this._vertexBundles.map((vertexBundle) => { + private _createVertexBuffers (gfxDevice: GFXDevice, data: ArrayBuffer): GFXBuffer[] { + return this._vertexBundles.map((vertexBundle) => { const vertexBuffer = gfxDevice.createBuffer({ usage: GFXBufferUsageBit.VERTEX, memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE, size: vertexBundle.data.length, stride: vertexBundle.data.length / vertexBundle.verticesCount, }); - - if (!vertexBuffer) { - nullish = true; - return null; - } - vertexBuffer.update(new Uint8Array(data, vertexBundle.data.offset, vertexBundle.data.length)); return vertexBuffer; }); - - if (!nullish) { - this._vertexBuffers = vertexBuffers as GFXBuffer[]; - } else { - vertexBuffers.forEach((vertexBuffer) => { - if (vertexBuffer) { - vertexBuffer.destroy(); - } - }); - } - - return !nullish; } } cc.Mesh = Mesh; diff --git a/cocos/3d/misc/utils.js b/cocos/3d/misc/utils.js index f4f7bde08a..b9b9f948d5 100644 --- a/cocos/3d/misc/utils.js +++ b/cocos/3d/misc/utils.js @@ -1,7 +1,8 @@ import { renderer } from '../../renderer'; import find from '../../scene-graph/find'; -import Mesh from '../assets/mesh'; +import Mesh, { RenderingMesh } from '../assets/mesh'; import gfx from '../../renderer/gfx/index'; +import { GFXPrimitiveMode } from '../../gfx/define'; /** * @@ -44,7 +45,8 @@ function createJointsTexture(skinning) { function createMesh(context, data) { let ia = renderer.createIA(context, data); let meshAsset = new Mesh(); - meshAsset._subMeshes = [ia]; + const primitiveMode = data.primitiveMode === undefined ? GFXPrimitiveMode.TRIANGLE_LIST : data.primitiveMode; + meshAsset._renderingMesh = new RenderingMesh([{inputAssembler: ia, primitiveMode}], [], []); meshAsset._minPosition = data.minPos; meshAsset._maxPosition = data.maxPos; diff --git a/cocos/3d/primitive/box.ts b/cocos/3d/primitive/box.ts index 7617d64963..8d4661e587 100644 --- a/cocos/3d/primitive/box.ts +++ b/cocos/3d/primitive/box.ts @@ -131,7 +131,7 @@ export default function box (options: IBoxOptions): IGeometry { _buildPlane(2, ws, ls); // TOP return { - primitiveType: GFXPrimitiveMode.TRIANGLE_LIST, + primitiveMode: GFXPrimitiveMode.TRIANGLE_LIST, positions, normals, uvs, diff --git a/cocos/3d/primitive/defines.ts b/cocos/3d/primitive/defines.ts index 145b174f65..c189b4c6d2 100644 --- a/cocos/3d/primitive/defines.ts +++ b/cocos/3d/primitive/defines.ts @@ -17,7 +17,7 @@ export interface IGeometry { /** * Topology of the geometry vertices. */ - primitiveType: GFXPrimitiveMode; + primitiveMode: GFXPrimitiveMode; /** * Vertex positions.