Search in sources :

Example 71 with Spatial

use of com.jme3.scene.Spatial in project jmonkeyengine by jMonkeyEngine.

the class ConstraintHelper method applyTransform.

/**
     * Applies transform to a feature (bone or spatial). Computations transform
     * the given transformation from the given space to the feature's local
     * space.
     * 
     * @param oma
     *            the OMA of the feature we apply transformation to
     * @param subtargetName
     *            the name of the feature's subtarget (bone in case of armature)
     * @param space
     *            the space in which the given transform is to be applied
     * @param transform
     *            the transform we apply
     */
public void applyTransform(Long oma, String subtargetName, Space space, Transform transform) {
    Spatial feature = (Spatial) blenderContext.getLoadedFeature(oma, LoadedDataType.FEATURE);
    boolean isArmature = blenderContext.getMarkerValue(ObjectHelper.ARMATURE_NODE_MARKER, feature) != null;
    if (isArmature) {
        Skeleton skeleton = blenderContext.getSkeleton(oma);
        BoneContext targetBoneContext = blenderContext.getBoneByName(oma, subtargetName);
        Bone bone = targetBoneContext.getBone();
        if (bone.getParent() == null && (space == Space.CONSTRAINT_SPACE_LOCAL || space == Space.CONSTRAINT_SPACE_PARLOCAL)) {
            space = Space.CONSTRAINT_SPACE_POSE;
        }
        TempVars tempVars = TempVars.get();
        switch(space) {
            case CONSTRAINT_SPACE_LOCAL:
                assert bone.getParent() != null : "CONSTRAINT_SPACE_LOCAL should be evaluated as CONSTRAINT_SPACE_POSE if the bone has no parent!";
                bone.setBindTransforms(transform.getTranslation(), transform.getRotation(), transform.getScale());
                break;
            case CONSTRAINT_SPACE_WORLD:
                {
                    Matrix4f boneMatrixInWorldSpace = this.toMatrix(transform, tempVars.tempMat4);
                    Matrix4f modelWorldMatrix = this.toMatrix(this.getTransform(targetBoneContext.getSkeletonOwnerOma(), null, Space.CONSTRAINT_SPACE_WORLD), tempVars.tempMat42);
                    Matrix4f boneMatrixInModelSpace = modelWorldMatrix.invertLocal().multLocal(boneMatrixInWorldSpace);
                    Bone parent = bone.getParent();
                    if (parent != null) {
                        Matrix4f parentMatrixInModelSpace = this.toMatrix(parent.getModelSpacePosition(), parent.getModelSpaceRotation(), parent.getModelSpaceScale(), tempVars.tempMat4);
                        boneMatrixInModelSpace = parentMatrixInModelSpace.invertLocal().multLocal(boneMatrixInModelSpace);
                    }
                    bone.setBindTransforms(boneMatrixInModelSpace.toTranslationVector(), boneMatrixInModelSpace.toRotationQuat(), boneMatrixInModelSpace.toScaleVector());
                    break;
                }
            case CONSTRAINT_SPACE_POSE:
                {
                    Matrix4f armatureWorldMatrix = this.toMatrix(feature.getWorldTransform(), tempVars.tempMat4);
                    Matrix4f boneMatrixInWorldSpace = armatureWorldMatrix.multLocal(this.toMatrix(transform, tempVars.tempMat42));
                    Matrix4f invertedModelMatrix = this.toMatrix(this.getTransform(targetBoneContext.getSkeletonOwnerOma(), null, Space.CONSTRAINT_SPACE_WORLD), tempVars.tempMat42).invertLocal();
                    Matrix4f boneMatrixInModelSpace = invertedModelMatrix.multLocal(boneMatrixInWorldSpace);
                    Bone parent = bone.getParent();
                    if (parent != null) {
                        Matrix4f parentMatrixInModelSpace = this.toMatrix(parent.getModelSpacePosition(), parent.getModelSpaceRotation(), parent.getModelSpaceScale(), tempVars.tempMat4);
                        boneMatrixInModelSpace = parentMatrixInModelSpace.invertLocal().multLocal(boneMatrixInModelSpace);
                    }
                    bone.setBindTransforms(boneMatrixInModelSpace.toTranslationVector(), boneMatrixInModelSpace.toRotationQuat(), boneMatrixInModelSpace.toScaleVector());
                    break;
                }
            case CONSTRAINT_SPACE_PARLOCAL:
                Matrix4f armatureWorldMatrix = this.toMatrix(feature.getWorldTransform(), tempVars.tempMat4);
                Matrix4f boneMatrixInWorldSpace = armatureWorldMatrix.multLocal(this.toMatrix(transform, tempVars.tempMat42));
                Matrix4f invertedModelMatrix = this.toMatrix(this.getTransform(targetBoneContext.getSkeletonOwnerOma(), null, Space.CONSTRAINT_SPACE_WORLD), tempVars.tempMat42).invertLocal();
                Matrix4f boneMatrixInModelSpace = invertedModelMatrix.multLocal(boneMatrixInWorldSpace);
                Bone parent = bone.getParent();
                if (parent != null) {
                    //first add the initial parent matrix to the bone's model matrix
                    BoneContext parentContext = blenderContext.getBoneContext(parent);
                    Matrix4f initialParentMatrixInModelSpace = parentContext.getBoneMatrixInModelSpace();
                    Matrix4f currentParentMatrixInModelSpace = this.toMatrix(parent.getModelSpacePosition(), parent.getModelSpaceRotation(), parent.getModelSpaceScale(), tempVars.tempMat4);
                    //the bone will now move with its parent in model space
                    //now we need to subtract the difference between current parent's model matrix and its initial model matrix
                    boneMatrixInModelSpace = initialParentMatrixInModelSpace.mult(boneMatrixInModelSpace);
                    Matrix4f diffMatrix = initialParentMatrixInModelSpace.mult(currentParentMatrixInModelSpace.invert());
                    boneMatrixInModelSpace.multLocal(diffMatrix);
                //now the bone will have its position in model space with initial parent's model matrix added
                }
                bone.setBindTransforms(boneMatrixInModelSpace.toTranslationVector(), boneMatrixInModelSpace.toRotationQuat(), boneMatrixInModelSpace.toScaleVector());
                break;
            default:
                tempVars.release();
                throw new IllegalStateException("Invalid space type for target object: " + space.toString());
        }
        tempVars.release();
        skeleton.updateWorldVectors();
    } else {
        switch(space) {
            case CONSTRAINT_SPACE_LOCAL:
                feature.getLocalTransform().set(transform);
                break;
            case CONSTRAINT_SPACE_WORLD:
                if (feature.getParent() == null) {
                    feature.setLocalTransform(transform);
                } else {
                    Transform parentWorldTransform = feature.getParent().getWorldTransform();
                    TempVars tempVars = TempVars.get();
                    Matrix4f parentInverseMatrix = this.toMatrix(parentWorldTransform, tempVars.tempMat4).invertLocal();
                    Matrix4f m = this.toMatrix(transform, tempVars.tempMat42);
                    m = m.multLocal(parentInverseMatrix);
                    tempVars.release();
                    transform.setTranslation(m.toTranslationVector());
                    transform.setRotation(m.toRotationQuat());
                    transform.setScale(m.toScaleVector());
                    feature.setLocalTransform(transform);
                }
                break;
            default:
                throw new IllegalStateException("Invalid space type for spatial object: " + space.toString());
        }
    }
}
Also used : Matrix4f(com.jme3.math.Matrix4f) Spatial(com.jme3.scene.Spatial) BoneContext(com.jme3.scene.plugins.blender.animations.BoneContext) Skeleton(com.jme3.animation.Skeleton) Bone(com.jme3.animation.Bone) TempVars(com.jme3.util.TempVars) Transform(com.jme3.math.Transform)

Example 72 with Spatial

use of com.jme3.scene.Spatial in project jmonkeyengine by jMonkeyEngine.

the class ConstraintHelper method bakeConstraints.

/**
     * The method bakes all available and valid constraints.
     * 
     * @param blenderContext
     *            the blender context
     */
public void bakeConstraints(BlenderContext blenderContext) {
    Set<Long> owners = new HashSet<Long>();
    for (Constraint constraint : blenderContext.getAllConstraints()) {
        if (constraint instanceof BoneConstraint) {
            BoneContext boneContext = blenderContext.getBoneContext(constraint.ownerOMA);
            owners.add(boneContext.getArmatureObjectOMA());
        } else {
            Spatial spatial = (Spatial) blenderContext.getLoadedFeature(constraint.ownerOMA, LoadedDataType.FEATURE);
            while (spatial.getParent() != null) {
                spatial = spatial.getParent();
            }
            owners.add((Long) blenderContext.getMarkerValue(ObjectHelper.OMA_MARKER, spatial));
        }
    }
    List<SimulationNode> simulationRootNodes = new ArrayList<SimulationNode>(owners.size());
    for (Long ownerOMA : owners) {
        simulationRootNodes.add(new SimulationNode(ownerOMA, blenderContext));
    }
    for (SimulationNode node : simulationRootNodes) {
        node.simulate();
    }
}
Also used : Spatial(com.jme3.scene.Spatial) BoneContext(com.jme3.scene.plugins.blender.animations.BoneContext) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 73 with Spatial

use of com.jme3.scene.Spatial in project jmonkeyengine by jMonkeyEngine.

the class SimulationNode method simulateSpatial.

/**
     * Simulates the spatial node.
     */
private void simulateSpatial() {
    List<Constraint> constraints = blenderContext.getConstraints(featureOMA);
    if (constraints != null && constraints.size() > 0) {
        LOGGER.fine("Simulating spatial.");
        boolean applyStaticConstraints = true;
        if (animations != null) {
            for (Animation animation : animations) {
                float[] animationTimeBoundaries = this.computeAnimationTimeBoundaries(animation);
                int maxFrame = (int) animationTimeBoundaries[0];
                float maxTime = animationTimeBoundaries[1];
                VirtualTrack vTrack = new VirtualTrack(spatial.getName(), maxFrame, maxTime);
                for (Track track : animation.getTracks()) {
                    for (int frame = 0; frame < maxFrame; ++frame) {
                        spatial.setLocalTranslation(((SpatialTrack) track).getTranslations()[frame]);
                        spatial.setLocalRotation(((SpatialTrack) track).getRotations()[frame]);
                        spatial.setLocalScale(((SpatialTrack) track).getScales()[frame]);
                        for (Constraint constraint : constraints) {
                            constraint.apply(frame);
                            vTrack.setTransform(frame, spatial.getLocalTransform());
                        }
                    }
                    Track newTrack = vTrack.getAsSpatialTrack();
                    if (newTrack != null) {
                        animation.removeTrack(track);
                        animation.addTrack(newTrack);
                    }
                    applyStaticConstraints = false;
                }
            }
        }
        // object's transformation
        if (applyStaticConstraints) {
            for (Constraint constraint : constraints) {
                constraint.apply(0);
            }
        }
    }
    for (SimulationNode child : children) {
        child.simulate();
    }
}
Also used : SpatialTrack(com.jme3.animation.SpatialTrack) Animation(com.jme3.animation.Animation) SpatialTrack(com.jme3.animation.SpatialTrack) BoneTrack(com.jme3.animation.BoneTrack) Track(com.jme3.animation.Track)

Example 74 with Spatial

use of com.jme3.scene.Spatial in project jmonkeyengine by jMonkeyEngine.

the class AbstractBlenderHelper method loadLibrary.

/**
     * The method loads library of a given ID from linked blender file.
     * @param id
     *            the ID of the linked feature (it contains its name and blender path)
     * @return loaded feature or null if none was found
     * @throws BlenderFileException
     *             and exception is throw when problems with reading a blend file occur
     */
