Search in sources :

Example 6 with Skeleton

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

the class TestOgreComplexAnim method simpleInitApp.

@Override
public void simpleInitApp() {
    flyCam.setMoveSpeed(10f);
    cam.setLocation(new Vector3f(6.4013605f, 7.488437f, 12.843031f));
    cam.setRotation(new Quaternion(-0.060740203f, 0.93925786f, -0.2398315f, -0.2378785f));
    DirectionalLight dl = new DirectionalLight();
    dl.setDirection(new Vector3f(-0.1f, -0.7f, -1).normalizeLocal());
    dl.setColor(new ColorRGBA(1f, 1f, 1f, 1.0f));
    rootNode.addLight(dl);
    Node model = (Node) assetManager.loadModel("Models/Oto/Oto.mesh.xml");
    control = model.getControl(AnimControl.class);
    AnimChannel feet = control.createChannel();
    AnimChannel leftHand = control.createChannel();
    AnimChannel rightHand = control.createChannel();
    // feet will dodge
    feet.addFromRootBone("hip.right");
    feet.addFromRootBone("hip.left");
    feet.setAnim("Dodge");
    feet.setSpeed(2);
    feet.setLoopMode(LoopMode.Cycle);
    // will blend over 15 seconds to stand
    feet.setAnim("Walk", 15);
    feet.setSpeed(0.25f);
    feet.setLoopMode(LoopMode.Cycle);
    // left hand will pull
    leftHand.addFromRootBone("uparm.right");
    leftHand.setAnim("pull");
    leftHand.setSpeed(.5f);
    // will blend over 15 seconds to stand
    leftHand.setAnim("stand", 15);
    // right hand will push
    rightHand.addBone("spinehigh");
    rightHand.addFromRootBone("uparm.left");
    rightHand.setAnim("push");
    SkeletonDebugger skeletonDebug = new SkeletonDebugger("skeleton", control.getSkeleton());
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat.getAdditionalRenderState().setWireframe(true);
    mat.setColor("Color", ColorRGBA.Green);
    mat.getAdditionalRenderState().setDepthTest(false);
    skeletonDebug.setMaterial(mat);
    model.attachChild(skeletonDebug);
    rootNode.attachChild(model);
}
Also used : SkeletonDebugger(com.jme3.scene.debug.SkeletonDebugger) ColorRGBA(com.jme3.math.ColorRGBA) Quaternion(com.jme3.math.Quaternion) Vector3f(com.jme3.math.Vector3f) DirectionalLight(com.jme3.light.DirectionalLight) Node(com.jme3.scene.Node) AnimChannel(com.jme3.animation.AnimChannel) Material(com.jme3.material.Material) AnimControl(com.jme3.animation.AnimControl)

Example 7 with Skeleton

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

the class TestCustomAnim 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);
    Box box = new Box(1, 1, 1);
    VertexBuffer weightsHW = new VertexBuffer(Type.HWBoneWeight);
    VertexBuffer indicesHW = new VertexBuffer(Type.HWBoneIndex);
    indicesHW.setUsage(Usage.CpuOnly);
    weightsHW.setUsage(Usage.CpuOnly);
    box.setBuffer(weightsHW);
    box.setBuffer(indicesHW);
    // Setup bone weight buffer
    FloatBuffer weights = FloatBuffer.allocate(box.getVertexCount() * 4);
    VertexBuffer weightsBuf = new VertexBuffer(Type.BoneWeight);
    weightsBuf.setupData(Usage.CpuOnly, 4, Format.Float, weights);
    box.setBuffer(weightsBuf);
    // Setup bone index buffer
    ByteBuffer indices = ByteBuffer.allocate(box.getVertexCount() * 4);
    VertexBuffer indicesBuf = new VertexBuffer(Type.BoneIndex);
    indicesBuf.setupData(Usage.CpuOnly, 4, Format.UnsignedByte, indices);
    box.setBuffer(indicesBuf);
    // Create bind pose buffers
    box.generateBindPose(true);
    // Create skeleton
    bone = new Bone("root");
    bone.setBindTransforms(Vector3f.ZERO, Quaternion.IDENTITY, Vector3f.UNIT_XYZ);
    bone.setUserControl(true);
    skeleton = new Skeleton(new Bone[] { bone });
    // Assign all verticies to bone 0 with weight 1
    for (int i = 0; i < box.getVertexCount() * 4; i += 4) {
        // assign vertex to bone index 0
        indices.array()[i + 0] = 0;
        indices.array()[i + 1] = 0;
        indices.array()[i + 2] = 0;
        indices.array()[i + 3] = 0;
        // set weight to 1 only for first entry
        weights.array()[i + 0] = 1;
        weights.array()[i + 1] = 0;
        weights.array()[i + 2] = 0;
        weights.array()[i + 3] = 0;
    }
    // Maximum number of weights per bone is 1
    box.setMaxNumWeights(1);
    // Create model
    Geometry geom = new Geometry("box", box);
    geom.setMaterial(assetManager.loadMaterial("Textures/Terrain/BrickWall/BrickWall.j3m"));
    Node model = new Node("model");
    model.attachChild(geom);
    // Create skeleton control
    SkeletonControl skeletonControl = new SkeletonControl(skeleton);
    model.addControl(skeletonControl);
    rootNode.attachChild(model);
}
Also used : Geometry(com.jme3.scene.Geometry) VertexBuffer(com.jme3.scene.VertexBuffer) DirectionalLight(com.jme3.light.DirectionalLight) Node(com.jme3.scene.Node) SkeletonControl(com.jme3.animation.SkeletonControl) Box(com.jme3.scene.shape.Box) FloatBuffer(java.nio.FloatBuffer) Skeleton(com.jme3.animation.Skeleton) Bone(com.jme3.animation.Bone) ByteBuffer(java.nio.ByteBuffer) AmbientLight(com.jme3.light.AmbientLight)

