Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package mygame;
- import com.jme3.app.SimpleApplication;
- import com.jme3.input.controls.ActionListener;
- import com.jme3.light.AmbientLight;
- import com.jme3.light.DirectionalLight;
- import com.jme3.material.Material;
- import com.jme3.math.ColorRGBA;
- import com.jme3.math.Vector2f;
- import com.jme3.math.Vector3f;
- import com.jme3.renderer.RenderManager;
- import com.jme3.renderer.ViewPort;
- import com.jme3.scene.control.AbstractControl;
- import com.jme3.terrain.geomipmap.TerrainLodControl;
- import com.jme3.terrain.geomipmap.TerrainQuad;
- import com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator;
- import com.jme3.terrain.heightmap.AbstractHeightMap;
- import com.jme3.terrain.heightmap.ImageBasedHeightMap;
- import com.jme3.texture.Texture;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class BugReport extends SimpleApplication
- implements ActionListener {
- private TerrainQuad terrain;
- Material matTerrain;
- private float grassScale = 64;
- private float dirtScale = 16;
- private float rockScale = 128;
- public static void main(String[] args) {
- BugReport app = new BugReport();
- app.setShowSettings(false);
- app.start();
- }
- public void simpleInitApp() {
- createTerrain();
- DirectionalLight light = new DirectionalLight();
- light.setDirection((new Vector3f(-0.5f, -1f, -0.5f)).normalize());
- rootNode.addLight(light);
- AmbientLight ambLight = new AmbientLight();
- ambLight.setColor(new ColorRGBA(1f, 1f, 0.8f, 0.2f));
- rootNode.addLight(ambLight);
- cam.setLocation(new Vector3f(0,-60f,0));
- new Thread(new Runnable(){public void run(){
- try {
- Thread.sleep(1000);
- } catch (InterruptedException ex) {
- Logger.getLogger(BugReport.class.getName()).log(Level.SEVERE, null, ex);
- }
- terrain.addControl(new RullalControl(new Vector3f(-0.1f,-0.5f,0f), terrain,BugReport.this));
- }}).start();
- }
- public void onAction(String binding, boolean isPressed, float tpf) {
- }
- @Override
- public void simpleUpdate(float tpf) {
- }
- public void rulla(Vector3f loc){
- }
- private void createTerrain() {
- // First, we load up our textures and the heightmap texture for the terrain
- // TERRAIN TEXTURE material
- matTerrain = new Material(assetManager, "Common/MatDefs/Terrain/TerrainLighting.j3md");
- matTerrain.setBoolean("useTriPlanarMapping", false);
- matTerrain.setBoolean("WardIso", true);
- matTerrain.setFloat("Shininess", 0);
- // ALPHA map (for splat textures)
- matTerrain.setTexture("AlphaMap", assetManager.loadTexture("Textures/Terrain/splat/alphamap.png"));
- // GRASS texture
- Texture grass = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");
- grass.setWrap(Texture.WrapMode.Repeat);
- matTerrain.setTexture("DiffuseMap", grass);
- matTerrain.setFloat("DiffuseMap_0_scale", grassScale);
- // DIRT texture
- Texture dirt = assetManager.loadTexture("Textures/Terrain/splat/dirt.jpg");
- dirt.setWrap(Texture.WrapMode.Repeat);
- matTerrain.setTexture("DiffuseMap_1", dirt);
- matTerrain.setFloat("DiffuseMap_1_scale", dirtScale);
- // ROCK texture
- Texture rock = assetManager.loadTexture("Textures/Terrain/splat/road.jpg");
- rock.setWrap(Texture.WrapMode.Repeat);
- matTerrain.setTexture("DiffuseMap_2", rock);
- matTerrain.setFloat("DiffuseMap_2_scale", rockScale);
- // HEIGHTMAP image (for the terrain heightmap)
- Texture heightMapImage = assetManager.loadTexture("Textures/Terrain/splat/mountains512.png");
- AbstractHeightMap heightmap = null;
- try {
- heightmap = new ImageBasedHeightMap(heightMapImage.getImage(), 0.5f);
- heightmap.load();
- heightmap.smooth(0.9f, 1);
- } catch (Exception e) {
- e.printStackTrace();
- }
- // CREATE THE TERRAIN
- terrain = new TerrainQuad("terrain", 65, 513, heightmap.getHeightMap());
- TerrainLodControl control = new TerrainLodControl(terrain, getCamera());
- control.setLodCalculator( new DistanceLodCalculator(65, 2.7f) ); // patch size, and a multiplier
- terrain.addControl(control);
- terrain.setMaterial(matTerrain);
- terrain.setLocalTranslation(0, -100, 0);
- terrain.setLocalScale(2.5f, 0.5f, 2.5f);
- rootNode.attachChild(terrain);
- }
- class RullalControl extends AbstractControl{
- Vector3f loc;
- TerrainQuad terrain;
- BugReport app;
- float radius=64;
- float tpf=0;
- List<Vector2f> locs = new ArrayList<Vector2f>();
- List<Float> heights = new ArrayList<Float>();
- float height=10;
- public RullalControl(Vector3f loc, TerrainQuad terrain,BugReport app){
- this.loc=loc;
- this.terrain=terrain;
- this.app=app;
- //start = System.currentTimeMillis();
- }
- @Override
- protected void controlUpdate(float tpf) {
- //long tTime=System.currentTimeMillis()-start;
- //radius-=(tpf);
- // offset it by radius because in the loop we iterate through 2 radii
- int radiusStepsX = (int) (radius / terrain.getLocalScale().x);
- int radiusStepsZ = (int) (radius / terrain.getLocalScale().z);
- float xStepAmount = terrain.getLocalScale().x;
- float zStepAmount = terrain.getLocalScale().z;
- for (int z = -radiusStepsZ; z < radiusStepsZ; z++) {
- for (int x = -radiusStepsX; x < radiusStepsX; x++) {
- float locX = loc.x + (x * xStepAmount);
- float locZ = loc.z + (z * zStepAmount);
- // see if it is in the radius of the tool
- float hLocal=terrain.getHeight(new Vector2f(locX, locZ));
- if ((hLocal-height)>=1){
- heights.add(hLocal-0.0001f);
- }
- else if((hLocal-height)<0){
- heights.add(hLocal+0.0001f);
- }
- else heights.add(height);
- //float h = calculateHeightRulla(radius, height, locX - loc.x, locZ - loc.z);
- locs.add(new Vector2f(locX, locZ));
- //heights.add(height);
- }
- }
- terrain.setHeight(locs, heights);
- //System.out.println("Modified "+locs.size()+" points, took: " + (System.currentTimeMillis() - start)+" ms");
- heights.clear();
- locs.clear();
- terrain.updateModelBound();
- this.tpf+=tpf;
- //if (this.tpf>5){
- spatial.removeControl(this);
- //}
- }
- @Override
- protected void controlRender(RenderManager rm, ViewPort vp) {
- //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment