Search in sources :

Example 1 with ParticleEmitter

use of com.jme3.effect.ParticleEmitter in project jmonkeyengine by jMonkeyEngine.

the class ParticleEmitter method read.

@Override
public void read(JmeImporter im) throws IOException {
    super.read(im);
    InputCapsule ic = im.getCapsule(this);
    shape = (EmitterShape) ic.readSavable("shape", DEFAULT_SHAPE);
    if (shape == DEFAULT_SHAPE) {
        // Prevent reference to static
        shape = shape.deepClone();
    }
    meshType = ic.readEnum("meshType", ParticleMesh.Type.class, ParticleMesh.Type.Triangle);
    int numParticles = ic.readInt("numParticles", 0);
    enabled = ic.readBoolean("enabled", true);
    particlesPerSec = ic.readFloat("particlesPerSec", 0);
    lowLife = ic.readFloat("lowLife", 0);
    highLife = ic.readFloat("highLife", 0);
    gravity = (Vector3f) ic.readSavable("gravity", null);
    imagesX = ic.readInt("imagesX", 1);
    imagesY = ic.readInt("imagesY", 1);
    startColor = (ColorRGBA) ic.readSavable("startColor", null);
    endColor = (ColorRGBA) ic.readSavable("endColor", null);
    startSize = ic.readFloat("startSize", 0);
    endSize = ic.readFloat("endSize", 0);
    worldSpace = ic.readBoolean("worldSpace", false);
    this.setIgnoreTransform(worldSpace);
    facingVelocity = ic.readBoolean("facingVelocity", false);
    faceNormal = (Vector3f) ic.readSavable("faceNormal", new Vector3f(Vector3f.NAN));
    selectRandomImage = ic.readBoolean("selectRandomImage", false);
    randomAngle = ic.readBoolean("randomAngle", false);
    rotateSpeed = ic.readFloat("rotateSpeed", 0);
    switch(meshType) {
        case Point:
            particleMesh = new ParticlePointMesh();
            this.setMesh(particleMesh);
            break;
        case Triangle:
            particleMesh = new ParticleTriMesh();
            this.setMesh(particleMesh);
            break;
        default:
            throw new IllegalStateException("Unrecognized particle type: " + meshType);
    }
    this.setNumParticles(numParticles);
    //        particleMesh.initParticleData(this, particles.length);
    //        particleMesh.setImagesXY(imagesX, imagesY);
    particleInfluencer = (ParticleInfluencer) ic.readSavable("influencer", DEFAULT_INFLUENCER);
    if (particleInfluencer == DEFAULT_INFLUENCER) {
        particleInfluencer = particleInfluencer.clone();
    }
    if (im.getFormatVersion() == 0) {
        // find it in the controls and take it out, then add the proper one in
        for (int i = 0; i < controls.size(); i++) {
            Object obj = controls.get(i);
            if (obj instanceof ParticleEmitter) {
                controls.remove(i);
                // now add the proper one in
                controls.add(new ParticleEmitterControl(this));
                break;
            }
        }
        // compatability before gravity was not a vector but a float
        if (gravity == null) {
            gravity = new Vector3f();
            gravity.y = ic.readFloat("gravity", 0);
        }
    } else {
        // since the parentEmitter is not loaded, it must be
        // loaded separately
        control = getControl(ParticleEmitterControl.class);
        control.parentEmitter = this;
    }
}
Also used : Type(com.jme3.effect.ParticleMesh.Type) InputCapsule(com.jme3.export.InputCapsule) Vector3f(com.jme3.math.Vector3f)

Example 2 with ParticleEmitter

use of com.jme3.effect.ParticleEmitter in project jmonkeyengine by jMonkeyEngine.

the class ParticlePointMesh method initParticleData.

