Advertisement
Guest User

Untitled

a guest
Aug 5th, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.38 KB | None | 0 0
  1. package hello;
  2.  
  3. import com.jme3.app.SimpleApplication;
  4. import com.jme3.input.KeyInput;
  5. import com.jme3.input.controls.ActionListener;
  6. import com.jme3.input.controls.AnalogListener;
  7. import com.jme3.input.controls.KeyTrigger;
  8. import com.jme3.light.AmbientLight;
  9. import com.jme3.light.DirectionalLight;
  10. import com.jme3.material.Material;
  11. import com.jme3.math.ColorRGBA;
  12. import com.jme3.math.Vector3f;
  13. import com.jme3.post.FilterPostProcessor;
  14. import com.jme3.post.ssao.SSAOFilter;
  15. import com.jme3.renderer.queue.RenderQueue.ShadowMode;
  16. import com.jme3.scene.Geometry;
  17. import com.jme3.scene.Node;
  18. import com.jme3.scene.shape.Box;
  19. import com.jme3.shadow.PssmShadowRenderer;
  20. import com.jme3.system.AppSettings;
  21.  
  22. public class HelloShadows extends SimpleApplication {
  23.  
  24.     public static void main(String[] args) {
  25.         AppSettings settings = new AppSettings(true);
  26.         settings.setResolution(800, 600);
  27.         settings.setTitle("HelloShadows");
  28.         settings.setSamples(4);
  29.         settings.setFrameRate(120);        
  30.        
  31.         HelloShadows app = new HelloShadows();
  32.         app.setShowSettings(false);
  33.         app.setSettings(settings);
  34.         app.setPauseOnLostFocus(false);
  35.         app.start();        
  36.     }
  37.    
  38.     private float speedX = 0.1f, speedY = 0.1f;
  39.     private Vector3f lightDirection = new Vector3f(-.5f, -.5f, -.5f);
  40.     private PssmShadowRenderer pssmRenderer;
  41.     private FilterPostProcessor fppSSAO;
  42.    
  43.     @Override
  44.     public void simpleInitApp() {      
  45.         setupCamera();
  46.         setupKeys();
  47.         setupFloor();
  48.         setupBox();
  49.         setupSSAO();
  50.         setupShadows();
  51.         setupLights();
  52.     }
  53.    
  54.     private void setupCamera() {
  55.         flyCam.setEnabled(false);
  56.         inputManager.removeListener(flyCam);
  57.        
  58.         final RtsCam rtsCam = new RtsCam(cam, rootNode);
  59.         rtsCam.registerWithInput(inputManager);
  60.         rtsCam.setCenter(new Vector3f(0f, 0f, 0f));
  61.     }
  62.    
  63.     private void setupKeys() {
  64.         inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_LEFT));    
  65.         inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_RIGHT));
  66.         inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_UP));
  67.         inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_DOWN));
  68.         inputManager.addMapping("SSAO", new KeyTrigger(KeyInput.KEY_O));
  69.         inputManager.addMapping("PSSM", new KeyTrigger(KeyInput.KEY_P));
  70.        
  71.         inputManager.addListener(analogListener, new String[]{"Left", "Right", "Up", "Down"});
  72.         inputManager.addListener(actionListener, new String[]{"SSAO", "PSSM"});
  73.     }
  74.    
  75.     private AnalogListener analogListener = new AnalogListener() {
  76.         public void onAnalog(String name, float value, float tpf) {
  77.             if (name.equals("Left")) speedY -= 1f * tpf;
  78.             if (name.equals("Right")) speedY += 1f * tpf;
  79.             if (name.equals("Up")) speedX += 1f * tpf;
  80.             if (name.equals("Down")) speedX -= 1f * tpf;
  81.         }
  82.     };
  83.    
  84.     private ActionListener actionListener = new ActionListener() {
  85.         public void onAction(String name, boolean keyPressed, float tpf) {
  86.             if (name.equals("SSAO") && keyPressed) {
  87.                 if (viewPort.getProcessors().contains(fppSSAO)) viewPort.removeProcessor(fppSSAO);
  88.                 else viewPort.addProcessor(fppSSAO);               
  89.             }
  90.             if (name.equals("PSSM") && keyPressed) {
  91.                 if (viewPort.getProcessors().contains(pssmRenderer)) viewPort.removeProcessor(pssmRenderer);
  92.                 else viewPort.addProcessor(pssmRenderer);
  93.             }
  94.         }
  95.     };
  96.    
  97.     private void setupFloor() {
  98.         Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");       
  99.         mat.setBoolean("UseMaterialColors", true);
  100.         mat.setColor("Ambient", ColorRGBA.Gray);
  101.         mat.setColor("Diffuse", ColorRGBA.Gray);
  102.         mat.setColor("Specular", ColorRGBA.Gray);      
  103.         mat.setFloat("Shininess", 2);
  104.        
  105.         Geometry box = new Geometry("Floor", new Box(Vector3f.ZERO, 5f, 0.1f, 5f));
  106.         box.setMaterial(mat);
  107.         box.setShadowMode(ShadowMode.Receive);
  108.         rootNode.attachChild(box);
  109.     }
  110.    
  111.     private void setupBox() {
  112.         Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");       
  113.         mat.setBoolean("UseMaterialColors", true);
  114.         mat.setColor("Ambient", ColorRGBA.White);
  115.         mat.setColor("Diffuse", ColorRGBA.White);
  116.         mat.setColor("Specular", ColorRGBA.White);
  117.         mat.setFloat("Shininess", 12);
  118.        
  119.         Geometry box = new Geometry("Box", new Box(Vector3f.ZERO, 1, 1, 1));
  120.         box.setMaterial(mat);
  121.         box.setShadowMode(ShadowMode.CastAndReceive);
  122.        
  123.         Node pivot = new Node("pivot");
  124.         pivot.move(0f, 2f, 0f);
  125.         pivot.attachChild(box);
  126.        
  127.         rootNode.attachChild(pivot);
  128.     }
  129.    
  130.     private void setupSSAO() {     
  131.         fppSSAO = new FilterPostProcessor(assetManager);
  132.         SSAOFilter ssaoFilter = new SSAOFilter(12.940201f, 43.928635f, 0.32999992f, 0.6059958f);       
  133.         fppSSAO.addFilter(ssaoFilter);
  134.         viewPort.addProcessor(fppSSAO);
  135.     }
  136.    
  137.     private void setupShadows() {
  138.         pssmRenderer = new PssmShadowRenderer(assetManager, 1024, 3);
  139.         pssmRenderer.setDirection(lightDirection.normalizeLocal());
  140.         viewPort.addProcessor(pssmRenderer);
  141.         rootNode.setShadowMode(ShadowMode.Off);
  142.     }
  143.    
  144.     private void setupLights() {
  145.         DirectionalLight dl = new DirectionalLight();
  146.         dl.setDirection(lightDirection.normalizeLocal());
  147.         dl.setColor(ColorRGBA.White);
  148.         rootNode.addLight(dl);
  149.        
  150.         AmbientLight al = new AmbientLight();
  151.         al.setColor(ColorRGBA.White.mult(0.2f));
  152.         rootNode.addLight(al);
  153.     }
  154.  
  155.     @Override
  156.     public void simpleUpdate(float tpf) {
  157.         rootNode.getChild("pivot").rotate(speedX*tpf, speedY*tpf, 0f);
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement