Search in sources :

Example 6 with CameraNode

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

the class ObjectHelper method toObject.

/**
     * This method reads the given structure and createn an object that
     * represents the data.
     * 
     * @param objectStructure
     *            the object's structure
     * @param blenderContext
     *            the blender context
     * @return blener's object representation or null if its type is excluded from loading
     * @throws BlenderFileException
     *             an exception is thrown when the given data is inapropriate
     */
public Object toObject(Structure objectStructure, BlenderContext blenderContext) throws BlenderFileException {
    Object loadedResult = blenderContext.getLoadedFeature(objectStructure.getOldMemoryAddress(), LoadedDataType.FEATURE);
    if (loadedResult != null) {
        return loadedResult;
    }
    LOGGER.fine("Loading blender object.");
    if ("ID".equals(objectStructure.getType())) {
        Node object = (Node) this.loadLibrary(objectStructure);
        if (object.getParent() != null) {
            LOGGER.log(Level.FINEST, "Detaching object {0}, loaded from external file, from its parent.", object);
            object.getParent().detachChild(object);
        }
        return object;
    }
    int type = ((Number) objectStructure.getFieldValue("type")).intValue();
    ObjectType objectType = ObjectType.valueOf(type);
    LOGGER.log(Level.FINE, "Type of the object: {0}.", objectType);
    int lay = ((Number) objectStructure.getFieldValue("lay")).intValue();
    if ((lay & blenderContext.getBlenderKey().getLayersToLoad()) == 0) {
        LOGGER.fine("The layer this object is located in is not included in loading.");
        return null;
    }
    blenderContext.pushParent(objectStructure);
    String name = objectStructure.getName();
    LOGGER.log(Level.FINE, "Loading obejct: {0}", name);
    int restrictflag = ((Number) objectStructure.getFieldValue("restrictflag")).intValue();
    boolean visible = (restrictflag & 0x01) != 0;
    Pointer pParent = (Pointer) objectStructure.getFieldValue("parent");
    Object parent = blenderContext.getLoadedFeature(pParent.getOldMemoryAddress(), LoadedDataType.FEATURE);
    if (parent == null && pParent.isNotNull()) {
        Structure parentStructure = pParent.fetchData().get(0);
        parent = this.toObject(parentStructure, blenderContext);
    }
    Transform t = this.getTransformation(objectStructure, blenderContext);
    LOGGER.log(Level.FINE, "Importing object of type: {0}", objectType);
    Node result = null;
    try {
        switch(objectType) {
            case LATTICE:
            case METABALL:
            case TEXT:
            case WAVE:
                LOGGER.log(Level.WARNING, "{0} type is not supported but the node will be returned in order to keep parent - child relationship.", objectType);
            case EMPTY:
            case ARMATURE:
                // need to use an empty node to properly create
                // parent-children relationships between nodes
                result = new Node(name);
                break;
            case MESH:
                result = new Node(name);
                MeshHelper meshHelper = blenderContext.getHelper(MeshHelper.class);
                Pointer pMesh = (Pointer) objectStructure.getFieldValue("data");
                List<Structure> meshesArray = pMesh.fetchData();
                TemporalMesh temporalMesh = meshHelper.toTemporalMesh(meshesArray.get(0), blenderContext);
                if (temporalMesh != null) {
                    result.attachChild(temporalMesh);
                }
                break;
            case SURF:
            case CURVE:
                result = new Node(name);
                Pointer pCurve = (Pointer) objectStructure.getFieldValue("data");
                if (pCurve.isNotNull()) {
                    CurvesHelper curvesHelper = blenderContext.getHelper(CurvesHelper.class);
                    Structure curveData = pCurve.fetchData().get(0);
                    TemporalMesh curvesTemporalMesh = curvesHelper.toCurve(curveData, blenderContext);
                    if (curvesTemporalMesh != null) {
                        result.attachChild(curvesTemporalMesh);
                    }
                }
                break;
            case LAMP:
                Pointer pLamp = (Pointer) objectStructure.getFieldValue("data");
                if (pLamp.isNotNull()) {
                    LightHelper lightHelper = blenderContext.getHelper(LightHelper.class);
                    List<Structure> lampsArray = pLamp.fetchData();
                    Light light = lightHelper.toLight(lampsArray.get(0), blenderContext);
                    if (light == null) {
                        // probably some light type is not supported, just create a node so that we can maintain child-parent relationship for nodes
                        result = new Node(name);
                    } else {
                        result = new LightNode(name, light);
                    }
                }
                break;
            case CAMERA:
                Pointer pCamera = (Pointer) objectStructure.getFieldValue("data");
                if (pCamera.isNotNull()) {
                    CameraHelper cameraHelper = blenderContext.getHelper(CameraHelper.class);
                    List<Structure> camerasArray = pCamera.fetchData();
                    Camera camera = cameraHelper.toCamera(camerasArray.get(0), blenderContext);
                    if (camera == null) {
                        // just create a node so that we can maintain child-parent relationship for nodes
                        result = new Node(name);
                    } else {
                        result = new CameraNode(name, camera);
                    }
                }
                break;
            default:
                LOGGER.log(Level.WARNING, "Unsupported object type: {0}", type);
        }
        if (result != null) {
            LOGGER.fine("Storing loaded feature in blender context and applying markers (those will be removed before the final result is released).");
            Long oma = objectStructure.getOldMemoryAddress();
            blenderContext.addLoadedFeatures(oma, LoadedDataType.STRUCTURE, objectStructure);
            blenderContext.addLoadedFeatures(oma, LoadedDataType.FEATURE, result);
            blenderContext.addMarker(OMA_MARKER, result, objectStructure.getOldMemoryAddress());
            if (objectType == ObjectType.ARMATURE) {
                blenderContext.addMarker(ARMATURE_NODE_MARKER, result, Boolean.TRUE);
            }
            result.setLocalTransform(t);
            result.setCullHint(visible ? CullHint.Always : CullHint.Inherit);
            if (parent instanceof Node) {
                ((Node) parent).attachChild(result);
            }
            LOGGER.fine("Reading and applying object's modifiers.");
            ModifierHelper modifierHelper = blenderContext.getHelper(ModifierHelper.class);
            Collection<Modifier> modifiers = modifierHelper.readModifiers(objectStructure, blenderContext);
            for (Modifier modifier : modifiers) {
                modifier.apply(result, blenderContext);
            }
            if (result.getChildren() != null && result.getChildren().size() > 0) {
                if (result.getChildren().size() == 1 && result.getChild(0) instanceof TemporalMesh) {
                    LOGGER.fine("Converting temporal mesh into jme geometries.");
                    ((TemporalMesh) result.getChild(0)).toGeometries();
                }
                LOGGER.fine("Applying proper scale to the geometries.");
                for (Spatial child : result.getChildren()) {
                    if (child instanceof Geometry) {
                        this.flipMeshIfRequired((Geometry) child, child.getWorldScale());
                    }
                }
            }
            // I prefer do compute bounding box here than read it from the file
            result.updateModelBound();
            LOGGER.fine("Applying animations to the object if such are defined.");
            AnimationHelper animationHelper = blenderContext.getHelper(AnimationHelper.class);
            animationHelper.applyAnimations(result, blenderContext.getBlenderKey().getAnimationMatchMethod());
            LOGGER.fine("Loading constraints connected with this object.");
            ConstraintHelper constraintHelper = blenderContext.getHelper(ConstraintHelper.class);
            constraintHelper.loadConstraints(objectStructure, blenderContext);
            LOGGER.fine("Loading custom properties.");
            if (blenderContext.getBlenderKey().isLoadObjectProperties()) {
                Properties properties = this.loadProperties(objectStructure, blenderContext);
                // each value and set it to Spatial
                if (properties != null && properties.getValue() != null) {
                    this.applyProperties(result, properties);
                }
            }
        }
    } finally {
        blenderContext.popParent();
    }
    return result;
}
Also used : LightNode(com.jme3.scene.LightNode) Node(com.jme3.scene.Node) CameraNode(com.jme3.scene.CameraNode) CameraNode(com.jme3.scene.CameraNode) Pointer(com.jme3.scene.plugins.blender.file.Pointer) ModifierHelper(com.jme3.scene.plugins.blender.modifiers.ModifierHelper) LightNode(com.jme3.scene.LightNode) Light(com.jme3.light.Light) Camera(com.jme3.renderer.Camera) Structure(com.jme3.scene.plugins.blender.file.Structure) Modifier(com.jme3.scene.plugins.blender.modifiers.Modifier) CameraHelper(com.jme3.scene.plugins.blender.cameras.CameraHelper) AnimationHelper(com.jme3.scene.plugins.blender.animations.AnimationHelper) ConstraintHelper(com.jme3.scene.plugins.blender.constraints.ConstraintHelper) CullHint(com.jme3.scene.Spatial.CullHint) TemporalMesh(com.jme3.scene.plugins.blender.meshes.TemporalMesh) Geometry(com.jme3.scene.Geometry) Spatial(com.jme3.scene.Spatial) CurvesHelper(com.jme3.scene.plugins.blender.curves.CurvesHelper) Transform(com.jme3.math.Transform) LightHelper(com.jme3.scene.plugins.blender.lights.LightHelper) MeshHelper(com.jme3.scene.plugins.blender.meshes.MeshHelper)