Example 8 with Skeleton

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

the class SkeletonLoader method startElement.

public void startElement(String uri, String localName, String qName, Attributes attribs) throws SAXException {
    if (qName.equals("position") || qName.equals("translate")) {
        position = SAXUtil.parseVector3(attribs);
    } else if (qName.equals("rotation") || qName.equals("rotate")) {
        angle = SAXUtil.parseFloat(attribs.getValue("angle"));
    } else if (qName.equals("axis")) {
        assert elementStack.peek().equals("rotation") || elementStack.peek().equals("rotate");
        axis = SAXUtil.parseVector3(attribs);
    } else if (qName.equals("scale")) {
        scale = SAXUtil.parseVector3(attribs);
    } else if (qName.equals("keyframe")) {
        assert elementStack.peek().equals("keyframes");
        time = SAXUtil.parseFloat(attribs.getValue("time"));
    } else if (qName.equals("keyframes")) {
        assert elementStack.peek().equals("track");
    } else if (qName.equals("track")) {
        assert elementStack.peek().equals("tracks");
        String boneName = SAXUtil.parseString(attribs.getValue("bone"));
        Bone bone = nameToBone.get(boneName);
        int index = skeleton.getBoneIndex(bone);
        track = new BoneTrack(index);
    } else if (qName.equals("boneparent")) {
        assert elementStack.peek().equals("bonehierarchy");
        String boneName = attribs.getValue("bone");
        String parentName = attribs.getValue("parent");
        Bone bone = nameToBone.get(boneName);
        Bone parent = nameToBone.get(parentName);
        parent.addChild(bone);
    } else if (qName.equals("bone")) {
        assert elementStack.peek().equals("bones");
        // insert bone into indexed map
        bone = new Bone(attribs.getValue("name"));
        int id = SAXUtil.parseInt(attribs.getValue("id"));
        indexToBone.put(id, bone);
        nameToBone.put(bone.getName(), bone);
    } else if (qName.equals("tracks")) {
        assert elementStack.peek().equals("animation");
        tracks.clear();
    } else if (qName.equals("animation")) {
        assert elementStack.peek().equals("animations");
        String name = SAXUtil.parseString(attribs.getValue("name"));
        float length = SAXUtil.parseFloat(attribs.getValue("length"));
        animation = new Animation(name, length);
    } else if (qName.equals("bonehierarchy")) {
        assert elementStack.peek().equals("skeleton");
    } else if (qName.equals("animations")) {
        assert elementStack.peek().equals("skeleton");
        animations = new ArrayList<Animation>();
    } else if (qName.equals("bones")) {
        assert elementStack.peek().equals("skeleton");
    } else if (qName.equals("skeleton")) {
        assert elementStack.size() == 0;
    }
    elementStack.add(qName);
}
Also used : BoneTrack(com.jme3.animation.BoneTrack) Animation(com.jme3.animation.Animation) Bone(com.jme3.animation.Bone)

Example 9 with Skeleton

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

the class AnimationHelper method applyAnimations.

/**
     * The method applies skeleton animations to the given node.
     * @param node
     *            the node where the animations will be applied
     * @param skeleton
     *            the skeleton of the node
     * @param animationMatchMethod
     *            the way animation should be matched with skeleton
     */
public void applyAnimations(Node node, Skeleton skeleton, AnimationMatchMethod animationMatchMethod) {
    node.addControl(new SkeletonControl(skeleton));
    blenderContext.setNodeForSkeleton(skeleton, node);
    List<BlenderAction> actions = this.getActions(skeleton, animationMatchMethod);
    if (actions.size() > 0) {
        List<Animation> animations = new ArrayList<Animation>();
        for (BlenderAction action : actions) {
            BoneTrack[] tracks = action.toTracks(skeleton, blenderContext);
            if (tracks != null && tracks.length > 0) {
                Animation boneAnimation = new Animation(action.getName(), action.getAnimationTime());
                boneAnimation.setTracks(tracks);
                animations.add(boneAnimation);
                Long animatedNodeOMA = ((Number) blenderContext.getMarkerValue(ObjectHelper.OMA_MARKER, node)).longValue();
                blenderContext.addAnimation(animatedNodeOMA, boneAnimation);
            }
        }
        if (animations.size() > 0) {
            AnimControl control = new AnimControl(skeleton);
            HashMap<String, Animation> anims = new HashMap<String, Animation>(animations.size());
            for (int i = 0; i < animations.size(); ++i) {
                Animation animation = animations.get(i);
                anims.put(animation.getName(), animation);
            }
            control.setAnimations(anims);
            node.addControl(control);
            // make sure that SkeletonControl is added AFTER the AnimControl
            SkeletonControl skeletonControl = node.getControl(SkeletonControl.class);
            if (skeletonControl != null) {
                node.removeControl(SkeletonControl.class);
                node.addControl(skeletonControl);
            }
        }
    }
}
Also used : BoneTrack(com.jme3.animation.BoneTrack) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SkeletonControl(com.jme3.animation.SkeletonControl) AnimControl(com.jme3.animation.AnimControl) Animation(com.jme3.animation.Animation)

