Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Particle helper #4288

Merged
merged 17 commits into from May 12, 2018
11,386 changes: 6,269 additions & 5,117 deletions Playground/babylon.d.txt

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions Tools/Gulp/config.json
Expand Up @@ -111,6 +111,7 @@
"instrumentation",
"backgroundMaterial",
"environmentHelper",
"particleHelper",
"videoDome"
],
"minimal": [
Expand Down Expand Up @@ -1170,6 +1171,16 @@
"additionalTextures"
]
},
"particleHelper": {
"files": [
"../../src/Helpers/babylon.particleHelper.js"
],
"dependUpon": [
"core",
"backgroundMaterial",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should just rely on particles actually

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah that's what I said I was not sure at all what to do here so just :

"dependUpon": [
    "particles"
]

is good?

"additionalTextures"
]
},
"videoDome": {
"files": [
"../../src/Helpers/babylon.videoDome.js"
Expand Down
3 changes: 3 additions & 0 deletions assets/particles/fire.json
@@ -0,0 +1,3 @@
{
"type": "fire"
}
Binary file added assets/particles/textures/Flare2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/particles/textures/fire.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/particles/textures/fire.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/particles/textures/flare.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/particles/textures/flare3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion dist/preview release/what's new.md
Expand Up @@ -11,7 +11,6 @@
- Add webVR support for Oculus Go ([TrevorDev](https://github.com/TrevorDev))
- Add ability to not generate polynomials harmonics upon prefiltered texture creation ([sebavan](http://www.github.com/sebavan))
- Add predicate function to customize the list of mesh included in the computation of bounding vectors in the ```getHierarchyBoundingVectors``` method ([sebavan](http://www.github.com/sebavan))
- Add webVR constructor options: disable laser pointer toggle, teleportation floor meshes ([TrevorDev](https://github.com/TrevorDev))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a line in the major update to present your great work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I will do that :)

### glTF Loader

Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -43,4 +43,4 @@
"base64-font-loader": "0.0.4",
"typescript": "^2.8.1"
}
}
}
164 changes: 164 additions & 0 deletions src/Helpers/babylon.particleHelper.ts
@@ -0,0 +1,164 @@
module BABYLON {
/**
* ParticleSystemType
*/
export enum ParticleSystemType {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed anymore

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mhhh I was thinking this was a good idea to let this with the parse method but yeah if a user made a typo is not our fault ^^

/**
* None is to represents an error in parsing the type string in the create method.
*/
None,
/**
* Fire particle system.
*/
Fire,
/**
* Smoke particle system.
*/
Smoke
}
/**
* This class is made for on one-liner static method to help creating particle systems.
*/
export class ParticleHelper {
/**
* Base Assets URL.
*/
private static _baseAssetsUrl = "https://assets.babylonjs.com/particles/";

private static _scene: Scene;

private static _emitter: AbstractMesh;

/**
* This is the main static method (one-liner) of this helper to create different particle systems.
* @param type This string will be parsed to a ParticleSystemType
* @param emitter The object where the particle system will start to emit from.
* @param scene The scene where the particle system should live.
* @param gpu If the system will use gpu.
* @returns the ParticleSystem created.
*/
public static Create(type: string, emitter: AbstractMesh, scene: Nullable<Scene> = Engine.LastCreatedScene, gpu: boolean = false): ParticleSystem {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather prefer having a json file loaded where you can find fire or smoke parameters

Does it make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I understand that, but how can I properly load a JSON file (I already created it in assets/particles)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create will be async and you can just call Tools.LoadFile to get the content of the json file (from
baseAssetsUrl + "/systems.json" for instance)
You can rely on promise to get a CreateAsync function for isntance

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh okay, i haven't see that, thanks you :)

Copy link
Contributor Author

@yovanoc yovanoc May 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But, can I know why you prefer async method + JSON file over a simple synchronous method?
And I want to do some unit tests but my particle helper does not appear under BABYLON module? Do I have to add this to a config file?

const typeParsed = this._parseType(type);
if (typeParsed === ParticleSystemType.None) {
throw new Error("This particle system type doesn't exist.");
}

if (scene) {
this._scene = scene;
} else {
throw new Error("A particle system need a scene.");
}

this._emitter = emitter;

switch (typeParsed) {
case ParticleSystemType.Fire:
return this._createFire();
case ParticleSystemType.Smoke:
return this._createSmoke();
default:
throw new Error("Not yet implemented.");
}
}

private static _parseType(type: string): ParticleSystemType {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed

switch (type) {
case "fire":
case "Fire":
case "FIRE":
return ParticleSystemType.Fire;
case "smoke":
case "Smoke":
case "SMOKE":
return ParticleSystemType.Smoke;
default:
return ParticleSystemType.None;
}
}

private static _createFire(): ParticleSystem {
// Create a particle system
const fireSystem = new ParticleSystem("particles", 2000, this._scene);
// Texture of each particle
fireSystem.particleTexture = new Texture(`${this._baseAssetsUrl}textures/flare.png`, this._scene);
// Where the particles come from
fireSystem.emitter = this._emitter; // the starting object, the emitter
fireSystem.minEmitBox = new Vector3(-0.5, 1, -0.5); // Starting all from
fireSystem.maxEmitBox = new Vector3(0.5, 1, 0.5); // To...

// Colors of all particles
fireSystem.color1 = new Color4(1, 0.5, 0, 1.0);
fireSystem.color2 = new Color4(1, 0.5, 0, 1.0);
fireSystem.colorDead = new Color4(0, 0, 0, 0.0);

// Size of each particle (random between...
fireSystem.minSize = 0.3;
fireSystem.maxSize = 1;

// Life time of each particle (random between...
fireSystem.minLifeTime = 0.2;
fireSystem.maxLifeTime = 0.4;

// Emission rate
fireSystem.emitRate = 600;

// Blend mode : BLENDMODE_ONEONE, or BLENDMODE_STANDARD
fireSystem.blendMode = ParticleSystem.BLENDMODE_ONEONE;

// Set the gravity of all particles
fireSystem.gravity = new Vector3(0, 0, 0);

// Direction of each particle after it has been emitted
fireSystem.direction1 = new Vector3(0, 4, 0);
fireSystem.direction2 = new Vector3(0, 4, 0);

// Angular speed, in radians
fireSystem.minAngularSpeed = 0;
fireSystem.maxAngularSpeed = Math.PI;

// Speed
fireSystem.minEmitPower = 1;
fireSystem.maxEmitPower = 3;
fireSystem.updateSpeed = 0.007;

return fireSystem;
}

private static _createSmoke(): ParticleSystem {
const smokeSystem = new ParticleSystem("smoke", 1000, this._scene);
smokeSystem.particleTexture = new Texture(`${this._baseAssetsUrl}textures/flare.png`, this._scene);
smokeSystem.emitter = this._emitter;
smokeSystem.minEmitBox = new Vector3(-0.5, 1, -0.5);
smokeSystem.maxEmitBox = new Vector3(0.5, 1, 0.5);

smokeSystem.color1 = new Color4(0.1, 0.1, 0.1, 1.0);
smokeSystem.color2 = new Color4(0.1, 0.1, 0.1, 1.0);
smokeSystem.colorDead = new Color4(0, 0, 0, 0.0);

smokeSystem.minSize = 0.3;
smokeSystem.maxSize = 1;

smokeSystem.minLifeTime = 0.3;
smokeSystem.maxLifeTime = 1.5;

smokeSystem.emitRate = 350;

smokeSystem.blendMode = ParticleSystem.BLENDMODE_ONEONE;

smokeSystem.gravity = new Vector3(0, 0, 0);

smokeSystem.direction1 = new Vector3(-1.5, 8, -1.5);
smokeSystem.direction2 = new Vector3(1.5, 8, 1.5);

smokeSystem.minAngularSpeed = 0;
smokeSystem.maxAngularSpeed = Math.PI;

smokeSystem.minEmitPower = 0.5;
smokeSystem.maxEmitPower = 1.5;
smokeSystem.updateSpeed = 0.005;

return smokeSystem;
}
}

}
2 changes: 1 addition & 1 deletion tests/unit/package.json
Expand Up @@ -4,7 +4,7 @@
"description": "Unit Tests Suite For Babylon.js",
"main": "",
"repository": {
"url": "https://github.com/BabylonJS/Babylon.js/"
"url": "https://github.com/BabylonJS/Babylon.js/"
},
"readme": "https://github.com/BabylonJS/Babylon.js/edit/master/readme.md",
"license": "(Apache-2.0)",
Expand Down