Example 7 with CameraNode

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

the class TestBetterCharacter method simpleInitApp.

@Override
public void simpleInitApp() {
    //setup keyboard mapping
    setupKeys();
    // activate physics
    bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);
    bulletAppState.setDebugEnabled(true);
    // init a physics test scene
    PhysicsTestHelper.createPhysicsTestWorldSoccer(rootNode, assetManager, bulletAppState.getPhysicsSpace());
    PhysicsTestHelper.createBallShooter(this, rootNode, bulletAppState.getPhysicsSpace());
    setupPlanet();
    // Create a node for the character model
    characterNode = new Node("character node");
    characterNode.setLocalTranslation(new Vector3f(4, 5, 2));
    // Add a character control to the node so we can add other things and
    // control the model rotation
    physicsCharacter = new BetterCharacterControl(0.3f, 2.5f, 8f);
    characterNode.addControl(physicsCharacter);
    getPhysicsSpace().add(physicsCharacter);
    // Load model, attach to character node
    Node model = (Node) assetManager.loadModel("Models/Jaime/Jaime.j3o");
    model.setLocalScale(1.50f);
    characterNode.attachChild(model);
    // Add character node to the rootNode
    rootNode.attachChild(characterNode);
    // Set forward camera node that follows the character, only used when
    // view is "locked"
    camNode = new CameraNode("CamNode", cam);
    camNode.setControlDir(ControlDirection.SpatialToCamera);
    camNode.setLocalTranslation(new Vector3f(0, 2, -6));
    Quaternion quat = new Quaternion();
    // These coordinates are local, the camNode is attached to the character node!
    quat.lookAt(Vector3f.UNIT_Z, Vector3f.UNIT_Y);
    camNode.setLocalRotation(quat);
    characterNode.attachChild(camNode);
    // Disable by default, can be enabled via keyboard shortcut
    camNode.setEnabled(false);
}
Also used : Quaternion(com.jme3.math.Quaternion) BulletAppState(com.jme3.bullet.BulletAppState) Node(com.jme3.scene.Node) CameraNode(com.jme3.scene.CameraNode) Vector3f(com.jme3.math.Vector3f) CameraNode(com.jme3.scene.CameraNode) BetterCharacterControl(com.jme3.bullet.control.BetterCharacterControl)

