add billboard component (#4439)

* add billboard component

* fix some particle bugs

* fix canvas deactive bug
This commit is contained in:
zxg0622
2019-05-17 15:58:34 +08:00
committed by Weiliang Wang
parent bb4435e4f5
commit cdd31ecaad
10 changed files with 233 additions and 27 deletions
+52 -12
View File
File diff suppressed because one or more lines are too long
+6
View File
@@ -180,6 +180,12 @@ class BuiltinResMgr {
defaultTrailMtl.initialize({ effectName: 'builtin-particle-trail' });
resources[defaultTrailMtl._uuid] = defaultTrailMtl;
// default particle material
const defaultBillboardMtl = new cc.Material();
defaultBillboardMtl._uuid = 'default-billboard-material';
defaultBillboardMtl.initialize({ effectName: 'builtin-billboard' });
resources[defaultBillboardMtl._uuid] = defaultBillboardMtl;
// customization for special effects
const tmp = aabb.create();
customizationManager.register('bounds-merge-shadow', {
+3
View File
@@ -31,6 +31,7 @@ import { DirectionalLightComponent } from './directional-light-component';
import { EditorCameraComponent } from './editor-camera-component';
import { LightComponent } from './light-component';
import { ModelComponent } from './model-component';
import { BillboardComponent } from './particle/billboard-component';
import { ParticleSystemComponent } from './particle/particle-system-component';
import { ParticleUtils } from './particle/particle-utils';
import { BoxColliderComponent, SphereColliderComponent } from './physics/collider-component';
@@ -50,6 +51,7 @@ export {
BoxColliderComponent,
ParticleSystemComponent,
BillboardComponent,
RigidBodyComponent,
SphereColliderComponent,
@@ -72,6 +74,7 @@ cc.SpotLightComponent = SpotLightComponent;
cc.BoxColliderComponent = BoxColliderComponent;
cc.ParticleSystemComponent = ParticleSystemComponent;
cc.BillboardComponent = BillboardComponent;
cc.RigidBodyComponent = RigidBodyComponent;
cc.SphereColliderComponent = SphereColliderComponent;
@@ -0,0 +1,144 @@
import { RenderableComponent } from "../renderable-component";
import { ccclass, property, executeInEditMode, menu } from "../../../core/data/class-decorator";
import { Material, Mesh } from "../../assets";
import { Texture2D } from "../../../assets";
import { createMesh } from "../../misc/utils";
import { GFXPrimitiveMode, GFXAttributeName, GFXFormat } from "../../../gfx";
import { Color } from "../../../core";
import { Model } from "../../../renderer";
import { builtinResMgr } from "../../builtin";
import { Component } from "../../../components";
import { toRadian, vec4, toDegree } from "../../../core/vmath";
@ccclass('cc.BillboardComponent')
@menu('Components/BillboardComponent')
@executeInEditMode
export class BillboardComponent extends Component {
@property({
type: Texture2D,
})
private _texture = null;
@property({
type: Texture2D,
})
get texture () {
return this._texture;
}
set texture (val) {
this._texture = val;
if (this._material) {
this._material.setProperty('mainTexture', val);
}
}
@property
private _height = 0;
@property
get height () {
return this._height;
}
set height (val) {
this._height = val;
if (this._material) {
this._uniform.y = val;
this._material.setProperty('cc_size_rotation', this._uniform);
}
}
@property
private _width = 0;
@property
public get width () {
return this._width;
}
public set width (val) {
this._width = val;
if (this._material) {
this._uniform.x = val;
this._material.setProperty('cc_size_rotation', this._uniform);
}
}
@property
private _rotation = 0;
@property
public get rotation () {
return Math.round(toDegree(this._rotation) * 100) / 100;
}
public set rotation (val) {
this._rotation = toRadian(val);
if (this._material) {
this._uniform.z = this._rotation;
this._material.setProperty('cc_size_rotation', this._uniform);
}
}
private _model: Model | null = null;
private _mesh: Mesh | null = null;
private _material: Material | null = null;
private _uniform = cc.v4(1, 1, 0, 0);
constructor () {
super();
}
public onEnable () {
if (!this._model) {
this.createModel();
}
this._model!.enabled = true;
this.width = this._width;
this.height = this._height;
this.rotation = this.rotation;
this.texture = this.texture;
}
public onDisable () {
if (this._model) {
this._model.enabled = false;
}
}
private createModel () {
this._mesh = createMesh({
primitiveMode: GFXPrimitiveMode.TRIANGLE_LIST,
positions: [0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0],
uvs: [0, 0,
1, 0,
0, 1,
1, 1],
colors: [
Color.WHITE.r, Color.WHITE.g, Color.WHITE.b, Color.WHITE.a,
Color.WHITE.r, Color.WHITE.g, Color.WHITE.b, Color.WHITE.a,
Color.WHITE.r, Color.WHITE.g, Color.WHITE.b, Color.WHITE.a,
Color.WHITE.r, Color.WHITE.g, Color.WHITE.b, Color.WHITE.a],
attributes: [
{ name: GFXAttributeName.ATTR_POSITION, format: GFXFormat.RGB32F },
{ name: GFXAttributeName.ATTR_TEX_COORD, format: GFXFormat.RG32F },
{ name: GFXAttributeName.ATTR_COLOR, format: GFXFormat.RGBA8UI, isNormalized: true },
],
indices: [0, 1, 2, 1, 2, 3],
}, undefined, { calculateBounds: false });
this._model = this._getRenderScene().createModel(Model, this.node);
if (this._material == null) {
this._material = new Material();
this._material.copy(builtinResMgr.get<Material>('default-billboard-material'));
}
this._model.initSubModel(0, this._mesh.getSubMesh(0), this._material!);
}
}
+5 -5
View File
@@ -1,7 +1,7 @@
import { repeat } from "../../../core/vmath";
import CurveRange from "./animator/curve-range";
import { CCClass } from "../../../core/data";
import { property, ccclass } from "../../../core/data/class-decorator";
import { CCClass } from '../../../core/data';
import { ccclass, property } from '../../../core/data/class-decorator';
import { repeat } from '../../../core/vmath';
import CurveRange from './animator/curve-range';
@ccclass('cc.Burst')
export default class Burst {
@@ -82,7 +82,7 @@ export default class Burst {
preFrameTime = (preFrameTime > 0.0) ? preFrameTime : 0.0;
const curFrameTime = repeat(psys.time - psys.startDelay.evaluate(), psys.duration);
if (this._curTime >= preFrameTime && this._curTime < curFrameTime) {
psys.emit(this.count.evaluate(this._curTime / psys.duration, 1));
psys.emit(this.count.evaluate(this._curTime / psys.duration, 1), dt - (curFrameTime - this._curTime));
this._curTime += this.repeatInterval;
--this._remainingCount;
}
@@ -506,10 +506,7 @@ export class ParticleSystemComponent extends RenderableComponent {
}
}
private emit (count, emitParams = null) {
if (emitParams !== null) {
// TODO:
}
private emit (count, dt) {
for (let i = 0; i < count; ++i) {
const particle = this.renderer!._getFreeParticle();
@@ -527,7 +524,6 @@ export class ParticleSystemComponent extends RenderableComponent {
}
vec3.scale(particle.velocity, particle.velocity, this.startSpeed.evaluate(this._time / this.duration, rand)!);
vec3.copy(particle.ultimateVelocity, particle.velocity);
switch (this._simulationSpace) {
case Space.Local:
@@ -543,6 +539,7 @@ export class ParticleSystemComponent extends RenderableComponent {
// TODO:
break;
}
vec3.copy(particle.ultimateVelocity, particle.velocity);
// apply startRotation. now 2D only.
vec3.set(particle.rotation, this.startRotation.evaluate(this._time / this.duration, rand)!, 0, 0);
@@ -555,7 +552,7 @@ export class ParticleSystemComponent extends RenderableComponent {
particle.color.set(particle.startColor);
// apply startLifetime.
particle.startLifetime = this.startLifetime.evaluate(this._time / this.duration, rand)!;
particle.startLifetime = this.startLifetime.evaluate(this._time / this.duration, rand)! + dt;
particle.remainingLifetime = particle.startLifetime;
particle.randomSeed = randomRangeInt(0, 233280);
@@ -598,7 +595,7 @@ export class ParticleSystemComponent extends RenderableComponent {
if (this._emitRateTimeCounter > 1 && this._isEmitting) {
const emitNum = Math.floor(this._emitRateTimeCounter);
this._emitRateTimeCounter -= emitNum;
this.emit(emitNum);
this.emit(emitNum, dt);
}
// emit by rateOverDistance
this.node.getWorldPosition(this._curWPos);
@@ -608,7 +605,7 @@ export class ParticleSystemComponent extends RenderableComponent {
if (this._emitRateDistanceCounter > 1 && this._isEmitting) {
const emitNum = Math.floor(this._emitRateDistanceCounter);
this._emitRateDistanceCounter -= emitNum;
this.emit(emitNum);
this.emit(emitNum, dt);
}
// bursts
@@ -115,6 +115,7 @@ export default class TrailModule {
this._createModel();
}
if (val && !this._enable) {
this._enable = val;
this._particleSystem.renderer._updateTrailMaterial();
}
this._enable = val;
+10
View File
@@ -125,6 +125,16 @@ export function createMesh (geometry: IGeometry, out?: Mesh, options?: ICreateMe
stride += info.size;
}
if (geometry.customAttributes) {
for (const ca of geometry.customAttributes) {
const info = GFXFormatInfos[ca.attr.format];
attributes.push(ca.attr);
vertCount = Math.max(vertCount, Math.floor(ca.values.length / info.count));
channels.push({ offset: stride, data: ca.values, attribute: ca.attr });
stride += info.size;
}
}
// Use this to generate final merged buffer.
const bufferBlob = new BufferBlob();
+5
View File
@@ -88,4 +88,9 @@ export interface IGeometry {
* specify vertex attributes, use (positions|normals|uvs|colors) as keys
*/
attributes?: IGFXAttribute[];
customAttributes?: Array<{
attr: IGFXAttribute,
values: number[],
}>;
}
+2 -2
View File
@@ -237,10 +237,10 @@ export class UI {
}
let camera: Camera | null;
for (let i = idx; i < this._screens.length;) {
for (let i = idx; i < this._screens.length; i++) {
camera = this._screens[i].camera;
if (camera) {
camera.view.visibility = ++i;
camera.view.visibility = i;
}
}
}