Guest User

badcode

a guest
Apr 27th, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.33 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package mygame.state;
  7.  
  8. import com.jme3.app.Application;
  9. import com.jme3.app.SimpleApplication;
  10. import com.jme3.app.state.AbstractAppState;
  11. import com.jme3.app.state.AppStateManager;
  12. import com.jme3.asset.AssetManager;
  13. import com.jme3.input.InputManager;
  14. import com.jme3.input.KeyInput;
  15. import com.jme3.input.controls.AnalogListener;
  16. import com.jme3.input.controls.KeyTrigger;
  17. import com.jme3.light.DirectionalLight;
  18. import com.jme3.material.Material;
  19. import com.jme3.math.ColorRGBA;
  20. import com.jme3.math.Vector3f;
  21. import com.jme3.post.FilterPostProcessor;
  22. import com.jme3.post.ssao.SSAOFilter;
  23. import com.jme3.renderer.Camera;
  24. import com.jme3.renderer.queue.RenderQueue.ShadowMode;
  25. import com.jme3.scene.Geometry;
  26. import com.jme3.scene.Node;
  27. import com.jme3.scene.Spatial;
  28. import com.jme3.scene.shape.Box;
  29. import com.jme3.shadow.DirectionalLightShadowFilter;
  30. import com.jme3.shadow.DirectionalLightShadowRenderer;
  31. import com.jme3.terrain.heightmap.ImageBasedHeightMap;
  32. import com.jme3.water.WaterFilter;
  33. import mygame.util.Vector3Int;
  34. import mygame.voxel.BlockSettings;
  35. import mygame.voxel.BlockTerrainControl;
  36. import mygame.voxel.BlockTerrainListener;
  37. import mygame.voxel.ChunkControl;
  38. import mygame.voxel.StoneBlock;
  39.  
  40. /**
  41.  *
  42.  * @author Stas4T
  43.  */
  44. public class st_dev extends AbstractAppState{
  45.  
  46.     private final Node rootNode;
  47.     private final Node localRootNode = new Node("st_dev");
  48.     private final AssetManager assetManager;
  49.     private final InputManager inputManager;
  50.     private Spatial player;
  51.     private Camera camera;
  52.     private Vector3f cam_translate;
  53.     private AppStateManager st_manager;
  54.     private Application app_manager;
  55.     private BlockTerrainControl blocks;
  56.     private DirectionalLightShadowFilter dlsf;
  57.     private DirectionalLight sun;
  58.     private FilterPostProcessor fpp;
  59.    
  60.     public st_dev(SimpleApplication app){
  61.         rootNode = app.getRootNode();
  62.         assetManager = app.getAssetManager();
  63.         inputManager = app.getInputManager();
  64.     }
  65.     @Override
  66.     public void initialize(AppStateManager stManager, Application app){
  67.         super.initialize(stManager, app);
  68.        
  69.         st_manager = stManager;
  70.         app_manager = app;
  71.        
  72.         cam_translate = new Vector3f(0,0,0);
  73.        
  74.         rootNode.attachChild(localRootNode);
  75.        
  76.         Box b = new Box(1, 1, 1);
  77.         Geometry geom = new Geometry("Box", b);
  78.         Material mat = new Material(assetManager, "MatDefs/BlockyTexture.j3md");
  79.         mat.setColor("Color", ColorRGBA.Blue);
  80.         geom.setMaterial(mat);
  81.         localRootNode.attachChild(geom);
  82.        
  83. //        player = localRootNode.getChild("Player");
  84. //        playerControl = new CharacterControl();
  85. //        playerControl.setCo
  86. //        player.addControl(playerControl);
  87.  
  88.         camera = app.getCamera();
  89.        
  90.         //                                      // TO FIX START //
  91.  
  92.        
  93.         final BlockSettings blockSettings = new BlockSettings(this.app_manager);
  94.         blockSettings.setChunkSize(new Vector3Int(16, 16, 16));
  95.         blockSettings.setBlockSize(2);
  96.         Material  blockyMat = new Material(assetManager, "MatDefs/BlockyTexture.j3md");
  97.         //Material blockyMat = assetManager.loadMaterial("Materials/BlockyTexture2.j3m");
  98.         //blockyMat.setColor("m_Color", ColorRGBA.Green);
  99.         blockSettings.setMaterial(blockyMat);
  100.         blockSettings.setWorldSize(new Vector3Int(50, 10, 50));
  101.         blockSettings.setViewDistance(200f);
  102.         blocks = new BlockTerrainControl(blockSettings);
  103.  
  104.        
  105.         //geom2.move(8, 0, 0);
  106.         final Node terrain = new Node();
  107.         terrain.addControl(blocks);
  108.        
  109.         blocks.registerBlock(new StoneBlock());
  110.  
  111.         ImageBasedHeightMap heightmap = new ImageBasedHeightMap(assetManager.loadTexture("Textures/test500x500.jpg").getImage(), .5f);
  112.         heightmap.load();
  113.  
  114.         blocks.loadFromHeightMap(new Vector3Int(0, 0, 0),heightmap, StoneBlock.class);
  115.        
  116.         blocks.addListener(new BlockTerrainListener() {
  117.            
  118.             @Override
  119.             public void onChunkUpdated(ChunkControl c) {
  120.                // Geometry geom = c.getGeometry();
  121.                 //RigidBodyControl control = geom.getControl(RigidBodyControl.class);
  122.                // if(control == null){
  123.                   //  control = new RigidBodyControl(0);
  124.                   //  geom.addControl(control);
  125.                     //bas.getPhysicsSpace().add(control);
  126.                // }
  127.                 //control.setCollisionShape(new MeshCollisionShape(geom.getMesh()));
  128.                
  129.             }
  130.         });
  131.        
  132.          sun = new DirectionalLight();
  133.         sun.setDirection(new Vector3f(-5, -4, -4).normalizeLocal());
  134.         localRootNode.addLight(sun);
  135.        
  136.         localRootNode.setShadowMode(ShadowMode.CastAndReceive);
  137.        
  138.          fpp = new FilterPostProcessor(assetManager);
  139.  
  140.        
  141.         /* Drop shadows */
  142.         final int SHADOWMAP_SIZE=2048;
  143.         DirectionalLightShadowRenderer dlsr = new DirectionalLightShadowRenderer(assetManager, SHADOWMAP_SIZE, 3);
  144.         dlsr.setLight(sun);
  145.         app_manager.getViewPort().addProcessor(dlsr);
  146.  
  147.          dlsf = new DirectionalLightShadowFilter(assetManager, SHADOWMAP_SIZE, 3);
  148.          dlsf.setShadowZExtend(2048f);
  149.        
  150.         //fpp.addFilter(dlsf);
  151.        
  152.         SSAOFilter ssaoFilter = new SSAOFilter(12.94f, 43.92f, 0.33f, 0.61f);
  153.         fpp.addFilter(ssaoFilter);
  154.        
  155.        
  156.        
  157.         app_manager.getViewPort().addProcessor(fpp);
  158.         app_manager.getViewPort().setBackgroundColor(ColorRGBA.Cyan);
  159.         /*
  160.         BitmapText hudText = new BitmapText(guiFont, false);
  161.         hudText.setSize(guiFont.getCharSet().getRenderedSize());      // font size
  162.         hudText.setColor(ColorRGBA.White);                             // font color
  163.         hudText.setText("+");             // the text
  164.         hudText.setLocalTranslation(this.settings.getWidth() / 2 - hudText.getLineWidth()/2, this.settings.getHeight() / 2 + hudText.getLineHeight(), 0); // position
  165.        
  166.        
  167.         guiNode.attachChild(hudText);*/
  168.         terrain.setLocalScale(2f);
  169.         localRootNode.attachChild(terrain);
  170.        
  171.        
  172.         //flyCam.setMoveSpeed(20);
  173.         //viewPort.setBackgroundColor(ColorRGBA.Black);
  174.        /*inputManager.addListener(new ActionListener() {
  175.             int count = 50;
  176.             int up = 50;
  177.             int multiplier = 1;
  178.  
  179.             public void onAction(String name, boolean isPressed, float tpf) {
  180.                 if (!isPressed) {
  181.                     return;
  182.                 }
  183.                 CollisionResults results = new CollisionResults();
  184.                
  185.                 Vector3f click3d = cam.getWorldCoordinates(new Vector2f(cam.getWidth() / 2, cam.getHeight() / 2), 0f).clone();
  186.                 Vector3f dir = cam.getWorldCoordinates(new Vector2f(cam.getWidth() / 2, cam.getHeight() / 2), 1f).subtractLocal(click3d).normalizeLocal();
  187.                
  188.                 Ray ray = new Ray(click3d, dir);
  189.                
  190.                 terrain.collideWith(ray, results);
  191.                
  192.                 CollisionResult result = results.getClosestCollision();
  193.                 if (result != null) {
  194.                     Vector3f position = result.getContactPoint();
  195.                     Vector3Int blockPosition = blocks.getPointedBlockLocation(position, false);
  196.                     blocks.removeBlock(blockPosition);
  197.                 }
  198.  
  199.  
  200.             }
  201.         }, "toggle");*/
  202.         //                                      // TO FIX END
  203.        
  204.        
  205.         //inputManager.addMapping("Pause", new KeyTrigger(KeyInput.KEY_P));
  206.         //inputManager.addListener(analogListener, "Pause");
  207.        
  208.         inputManager.addMapping("Forth", new KeyTrigger(KeyInput.KEY_U));
  209.         inputManager.addListener(analogListener, "Forth");
  210.        
  211.                 inputManager.addMapping("Back", new KeyTrigger(KeyInput.KEY_J));
  212.         inputManager.addListener(analogListener, "Back");
  213.        
  214.                 inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_H));
  215.         inputManager.addListener(analogListener, "Left");
  216.        
  217.                 inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_K));
  218.         inputManager.addListener(analogListener, "Right");      
  219.        
  220.                 inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_Y));
  221.         inputManager.addListener(analogListener, "Up");
  222.        
  223.                 inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_N));
  224.         inputManager.addListener(analogListener, "Down");
  225.     }
  226.    
  227.         private final AnalogListener analogListener = new AnalogListener() {
  228.        
  229.                 @Override
  230.                 public void onAnalog(String name, float value, float tpf)
  231.                 {
  232.                     if(name.equals("Pause") && value>0){
  233.                         setEnabled(!isEnabled());
  234.                     }
  235.                    
  236.                     if(name.equals("Forth") && value>0){
  237.                         cam_translate = cam_translate.add(100.0f,0,0);
  238.                     }
  239.                      if(name.equals("Back") && value>0){
  240.                         cam_translate = cam_translate.add(-100.0f,0,0);
  241.                     }
  242.                       if(name.equals("Left") && value>0){
  243.                         cam_translate = cam_translate.add(0f,0,-100.0f);
  244.                     }
  245.                        if(name.equals("Right") && value>0){
  246.                         cam_translate = cam_translate.add(0f,0,100.0f);
  247.                     }
  248.                                if(name.equals("Up") && value>0){
  249.                         cam_translate = cam_translate.add(0f,100f,0f);
  250.                     }
  251.                        if(name.equals("Down") && value>0){
  252.                         cam_translate = cam_translate.add(0f,-100,0f);
  253.                     }
  254.                        
  255.                        //System.out.println("Camera X,Y,Z: " + cam_translate.getX()+", "+cam_translate.getY()+", "+cam_translate.getZ());
  256.             }
  257.     };
  258.    
  259.     @Override
  260.     public void update(float tpf){
  261.        
  262.         camera.setLocation(camera.getLocation().add(cam_translate.mult(tpf)));
  263.      
  264.        
  265.         Spatial geom = localRootNode.getChild("Box");
  266.         if(geom !=null)
  267.         {
  268.             float speed = 1.0f;
  269.             geom.rotate(speed*tpf, 0, 0);
  270.         }
  271.  
  272.        
  273.         cam_translate = new Vector3f();
  274.     }
  275.    
  276.     @Override
  277.     public void cleanup(){
  278.         rootNode.detachChild(localRootNode);
  279.        
  280.         super.cleanup();
  281.     }
  282. }
  283. /*
  284. //==============================//
  285. //     BlockyTexture.j3md:
  286. //==============================//
  287. MaterialDef BlockyTexture {
  288.  
  289.     MaterialParameters {
  290.         Vector4 Color
  291.     }
  292.     Technique {
  293.         VertexShader GLSL330:   Shaders/TerrainLOD.vert
  294.         FragmentShader GLSL330: Shaders/TerrainLOD.frag
  295.  
  296.         WorldParameters {
  297.             WorldViewProjectionMatrix
  298.             LightDirection
  299.             LightColor
  300.             LightPosition
  301.             inColor
  302.         }
  303.     }
  304. }
  305. //==============================//
  306. //     TerrainLOD.vert
  307. //==============================//
  308. //the global uniform World view projection matrix
  309. //(more on global uniforms below)
  310. uniform mat4 g_WorldViewProjectionMatrix;
  311. //The attribute inPosition is the Object space position of the vertex
  312. attribute vec3 inPosition;
  313. void main(){
  314.  
  315.     gl_Position = g_WorldViewProjectionMatrix * vec4(inPosition, 1.0);
  316. }
  317. //==============================//
  318. //     TerrainLOD.frag
  319. //==============================//
  320. uniform vec4 u_LightColor;
  321. uniform vec4 u_LightDirection;
  322. uniform vec4 u_LightPosition;
  323. uniform vec4 inColor;
  324.  
  325. void main(){
  326.     //returning the color of the pixel (here solid blue)
  327.     //- gl_FragColor is the standard GLSL variable that holds the pixel
  328.     //color. It must be filled in the Fragment Shader.
  329.     vec3 color = inColor.xyz+vec3(0.1,0.75,0.1)+u_LightColor.xyz;
  330.     //gl_FragColor = vec4(0.0,1.0,0.0, 1.0);
  331.     gl_FragColor = vec4(color, 0.50);
  332.     //gl_FragColor=gl_FragColor;
  333. }
  334. //==============================//
  335. */
Advertisement
Add Comment
Please, Sign In to add comment