Search in sources :

Example 6 with LightNode

use of com.jme3.scene.LightNode 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 LightNode

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

the class TestManyLightsSingle method simpleInitApp.

@Override
public void simpleInitApp() {
    renderManager.setPreferredLightMode(lm);
    renderManager.setSinglePassLightBatchSize(6);
    flyCam.setMoveSpeed(10);
    Node scene = (Node) assetManager.loadModel("Scenes/ManyLights/Main.scene");
    rootNode.attachChild(scene);
    Node n = (Node) rootNode.getChild(0);
    final LightList lightList = n.getWorldLightList();
    final Geometry g = (Geometry) n.getChild("Grid-geom-1");
    g.getMaterial().setColor("Ambient", new ColorRGBA(0.2f, 0.2f, 0.2f, 1f));
    /* A colored lit cube. Needs light source! */
    Box boxMesh = new Box(1f, 1f, 1f);
    final Geometry boxGeo = new Geometry("Colored Box", boxMesh);
    Material boxMat = g.getMaterial().clone();
    boxMat.clearParam("DiffuseMap");
    boxMat.setBoolean("UseMaterialColors", true);
    boxMat.setColor("Ambient", new ColorRGBA(0.2f, 0.2f, 0.2f, 1f));
    boxMat.setColor("Diffuse", ColorRGBA.Blue);
    boxGeo.setMaterial(boxMat);
    final Node cubeNodes = new Node();
    n.attachChild(cubeNodes);
    int nb = 0;
    for (Light light : lightList) {
        nb++;
        PointLight p = (PointLight) light;
        if (nb > 60) {
            n.removeLight(light);
        } else {
            LightNode ln = new LightNode("l", light);
            n.attachChild(ln);
            ln.setLocalTranslation(p.getPosition());
            int rand = FastMath.nextRandomInt(0, 3);
            switch(rand) {
                case 0:
                    light.setColor(ColorRGBA.Red);
                    //   ln.addControl(new MoveControl(5f));
                    break;
                case 1:
                    light.setColor(ColorRGBA.Yellow);
                    //    ln.addControl(new MoveControl(5f));
                    break;
                case 2:
                    light.setColor(ColorRGBA.Green);
                    //ln.addControl(new MoveControl(-5f));
                    break;
                case 3:
                    light.setColor(ColorRGBA.Orange);
                    //ln.addControl(new MoveControl(-5f));
                    break;
            }
        }
        Geometry b = boxGeo.clone(false);
        cubeNodes.attachChild(b);
        b.setLocalTranslation(p.getPosition().x, 2, p.getPosition().z);
    }
    //        cam.setLocation(new Vector3f(3.1893547f, 17.977385f, 30.8378f));
    //        cam.setRotation(new Quaternion(0.14317635f, 0.82302624f, -0.23777823f, 0.49557027f));
    cam.setLocation(new Vector3f(-1.8901939f, 29.34097f, 73.07533f));
    cam.setRotation(new Quaternion(0.0021000702f, 0.971012f, -0.23886925f, 0.008527749f));
    BasicProfilerState profiler = new BasicProfilerState(true);
    profiler.setGraphScale(1000f);
    //  getStateManager().attach(profiler);
    //        guiNode.setCullHint(CullHint.Always);
    flyCam.setDragToRotate(true);
    flyCam.setMoveSpeed(50);
    final MaterialDebugAppState debug = new MaterialDebugAppState();
    stateManager.attach(debug);
    inputManager.addListener(new ActionListener() {

        public void onAction(String name, boolean isPressed, float tpf) {
            if (name.equals("toggle") && isPressed) {
                if (lm == TechniqueDef.LightMode.SinglePass) {
                    lm = TechniqueDef.LightMode.MultiPass;
                    helloText.setText("(Multi pass)");
                } else {
                    lm = TechniqueDef.LightMode.SinglePass;
                    helloText.setText("(Single pass) nb lights per batch : " + renderManager.getSinglePassLightBatchSize());
                }
                renderManager.setPreferredLightMode(lm);
                reloadScene(g, boxGeo, cubeNodes);
            }
            if (name.equals("lightsUp") && isPressed) {
                renderManager.setSinglePassLightBatchSize(renderManager.getSinglePassLightBatchSize() + 1);
                helloText.setText("(Single pass) nb lights per batch : " + renderManager.getSinglePassLightBatchSize());
            }
            if (name.equals("lightsDown") && isPressed) {
                renderManager.setSinglePassLightBatchSize(renderManager.getSinglePassLightBatchSize() - 1);
                helloText.setText("(Single pass) nb lights per batch : " + renderManager.getSinglePassLightBatchSize());
            }
            if (name.equals("toggleOnOff") && isPressed) {
                for (final Light light : lightList) {
                    if (light instanceof AmbientLight) {
                        continue;
                    }
                    light.setEnabled(!light.isEnabled());
                }
            }
        }
    }, "toggle", "lightsUp", "lightsDown", "toggleOnOff");
    inputManager.addMapping("toggle", new KeyTrigger(KeyInput.KEY_SPACE));
    inputManager.addMapping("lightsUp", new KeyTrigger(KeyInput.KEY_UP));
    inputManager.addMapping("lightsDown", new KeyTrigger(KeyInput.KEY_DOWN));
    inputManager.addMapping("toggleOnOff", new KeyTrigger(KeyInput.KEY_L));
    SpotLight spot = new SpotLight();
    spot.setDirection(new Vector3f(-1f, -1f, -1f).normalizeLocal());
    spot.setColor(ColorRGBA.Blue.mult(5));
    spot.setSpotOuterAngle(FastMath.DEG_TO_RAD * 20);
    spot.setSpotInnerAngle(FastMath.DEG_TO_RAD * 5);
    spot.setPosition(new Vector3f(10, 10, 20));
    rootNode.addLight(spot);
    DirectionalLight dl = new DirectionalLight();
    dl.setDirection(new Vector3f(-1, -1, 1));
    rootNode.addLight(dl);
    AmbientLight al = new AmbientLight();
    al.setColor(new ColorRGBA(0.2f, 0.2f, 0.2f, 1f));
    rootNode.addLight(al);
    /**
         * Write text on the screen (HUD)
         */
    guiNode.detachAllChildren();
    guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
    helloText = new BitmapText(guiFont, false);
    helloText.setSize(guiFont.getCharSet().getRenderedSize());
    helloText.setText("(Single pass) nb lights per batch : " + renderManager.getSinglePassLightBatchSize());
    helloText.setLocalTranslation(300, helloText.getLineHeight(), 0);
    guiNode.attachChild(helloText);
}
Also used : Quaternion(com.jme3.math.Quaternion) MaterialDebugAppState(com.jme3.util.MaterialDebugAppState) LightNode(com.jme3.scene.LightNode) Node(com.jme3.scene.Node) KeyTrigger(com.jme3.input.controls.KeyTrigger) Box(com.jme3.scene.shape.Box) Material(com.jme3.material.Material) SpotLight(com.jme3.light.SpotLight) BasicProfilerState(com.jme3.app.BasicProfilerState) Geometry(com.jme3.scene.Geometry) ColorRGBA(com.jme3.math.ColorRGBA) ActionListener(com.jme3.input.controls.ActionListener) BitmapText(com.jme3.font.BitmapText) LightNode(com.jme3.scene.LightNode) DirectionalLight(com.jme3.light.DirectionalLight) SpotLight(com.jme3.light.SpotLight) PointLight(com.jme3.light.PointLight) Light(com.jme3.light.Light) AmbientLight(com.jme3.light.AmbientLight) Vector3f(com.jme3.math.Vector3f) DirectionalLight(com.jme3.light.DirectionalLight) LightList(com.jme3.light.LightList) PointLight(com.jme3.light.PointLight) AmbientLight(com.jme3.light.AmbientLight)

