use of com.jme3.terrain.heightmap.HeightMap in project jmonkeyengine by jMonkeyEngine.
the class ImageUtils method convertToNormalMapTexture.
/**
* This method converts the given texture into normal-map texture.
*
* @param source
* the source texture
* @param strengthFactor
* the normal strength factor
* @return normal-map texture
*/
public static Image convertToNormalMapTexture(Image source, float strengthFactor) {
BufferedImage sourceImage = ImageToAwt.convert(source, false, false, 0);
BufferedImage heightMap = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
BufferedImage bumpMap = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
ColorConvertOp gscale = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
gscale.filter(sourceImage, heightMap);
Vector3f S = new Vector3f();
Vector3f T = new Vector3f();
Vector3f N = new Vector3f();
for (int x = 0; x < bumpMap.getWidth(); ++x) {
for (int y = 0; y < bumpMap.getHeight(); ++y) {
// generating bump pixel
S.x = 1;
S.y = 0;
S.z = strengthFactor * ImageUtils.getHeight(heightMap, x + 1, y) - strengthFactor * ImageUtils.getHeight(heightMap, x - 1, y);
T.x = 0;
T.y = 1;
T.z = strengthFactor * ImageUtils.getHeight(heightMap, x, y + 1) - strengthFactor * ImageUtils.getHeight(heightMap, x, y - 1);
float den = (float) Math.sqrt(S.z * S.z + T.z * T.z + 1);
N.x = -S.z;
N.y = -T.z;
N.z = 1;
N.divideLocal(den);
// setting thge pixel in the result image
bumpMap.setRGB(x, y, ImageUtils.vectorToColor(N.x, N.y, N.z));
}
}
return ImageUtils.toJmeImage(bumpMap, source.getFormat());
}
use of com.jme3.terrain.heightmap.HeightMap in project jmonkeyengine by jMonkeyEngine.
the class TestFog method createTerrain.
private void createTerrain(Node rootNode) {
Material matRock = new Material(assetManager, "Common/MatDefs/Terrain/TerrainLighting.j3md");
matRock.setBoolean("useTriPlanarMapping", false);
matRock.setBoolean("WardIso", true);
matRock.setTexture("AlphaMap", assetManager.loadTexture("Textures/Terrain/splat/alphamap.png"));
Texture heightMapImage = assetManager.loadTexture("Textures/Terrain/splat/mountains512.png");
Texture grass = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");
grass.setWrap(Texture.WrapMode.Repeat);
matRock.setTexture("DiffuseMap", grass);
matRock.setFloat("DiffuseMap_0_scale", 64);
Texture dirt = assetManager.loadTexture("Textures/Terrain/splat/dirt.jpg");
dirt.setWrap(Texture.WrapMode.Repeat);
matRock.setTexture("DiffuseMap_1", dirt);
matRock.setFloat("DiffuseMap_1_scale", 16);
Texture rock = assetManager.loadTexture("Textures/Terrain/splat/road.jpg");
rock.setWrap(Texture.WrapMode.Repeat);
matRock.setTexture("DiffuseMap_2", rock);
matRock.setFloat("DiffuseMap_2_scale", 128);
Texture normalMap0 = assetManager.loadTexture("Textures/Terrain/splat/grass_normal.jpg");
normalMap0.setWrap(Texture.WrapMode.Repeat);
Texture normalMap1 = assetManager.loadTexture("Textures/Terrain/splat/dirt_normal.png");
normalMap1.setWrap(Texture.WrapMode.Repeat);
Texture normalMap2 = assetManager.loadTexture("Textures/Terrain/splat/road_normal.png");
normalMap2.setWrap(Texture.WrapMode.Repeat);
matRock.setTexture("NormalMap", normalMap0);
matRock.setTexture("NormalMap_1", normalMap2);
matRock.setTexture("NormalMap_2", normalMap2);
AbstractHeightMap heightmap = null;
try {
heightmap = new ImageBasedHeightMap(heightMapImage.getImage(), 0.25f);
heightmap.load();
} catch (Exception e) {
e.printStackTrace();
}
TerrainQuad terrain = new TerrainQuad("terrain", 65, 513, heightmap.getHeightMap());
List<Camera> cameras = new ArrayList<Camera>();
cameras.add(getCamera());
terrain.setMaterial(matRock);
terrain.setLocalScale(new Vector3f(5, 5, 5));
terrain.setLocalTranslation(new Vector3f(0, -30, 0));
// unlock it so we can edit the height
terrain.setLocked(false);
terrain.setShadowMode(RenderQueue.ShadowMode.Receive);
rootNode.attachChild(terrain);
}
use of com.jme3.terrain.heightmap.HeightMap in project jmonkeyengine by jMonkeyEngine.
the class HelloTerrain method simpleInitApp.
@Override
public void simpleInitApp() {
flyCam.setMoveSpeed(50);
/** 1. Create terrain material and load four textures into it. */
mat_terrain = new Material(assetManager, "Common/MatDefs/Terrain/Terrain.j3md");
/** 1.1) Add ALPHA map (for red-blue-green coded splat textures) */
mat_terrain.setTexture("Alpha", assetManager.loadTexture("Textures/Terrain/splat/alphamap.png"));
/** 1.2) Add GRASS texture into the red layer (Tex1). */
Texture grass = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");
grass.setWrap(WrapMode.Repeat);
mat_terrain.setTexture("Tex1", grass);
mat_terrain.setFloat("Tex1Scale", 64f);
/** 1.3) Add DIRT texture into the green layer (Tex2) */
Texture dirt = assetManager.loadTexture("Textures/Terrain/splat/dirt.jpg");
dirt.setWrap(WrapMode.Repeat);
mat_terrain.setTexture("Tex2", dirt);
mat_terrain.setFloat("Tex2Scale", 32f);
/** 1.4) Add ROAD texture into the blue layer (Tex3) */
Texture rock = assetManager.loadTexture("Textures/Terrain/splat/road.jpg");
rock.setWrap(WrapMode.Repeat);
mat_terrain.setTexture("Tex3", rock);
mat_terrain.setFloat("Tex3Scale", 128f);
/** 2.a Create a custom height map from an image */
AbstractHeightMap heightmap = null;
Texture heightMapImage = assetManager.loadTexture("Textures/Terrain/splat/mountains512.png");
heightmap = new ImageBasedHeightMap(heightMapImage.getImage());
/** 2.b Create a random height map */
// HillHeightMap heightmap = null;
// HillHeightMap.NORMALIZE_RANGE = 100;
// try {
// heightmap = new HillHeightMap(513, 1000, 50, 100, (byte) 3);
// } catch (Exception ex) {
// ex.printStackTrace();
// }
heightmap.load();
/** 3. We have prepared material and heightmap.
* Now we create the actual terrain:
* 3.1) Create a TerrainQuad and name it "my terrain".
* 3.2) A good value for terrain tiles is 64x64 -- so we supply 64+1=65.
* 3.3) We prepared a heightmap of size 512x512 -- so we supply 512+1=513.
* 3.4) As LOD step scale we supply Vector3f(1,1,1).
* 3.5) We supply the prepared heightmap itself.
*/
int patchSize = 65;
terrain = new TerrainQuad("my terrain", patchSize, 513, heightmap.getHeightMap());
/** 4. We give the terrain its material, position & scale it, and attach it. */
terrain.setMaterial(mat_terrain);
terrain.setLocalTranslation(0, -100, 0);
terrain.setLocalScale(2f, 1f, 2f);
rootNode.attachChild(terrain);
/** 5. The LOD (level of detail) depends on were the camera is: */
TerrainLodControl control = new TerrainLodControl(terrain, getCamera());
// patch size, and a multiplier
control.setLodCalculator(new DistanceLodCalculator(patchSize, 2.7f));
terrain.addControl(control);
}
use of com.jme3.terrain.heightmap.HeightMap in project jmonkeyengine by jMonkeyEngine.
the class FractalTileLoader method getHeightMapAt.
private HeightMap getHeightMapAt(Vector3f location) {
AbstractHeightMap heightmap = null;
FloatBuffer buffer = this.base.getBuffer(location.x * (this.quadSize - 1), location.z * (this.quadSize - 1), 0, this.quadSize);
float[] arr = buffer.array();
for (int i = 0; i < arr.length; i++) {
arr[i] = arr[i] * this.heightScale;
}
heightmap = new FloatBufferHeightMap(buffer);
heightmap.load();
return heightmap;
}
use of com.jme3.terrain.heightmap.HeightMap in project jmonkeyengine by jMonkeyEngine.
the class ImageTileLoader method getHeightMapAt.
/**
* Lets you specify the type of images that are being loaded. All images
* must be the same type.
* @param imageType eg. BufferedImage.TYPE_USHORT_GRAY
*/
/*public void setImageType(int imageType) {
this.imageType = imageType;
}*/
/**
* The ImageHeightmap that will parse the image type that you
* specify with setImageType().
* @param customImageHeightmap must extend AbstractHeightmap
*/
/*public void setCustomImageHeightmap(ImageHeightmap customImageHeightmap) {
if (!(customImageHeightmap instanceof AbstractHeightMap)) {
throw new IllegalArgumentException("customImageHeightmap must be an AbstractHeightMap!");
}
this.customImageHeightmap = customImageHeightmap;
}*/
private HeightMap getHeightMapAt(Vector3f location) {
// HEIGHTMAP image (for the terrain heightmap)
int x = (int) location.x;
int z = (int) location.z;
AbstractHeightMap heightmap = null;
//BufferedImage im = null;
String name = null;
try {
name = namer.getName(x, z);
logger.log(Level.FINE, "Loading heightmap from file: {0}", name);
final Texture texture = assetManager.loadTexture(new TextureKey(name));
heightmap = new ImageBasedHeightMap(texture.getImage());
/*if (assetInfo != null){
InputStream in = assetInfo.openStream();
im = ImageIO.read(in);
} else {
im = new BufferedImage(patchSize, patchSize, imageType);
logger.log(Level.WARNING, "File: {0} not found, loading zero heightmap instead", name);
}*/
// CREATE HEIGHTMAP
/*if (imageType == BufferedImage.TYPE_USHORT_GRAY) {
heightmap = new Grayscale16BitHeightMap(im);
} else if (imageType == BufferedImage.TYPE_3BYTE_BGR) {
heightmap = new ImageBasedHeightMap(im);
} else if (customImageHeightmap != null && customImageHeightmap instanceof AbstractHeightMap) {
// If it gets here, it means you have specified a different image type, and you must
// then also supply a custom image heightmap class that can parse that image into
// a heightmap.
customImageHeightmap.setImage(im);
heightmap = (AbstractHeightMap) customImageHeightmap;
} else {
// error, no supported image format and no custom image heightmap specified
if (customImageHeightmap == null)
logger.log(Level.SEVERE, "Custom image type specified [{0}] but no customImageHeightmap declared! Use setCustomImageHeightmap()",imageType);
if (!(customImageHeightmap instanceof AbstractHeightMap))
logger.severe("customImageHeightmap must be an AbstractHeightMap!");
return null;
}*/
heightmap.setHeightScale(1);
heightmap.load();
//} catch (IOException e) {
// e.printStackTrace();
} catch (AssetNotFoundException e) {
logger.log(Level.WARNING, "Asset {0} not found, loading zero heightmap instead", name);
}
return heightmap;
}
Aggregations