Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package mygame.state;
- import com.jme3.app.Application;
- import com.jme3.app.SimpleApplication;
- import com.jme3.app.state.AbstractAppState;
- import com.jme3.app.state.AppStateManager;
- import com.jme3.asset.AssetManager;
- import com.jme3.input.InputManager;
- import com.jme3.input.KeyInput;
- import com.jme3.input.controls.AnalogListener;
- import com.jme3.input.controls.KeyTrigger;
- 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.Camera;
- import com.jme3.renderer.queue.RenderQueue.ShadowMode;
- import com.jme3.scene.Geometry;
- import com.jme3.scene.Node;
- import com.jme3.scene.Spatial;
- import com.jme3.scene.shape.Box;
- import com.jme3.shadow.DirectionalLightShadowFilter;
- import com.jme3.shadow.DirectionalLightShadowRenderer;
- import com.jme3.terrain.heightmap.ImageBasedHeightMap;
- import com.jme3.water.WaterFilter;
- import mygame.util.Vector3Int;
- import mygame.voxel.BlockSettings;
- import mygame.voxel.BlockTerrainControl;
- import mygame.voxel.BlockTerrainListener;
- import mygame.voxel.ChunkControl;
- import mygame.voxel.StoneBlock;
- /**
- *
- * @author Stas4T
- */
- public class st_dev extends AbstractAppState{
- private final Node rootNode;
- private final Node localRootNode = new Node("st_dev");
- private final AssetManager assetManager;
- private final InputManager inputManager;
- private Spatial player;
- private Camera camera;
- private Vector3f cam_translate;
- private AppStateManager st_manager;
- private Application app_manager;
- private BlockTerrainControl blocks;
- private DirectionalLightShadowFilter dlsf;
- private DirectionalLight sun;
- private FilterPostProcessor fpp;
- public st_dev(SimpleApplication app){
- rootNode = app.getRootNode();
- assetManager = app.getAssetManager();
- inputManager = app.getInputManager();
- }
- @Override
- public void initialize(AppStateManager stManager, Application app){
- super.initialize(stManager, app);
- st_manager = stManager;
- app_manager = app;
- cam_translate = new Vector3f(0,0,0);
- rootNode.attachChild(localRootNode);
- Box b = new Box(1, 1, 1);
- Geometry geom = new Geometry("Box", b);
- Material mat = new Material(assetManager, "MatDefs/BlockyTexture.j3md");
- mat.setColor("Color", ColorRGBA.Blue);
- geom.setMaterial(mat);
- localRootNode.attachChild(geom);
- // player = localRootNode.getChild("Player");
- // playerControl = new CharacterControl();
- // playerControl.setCo
- // player.addControl(playerControl);
- camera = app.getCamera();
- // // TO FIX START //
- final BlockSettings blockSettings = new BlockSettings(this.app_manager);
- blockSettings.setChunkSize(new Vector3Int(16, 16, 16));
- blockSettings.setBlockSize(2);
- Material blockyMat = new Material(assetManager, "MatDefs/BlockyTexture.j3md");
- //Material blockyMat = assetManager.loadMaterial("Materials/BlockyTexture2.j3m");
- //blockyMat.setColor("m_Color", ColorRGBA.Green);
- blockSettings.setMaterial(blockyMat);
- blockSettings.setWorldSize(new Vector3Int(50, 10, 50));
- blockSettings.setViewDistance(200f);
- blocks = new BlockTerrainControl(blockSettings);
- //geom2.move(8, 0, 0);
- final Node terrain = new Node();
- terrain.addControl(blocks);
- blocks.registerBlock(new StoneBlock());
- ImageBasedHeightMap heightmap = new ImageBasedHeightMap(assetManager.loadTexture("Textures/test500x500.jpg").getImage(), .5f);
- heightmap.load();
- blocks.loadFromHeightMap(new Vector3Int(0, 0, 0),heightmap, StoneBlock.class);
- blocks.addListener(new BlockTerrainListener() {
- @Override
- public void onChunkUpdated(ChunkControl c) {
- // Geometry geom = c.getGeometry();
- //RigidBodyControl control = geom.getControl(RigidBodyControl.class);
- // if(control == null){
- // control = new RigidBodyControl(0);
- // geom.addControl(control);
- //bas.getPhysicsSpace().add(control);
- // }
- //control.setCollisionShape(new MeshCollisionShape(geom.getMesh()));
- }
- });
- sun = new DirectionalLight();
- sun.setDirection(new Vector3f(-5, -4, -4).normalizeLocal());
- localRootNode.addLight(sun);
- localRootNode.setShadowMode(ShadowMode.CastAndReceive);
- fpp = new FilterPostProcessor(assetManager);
- /* Drop shadows */
- final int SHADOWMAP_SIZE=2048;
- DirectionalLightShadowRenderer dlsr = new DirectionalLightShadowRenderer(assetManager, SHADOWMAP_SIZE, 3);
- dlsr.setLight(sun);
- app_manager.getViewPort().addProcessor(dlsr);
- dlsf = new DirectionalLightShadowFilter(assetManager, SHADOWMAP_SIZE, 3);
- dlsf.setShadowZExtend(2048f);
- //fpp.addFilter(dlsf);
- SSAOFilter ssaoFilter = new SSAOFilter(12.94f, 43.92f, 0.33f, 0.61f);
- fpp.addFilter(ssaoFilter);
- app_manager.getViewPort().addProcessor(fpp);
- app_manager.getViewPort().setBackgroundColor(ColorRGBA.Cyan);
- /*
- BitmapText hudText = new BitmapText(guiFont, false);
- hudText.setSize(guiFont.getCharSet().getRenderedSize()); // font size
- hudText.setColor(ColorRGBA.White); // font color
- hudText.setText("+"); // the text
- hudText.setLocalTranslation(this.settings.getWidth() / 2 - hudText.getLineWidth()/2, this.settings.getHeight() / 2 + hudText.getLineHeight(), 0); // position
- guiNode.attachChild(hudText);*/
- terrain.setLocalScale(2f);
- localRootNode.attachChild(terrain);
- //flyCam.setMoveSpeed(20);
- //viewPort.setBackgroundColor(ColorRGBA.Black);
- /*inputManager.addListener(new ActionListener() {
- int count = 50;
- int up = 50;
- int multiplier = 1;
- public void onAction(String name, boolean isPressed, float tpf) {
- if (!isPressed) {
- return;
- }
- CollisionResults results = new CollisionResults();
- Vector3f click3d = cam.getWorldCoordinates(new Vector2f(cam.getWidth() / 2, cam.getHeight() / 2), 0f).clone();
- Vector3f dir = cam.getWorldCoordinates(new Vector2f(cam.getWidth() / 2, cam.getHeight() / 2), 1f).subtractLocal(click3d).normalizeLocal();
- Ray ray = new Ray(click3d, dir);
- terrain.collideWith(ray, results);
- CollisionResult result = results.getClosestCollision();
- if (result != null) {
- Vector3f position = result.getContactPoint();
- Vector3Int blockPosition = blocks.getPointedBlockLocation(position, false);
- blocks.removeBlock(blockPosition);
- }
- }
- }, "toggle");*/
- // // TO FIX END
- //inputManager.addMapping("Pause", new KeyTrigger(KeyInput.KEY_P));
- //inputManager.addListener(analogListener, "Pause");
- inputManager.addMapping("Forth", new KeyTrigger(KeyInput.KEY_U));
- inputManager.addListener(analogListener, "Forth");
- inputManager.addMapping("Back", new KeyTrigger(KeyInput.KEY_J));
- inputManager.addListener(analogListener, "Back");
- inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_H));
- inputManager.addListener(analogListener, "Left");
- inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_K));
- inputManager.addListener(analogListener, "Right");
- inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_Y));
- inputManager.addListener(analogListener, "Up");
- inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_N));
- inputManager.addListener(analogListener, "Down");
- }
- private final AnalogListener analogListener = new AnalogListener() {
- @Override
- public void onAnalog(String name, float value, float tpf)
- {
- if(name.equals("Pause") && value>0){
- setEnabled(!isEnabled());
- }
- if(name.equals("Forth") && value>0){
- cam_translate = cam_translate.add(100.0f,0,0);
- }
- if(name.equals("Back") && value>0){
- cam_translate = cam_translate.add(-100.0f,0,0);
- }
- if(name.equals("Left") && value>0){
- cam_translate = cam_translate.add(0f,0,-100.0f);
- }
- if(name.equals("Right") && value>0){
- cam_translate = cam_translate.add(0f,0,100.0f);
- }
- if(name.equals("Up") && value>0){
- cam_translate = cam_translate.add(0f,100f,0f);
- }
- if(name.equals("Down") && value>0){
- cam_translate = cam_translate.add(0f,-100,0f);
- }
- //System.out.println("Camera X,Y,Z: " + cam_translate.getX()+", "+cam_translate.getY()+", "+cam_translate.getZ());
- }
- };
- @Override
- public void update(float tpf){
- camera.setLocation(camera.getLocation().add(cam_translate.mult(tpf)));
- Spatial geom = localRootNode.getChild("Box");
- if(geom !=null)
- {
- float speed = 1.0f;
- geom.rotate(speed*tpf, 0, 0);
- }
- cam_translate = new Vector3f();
- }
- @Override
- public void cleanup(){
- rootNode.detachChild(localRootNode);
- super.cleanup();
- }
- }
- /*
- //==============================//
- // BlockyTexture.j3md:
- //==============================//
- MaterialDef BlockyTexture {
- MaterialParameters {
- Vector4 Color
- }
- Technique {
- VertexShader GLSL330: Shaders/TerrainLOD.vert
- FragmentShader GLSL330: Shaders/TerrainLOD.frag
- WorldParameters {
- WorldViewProjectionMatrix
- LightDirection
- LightColor
- LightPosition
- inColor
- }
- }
- }
- //==============================//
- // TerrainLOD.vert
- //==============================//
- //the global uniform World view projection matrix
- //(more on global uniforms below)
- uniform mat4 g_WorldViewProjectionMatrix;
- //The attribute inPosition is the Object space position of the vertex
- attribute vec3 inPosition;
- void main(){
- gl_Position = g_WorldViewProjectionMatrix * vec4(inPosition, 1.0);
- }
- //==============================//
- // TerrainLOD.frag
- //==============================//
- uniform vec4 u_LightColor;
- uniform vec4 u_LightDirection;
- uniform vec4 u_LightPosition;
- uniform vec4 inColor;
- void main(){
- //returning the color of the pixel (here solid blue)
- //- gl_FragColor is the standard GLSL variable that holds the pixel
- //color. It must be filled in the Fragment Shader.
- vec3 color = inColor.xyz+vec3(0.1,0.75,0.1)+u_LightColor.xyz;
- //gl_FragColor = vec4(0.0,1.0,0.0, 1.0);
- gl_FragColor = vec4(color, 0.50);
- //gl_FragColor=gl_FragColor;
- }
- //==============================//
- */
Advertisement
Add Comment
Please, Sign In to add comment