Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hello;
- import com.jme3.app.SimpleApplication;
- import com.jme3.input.KeyInput;
- import com.jme3.input.controls.ActionListener;
- import com.jme3.input.controls.AnalogListener;
- import com.jme3.input.controls.KeyTrigger;
- import com.jme3.light.AmbientLight;
- import com.jme3.light.DirectionalLight;
- import com.jme3.material.Material;
- import com.jme3.math.ColorRGBA;
- import com.jme3.math.Vector3f;
- import com.jme3.post.FilterPostProcessor;
- import com.jme3.post.ssao.SSAOFilter;
- import com.jme3.renderer.queue.RenderQueue.ShadowMode;
- import com.jme3.scene.Geometry;
- import com.jme3.scene.Node;
- import com.jme3.scene.shape.Box;
- import com.jme3.shadow.PssmShadowRenderer;
- import com.jme3.system.AppSettings;
- public class HelloShadows extends SimpleApplication {
- public static void main(String[] args) {
- AppSettings settings = new AppSettings(true);
- settings.setResolution(800, 600);
- settings.setTitle("HelloShadows");
- settings.setSamples(4);
- settings.setFrameRate(120);
- HelloShadows app = new HelloShadows();
- app.setShowSettings(false);
- app.setSettings(settings);
- app.setPauseOnLostFocus(false);
- app.start();
- }
- private float speedX = 0.1f, speedY = 0.1f;
- private Vector3f lightDirection = new Vector3f(-.5f, -.5f, -.5f);
- private PssmShadowRenderer pssmRenderer;
- private FilterPostProcessor fppSSAO;
- @Override
- public void simpleInitApp() {
- setupCamera();
- setupKeys();
- setupFloor();
- setupBox();
- setupSSAO();
- setupShadows();
- setupLights();
- }
- private void setupCamera() {
- flyCam.setEnabled(false);
- inputManager.removeListener(flyCam);
- final RtsCam rtsCam = new RtsCam(cam, rootNode);
- rtsCam.registerWithInput(inputManager);
- rtsCam.setCenter(new Vector3f(0f, 0f, 0f));
- }
- private void setupKeys() {
- inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_LEFT));
- inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_RIGHT));
- inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_UP));
- inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_DOWN));
- inputManager.addMapping("SSAO", new KeyTrigger(KeyInput.KEY_O));
- inputManager.addMapping("PSSM", new KeyTrigger(KeyInput.KEY_P));
- inputManager.addListener(analogListener, new String[]{"Left", "Right", "Up", "Down"});
- inputManager.addListener(actionListener, new String[]{"SSAO", "PSSM"});
- }
- private AnalogListener analogListener = new AnalogListener() {
- public void onAnalog(String name, float value, float tpf) {
- if (name.equals("Left")) speedY -= 1f * tpf;
- if (name.equals("Right")) speedY += 1f * tpf;
- if (name.equals("Up")) speedX += 1f * tpf;
- if (name.equals("Down")) speedX -= 1f * tpf;
- }
- };
- private ActionListener actionListener = new ActionListener() {
- public void onAction(String name, boolean keyPressed, float tpf) {
- if (name.equals("SSAO") && keyPressed) {
- if (viewPort.getProcessors().contains(fppSSAO)) viewPort.removeProcessor(fppSSAO);
- else viewPort.addProcessor(fppSSAO);
- }
- if (name.equals("PSSM") && keyPressed) {
- if (viewPort.getProcessors().contains(pssmRenderer)) viewPort.removeProcessor(pssmRenderer);
- else viewPort.addProcessor(pssmRenderer);
- }
- }
- };
- private void setupFloor() {
- Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
- mat.setBoolean("UseMaterialColors", true);
- mat.setColor("Ambient", ColorRGBA.Gray);
- mat.setColor("Diffuse", ColorRGBA.Gray);
- mat.setColor("Specular", ColorRGBA.Gray);
- mat.setFloat("Shininess", 2);
- Geometry box = new Geometry("Floor", new Box(Vector3f.ZERO, 5f, 0.1f, 5f));
- box.setMaterial(mat);
- box.setShadowMode(ShadowMode.Receive);
- rootNode.attachChild(box);
- }
- private void setupBox() {
- Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
- mat.setBoolean("UseMaterialColors", true);
- mat.setColor("Ambient", ColorRGBA.White);
- mat.setColor("Diffuse", ColorRGBA.White);
- mat.setColor("Specular", ColorRGBA.White);
- mat.setFloat("Shininess", 12);
- Geometry box = new Geometry("Box", new Box(Vector3f.ZERO, 1, 1, 1));
- box.setMaterial(mat);
- box.setShadowMode(ShadowMode.CastAndReceive);
- Node pivot = new Node("pivot");
- pivot.move(0f, 2f, 0f);
- pivot.attachChild(box);
- rootNode.attachChild(pivot);
- }
- private void setupSSAO() {
- fppSSAO = new FilterPostProcessor(assetManager);
- SSAOFilter ssaoFilter = new SSAOFilter(12.940201f, 43.928635f, 0.32999992f, 0.6059958f);
- fppSSAO.addFilter(ssaoFilter);
- viewPort.addProcessor(fppSSAO);
- }
- private void setupShadows() {
- pssmRenderer = new PssmShadowRenderer(assetManager, 1024, 3);
- pssmRenderer.setDirection(lightDirection.normalizeLocal());
- viewPort.addProcessor(pssmRenderer);
- rootNode.setShadowMode(ShadowMode.Off);
- }
- private void setupLights() {
- DirectionalLight dl = new DirectionalLight();
- dl.setDirection(lightDirection.normalizeLocal());
- dl.setColor(ColorRGBA.White);
- rootNode.addLight(dl);
- AmbientLight al = new AmbientLight();
- al.setColor(ColorRGBA.White.mult(0.2f));
- rootNode.addLight(al);
- }
- @Override
- public void simpleUpdate(float tpf) {
- rootNode.getChild("pivot").rotate(speedX*tpf, speedY*tpf, 0f);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement