com.jme3.animation.SkeletonControl Java Examples

The following examples show how to use com.jme3.animation.SkeletonControl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ControlTreeNodeFactory.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
@Override
@FxThread
public <T, V extends TreeNode<T>> @Nullable V createFor(@Nullable final T element, final long objectId) {

    if (element instanceof MotionEvent) {
        return unsafeCast(new MotionEventTreeNode((MotionEvent) element, objectId));
    } else if (element instanceof KinematicRagdollControl) {
        return unsafeCast(new RagdollControlTreeNode((KinematicRagdollControl) element, objectId));
    } else if (element instanceof VehicleControl) {
        return unsafeCast(new VehicleControlTreeNode((VehicleControl) element, objectId));
    } else if (element instanceof SkeletonControl) {
        return unsafeCast(new SkeletonControlTreeNode((SkeletonControl) element, objectId));
    } else if (element instanceof BetterCharacterControl) {
        return unsafeCast(new BetterCharacterControlTreeNode((BetterCharacterControl) element, objectId));
    } else if (element instanceof RigidBodyControl) {
        return unsafeCast(new RigidBodyControlTreeNode((RigidBodyControl) element, objectId));
    }  else if (element instanceof LightControl) {
        return unsafeCast(new LightControlTreeNode((LightControl) element, objectId));
    } else if (element instanceof Control) {
        return unsafeCast(new ControlTreeNode<>((Control) element, objectId));
    }

    return null;
}
 
Example #2
Source File: TestAttachmentsNode.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void simpleInitApp() {
    flyCam.setMoveSpeed(10f);
    cam.setLocation(new Vector3f(6.4f, 7.5f, 12.8f));
    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(ColorRGBA.White);
    rootNode.addLight(dl);

    Spatial model = assetManager.loadModel("Models/Jaime/Jaime.j3o");
    control = model.getControl(AnimControl.class);
    SkeletonControl skeletonControl = model.getControl(SkeletonControl.class);

    model.center();
    model.setLocalScale(5f);

    control.addListener(this);
    channel = control.createChannel();
    channel.setAnim("Idle");

    Box box = new Box(0.3f, 0.02f, 0.02f);
    Geometry saber = new Geometry("saber", box);
    saber.move(0.4f, 0.05f, 0.01f);
    Material red = assetManager.loadMaterial("Common/Materials/RedColor.j3m");
    saber.setMaterial(red);
    Node n = skeletonControl.getAttachmentsNode("hand.R");
    n.attachChild(saber);
    rootNode.attachChild(model);

    inputManager.addListener(this, "Attack");
    inputManager.addMapping("Attack", new KeyTrigger(KeyInput.KEY_SPACE));
}
 
Example #3
Source File: RagdollUtils.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Build a map of mesh vertices in a subtree of the scene graph.
 *
 * @param model the root of the subtree (may be null)
 * @return a new map (not null)
 */
public static Map<Integer, List<Float>> buildPointMap(Spatial model) {
    Map<Integer, List<Float>> map = new HashMap<>();

    SkeletonControl skeletonCtrl = model.getControl(SkeletonControl.class);
    Mesh[] targetMeshes = skeletonCtrl.getTargets();
    for (Mesh mesh : targetMeshes) {
        buildPointMapForMesh(mesh, map);
    }

    return map;
}
 
Example #4
Source File: TestOgreAnim.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@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);

    Spatial model = (Spatial) assetManager.loadModel("Models/Oto/Oto.mesh.xml");
    model.center();

    control = model.getControl(AnimControl.class);
    control.addListener(this);
    channel = control.createChannel();

    for (String anim : control.getAnimationNames())
        System.out.println(anim);

    channel.setAnim("stand");

    SkeletonControl skeletonControl = model.getControl(SkeletonControl.class);

    Box b = new Box(.25f,3f,.25f);
    Geometry item = new Geometry("Item", b);
    item.move(0, 1.5f, 0);
    item.setMaterial(assetManager.loadMaterial("Common/Materials/RedColor.j3m"));
    Node n = skeletonControl.getAttachmentsNode("hand.right");
    n.attachChild(item);

    rootNode.attachChild(model);

    inputManager.addListener(this, "Attack");
    inputManager.addMapping("Attack", new KeyTrigger(KeyInput.KEY_SPACE));
}
 
Example #5
Source File: SkeletonControlTreeNode.java    From jmonkeybuilder with Apache License 2.0 4 votes vote down vote up
public SkeletonControlTreeNode(@NotNull final SkeletonControl element, final long objectId) {
    super(element, objectId);
}
 
