use of com.jme3.light.SpotLight in project jmonkeyengine by jMonkeyEngine.
the class SinglePassLightingLogic 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) {
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");
if (startIndex != 0) {
// apply additive blending for 2nd and future passes
rm.getRenderer().applyRenderState(ADDITIVE_LIGHT);
ambientColor.setValue(VarType.Vector4, ColorRGBA.Black);
} else {
ambientColor.setValue(VarType.Vector4, getAmbientColor(lightList, true, ambientLightColor));
}
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();
//Color
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);
rm.getCurrentCamera().getViewMatrix().mult(tmpVec, tmpVec);
// tmpVec.divideLocal(tmpVec.w);
// tmpVec.normalizeLocal();
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);
rm.getCurrentCamera().getViewMatrix().mult(tmpVec, tmpVec);
//tmpVec.divideLocal(tmpVec.w);
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);
rm.getCurrentCamera().getViewMatrix().mult(tmpVec, tmpVec);
// tmpVec.divideLocal(tmpVec.w);
lightData.setVector4InArray(tmpVec.getX(), tmpVec.getY(), tmpVec.getZ(), invRange, lightDataIndex);
lightDataIndex++;
//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.0f);
rm.getCurrentCamera().getViewMatrix().mult(tmpVec, tmpVec);
tmpVec.normalizeLocal();
lightData.setVector4InArray(tmpVec.getX(), tmpVec.getY(), tmpVec.getZ(), spotAngleCos, lightDataIndex);
lightDataIndex++;
break;
case Probe:
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;
}
use of com.jme3.light.SpotLight 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);
}
use of com.jme3.light.SpotLight 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);
}
use of com.jme3.light.SpotLight in project jmonkeyengine by jMonkeyEngine.
the class TestSpotLight method setupLighting.
public void setupLighting() {
AmbientLight al = new AmbientLight();
al.setColor(ColorRGBA.White.mult(0.02f));
rootNode.addLight(al);
spot = new SpotLight();
spot.setSpotRange(1000);
spot.setSpotInnerAngle(5 * FastMath.DEG_TO_RAD);
spot.setSpotOuterAngle(10 * FastMath.DEG_TO_RAD);
spot.setPosition(new Vector3f(77.70334f, 34.013165f, 27.1017f));
spot.setDirection(lightTarget.subtract(spot.getPosition()));
spot.setColor(ColorRGBA.White.mult(2));
rootNode.addLight(spot);
// PointLight pl=new PointLight();
// pl.setPosition(new Vector3f(77.70334f, 34.013165f, 27.1017f));
// pl.setRadius(1000);
// pl.setColor(ColorRGBA.White.mult(2));
// rootNode.addLight(pl);
lightMdl = new Geometry("Light", new Sphere(10, 10, 0.1f));
lightMdl.setMaterial(assetManager.loadMaterial("Common/Materials/RedColor.j3m"));
lightMdl.setLocalTranslation(new Vector3f(77.70334f, 34.013165f, 27.1017f));
lightMdl.setLocalScale(5);
rootNode.attachChild(lightMdl);
// DirectionalLight dl = new DirectionalLight();
// dl.setDirection(lightTarget.subtract(new Vector3f(77.70334f, 34.013165f, 27.1017f)));
// dl.setColor(ColorRGBA.White.mult(2));
// rootNode.addLight(dl);
}
use of com.jme3.light.SpotLight in project jmonkeyengine by jMonkeyEngine.
the class TestSpotLightShadows method setupLighting.
public void setupLighting() {
AmbientLight al = new AmbientLight();
al.setColor(ColorRGBA.White.mult(0.02f));
rootNode.addLight(al);
rootNode.setShadowMode(ShadowMode.CastAndReceive);
spot = new SpotLight();
spot.setSpotRange(1000);
spot.setSpotInnerAngle(5f * FastMath.DEG_TO_RAD);
spot.setSpotOuterAngle(10 * FastMath.DEG_TO_RAD);
spot.setPosition(new Vector3f(70.70334f, 34.013165f, 27.1017f));
spot.setDirection(lightTarget.subtract(spot.getPosition()).normalizeLocal());
spot.setColor(ColorRGBA.White.mult(2));
rootNode.addLight(spot);
// PointLight pl=new PointLight();
// pl.setPosition(new Vector3f(77.70334f, 34.013165f, 27.1017f));
// pl.setRadius(1000);
// pl.setColor(ColorRGBA.White.mult(2));
// rootNode.addLight(pl);
lightMdl = new Geometry("Light", new Sphere(10, 10, 0.1f));
lightMdl.setMaterial(assetManager.loadMaterial("Common/Materials/RedColor.j3m"));
lightMdl.setLocalTranslation(new Vector3f(77.70334f, 34.013165f, 27.1017f));
lightMdl.setLocalScale(5);
rootNode.attachChild(lightMdl);
// DirectionalLight dl = new DirectionalLight();
// dl.setDirection(lightTarget.subtract(new Vector3f(77.70334f, 34.013165f, 27.1017f)));
// dl.setColor(ColorRGBA.White.mult(0.7f));
// rootNode.addLight(dl);
final SpotLightShadowRenderer slsr = new SpotLightShadowRenderer(assetManager, 512);
slsr.setLight(spot);
slsr.setShadowIntensity(0.5f);
slsr.setShadowZExtend(100);
slsr.setShadowZFadeLength(5);
slsr.setEdgeFilteringMode(EdgeFilteringMode.PCFPOISSON);
viewPort.addProcessor(slsr);
SpotLightShadowFilter slsf = new SpotLightShadowFilter(assetManager, 512);
slsf.setLight(spot);
slsf.setShadowIntensity(0.5f);
slsf.setShadowZExtend(100);
slsf.setShadowZFadeLength(5);
slsf.setEdgeFilteringMode(EdgeFilteringMode.PCFPOISSON);
slsf.setEnabled(false);
FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
fpp.addFilter(slsf);
viewPort.addProcessor(fpp);
ShadowTestUIManager uiMan = new ShadowTestUIManager(assetManager, slsr, slsf, guiNode, inputManager, viewPort);
inputManager.addListener(new ActionListener() {
public void onAction(String name, boolean isPressed, float tpf) {
if (name.equals("stop") && isPressed) {
stop = !stop;
// slsr.displayFrustum();
System.out.println("pos : " + spot.getPosition());
System.out.println("dir : " + spot.getDirection());
}
}
}, "stop");
inputManager.addMapping("stop", new KeyTrigger(KeyInput.KEY_1));
flyCam.setDragToRotate(true);
}
Aggregations