three-proton-loader v1.1.0
ProtonLoader
ProtonLoader is a class to make three.proton easier to use. It provides a very easy API to create your particle system by instantiating a single class, with object based parameters
Demo
Usage
Note that you need to pass the Proton object to the constructor of ProtonLoader yourself.
const protonLoader = new ProtonLoader(Proton);
protonLoader.createParticles({
container: scene, // Your three.js scene
renderType: 'MeshRender',
body: {
type: 'sphere',
radius: 3,
segments: 8,
},
particleProps: {
rate: [4, 16],
position: [0, 0],
mass: 1,
radius: [6, 12],
life: 3,
alpha: [1, 0],
scale: [0.1, 1.3],
colors: [0xFF0000, 0x00FF00],
velocity: {
speed: 45,
direction: [0, 1, 0],
variance: 180,
},
},
});This will create a new Proton instance, using the given parameters to setup the particle emitter.
Update Function
In order for your particles to be updated every frame, you need to call the update function.
var protonLoader = new ProtonLoader(Proton);
// create particles or something
function update() {
protonLoader.update();
window.requestAnimationFrame(update);
}Destroy function
When you are done using a particle system, you should destroy it
var protonLoader = new ProtonLoader(Proton);
var myParticles = protonLoader.createParticles(myOptions);
setTimeout(function () {
myParticles.destroy();
}, 5000); // Particles will be destroyed after 5 secondsOptions
Container
The container property has to be a three.js object. Either your scene, or another object in your scene. The particles will be created in this object.
renderType
The renderType option has to be specified, and defines the type of mesh that will be used for the particle. The most common options are:
MeshRenderAny kind of meshSpriteRenderA 2D sprite
The ProtonLoader just gives this renderType to Proton, which uses it internally. See Proton's doc/code for more info.
body
The body option defines what your particle is. You have multiple type of bodies available:
sphereWill create athree.jsSphereGeometry. It uses the following properties:radiussegments(number of segments in the geometry, default is 8
boxWill create athree.jsBoxGeometry. It uses the following properties:size
meshWill use a provided mesh object as particle. This allows you to create particles based on any kind of object you want. It uses:- 'mesh': any kind of
three.jsObject3Dthat Proton will use as a source for the particles
- 'mesh': any kind of
- 'sprite' Will create a 2D sprite. It uses:
srcCan be either athree.jsTexture, or astringcontaining the url of the image. Ifsrcis an url, aTHREE.TextureLoaderwill be created to load the image. Example:
body: {
type: 'mesh',
mesh: myThreeJSMesh
};
body: {
type: 'box',
size: 2
};particleProps
The particleProps property contains all the properties that can be applied to the particle emitter.
In three.proton, you usually setup your particle properties by calling functions for each of them. For example:
emitter.addInitialize(new Proton.Radius(6, 12));
emitter.addBehaviour(new Proton.Alpha(1, 0));With ProtonLoader, those properties are just passed as parameters in the particleProps object, and ProtonLoader will call those functions for you:
const myParticles = new ProtonLoader({
container: scene,
renderType: 'MeshRender',
body: {
type: 'sprite',
src: 'myImage.png'
},
particleProps: {
radius: [6, 12], // The ProtonLoader will launch emitter.addInitialize(new Proton.Radius(6, 12));
alpha: [1, 0]
}
});