diff --git a/cocos/3d/assets/mesh.js b/cocos/3d/assets/mesh.js deleted file mode 100644 index 16f7f76ddf..0000000000 --- a/cocos/3d/assets/mesh.js +++ /dev/null @@ -1,339 +0,0 @@ -/**************************************************************************** - Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. - - http://www.cocos.com - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated engine source code (the "Software"), a limited, - worldwide, royalty-free, non-assignable, revocable and non-exclusive license - to use Cocos Creator solely to develop games on your target platforms. You shall - not use Cocos Creator software for developing other software or tools that's - used for developing games. You are not granted to publish, distribute, - sublicense, and/or sell copies of Cocos Creator. - - The software or tools in this License Agreement are licensed, not sold. - Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. - ****************************************************************************/ - -// @ts-check -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"; - -/** - * 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 { - /** - * The data range of this bundle. - * This range of data is essentially mapped to a GPU vertex buffer. - * @type {BufferRange} - */ - @property(BufferRange) - _data = null; - - /** - * This bundle's vertices count. - * @type {number} - */ - @property(Number) - _verticesCount = 0; - - /** - * The attributes's formats. - * @type {{name: string, type: number, num: number, normalize: boolean}[]} - */ - @property - _formats = []; -} -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 { - - /** - * The vertex bundles that this primitive use. - * @type {number[]} - */ - @property([Number]) - _vertexBundelIndices = []; - - /** - * The indices data range of this primitive. - * @type {BufferRange} - */ - @property(BufferRange) - _indices = null; - - /** - * The type of this primitive's indices. - * @type {number} - */ - @property(Number) - _indexUnit = gfxEnums.INDEX_FMT_UINT16; - - /** - * This primitive's topology. - * @type {number} - */ - @property(Number) - _topology = gfxEnums.PT_TRIANGLES; -} -cc.Primitive = Primitive; - -@ccclass('cc.Mesh') -export default class Mesh extends Asset { - /** - * The vertex bundles that this mesh owns. - * @type {VertexBundle[]} - */ - @property([VertexBundle]) - _vertexBundles = []; - - /** - * @type {Primitive[]} - */ - @property([Primitive]) - _primitives = []; - - /** - * The min position of this mesh's vertices. - */ - @property(Vec3) - _minPosition = null; - - /** - * The max position of this mesh's vertices. - */ - @property(Vec3) - _maxPosition = null; - - constructor() { - super(); - - /** - * @type {GFXInputAssembler[]} - */ - this._subMeshes = null; - - /** - * @type {Uint8Array} - */ - this._data = null; - } - - get _nativeAsset() { - return this._data; - } - - set _nativeAsset(value) { - this._data = value; - } - - /** - * - */ - _lazyInitRenderResources() { - if (this._subMeshes != null) { - return; - } - - this._subMeshes = []; - - if (this._data === null) { - return; - } - const buffer = this._getBuffer(); - const vertexBuffers = this._vertexBundles.map((vertexBundle) => { - let 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]]; - - - let 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)); - - let attributes = []; - - let vertexBundle = this._vertexBundles[primitive._vertexBundelIndices[0]]; - for (let i = 0; i < vertexBundle._formats.length; i++) { - let 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. - */ - 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. - */ - getSubMesh(index) { - this._lazyInitRenderResources(); - return this._subMeshes[index]; - } - - get minPosition() { - return this._minPosition; - } - - 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; - // } - // } - // } - - /** - * @return {ArrayBuffer} - */ - _getBuffer() { - return this._data.buffer; - } - - _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; - } - return 1; - } -} -cc.Mesh = Mesh; diff --git a/cocos/3d/assets/mesh.ts b/cocos/3d/assets/mesh.ts new file mode 100644 index 0000000000..3fb1a594b0 --- /dev/null +++ b/cocos/3d/assets/mesh.ts @@ -0,0 +1,485 @@ +/**************************************************************************** + Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +// @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, GFXPrimitiveMode } from '../../gfx/define'; +import { GFXDevice } from '../../gfx/device'; +import { GFXInputAssembler, IGFXInputAttribute } from '../../gfx/input-assembler'; +import { IBufferRange } from './utils/buffer-range'; + +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 = GFXPrimitiveMode.POINT_LIST, + + /** + * Line list. + */ + LINE_LIST = GFXPrimitiveMode.LINE_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. + */ + 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. + */ + data: IBufferRange; + + /** + * This bundle's vertices count. + */ + verticesCount: number; + + /** + * Attributes. + */ + attributes: IVertexAttribute[]; +} + +/** + * A primitive is a geometry constituted with a list of + * same topology primitive graphic(such as points, lines or triangles). + */ +export interface IPrimitive { + /** + * The vertex bundles that this primitive use. + */ + vertexBundelIndices: number[]; + + /** + * The indices data range of this primitive. + */ + indices: IBufferRange; + + /** + * The type of this primitive's indices. + */ + indexUnit: IndexUnit; + + /** + * This primitive's topology. + */ + 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 { + + get _nativeAsset () { + return this._data; + } + + set _nativeAsset (value) { + this._data = value; + } + + /** + * Submeshes count of this mesh. + * @deprecated Use this.renderingMesh.subMeshCount instead. + */ + get subMeshCount () { + 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. + */ + @property + public _vertexBundles: IVertexBundle[] = []; + + /** + * The primitives that this mesh owns. + */ + @property + 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 _renderingMesh: RenderingMesh | null = null; + + constructor () { + super(); + } + + /** + * Destory this mesh and immediately release its video memory. + */ + public destroy () { + 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 index Index of the specified submesh. + * @deprecated Using this.renderingMesh() instead. + */ + public getSubMesh (index: number) { + const renderingMesh = this.renderingMesh; + return renderingMesh ? renderingMesh.getSubmesh(index) : null; + } + + private _lazyInitRenderResources () { + if (this._initialized) { + return; + } + + this._initialized = true; + + if (this._data === null) { + return; + } + + const buffer = this._data.buffer; + + const gfxDevice = cc.director.root.device as GFXDevice; + + const vertexBuffers = this._createVertexBuffers(gfxDevice, buffer); + + const indexBuffers: GFXBuffer[] = []; + + const renderingSubmeshes = 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), + }); + indexBuffers.push(indexBuffer); + + 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) => vertexBuffers[i]); + + return { + primitiveMode: toGFXTopology(primitive.topology), + inputAssembler: gfxDevice.createInputAssembler({ + attributes: gfxAttributes, + vertexBuffers: referedVertexBuffers, + indexBuffer, + }), + } as IRenderingSubmesh ; + }); + + this._renderingMesh = new RenderingMesh(renderingSubmeshes, vertexBuffers, indexBuffers); + } + + 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, + }); + vertexBuffer.update(new Uint8Array(data, vertexBundle.data.offset, vertexBundle.data.length)); + return vertexBuffer; + }); + } +} +cc.Mesh = Mesh; diff --git a/cocos/3d/assets/utils/buffer-range.js b/cocos/3d/assets/utils/buffer-range.ts similarity index 70% rename from cocos/3d/assets/utils/buffer-range.js rename to cocos/3d/assets/utils/buffer-range.ts index bbd720b2df..25f2f386ed 100644 --- a/cocos/3d/assets/utils/buffer-range.js +++ 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) - _offset = 0; - - /** - * The length of the range, in bytes. - * @type {Number} - */ - @property(Number) - _length = 0; - - constructor(offset, length) { - this._offset = offset; - this._length = length; - } +export interface IBufferRange { + offset: number; + length: number; } - -cc.BufferRange = BufferRange; \ No newline at end of file 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.