Example 8 with CameraNode

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

the class TestPhysicsCharacter method simpleInitApp.

@Override
public void simpleInitApp() {
    // activate physics
    bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);
    // init a physical test scene
    PhysicsTestHelper.createPhysicsTestWorldSoccer(rootNode, assetManager, bulletAppState.getPhysicsSpace());
    setupKeys();
    // Add a physics character to the world
    physicsCharacter = new CharacterControl(new CapsuleCollisionShape(0.5f, 1.8f), .1f);
    physicsCharacter.setPhysicsLocation(new Vector3f(0, 1, 0));
    characterNode = new Node("character node");
    Spatial model = assetManager.loadModel("Models/Sinbad/Sinbad.mesh.xml");
    model.scale(0.25f);
    characterNode.addControl(physicsCharacter);
    getPhysicsSpace().add(physicsCharacter);
    rootNode.attachChild(characterNode);
    characterNode.attachChild(model);
    // set forward camera node that follows the character
    camNode = new CameraNode("CamNode", cam);
    camNode.setControlDir(ControlDirection.SpatialToCamera);
    camNode.setLocalTranslation(new Vector3f(0, 1, -5));
    camNode.lookAt(model.getLocalTranslation(), Vector3f.UNIT_Y);
    characterNode.attachChild(camNode);
    //disable the default 1st-person flyCam (don't forget this!!)
    flyCam.setEnabled(false);
}
Also used : CharacterControl(com.jme3.bullet.control.CharacterControl) Spatial(com.jme3.scene.Spatial) BulletAppState(com.jme3.bullet.BulletAppState) Vector3f(com.jme3.math.Vector3f) Node(com.jme3.scene.Node) CameraNode(com.jme3.scene.CameraNode) CameraNode(com.jme3.scene.CameraNode) CapsuleCollisionShape(com.jme3.bullet.collision.shapes.CapsuleCollisionShape)

Example 9 with CameraNode

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

the class Cinematic method bindCamera.

/**
     * Binds a camera to this cinematic, tagged by a unique name. This methods
     * creates and returns a CameraNode for the cam and attach it to the scene.
     * The control direction is set to SpatialToCamera. This camera Node can
     * then be used in other events to handle the camera movements during the
     * playback
     *
     * @param cameraName the unique tag the camera should have
     * @param cam the scene camera.
     * @return the created CameraNode.
     */
