Search in sources :

Example 11 with SpotLight

use of com.jme3.light.SpotLight in project jmonkeyengine by jMonkeyEngine.

the class SceneLoader method endElement.

@Override
public void endElement(String uri, String name, String qName) throws SAXException {
    if (qName.equals("node")) {
        node = node.getParent();
    } else if (qName.equals("nodes")) {
        node = null;
    } else if (qName.equals("entity")) {
        node = entityNode.getParent();
        entityNode = null;
    } else if (qName.equals("camera")) {
        node = cameraNode.getParent();
        cameraNode = null;
    } else if (qName.equals("light")) {
        // apply the node's world transform on the light..
        root.updateGeometricState();
        if (light != null) {
            if (light instanceof DirectionalLight) {
                DirectionalLight dl = (DirectionalLight) light;
                Quaternion q = node.getWorldRotation();
                Vector3f dir = dl.getDirection();
                q.multLocal(dir);
                dl.setDirection(dir);
            } else if (light instanceof PointLight) {
                PointLight pl = (PointLight) light;
                Vector3f pos = node.getWorldTranslation();
                pl.setPosition(pos);
            } else if (light instanceof SpotLight) {
                SpotLight sl = (SpotLight) light;
                Vector3f pos = node.getWorldTranslation();
                sl.setPosition(pos);
                Quaternion q = node.getWorldRotation();
                Vector3f dir = sl.getDirection();
                q.multLocal(dir);
                sl.setDirection(dir);
            }
        }
        light = null;
    }
    checkTopNode(qName);
    elementStack.pop();
}
Also used : Quaternion(com.jme3.math.Quaternion) DirectionalLight(com.jme3.light.DirectionalLight) Vector3f(com.jme3.math.Vector3f) PointLight(com.jme3.light.PointLight) SpotLight(com.jme3.light.SpotLight)

Example 12 with SpotLight

use of com.jme3.light.SpotLight in project jmonkeyengine by jMonkeyEngine.

the class TestJaime method setupLights.

public void setupLights() {
    AmbientLight al = new AmbientLight();
    al.setColor(new ColorRGBA(0.1f, 0.1f, 0.1f, 1));
    rootNode.addLight(al);
    SpotLight sl = new SpotLight();
    sl.setColor(ColorRGBA.White.mult(1.0f));
    sl.setPosition(new Vector3f(1.2074411f, 10.6868908f, 4.1489987f));
    sl.setDirection(sl.getPosition().mult(-1));
    sl.setSpotOuterAngle(0.1f);
    sl.setSpotInnerAngle(0.004f);
    rootNode.addLight(sl);
    //pointlight to fake indirect light coming from the ground
    PointLight pl = new PointLight();
    pl.setColor(ColorRGBA.White.mult(1.5f));
    pl.setPosition(new Vector3f(0, 0, 1));
    pl.setRadius(2);
    rootNode.addLight(pl);
    SpotLightShadowRenderer shadows = new SpotLightShadowRenderer(assetManager, 1024);
    shadows.setLight(sl);
    shadows.setShadowIntensity(0.3f);
    shadows.setEdgeFilteringMode(EdgeFilteringMode.PCF8);
    viewPort.addProcessor(shadows);
    FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
    SSAOFilter filter = new SSAOFilter(0.10997847f, 0.440001f, 0.39999998f, -0.008000026f);
    ;
    fpp.addFilter(filter);
    fpp.addFilter(new FXAAFilter());
    fpp.addFilter(new FXAAFilter());
    viewPort.addProcessor(fpp);
}
Also used : SSAOFilter(com.jme3.post.ssao.SSAOFilter) FXAAFilter(com.jme3.post.filters.FXAAFilter) ColorRGBA(com.jme3.math.ColorRGBA) Vector3f(com.jme3.math.Vector3f) FilterPostProcessor(com.jme3.post.FilterPostProcessor) PointLight(com.jme3.light.PointLight) SpotLightShadowRenderer(com.jme3.shadow.SpotLightShadowRenderer) SpotLight(com.jme3.light.SpotLight) AmbientLight(com.jme3.light.AmbientLight)