Example 10 with Skeleton

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

the class BoneContext method buildBone.

/**
     * This method builds the bone. It recursively builds the bone's children.
     * 
     * @param bones
     *            a list of bones where the newly created bone will be added
     * @param skeletonOwnerOma
     *            the spatial of the object that will own the skeleton
     * @param blenderContext
     *            the blender context
     * @return newly created bone
     */
public Bone buildBone(List<Bone> bones, Long skeletonOwnerOma, BlenderContext blenderContext) {
    this.skeletonOwnerOma = skeletonOwnerOma;
    Long boneOMA = boneStructure.getOldMemoryAddress();
    bone = new Bone(boneName);
    bones.add(bone);
    blenderContext.addLoadedFeatures(boneOMA, LoadedDataType.STRUCTURE, boneStructure);
    blenderContext.addLoadedFeatures(boneOMA, LoadedDataType.FEATURE, bone);
    ObjectHelper objectHelper = blenderContext.getHelper(ObjectHelper.class);
    Structure skeletonOwnerObjectStructure = (Structure) blenderContext.getLoadedFeature(skeletonOwnerOma, LoadedDataType.STRUCTURE);
    // I could load 'imat' here, but apparently in some older blenders there were bugs or unfinished functionalities that stored ZERO matrix in imat field
    // loading 'obmat' and inverting it makes us avoid errors in such cases
    Matrix4f invertedObjectOwnerGlobalMatrix = objectHelper.getMatrix(skeletonOwnerObjectStructure, "obmat", blenderContext.getBlenderKey().isFixUpAxis()).invertLocal();
    if (objectHelper.isParent(skeletonOwnerOma, armatureObjectOMA)) {
        boneMatrixInModelSpace = globalBoneMatrix.mult(invertedObjectOwnerGlobalMatrix);
    } else {
        boneMatrixInModelSpace = invertedObjectOwnerGlobalMatrix.mult(globalBoneMatrix);
    }
    Matrix4f boneLocalMatrix = parent == null ? boneMatrixInModelSpace : parent.boneMatrixInModelSpace.invert().multLocal(boneMatrixInModelSpace);
    Vector3f poseLocation = parent == null || !this.is(CONNECTED_TO_PARENT) ? boneLocalMatrix.toTranslationVector() : new Vector3f(0, parent.length, 0);
    Quaternion rotation = boneLocalMatrix.toRotationQuat().normalizeLocal();
    Vector3f scale = boneLocalMatrix.toScaleVector();
    bone.setBindTransforms(poseLocation, rotation, scale);
    for (BoneContext child : children) {
        bone.addChild(child.buildBone(bones, skeletonOwnerOma, blenderContext));
    }
    return bone;
}
Also used : ObjectHelper(com.jme3.scene.plugins.blender.objects.ObjectHelper) Matrix4f(com.jme3.math.Matrix4f) Quaternion(com.jme3.math.Quaternion) Vector3f(com.jme3.math.Vector3f) Bone(com.jme3.animation.Bone) Structure(com.jme3.scene.plugins.blender.file.Structure)

Aggregations

Quaternion (com.jme3.math.Quaternion)13 Vector3f (com.jme3.math.Vector3f)13 Bone (com.jme3.animation.Bone)11 Animation (com.jme3.animation.Animation)7 Skeleton (com.jme3.animation.Skeleton)7 AnimControl (com.jme3.animation.AnimControl)6 BoneTrack (com.jme3.animation.BoneTrack)6 TempVars (com.jme3.util.TempVars)6 HashMap (java.util.HashMap)6 SkeletonControl (com.jme3.animation.SkeletonControl)5 Material (com.jme3.material.Material)3 Transform (com.jme3.math.Transform)3 Node (com.jme3.scene.Node)3 Spatial (com.jme3.scene.Spatial)3 AnimChannel (com.jme3.animation.AnimChannel)2 Track (com.jme3.animation.Track)2 SixDofJoint (com.jme3.bullet.joints.SixDofJoint)2 DirectionalLight (com.jme3.light.DirectionalLight)2 Matrix4f (com.jme3.math.Matrix4f)2 Geometry (com.jme3.scene.Geometry)2