Search in sources :

Example 1 with FbxNode

use of com.jme3.scene.plugins.fbx.node.FbxNode in project jmonkeyengine by jMonkeyEngine.

the class FbxNode method link.

@Override
public void link(FbxObject otherObject) {
    if (otherObject instanceof FbxMaterial) {
        FbxMaterial m = (FbxMaterial) otherObject;
        Material mat = m.material;
        if (cullMode != FaceCullMode.Back)
            mat.getAdditionalRenderState().setFaceCullMode(cullMode);
        for (Geometry g : mesh.geometries) {
            if (g.getUserData("FBXMaterial") != null) {
                if ((Integer) g.getUserData("FBXMaterial") == mesh.lastMaterialId)
                    g.setMaterial(mat);
            } else {
                g.setMaterial(mat);
            }
        }
        mesh.lastMaterialId++;
    } else if (otherObject instanceof FbxNode) {
        FbxNode n = (FbxNode) otherObject;
        node.attachChild(n.node);
        n.parentFbxNode = this;
        if (isLimb() && n.isLimb()) {
            if (bone == null)
                bone = new Bone(name);
            if (n.bone == null)
                n.bone = new Bone(n.name);
            bone.addChild(n.bone);
        }
    } else if (otherObject instanceof FbxMesh) {
        FbxMesh m = (FbxMesh) otherObject;
        m.setParent(node);
        m.parent = this;
        mesh = m;
    }
}
Also used : Geometry(com.jme3.scene.Geometry) Material(com.jme3.material.Material) Bone(com.jme3.animation.Bone)

Example 2 with FbxNode

use of com.jme3.scene.plugins.fbx.node.FbxNode in project jmonkeyengine by jMonkeyEngine.

the class FbxNode method updateWorldTransforms.

public void updateWorldTransforms(Transform jmeParentNodeTransform, Transform parentBindPose) {
    Transform fbxLocalTransform = computeFbxLocalTransform();
    jmeLocalNodeTransform.set(fbxLocalTransform);
    if (jmeParentNodeTransform != null) {
        jmeParentNodeTransform = jmeParentNodeTransform.clone();
        switch(inheritMode) {
            case NoParentScale:
            case ScaleAfterChildRotation:
            case ScaleBeforeChildRotation:
                jmeWorldNodeTransform.set(jmeLocalNodeTransform);
                jmeWorldNodeTransform.combineWithParent(jmeParentNodeTransform);
                break;
        }
    } else {
        jmeWorldNodeTransform.set(jmeLocalNodeTransform);
    }
    if (jmeWorldBindPose != null) {
        jmeLocalBindPose = new Transform();
        // Need to derive local bind pose from world bind pose
        // (this is to be expected for FBX limbs)
        jmeLocalBindPose.set(jmeWorldBindPose);
        jmeLocalBindPose.combineWithParent(parentBindPose.invert());
        // Its somewhat odd for the transforms to differ ...
        System.out.println("Bind Pose for: " + getName());
        if (!jmeLocalBindPose.equals(jmeLocalNodeTransform)) {
            System.out.println("Local Bind: " + jmeLocalBindPose);
            System.out.println("Local Trans: " + jmeLocalNodeTransform);
        }
        if (!jmeWorldBindPose.equals(jmeWorldNodeTransform)) {
            System.out.println("World Bind: " + jmeWorldBindPose);
            System.out.println("World Trans: " + jmeWorldNodeTransform);
        }
    } else {
        // World pose derived from local transforms
        // (this is to be expected for FBX nodes)
        jmeLocalBindPose = new Transform();
        jmeWorldBindPose = new Transform();
        jmeLocalBindPose.set(jmeLocalNodeTransform);
        if (parentBindPose != null) {
            jmeWorldBindPose.set(jmeLocalNodeTransform);
            jmeWorldBindPose.combineWithParent(parentBindPose);
        } else {
            jmeWorldBindPose.set(jmeWorldNodeTransform);
        }
    }
    for (FbxNode child : children) {
        child.updateWorldTransforms(jmeWorldNodeTransform, jmeWorldBindPose);
    }
}
Also used : Transform(com.jme3.math.Transform)

Example 3 with FbxNode

use of com.jme3.scene.plugins.fbx.node.FbxNode in project jmonkeyengine by jMonkeyEngine.

the class FbxLoader method constructAnimations.