Example 13 with SpotLight

use of com.jme3.light.SpotLight in project jmonkeyengine by jMonkeyEngine.

the class LightHelper method toLight.

public Light toLight(Structure structure, BlenderContext blenderContext) throws BlenderFileException {
    Light result = (Light) blenderContext.getLoadedFeature(structure.getOldMemoryAddress(), LoadedDataType.FEATURE);
    if (result != null) {
        return result;
    }
    Light light = null;
    int type = ((Number) structure.getFieldValue("type")).intValue();
    switch(type) {
        case // Lamp
        0:
            light = new PointLight();
            float distance = ((Number) structure.getFieldValue("dist")).floatValue();
            ((PointLight) light).setRadius(distance);
            break;
        case // Sun
        1:
            LOGGER.log(Level.WARNING, "'Sun' lamp is not supported in jMonkeyEngine. Using PointLight with radius = Float.MAX_VALUE.");
            light = new PointLight();
            ((PointLight) light).setRadius(Float.MAX_VALUE);
            break;
        case // Spot
        2:
            light = new SpotLight();
            // range
            ((SpotLight) light).setSpotRange(((Number) structure.getFieldValue("dist")).floatValue());
            // outer angle
            float outerAngle = ((Number) structure.getFieldValue("spotsize")).floatValue() * FastMath.DEG_TO_RAD * 0.5f;
            ((SpotLight) light).setSpotOuterAngle(outerAngle);
            // inner angle
            float spotblend = ((Number) structure.getFieldValue("spotblend")).floatValue();
            spotblend = FastMath.clamp(spotblend, 0, 1);
            float innerAngle = outerAngle * (1 - spotblend);
            ((SpotLight) light).setSpotInnerAngle(innerAngle);
            break;
        case // Hemi
        3:
            LOGGER.log(Level.WARNING, "'Hemi' lamp is not supported in jMonkeyEngine. Using DirectionalLight instead.");
        case // Area
        4:
            light = new DirectionalLight();
            break;
        default:
            throw new BlenderFileException("Unknown light source type: " + type);
    }
    float r = ((Number) structure.getFieldValue("r")).floatValue();
    float g = ((Number) structure.getFieldValue("g")).floatValue();
    float b = ((Number) structure.getFieldValue("b")).floatValue();
    light.setColor(new ColorRGBA(r, g, b, 1.0f));
    light.setName(structure.getName());
    return light;
}
Also used : ColorRGBA(com.jme3.math.ColorRGBA) DirectionalLight(com.jme3.light.DirectionalLight) SpotLight(com.jme3.light.SpotLight) Light(com.jme3.light.Light) PointLight(com.jme3.light.PointLight) DirectionalLight(com.jme3.light.DirectionalLight) BlenderFileException(com.jme3.scene.plugins.blender.file.BlenderFileException) PointLight(com.jme3.light.PointLight) SpotLight(com.jme3.light.SpotLight)

Example 14 with SpotLight

use of com.jme3.light.SpotLight in project jmonkeyengine by jMonkeyEngine.

the class LightSortTest method testSimpleSort.

@Test
public void testSimpleSort() {
    Geometry g = new Geometry("test", new Mesh());
    LightList list = new LightList(g);
    list.add(new SpotLight(Vector3f.ZERO, Vector3f.UNIT_X));
    list.add(new PointLight(Vector3f.UNIT_X));
    list.add(new DirectionalLight(Vector3f.UNIT_X));
    list.add(new AmbientLight());
    list.sort(true);
    // Ambients always first
    assert list.get(0) instanceof AmbientLight;
    // .. then directionals
    assert list.get(1) instanceof DirectionalLight;
    // Spot is 0 units away from geom
    assert list.get(2) instanceof SpotLight;
    // .. and point is 1 unit away.
    assert list.get(3) instanceof PointLight;
}
Also used : Geometry(com.jme3.scene.Geometry) Mesh(com.jme3.scene.Mesh) Test(org.junit.Test)