@Override
public void initParticleData(ParticleEmitter emitter, int numParticles) {
    setMode(Mode.Points);
    this.emitter = emitter;
    // set positions
    FloatBuffer pb = BufferUtils.createVector3Buffer(numParticles);
    //if the buffer is already set only update the data
    VertexBuffer buf = getBuffer(VertexBuffer.Type.Position);
    if (buf != null) {
        buf.updateData(pb);
    } else {
        VertexBuffer pvb = new VertexBuffer(VertexBuffer.Type.Position);
        pvb.setupData(Usage.Stream, 3, Format.Float, pb);
        setBuffer(pvb);
    }
    // set colors
    ByteBuffer cb = BufferUtils.createByteBuffer(numParticles * 4);
    buf = getBuffer(VertexBuffer.Type.Color);
    if (buf != null) {
        buf.updateData(cb);
    } else {
        VertexBuffer cvb = new VertexBuffer(VertexBuffer.Type.Color);
        cvb.setupData(Usage.Stream, 4, Format.UnsignedByte, cb);
        cvb.setNormalized(true);
        setBuffer(cvb);
    }
    // set sizes
    FloatBuffer sb = BufferUtils.createFloatBuffer(numParticles);
    buf = getBuffer(VertexBuffer.Type.Size);
    if (buf != null) {
        buf.updateData(sb);
    } else {
        VertexBuffer svb = new VertexBuffer(VertexBuffer.Type.Size);
        svb.setupData(Usage.Stream, 1, Format.Float, sb);
        setBuffer(svb);
    }
    // set UV-scale
    FloatBuffer tb = BufferUtils.createFloatBuffer(numParticles * 4);
    buf = getBuffer(VertexBuffer.Type.TexCoord);
    if (buf != null) {
        buf.updateData(tb);
    } else {
        VertexBuffer tvb = new VertexBuffer(VertexBuffer.Type.TexCoord);
        tvb.setupData(Usage.Stream, 4, Format.Float, tb);
        setBuffer(tvb);
    }
    updateCounts();
}
Also used : VertexBuffer(com.jme3.scene.VertexBuffer) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 3 with ParticleEmitter

use of com.jme3.effect.ParticleEmitter in project jmonkeyengine by jMonkeyEngine.

the class ParticleTriMesh method initParticleData.

//    private Particle[] particlesCopy;
@Override
public void initParticleData(ParticleEmitter emitter, int numParticles) {
    setMode(Mode.Triangles);
    this.emitter = emitter;
    //        particlesCopy = new Particle[numParticles];
    // set positions
    FloatBuffer pb = BufferUtils.createVector3Buffer(numParticles * 4);
    // if the buffer is already set only update the data
    VertexBuffer buf = getBuffer(VertexBuffer.Type.Position);
    if (buf != null) {
        buf.updateData(pb);
    } else {
        VertexBuffer pvb = new VertexBuffer(VertexBuffer.Type.Position);
        pvb.setupData(Usage.Stream, 3, Format.Float, pb);
        setBuffer(pvb);
    }
    // set colors
    ByteBuffer cb = BufferUtils.createByteBuffer(numParticles * 4 * 4);
    buf = getBuffer(VertexBuffer.Type.Color);
    if (buf != null) {
        buf.updateData(cb);
    } else {
        VertexBuffer cvb = new VertexBuffer(VertexBuffer.Type.Color);
        cvb.setupData(Usage.Stream, 4, Format.UnsignedByte, cb);
        cvb.setNormalized(true);
        setBuffer(cvb);
    }
    // set texcoords
    FloatBuffer tb = BufferUtils.createVector2Buffer(numParticles * 4);
    uniqueTexCoords = false;
    for (int i = 0; i < numParticles; i++) {
        tb.put(0f).put(1f);
        tb.put(1f).put(1f);
        tb.put(0f).put(0f);
        tb.put(1f).put(0f);
    }
    tb.flip();
    buf = getBuffer(VertexBuffer.Type.TexCoord);
    if (buf != null) {
        buf.updateData(tb);
    } else {
        VertexBuffer tvb = new VertexBuffer(VertexBuffer.Type.TexCoord);
        tvb.setupData(Usage.Static, 2, Format.Float, tb);
        setBuffer(tvb);
    }
    // set indices
    ShortBuffer ib = BufferUtils.createShortBuffer(numParticles * 6);
    for (int i = 0; i < numParticles; i++) {
        int startIdx = (i * 4);
        // triangle 1
        ib.put((short) (startIdx + 1)).put((short) (startIdx + 0)).put((short) (startIdx + 2));
        // triangle 2
        ib.put((short) (startIdx + 1)).put((short) (startIdx + 2)).put((short) (startIdx + 3));
    }
    ib.flip();
    buf = getBuffer(VertexBuffer.Type.Index);
    if (buf != null) {
        buf.updateData(ib);
    } else {
        VertexBuffer ivb = new VertexBuffer(VertexBuffer.Type.Index);
        ivb.setupData(Usage.Static, 3, Format.UnsignedShort, ib);
        setBuffer(ivb);
    }
    updateCounts();
}
Also used : VertexBuffer(com.jme3.scene.VertexBuffer) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer) ShortBuffer(java.nio.ShortBuffer)