private void constructAnimations() {
    // In FBX, animation are not attached to any root.
    // They are implicitly global.
    // So, we need to use hueristics to find which node(s) 
    // an animation is associated with, so we can create the AnimControl
    // in the appropriate location in the scene.
    Map<FbxToJmeTrack, FbxToJmeTrack> pairs = new HashMap<FbxToJmeTrack, FbxToJmeTrack>();
    for (FbxAnimStack stack : animStacks) {
        for (FbxAnimLayer layer : stack.getLayers()) {
            for (FbxAnimCurveNode curveNode : layer.getAnimationCurveNodes()) {
                for (Map.Entry<FbxNode, String> nodePropertyEntry : curveNode.getInfluencedNodeProperties().entrySet()) {
                    FbxToJmeTrack lookupPair = new FbxToJmeTrack();
                    lookupPair.animStack = stack;
                    lookupPair.animLayer = layer;
                    lookupPair.node = nodePropertyEntry.getKey();
                    // Find if this pair is already stored
                    FbxToJmeTrack storedPair = pairs.get(lookupPair);
                    if (storedPair == null) {
                        // If not, store it.
                        storedPair = lookupPair;
                        pairs.put(storedPair, storedPair);
                    }
                    String property = nodePropertyEntry.getValue();
                    storedPair.animCurves.put(property, curveNode);
                }
            }
        }
    }
    // At this point we can construct the animation for all pairs ...
    for (FbxToJmeTrack pair : pairs.values()) {
        String animName = pair.animStack.getName();
        float duration = pair.animStack.getDuration();
        System.out.println("ANIMATION: " + animName + ", duration = " + duration);
        System.out.println("NODE: " + pair.node.getName());
        duration = pair.getDuration();
        if (pair.node instanceof FbxLimbNode) {
            // Find the spatial that has the skeleton for this limb.
            FbxLimbNode limbNode = (FbxLimbNode) pair.node;
            Bone bone = limbNode.getJmeBone();
            Spatial jmeSpatial = limbNode.getSkeletonHolder().getJmeObject();
            Skeleton skeleton = limbNode.getSkeletonHolder().getJmeSkeleton();
            // Get the animation control (create if missing).
            AnimControl animControl = jmeSpatial.getControl(AnimControl.class);
            if (animControl.getSkeleton() != skeleton) {
                throw new UnsupportedOperationException();
            }
            // Get the animation (create if missing).
            Animation anim = animControl.getAnim(animName);
            if (anim == null) {
                anim = new Animation(animName, duration);
                animControl.addAnim(anim);
            }
            // Find the bone index from the spatial's skeleton.
            int boneIndex = skeleton.getBoneIndex(bone);
            // Generate the bone track.
            BoneTrack bt = pair.toJmeBoneTrack(boneIndex, bone.getBindInverseTransform());
            // Add the bone track to the animation.
            anim.addTrack(bt);
        } else {
            // Create the spatial animation
            Animation anim = new Animation(animName, duration);
            anim.setTracks(new Track[] { pair.toJmeSpatialTrack() });
            // Get the animation control (create if missing).
            Spatial jmeSpatial = pair.node.getJmeObject();
            AnimControl animControl = jmeSpatial.getControl(AnimControl.class);
            if (animControl == null) {
                animControl = new AnimControl(null);
                jmeSpatial.addControl(animControl);
            }
            // Add the spatial animation
            animControl.addAnim(anim);
        }
    }
}
Also used : BoneTrack(com.jme3.animation.BoneTrack) HashMap(java.util.HashMap) FbxAnimCurveNode(com.jme3.scene.plugins.fbx.anim.FbxAnimCurveNode) AnimControl(com.jme3.animation.AnimControl) FbxNode(com.jme3.scene.plugins.fbx.node.FbxNode) Spatial(com.jme3.scene.Spatial) Animation(com.jme3.animation.Animation) FbxToJmeTrack(com.jme3.scene.plugins.fbx.anim.FbxToJmeTrack) Skeleton(com.jme3.animation.Skeleton) Bone(com.jme3.animation.Bone) FbxLimbNode(com.jme3.scene.plugins.fbx.anim.FbxLimbNode) FbxAnimLayer(com.jme3.scene.plugins.fbx.anim.FbxAnimLayer) HashMap(java.util.HashMap) Map(java.util.Map) FbxAnimStack(com.jme3.scene.plugins.fbx.anim.FbxAnimStack)

Example 4 with FbxNode

use of com.jme3.scene.plugins.fbx.node.FbxNode in project jmonkeyengine by jMonkeyEngine.

the class FbxLoader method updateWorldTransforms.

/**
     * Updates world transforms and bind poses for the FBX scene graph.
     */
private void updateWorldTransforms() {
    FbxNode fbxRoot = (FbxNode) objectMap.get(FbxId.ROOT);
    fbxRoot.updateWorldTransforms(null, null);
}
Also used : FbxNode(com.jme3.scene.plugins.fbx.node.FbxNode)

Example 5 with FbxNode

use of com.jme3.scene.plugins.fbx.node.FbxNode in project jmonkeyengine by jMonkeyEngine.

the class FbxLoader method constructSkeletons.

private void constructSkeletons() {
    FbxNode fbxRoot = (FbxNode) objectMap.get(FbxId.ROOT);
    FbxNode.createSkeletons(fbxRoot);
}
Also used : FbxNode(com.jme3.scene.plugins.fbx.node.FbxNode)

Aggregations

FbxNode (com.jme3.scene.plugins.fbx.node.FbxNode)6 Bone (com.jme3.animation.Bone)4 FbxLimbNode (com.jme3.scene.plugins.fbx.anim.FbxLimbNode)4 FbxNode (com.jme3.scene.plugins.fbx.objects.FbxNode)4 AnimControl (com.jme3.animation.AnimControl)3 FbxAnimCurveNode (com.jme3.scene.plugins.fbx.anim.FbxAnimCurveNode)3 HashMap (java.util.HashMap)3 Animation (com.jme3.animation.Animation)2 BoneTrack (com.jme3.animation.BoneTrack)2 Skeleton (com.jme3.animation.Skeleton)2 SkeletonControl (com.jme3.animation.SkeletonControl)2 Material (com.jme3.material.Material)2 Node (com.jme3.scene.Node)2 Spatial (com.jme3.scene.Spatial)2 FbxAnimNode (com.jme3.scene.plugins.fbx.objects.FbxAnimNode)2 FbxBindPose (com.jme3.scene.plugins.fbx.objects.FbxBindPose)2 FbxMesh (com.jme3.scene.plugins.fbx.objects.FbxMesh)2 FbxObject (com.jme3.scene.plugins.fbx.objects.FbxObject)2 Map (java.util.Map)2 Track (com.jme3.animation.Track)1