Example #6
Source File: FbxNode.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static Spatial createScene(FbxNode fbxNode) {
    Spatial jmeSpatial = fbxNode.getJmeObject();
    
    if (jmeSpatial instanceof Node) {
        // Attach children to Node
        Node jmeNode = (Node) jmeSpatial;
        for (FbxNode fbxChild : fbxNode.children) {
            if (!(fbxChild instanceof FbxLimbNode)) {
                createScene(fbxChild);
                
                FbxNode preferredParent = fbxChild.getPreferredParent();
                Spatial jmeChild = fbxChild.getJmeObject();
                if (preferredParent != null) {
                    System.out.println("Preferred parent for " + fbxChild + " is " + preferredParent);
                    
                    Node jmePreferredParent = (Node) preferredParent.getJmeObject();
                    relocateSpatial(jmeChild, fbxChild.jmeWorldNodeTransform, 
                                              preferredParent.jmeWorldNodeTransform);
                    jmePreferredParent.attachChild(jmeChild);
                } else {
                    jmeNode.attachChild(jmeChild);
                }
            }
        }
    }
    
    if (fbxNode.skeleton != null) { 
        jmeSpatial.addControl(new AnimControl(fbxNode.skeleton));
        jmeSpatial.addControl(new SkeletonControl(fbxNode.skeleton));
        
        SkeletonDebugger sd = new SkeletonDebugger("debug", fbxNode.skeleton);
        Material mat = new Material(fbxNode.assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat.getAdditionalRenderState().setWireframe(true);
        mat.getAdditionalRenderState().setDepthTest(false);
        mat.setColor("Color", ColorRGBA.Green);
        sd.setMaterial(mat);
        
        ((Node)jmeSpatial).attachChild(sd);
    }
    
    return jmeSpatial;
}
 
Example #7
Source File: KinematicRagdollControl.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void setSpatial(Spatial model) {
    if (model == null) {
        removeFromPhysicsSpace();
        clearData();
        return;
    }
    targetModel = model;
    Node parent = model.getParent();


    Vector3f initPosition = model.getLocalTranslation().clone();
    Quaternion initRotation = model.getLocalRotation().clone();
    initScale = model.getLocalScale().clone();

    model.removeFromParent();
    model.setLocalTranslation(Vector3f.ZERO);
    model.setLocalRotation(Quaternion.IDENTITY);
    model.setLocalScale(1);
    //HACK ALERT change this
    //I remove the skeletonControl and readd it to the spatial to make sure it's after the ragdollControl in the stack
    //Find a proper way to order the controls.
    SkeletonControl sc = model.getControl(SkeletonControl.class);
    model.removeControl(sc);
    model.addControl(sc);
    //---- 

    removeFromPhysicsSpace();
    clearData();
    // put into bind pose and compute bone transforms in model space
    // maybe dont reset to ragdoll out of animations?
    scanSpatial(model);


    if (parent != null) {
        parent.attachChild(model);

    }
    model.setLocalTranslation(initPosition);
    model.setLocalRotation(initRotation);
    model.setLocalScale(initScale);

    logger.log(Level.INFO, "Created physics ragdoll for skeleton {0}", skeleton);
}
 
Example #8
Source File: KinematicRagdollControl.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void setSpatial(Spatial model) {
    if (model == null) {
        removeFromPhysicsSpace();
        clearData();
        return;
    }
    targetModel = model;
    Node parent = model.getParent();


    Vector3f initPosition = model.getLocalTranslation().clone();
    Quaternion initRotation = model.getLocalRotation().clone();
    initScale = model.getLocalScale().clone();

    model.removeFromParent();
    model.setLocalTranslation(Vector3f.ZERO);
    model.setLocalRotation(Quaternion.IDENTITY);
    model.setLocalScale(1);
    //HACK ALERT change this
    //I remove the skeletonControl and readd it to the spatial to make sure it's after the ragdollControl in the stack
    //Find a proper way to order the controls.
    SkeletonControl sc = model.getControl(SkeletonControl.class);
    model.removeControl(sc);
    model.addControl(sc);
    //---- 

    removeFromPhysicsSpace();
    clearData();
    // put into bind pose and compute bone transforms in model space
    // maybe dont reset to ragdoll out of animations?
    scanSpatial(model);


    if (parent != null) {
        parent.attachChild(model);

    }
    model.setLocalTranslation(initPosition);
    model.setLocalRotation(initRotation);
    model.setLocalScale(initScale);

    logger.log(Level.INFO, "Created physics ragdoll for skeleton {0}", skeleton);
}
 
Example #9
Source File: TestCustomAnim.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@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);

    // 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);
}