Search in sources :

Example 31 with Animation

use of com.jme3.animation.Animation in project jmonkeyengine by jMonkeyEngine.

the class SpatialTrack method setTime.

/**
     * 
     * Modify the spatial which this track modifies.
     * 
     * @param time
     *            the current time of the animation
     */
public void setTime(float time, float weight, AnimControl control, AnimChannel channel, TempVars vars) {
    Spatial spatial = control.getSpatial();
    Vector3f tempV = vars.vect1;
    Vector3f tempS = vars.vect2;
    Quaternion tempQ = vars.quat1;
    Vector3f tempV2 = vars.vect3;
    Vector3f tempS2 = vars.vect4;
    Quaternion tempQ2 = vars.quat2;
    int lastFrame = times.length - 1;
    if (time < 0 || lastFrame == 0) {
        if (rotations != null)
            rotations.get(0, tempQ);
        if (translations != null)
            translations.get(0, tempV);
        if (scales != null) {
            scales.get(0, tempS);
        }
    } else if (time >= times[lastFrame]) {
        if (rotations != null)
            rotations.get(lastFrame, tempQ);
        if (translations != null)
            translations.get(lastFrame, tempV);
        if (scales != null) {
            scales.get(lastFrame, tempS);
        }
    } else {
        int startFrame = 0;
        int endFrame = 1;
        // use lastFrame so we never overflow the array
        for (int i = 0; i < lastFrame && times[i] < time; ++i) {
            startFrame = i;
            endFrame = i + 1;
        }
        float blend = (time - times[startFrame]) / (times[endFrame] - times[startFrame]);
        if (rotations != null)
            rotations.get(startFrame, tempQ);
        if (translations != null)
            translations.get(startFrame, tempV);
        if (scales != null) {
            scales.get(startFrame, tempS);
        }
        if (rotations != null)
            rotations.get(endFrame, tempQ2);
        if (translations != null)
            translations.get(endFrame, tempV2);
        if (scales != null) {
            scales.get(endFrame, tempS2);
        }
        tempQ.nlerp(tempQ2, blend);
        tempV.interpolateLocal(tempV2, blend);
        tempS.interpolateLocal(tempS2, blend);
    }
    if (translations != null)
        spatial.setLocalTranslation(tempV);
    if (rotations != null)
        spatial.setLocalRotation(tempQ);
    if (scales != null) {
        spatial.setLocalScale(tempS);
    }
}
Also used : Spatial(com.jme3.scene.Spatial) Quaternion(com.jme3.math.Quaternion) Vector3f(com.jme3.math.Vector3f)

Example 32 with Animation

use of com.jme3.animation.Animation in project jmonkeyengine by jMonkeyEngine.

the class CursorLoader method loadCursor.

private JmeCursor loadCursor(InputStream inStream) throws IOException {
    // new byte [0] facilitates read()
    byte[] icoimages = new byte[0];
    if (isAni) {
        CursorLoader.CursorImageData ciDat = new CursorLoader.CursorImageData();
        int numIcons = 0;
        int jiffy = 0;
        // not using those but keeping references for now.
        int steps = 0;
        int width = 0;
        int height = 0;
        // we don't use that.
        int flag = 0;
        int[] rate = null;
        int[] animSeq = null;
        ArrayList<byte[]> icons;
        DataInput leIn = new LittleEndien(inStream);
        int riff = leIn.readInt();
        if (riff == 0x46464952) {
            // RIFF
            // read next int (file length), discarding it, we don't need that.
            leIn.readInt();
            int nextInt = 0;
            nextInt = getNext(leIn);
            if (nextInt == 0x4e4f4341) {
                // We have ACON, we do nothing
                //                    System.out.println("We have ACON. Next!");
                nextInt = getNext(leIn);
                while (nextInt >= 0) {
                    if (nextInt == 0x68696e61) {
                        //                            System.out.println("we have 'anih' header");
                        // internal struct length (always 36)
                        leIn.skipBytes(8);
                        numIcons = leIn.readInt();
                        // number of blits for ani cycles
                        steps = leIn.readInt();
                        width = leIn.readInt();
                        height = leIn.readInt();
                        leIn.skipBytes(8);
                        jiffy = leIn.readInt();
                        flag = leIn.readInt();
                        nextInt = leIn.readInt();
                    } else if (nextInt == 0x65746172) {
                        // found a 'rate' of animation
                        //                            System.out.println("we have 'rate'.");
                        // Fill rate here.
                        // Rate is synchronous with frames.
                        int length = leIn.readInt();
                        rate = new int[length / 4];
                        for (int i = 0; i < length / 4; i++) {
                            rate[i] = leIn.readInt();
                        }
                        nextInt = leIn.readInt();
                    } else if (nextInt == 0x20716573) {
                        // found a 'seq ' of animation
                        //                            System.out.println("we have 'seq '.");
                        // Fill animation sequence here
                        int length = leIn.readInt();
                        animSeq = new int[length / 4];
                        for (int i = 0; i < length / 4; i++) {
                            animSeq[i] = leIn.readInt();
                        }
                        nextInt = leIn.readInt();
                    } else if (nextInt == 0x5453494c) {
                        // Found a LIST
                        //                            System.out.println("we have 'LIST'.");
                        int length = leIn.readInt();
                        nextInt = leIn.readInt();
                        if (nextInt == 0x4f464e49) {
                            // Got an INFO, skip its length
                            // this part consist  of Author, title, etc
                            leIn.skipBytes(length - 4);
                            //                                System.out.println(" Discarding INFO (skipped = " + skipped + ")");
                            nextInt = leIn.readInt();
                        } else if (nextInt == 0x6d617266) {
                            //                                System.out.println("we have 'fram'.");
                            if (leIn.readInt() == 0x6e6f6369) {
                                // we have 'icon'
                                // We have an icon and from this point on
                                // the rest is only icons.
                                int icoLength = leIn.readInt();
                                ciDat.numImages = numIcons;
                                icons = new ArrayList<byte[]>(numIcons);
                                for (int i = 0; i < numIcons; i++) {
                                    if (i > 0) {
                                        // skip 'icon' header and length as they are
                                        // known already and won't change.
                                        leIn.skipBytes(8);
                                    }
                                    byte[] data = new byte[icoLength];
                                    ((InputStream) leIn).read(data, 0, icoLength);
                                    // get it from first image.
                                    if (width == 0 || height == 0 && i == 1) {
                                        width = data[6];
                                        height = data[7];
                                    }
                                    icons.add(data);
                                }
                                // at this point we have the icons, rates (either
                                // through jiffy or rate array, the sequence (if
                                // applicable) and the ani header info.
                                // Put things together.
                                ciDat.assembleCursor(icons, rate, animSeq, jiffy, steps, width, height);
                                ciDat.completeCursor();
                                nextInt = leIn.readInt();
                                // if for some reason there's JUNK (nextInt > -1)
                                // bail out.
                                nextInt = nextInt > -1 ? -1 : nextInt;
                            }
                        }
                    }
                }
            }
            return setJmeCursor(ciDat);
        } else if (riff == 0x58464952) {
            throw new IllegalArgumentException("Big-Endian RIFX is not supported. Sorry.");
        } else {
            throw new IllegalArgumentException("Unknown format.");
        }
    } else if (isCur || isIco) {
        DataInputStream in = new DataInputStream(inStream);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[16384];
        int bytesRead;
        while ((bytesRead = in.read(buffer)) >= 0) {
            out.write(buffer, 0, bytesRead);
        }
        icoimages = out.toByteArray();
    }
    BufferedImage[] bi = parseICOImage(icoimages);
    int hotSpotX = 0;
    int hotSpotY = 0;
    CursorLoader.CursorImageData cid = new CursorLoader.CursorImageData(bi, 0, hotSpotX, hotSpotY, 0);
    if (isCur) {
        /*
             * Per http://msdn.microsoft.com/en-us/library/ms997538.aspx
             * every .cur file should provide hotspot coordinates.
             */
        hotSpotX = icoimages[FDE_OFFSET + 4] + icoimages[FDE_OFFSET + 5] * 255;
        hotSpotY = icoimages[FDE_OFFSET + 6] + icoimages[FDE_OFFSET + 7] * 255;
        cid.xHotSpot = hotSpotX;
        /*
             * Flip the Y-coordinate.
             */
        cid.yHotSpot = cid.height - 1 - hotSpotY;
    }
    cid.completeCursor();
    return setJmeCursor(cid);
}
Also used : DataInputStream(java.io.DataInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) LittleEndien(com.jme3.util.LittleEndien) BufferedImage(java.awt.image.BufferedImage) DataInput(java.io.DataInput)

