Search in sources :

Example 21 with Bone

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

the class ConstraintHelper method loadConstraints.

/**
     * This method reads constraints for for the given structure. The
     * constraints are loaded only once for object/bone.
     * 
     * @param objectStructure
     *            the structure we read constraint's for
     * @param blenderContext
     *            the blender context
     * @throws BlenderFileException
     */
public void loadConstraints(Structure objectStructure, BlenderContext blenderContext) throws BlenderFileException {
    LOGGER.fine("Loading constraints.");
    // reading influence ipos for the constraints
    AnimationHelper animationHelper = blenderContext.getHelper(AnimationHelper.class);
    Map<String, Map<String, Ipo>> constraintsIpos = new HashMap<String, Map<String, Ipo>>();
    Pointer pActions = (Pointer) objectStructure.getFieldValue("action");
    if (pActions.isNotNull()) {
        List<Structure> actions = pActions.fetchData();
        for (Structure action : actions) {
            Structure chanbase = (Structure) action.getFieldValue("chanbase");
            List<Structure> actionChannels = chanbase.evaluateListBase();
            for (Structure actionChannel : actionChannels) {
                Map<String, Ipo> ipos = new HashMap<String, Ipo>();
                Structure constChannels = (Structure) actionChannel.getFieldValue("constraintChannels");
                List<Structure> constraintChannels = constChannels.evaluateListBase();
                for (Structure constraintChannel : constraintChannels) {
                    Pointer pIpo = (Pointer) constraintChannel.getFieldValue("ipo");
                    if (pIpo.isNotNull()) {
                        String constraintName = constraintChannel.getFieldValue("name").toString();
                        Ipo ipo = animationHelper.fromIpoStructure(pIpo.fetchData().get(0), blenderContext);
                        ipos.put(constraintName, ipo);
                    }
                }
                String actionName = actionChannel.getFieldValue("name").toString();
                constraintsIpos.put(actionName, ipos);
            }
        }
    }
    // loading constraints connected with the object's bones
    Pointer pPose = (Pointer) objectStructure.getFieldValue("pose");
    if (pPose.isNotNull()) {
        List<Structure> poseChannels = ((Structure) pPose.fetchData().get(0).getFieldValue("chanbase")).evaluateListBase();
        for (Structure poseChannel : poseChannels) {
            List<Constraint> constraintsList = new ArrayList<Constraint>();
            Long boneOMA = Long.valueOf(((Pointer) poseChannel.getFieldValue("bone")).getOldMemoryAddress());
            // the name is read directly from structure because bone might
            // not yet be loaded
            String name = blenderContext.getFileBlock(boneOMA).getStructure(blenderContext).getFieldValue("name").toString();
            List<Structure> constraints = ((Structure) poseChannel.getFieldValue("constraints")).evaluateListBase();
            for (Structure constraint : constraints) {
                String constraintName = constraint.getFieldValue("name").toString();
                Map<String, Ipo> ipoMap = constraintsIpos.get(name);
                Ipo ipo = ipoMap == null ? null : ipoMap.get(constraintName);
                if (ipo == null) {
                    float enforce = ((Number) constraint.getFieldValue("enforce")).floatValue();
                    ipo = animationHelper.fromValue(enforce);
                }
                constraintsList.add(new BoneConstraint(constraint, boneOMA, ipo, blenderContext));
            }
            blenderContext.addConstraints(boneOMA, constraintsList);
        }
    }
    // loading constraints connected with the object itself
    List<Structure> constraints = ((Structure) objectStructure.getFieldValue("constraints")).evaluateListBase();
    if (constraints != null && constraints.size() > 0) {
        Pointer pData = (Pointer) objectStructure.getFieldValue("data");
        String dataType = pData.isNotNull() ? pData.fetchData().get(0).getType() : null;
        List<Constraint> constraintsList = new ArrayList<Constraint>(constraints.size());
        for (Structure constraint : constraints) {
            String constraintName = constraint.getFieldValue("name").toString();
            String objectName = objectStructure.getName();
            Map<String, Ipo> objectConstraintsIpos = constraintsIpos.get(objectName);
            Ipo ipo = objectConstraintsIpos != null ? objectConstraintsIpos.get(constraintName) : null;
            if (ipo == null) {
                float enforce = ((Number) constraint.getFieldValue("enforce")).floatValue();
                ipo = animationHelper.fromValue(enforce);
            }
            constraintsList.add(this.createConstraint(dataType, constraint, objectStructure.getOldMemoryAddress(), ipo, blenderContext));
        }
        blenderContext.addConstraints(objectStructure.getOldMemoryAddress(), constraintsList);
    }
}
Also used : AnimationHelper(com.jme3.scene.plugins.blender.animations.AnimationHelper) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Ipo(com.jme3.scene.plugins.blender.animations.Ipo) Pointer(com.jme3.scene.plugins.blender.file.Pointer) Structure(com.jme3.scene.plugins.blender.file.Structure) HashMap(java.util.HashMap) Map(java.util.Map)

