TerrainGridListener is extended to be able to change material of individual grid files.

Also added new grid test-data, cause it somehow disappeared

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7611 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
ant..om 14 years ago
parent 3746238df3
commit 2073f58cf8
  1. 18
      engine/src/terrain/com/jme3/terrain/geomipmap/TerrainGrid.java
  2. 3
      engine/src/terrain/com/jme3/terrain/geomipmap/TerrainGridListener.java
  3. BIN
      engine/src/test-data/Textures/Terrain/grid/alphamap_0_0.png
  4. BIN
      engine/src/test-data/Textures/Terrain/grid/alphamap_0_512.png
  5. BIN
      engine/src/test-data/Textures/Terrain/grid/alphamap_512_0.png
  6. BIN
      engine/src/test-data/Textures/Terrain/grid/alphamap_512_512.png
  7. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_-1_-1.png
  8. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_-1_-2.png
  9. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_-1_0.png
  10. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_-1_1.png
  11. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_-1_2.png
  12. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_-1_3.png
  13. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_-1_4.png
  14. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_0_-1.png
  15. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_0_-2.png
  16. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_0_-3.png
  17. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_0_0.png
  18. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_0_1.png
  19. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_0_2.png
  20. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_0_3.png
  21. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_0_4.png
  22. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_1_-1.png
  23. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_1_-2.png
  24. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_1_-3.png
  25. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_1_0.png
  26. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_1_1.png
  27. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_1_2.png
  28. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_1_3.png
  29. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_1_4.png
  30. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_2_-1.png
  31. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_2_-2.png
  32. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_2_-3.png
  33. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_2_0.png
  34. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_2_1.png
  35. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_2_2.png
  36. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_2_3.png
  37. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_2_4.png
  38. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_3_-1.png
  39. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_3_-2.png
  40. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_3_-3.png
  41. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_3_0.png
  42. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_3_1.png
  43. BIN
      engine/src/test-data/Textures/Terrain/grid/terrain_3_2.png
  44. 258
      engine/src/test/jme3test/terrain/TerrainGridAlphaMapTest.java