Example 33 with Animation

use of com.jme3.animation.Animation in project jmonkeyengine by jMonkeyEngine.

the class TestCinematic method simpleInitApp.

@Override
public void simpleInitApp() {
    //just some text
    NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(getAssetManager(), getInputManager(), getAudioRenderer(), getGuiViewPort());
    Nifty nifty;
    nifty = niftyDisplay.getNifty();
    nifty.fromXmlWithoutStartScreen("Interface/Nifty/CinematicTest.xml");
    getGuiViewPort().addProcessor(niftyDisplay);
    guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
    final BitmapText text = new BitmapText(guiFont, false);
    text.setSize(guiFont.getCharSet().getRenderedSize());
    text.setText("Press enter to play/pause cinematic");
    text.setLocalTranslation((cam.getWidth() - text.getLineWidth()) / 2, cam.getHeight(), 0);
    guiNode.attachChild(text);
    createScene();
    cinematic = new Cinematic(rootNode, 20);
    stateManager.attach(cinematic);
    createCameraMotion();
    //creating spatial animation for the teapot
    AnimationFactory factory = new AnimationFactory(20, "teapotAnim");
    factory.addTimeTranslation(0, new Vector3f(10, 0, 10));
    factory.addTimeTranslation(20, new Vector3f(10, 0, -10));
    factory.addTimeScale(10, new Vector3f(4, 4, 4));
    factory.addTimeScale(20, new Vector3f(1, 1, 1));
    factory.addTimeRotationAngles(20, 0, 4 * FastMath.TWO_PI, 0);
    AnimControl control = new AnimControl();
    control.addAnim(factory.buildAnimation());
    teapot.addControl(control);
    //fade in
    cinematic.addCinematicEvent(0, new FadeEvent(true));
    // cinematic.activateCamera(0, "aroundCam");
    cinematic.addCinematicEvent(0, new AnimationEvent(teapot, "teapotAnim", LoopMode.DontLoop));
    cinematic.addCinematicEvent(0, cameraMotionEvent);
    cinematic.addCinematicEvent(0, new SoundEvent("Sound/Environment/Nature.ogg", LoopMode.Loop));
    cinematic.addCinematicEvent(3f, new SoundEvent("Sound/Effects/kick.wav"));
    cinematic.addCinematicEvent(3, new SubtitleTrack(nifty, "start", 3, "jMonkey engine really kicks A..."));
    cinematic.addCinematicEvent(5.1f, new SoundEvent("Sound/Effects/Beep.ogg", 1));
    cinematic.addCinematicEvent(2, new AnimationEvent(model, "Walk", LoopMode.Loop));
    cinematic.activateCamera(0, "topView");
    //  cinematic.activateCamera(10, "aroundCam");
    //fade out
    cinematic.addCinematicEvent(19, new FadeEvent(false));
    //        cinematic.addCinematicEvent(19, new AbstractCinematicEvent() {
    //
    //            @Override
    //            public void onPlay() {
    //                fade.setDuration(1f / cinematic.getSpeed());
    //                fade.fadeOut();
    //
    //            }
    //
    //            @Override
    //            public void onUpdate(float tpf) {
    //            }
    //
    //            @Override
    //            public void onStop() {
    //            }
    //
    //            @Override
    //            public void onPause() {
    //            }
    //        });
    cinematic.addListener(new CinematicEventListener() {

        public void onPlay(CinematicEvent cinematic) {
            chaseCam.setEnabled(false);
            System.out.println("play");
        }

        public void onPause(CinematicEvent cinematic) {
            System.out.println("pause");
        }

        public void onStop(CinematicEvent cinematic) {
            chaseCam.setEnabled(true);
            fade.setValue(1);
            System.out.println("stop");
        }
    });
    //cinematic.setSpeed(2);
    flyCam.setEnabled(false);
    chaseCam = new ChaseCamera(cam, model, inputManager);
    initInputs();
}
Also used : NiftyJmeDisplay(com.jme3.niftygui.NiftyJmeDisplay) Cinematic(com.jme3.cinematic.Cinematic) ChaseCamera(com.jme3.input.ChaseCamera) Nifty(de.lessvoid.nifty.Nifty) AnimControl(com.jme3.animation.AnimControl) BitmapText(com.jme3.font.BitmapText) AnimationFactory(com.jme3.animation.AnimationFactory) Vector3f(com.jme3.math.Vector3f)