Example 22 with Bone

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

the class ConstraintDefinitionMaintainVolume method bake.

@Override
public void bake(Space ownerSpace, Space targetSpace, Transform targetTransform, float influence) {
    if (trackToBeChanged && influence > 0) {
        // but in case of bones we need to make computations
        if (this.getOwner() instanceof Bone) {
            Transform ownerTransform = this.getOwnerTransform(ownerSpace);
            switch(flag) {
                case FLAG_MASK_X:
                    ownerTransform.getScale().multLocal(1, volume, volume);
                    break;
                case FLAG_MASK_Y:
                    ownerTransform.getScale().multLocal(volume, 1, volume);
                    break;
                case FLAG_MASK_Z:
                    ownerTransform.getScale().multLocal(volume, volume, 1);
                    break;
                default:
                    throw new IllegalStateException("Unknown flag value: " + flag);
            }
            this.applyOwnerTransform(ownerTransform, ownerSpace);
        }
    }
}
Also used : Bone(com.jme3.animation.Bone) Transform(com.jme3.math.Transform)

Example 23 with Bone

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

the class ConstraintDefinitionTransLike method bake.

@Override
public void bake(Space ownerSpace, Space targetSpace, Transform targetTransform, float influence) {
    if (influence == 0 || targetTransform == null) {
        // no need to do anything
        return;
    }
    // Bone or Node
    Object target = this.getTarget();
    // Bone or Node
    Object owner = this.getOwner();
    if (!target.getClass().equals(owner.getClass())) {
        ConstraintHelper constraintHelper = blenderContext.getHelper(ConstraintHelper.class);
        TempVars tempVars = TempVars.get();
        Matrix4f m = constraintHelper.toMatrix(targetTransform, tempVars.tempMat4);
        tempVars.tempMat42.set(BoneContext.BONE_ARMATURE_TRANSFORMATION_MATRIX);
        if (target instanceof Bone) {
            tempVars.tempMat42.invertLocal();
        }
        m = m.multLocal(tempVars.tempMat42);
        tempVars.release();
        targetTransform = new Transform(m.toTranslationVector(), m.toRotationQuat(), m.toScaleVector());
    }
    this.applyOwnerTransform(targetTransform, ownerSpace);
}
Also used : Matrix4f(com.jme3.math.Matrix4f) TempVars(com.jme3.util.TempVars) Bone(com.jme3.animation.Bone) ConstraintHelper(com.jme3.scene.plugins.blender.constraints.ConstraintHelper) Transform(com.jme3.math.Transform)

Example 24 with Bone

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

the class ConstraintDefinitionTransLike method getTarget.

/**
     * @return the target feature; it is either Node or Bone (vertex group subtarger is not yet supported)
     */
private Object getTarget() {
    Object target = blenderContext.getLoadedFeature(targetOMA, LoadedDataType.FEATURE);
    if (subtargetName != null && blenderContext.getMarkerValue(ObjectHelper.ARMATURE_NODE_MARKER, target) != null) {
        Skeleton skeleton = blenderContext.getSkeleton(targetOMA);
        target = skeleton.getBone(subtargetName);
    }
    return target;
}
Also used : Skeleton(com.jme3.animation.Skeleton)