@ -61,6 +61,11 @@ public class TerrainGrid extends TerrainQuad {
if (cache.get(temp) == null) {
HeightMap heightMapAt = heightMapGrid.getHeightMapAt(temp);
TerrainQuad q = new TerrainQuad(getName() + "Quad" + temp, patchSize, quadSize, heightMapAt == null ? null : heightMapAt.getHeightMap(), lodCalculatorFactory);
Material mat = material.clone();
for (TerrainGridListener l : listeners.values()) {
mat = l.tileLoaded(mat, temp);
}
q.setMaterial(mat);
cache.put(temp, q);
}
}
@ -95,8 +100,6 @@ public class TerrainGrid extends TerrainQuad {
new Vector3f(0, 0, -1), new Vector3f(0, 0, 0), new Vector3f(0, 0, 1), new Vector3f(0, 0, 2),
new Vector3f(1, 0, -1), new Vector3f(1, 0, 0), new Vector3f(1, 0, 1), new Vector3f(1, 0, 2),
new Vector3f(2, 0, -1), new Vector3f(2, 0, 0), new Vector3f(2, 0, 1), new Vector3f(2, 0, 2)};
updateChildrens(Vector3f.ZERO);
}
public TerrainGrid(String name, int patchSize, int size, Vector3f scale, HeightMapGrid heightMapGrid,
@ -115,6 +118,14 @@ public class TerrainGrid extends TerrainQuad {
public TerrainGrid() {
}
public void initialize(Vector3f location) {
Vector3f camCell = this.getCell(location);
this.updateChildrens(camCell);
for (TerrainGridListener l : this.listeners.values()) {
l.gridMoved(camCell);
}
}
@Override
public void update(List<Vector3f> locations) {
// for now, only the first camera is handled.
@ -152,7 +163,7 @@ public class TerrainGrid extends TerrainQuad {
}
protected void attachQuadAt(TerrainQuad q, int quadrant) {
q.setMaterial(this.material);
//q.setMaterial(this.material);
q.setLocalTranslation(quadOrigins[quadrant - 1]);
q.setQuadrant((short) quadrant);
this.attachChild(q);
@ -223,6 +234,7 @@ public class TerrainGrid extends TerrainQuad {
this.removeQuad(2);
this.removeQuad(3);
this.removeQuad(4);
attachQuadAt(q1, 1);
attachQuadAt(q2, 2);
attachQuadAt(q3, 3);

@ -4,6 +4,7 @@
*/
package com.jme3.terrain.geomipmap;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
/**
@ -14,4 +15,6 @@ public interface TerrainGridListener {
public void gridMoved(Vector3f newCenter);
public Material tileLoaded(Material material, Vector3f cell);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 520 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 708 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 708 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 702 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 452 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 447 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 462 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 467 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 464 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 451 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 444 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 446 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 449 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 452 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 440 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 449 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 443 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 449 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 450 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 444 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 451 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 444 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 443 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 450 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 443 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 442 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 446 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 458 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 444 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 442 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 444 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 442 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 450 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 454 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 446 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 436 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 456 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 453 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 453 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 444 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 448 KiB

@ -0,0 +1,258 @@
package jme3test.terrain;
import java.util.ArrayList;
import java.util.List;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.ScreenshotAppState;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.HeightfieldCollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.terrain.geomipmap.TerrainGrid;
import com.jme3.terrain.geomipmap.TerrainGridListener;
import com.jme3.terrain.geomipmap.TerrainLodControl;
import com.jme3.terrain.geomipmap.TerrainQuad;
import com.jme3.terrain.heightmap.FractalHeightMapGrid;
import com.jme3.terrain.heightmap.ImageBasedHeightMapGrid;
import com.jme3.terrain.heightmap.Namer;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture.WrapMode;
import org.novyon.noise.ShaderUtils;
import org.novyon.noise.basis.FilteredBasis;
import org.novyon.noise.filter.IterativeFilter;
import org.novyon.noise.filter.OptimizedErode;
import org.novyon.noise.filter.PerturbFilter;
import org.novyon.noise.filter.SmoothFilter;
import org.novyon.noise.fractal.FractalSum;
import org.novyon.noise.modulator.NoiseModulator;
public class TerrainGridAlphaMapTest extends SimpleApplication {
private TerrainGrid terrain;
private float grassScale = 64;
private float dirtScale = 16;
private float rockScale = 128;
private boolean usePhysics = true;
public static void main(final String[] args) {
TerrainGridAlphaMapTest app = new TerrainGridAlphaMapTest();
app.start();
}
private CharacterControl player3;
private FractalSum base;
private PerturbFilter perturb;
private OptimizedErode therm;
private SmoothFilter smooth;
private IterativeFilter iterate;
private Material matRock;
private Material matWire;
@Override
public void simpleInitApp() {
this.flyCam.setMoveSpeed(100f);
ScreenshotAppState state = new ScreenshotAppState();
this.stateManager.attach(state);
// TERRAIN TEXTURE material
matRock = new Material(assetManager, "Common/MatDefs/Terrain/Terrain.j3md");
matRock.setBoolean("useTriPlanarMapping", false);
// GRASS texture
Texture grass = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");
grass.setWrap(WrapMode.Repeat);
matRock.setTexture("Tex1", grass);
matRock.setFloat("Tex1Scale", grassScale);
// DIRT texture
Texture dirt = assetManager.loadTexture("Textures/Terrain/splat/dirt.jpg");
dirt.setWrap(WrapMode.Repeat);
matRock.setTexture("Tex2", dirt);
matRock.setFloat("Tex2Scale", dirtScale);
// ROCK texture
Texture rock = assetManager.loadTexture("Textures/Terrain/splat/road.jpg");
rock.setWrap(WrapMode.Repeat);
matRock.setTexture("Tex3", rock);
matRock.setFloat("Tex3Scale", rockScale);
// WIREFRAME material
matWire = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
matWire.getAdditionalRenderState().setWireframe(true);
matWire.setColor("Color", ColorRGBA.Green);
this.base = new FractalSum();
this.base.setRoughness(0.7f);
this.base.setFrequency(1.0f);
this.base.setAmplitude(1.0f);
this.base.setLacunarity(2.12f);
this.base.setOctaves(8);
this.base.setScale(0.02125f);
this.base.addModulator(new NoiseModulator() {
@Override
public float value(float... in) {
return ShaderUtils.clamp(in[0] * 0.5f + 0.5f, 0, 1);
}
});
FilteredBasis ground = new FilteredBasis(this.base);
this.perturb = new PerturbFilter();
this.perturb.setMagnitude(0.119f);
this.therm = new OptimizedErode();
this.therm.setRadius(5);
this.therm.setTalus(0.011f);
this.smooth = new SmoothFilter();
this.smooth.setRadius(1);
this.smooth.setEffect(0.7f);
this.iterate = new IterativeFilter();
this.iterate.addPreFilter(this.perturb);
this.iterate.addPostFilter(this.smooth);
this.iterate.setFilter(this.therm);
this.iterate.setIterations(1);
ground.addPreFilter(this.iterate);
this.terrain = new TerrainGrid("terrain", 65, 1025, new ImageBasedHeightMapGrid(assetManager, new Namer() {
public String getName(int x, int y) {
return "Textures/Terrain/grid/terrain_" + x + "_" + y + ".png";
}
}));
this.terrain.setMaterial(this.matRock);
this.terrain.addListener("alphaListener", new TerrainGridListener() {
public void gridMoved(Vector3f newCenter) {
}
public Material tileLoaded(Material material, Vector3f cell) {
material.setTexture("Alpha", assetManager.loadTexture("Textures/Terrain/grid/alphamap_" + (int)Math.abs(512 * (cell.x % 2)) + "_" + (int)Math.abs(512 * (cell.z % 2)) + ".png"));
return material;
}
});
this.terrain.setLocalTranslation(0, 0, 0);
this.terrain.setLocalScale(2f, 1f, 2f);
this.terrain.initialize(Vector3f.ZERO);
this.rootNode.attachChild(this.terrain);
List<Camera> cameras = new ArrayList<Camera>();
cameras.add(this.getCamera());
TerrainLodControl control = new TerrainLodControl(this.terrain, cameras);
this.terrain.addControl(control);
BulletAppState bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
this.getCamera().setLocation(new Vector3f(0, 256, 0));
this.viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));
if (usePhysics) {
RigidBodyControl body = new RigidBodyControl(new HeightfieldCollisionShape(terrain.getHeightMap(), terrain.getLocalScale()), 0);
terrain.addControl(body);
bulletAppState.getPhysicsSpace().add(terrain);
CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(0.5f, 1.8f, 1);
this.player3 = new CharacterControl(capsuleShape, 0.5f);
this.player3.setJumpSpeed(20);
this.player3.setFallSpeed(30);
this.player3.setGravity(30);
this.player3.setPhysicsLocation(new Vector3f(0, 256, 0));
bulletAppState.getPhysicsSpace().add(this.player3);
}
this.initKeys();
}
private void initKeys() {
// You can map one or several inputs to one named action
this.inputManager.addMapping("Lefts", new KeyTrigger(KeyInput.KEY_A));
this.inputManager.addMapping("Rights", new KeyTrigger(KeyInput.KEY_D));
this.inputManager.addMapping("Ups", new KeyTrigger(KeyInput.KEY_W));
this.inputManager.addMapping("Downs", new KeyTrigger(KeyInput.KEY_S));
this.inputManager.addMapping("Jumps", new KeyTrigger(KeyInput.KEY_SPACE));
this.inputManager.addListener(this.actionListener, "Lefts");
this.inputManager.addListener(this.actionListener, "Rights");
this.inputManager.addListener(this.actionListener, "Ups");
this.inputManager.addListener(this.actionListener, "Downs");
this.inputManager.addListener(this.actionListener, "Jumps");
}
private boolean left;
private boolean right;
private boolean up;
private boolean down;
private final ActionListener actionListener = new ActionListener() {
@Override
public void onAction(final String name, final boolean keyPressed, final float tpf) {
if (name.equals("Lefts")) {
if (keyPressed) {
TerrainGridAlphaMapTest.this.left = true;
} else {
TerrainGridAlphaMapTest.this.left = false;
}
} else if (name.equals("Rights")) {
if (keyPressed) {
TerrainGridAlphaMapTest.this.right = true;
} else {
TerrainGridAlphaMapTest.this.right = false;
}
} else if (name.equals("Ups")) {
if (keyPressed) {
TerrainGridAlphaMapTest.this.up = true;
} else {
TerrainGridAlphaMapTest.this.up = false;
}
} else if (name.equals("Downs")) {
if (keyPressed) {
TerrainGridAlphaMapTest.this.down = true;
} else {
TerrainGridAlphaMapTest.this.down = false;
}
} else if (name.equals("Jumps")) {
TerrainGridAlphaMapTest.this.player3.jump();
}
}
};
private final Vector3f walkDirection = new Vector3f();
@Override
public void simpleUpdate(final float tpf) {
Vector3f camDir = this.cam.getDirection().clone().multLocal(0.6f);
Vector3f camLeft = this.cam.getLeft().clone().multLocal(0.4f);
this.walkDirection.set(0, 0, 0);
if (this.left) {
this.walkDirection.addLocal(camLeft);
}
if (this.right) {
this.walkDirection.addLocal(camLeft.negate());
}
if (this.up) {
this.walkDirection.addLocal(camDir);
}
if (this.down) {
this.walkDirection.addLocal(camDir.negate());
}
if (usePhysics) {
this.player3.setWalkDirection(this.walkDirection);
this.cam.setLocation(this.player3.getPhysicsLocation());
}
}
}
Loading…
Cancel
Save