diff --git a/cocos/3d/assets/material.ts b/cocos/3d/assets/material.ts index 676ea135d6..1b9b95941d 100644 --- a/cocos/3d/assets/material.ts +++ b/cocos/3d/assets/material.ts @@ -26,14 +26,14 @@ import { Asset } from '../../assets/asset'; import { ccclass, property } from '../../core/data/class-decorator'; import { murmurhash2_32_gc } from '../../core/utils/murmurhash2_gc'; import { GFXBindingType } from '../../gfx/define'; -import { Effect } from '../../renderer/core/effect'; +import { Effect, IDefineMap } from '../../renderer/core/effect'; import { Pass, PassOverrides } from '../../renderer/core/pass'; import { RenderableComponent } from '../framework/renderable-component'; import { EffectAsset } from './effect-asset'; export interface IMaterialInfo { technique?: number; - defines?: Record; + defines?: IDefineMap[]; effectName?: string; } @@ -56,7 +56,7 @@ export class Material extends Asset { @property protected _techIdx = 0; @property - protected _defines: Record = {}; + protected _defines: IDefineMap[] = []; @property protected _props: Array> = []; @@ -68,7 +68,7 @@ export class Material extends Asset { super(); if (info) { if (info.technique) { this._techIdx = info.technique; } - if (info.defines) { this._defines = info.defines; } + if (info.defines) { this.setDefines(info.defines); } if (info.effectName) { this.effectName = info.effectName; } } } @@ -109,8 +109,8 @@ export class Material extends Asset { return this._hash; } - public setDefines (defines: Record) { - this._defines = defines; + public setDefines (defines: IDefineMap | IDefineMap[]) { + this._defines = Array.isArray(defines) ? defines : [defines]; this.update(); } diff --git a/cocos/3d/assets/mesh.ts b/cocos/3d/assets/mesh.ts index 6b463faeed..553632a416 100644 --- a/cocos/3d/assets/mesh.ts +++ b/cocos/3d/assets/mesh.ts @@ -293,6 +293,7 @@ export interface IGeometricInfo { export interface IRenderingSubmesh { vertexBuffers: GFXBuffer[]; indexBuffer: GFXBuffer; + indirectBuffer?: GFXBuffer; attributes: IGFXInputAttribute[]; primitiveMode: GFXPrimitiveMode; geometricInfo?: IGeometricInfo; diff --git a/cocos/3d/framework/model-component.js b/cocos/3d/framework/model-component.js index 3eda1188bc..5abf7ec4d6 100644 --- a/cocos/3d/framework/model-component.js +++ b/cocos/3d/framework/model-component.js @@ -182,20 +182,23 @@ export class ModelComponent extends RenderableComponent { } _updateModels() { - if (!this.node._scene) { + if (!this.enabled || !this.node._scene) { return; } + if (this._model) { + this._getRenderScene().destroyModel(this._model); + } + let model = this._createModel(); model.createBoundingShape(this._mesh.minPosition, this._mesh.maxPosition); this._model = model; - this._model.node = this.node; this._updateModelParams(); } _createModel() { - return this._getRenderScene().createModel(Model); + return this._getRenderScene().createModel(Model, this.node); } _updateModelParams() { diff --git a/cocos/3d/framework/particle/particle-system-component.js b/cocos/3d/framework/particle/particle-system-component.js index ef0f1ed6db..39e32fc75a 100644 --- a/cocos/3d/framework/particle/particle-system-component.js +++ b/cocos/3d/framework/particle/particle-system-component.js @@ -368,7 +368,7 @@ export default class ParticleSystemComponent extends Component { // } // } - this._particles.remove(i); + this._particles.removeAt(i); --i; continue; } diff --git a/cocos/3d/framework/particle/renderer/particle-system-renderer.ts b/cocos/3d/framework/particle/renderer/particle-system-renderer.ts index 0a312b1d41..66f301cfa4 100644 --- a/cocos/3d/framework/particle/renderer/particle-system-renderer.ts +++ b/cocos/3d/framework/particle/renderer/particle-system-renderer.ts @@ -1,28 +1,28 @@ +import { ccclass, executeInEditMode, property } from '../../../../core/data/class-decorator'; +import { Enum, Vec2 } from '../../../../core/value-types'; +import { mat4, vec2, vec3, vec4 } from '../../../../core/vmath'; +import { GFXAttributeName, GFXFormat } from '../../../../gfx/define'; +import { IGFXInputAttribute } from '../../../../gfx/input-assembler'; import * as renderer from '../../../../renderer'; -import { vec3, vec4, vec2, mat4 } from '../../../../core/vmath'; import gfx from '../../../../renderer/gfx'; +import ParticleBatchModel from '../../../../renderer/models/particle-batch-model'; import { Material } from '../../../assets/material'; import { RecyclePool } from '../../../memop'; import Particle from '../particle'; import { RenderableComponent } from '../../renderable-component'; -import { Enum, Vec2 } from '../../../../core/value-types'; -import { property, ccclass, executeInEditMode } from '../../../../core/data/class-decorator'; import { Space } from '../particle-general-function'; -import { GFXAttributeName, GFXFormat } from '../../../../gfx/define'; -import ParticleBatchModel from '../../../../renderer/models/particle-batch-model'; -import { IGFXInputAttribute } from '../../../../gfx/input-assembler'; // import ParticleSystemComponent from '../particle-system-component'; -let _tempAttribUV: vec3 = vec3.create(); -let _tempAttribUV0: vec2 = vec2.create(); -let _tempAttribColor: vec4 = vec4.create(); -let _tempWorldTrans: mat4 = mat4.create(); +const _tempAttribUV: vec3 = vec3.create(); +const _tempAttribUV0: vec2 = vec2.create(); +const _tempAttribColor: vec4 = vec4.create(); +const _tempWorldTrans: mat4 = mat4.create(); -let _uvs = [ +const _uvs = [ 0, 0, // bottom-left 1, 0, // bottom-right 0, 1, // top-left - 1, 1 // top-right + 1, 1, // top-right ]; const RenderMode = Enum({ @@ -30,32 +30,27 @@ const RenderMode = Enum({ StrecthedBillboard: 1, HorizontalBillboard: 2, VerticalBillboard: 3, - Mesh: 4 + Mesh: 4, }); -let USE_WORLD_SPACE = 'USE_WORLD_SPACE'; -let USE_BILLBOARD = 'USE_BILLBOARD'; -let USE_STRETCHED_BILLBOARD = 'USE_STRETCHED_BILLBOARD'; -let USE_HORIZONTAL_BILLBOARD = 'USE_HORIZONTAL_BILLBOARD'; -let USE_VERTICAL_BILLBOARD = 'USE_VERTICAL_BILLBOARD'; +const USE_WORLD_SPACE = 'USE_WORLD_SPACE'; +const USE_BILLBOARD = 'USE_BILLBOARD'; +const USE_STRETCHED_BILLBOARD = 'USE_STRETCHED_BILLBOARD'; +const USE_HORIZONTAL_BILLBOARD = 'USE_HORIZONTAL_BILLBOARD'; +const USE_VERTICAL_BILLBOARD = 'USE_VERTICAL_BILLBOARD'; @ccclass('cc.ParticleSystemRenderer') @executeInEditMode export default class ParticleSystemRenderer extends RenderableComponent { @property({ - type: RenderMode + type: RenderMode, }) - private _renderMode = RenderMode.Billboard; - - @property({ - type: RenderMode - }) - public get renderMode() { + public get renderMode () { return this._renderMode; } - public set renderMode(val) { + public set renderMode (val) { if (this._renderMode === val) { return; } @@ -65,43 +60,47 @@ export default class ParticleSystemRenderer extends RenderableComponent { } @property - private _velocityScale = 1; - - @property - public get velocityScale() { + public get velocityScale () { return this._velocityScale; } - public set velocityScale(val) { + public set velocityScale (val) { this._velocityScale = val; this._updateMaterialParams(); this._updateModel(); } @property - private _lengthScale = 1; - - @property - public get lengthScale() { + public get lengthScale () { return this._lengthScale; } - public set lengthScale(val) { + public set lengthScale (val) { this._lengthScale = val; this._updateMaterialParams(); this._updateModel(); } + @property({ + type: RenderMode, + }) + private _renderMode = RenderMode.Billboard; + + @property + private _velocityScale = 1; + + @property + private _lengthScale = 1; + private _defines: { [index: string]: boolean }; private _model: ParticleBatchModel | null; private frameTile: Vec2; - private attrs: [any]; + private attrs: any[]; private _vertAttrs: IGFXInputAttribute[]; private particleSystem: any; private _particles: RecyclePool; - - constructor() { + constructor () { super(); this._model = null; @@ -111,18 +110,18 @@ export default class ParticleSystemRenderer extends RenderableComponent { { name: GFXAttributeName.ATTR_POSITION, format: GFXFormat.RGB32F }, { name: GFXAttributeName.ATTR_TEX_COORD, format: GFXFormat.RGB32F }, { name: GFXAttributeName.ATTR_TEX_COORD1, format: GFXFormat.RG32F }, - { name: GFXAttributeName.ATTR_COLOR, format: GFXFormat.RGBA8, isNormalized: true } + { name: GFXAttributeName.ATTR_COLOR, format: GFXFormat.RGBA8, isNormalized: true }, ]; this._defines = { USE_WORLD_SPACE: true, USE_BILLBOARD: true, USE_STRETCHED_BILLBOARD: false, USE_HORIZONTAL_BILLBOARD: false, - USE_VERTICAL_BILLBOARD: false - } + USE_VERTICAL_BILLBOARD: false, + }; } - onInit() { + public onInit () { this.particleSystem = this.node.getComponent('cc.ParticleSystemComponent'); this._particles = new RecyclePool(() => { return new Particle(this); @@ -136,34 +135,138 @@ export default class ParticleSystemRenderer extends RenderableComponent { this._updateModel(); } - onEnable() { + public onEnable () { if (!this.particleSystem) { return; } if (this._model == null) { - this._model = this._getRenderScene().createModel(ParticleBatchModel); + this._model = this._getRenderScene().createModel(ParticleBatchModel, this.node) as ParticleBatchModel; this._model.setCapacity(this.particleSystem.capacity); this._model.setVertexAttributes(this._vertAttrs); this._model.node = this.node; } } - onDisable() { + public onDisable () { this._getRenderScene().destroyModel(this._model!); } - onDestroy() { + public onDestroy () { this._model!.destroy(); } - _updateMaterialParams() { + public _getFreeParticle () { + if (this._particles.length >= this.particleSystem._capacity) { + return null; + } + return this._particles.add(); + } + + public _setNewParticle (p) { + + } + + public _updateParticles (dt) { + this.node.getWorldMatrix(_tempWorldTrans); + if (this.particleSystem.velocityOvertimeModule.enable) { + this.particleSystem.velocityOvertimeModule.update(this.particleSystem._simulationSpace, _tempWorldTrans); + } + if (this.particleSystem.forceOvertimeModule.enable) { + this.particleSystem.forceOvertimeModule.update(this.particleSystem._simulationSpace, _tempWorldTrans); + } + for (let i = 0; i < this._particles.length; ++i) { + const p = this._particles.data[i]; + p.remainingLifetime -= dt; + vec3.set(p.animatedVelocity, 0, 0, 0); + + if (p.remainingLifetime < 0.0) { + this._particles.removeAt(i); + --i; + continue; + } + + p.velocity.y -= this.particleSystem.gravityModifier.evaluate(1 - p.remainingLifetime / p.startLifetime, p.randomSeed) * 9.8 * dt; // apply gravity. + if (this.particleSystem.sizeOvertimeModule.enable) { + this.particleSystem.sizeOvertimeModule.animate(p); + } + if (this.particleSystem.colorOverLifetimeModule.enable) { + this.particleSystem.colorOverLifetimeModule.animate(p); + } + if (this.particleSystem.forceOvertimeModule.enable) { + this.particleSystem.forceOvertimeModule.animate(p, dt); + } + if (this.particleSystem.velocityOvertimeModule.enable) { + this.particleSystem.velocityOvertimeModule.animate(p); + } else { + vec3.copy(p.ultimateVelocity, p.velocity); + } + if (this.particleSystem.limitVelocityOvertimeModule.enable) { + this.particleSystem.limitVelocityOvertimeModule.animate(p); + } + if (this.particleSystem.rotationOvertimeModule.enable) { + this.particleSystem.rotationOvertimeModule.animate(p, dt); + } + if (this.particleSystem.textureAnimationModule.enable) { + this.particleSystem.textureAnimationModule.animate(p); + } + vec3.scaleAndAdd(p.position, p.position, p.ultimateVelocity, dt); // apply velocity. + } + } + + // internal function + public _updateRenderData () { + // update vertex buffer + let idx = 0; + const uploadVel = this._renderMode === RenderMode.StrecthedBillboard; + for (let i = 0; i < this._particles.length; ++i) { + const p = this._particles.data[i]; + let fi = 0; + if (this.particleSystem.textureAnimationModule.enable) { + fi = p.frameIndex; + } + idx = i * 4; + let attrNum = 0; + for (let j = 0; j < 4; ++j) { // four verts per particle. + attrNum = 0; + this.attrs[attrNum++] = p.position; + _tempAttribUV.x = _uvs[2 * j]; + _tempAttribUV.y = _uvs[2 * j + 1]; + _tempAttribUV.z = fi; + this.attrs[attrNum++] = _tempAttribUV; + _tempAttribUV0.x = p.size.x; + _tempAttribUV0.y = p.rotation.x; + this.attrs[attrNum++] = _tempAttribUV0; + this.attrs[attrNum++] = p.color._val; + + if (uploadVel) { + this.attrs[attrNum++] = p.ultimateVelocity; + } + + this._model!.addParticleVertexData(idx++, this.attrs); + } + } + + // because we use index buffer, per particle index count = 6. + this._model!.updateIA(this._particles.length * 6); + } + + public updateShaderUniform () { + + } + + public _onMaterialModified (index, material) { + this._updateMaterialParams(); + this._updateModel(); + } + + private _updateMaterialParams () { if (!this.particleSystem) { return; } if (this.particleSystem._simulationSpace === Space.World) { this._defines[USE_WORLD_SPACE] = true; } else { - this._defines[USE_WORLD_SPACE] = true; + this._defines[USE_WORLD_SPACE] = false; } if (this._renderMode === RenderMode.Billboard) { @@ -195,129 +298,25 @@ export default class ParticleSystemRenderer extends RenderableComponent { if (this.particleSystem.textureAnimationModule.enable) { this.sharedMaterial!.setProperty('frameTile', vec2.set(this.frameTile, this.particleSystem.textureAnimationModule.numTilesX, this.particleSystem.textureAnimationModule.numTilesY)); - } - else { + } else { this.sharedMaterial!.setProperty('frameTile', this.frameTile); } } - _updateModel() { + private _updateModel () { if (!this.particleSystem) { return; } - this._model!.material = this.sharedMaterial ? this.sharedMaterial : null; - if (Object.getPrototypeOf(this).constructor.name === 'ParticleSystemGpuRenderer') + this._model!.setSubModelMaterial(0, this.sharedMaterial ? this.sharedMaterial : null); + if (Object.getPrototypeOf(this).constructor.name === 'ParticleSystemGpuRenderer') { return; + } if (this._renderMode === RenderMode.StrecthedBillboard) { this._model!.enableStretchedBillboard(); } else { this._model!.disableStretchedBillboard(); } } - - _getFreeParticle() { - if (this._particles.length >= this.particleSystem._capacity) - return null; - return this._particles.add(); - } - - _setNewParticle(p) { - - } - - _updateParticles(dt) { - this.node.getWorldMatrix(_tempWorldTrans); - if (this.particleSystem.velocityOvertimeModule.enable) { - this.particleSystem.velocityOvertimeModule.update(this.particleSystem._simulationSpace, _tempWorldTrans); - } - if (this.particleSystem.forceOvertimeModule.enable) { - this.particleSystem.forceOvertimeModule.update(this.particleSystem._simulationSpace, _tempWorldTrans); - } - for (let i = 0; i < this._particles.length; ++i) { - let p = this._particles.data[i]; - p.remainingLifetime -= dt; - vec3.set(p.animatedVelocity, 0, 0, 0); - - if (p.remainingLifetime < 0.0) { - this._particles.remove(i); - --i; - continue; - } - - p.velocity.y -= this.particleSystem.gravityModifier.evaluate(1 - p.remainingLifetime / p.startLifetime, p.randomSeed) * 9.8 * dt; // apply gravity. - if (this.particleSystem.sizeOvertimeModule.enable) { - this.particleSystem.sizeOvertimeModule.animate(p); - } - if (this.particleSystem.colorOverLifetimeModule.enable) { - this.particleSystem.colorOverLifetimeModule.animate(p); - } - if (this.particleSystem.forceOvertimeModule.enable) { - this.particleSystem.forceOvertimeModule.animate(p, dt); - } - if (this.particleSystem.velocityOvertimeModule.enable) { - this.particleSystem.velocityOvertimeModule.animate(p); - } - else { - vec3.copy(p.ultimateVelocity, p.velocity); - } - if (this.particleSystem.limitVelocityOvertimeModule.enable) { - this.particleSystem.limitVelocityOvertimeModule.animate(p); - } - if (this.particleSystem.rotationOvertimeModule.enable) { - this.particleSystem.rotationOvertimeModule.animate(p, dt); - } - if (this.particleSystem.textureAnimationModule.enable) { - this.particleSystem.textureAnimationModule.animate(p); - } - vec3.scaleAndAdd(p.position, p.position, p.ultimateVelocity, dt); // apply velocity. - } - } - - // internal function - _updateRenderData() { - // update vertex buffer - let idx = 0; - let uploadVel = this._renderMode == RenderMode.StrecthedBillboard; - for (let i = 0; i < this._particles.length; ++i) { - let p = this._particles.data[i]; - let fi = 0; - if (this.particleSystem.textureAnimationModule.enable) { - fi = p.frameIndex; - } - idx = i * 4; - let attrNum = 0; - for (let j = 0; j < 4; ++j) { // four verts per particle. - attrNum = 0; - this.attrs[attrNum++] = p.position; - _tempAttribUV.x = _uvs[2 * j]; - _tempAttribUV.y = _uvs[2 * j + 1]; - _tempAttribUV.z = fi; - this.attrs[attrNum++] = _tempAttribUV; - _tempAttribUV0.x = p.size.x; - _tempAttribUV0.y = p.rotation.x; - this.attrs[attrNum++] = _tempAttribUV0; - this.attrs[attrNum++] = p.color._val; - - if (uploadVel) { - this.attrs[attrNum++] = p.ultimateVelocity; - } - - this._model!.addParticleVertexData(idx++, this.attrs); - } - } - - // because we use index buffer, per particle index count = 6. - this._model!.updateIA(this._particles.length * 6); - } - - updateShaderUniform() { - - } - - _onMaterialModified(index, material) { - this._updateMaterialParams(); - this._updateModel(); - } } Object.assign(ParticleSystemRenderer, { uv: _uvs }); diff --git a/cocos/3d/framework/renderable-component.ts b/cocos/3d/framework/renderable-component.ts index 33fd72015b..3a66658077 100644 --- a/cocos/3d/framework/renderable-component.ts +++ b/cocos/3d/framework/renderable-component.ts @@ -99,11 +99,11 @@ export class RenderableComponent extends Component { this._onMaterialModified(index, material); } - public _onMaterialModified (index: number, material: Material | null) { + protected _onMaterialModified (index: number, material: Material | null) { } - public _clearMaterials () { + private _clearMaterials () { } } diff --git a/cocos/3d/memop/recycle-pool.ts b/cocos/3d/memop/recycle-pool.ts index 5d231f143e..fb48e86b17 100644 --- a/cocos/3d/memop/recycle-pool.ts +++ b/cocos/3d/memop/recycle-pool.ts @@ -4,13 +4,16 @@ export default class RecyclePool { private _fn: () => T; private _count = 0; private _data: T[]; + private _objIdx: Map; constructor (fn: () => T, size: number) { this._fn = fn; this._data = new Array(size); + this._objIdx = new Map(); for (let i = 0; i < size; ++i) { this._data[i] = fn(); + this._objIdx.set(this._data[i], i); } } @@ -51,13 +54,14 @@ export default class RecyclePool { const tmp = this._data[idx]; this._data[idx] = this._data[last]; this._data[last] = tmp; + this._objIdx.set(this._data[idx], idx); + this._objIdx.set(this._data[last], last); this._count -= 1; } - public remove (element: T) { - const idx = this.data.indexOf(element); - if (idx >= 0) { - this.removeAt(idx); + public remove (obj: T) { + if (this._objIdx.has(obj)) { + this.removeAt(this._objIdx.get(obj)); } } diff --git a/cocos/components/component.d.ts b/cocos/components/component.d.ts index 746c5890cc..a08b6af267 100644 --- a/cocos/components/component.d.ts +++ b/cocos/components/component.d.ts @@ -28,7 +28,7 @@ import { CCObject } from '../core/data/object'; import { getClassName, value } from '../core/utils/js'; import IDGenerator from '../core/utils/id-generator'; import { ccclass, property } from '../core/data/class-decorator'; -import { Node } from '../scene-graph/node'; +import { Node } from '../scene-graph'; import { RenderScene } from '../renderer/scene/render-scene'; export class Component extends CCObject { diff --git a/cocos/gfx/webgl/webgl-commands.ts b/cocos/gfx/webgl/webgl-commands.ts index acaf959180..045d3a6d8a 100644 --- a/cocos/gfx/webgl/webgl-commands.ts +++ b/cocos/gfx/webgl/webgl-commands.ts @@ -2081,7 +2081,7 @@ export function WebGLCmdFuncExecuteCmds (device: WebGLGFXDevice, cmdPackage: Web if (gpuInputAssembler.gpuIndirectBuffer) { for (const drawInfo of gpuInputAssembler.gpuIndirectBuffer.indirects) { const gpuBuffer = gpuInputAssembler.gpuIndexBuffer; - if (gpuBuffer && drawInfo.indexCount > 0) { + if (gpuBuffer && drawInfo.indexCount > -1) { const offset = drawInfo.firstIndex * gpuBuffer.stride; gl.drawElements(glPrimitive, drawInfo.indexCount, gpuInputAssembler.glIndexType, offset); } else { diff --git a/cocos/renderer/core/effect.ts b/cocos/renderer/core/effect.ts index 1300a0ba6d..82f375b1de 100644 --- a/cocos/renderer/core/effect.ts +++ b/cocos/renderer/core/effect.ts @@ -7,7 +7,7 @@ import { IPassInfoFull, Pass } from './pass'; export interface IDefineMap { [name: string]: number | boolean; } export interface IEffectInfo { techIdx?: number; - defines?: IDefineMap; + defines?: IDefineMap[]; } export class Effect { @@ -24,7 +24,7 @@ export class Effect { for (let k = 0; k < passNum; ++k) { const passInfo = tech.passes[k] as IPassInfoFull; const prog = cc.game._programLib.getTemplate(passInfo.program); - passInfo.shader = cc.game._programLib.getGFXShader(passInfo.program, defines || {}); + passInfo.shader = cc.game._programLib.getGFXShader(passInfo.program, defines && defines[k] || {}); passInfo.renderPass = cc.director.root.pipeline.getRenderPass(passInfo.stage || RenderPassStage.DEFAULT); passInfo.globals = cc.director.root.pipeline.globalUBO; passInfo.blocks = prog!.blocks; diff --git a/cocos/renderer/models/particle-batch-model.ts b/cocos/renderer/models/particle-batch-model.ts index f9a0b6aedd..d56794d8e2 100644 --- a/cocos/renderer/models/particle-batch-model.ts +++ b/cocos/renderer/models/particle-batch-model.ts @@ -6,9 +6,10 @@ import { GFX_DRAW_INFO_SIZE, GFXBuffer, IGFXIndirectBuffer } from '../../gfx/buf import { GFXAttributeName, GFXBufferUsageBit, GFXFormat, GFXFormatInfos, GFXMemoryUsageBit, GFXPrimitiveMode } from '../../gfx/define'; import { IGFXInputAttribute } from '../../gfx/input-assembler'; -import { Node } from '../../scene-graph/node'; +import { Node } from '../../scene-graph'; import { Model } from '../scene/model'; import { RenderScene } from '../scene/render-scene'; +import { IRenderingSubmesh } from '../../3d/assets/mesh'; export default class ParticleBatchModel extends Model { @@ -21,6 +22,7 @@ export default class ParticleBatchModel extends Model { private _vdataUint32: Uint32Array | null; private _iaInfo: IGFXIndirectBuffer; private _iaInfoBuffer: GFXBuffer; + private _subMeshData: IRenderingSubmesh | null; constructor (scene: RenderScene, node: Node) { super(scene, node); @@ -50,6 +52,7 @@ export default class ParticleBatchModel extends Model { size: GFX_DRAW_INFO_SIZE, stride: 1, }); + this._subMeshData = null; } public setCapacity (capacity: number) { @@ -60,17 +63,20 @@ export default class ParticleBatchModel extends Model { this._vertAttrs = attrs; this._vertSize = 0; for (const a of this._vertAttrs) { - this._vertSize += GFXFormatInfos[a.format].size; + this._vertSize += GFXFormatInfos[a.format].size * GFXFormatInfos[a.format].count; } // rebuid - this._vBuffer = this._createInputAssembler(); + this._vBuffer = this._createSubMeshData(); this._vertAttrsFloatCount = this._vertSize / 4; // number of float this._vBuffer = new ArrayBuffer(this._capacity * this._vertSize * 4); this._vdataF32 = new Float32Array(this._vBuffer); this._vdataUint32 = new Uint32Array(this._vBuffer); } - public _createInputAssembler (): ArrayBuffer { + public _createSubMeshData (): ArrayBuffer { + if (this._subMeshData) { + this.destroySubMeshData(); + } const vertexBuffer = this._device.createBuffer({ usage: GFXBufferUsageBit.VERTEX | GFXBufferUsageBit.TRANSFER_DST, memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE, @@ -105,15 +111,14 @@ export default class ParticleBatchModel extends Model { this._iaInfo.drawInfos[0].indexCount = this._capacity * 6; this._iaInfoBuffer.update(this._iaInfo); - this.subMeshData = { - inputAssembler: this._device.createInputAssembler({ - attributes: this._vertAttrs!, - vertexBuffers: [vertexBuffer], - indexBuffer, - indirectBuffer: this._iaInfoBuffer, - }), + this._subMeshData = { + vertexBuffers: [vertexBuffer], + indexBuffer, + indirectBuffer: this._iaInfoBuffer, + attributes: this._vertAttrs!, primitiveMode: GFXPrimitiveMode.TRIANGLE_LIST, }; + this.setSubModelMesh(0, this._subMeshData); return vBuffer; } @@ -131,7 +136,7 @@ export default class ParticleBatchModel extends Model { } } - public addParticleVertexData (index: number, pvdata: [any]) { + public addParticleVertexData (index: number, pvdata: any[]) { // if (pvdata.length !== this._vertAttrs.length) { // console.error('particle vertex stream data not match.'); // } @@ -160,19 +165,27 @@ export default class ParticleBatchModel extends Model { } public updateIA (count: number) { - this._subMeshObject!.inputAssembler.vertexBuffers[0].update(this._vdataF32!); - this._subMeshObject!.inputAssembler.indexCount = count; - this._subMeshObject!.inputAssembler.extractDrawInfo(this._iaInfo.drawInfos[0]); + this.getSubModel(0).inputAssembler!.vertexBuffers[0].update(this._vdataF32!); + this.getSubModel(0).inputAssembler!.indexCount = count; + this.getSubModel(0).inputAssembler!.extractDrawInfo(this._iaInfo.drawInfos[0]); this._iaInfoBuffer.update(this._iaInfo); } public clear () { - this._subMeshObject!.inputAssembler.indexCount = 0; + this.getSubModel(0).inputAssembler!.indexCount = 0; + } + + private destroySubMeshData () { + for (const vb of this._subMeshData!.vertexBuffers) { + vb.destroy(); + } + this._subMeshData!.indexBuffer.destroy(); } public destroy () { + super.destroy(); this._vdataF32 = null; - this._subMeshObject!.inputAssembler.destroy(); + this.destroySubMeshData(); this._iaInfoBuffer.destroy(); } } diff --git a/cocos/renderer/scene/model.ts b/cocos/renderer/scene/model.ts index 6b785fdb5a..b601af39d1 100644 --- a/cocos/renderer/scene/model.ts +++ b/cocos/renderer/scene/model.ts @@ -69,6 +69,8 @@ export class Model { subModel.destroy(); _subMeshPool.remove(subModel); } + this._matPSORecord.clear(); + this._matRefCount.clear(); } set scene (scene: RenderScene) { @@ -93,14 +95,16 @@ export class Model { } public _updateTransform () { - if (!this._node.hasChanged) { return; } - this._node.updateWorldTransformFull(); - if (!this._modelBounds) { return; } + if (this._node.hasChanged) { + this._node.updateWorldTransformFull(); + if (this._modelBounds) { - this._modelBounds.transform( - // @ts-ignore - this._node._mat, this._node._pos, this._node._rot, this._node._scale, - this._worldBounds); + this._modelBounds.transform( + // @ts-ignore + this._node._mat, this._node._pos, this._node._rot, this._node._scale, + this._worldBounds); + } + } } public updateUBOs () { @@ -182,19 +186,30 @@ export class Model { this._subModels[idx].initialize(subMeshData, mat, this._matPSORecord.get(mat)!); } - public setSubModelMaterial (idx: number, mat: Material) { + public setSubModelMesh (idx: number, subMeshData: IRenderingSubmesh) { + if (this._subModels[idx] == null) { + this._subModels[idx] = _subMeshPool.add(); + } + this._subModels[idx].subMeshData = subMeshData; + } + + public setSubModelMaterial (idx: number, mat: Material | null) { if (this._subModels[idx] == null) { return; } if (this._subModels[idx].material === mat) { - this.destroyPipelineState(mat, this._matPSORecord.get(mat)!); - this._matPSORecord.set(mat, this.createPipelineState(mat)); + this.destroyPipelineState(mat!, this._matPSORecord.get(mat!)!); + this._matPSORecord.set(mat!, this.createPipelineState(mat!)); } else { - this.releasePSO(this._subModels[idx].material); - this.allocatePSO(mat); + if (this._subModels[idx].material) { + this.releasePSO(this._subModels[idx].material!); + } + if (mat) { + this.allocatePSO(mat); + } } + this._subModels[idx]._psos = mat ? this._matPSORecord.get(mat)! : null; this._subModels[idx].material = mat; - this._subModels[idx].psos = this._matPSORecord.get(mat)!; } protected createPipelineState (mat: Material): GFXPipelineState[] { diff --git a/cocos/renderer/scene/submodel.ts b/cocos/renderer/scene/submodel.ts index 00962ea902..0006f0e737 100644 --- a/cocos/renderer/scene/submodel.ts +++ b/cocos/renderer/scene/submodel.ts @@ -9,7 +9,7 @@ import { Pass } from '../core/pass'; export class SubModel { protected _subMeshObject: IRenderingSubmesh | null; - private _inputAssembler: GFXInputAssembler | null; + protected _inputAssembler: GFXInputAssembler | null; private _material: Material | null; private _cmdBuffers: GFXCommandBuffer[]; private _psos: GFXPipelineState[] | null; @@ -27,13 +27,6 @@ export class SubModel { public initialize (subMesh: IRenderingSubmesh, mat: Material, psos: GFXPipelineState[]) { this._psos = psos; this.subMeshData = subMesh; - const iaInfo = {} as IGFXInputAssemblerInfo; - iaInfo.attributes = this._subMeshObject!.attributes; - iaInfo.vertexBuffers = this._subMeshObject!.vertexBuffers; - if (this._subMeshObject!.indexBuffer) { - iaInfo.indexBuffer = this._subMeshObject!.indexBuffer; - } - this._inputAssembler = (cc.director.root.device as GFXDevice).createInputAssembler(iaInfo); this.material = mat; } @@ -42,10 +35,29 @@ export class SubModel { if (this._inputAssembler) { this._inputAssembler.destroy(); } + for (let i = 0; i < this.passes.length; i++) { + this.passes[i].destroyPipelineState(this._psos![i]); + } + for (const cmdBuffer of this._cmdBuffers) { + cmdBuffer.destroy(); + } } set subMeshData (sm: IRenderingSubmesh) { + if (this._inputAssembler) { + this._inputAssembler.destroy(); + } this._subMeshObject = sm; + const iaInfo = {} as IGFXInputAssemblerInfo; + iaInfo.attributes = this._subMeshObject!.attributes; + iaInfo.vertexBuffers = this._subMeshObject!.vertexBuffers; + if (this._subMeshObject!.indexBuffer) { + iaInfo.indexBuffer = this._subMeshObject!.indexBuffer; + } + if (this._subMeshObject!.indirectBuffer) { + iaInfo.indirectBuffer = this._subMeshObject!.indirectBuffer; + } + this._inputAssembler = (cc.director.root.device as GFXDevice).createInputAssembler(iaInfo); } get subMeshData () { @@ -60,14 +72,14 @@ export class SubModel { this._psos = val; } - set material (material: Material) { + set material (material: Material | null) { this._material = material; if (material == null) { return; } for (let i = 0; i < this._material!.passes.length; i++) { if (this._material!.passes[i].primitive !== this._subMeshObject!.primitiveMode) { - cc.error('the model(%d)\'s primitive type doesn\'t match its pass\'s', i); + cc.error(`the model(%d)'s primitive type doesn't match its pass's`, i); } this.recordCommandBuffer(i); } @@ -79,10 +91,14 @@ export class SubModel { } } - get material (): Material { + get material (): Material | null { return this._material!; } + get inputAssembler (): GFXInputAssembler | null { + return this._inputAssembler; + } + get castShadow (): boolean { return this._castShadow; } diff --git a/playground/gizmo.html b/playground/gizmo.html index faf583c1dc..774bbb2486 100644 --- a/playground/gizmo.html +++ b/playground/gizmo.html @@ -52,12 +52,13 @@ cameraNode.parent = scene; cameraNode.setPosition(20, 20, 20); cameraNode.lookAt(cc.v3()); - cameraNode.addComponent(FirstPersonCamera); + // cameraNode.addComponent(FirstPersonCamera); const camComp = cameraNode.addComponent(cc.CameraComponent); - const material = new cc.Material(); - material.setDefines({ USE_COLOR: true }); - material.effectName = 'builtin-effect-unlit'; + const material = new cc.Material({ + effectName: 'builtin-effect-unlit', + defines: { USE_COLOR: true }, + }); const createArrow = (color, rotation) => {