Example 8 with LightNode

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

the class TestPointDirectionalAndSpotLightShadows method simpleInitApp.

@Override
public void simpleInitApp() {
    flyCam.setMoveSpeed(10);
    cam.setLocation(new Vector3f(0.040581334f, 1.7745866f, 6.155161f));
    cam.setRotation(new Quaternion(4.3868728E-5f, 0.9999293f, -0.011230096f, 0.0039059948f));
    Node scene = (Node) assetManager.loadModel("Models/Test/CornellBox.j3o");
    scene.setShadowMode(RenderQueue.ShadowMode.CastAndReceive);
    rootNode.attachChild(scene);
    rootNode.getChild("Cube").setShadowMode(RenderQueue.ShadowMode.Receive);
    lightNode = (Node) rootNode.getChild("Lamp");
    Geometry lightMdl = new Geometry("Light", new Sphere(10, 10, 0.1f));
    //Geometry  lightMdl = new Geometry("Light", new Box(.1f,.1f,.1f));
    lightMdl.setMaterial(assetManager.loadMaterial("Common/Materials/RedColor.j3m"));
    lightMdl.setShadowMode(RenderQueue.ShadowMode.Off);
    lightNode.attachChild(lightMdl);
    //lightMdl.setLocalTranslation(lightNode.getLocalTranslation());
    Geometry box = new Geometry("box", new Box(0.2f, 0.2f, 0.2f));
    //Geometry  lightMdl = new Geometry("Light", new Box(.1f,.1f,.1f));
    box.setMaterial(assetManager.loadMaterial("Common/Materials/RedColor.j3m"));
    box.setShadowMode(RenderQueue.ShadowMode.CastAndReceive);
    rootNode.attachChild(box);
    box.setLocalTranslation(-1f, 0.5f, -2);
    ((PointLight) scene.getLocalLightList().get(0)).setColor(ColorRGBA.Red);
    plsr = new PointLightShadowRenderer(assetManager, SHADOWMAP_SIZE);
    plsr.setLight((PointLight) scene.getLocalLightList().get(0));
    plsr.setEdgeFilteringMode(EdgeFilteringMode.PCF4);
    plsf = new PointLightShadowFilter(assetManager, SHADOWMAP_SIZE);
    plsf.setLight((PointLight) scene.getLocalLightList().get(0));
    plsf.setEdgeFilteringMode(EdgeFilteringMode.PCF4);
    plsf.setEnabled(useFilter);
    //DIRECTIONAL LIGHT
    DirectionalLight directionalLight = new DirectionalLight();
    rootNode.addLight(directionalLight);
    directionalLight.setColor(ColorRGBA.Blue);
    directionalLight.setDirection(new Vector3f(-1f, -.2f, 0f));
    dlsr = new DirectionalLightShadowRenderer(assetManager, SHADOWMAP_SIZE * 2, 4);
    dlsr.setLight(directionalLight);
    dlsr.setEdgeFilteringMode(EdgeFilteringMode.PCF4);
    dlsf = new DirectionalLightShadowFilter(assetManager, SHADOWMAP_SIZE * 2, 4);
    dlsf.setEdgeFilteringMode(EdgeFilteringMode.PCF4);
    dlsf.setLight(directionalLight);
    dlsf.setEnabled(useFilter);
    //SPOT LIGHT
    spotLight = new SpotLight();
    spotLight.setDirection(new Vector3f(1f, -1f, 0f));
    spotLight.setPosition(new Vector3f(-1f, 3f, 0f));
    spotLight.setSpotOuterAngle(0.5f);
    spotLight.setColor(ColorRGBA.Green);
    Sphere sphere = new Sphere(8, 8, .1f);
    Geometry sphereGeometry = new Geometry("Sphere", sphere);
    sphereGeometry.setLocalTranslation(-1f, 3f, 0f);
    sphereGeometry.setMaterial(assetManager.loadMaterial("Common/Materials/WhiteColor.j3m"));
    rootNode.attachChild(sphereGeometry);
    rootNode.addLight(spotLight);
    slsr = new SpotLightShadowRenderer(assetManager, SHADOWMAP_SIZE);
    slsr.setLight(spotLight);
    slsr.setEdgeFilteringMode(EdgeFilteringMode.PCF4);
    slsf = new SpotLightShadowFilter(assetManager, SHADOWMAP_SIZE);
    slsf.setLight(spotLight);
    slsf.setEdgeFilteringMode(EdgeFilteringMode.PCF4);
    slsf.setEnabled(useFilter);
    if (!useFilter)
        viewPort.addProcessor(slsr);
    if (!useFilter)
        viewPort.addProcessor(plsr);
    if (!useFilter)
        viewPort.addProcessor(dlsr);
    FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
    fpp.addFilter(plsf);
    fpp.addFilter(dlsf);
    fpp.addFilter(slsf);
    viewPort.addProcessor(fpp);
    ShadowTestUIManager uiMan = new ShadowTestUIManager(assetManager, plsr, plsf, guiNode, inputManager, viewPort);
    ShadowTestUIManager uiManPls = new ShadowTestUIManager(assetManager, plsr, plsf, guiNode, inputManager, viewPort);
    ShadowTestUIManager uiManDls = new ShadowTestUIManager(assetManager, dlsr, dlsf, guiNode, inputManager, viewPort);
    ShadowTestUIManager uiManSls = new ShadowTestUIManager(assetManager, slsr, slsf, guiNode, inputManager, viewPort);
}
Also used : SpotLightShadowFilter(com.jme3.shadow.SpotLightShadowFilter) Quaternion(com.jme3.math.Quaternion) Node(com.jme3.scene.Node) PointLightShadowFilter(com.jme3.shadow.PointLightShadowFilter) Box(com.jme3.scene.shape.Box) PointLightShadowRenderer(com.jme3.shadow.PointLightShadowRenderer) FilterPostProcessor(com.jme3.post.FilterPostProcessor) DirectionalLightShadowFilter(com.jme3.shadow.DirectionalLightShadowFilter) SpotLightShadowRenderer(com.jme3.shadow.SpotLightShadowRenderer) SpotLight(com.jme3.light.SpotLight) Geometry(com.jme3.scene.Geometry) Sphere(com.jme3.scene.shape.Sphere) Vector3f(com.jme3.math.Vector3f) DirectionalLight(com.jme3.light.DirectionalLight) DirectionalLightShadowRenderer(com.jme3.shadow.DirectionalLightShadowRenderer) PointLight(com.jme3.light.PointLight)

Aggregations

Node (com.jme3.scene.Node)8 Geometry (com.jme3.scene.Geometry)6 LightNode (com.jme3.scene.LightNode)6 DirectionalLight (com.jme3.light.DirectionalLight)4 Vector3f (com.jme3.math.Vector3f)4 Box (com.jme3.scene.shape.Box)4 AmbientLight (com.jme3.light.AmbientLight)3 Light (com.jme3.light.Light)3 PointLight (com.jme3.light.PointLight)3 SpotLight (com.jme3.light.SpotLight)3 Material (com.jme3.material.Material)3 Quaternion (com.jme3.math.Quaternion)3 CameraNode (com.jme3.scene.CameraNode)3 Spatial (com.jme3.scene.Spatial)3 Structure (com.jme3.scene.plugins.blender.file.Structure)3 Sphere (com.jme3.scene.shape.Sphere)3 ActionListener (com.jme3.input.controls.ActionListener)2 KeyTrigger (com.jme3.input.controls.KeyTrigger)2 FilterPostProcessor (com.jme3.post.FilterPostProcessor)2 AnimationHelper (com.jme3.scene.plugins.blender.animations.AnimationHelper)2