Example 15 with SpotLight

use of com.jme3.light.SpotLight in project jmonkeyengine by jMonkeyEngine.

the class SinglePassAndImageBasedLightingLogic method updateLightListUniforms.

/**
     * Uploads the lights in the light list as two uniform arrays.<br/><br/> *
     * <p>
     * <code>uniform vec4 g_LightColor[numLights];</code><br/> //
     * g_LightColor.rgb is the diffuse/specular color of the light.<br/> //
     * g_Lightcolor.a is the type of light, 0 = Directional, 1 = Point, <br/> //
     * 2 = Spot. <br/> <br/>
     * <code>uniform vec4 g_LightPosition[numLights];</code><br/> //
     * g_LightPosition.xyz is the position of the light (for point lights)<br/>
     * // or the direction of the light (for directional lights).<br/> //
     * g_LightPosition.w is the inverse radius (1/r) of the light (for
     * attenuation) <br/> </p>
     */
protected int updateLightListUniforms(Shader shader, Geometry g, LightList lightList, int numLights, RenderManager rm, int startIndex, int lastTexUnit) {
    if (numLights == 0) {
        // this shader does not do lighting, ignore.
        return 0;
    }
    Uniform lightData = shader.getUniform("g_LightData");
    //8 lights * max 3
    lightData.setVector4Length(numLights * 3);
    Uniform ambientColor = shader.getUniform("g_AmbientLightColor");
    Uniform lightProbeData = shader.getUniform("g_LightProbeData");
    lightProbeData.setVector4Length(1);
    Uniform lightProbeIrrMap = shader.getUniform("g_IrradianceMap");
    Uniform lightProbePemMap = shader.getUniform("g_PrefEnvMap");
    lightProbe = null;
    if (startIndex != 0) {
        // apply additive blending for 2nd and future passes
        rm.getRenderer().applyRenderState(ADDITIVE_LIGHT);
        ambientColor.setValue(VarType.Vector4, ColorRGBA.Black);
    } else {
        lightProbe = extractIndirectLights(lightList, true);
        ambientColor.setValue(VarType.Vector4, ambientLightColor);
    }
    //If there is a lightProbe in the list we force it's render on the first pass
    if (lightProbe != null) {
        BoundingSphere s = (BoundingSphere) lightProbe.getBounds();
        lightProbeData.setVector4InArray(lightProbe.getPosition().x, lightProbe.getPosition().y, lightProbe.getPosition().z, 1f / s.getRadius(), 0);
        //assigning new texture indexes
        int irrUnit = lastTexUnit++;
        int pemUnit = lastTexUnit++;
        rm.getRenderer().setTexture(irrUnit, lightProbe.getIrradianceMap());
        lightProbeIrrMap.setValue(VarType.Int, irrUnit);
        rm.getRenderer().setTexture(pemUnit, lightProbe.getPrefilteredEnvMap());
        lightProbePemMap.setValue(VarType.Int, pemUnit);
    } else {
        //Disable IBL for this pass
        lightProbeData.setVector4InArray(0, 0, 0, -1, 0);
    }
    int lightDataIndex = 0;
    TempVars vars = TempVars.get();
    Vector4f tmpVec = vars.vect4f1;
    int curIndex;
    int endIndex = numLights + startIndex;
    for (curIndex = startIndex; curIndex < endIndex && curIndex < lightList.size(); curIndex++) {
        Light l = lightList.get(curIndex);
        if (l.getType() == Light.Type.Ambient) {
            endIndex++;
            continue;
        }
        ColorRGBA color = l.getColor();
        if (l.getType() != Light.Type.Probe) {
            lightData.setVector4InArray(color.getRed(), color.getGreen(), color.getBlue(), l.getType().getId(), lightDataIndex);
            lightDataIndex++;
        }
        switch(l.getType()) {
            case Directional:
                DirectionalLight dl = (DirectionalLight) l;
                Vector3f dir = dl.getDirection();
                //Data directly sent in view space to avoid a matrix mult for each pixel
                tmpVec.set(dir.getX(), dir.getY(), dir.getZ(), 0.0f);
                lightData.setVector4InArray(tmpVec.getX(), tmpVec.getY(), tmpVec.getZ(), -1, lightDataIndex);
                lightDataIndex++;
                //PADDING
                lightData.setVector4InArray(0, 0, 0, 0, lightDataIndex);
                lightDataIndex++;
                break;
            case Point:
                PointLight pl = (PointLight) l;
                Vector3f pos = pl.getPosition();
                float invRadius = pl.getInvRadius();
                tmpVec.set(pos.getX(), pos.getY(), pos.getZ(), 1.0f);
                lightData.setVector4InArray(tmpVec.getX(), tmpVec.getY(), tmpVec.getZ(), invRadius, lightDataIndex);
                lightDataIndex++;
                //PADDING
                lightData.setVector4InArray(0, 0, 0, 0, lightDataIndex);
                lightDataIndex++;
                break;
            case Spot:
                SpotLight sl = (SpotLight) l;
                Vector3f pos2 = sl.getPosition();
                Vector3f dir2 = sl.getDirection();
                float invRange = sl.getInvSpotRange();
                float spotAngleCos = sl.getPackedAngleCos();
                tmpVec.set(pos2.getX(), pos2.getY(), pos2.getZ(), 1.0f);
                lightData.setVector4InArray(tmpVec.getX(), tmpVec.getY(), tmpVec.getZ(), invRange, lightDataIndex);
                lightDataIndex++;
                tmpVec.set(dir2.getX(), dir2.getY(), dir2.getZ(), 0.0f);
                lightData.setVector4InArray(tmpVec.getX(), tmpVec.getY(), tmpVec.getZ(), spotAngleCos, lightDataIndex);
                lightDataIndex++;
                break;
            default:
                throw new UnsupportedOperationException("Unknown type of light: " + l.getType());
        }
    }
    vars.release();
    //Padding of unsued buffer space
    while (lightDataIndex < numLights * 3) {
        lightData.setVector4InArray(0f, 0f, 0f, 0f, lightDataIndex);
        lightDataIndex++;
    }
    return curIndex;
}
Also used : BoundingSphere(com.jme3.bounding.BoundingSphere) TempVars(com.jme3.util.TempVars)

Aggregations

SpotLight (com.jme3.light.SpotLight)17 DirectionalLight (com.jme3.light.DirectionalLight)12 PointLight (com.jme3.light.PointLight)12 Vector3f (com.jme3.math.Vector3f)10 AmbientLight (com.jme3.light.AmbientLight)8 Geometry (com.jme3.scene.Geometry)7 ColorRGBA (com.jme3.math.ColorRGBA)6 Quaternion (com.jme3.math.Quaternion)6 Light (com.jme3.light.Light)5 Node (com.jme3.scene.Node)5 TempVars (com.jme3.util.TempVars)5 Box (com.jme3.scene.shape.Box)4 SpotLightShadowRenderer (com.jme3.shadow.SpotLightShadowRenderer)4 ActionListener (com.jme3.input.controls.ActionListener)3 KeyTrigger (com.jme3.input.controls.KeyTrigger)3 FilterPostProcessor (com.jme3.post.FilterPostProcessor)3 Sphere (com.jme3.scene.shape.Sphere)3 Uniform (com.jme3.shader.Uniform)3 Test (org.junit.Test)3 BoundingSphere (com.jme3.bounding.BoundingSphere)2