Search in sources :

Example 36 with Light

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

the class Spatial method oldClone.

/**
     *  The old clone() method that did not use the new Cloner utility.
     */
public Spatial oldClone(boolean cloneMaterial) {
    try {
        Spatial clone = (Spatial) super.clone();
        if (worldBound != null) {
            clone.worldBound = worldBound.clone();
        }
        clone.worldLights = worldLights.clone();
        clone.localLights = localLights.clone();
        // Set the new owner of the light lists
        clone.localLights.setOwner(clone);
        clone.worldLights.setOwner(clone);
        clone.worldOverrides = new SafeArrayList<>(MatParamOverride.class);
        clone.localOverrides = new SafeArrayList<>(MatParamOverride.class);
        for (MatParamOverride override : localOverrides) {
            clone.localOverrides.add((MatParamOverride) override.clone());
        }
        // No need to force cloned to update.
        // This node already has the refresh flags
        // set below so it will have to update anyway.
        clone.worldTransform = worldTransform.clone();
        clone.localTransform = localTransform.clone();
        if (clone instanceof Node) {
            Node node = (Node) this;
            Node nodeClone = (Node) clone;
            nodeClone.children = new SafeArrayList<Spatial>(Spatial.class);
            for (Spatial child : node.children) {
                Spatial childClone = child.clone(cloneMaterial);
                childClone.parent = nodeClone;
                nodeClone.children.add(childClone);
            }
        }
        clone.parent = null;
        clone.setBoundRefresh();
        clone.setTransformRefresh();
        clone.setLightListRefresh();
        clone.setMatParamOverrideRefresh();
        clone.controls = new SafeArrayList<Control>(Control.class);
        for (int i = 0; i < controls.size(); i++) {
            Control newControl = controls.get(i).cloneForSpatial(clone);
            newControl.setSpatial(clone);
            clone.controls.add(newControl);
        }
        if (userData != null) {
            clone.userData = (HashMap<String, Savable>) userData.clone();
        }
        return clone;
    } catch (CloneNotSupportedException ex) {
        throw new AssertionError();
    }
}
Also used : Control(com.jme3.scene.control.Control) MatParamOverride(com.jme3.material.MatParamOverride)

Example 37 with Light

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

the class MultiPassLightingLogic method render.