public CameraNode bindCamera(String cameraName, Camera cam) {
    if (cameras.containsKey(cameraName)) {
        throw new IllegalArgumentException("Camera " + cameraName + " is already binded to this cinematic");
    }
    CameraNode node = new CameraNode(cameraName, cam);
    node.setControlDir(ControlDirection.SpatialToCamera);
    node.getControl(CameraControl.class).setEnabled(false);
    cameras.put(cameraName, node);
    scene.attachChild(node);
    return node;
}
Also used : CameraNode(com.jme3.scene.CameraNode) CameraControl(com.jme3.scene.control.CameraControl)

Example 10 with CameraNode

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

the class TestCameraMotionPath method simpleInitApp.

@Override
public void simpleInitApp() {
    createScene();
    cam.setLocation(new Vector3f(8.4399185f, 11.189463f, 14.267577f));
    camNode = new CameraNode("Motion cam", cam);
    camNode.setControlDir(ControlDirection.SpatialToCamera);
    camNode.setEnabled(false);
    path = new MotionPath();
    path.setCycle(true);
    path.addWayPoint(new Vector3f(20, 3, 0));
    path.addWayPoint(new Vector3f(0, 3, 20));
    path.addWayPoint(new Vector3f(-20, 3, 0));
    path.addWayPoint(new Vector3f(0, 3, -20));
    path.setCurveTension(0.83f);
    path.enableDebugShape(assetManager, rootNode);
    cameraMotionControl = new MotionEvent(camNode, path);
    cameraMotionControl.setLoopMode(LoopMode.Loop);
    //cameraMotionControl.setDuration(15f);
    cameraMotionControl.setLookAt(teapot.getWorldTranslation(), Vector3f.UNIT_Y);
    cameraMotionControl.setDirectionType(MotionEvent.Direction.LookAt);
    rootNode.attachChild(camNode);
    guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
    final BitmapText wayPointsText = new BitmapText(guiFont, false);
    wayPointsText.setSize(guiFont.getCharSet().getRenderedSize());
    guiNode.attachChild(wayPointsText);
    path.addListener(new MotionPathListener() {

        public void onWayPointReach(MotionEvent control, int wayPointIndex) {
            if (path.getNbWayPoints() == wayPointIndex + 1) {
                wayPointsText.setText(control.getSpatial().getName() + " Finish!!! ");
            } else {
                wayPointsText.setText(control.getSpatial().getName() + " Reached way point " + wayPointIndex);
            }
            wayPointsText.setLocalTranslation((cam.getWidth() - wayPointsText.getLineWidth()) / 2, cam.getHeight(), 0);
        }
    });
    flyCam.setEnabled(false);
    chaser = new ChaseCamera(cam, teapot);
    chaser.registerWithInput(inputManager);
    chaser.setSmoothMotion(true);
    chaser.setMaxDistance(50);
    chaser.setDefaultDistance(50);
    initInputs();
}
Also used : MotionPathListener(com.jme3.cinematic.MotionPathListener) BitmapText(com.jme3.font.BitmapText) Vector3f(com.jme3.math.Vector3f) CameraNode(com.jme3.scene.CameraNode) ChaseCamera(com.jme3.input.ChaseCamera) MotionPath(com.jme3.cinematic.MotionPath) MotionEvent(com.jme3.cinematic.events.MotionEvent)

Aggregations

CameraNode (com.jme3.scene.CameraNode)10 Vector3f (com.jme3.math.Vector3f)7 Node (com.jme3.scene.Node)5 MotionPath (com.jme3.cinematic.MotionPath)3 Quaternion (com.jme3.math.Quaternion)3 BulletAppState (com.jme3.bullet.BulletAppState)2 MotionEvent (com.jme3.cinematic.events.MotionEvent)2 Light (com.jme3.light.Light)2 Camera (com.jme3.renderer.Camera)2 Geometry (com.jme3.scene.Geometry)2 LightNode (com.jme3.scene.LightNode)2 Spatial (com.jme3.scene.Spatial)2 AnimationHelper (com.jme3.scene.plugins.blender.animations.AnimationHelper)2 ConstraintHelper (com.jme3.scene.plugins.blender.constraints.ConstraintHelper)2 Structure (com.jme3.scene.plugins.blender.file.Structure)2 MeshHelper (com.jme3.scene.plugins.blender.meshes.MeshHelper)2 TemporalMesh (com.jme3.scene.plugins.blender.meshes.TemporalMesh)2 AnimControl (com.jme3.animation.AnimControl)1 AnimationFactory (com.jme3.animation.AnimationFactory)1 BlenderKey (com.jme3.asset.BlenderKey)1