Example 25 with Bone

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

the class KinematicRagdollControl method boneRecursion.

protected void boneRecursion(Spatial model, Bone bone, PhysicsRigidBody parent, int reccount, Map<Integer, List<Float>> pointsMap) {
    PhysicsRigidBody parentShape = parent;
    if (boneList.isEmpty() || boneList.contains(bone.getName())) {
        PhysicsBoneLink link = new PhysicsBoneLink();
        link.bone = bone;
        //creating the collision shape 
        HullCollisionShape shape = null;
        if (pointsMap != null) {
            //build a shape for the bone, using the vertices that are most influenced by this bone
            shape = RagdollUtils.makeShapeFromPointMap(pointsMap, RagdollUtils.getBoneIndices(link.bone, skeleton, boneList), initScale, link.bone.getModelSpacePosition());
        } else {
            //build a shape for the bone, using the vertices associated with this bone with a weight above the threshold
            shape = RagdollUtils.makeShapeFromVerticeWeights(model, RagdollUtils.getBoneIndices(link.bone, skeleton, boneList), initScale, link.bone.getModelSpacePosition(), weightThreshold);
        }
        PhysicsRigidBody shapeNode = new PhysicsRigidBody(shape, rootMass / (float) reccount);
        shapeNode.setKinematic(mode == Mode.Kinematic);
        totalMass += rootMass / (float) reccount;
        link.rigidBody = shapeNode;
        link.initalWorldRotation = bone.getModelSpaceRotation().clone();
        if (parent != null) {
            //get joint position for parent
            Vector3f posToParent = new Vector3f();
            if (bone.getParent() != null) {
                bone.getModelSpacePosition().subtract(bone.getParent().getModelSpacePosition(), posToParent).multLocal(initScale);
            }
            SixDofJoint joint = new SixDofJoint(parent, shapeNode, posToParent, new Vector3f(0, 0, 0f), true);
            preset.setupJointForBone(bone.getName(), joint);
            link.joint = joint;
            joint.setCollisionBetweenLinkedBodys(false);
        }
        boneLinks.put(bone.getName(), link);
        shapeNode.setUserObject(link);
        parentShape = shapeNode;
    }
    for (Iterator<Bone> it = bone.getChildren().iterator(); it.hasNext(); ) {
        Bone childBone = it.next();
        boneRecursion(model, childBone, parentShape, reccount + 1, pointsMap);
    }
}
Also used : SixDofJoint(com.jme3.bullet.joints.SixDofJoint) Vector3f(com.jme3.math.Vector3f) Bone(com.jme3.animation.Bone) PhysicsRigidBody(com.jme3.bullet.objects.PhysicsRigidBody) HullCollisionShape(com.jme3.bullet.collision.shapes.HullCollisionShape)

Aggregations

Bone (com.jme3.animation.Bone)35 Vector3f (com.jme3.math.Vector3f)25 Quaternion (com.jme3.math.Quaternion)17 TempVars (com.jme3.util.TempVars)13 SixDofJoint (com.jme3.bullet.joints.SixDofJoint)10 FloatBuffer (java.nio.FloatBuffer)10 VertexBuffer (com.jme3.scene.VertexBuffer)8 HashMap (java.util.HashMap)8 Skeleton (com.jme3.animation.Skeleton)7 Matrix4f (com.jme3.math.Matrix4f)7 Transform (com.jme3.math.Transform)7 BoneContext (com.jme3.scene.plugins.blender.animations.BoneContext)7 ByteBuffer (java.nio.ByteBuffer)7 BoneTrack (com.jme3.animation.BoneTrack)6 Structure (com.jme3.scene.plugins.blender.file.Structure)6 ArrayList (java.util.ArrayList)6 AnimControl (com.jme3.animation.AnimControl)5 Spatial (com.jme3.scene.Spatial)5 Map (java.util.Map)5 Animation (com.jme3.animation.Animation)4