mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 00:22:43 +08:00
add particle model
This commit is contained in:
Vendored
+34
@@ -64,5 +64,39 @@ export default [
|
||||
"dependencies": {}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "builtin-effect-particle-add",
|
||||
"techniques": [{"queue": "transparent", "lod": 200, "passes": [{"rasterizerState": {"cullMode": 0}, "depthStencilState": {"depthTest": true, "depthWrite": false}, "blendState": {"targets": [{"blend": true, "blendSrc": 2, "blendDst": 1, "blendSrcAlpha": 2, "blendDstAlpha": 1}]}, "properties": {"mainTexture": {"type": 29, "value": "grey"}, "mainTiling": {"type": 14, "value": [1, 1]}, "mainOffset": {"type": 14, "value": [0, 0]}, "frameTile": {"type": 14, "value": [1, 1]}, "velocityScale": {"type": 13, "value": [0]}, "lengthScale": {"type": 13, "value": [0]}, "tintColor": {"type": 17, "value": [0.5, 0.5, 0.5, 0.5]}}, "program": "7b4748a3d0952030fe04084550aafa0c0d63899ce6b9537dfce75c0b"}]}],
|
||||
"shaders": [
|
||||
{
|
||||
"name": "7b4748a3d0952030fe04084550aafa0c0d63899ce6b9537dfce75c0b",
|
||||
"vert": "\n uniform vec2 frameTile;\n uniform vec2 mainTiling;\n uniform vec2 mainOffset;\n uniform float velocityScale;\n uniform float lengthScale;\n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\n uniform mat4 cc_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matViewProj;\n uniform mat4 cc_matViewProjInv;\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform vec4 cc_cameraSize;\n uniform vec4 cc_cameraPos;\n uniform vec4 cc_dirLightDirection;\n uniform vec4 cc_dirLightColor;\n uniform vec4 cc_pointLightPositionAndRange;\n uniform vec4 cc_pointLightColor;\n uniform vec4 cc_spotLightPositionAndRange;\n uniform vec4 cc_spotLightDirection;\n uniform vec4 cc_spotLightColor;\n uniform mat4 cc_matViewProjLight;\n uniform vec4 cc_shadowParam1;\n uniform vec4 cc_shadowParam2;\n uniform mat4 cc_matWorld;\n uniform mat4 cc_matWorldIT;\nvarying vec2 uv;\nvarying vec4 color;\nvoid computeVertPos(inout vec4 pos, vec2 vertOffset\n#if USE_BILLBOARD || USE_VERTICAL_BILLBOARD\n , mat4 view\n#endif\n#if USE_STRETCHED_BILLBOARD\n , vec3 eye\n , vec4 velocity\n , float velocityScale\n , float lengthScale\n , float size\n , float xIndex\n#endif\n) {\n#if USE_BILLBOARD\n vec3 camRight = normalize(vec3(view[0][0], view[1][0], view[2][0]));\n vec3 camUp = normalize(vec3(view[0][1], view[1][1], view[2][1]));\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#elif USE_STRETCHED_BILLBOARD\n vec3 camRight = normalize(cross(pos.xyz - eye, velocity.xyz));\n vec3 camUp = velocity.xyz * velocityScale + normalize(velocity.xyz) * lengthScale * size;\n pos.xyz += (camRight * abs(vertOffset.x) * sign(vertOffset.y)) - camUp * xIndex;\n#elif USE_HORIZONTAL_BILLBOARD\n vec3 camRight = vec3(1, 0, 0);\n vec3 camUp = vec3(0, 0, -1);\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#elif USE_VERTICAL_BILLBOARD\n vec3 camRight = normalize(vec3(view[0][0], view[1][0], view[2][0]));\n vec3 camUp = vec3(0, 1, 0);\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#else\n pos.x += vertOffset.x;\n pos.y += vertOffset.y;\n#endif\n}\nvec2 computeUV(float frameIndex, vec2 vertIndex, vec2 frameTile) {\n vec2 aniUV = vec2(0, floor(frameIndex * frameTile.y));\n aniUV.x = floor(frameIndex * frameTile.x * frameTile.y - aniUV.y * frameTile.x);\n \n vertIndex.x = 1. - vertIndex.x;\n return (aniUV.xy + vertIndex) / vec2(frameTile.x, frameTile.y);\n}\nvoid rotateCorner(inout vec2 corner, float angle) {\n float xOS = cos(angle) * corner.x - sin(angle) * corner.y;\n float yOS = sin(angle) * corner.x + cos(angle) * corner.y;\n corner.x = xOS;\n corner.y = yOS;\n}\nattribute vec3 a_position; \nattribute vec3 a_texCoord; \nattribute vec2 a_texCoord1; \nattribute vec4 a_color;\n#if USE_STRETCHED_BILLBOARD\nattribute vec3 a_color1; \n#endif\nvec4 lpvs_main() {\n vec4 pos = vec4(a_position, 1);\n#if USE_STRETCHED_BILLBOARD\n vec4 velocity = vec4(a_color1.xyz, 0);\n#endif\n#if !USE_WORLD_SPACE\n pos = cc_matWorld * pos;\n #if USE_STRETCHED_BILLBOARD\n velocity = cc_matWorld * velocity;\n #endif\n#endif\n vec2 cornerOffset = vec2((a_texCoord.xy - 0.5) * a_texCoord1.x);\n#if !USE_STRETCHED_BILLBOARD\n \n rotateCorner(cornerOffset, a_texCoord1.y);\n#endif\n computeVertPos(pos, cornerOffset\n #if USE_BILLBOARD || USE_VERTICAL_BILLBOARD\n , cc_matView\n #endif\n #if USE_STRETCHED_BILLBOARD\n , cc_cameraPos\n , velocity\n , velocityScale\n , lengthScale\n , a_texCoord1.x\n , a_texCoord.x\n #endif\n );\n pos = cc_matViewProj * pos;\n uv = computeUV(a_texCoord.z, a_texCoord.xy, frameTile) * mainTiling + mainOffset;\n color = a_color;\n return pos;\n}\nvoid main() { gl_Position = lpvs_main(); }\n",
|
||||
"frag": "\n uniform sampler2D mainTexture;\n uniform vec4 tintColor;\n varying vec2 uv;\n varying vec4 color;\n void main () {\n \n gl_FragColor = 2.0 * color * tintColor * texture2D(mainTexture, uv);\n }\n",
|
||||
"defines": [
|
||||
{"name": "USE_BILLBOARD", "type": "boolean", "defines": []},
|
||||
{"name": "USE_STRETCHED_BILLBOARD", "type": "boolean", "defines": []},
|
||||
{"name": "USE_HORIZONTAL_BILLBOARD", "type": "boolean", "defines": []},
|
||||
{"name": "USE_VERTICAL_BILLBOARD", "type": "boolean", "defines": []},
|
||||
{"name": "USE_WORLD_SPACE", "type": "boolean", "defines": []}
|
||||
],
|
||||
"blocks": [
|
||||
{"name": "Constants", "size": 32, "defines": [], "binding": 0, "members": [
|
||||
{"name": "frameTile", "type": 14, "count": 1, "size": 8},
|
||||
{"name": "mainTiling", "type": 14, "count": 1, "size": 8},
|
||||
{"name": "mainOffset", "type": 14, "count": 1, "size": 8},
|
||||
{"name": "velocityScale", "type": 13, "count": 1, "size": 4},
|
||||
{"name": "lengthScale", "type": 13, "count": 1, "size": 4}
|
||||
]},
|
||||
{"name": "FragConstants", "size": 16, "defines": [], "binding": 1, "members": [
|
||||
{"name": "tintColor", "type": 16, "count": 1, "size": 16}
|
||||
]}
|
||||
],
|
||||
"samplers": [
|
||||
{"name": "mainTexture", "type": 29, "count": 1, "defines": [], "binding": 2}
|
||||
],
|
||||
"dependencies": {}
|
||||
}
|
||||
]
|
||||
}
|
||||
];
|
||||
|
||||
+88
-61
@@ -5,14 +5,18 @@ import { Material } from '../../../assets/material';
|
||||
import { RecyclePool } from '../../../memop';
|
||||
import Particle from '../particle';
|
||||
import RenderableComponent from '../../renderable-component';
|
||||
import { Enum } from '../../../../core/value-types';
|
||||
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.create();
|
||||
let _tempAttribUV0 = vec2.create();
|
||||
let _tempAttribColor = vec4.create();
|
||||
let _tempWorldTrans = mat4.create();
|
||||
let _tempAttribUV: vec3 = vec3.create();
|
||||
let _tempAttribUV0: vec2 = vec2.create();
|
||||
let _tempAttribColor: vec4 = vec4.create();
|
||||
let _tempWorldTrans: mat4 = mat4.create();
|
||||
|
||||
let _uvs = [
|
||||
0, 0, // bottom-left
|
||||
@@ -29,6 +33,12 @@ const RenderMode = Enum({
|
||||
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';
|
||||
|
||||
@ccclass('cc.ParticleSystemRenderer')
|
||||
@executeInEditMode
|
||||
export default class ParticleSystemRenderer extends RenderableComponent {
|
||||
@@ -36,16 +46,16 @@ export default class ParticleSystemRenderer extends RenderableComponent {
|
||||
@property({
|
||||
type: RenderMode
|
||||
})
|
||||
_renderMode = RenderMode.Billboard;
|
||||
private _renderMode = RenderMode.Billboard;
|
||||
|
||||
@property({
|
||||
type: RenderMode
|
||||
})
|
||||
get renderMode() {
|
||||
public get renderMode() {
|
||||
return this._renderMode;
|
||||
}
|
||||
|
||||
set renderMode(val) {
|
||||
public set renderMode(val) {
|
||||
if (this._renderMode === val) {
|
||||
return;
|
||||
}
|
||||
@@ -55,74 +65,95 @@ export default class ParticleSystemRenderer extends RenderableComponent {
|
||||
}
|
||||
|
||||
@property
|
||||
_velocityScale = 1;
|
||||
private _velocityScale = 1;
|
||||
|
||||
@property
|
||||
get velocityScale() {
|
||||
public get velocityScale() {
|
||||
return this._velocityScale;
|
||||
}
|
||||
|
||||
set velocityScale(val) {
|
||||
public set velocityScale(val) {
|
||||
this._velocityScale = val;
|
||||
this._updateMaterialParams();
|
||||
this._updateModel();
|
||||
}
|
||||
|
||||
@property
|
||||
_lengthScale = 1;
|
||||
private _lengthScale = 1;
|
||||
|
||||
@property
|
||||
get lengthScale() {
|
||||
public get lengthScale() {
|
||||
return this._lengthScale;
|
||||
}
|
||||
|
||||
set lengthScale(val) {
|
||||
public set lengthScale(val) {
|
||||
this._lengthScale = val;
|
||||
this._updateMaterialParams();
|
||||
this._updateModel();
|
||||
}
|
||||
|
||||
private _defines: { [index: string]: boolean };
|
||||
private _model: ParticleBatchModel | null;
|
||||
private frameTile: Vec2;
|
||||
private attrs: [any];
|
||||
private _vertAttrs: IGFXInputAttribute[];
|
||||
private particleSystem: any;
|
||||
private _particles: RecyclePool;
|
||||
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this._model = null;
|
||||
|
||||
this.frameTile = cc.v2(1, 1);
|
||||
this.attrs = new Array(5);
|
||||
|
||||
this.device = cc.game._gfxDevice;
|
||||
this._vertAttrs = [
|
||||
{ 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 }
|
||||
];
|
||||
this._defines = {
|
||||
USE_WORLD_SPACE: true,
|
||||
USE_BILLBOARD: true,
|
||||
USE_STRETCHED_BILLBOARD: false,
|
||||
USE_HORIZONTAL_BILLBOARD: false,
|
||||
USE_VERTICAL_BILLBOARD: false
|
||||
}
|
||||
}
|
||||
|
||||
onInit() {
|
||||
this._vertAttrs = [
|
||||
{ name: gfx.ATTR_POSITION, type: gfx.ATTR_TYPE_FLOAT32, num: 3, index: gfx.ATTR_INDEX_POSITION },
|
||||
{ name: gfx.ATTR_UV, type: gfx.ATTR_TYPE_FLOAT32, num: 3, index: gfx.ATTR_INDEX_UV },
|
||||
{ name: gfx.ATTR_UV0, type: gfx.ATTR_TYPE_FLOAT32, num: 2, index: gfx.ATTR_INDEX_UV0 },
|
||||
{ name: gfx.ATTR_COLOR, type: gfx.ATTR_TYPE_UINT8, num: 4, index: gfx.ATTR_INDEX_COLOR, normalize: true }
|
||||
];
|
||||
this.particleSystem = this.node.getComponent('cc.ParticleSystemComponent');
|
||||
this._particles = new RecyclePool(() => {
|
||||
return new Particle(this);
|
||||
}, this.particleSystem.capacity);
|
||||
if (this.sharedMaterial == null) {
|
||||
this.material = new Material();
|
||||
this.sharedMaterial.effectAsset = cc.EffectAsset.get('builtin-effect-particle-add');
|
||||
this._materials[0] = new Material();
|
||||
this.sharedMaterial!.effectName = 'builtin-effect-particle-add';
|
||||
}
|
||||
this.onEnable();
|
||||
this._updateMaterialParams();
|
||||
this._updateModel();
|
||||
this.onEnable();
|
||||
}
|
||||
|
||||
onEnable() {
|
||||
if (this._model != null)
|
||||
this._scene.addModel(this._model);
|
||||
if (!this.particleSystem) {
|
||||
return;
|
||||
}
|
||||
if (this._model == null) {
|
||||
this._model = <ParticleBatchModel>this._getRenderScene().createModel(ParticleBatchModel);
|
||||
this._model.setCapacity(this.particleSystem.capacity);
|
||||
this._model.setVertexAttributes(this._vertAttrs);
|
||||
this._model.node = this.node;
|
||||
}
|
||||
}
|
||||
|
||||
onDisable() {
|
||||
this._scene.removeModel(this._model);
|
||||
this._getRenderScene().destroyModel(this._model!);
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
this._model.destroy();
|
||||
this._model!.destroy();
|
||||
}
|
||||
|
||||
_updateMaterialParams() {
|
||||
@@ -130,42 +161,43 @@ export default class ParticleSystemRenderer extends RenderableComponent {
|
||||
return;
|
||||
}
|
||||
if (this.particleSystem._simulationSpace === Space.World) {
|
||||
this.sharedMaterial.define('USE_WORLD_SPACE', true);
|
||||
this._defines[USE_WORLD_SPACE] = true;
|
||||
} else {
|
||||
this.sharedMaterial.define('USE_WORLD_SPACE', false);
|
||||
this._defines[USE_WORLD_SPACE] = true;
|
||||
}
|
||||
|
||||
if (this._renderMode === RenderMode.Billboard) {
|
||||
this.sharedMaterial.define('USE_BILLBOARD', true);
|
||||
this.sharedMaterial.define('USE_STRETCHED_BILLBOARD', false);
|
||||
this.sharedMaterial.define('USE_HORIZONTAL_BILLBOARD', false);
|
||||
this.sharedMaterial.define('USE_VERTICAL_BILLBOARD', false);
|
||||
this._defines[USE_BILLBOARD] = true;
|
||||
this._defines[USE_STRETCHED_BILLBOARD] = false;
|
||||
this._defines[USE_HORIZONTAL_BILLBOARD] = false;
|
||||
this._defines[USE_VERTICAL_BILLBOARD] = false;
|
||||
} else if (this._renderMode === RenderMode.StrecthedBillboard) {
|
||||
this.sharedMaterial.define('USE_BILLBOARD', false);
|
||||
this.sharedMaterial.define('USE_STRETCHED_BILLBOARD', true);
|
||||
this.sharedMaterial.define('USE_HORIZONTAL_BILLBOARD', false);
|
||||
this.sharedMaterial.define('USE_VERTICAL_BILLBOARD', false);
|
||||
this.sharedMaterial.setProperty('velocityScale', this._velocityScale);
|
||||
this.sharedMaterial.setProperty('lengthScale', this._lengthScale);
|
||||
this._defines[USE_BILLBOARD] = false;
|
||||
this._defines[USE_STRETCHED_BILLBOARD] = true;
|
||||
this._defines[USE_HORIZONTAL_BILLBOARD] = false;
|
||||
this._defines[USE_VERTICAL_BILLBOARD] = false;
|
||||
this.sharedMaterial!.setProperty('velocityScale', this._velocityScale);
|
||||
this.sharedMaterial!.setProperty('lengthScale', this._lengthScale);
|
||||
} else if (this._renderMode === RenderMode.HorizontalBillboard) {
|
||||
this.sharedMaterial.define('USE_BILLBOARD', false);
|
||||
this.sharedMaterial.define('USE_STRETCHED_BILLBOARD', false);
|
||||
this.sharedMaterial.define('USE_HORIZONTAL_BILLBOARD', true);
|
||||
this.sharedMaterial.define('USE_VERTICAL_BILLBOARD', false);
|
||||
this._defines[USE_BILLBOARD] = false;
|
||||
this._defines[USE_STRETCHED_BILLBOARD] = false;
|
||||
this._defines[USE_HORIZONTAL_BILLBOARD] = true;
|
||||
this._defines[USE_VERTICAL_BILLBOARD] = false;
|
||||
} else if (this._renderMode === RenderMode.VerticalBillboard) {
|
||||
this.sharedMaterial.define('USE_BILLBOARD', false);
|
||||
this.sharedMaterial.define('USE_STRETCHED_BILLBOARD', false);
|
||||
this.sharedMaterial.define('USE_HORIZONTAL_BILLBOARD', false);
|
||||
this.sharedMaterial.define('USE_VERTICAL_BILLBOARD', true);
|
||||
this._defines[USE_BILLBOARD] = false;
|
||||
this._defines[USE_STRETCHED_BILLBOARD] = false;
|
||||
this._defines[USE_HORIZONTAL_BILLBOARD] = false;
|
||||
this._defines[USE_VERTICAL_BILLBOARD] = true;
|
||||
} else {
|
||||
console.warn(`particle system renderMode ${this._renderMode} not support.`);
|
||||
}
|
||||
this.sharedMaterial!.setDefines(this._defines);
|
||||
|
||||
if (this.particleSystem.textureAnimationModule.enable) {
|
||||
this.sharedMaterial.setProperty('frameTile', vec2.set(this.frameTile, this.particleSystem.textureAnimationModule.numTilesX, this.particleSystem.textureAnimationModule.numTilesY));
|
||||
this.sharedMaterial!.setProperty('frameTile', vec2.set(this.frameTile, this.particleSystem.textureAnimationModule.numTilesX, this.particleSystem.textureAnimationModule.numTilesY));
|
||||
}
|
||||
else {
|
||||
this.sharedMaterial.setProperty('frameTile', this.frameTile);
|
||||
this.sharedMaterial!.setProperty('frameTile', this.frameTile);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -173,18 +205,13 @@ export default class ParticleSystemRenderer extends RenderableComponent {
|
||||
if (!this.particleSystem) {
|
||||
return;
|
||||
}
|
||||
if (this._model === null) {
|
||||
this._model = new renderer.ParticleBatchModel(this.device, this.particleSystem.capacity, this._vertAttrs);
|
||||
this._model.setParticleRenderer(this);
|
||||
this._model.setNode(this.node);
|
||||
}
|
||||
this._model.setEffect(this.sharedMaterial ? this.sharedMaterial.effect : null);
|
||||
this._model!.material = this.sharedMaterial ? this.sharedMaterial : null;
|
||||
if (Object.getPrototypeOf(this).constructor.name === 'ParticleSystemGpuRenderer')
|
||||
return;
|
||||
if (this._renderMode === RenderMode.StrecthedBillboard) {
|
||||
this._model.enableStretchedBillboard();
|
||||
this._model!.enableStretchedBillboard();
|
||||
} else {
|
||||
this._model.disableStretchedBillboard();
|
||||
this._model!.disableStretchedBillboard();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -275,12 +302,12 @@ export default class ParticleSystemRenderer extends RenderableComponent {
|
||||
this.attrs[attrNum++] = p.ultimateVelocity;
|
||||
}
|
||||
|
||||
this._model.addParticleVertexData(idx++, this.attrs);
|
||||
this._model!.addParticleVertexData(idx++, this.attrs);
|
||||
}
|
||||
}
|
||||
|
||||
// because we use index buffer, per particle index count = 6.
|
||||
this._model.updateIA(this._particles.length * 6);
|
||||
this._model!.updateIA(this._particles.length * 6);
|
||||
}
|
||||
|
||||
updateShaderUniform() {
|
||||
Vendored
+1
-1
@@ -39,7 +39,7 @@ export class Component extends CCObject {
|
||||
|
||||
public constructor();
|
||||
|
||||
protected _getRenderScene(index?: number): RenderScene;
|
||||
protected _getRenderScene(): RenderScene;
|
||||
public addComponent(typeOrClassName: Function | string): Component;
|
||||
public getComponent(typeOrClassName: Function | string): Component;
|
||||
public getComponents(typeOrClassName: Function | string): Component[];
|
||||
|
||||
@@ -18,6 +18,8 @@ export interface IGFXDrawInfo {
|
||||
firstInstance: number;
|
||||
}
|
||||
|
||||
export const GFX_DRAW_INFO_SIZE: number = 56;
|
||||
|
||||
export interface IGFXIndirectBuffer {
|
||||
drawInfos: IGFXDrawInfo[];
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ export class TestModelStage extends RenderStage {
|
||||
private _init: boolean = false;
|
||||
private _textureAsset: Texture2D | null = null;
|
||||
|
||||
constructor (flow: RenderFlow) {
|
||||
constructor(flow: RenderFlow) {
|
||||
super(flow);
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ export class TestModelStage extends RenderStage {
|
||||
// load texture
|
||||
const imgElms = this._device.canvas.getElementsByTagName('img');
|
||||
if (imgElms.length) {
|
||||
if (!this.loadTexture( imgElms[0] as HTMLImageElement)) {
|
||||
if (!this.loadTexture(imgElms[0] as HTMLImageElement)) {
|
||||
console.error('Load texture failed.');
|
||||
return false;
|
||||
}
|
||||
@@ -85,7 +85,7 @@ export class TestModelStage extends RenderStage {
|
||||
this._scene._scene = this._scene;
|
||||
const modelCom = this._scene.addComponent('cc.ModelComponent');
|
||||
modelCom.material = this._material;
|
||||
modelCom.mesh = cc.utils.createMesh(cc.game._gfxDevice, cc.primitives.box({length: 1, width: 1, height: 1}));
|
||||
modelCom.mesh = cc.utils.createMesh(cc.game._gfxDevice, cc.primitives.box({ length: 1, width: 1, height: 1 }));
|
||||
this._model = modelCom._models[0];
|
||||
this._scene.setRotationFromEuler(45, 45, 45);
|
||||
this._scene._rot = this._scene._lrot;
|
||||
@@ -94,16 +94,16 @@ export class TestModelStage extends RenderStage {
|
||||
|
||||
const camera = view.camera as Camera;
|
||||
|
||||
const cmdBuff = this._cmdBuff as GFXCommandBuffer;
|
||||
const cmdBuff = this._cmdBuff as GFXCommandBuffer;
|
||||
this._renderArea.width = camera.width;
|
||||
this._renderArea.height = camera.height;
|
||||
this._model._updateTransform();
|
||||
this._model.updateRenderData();
|
||||
this._model.updateUBOs();
|
||||
cmdBuff.begin();
|
||||
cmdBuff.beginRenderPass( this._framebuffer as GFXFramebuffer, this._renderArea, this._clearColors,
|
||||
cmdBuff.beginRenderPass(this._framebuffer as GFXFramebuffer, this._renderArea, this._clearColors,
|
||||
this._clearDepth, this._clearStencil);
|
||||
|
||||
cmdBuff.execute(this._model.commandBuffers);
|
||||
cmdBuff.execute(this._model.commandBuffers, this._model.commandBuffers.length);
|
||||
cmdBuff.endRenderPass();
|
||||
cmdBuff.end();
|
||||
|
||||
|
||||
@@ -1,119 +0,0 @@
|
||||
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
import gfx from '../gfx';
|
||||
import InputAssembler from '../core/input-assembler';
|
||||
|
||||
import { Model } from '../scene/model';
|
||||
|
||||
function _createInputAssembler(out) {
|
||||
let vb = new gfx.VertexBuffer(
|
||||
out._device,
|
||||
new gfx.VertexFormat(out._vertAttrs),
|
||||
gfx.USAGE_DYNAMIC,
|
||||
null,
|
||||
out._capacity * 4
|
||||
);
|
||||
|
||||
let indices = new Array(out._capacity);
|
||||
let dst = 0;
|
||||
for (let i = 0; i < out._capacity; ++i) {
|
||||
let baseIdx = 4 * i;
|
||||
indices[dst++] = baseIdx;
|
||||
indices[dst++] = baseIdx + 1;
|
||||
indices[dst++] = baseIdx + 2;
|
||||
indices[dst++] = baseIdx + 3;
|
||||
indices[dst++] = baseIdx + 2;
|
||||
indices[dst++] = baseIdx + 1;
|
||||
}
|
||||
let ib = new gfx.IndexBuffer(
|
||||
out._device,
|
||||
gfx.INDEX_FMT_UINT16,
|
||||
gfx.USAGE_STATIC,
|
||||
new Uint16Array(indices),
|
||||
indices.length
|
||||
);
|
||||
|
||||
out._ia = new InputAssembler(vb, ib);
|
||||
}
|
||||
|
||||
export default class ParticleBatchModel extends Model {
|
||||
constructor(device, capacity, vertAttrs) {
|
||||
super();
|
||||
|
||||
this._type = 'particle-batch';
|
||||
this._device = device;
|
||||
this._capacity = capacity;
|
||||
this.setVertexAttributes(vertAttrs);
|
||||
}
|
||||
|
||||
setVertexAttributes(attrs) {
|
||||
this._vertAttrs = attrs;
|
||||
// rebuid
|
||||
_createInputAssembler(this);
|
||||
this._vertAttrsSize = this._ia._vertexBuffer._format._bytes / 4; // number of float
|
||||
this._vBuffer = new ArrayBuffer(this._capacity * this._ia._vertexBuffer._format._bytes);
|
||||
this._vdataF32 = new Float32Array(this._vBuffer);
|
||||
this._vdataUint32 = new Uint32Array(this._vBuffer);
|
||||
}
|
||||
|
||||
enableStretchedBillboard() {
|
||||
if (this._vertAttrs.find(attr => attr.name === gfx.ATTR_COLOR0) === undefined) {
|
||||
this._vertAttrs.push({ name: gfx.ATTR_COLOR0, type: gfx.ATTR_TYPE_FLOAT32, num: 3 });
|
||||
this.setVertexAttributes(this._vertAttrs);
|
||||
}
|
||||
}
|
||||
|
||||
disableStretchedBillboard() {
|
||||
if (this._vertAttrs.find(attr => attr.name === gfx.ATTR_COLOR0) !== undefined) {
|
||||
this._vertAttrs.pop();
|
||||
this.setVertexAttributes(this._vertAttrs);
|
||||
}
|
||||
}
|
||||
|
||||
setParticleRenderer(r) {
|
||||
this._renderer = r;
|
||||
}
|
||||
|
||||
addParticleVertexData(index, pvdata) {
|
||||
// if (pvdata.length !== this._vertAttrs.length) {
|
||||
// console.error('particle vertex stream data not match.');
|
||||
// }
|
||||
let offset = index * this._vertAttrsSize;
|
||||
for (let i = 0; i < this._vertAttrs.length; ++i) {
|
||||
let curAttr = this._vertAttrs[i];
|
||||
if (curAttr.num === 1) {
|
||||
this._vdataF32[offset] = pvdata[i]; // if not a single float?
|
||||
offset++;
|
||||
} else if (curAttr.num === 2) {
|
||||
this._vdataF32[offset] = pvdata[i].x;
|
||||
this._vdataF32[offset + 1] = pvdata[i].y;
|
||||
offset += 2;
|
||||
} else if (curAttr.num === 3) {
|
||||
this._vdataF32[offset] = pvdata[i].x;
|
||||
this._vdataF32[offset + 1] = pvdata[i].y;
|
||||
this._vdataF32[offset + 2] = pvdata[i].z;
|
||||
offset += 3;
|
||||
} else if (curAttr.index === gfx.ATTR_INDEX_COLOR) {
|
||||
this._vdataUint32[offset] = pvdata[i];
|
||||
offset += 1;
|
||||
} else {
|
||||
console.error('particle vertex attribute format not support');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
updateIA(count) {
|
||||
this._ia._count = count;
|
||||
this._ia._vertexBuffer.update(0, this._vdataF32);
|
||||
}
|
||||
|
||||
clear() {
|
||||
this._ia._count = 0;
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this._vdataF32 = null;
|
||||
this._ia._vertexBuffer.destroy();
|
||||
this._ia._indexBuffer.destroy();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
import gfx from '../gfx';
|
||||
|
||||
import { Model } from '../scene/model';
|
||||
import { GFXBufferUsageBit, GFXMemoryUsageBit, GFXFormat, GFXAttributeName, GFXPrimitiveMode, GFXFormatInfos } from '../../gfx/define';
|
||||
import { IGFXInputAttribute } from '../../gfx/input-assembler';
|
||||
import { GFXBuffer, GFX_DRAW_INFO_SIZE, IGFXIndirectBuffer } from '../../gfx/buffer';
|
||||
|
||||
export default class ParticleBatchModel extends Model {
|
||||
|
||||
private _capacity: number;
|
||||
private _vertAttrs: IGFXInputAttribute[] | null;
|
||||
private _vertSize: number;
|
||||
private _vBuffer: ArrayBuffer | null;
|
||||
private _vertAttrsFloatCount: number;
|
||||
private _vdataF32: Float32Array | null;
|
||||
private _vdataUint32: Uint32Array | null;
|
||||
private _iaInfo: IGFXIndirectBuffer;
|
||||
private _iaInfoBuffer: GFXBuffer;
|
||||
|
||||
constructor () {
|
||||
super();
|
||||
|
||||
this._type = 'particle-batch';
|
||||
this._capacity = 0;
|
||||
this._vertAttrs = null;
|
||||
this._vertSize = 0;
|
||||
this._vBuffer = null;
|
||||
this._vertAttrsFloatCount = 0;
|
||||
this._vdataF32 = null;
|
||||
this._vdataUint32 = null;
|
||||
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;
|
||||
}
|
||||
|
||||
public setVertexAttributes (attrs: IGFXInputAttribute[]) {
|
||||
this._vertAttrs = attrs;
|
||||
this._vertSize = 0;
|
||||
for (const a of this._vertAttrs) {
|
||||
this._vertSize += GFXFormatInfos[a.format].size;
|
||||
}
|
||||
// rebuid
|
||||
this._vBuffer = this._createInputAssembler();
|
||||
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 {
|
||||
const vertexBuffer = this._device.createBuffer({
|
||||
usage: GFXBufferUsageBit.VERTEX,
|
||||
memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE,
|
||||
size: this._vertSize * this._capacity * 4,
|
||||
stride: this._vertSize,
|
||||
});
|
||||
const vBuffer: ArrayBuffer = new ArrayBuffer(this._vertSize * this._capacity * 4);
|
||||
vertexBuffer.update(vBuffer);
|
||||
|
||||
const indices: Uint16Array = new Uint16Array(this._capacity * 6);
|
||||
let dst = 0;
|
||||
for (let i = 0; i < this._capacity; ++i) {
|
||||
const baseIdx = 4 * 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,
|
||||
memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE,
|
||||
size: this._capacity * 6 * Uint16Array.BYTES_PER_ELEMENT,
|
||||
stride: Uint16Array.BYTES_PER_ELEMENT,
|
||||
});
|
||||
|
||||
indexBuffer.update(indices);
|
||||
|
||||
this._iaInfo.drawInfos[0].vertexCount = this._capacity * 4;
|
||||
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,
|
||||
}),
|
||||
primitiveMode: GFXPrimitiveMode.TRIANGLE_LIST,
|
||||
};
|
||||
return vBuffer;
|
||||
}
|
||||
|
||||
public enableStretchedBillboard () {
|
||||
if (this._vertAttrs!.find((attr) => attr.name === GFXAttributeName.ATTR_COLOR1) === undefined) {
|
||||
this._vertAttrs!.push({ name: GFXAttributeName.ATTR_COLOR1, format: GFXFormat.RGB32F });
|
||||
this.setVertexAttributes(this._vertAttrs!);
|
||||
}
|
||||
}
|
||||
|
||||
public disableStretchedBillboard () {
|
||||
if (this._vertAttrs!.find((attr) => attr.name === gfx.ATTR_COLOR0) !== undefined) {
|
||||
this._vertAttrs!.pop();
|
||||
this.setVertexAttributes(this._vertAttrs!);
|
||||
}
|
||||
}
|
||||
|
||||
public addParticleVertexData (index: number, pvdata: [any]) {
|
||||
// if (pvdata.length !== this._vertAttrs.length) {
|
||||
// console.error('particle vertex stream data not match.');
|
||||
// }
|
||||
let offset: number = index * this._vertAttrsFloatCount;
|
||||
for (let i = 0; i < this._vertAttrs!.length; ++i) {
|
||||
const curAttr: IGFXInputAttribute = this._vertAttrs![i];
|
||||
if (curAttr.format === GFXFormat.R32F) {
|
||||
this._vdataF32![offset] = pvdata[i]; // if not a single float?
|
||||
offset++;
|
||||
} else if (curAttr.format === GFXFormat.RG32F) {
|
||||
this._vdataF32![offset] = pvdata[i].x;
|
||||
this._vdataF32![offset + 1] = pvdata[i].y;
|
||||
offset += 2;
|
||||
} else if (curAttr.format === GFXFormat.RGB32F) {
|
||||
this._vdataF32![offset] = pvdata[i].x;
|
||||
this._vdataF32![offset + 1] = pvdata[i].y;
|
||||
this._vdataF32![offset + 2] = pvdata[i].z;
|
||||
offset += 3;
|
||||
} else if (curAttr.format === GFXFormat.RGBA8) {
|
||||
this._vdataUint32![offset] = pvdata[i];
|
||||
offset += 1;
|
||||
} else {
|
||||
console.error('particle vertex attribute format not support');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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._iaInfoBuffer.update(this._iaInfo);
|
||||
}
|
||||
|
||||
public clear () {
|
||||
this._subMeshObject!.inputAssembler.indexCount = 0;
|
||||
}
|
||||
|
||||
public destroy () {
|
||||
this._vdataF32 = null;
|
||||
this._subMeshObject!.inputAssembler.destroy();
|
||||
this._iaInfoBuffer.destroy();
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import { Node } from '../../scene-graph';
|
||||
import { Effect } from '../core/effect';
|
||||
import { Pass } from '../core/pass';
|
||||
import { RenderScene } from './render-scene';
|
||||
import { GFXDevice } from '../../gfx/device';
|
||||
|
||||
const _temp_floatx16 = new Float32Array(16);
|
||||
const _temp_mat4 = mat4.create();
|
||||
@@ -21,13 +22,14 @@ const _temp_mat4 = mat4.create();
|
||||
*/
|
||||
export class Model {
|
||||
|
||||
protected _device: GFXDevice;
|
||||
protected _type: string;
|
||||
protected _subMeshObject: IRenderingSubmesh | null;
|
||||
private _scene: RenderScene | null;
|
||||
private _id: number;
|
||||
private _type: string;
|
||||
private _poolID: number;
|
||||
private _isEnable: boolean;
|
||||
private _node: Node;
|
||||
private _subMeshObject: IRenderingSubmesh | null;
|
||||
private _effect: Effect | null;
|
||||
private _defines: Object;
|
||||
private _dependencies: Object;
|
||||
@@ -64,10 +66,11 @@ export class Model {
|
||||
this._cmdBuffers = new Array<GFXCommandBuffer>();
|
||||
this._uboLocal = new UBOLocal();
|
||||
this._localUBO = null;
|
||||
this._device = cc.director.root.device;
|
||||
}
|
||||
|
||||
public initialize () {
|
||||
this._localUBO = cc.director.root.device.createBuffer({
|
||||
this._localUBO = this._device.createBuffer({
|
||||
usage: GFXBufferUsageBit.UNIFORM | GFXBufferUsageBit.TRANSFER_DST,
|
||||
memUsage: GFXMemoryUsageBit.HOST,
|
||||
size: UBOLocal.SIZE,
|
||||
@@ -104,17 +107,11 @@ export class Model {
|
||||
public updateUBOs () {
|
||||
mat4.array(_temp_floatx16, this._node._mat);
|
||||
this._node._mat.invert(_temp_mat4);
|
||||
if (this._uboLocal) {
|
||||
this._uboLocal.view.set(_temp_floatx16, UBOLocal.MAT_WORLD_OFFSET);
|
||||
}
|
||||
this._uboLocal.view.set(_temp_floatx16, UBOLocal.MAT_WORLD_OFFSET);
|
||||
mat4.array(_temp_floatx16, _temp_mat4);
|
||||
if (this._uboLocal) {
|
||||
this._uboLocal.view.set(_temp_floatx16, UBOLocal.MAT_WORLD_IT_OFFSET);
|
||||
}
|
||||
this._uboLocal.view.set(_temp_floatx16, UBOLocal.MAT_WORLD_IT_OFFSET);
|
||||
|
||||
if (this._localUBO) {
|
||||
this._localUBO.update(this._uboLocal.view);
|
||||
}
|
||||
this._localUBO!.update(this._uboLocal.view);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -194,7 +191,7 @@ export class Model {
|
||||
set material (material: Material) {
|
||||
this._material = material;
|
||||
for (let i = 0; i < this._material.passes.length; i++) {
|
||||
if (this._material.passes[i].primitive !== (this._subMeshObject as IRenderingSubmesh).primitiveMode) {
|
||||
if (this._material.passes[i].primitive !== this._subMeshObject!.primitiveMode) {
|
||||
cc.error('the model(%d)\'s primitive type doesn\'t match its pass\'s');
|
||||
}
|
||||
this.recordCommandBuffer(i);
|
||||
@@ -207,14 +204,14 @@ export class Model {
|
||||
}
|
||||
}
|
||||
|
||||
public recordCommandBuffer (index: number) {
|
||||
const pass = (this._material as Material).passes[index];
|
||||
private recordCommandBuffer (index: number) {
|
||||
const pass = this._material!.passes[index];
|
||||
if (this._cmdBuffers[index] == null) {
|
||||
const cmdBufferInfo = {
|
||||
allocator: cc.director.root.device.commandAllocator,
|
||||
allocator: this._device.commandAllocator,
|
||||
type: GFXCommandBufferType.SECONDARY,
|
||||
};
|
||||
this._cmdBuffers[index] = cc.director.root.device.createCommandBuffer(cmdBufferInfo);
|
||||
this._cmdBuffers[index] = this._device.createCommandBuffer(cmdBufferInfo);
|
||||
}
|
||||
|
||||
const localUBO = this._localUBO as GFXBuffer;
|
||||
@@ -240,7 +237,7 @@ export class Model {
|
||||
}
|
||||
|
||||
get passes (): Pass[] {
|
||||
return (this._material as Material).passes;
|
||||
return this._material!.passes;
|
||||
}
|
||||
|
||||
get commandBuffers (): GFXCommandBuffer[] {
|
||||
@@ -252,6 +249,6 @@ export class Model {
|
||||
}
|
||||
|
||||
get localUBO (): GFXBuffer {
|
||||
return this._localUBO as GFXBuffer;
|
||||
return this._localUBO!;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user