Example 34 with Animation

use of com.jme3.animation.Animation in project jmonkeyengine by jMonkeyEngine.

the class TestTransparentShadow method simpleInitApp.

public void simpleInitApp() {
    cam.setLocation(new Vector3f(5.700248f, 6.161693f, 5.1404157f));
    cam.setRotation(new Quaternion(-0.09441641f, 0.8993388f, -0.24089815f, -0.35248178f));
    viewPort.setBackgroundColor(ColorRGBA.DarkGray);
    Quad q = new Quad(20, 20);
    q.scaleTextureCoordinates(Vector2f.UNIT_XY.mult(10));
    TangentBinormalGenerator.generate(q);
    Geometry geom = new Geometry("floor", q);
    Material mat = assetManager.loadMaterial("Textures/Terrain/Pond/Pond.j3m");
    geom.setMaterial(mat);
    geom.rotate(-FastMath.HALF_PI, 0, 0);
    geom.center();
    geom.setShadowMode(ShadowMode.CastAndReceive);
    rootNode.attachChild(geom);
    AmbientLight al = new AmbientLight();
    al.setColor(ColorRGBA.White.mult(0.7f));
    rootNode.addLight(al);
    DirectionalLight dl1 = new DirectionalLight();
    dl1.setDirection(new Vector3f(0, -1, 0.5f).normalizeLocal());
    dl1.setColor(ColorRGBA.White.mult(1.5f));
    rootNode.addLight(dl1);
    // create the geometry and attach it
    Spatial tree = assetManager.loadModel("Models/Tree/Tree.mesh.j3o");
    tree.setQueueBucket(Bucket.Transparent);
    tree.setShadowMode(ShadowMode.CastAndReceive);
    rootNode.attachChild(tree);
    // Uses Texture from jme3-test-data library!
    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.setShadowMode(ShadowMode.Cast);
    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(0.6f);
    fire.setEndSize(0.1f);
    fire.setGravity(0, 0, 0);
    fire.setLowLife(0.5f);
    fire.setHighLife(1.5f);
    fire.getParticleInfluencer().setVelocityVariation(0.3f);
    fire.setLocalTranslation(5.0f, 0, 1.0f);
    fire.setLocalScale(0.3f);
    fire.setQueueBucket(Bucket.Translucent);
    rootNode.attachChild(fire);
    Material mat2 = assetManager.loadMaterial("Common/Materials/RedColor.j3m");
    Geometry ball = new Geometry("sphere", new Sphere(16, 16, 0.5f));
    ball.setMaterial(mat2);
    ball.setShadowMode(ShadowMode.CastAndReceive);
    rootNode.attachChild(ball);
    ball.setLocalTranslation(-1.0f, 1.5f, 1.0f);
    final DirectionalLightShadowRenderer dlsRenderer = new DirectionalLightShadowRenderer(assetManager, 1024, 1);
    dlsRenderer.setLight(dl1);
    dlsRenderer.setLambda(0.55f);
    dlsRenderer.setShadowIntensity(0.8f);
    dlsRenderer.setShadowCompareMode(CompareMode.Software);
    dlsRenderer.setEdgeFilteringMode(EdgeFilteringMode.Nearest);
    dlsRenderer.displayDebug();
    viewPort.addProcessor(dlsRenderer);
    inputManager.addMapping("stabilize", new KeyTrigger(KeyInput.KEY_B));
    inputManager.addListener(new ActionListener() {

        @Override
        public void onAction(String name, boolean isPressed, float tpf) {
            if (name.equals("stabilize") && isPressed) {
                dlsRenderer.setEnabledStabilization(!dlsRenderer.isEnabledStabilization());
            }
        }
    }, "stabilize");
}
Also used : ParticleEmitter(com.jme3.effect.ParticleEmitter) Quad(com.jme3.scene.shape.Quad) KeyTrigger(com.jme3.input.controls.KeyTrigger) Material(com.jme3.material.Material) Geometry(com.jme3.scene.Geometry) Sphere(com.jme3.scene.shape.Sphere) ActionListener(com.jme3.input.controls.ActionListener) Spatial(com.jme3.scene.Spatial) DirectionalLight(com.jme3.light.DirectionalLight) DirectionalLightShadowRenderer(com.jme3.shadow.DirectionalLightShadowRenderer) AmbientLight(com.jme3.light.AmbientLight)

