From dda6f56b797bace748a6ad429f2b2863531a3457 Mon Sep 17 00:00:00 2001 From: zxg0622 <247274249@qq.com> Date: Fri, 29 Nov 2019 13:43:03 +0800 Subject: [PATCH] change particle rendermode defines & adapt line billboard to new renderscene interface (#5806) --- cocos/core/renderer/scene/camera.ts | 8 +++- cocos/particle/billboard-component.ts | 28 ++++++++++--- cocos/particle/line-component.ts | 42 ++++++++++++++----- cocos/particle/models/line-model.ts | 4 +- .../renderer/particle-system-renderer.ts | 42 ++++++------------- cocos/ui/components/ui-model-component.ts | 2 +- 6 files changed, 74 insertions(+), 52 deletions(-) diff --git a/cocos/core/renderer/scene/camera.ts b/cocos/core/renderer/scene/camera.ts index c6d3d47244..ae8416dd3d 100644 --- a/cocos/core/renderer/scene/camera.ts +++ b/cocos/core/renderer/scene/camera.ts @@ -161,12 +161,16 @@ export class Camera { public attachToScene (scene: RenderScene) { this._scene = scene; - if (this._view) { this._view.enable(true); } + if (this._view) { + this._view.enable(true); + } } public detachFromScene () { this._scene = null; - if (this._view) { this._view.enable(false); } + if (this._view) { + this._view.enable(false); + } } public resize (width: number, height: number) { diff --git a/cocos/particle/billboard-component.ts b/cocos/particle/billboard-component.ts index 2a9c57b0d4..fd30f766d2 100644 --- a/cocos/particle/billboard-component.ts +++ b/cocos/particle/billboard-component.ts @@ -117,10 +117,12 @@ export class BillboardComponent extends Component { super(); } + public onLoad () { + this.createModel(); + } + public onEnable () { - if (!this._model) { - this.createModel(); - } + this.attachToScene(); this._model!.enabled = true; this.width = this._width; this.height = this._height; @@ -129,8 +131,21 @@ export class BillboardComponent extends Component { } public onDisable () { - if (this._model) { - this._model.enabled = false; + this.detachFromScene(); + } + + private attachToScene () { + if (this._model && this.node && this.node.scene) { + if (this._model.scene) { + this.detachFromScene(); + } + this._getRenderScene().addModel(this._model); + } + } + + private detachFromScene () { + if (this._model && this._model.scene) { + this._model.scene.removeModel(this._model); } } @@ -157,7 +172,8 @@ export class BillboardComponent extends Component { ], indices: [0, 1, 2, 1, 2, 3], }, undefined, { calculateBounds: false }); - this._model = this._getRenderScene().createModel(Model, this.node); + this._model = cc.director.root.createModel(Model, this.node); + this._model.initialize(this.node); if (this._material == null) { this._material = new Material(); this._material.copy(builtinResMgr.get('default-billboard-material')); diff --git a/cocos/particle/line-component.ts b/cocos/particle/line-component.ts index b136fc08d5..2842977a9c 100644 --- a/cocos/particle/line-component.ts +++ b/cocos/particle/line-component.ts @@ -199,19 +199,24 @@ export class LineComponent extends Component { super(); } + public onLoad () { + this._model = cc.director.root.createModel(LineModel); + this._model.initialize(this.node); + this._model.setCapacity(100); + if (this._material == null) { + this._material = new Material(); + this._material.copy(builtinResMgr.get('default-trail-material')); + define[CC_USE_WORLD_SPACE] = this.worldSpace; + this._material.recompileShaders(define); + } + this._model.setSubModelMaterial(0, this._material!); + } + public onEnable () { if (!this._model) { - this._model = this._getRenderScene().createModel(LineModel, this.node); - this._model.setCapacity(100); - if (this._material == null) { - this._material = new Material(); - this._material.copy(builtinResMgr.get('default-trail-material')); - define[CC_USE_WORLD_SPACE] = this.worldSpace; - this._material.recompileShaders(define); - } - this._model.setSubModelMaterial(0, this._material!); + return; } - this._model!.enabled = true; + this.attachToScene(); this.texture = this.texture; this.tile = this._tile; this.offset = this._offset; @@ -220,7 +225,22 @@ export class LineComponent extends Component { public onDisable () { if (this._model) { - this._model.enabled = false; + this.detachFromScene(); + } + } + + private attachToScene () { + if (this._model && this.node && this.node.scene) { + if (this._model.scene) { + this.detachFromScene(); + } + this._getRenderScene().addModel(this._model); + } + } + + private detachFromScene () { + if (this._model && this._model.scene) { + this._model.scene.removeModel(this._model); } } } diff --git a/cocos/particle/models/line-model.ts b/cocos/particle/models/line-model.ts index 7eb5929e6d..000993fa02 100644 --- a/cocos/particle/models/line-model.ts +++ b/cocos/particle/models/line-model.ts @@ -36,8 +36,8 @@ export class LineModel extends Model { private _vertCount: number = 0; private _indexCount: number = 0; - constructor (scene: RenderScene, node: INode) { - super(scene, node); + constructor () { + super(); this._capacity = 100; this._iaInfo = { drawInfos: [{ diff --git a/cocos/particle/renderer/particle-system-renderer.ts b/cocos/particle/renderer/particle-system-renderer.ts index 294271ff31..c1c248d04a 100644 --- a/cocos/particle/renderer/particle-system-renderer.ts +++ b/cocos/particle/renderer/particle-system-renderer.ts @@ -23,11 +23,13 @@ const _uvs = [ ]; const CC_USE_WORLD_SPACE = 'CC_USE_WORLD_SPACE'; -const CC_USE_BILLBOARD = 'CC_USE_BILLBOARD'; -const CC_USE_STRETCHED_BILLBOARD = 'CC_USE_STRETCHED_BILLBOARD'; -const CC_USE_HORIZONTAL_BILLBOARD = 'CC_USE_HORIZONTAL_BILLBOARD'; -const CC_USE_VERTICAL_BILLBOARD = 'CC_USE_VERTICAL_BILLBOARD'; -const CC_USE_MESH = 'CC_USE_MESH'; + +const CC_RENDER_MODE = 'CC_RENDER_MODE'; +const RENDER_MODE_BILLBOARD = 0; +const RENDER_MODE_STRETCHED_BILLBOARD = 1; +const RENDER_MODE_HORIZONTAL_BILLBOARD = 2; +const RENDER_MODE_VERTICAL_BILLBOARD = 3; +const RENDER_MODE_MESH = 4; const _vertex_attrs = [ { name: GFXAttributeName.ATTR_POSITION, format: GFXFormat.RGB32F }, // position @@ -466,37 +468,17 @@ export default class ParticleSystemRenderer { } if (this._renderMode === RenderMode.Billboard) { - this._defines[CC_USE_BILLBOARD] = true; - this._defines[CC_USE_STRETCHED_BILLBOARD] = false; - this._defines[CC_USE_HORIZONTAL_BILLBOARD] = false; - this._defines[CC_USE_VERTICAL_BILLBOARD] = false; - this._defines[CC_USE_MESH] = false; + this._defines[CC_RENDER_MODE] = RENDER_MODE_BILLBOARD; } else if (this._renderMode === RenderMode.StrecthedBillboard) { - this._defines[CC_USE_BILLBOARD] = false; - this._defines[CC_USE_STRETCHED_BILLBOARD] = true; - this._defines[CC_USE_HORIZONTAL_BILLBOARD] = false; - this._defines[CC_USE_VERTICAL_BILLBOARD] = false; - this._defines[CC_USE_MESH] = false; + this._defines[CC_RENDER_MODE] = RENDER_MODE_STRETCHED_BILLBOARD; this.frameTile_velLenScale.z = this._velocityScale; this.frameTile_velLenScale.w = this._lengthScale; } else if (this._renderMode === RenderMode.HorizontalBillboard) { - this._defines[CC_USE_BILLBOARD] = false; - this._defines[CC_USE_STRETCHED_BILLBOARD] = false; - this._defines[CC_USE_HORIZONTAL_BILLBOARD] = true; - this._defines[CC_USE_VERTICAL_BILLBOARD] = false; - this._defines[CC_USE_MESH] = false; + this._defines[CC_RENDER_MODE] = RENDER_MODE_HORIZONTAL_BILLBOARD; } else if (this._renderMode === RenderMode.VerticalBillboard) { - this._defines[CC_USE_BILLBOARD] = false; - this._defines[CC_USE_STRETCHED_BILLBOARD] = false; - this._defines[CC_USE_HORIZONTAL_BILLBOARD] = false; - this._defines[CC_USE_VERTICAL_BILLBOARD] = true; - this._defines[CC_USE_MESH] = false; + this._defines[CC_RENDER_MODE] = RENDER_MODE_VERTICAL_BILLBOARD; } else if (this._renderMode === RenderMode.Mesh) { - this._defines[CC_USE_BILLBOARD] = false; - this._defines[CC_USE_STRETCHED_BILLBOARD] = false; - this._defines[CC_USE_HORIZONTAL_BILLBOARD] = false; - this._defines[CC_USE_VERTICAL_BILLBOARD] = false; - this._defines[CC_USE_MESH] = true; + this._defines[CC_RENDER_MODE] = RENDER_MODE_MESH; } else { console.warn(`particle system renderMode ${this._renderMode} not support.`); } diff --git a/cocos/ui/components/ui-model-component.ts b/cocos/ui/components/ui-model-component.ts index ffceb60ce9..6f374232c5 100644 --- a/cocos/ui/components/ui-model-component.ts +++ b/cocos/ui/components/ui-model-component.ts @@ -92,7 +92,7 @@ export class UIModelComponent extends UIComponent { } this._modelComponent._sceneGetter = null; - if (cc.isValid(this.node, true)) { + if (cc.isValid(this._modelComponent, true)) { this._modelComponent._attachToScene(); } this._models = null;