@Override
public void render(RenderManager renderManager, Shader shader, Geometry geometry, LightList lights, int lastTexUnit) {
    Renderer r = renderManager.getRenderer();
    Uniform lightDir = shader.getUniform("g_LightDirection");
    Uniform lightColor = shader.getUniform("g_LightColor");
    Uniform lightPos = shader.getUniform("g_LightPosition");
    Uniform ambientColor = shader.getUniform("g_AmbientLightColor");
    boolean isFirstLight = true;
    boolean isSecondLight = false;
    getAmbientColor(lights, false, ambientLightColor);
    for (int i = 0; i < lights.size(); i++) {
        Light l = lights.get(i);
        if (l instanceof AmbientLight) {
            continue;
        }
        if (isFirstLight) {
            // set ambient color for first light only
            ambientColor.setValue(VarType.Vector4, ambientLightColor);
            isFirstLight = false;
            isSecondLight = true;
        } else if (isSecondLight) {
            ambientColor.setValue(VarType.Vector4, ColorRGBA.Black);
            // apply additive blending for 2nd and future lights
            r.applyRenderState(ADDITIVE_LIGHT);
            isSecondLight = false;
        }
        TempVars vars = TempVars.get();
        Quaternion tmpLightDirection = vars.quat1;
        Quaternion tmpLightPosition = vars.quat2;
        ColorRGBA tmpLightColor = vars.color;
        Vector4f tmpVec = vars.vect4f1;
        ColorRGBA color = l.getColor();
        tmpLightColor.set(color);
        tmpLightColor.a = l.getType().getId();
        lightColor.setValue(VarType.Vector4, tmpLightColor);
        switch(l.getType()) {
            case Directional:
                DirectionalLight dl = (DirectionalLight) l;
                Vector3f dir = dl.getDirection();
                //FIXME : there is an inconstency here due to backward
                //compatibility of the lighting shader.
                //The directional light direction is passed in the
                //LightPosition uniform. The lighting shader needs to be
                //reworked though in order to fix this.
                tmpLightPosition.set(dir.getX(), dir.getY(), dir.getZ(), -1);
                lightPos.setValue(VarType.Vector4, tmpLightPosition);
                tmpLightDirection.set(0, 0, 0, 0);
                lightDir.setValue(VarType.Vector4, tmpLightDirection);
                break;
            case Point:
                PointLight pl = (PointLight) l;
                Vector3f pos = pl.getPosition();
                float invRadius = pl.getInvRadius();
                tmpLightPosition.set(pos.getX(), pos.getY(), pos.getZ(), invRadius);
                lightPos.setValue(VarType.Vector4, tmpLightPosition);
                tmpLightDirection.set(0, 0, 0, 0);
                lightDir.setValue(VarType.Vector4, tmpLightDirection);
                break;
            case Spot:
                SpotLight sl = (SpotLight) l;
                Vector3f pos2 = sl.getPosition();
                Vector3f dir2 = sl.getDirection();
                float invRange = sl.getInvSpotRange();
                float spotAngleCos = sl.getPackedAngleCos();
                tmpLightPosition.set(pos2.getX(), pos2.getY(), pos2.getZ(), invRange);
                lightPos.setValue(VarType.Vector4, tmpLightPosition);
                //We transform the spot direction in view space here to save 5 varying later in the lighting shader
                //one vec4 less and a vec4 that becomes a vec3
                //the downside is that spotAngleCos decoding happens now in the frag shader.
                tmpVec.set(dir2.getX(), dir2.getY(), dir2.getZ(), 0);
                renderManager.getCurrentCamera().getViewMatrix().mult(tmpVec, tmpVec);
                tmpLightDirection.set(tmpVec.getX(), tmpVec.getY(), tmpVec.getZ(), spotAngleCos);
                lightDir.setValue(VarType.Vector4, tmpLightDirection);
                break;
            case Probe:
                break;
            default:
                throw new UnsupportedOperationException("Unknown type of light: " + l.getType());
        }
        vars.release();
        r.setShader(shader);
        renderMeshFromGeometry(r, geometry);
    }
    if (isFirstLight) {
        // Either there are no lights at all, or only ambient lights.
        // Render a dummy "normal light" so we can see the ambient color.
        ambientColor.setValue(VarType.Vector4, getAmbientColor(lights, false, ambientLightColor));
        lightColor.setValue(VarType.Vector4, ColorRGBA.BlackNoAlpha);
        lightPos.setValue(VarType.Vector4, NULL_DIR_LIGHT);
        r.setShader(shader);
        renderMeshFromGeometry(r, geometry);
    }
}
Also used : Quaternion(com.jme3.math.Quaternion) Uniform(com.jme3.shader.Uniform) TempVars(com.jme3.util.TempVars) SpotLight(com.jme3.light.SpotLight) ColorRGBA(com.jme3.math.ColorRGBA) Vector4f(com.jme3.math.Vector4f) DirectionalLight(com.jme3.light.DirectionalLight) SpotLight(com.jme3.light.SpotLight) Light(com.jme3.light.Light) PointLight(com.jme3.light.PointLight) AmbientLight(com.jme3.light.AmbientLight) DirectionalLight(com.jme3.light.DirectionalLight) Vector3f(com.jme3.math.Vector3f) Renderer(com.jme3.renderer.Renderer) PointLight(com.jme3.light.PointLight) AmbientLight(com.jme3.light.AmbientLight)

Example 38 with Light

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

the class StaticPassLightingLogic method makeCurrent.

@Override
public Shader makeCurrent(AssetManager assetManager, RenderManager renderManager, EnumSet<Caps> rendererCaps, LightList lights, DefineList defines) {
    // TODO: if it ever changes that render isn't called
    // right away with the same geometry after makeCurrent, it would be
    // a problem.
    // Do a radix sort.
    tempDirLights.clear();
    tempPointLights.clear();
    tempSpotLights.clear();
    for (Light light : lights) {
        switch(light.getType()) {
            case Directional:
                tempDirLights.add((DirectionalLight) light);
                break;
            case Point:
                tempPointLights.add((PointLight) light);
                break;
            case Spot:
                tempSpotLights.add((SpotLight) light);
                break;
        }
    }
    defines.set(numDirLightsDefineId, tempDirLights.size());
    defines.set(numPointLightsDefineId, tempPointLights.size());
    defines.set(numSpotLightsDefineId, tempSpotLights.size());
    return techniqueDef.getShader(assetManager, rendererCaps, defines);
}
Also used : DirectionalLight(com.jme3.light.DirectionalLight) SpotLight(com.jme3.light.SpotLight) Light(com.jme3.light.Light) PointLight(com.jme3.light.PointLight)

Example 39 with Light

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

the class StaticPassLightingLogic method updateLightListUniforms.