Example 35 with Animation

use of com.jme3.animation.Animation in project jmonkeyengine by jMonkeyEngine.

the class TestSpatialAnim method simpleInitApp.

@Override
public void simpleInitApp() {
    AmbientLight al = new AmbientLight();
    rootNode.addLight(al);
    DirectionalLight dl = new DirectionalLight();
    dl.setDirection(Vector3f.UNIT_XYZ.negate());
    rootNode.addLight(dl);
    // Create model
    Box box = new Box(1, 1, 1);
    Geometry geom = new Geometry("box", box);
    geom.setMaterial(assetManager.loadMaterial("Textures/Terrain/BrickWall/BrickWall.j3m"));
    Node model = new Node("model");
    model.attachChild(geom);
    Box child = new Box(0.5f, 0.5f, 0.5f);
    Geometry childGeom = new Geometry("box", child);
    childGeom.setMaterial(assetManager.loadMaterial("Textures/Terrain/BrickWall/BrickWall.j3m"));
    Node childModel = new Node("childmodel");
    childModel.setLocalTranslation(2, 2, 2);
    childModel.attachChild(childGeom);
    model.attachChild(childModel);
    //animation parameters
    float animTime = 5;
    int fps = 25;
    float totalXLength = 10;
    //calculating frames
    int totalFrames = (int) (fps * animTime);
    float dT = animTime / totalFrames, t = 0;
    float dX = totalXLength / totalFrames, x = 0;
    float[] times = new float[totalFrames];
    Vector3f[] translations = new Vector3f[totalFrames];
    Quaternion[] rotations = new Quaternion[totalFrames];
    Vector3f[] scales = new Vector3f[totalFrames];
    for (int i = 0; i < totalFrames; ++i) {
        times[i] = t;
        t += dT;
        translations[i] = new Vector3f(x, 0, 0);
        x += dX;
        rotations[i] = Quaternion.IDENTITY;
        scales[i] = Vector3f.UNIT_XYZ;
    }
    SpatialTrack spatialTrack = new SpatialTrack(times, translations, rotations, scales);
    //creating the animation
    Animation spatialAnimation = new Animation("anim", animTime);
    spatialAnimation.setTracks(new SpatialTrack[] { spatialTrack });
    //create spatial animation control
    AnimControl control = new AnimControl();
    HashMap<String, Animation> animations = new HashMap<String, Animation>();
    animations.put("anim", spatialAnimation);
    control.setAnimations(animations);
    model.addControl(control);
    rootNode.attachChild(model);
    //run animation
    control.createChannel().setAnim("anim");
}
Also used : Quaternion(com.jme3.math.Quaternion) HashMap(java.util.HashMap) Node(com.jme3.scene.Node) Box(com.jme3.scene.shape.Box) AnimControl(com.jme3.animation.AnimControl) Geometry(com.jme3.scene.Geometry) SpatialTrack(com.jme3.animation.SpatialTrack) DirectionalLight(com.jme3.light.DirectionalLight) Vector3f(com.jme3.math.Vector3f) Animation(com.jme3.animation.Animation) AmbientLight(com.jme3.light.AmbientLight)

Aggregations

Vector3f (com.jme3.math.Vector3f)18 Animation (com.jme3.animation.Animation)12 Quaternion (com.jme3.math.Quaternion)12 AnimControl (com.jme3.animation.AnimControl)11 BoneTrack (com.jme3.animation.BoneTrack)10 HashMap (java.util.HashMap)9 Bone (com.jme3.animation.Bone)8 SpatialTrack (com.jme3.animation.SpatialTrack)7 TempVars (com.jme3.util.TempVars)6 Material (com.jme3.material.Material)5 Track (com.jme3.animation.Track)4 ParticleEmitter (com.jme3.effect.ParticleEmitter)4 DirectionalLight (com.jme3.light.DirectionalLight)4 Geometry (com.jme3.scene.Geometry)4 Node (com.jme3.scene.Node)4 ArrayList (java.util.ArrayList)4 AmbientLight (com.jme3.light.AmbientLight)3 ColorRGBA (com.jme3.math.ColorRGBA)3 Transform (com.jme3.math.Transform)3 Spatial (com.jme3.scene.Spatial)3