Example 4 with ParticleEmitter

use of com.jme3.effect.ParticleEmitter in project jmonkeyengine by jMonkeyEngine.

the class HelloEffects method simpleInitApp.

@Override
public void simpleInitApp() {
    ParticleEmitter fire = new ParticleEmitter("Emitter", ParticleMesh.Type.Triangle, 30);
    Material mat_red = new Material(assetManager, "Common/MatDefs/Misc/Particle.j3md");
    mat_red.setTexture("Texture", assetManager.loadTexture("Effects/Explosion/flame.png"));
    fire.setMaterial(mat_red);
    fire.setImagesX(2);
    // 2x2 texture animation
    fire.setImagesY(2);
    // red
    fire.setEndColor(new ColorRGBA(1f, 0f, 0f, 1f));
    // yellow
    fire.setStartColor(new ColorRGBA(1f, 1f, 0f, 0.5f));
    fire.getParticleInfluencer().setInitialVelocity(new Vector3f(0, 2, 0));
    fire.setStartSize(1.5f);
    fire.setEndSize(0.1f);
    fire.setGravity(0, 0, 0);
    fire.setLowLife(1f);
    fire.setHighLife(3f);
    fire.getParticleInfluencer().setVelocityVariation(0.3f);
    rootNode.attachChild(fire);
    ParticleEmitter debris = new ParticleEmitter("Debris", ParticleMesh.Type.Triangle, 10);
    Material debris_mat = new Material(assetManager, "Common/MatDefs/Misc/Particle.j3md");
    debris_mat.setTexture("Texture", assetManager.loadTexture("Effects/Explosion/Debris.png"));
    debris.setMaterial(debris_mat);
    debris.setImagesX(3);
    // 3x3 texture animation
    debris.setImagesY(3);
    debris.setSelectRandomImage(true);
    debris.setRotateSpeed(4);
    debris.getParticleInfluencer().setInitialVelocity(new Vector3f(0, 4, 0));
    debris.setStartColor(ColorRGBA.White);
    debris.setGravity(0, 6, 0);
    debris.getParticleInfluencer().setVelocityVariation(.60f);
    rootNode.attachChild(debris);
    debris.emitAllParticles();
//    ParticleEmitter water = 
//            new ParticleEmitter("Emitter", ParticleMesh.Type.Triangle, 20);
//    Material mat_blue = new Material(assetManager, 
//            "Common/MatDefs/Misc/Particle.j3md");
//    mat_blue.setTexture("Texture", assetManager.loadTexture(
//            "Effects/Explosion/flame.png"));
//    water.setMaterial(mat_blue);
//    water.setImagesX(2); 
//    water.setImagesY(2); // 2x2 texture animation
//    water.setStartColor( ColorRGBA.Blue); 
//    water.setEndColor( ColorRGBA.Cyan); 
//    water.getParticleInfluencer().setInitialVelocity(new Vector3f(0, -4, 0));
//    water.setStartSize(1f);
//    water.setEndSize(1.5f);
//    water.setGravity(0,1,0);
//    water.setLowLife(1f);
//    water.setHighLife(1f);
//    water.getParticleInfluencer().setVelocityVariation(0.1f);
//    water.setLocalTranslation(0, 6, 0);
//    rootNode.attachChild(water);
}
Also used : ParticleEmitter(com.jme3.effect.ParticleEmitter) ColorRGBA(com.jme3.math.ColorRGBA) Vector3f(com.jme3.math.Vector3f) Material(com.jme3.material.Material)