protected Object loadLibrary(Structure id) throws BlenderFileException {
    Pointer pLib = (Pointer) id.getFieldValue("lib");
    if (pLib.isNotNull()) {
        // we need full name with the prefix
        String fullName = id.getFieldValue("name").toString();
        String nameOfFeatureToLoad = id.getName();
        Structure library = pLib.fetchData().get(0);
        String path = library.getFieldValue("filepath").toString();
        if (!blenderContext.getLinkedFeatures().keySet().contains(path)) {
            Spatial loadedAsset = null;
            BlenderKey blenderKey = new BlenderKey(path);
            blenderKey.setLoadUnlinkedAssets(true);
            try {
                loadedAsset = blenderContext.getAssetManager().loadAsset(blenderKey);
            } catch (AssetNotFoundException e) {
                LOGGER.log(Level.FINEST, "Cannot locate linked resource at path: {0}.", path);
            }
            if (loadedAsset != null) {
                Map<String, Map<String, Object>> linkedData = loadedAsset.getUserData("linkedData");
                for (Entry<String, Map<String, Object>> entry : linkedData.entrySet()) {
                    String linkedDataFilePath = "this".equals(entry.getKey()) ? path : entry.getKey();
                    blenderContext.getLinkedFeatures().put(linkedDataFilePath, entry.getValue());
                }
            } else {
                LOGGER.log(Level.WARNING, "No features loaded from path: {0}.", path);
            }
        }
        Object result = blenderContext.getLinkedFeature(path, fullName);
        if (result == null) {
            LOGGER.log(Level.WARNING, "Could NOT find asset named {0} in the library of path: {1}.", new Object[] { nameOfFeatureToLoad, path });
        } else {
            blenderContext.addLoadedFeatures(id.getOldMemoryAddress(), LoadedDataType.STRUCTURE, id);
            blenderContext.addLoadedFeatures(id.getOldMemoryAddress(), LoadedDataType.FEATURE, result);
        }
        return result;
    } else {
        LOGGER.warning("Library link points to nothing!");
    }
    return null;
}
Also used : Spatial(com.jme3.scene.Spatial) Pointer(com.jme3.scene.plugins.blender.file.Pointer) BlenderKey(com.jme3.asset.BlenderKey) AssetNotFoundException(com.jme3.asset.AssetNotFoundException) Structure(com.jme3.scene.plugins.blender.file.Structure) Map(java.util.Map)

Example 75 with Spatial

use of com.jme3.scene.Spatial in project jmonkeyengine by jMonkeyEngine.

the class TestFancyCar method findGeom.

//    public void setupFloor() {
//        Material mat = assetManager.loadMaterial("Textures/Terrain/BrickWall/BrickWall.j3m");
//        mat.getTextureParam("DiffuseMap").getTextureValue().setWrap(WrapMode.Repeat);
////        mat.getTextureParam("NormalMap").getTextureValue().setWrap(WrapMode.Repeat);
////        mat.getTextureParam("ParallaxMap").getTextureValue().setWrap(WrapMode.Repeat);
//
//        Box floor = new Box(Vector3f.ZERO, 140, 1f, 140);
//        floor.scaleTextureCoordinates(new Vector2f(112.0f, 112.0f));
//        Geometry floorGeom = new Geometry("Floor", floor);
//        floorGeom.setShadowMode(ShadowMode.Receive);
//        floorGeom.setMaterial(mat);
//
//        PhysicsNode tb = new PhysicsNode(floorGeom, new MeshCollisionShape(floorGeom.getMesh()), 0);
//        tb.setLocalTranslation(new Vector3f(0f, -6, 0f));
////        tb.attachDebugShape(assetManager);
//        rootNode.attachChild(tb);
//        getPhysicsSpace().add(tb);
//    }
private Geometry findGeom(Spatial spatial, String name) {
    if (spatial instanceof Node) {
        Node node = (Node) spatial;
        for (int i = 0; i < node.getQuantity(); i++) {
            Spatial child = node.getChild(i);
            Geometry result = findGeom(child, name);
            if (result != null) {
                return result;
            }
        }
    } else if (spatial instanceof Geometry) {
        if (spatial.getName().startsWith(name)) {
            return (Geometry) spatial;
        }
    }
    return null;
}
Also used : Geometry(com.jme3.scene.Geometry) Spatial(com.jme3.scene.Spatial) Node(com.jme3.scene.Node)

Aggregations

Spatial (com.jme3.scene.Spatial)123 Vector3f (com.jme3.math.Vector3f)74 Node (com.jme3.scene.Node)56 Geometry (com.jme3.scene.Geometry)50 DirectionalLight (com.jme3.light.DirectionalLight)46 Material (com.jme3.material.Material)39 Quaternion (com.jme3.math.Quaternion)34 FilterPostProcessor (com.jme3.post.FilterPostProcessor)17 Box (com.jme3.scene.shape.Box)17 KeyTrigger (com.jme3.input.controls.KeyTrigger)15 AmbientLight (com.jme3.light.AmbientLight)14 ColorRGBA (com.jme3.math.ColorRGBA)14 Camera (com.jme3.renderer.Camera)13 Sphere (com.jme3.scene.shape.Sphere)13 Texture (com.jme3.texture.Texture)12 TempVars (com.jme3.util.TempVars)12 InputCapsule (com.jme3.export.InputCapsule)11 ActionListener (com.jme3.input.controls.ActionListener)11 AnimControl (com.jme3.animation.AnimControl)10 OutputCapsule (com.jme3.export.OutputCapsule)9