From 619c8def4c13cf72fc3d714ec248b86824edb875 Mon Sep 17 00:00:00 2001 From: zxg0622 <247274249@qq.com> Date: Fri, 24 May 2019 10:18:41 +0800 Subject: [PATCH] add line render (#4493) * add line render * fix canvas not destroyed when switching scene --- cocos/3d/framework/index.ts | 3 + cocos/3d/framework/particle/line-component.ts | 150 ++++++++++++ cocos/3d/ui/components/canvas-component.ts | 6 +- cocos/renderer/models/line-model.ts | 220 ++++++++++++++++++ 4 files changed, 376 insertions(+), 3 deletions(-) create mode 100644 cocos/3d/framework/particle/line-component.ts create mode 100644 cocos/renderer/models/line-model.ts diff --git a/cocos/3d/framework/index.ts b/cocos/3d/framework/index.ts index 13ed89ec26..994ca15770 100644 --- a/cocos/3d/framework/index.ts +++ b/cocos/3d/framework/index.ts @@ -32,6 +32,7 @@ import { EditorCameraComponent } from './editor-camera-component'; import { LightComponent } from './light-component'; import { ModelComponent } from './model-component'; import { BillboardComponent } from './particle/billboard-component'; +import { LineComponent } from './particle/line-component'; import { ParticleSystemComponent } from './particle/particle-system-component'; import { ParticleUtils } from './particle/particle-utils'; import { BoxColliderComponent, SphereColliderComponent } from './physics/collider-component'; @@ -52,6 +53,7 @@ export { BoxColliderComponent, ParticleSystemComponent, BillboardComponent, + LineComponent, RigidBodyComponent, SphereColliderComponent, @@ -75,6 +77,7 @@ cc.SpotLightComponent = SpotLightComponent; cc.BoxColliderComponent = BoxColliderComponent; cc.ParticleSystemComponent = ParticleSystemComponent; cc.BillboardComponent = BillboardComponent; +cc.LineComponent = LineComponent; cc.RigidBodyComponent = RigidBodyComponent; cc.SphereColliderComponent = SphereColliderComponent; diff --git a/cocos/3d/framework/particle/line-component.ts b/cocos/3d/framework/particle/line-component.ts new file mode 100644 index 0000000000..c521d4edd3 --- /dev/null +++ b/cocos/3d/framework/particle/line-component.ts @@ -0,0 +1,150 @@ +import { Texture2D } from '../../../assets'; +import { Component } from '../../../components'; +import { ccclass, executeInEditMode, menu, property } from '../../../core/data/class-decorator'; +import { Vec3 } from '../../../core/value-types'; +import { LineModel } from '../../../renderer/models/line-model'; +import { Material } from '../../assets'; +import { builtinResMgr } from '../../builtin'; +import { AnimationCurve } from '../../geom-utils'; +import CurveRange from './animator/curve-range'; +import GradientRange from './animator/gradient-range'; + +const CC_USE_WORLD_SPACE = 'CC_USE_WORLD_SPACE'; +const define = { CC_USE_WORLD_SPACE: false }; + +@ccclass('cc.LineComponent') +@menu('Components/LineComponent') +@executeInEditMode +export class LineComponent extends Component { + @property({ + type: Texture2D, + }) + private _texture = null; + + @property({ + type: Texture2D, + displayOrder: 0, + }) + get texture () { + return this._texture; + } + + set texture (val) { + this._texture = val; + if (this._material) { + this._material.setProperty('mainTexture', val); + } + } + + private _material: Material | null = null; + + @property + private _worldSpace = false; + + @property({ + displayOrder: 1, + }) + get worldSpace () { + return this._worldSpace; + } + + set worldSpace (val) { + this._worldSpace = val; + if (this._material) { + define[CC_USE_WORLD_SPACE] = this.worldSpace; + this._material.recompileShaders(define); + if (this._model) { + this._model.setSubModelMaterial(0, this._material!); + } + } + } + + @property({ + type: [Vec3], + }) + private _positions = []; + + @property({ + type: [Vec3], + displayOrder: 2, + }) + get positions () { + return this._positions; + } + + set positions (val) { + this._positions = val; + if (this._model) { + this._model.addLineVertexData(this._positions, this._width, this._color); + } + } + + @property({ + type: CurveRange, + }) + private _width = new CurveRange(); + + @property({ + type: CurveRange, + displayOrder: 3, + }) + get width () { + return this._width; + } + + set width (val) { + this._width = val; + if (this._model) { + this._model.addLineVertexData(this._positions, this._width, this._color); + } + } + + @property({ + type: GradientRange, + }) + private _color = new GradientRange(); + + @property({ + type: GradientRange, + displayOrder: 4, + }) + get color () { + return this._color; + } + + set color (val) { + this._color = val; + if (this._model) { + this._model.addLineVertexData(this._positions, this._width, this._color); + } + } + + private _model: LineModel | null = null; + + constructor () { + super(); + } + + 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!); + } + this._model!.enabled = true; + this.texture = this.texture; + this._model.addLineVertexData(this._positions, this._width, this._color); + } + + public onDisable () { + if (this._model) { + this._model.enabled = false; + } + } +} diff --git a/cocos/3d/ui/components/canvas-component.ts b/cocos/3d/ui/components/canvas-component.ts index 4dda53ac85..9d91b0823f 100644 --- a/cocos/3d/ui/components/canvas-component.ts +++ b/cocos/3d/ui/components/canvas-component.ts @@ -172,11 +172,11 @@ export class CanvasComponent extends Component { } public __preload () { - const cameraNode = new cc.Node('UICamera'); + const cameraNode = new cc.Node('UICamera' + this.node.name); cameraNode.setPosition(0, 0, 900); if (!CC_EDITOR) { this._camera = cc.director.root.ui.renderScene.createCamera({ - name: 'ui', + name: 'ui' + this.node.name, node: cameraNode, projection: cc.CameraComponent.ProjectionType.ORTHO, targetDisplay: 0, @@ -212,7 +212,7 @@ export class CanvasComponent extends Component { public onDestroy () { if (this._camera) { - this._getRenderScene().destroyCamera(this._camera); + cc.director.root.ui.renderScene.destroyCamera(this._camera); } if (CC_EDITOR) { diff --git a/cocos/renderer/models/line-model.ts b/cocos/renderer/models/line-model.ts new file mode 100644 index 0000000000..17e2f3f095 --- /dev/null +++ b/cocos/renderer/models/line-model.ts @@ -0,0 +1,220 @@ +import { IRenderingSubmesh } from '../../3d/assets/mesh'; +import CurveRange from '../../3d/framework/particle/animator/curve-range'; +import { Vec3 } from '../../core/value-types'; +import { vec3 } from '../../core/vmath'; +import { GFXAttributeName, GFXBufferUsageBit, GFXFormat, GFXFormatInfos, GFXMemoryUsageBit, GFXPrimitiveMode } from '../../gfx/define'; +import { Node } from '../../scene-graph'; +import { Model } from '../scene/model'; +import { RenderScene } from '../scene/render-scene'; +import GradientRange from '../../3d/framework/particle/animator/gradient-range'; +import { GFX_DRAW_INFO_SIZE } from '../../gfx/buffer'; + +const _vertex_attrs = [ + { name: GFXAttributeName.ATTR_POSITION, format: GFXFormat.RGB32F }, // xyz:position + { name: GFXAttributeName.ATTR_TEX_COORD, format: GFXFormat.RGBA32F }, // x:index y:size zw:texcoord + { name: GFXAttributeName.ATTR_TEX_COORD1, format: GFXFormat.RGB32F }, // xyz:velocity + { name: GFXAttributeName.ATTR_COLOR, format: GFXFormat.RGBA8, isNormalized: true }, +]; + +const _temp_v1 = cc.v3(); +const _temp_v2 = cc.v3(); + +export class LineModel extends Model { + + private _capacity: number; + private _vertSize: number; + private _vBuffer: ArrayBuffer | null; + private _vertAttrsFloatCount: number; + private _vdataF32: Float32Array | null; + private _vdataUint32: Uint32Array | null; + private _iaInfo: IGFXIndirectBuffer; + private _iaInfoBuffer: GFXBuffer; + private _subMeshData: IRenderingSubmesh | null; + private _vertCount: number = 0; + private _indexCount: number = 0; + + constructor (scene: RenderScene, node: Node) { + super(scene, node); + this._capacity = 100; + this._iaInfo = { + drawInfos: [{ + vertexCount: 0, + firstVertex: 0, + indexCount: 0, + firstIndex: 0, + vertexOffset: 0, + instanceCount: 0, + firstInstance: 0, + }], + }; + this._iaInfoBuffer = this._device.createBuffer({ + usage: GFXBufferUsageBit.INDIRECT, + memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE, + size: GFX_DRAW_INFO_SIZE, + stride: 1, + }); + } + + public setCapacity (capacity: number) { + this._capacity = capacity; + this.createBuffer(); + } + + public createBuffer () { + this._vertSize = 0; + for (const a of _vertex_attrs) { + (a as any).offset = this._vertSize; + this._vertSize += GFXFormatInfos[a.format].size; + } + this._vertAttrsFloatCount = this._vertSize / 4; // number of float + this._vBuffer = this._createSubMeshData(); + this._vdataF32 = new Float32Array(this._vBuffer); + this._vdataUint32 = new Uint32Array(this._vBuffer); + } + + public _createSubMeshData (): ArrayBuffer { + if (this._subMeshData) { + this.destroySubMeshData(); + } + this._vertCount = 2; + this._indexCount = 6; + const vertexBuffer = this._device.createBuffer({ + usage: GFXBufferUsageBit.VERTEX | GFXBufferUsageBit.TRANSFER_DST, + memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE, + size: this._vertSize * this._capacity * this._vertCount, + stride: this._vertSize, + }); + const vBuffer: ArrayBuffer = new ArrayBuffer(this._vertSize * this._capacity * this._vertCount); + vertexBuffer.update(vBuffer); + + const indices: Uint16Array = new Uint16Array((this._capacity - 1) * this._indexCount); + let dst = 0; + for (let i = 0; i < this._capacity - 1; ++i) { + const baseIdx = 2 * i; + indices[dst++] = baseIdx; + indices[dst++] = baseIdx + 1; + indices[dst++] = baseIdx + 2; + indices[dst++] = baseIdx + 3; + indices[dst++] = baseIdx + 2; + indices[dst++] = baseIdx + 1; + } + + const indexBuffer = this._device.createBuffer({ + usage: GFXBufferUsageBit.INDEX | GFXBufferUsageBit.TRANSFER_DST, + memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE, + size: (this._capacity - 1) * this._indexCount * Uint16Array.BYTES_PER_ELEMENT, + stride: Uint16Array.BYTES_PER_ELEMENT, + }); + + indexBuffer.update(indices); + + this._iaInfo.drawInfos[0].vertexCount = this._capacity * this._vertCount; + this._iaInfo.drawInfos[0].indexCount = (this._capacity - 1) * this._indexCount; + this._iaInfoBuffer.update(this._iaInfo); + + this._subMeshData = { + vertexBuffers: [vertexBuffer], + indexBuffer, + indirectBuffer: this._iaInfoBuffer, + attributes: _vertex_attrs, + primitiveMode: GFXPrimitiveMode.TRIANGLE_LIST, + }; + this.setSubModelMesh(0, this._subMeshData); + return vBuffer; + } + + public addLineVertexData (positions: Vec3[], width: CurveRange, color: GradientRange) { + if (positions.length > 1) { + let offset: number = 0; + vec3.sub(_temp_v1, positions[1], positions[0]); + this._vdataF32![offset++] = positions[0].x; + this._vdataF32![offset++] = positions[0].y; + this._vdataF32![offset++] = positions[0].z; + this._vdataF32![offset++] = 0; + this._vdataF32![offset++] = width.evaluate(0, 1)!; + this._vdataF32![offset++] = 0; + this._vdataF32![offset++] = 0; + this._vdataF32![offset++] = _temp_v1.x; + this._vdataF32![offset++] = _temp_v1.y; + this._vdataF32![offset++] = _temp_v1.z; + this._vdataUint32![offset++] = color.evaluate(0, 1)._val; + this._vdataF32![offset++] = positions[0].x; + this._vdataF32![offset++] = positions[0].y; + this._vdataF32![offset++] = positions[0].z; + this._vdataF32![offset++] = 1; + this._vdataF32![offset++] = width.evaluate(0, 1)!; + this._vdataF32![offset++] = 0; + this._vdataF32![offset++] = 1; + this._vdataF32![offset++] = _temp_v1.x; + this._vdataF32![offset++] = _temp_v1.y; + this._vdataF32![offset++] = _temp_v1.z; + this._vdataUint32![offset++] = color.evaluate(0, 1)._val; + for (let i = 1; i < positions.length - 1; i++) { + vec3.sub(_temp_v1, positions[i - 1], positions[i]); + vec3.sub(_temp_v2, positions[i + 1], positions[i]); + vec3.sub(_temp_v2, _temp_v2, _temp_v1); + const seg = i / positions.length; + this._vdataF32![offset++] = positions[i].x; + this._vdataF32![offset++] = positions[i].y; + this._vdataF32![offset++] = positions[i].z; + this._vdataF32![offset++] = 0; + this._vdataF32![offset++] = width.evaluate(seg, 1)!; + this._vdataF32![offset++] = seg; + this._vdataF32![offset++] = 0; + this._vdataF32![offset++] = _temp_v2.x; + this._vdataF32![offset++] = _temp_v2.y; + this._vdataF32![offset++] = _temp_v2.z; + this._vdataUint32![offset++] = color.evaluate(seg, 1)._val; + this._vdataF32![offset++] = positions[i].x; + this._vdataF32![offset++] = positions[i].y; + this._vdataF32![offset++] = positions[i].z; + this._vdataF32![offset++] = 1; + this._vdataF32![offset++] = width.evaluate(seg, 1)!; + this._vdataF32![offset++] = seg; + this._vdataF32![offset++] = 1; + this._vdataF32![offset++] = _temp_v2.x; + this._vdataF32![offset++] = _temp_v2.y; + this._vdataF32![offset++] = _temp_v2.z; + this._vdataUint32![offset++] = color.evaluate(seg, 1)._val; + } + vec3.sub(_temp_v1, positions[positions.length - 1], positions[positions.length - 2]); + this._vdataF32![offset++] = positions[positions.length - 1].x; + this._vdataF32![offset++] = positions[positions.length - 1].y; + this._vdataF32![offset++] = positions[positions.length - 1].z; + this._vdataF32![offset++] = 0; + this._vdataF32![offset++] = width.evaluate(1, 1)!; + this._vdataF32![offset++] = 1; + this._vdataF32![offset++] = 0; + this._vdataF32![offset++] = _temp_v1.x; + this._vdataF32![offset++] = _temp_v1.y; + this._vdataF32![offset++] = _temp_v1.z; + this._vdataUint32![offset++] = color.evaluate(1, 1)._val; + this._vdataF32![offset++] = positions[positions.length - 1].x; + this._vdataF32![offset++] = positions[positions.length - 1].y; + this._vdataF32![offset++] = positions[positions.length - 1].z; + this._vdataF32![offset++] = 1; + this._vdataF32![offset++] = width.evaluate(1, 1)!; + this._vdataF32![offset++] = 1; + this._vdataF32![offset++] = 1; + this._vdataF32![offset++] = _temp_v1.x; + this._vdataF32![offset++] = _temp_v1.y; + this._vdataF32![offset++] = _temp_v1.z; + this._vdataUint32![offset++] = color.evaluate(1, 1)._val; + } + this.updateIA(Math.max(0, positions.length - 1)); + } + + public updateIA (count: number) { + this.getSubModel(0).inputAssembler!.vertexBuffers[0].update(this._vdataF32!); + this.getSubModel(0).inputAssembler!.indexCount = this._indexCount * count; + this.getSubModel(0).inputAssembler!.extractDrawInfo(this._iaInfo.drawInfos[0]); + this._iaInfoBuffer.update(this._iaInfo); + } + + private destroySubMeshData () { + for (const vb of this._subMeshData!.vertexBuffers) { + vb.destroy(); + } + this._subMeshData!.indexBuffer!.destroy(); + } +}