Example 5 with ParticleEmitter

use of com.jme3.effect.ParticleEmitter in project jmonkeyengine by jMonkeyEngine.

the class TestExplosionEffect method createSmokeTrail.

private void createSmokeTrail() {
    smoketrail = new ParticleEmitter("SmokeTrail", Type.Triangle, 22 * COUNT_FACTOR);
    smoketrail.setStartColor(new ColorRGBA(1f, 0.8f, 0.36f, (float) (1.0f / COUNT_FACTOR_F)));
    smoketrail.setEndColor(new ColorRGBA(1f, 0.8f, 0.36f, 0f));
    smoketrail.setStartSize(.2f);
    smoketrail.setEndSize(1f);
    //        smoketrail.setShape(new EmitterSphereShape(Vector3f.ZERO, 1f));
    smoketrail.setFacingVelocity(true);
    smoketrail.setParticlesPerSec(0);
    smoketrail.setGravity(0, 1, 0);
    smoketrail.setLowLife(.4f);
    smoketrail.setHighLife(.5f);
    smoketrail.setInitialVelocity(new Vector3f(0, 12, 0));
    smoketrail.setVelocityVariation(1);
    smoketrail.setImagesX(1);
    smoketrail.setImagesY(3);
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Particle.j3md");
    mat.setTexture("Texture", assetManager.loadTexture("Effects/Explosion/smoketrail.png"));
    smoketrail.setMaterial(mat);
    explosionEffect.attachChild(smoketrail);
}
Also used : ParticleEmitter(com.jme3.effect.ParticleEmitter) ColorRGBA(com.jme3.math.ColorRGBA) Vector3f(com.jme3.math.Vector3f) Material(com.jme3.material.Material)

Aggregations

ParticleEmitter (com.jme3.effect.ParticleEmitter)20 Material (com.jme3.material.Material)17 Vector3f (com.jme3.math.Vector3f)15 ColorRGBA (com.jme3.math.ColorRGBA)13 EmitterSphereShape (com.jme3.effect.shapes.EmitterSphereShape)7 Spatial (com.jme3.scene.Spatial)4 ActionListener (com.jme3.input.controls.ActionListener)3 KeyTrigger (com.jme3.input.controls.KeyTrigger)3 EmitterMeshVertexShape (com.jme3.effect.shapes.EmitterMeshVertexShape)2 InputCapsule (com.jme3.export.InputCapsule)2 Geometry (com.jme3.scene.Geometry)2 Node (com.jme3.scene.Node)2 VertexBuffer (com.jme3.scene.VertexBuffer)2 ByteBuffer (java.nio.ByteBuffer)2 FloatBuffer (java.nio.FloatBuffer)2 Type (com.jme3.effect.ParticleMesh.Type)1 EmptyParticleInfluencer (com.jme3.effect.influencers.EmptyParticleInfluencer)1 NewtonianParticleInfluencer (com.jme3.effect.influencers.NewtonianParticleInfluencer)1 ParticleInfluencer (com.jme3.effect.influencers.ParticleInfluencer)1 EmitterBoxShape (com.jme3.effect.shapes.EmitterBoxShape)1