Is there any way to generate a texture/image via code in jMonkey? I’m playing around with randomly generated terrains, and it’s probably going to be easiest for me to be able to generate a randomized texture to give to the terrain generator, rather than screwing around with trying to manually create my own random terrain shape (I’m still quite new to jMonkey, and to Java as well, so I’m just playing around and seeing what I can figure out). If there is a way to do this, can anyone point me toward some good tutorials or documentation that will help get me started? Thanks.
TerrainGridTest.java uses the HeightBasedTerrain material. That generates the textures based on the elevation/slope of the terrain. There are many ways to achieve generated textures, can you describe a little more how you want it to look to help narrow down what can be done?
I want to be able to generate a square heightmap image to give to the terrain generator in-code. So basically, I just need to know how you can “paint” your own texture with code. So I can use an algorithm of some sort to randomly make a heightmap (using perlin noise or some such thing).
Modify the heightmap and not some image you then generate it from completely.
Take a look at my recent thread about painting into custom textures. There was discussion there and links to a few examples.
@normen said:
Modify the heightmap and not some image you then generate it from completely.
Can you explain a little more about that? The only knowledge I have comes from the terrain tutorial.
@zarch said:
Take a look at my recent thread about painting into custom textures. There was discussion there and links to a few examples.
do you have a link?
Basically writing into an image is fairly straightforward. Reading from it not so much :s
I’ve got other stuff I need to get done first, planning to go back to that at some point though.
ok, thanks
Normen means that you do not need to change the heightmap image of the terrain, but modify the heightmap data directly (it will be faster). Terrain has some getHeight and setHeight methods that are good for this.
@jefequeso said:
Can you explain a little more about that? The only knowledge I have comes from the terrain tutorial.
You should look in the source of the tutorials and tests and try to understand what for example ImageHeightMap really do.
Something like this should get you started:
[java]
public class MyHeightMap extends AbstractHeightMap {
public MyHeightMap(int size) {
super();
this.size = size;
}
@Override
public boolean load() {
final int mapSize = getSize();
this.heightData = new float[mapSize * mapSize];
for (int xquad = 0; xquad < mapSize; xquad++) {
for (int zquad = 0; zquad < mapSize; zquad++) {
float height = // Do whatever you need to create the height
this.heightData[xquad + zquad * mapSize] = height;
}
}
return true;
}
}
[/java]
sploreg beat me to it do what he says, he knows this better than anyone.
I’m using this as a basis for the procedural textures in Myrena. Not sure if it’s better or worse than anything else mentioned.
Ok, I have a problem… I’ve got it generating random heightmaps with this code:
(quadmapsize = the size of the map’s sides (i.e, 128 x 128), number = a random number using Java’s built-in random generator)
[java]
int i = 0;
do
{
heights = number.nextFloat() * size;
i = i + 1;
}
while(i <= (quadmapsize * quadmapsize) - 1);
[/java]
which generates this:
Using terrainlighting.jme3, of course. Now… is it just me, or does it seem like the terrain is all going in the same direction? Like, shouldn’t it be more random looking? As it is, it seems like it’s making a lot of little diagonal hills.
Anyone have a solution?
This looks fine to me.
The thing here is that your random number is always between 0.0 and 0.9999(…). Meaning, very low “intensity” variation. You’ll have to insert more variation to get more… uh variation.
hmm… it might just be the lighting.
@jefequeso said:
Using terrainlighting.jme3, of course. Now... is it just me, or does it seem like the terrain is all going in the same direction? Like, shouldn't it be more random looking? As it is, it seems like it's making a lot of little diagonal hills.
The diagonal hills are because you are too random. You are basically simulating data at a frequency that the grid cannot properly replicate. Since all of the "quads" have a diagonal running in the same direction then you get lots of ridges.
These would be less noticable if the terrain didn't vary so wildly and rapidly.
Yeah, you need something like perlin noise really. Or use random at fewer points and interpolate between them.