private void updateLightListUniforms(Matrix4f viewMatrix, Shader shader, LightList lights) {
    Uniform ambientColor = shader.getUniform("g_AmbientLightColor");
    ambientColor.setValue(VarType.Vector4, getAmbientColor(lights, true, ambientLightColor));
    Uniform lightData = shader.getUniform("g_LightData");
    int totalSize = tempDirLights.size() * 2 + tempPointLights.size() * 2 + tempSpotLights.size() * 3;
    lightData.setVector4Length(totalSize);
    int index = 0;
    for (DirectionalLight light : tempDirLights) {
        ColorRGBA color = light.getColor();
        tempDirection.set(light.getDirection());
        transformDirection(viewMatrix, tempDirection);
        lightData.setVector4InArray(color.r, color.g, color.b, 1f, index++);
        lightData.setVector4InArray(tempDirection.x, tempDirection.y, tempDirection.z, 1f, index++);
    }
    for (PointLight light : tempPointLights) {
        ColorRGBA color = light.getColor();
        tempPosition.set(light.getPosition());
        float invRadius = light.getInvRadius();
        transformPosition(viewMatrix, tempPosition);
        lightData.setVector4InArray(color.r, color.g, color.b, 1f, index++);
        lightData.setVector4InArray(tempPosition.x, tempPosition.y, tempPosition.z, invRadius, index++);
    }
    for (SpotLight light : tempSpotLights) {
        ColorRGBA color = light.getColor();
        Vector3f pos = light.getPosition();
        Vector3f dir = light.getDirection();
        tempPosition.set(light.getPosition());
        tempDirection.set(light.getDirection());
        transformPosition(viewMatrix, tempPosition);
        transformDirection(viewMatrix, tempDirection);
        float invRange = light.getInvSpotRange();
        float spotAngleCos = light.getPackedAngleCos();
        lightData.setVector4InArray(color.r, color.g, color.b, 1f, index++);
        lightData.setVector4InArray(tempPosition.x, tempPosition.y, tempPosition.z, invRange, index++);
        lightData.setVector4InArray(tempDirection.x, tempDirection.y, tempDirection.z, spotAngleCos, index++);
    }
}
Also used : ColorRGBA(com.jme3.math.ColorRGBA) DirectionalLight(com.jme3.light.DirectionalLight) Vector3f(com.jme3.math.Vector3f) Uniform(com.jme3.shader.Uniform) PointLight(com.jme3.light.PointLight) SpotLight(com.jme3.light.SpotLight)

Example 40 with Light

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

the class LightControl method spatialTolight.

private void spatialTolight(Light light) {
    if (light instanceof PointLight) {
        ((PointLight) light).setPosition(spatial.getWorldTranslation());
    }
    TempVars vars = TempVars.get();
    if (light instanceof DirectionalLight) {
        ((DirectionalLight) light).setDirection(vars.vect1.set(spatial.getWorldTranslation()).multLocal(-1.0f));
    }
    if (light instanceof SpotLight) {
        ((SpotLight) light).setPosition(spatial.getWorldTranslation());
        ((SpotLight) light).setDirection(spatial.getWorldRotation().multLocal(vars.vect1.set(Vector3f.UNIT_Y).multLocal(-1)));
    }
    vars.release();
}
Also used : DirectionalLight(com.jme3.light.DirectionalLight) TempVars(com.jme3.util.TempVars) PointLight(com.jme3.light.PointLight) SpotLight(com.jme3.light.SpotLight)

Aggregations

Vector3f (com.jme3.math.Vector3f)64 Material (com.jme3.material.Material)61 DirectionalLight (com.jme3.light.DirectionalLight)55 Geometry (com.jme3.scene.Geometry)52 PointLight (com.jme3.light.PointLight)27 Spatial (com.jme3.scene.Spatial)27 Box (com.jme3.scene.shape.Box)26 Sphere (com.jme3.scene.shape.Sphere)26 ColorRGBA (com.jme3.math.ColorRGBA)24 Quaternion (com.jme3.math.Quaternion)21 Node (com.jme3.scene.Node)21 AmbientLight (com.jme3.light.AmbientLight)20 Texture (com.jme3.texture.Texture)18 SpotLight (com.jme3.light.SpotLight)16 FilterPostProcessor (com.jme3.post.FilterPostProcessor)15 KeyTrigger (com.jme3.input.controls.KeyTrigger)11 Test (org.junit.Test)11 TempVars (com.jme3.util.TempVars)10 Light (com.jme3.light.Light)9 Camera (com.jme3.renderer.Camera)9