use of com.jme3.light.PointLight in project jmonkeyengine by jMonkeyEngine.
the class ShadowUtil method getGeometriesInLightRadius.
/**
* Populates the outputGeometryList with the geometry of the
* inputGeomtryList that are in the radius of a light.
* The array of camera must be an array of 6 cameras initialized so they represent the light viewspace of a pointlight
*
* @param inputGeometryList The list containing all geometry to check
* against the camera frustum
* @param cameras the camera array to check geometries against
* @param outputGeometryList the list of all geometries that are in the
* camera frustum
*/
public static void getGeometriesInLightRadius(GeometryList inputGeometryList, Camera[] cameras, GeometryList outputGeometryList) {
for (int i = 0; i < inputGeometryList.size(); i++) {
Geometry g = inputGeometryList.get(i);
boolean inFrustum = false;
for (int j = 0; j < cameras.length && inFrustum == false; j++) {
Camera camera = cameras[j];
int planeState = camera.getPlaneState();
camera.setPlaneState(0);
inFrustum = camera.contains(g.getWorldBound()) != Camera.FrustumIntersect.Outside;
camera.setPlaneState(planeState);
}
if (inFrustum) {
outputGeometryList.add(g);
}
}
}
use of com.jme3.light.PointLight in project jmonkeyengine by jMonkeyEngine.
the class LightControl method lightToSpatial.
private void lightToSpatial(Light light) {
TempVars vars = TempVars.get();
if (light instanceof PointLight) {
PointLight pLight = (PointLight) light;
Vector3f vecDiff = vars.vect1.set(pLight.getPosition()).subtractLocal(spatial.getWorldTranslation());
spatial.setLocalTranslation(vecDiff.addLocal(spatial.getLocalTranslation()));
}
if (light instanceof DirectionalLight) {
DirectionalLight dLight = (DirectionalLight) light;
vars.vect1.set(dLight.getDirection()).multLocal(-1.0f);
Vector3f vecDiff = vars.vect1.subtractLocal(spatial.getWorldTranslation());
spatial.setLocalTranslation(vecDiff.addLocal(spatial.getLocalTranslation()));
}
vars.release();
//TODO add code for Spot light here when it's done
}
use of com.jme3.light.PointLight in project jmonkeyengine by jMonkeyEngine.
the class LightFilterTest method testPointFiltering.
@Test
public void testPointFiltering() {
PointLight pl = new PointLight(Vector3f.ZERO);
geom.addLight(pl);
// Infinite point lights must never be filtered
checkFilteredLights(1);
// Light at origin does not intersect geom which is at Z=10
pl.setRadius(1);
checkFilteredLights(0);
// Put it closer to geom, the very edge of the sphere touches the box.
// Still not considered an intersection though.
pl.setPosition(new Vector3f(0, 0, 8f));
checkFilteredLights(0);
// And more close - now its an intersection.
pl.setPosition(new Vector3f(0, 0, 8f + FastMath.ZERO_TOLERANCE));
checkFilteredLights(1);
// Move the geometry away
geom.move(0, 0, FastMath.ZERO_TOLERANCE);
checkFilteredLights(0);
// Test if the algorithm converts the sphere
// to a box before testing the collision (incorrect)
float sqrt3 = FastMath.sqrt(3);
pl.setPosition(new Vector3f(2, 2, 8));
pl.setRadius(sqrt3);
checkFilteredLights(0);
// Make it a wee bit larger.
pl.setRadius(sqrt3 + FastMath.ZERO_TOLERANCE);
checkFilteredLights(1);
// Rotate the camera so it is up, light is outside frustum.
cam.lookAtDirection(Vector3f.UNIT_Y, Vector3f.UNIT_Y);
checkFilteredLights(0);
// ==================================
// Tests for bounding Sphere
geom.setModelBound(new BoundingSphere(1f, Vector3f.ZERO));
geom.setLocalTranslation(0, 0, 2);
pl.setPosition(new Vector3f(0, 0, 2f));
// Infinite point lights must never be filtered
pl.setRadius(0);
checkFilteredLights(1);
pl.setRadius(1f);
// Put the light at the very close to the geom,
// the very edge of the sphere touches the other bounding sphere
// Still not considered an intersection though.
pl.setPosition(new Vector3f(0, 0, 0));
checkFilteredLights(0);
// And more close - now its an intersection.
pl.setPosition(new Vector3f(0, 0, 0f + FastMath.ZERO_TOLERANCE));
checkFilteredLights(1);
geom.setLocalTranslation(0, 0, 0);
// In this case its an intersection for pointLight v. box
// But not for pointLight v. sphere
// Vector3f(0, 0.5f, 0.5f).normalize().mult(2) ~ >= (0.0, 1.4142135, 1.4142135)
//pl.setPosition(new Vector3f(0, 0.5f, 0.5f).normalizeLocal().multLocal(2 + FastMath.ZERO_TOLERANCE));
pl.setPosition(new Vector3f(0f, 1.4142135f, 1.4142135f).multLocal(1 + FastMath.ZERO_TOLERANCE));
checkFilteredLights(0);
// Make the distance a wee bit closer, now its an intersection
//pl.setPosition(new Vector3f(0, 0.5f, 0.5f).normalizeLocal().multLocal(2 - FastMath.ZERO_TOLERANCE));
pl.setPosition(new Vector3f(0f, 1.4142135f, 1.4142135f).multLocal(1 - FastMath.ZERO_TOLERANCE));
checkFilteredLights(1);
// it's a point light, also test for the other corner
pl.setPosition(new Vector3f(0f, -1.4142135f, -1.4142135f).multLocal(1 - FastMath.ZERO_TOLERANCE));
checkFilteredLights(0);
}
use of com.jme3.light.PointLight 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;
}
use of com.jme3.light.PointLight 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;
}
Aggregations