[SOLVED] Heatmap vertex color problem

I have tried your code and it worked partly, it created 1 white box in the middle of the screen.
But how could i color just a simple spot based on a coordinate?
With my previous code.

public int getIndex(int x, int y) {
    int index = (y * size) + x;
}

Where x and y are your coordinates.

int index = getIndex(5, 2);
Vector3f vertex = verts[index]; // the position of the coord.
ColorRGBA color = colors[index]; // the color of the coord.

Im facing the same Problem as @M1ztl. I’m trying to display a heatmap.

But my vertex gradiant is only being shown in the quarters is there a way to solve this without using any plugins or anything for that matter.

I’ve tried to get the height based on the coordinates in a mesh but i couldnt figure that out.

Our codes are similar

Is this for a school project?

Indeed

so i understand heightmap from points is not a problem right?

it can be done like:

AbstractHeightMap heightmap = new AbstractHeightMap() {
    @Override
    public boolean load() {
        size = terrainSize;
        // clean up data if needed.
        if (null != heightData) {
            unloadHeightMap();
        }
            heightData = new float[size * size];
            // transfer temporary buffer to final heightmap
            for (int i = 0; i < size; i++) {
                for (int j = 0; j < size; j++) {
                    setHeightAtPoint(GETYOURHEIGHT(j,i), j, i);
                }
            }
            normalizeTerrain(NORMALIZE_RANGE);
        }
        return true;
    }
};
heightmap.load();

about gradient, not sure what you mean by vertex color(do it really need to be made via vertex color?), but i would make it in shader based on ModelSpace or some space of pixel. The more Y the more red or something like that.

in shader i belive it will be using:

wPosition = TransformWorld(modelSpacePos).xyz;

not sure it it were taking vertex, might not, if not then use somethign that take vetrex position.
so in fragment shader(pixel shader) the more y the more r something like:

gl_FragColor.r = wPosition.y / 100; or something similar, just an idea.

because im not sure if you need to make this Via vertex color, or just effect need to be like this.

Here’s what I meant by vertex colors https://wiki.jmonkeyengine.org/jme3/advanced/custom_meshes.html#example-vertex-colors

Doesn’t have to be made with vertex colors, I just thought it might be the easier way to implement a heatmap.

But i can give it a try i guess, thanks for the advice

its always best if you say what you exactly need to do.

if you say it dont need to be made with vertex color and custom mesh, then i suggest using JME Terrain with heightmap like i show above code, and override its material to use one with your shader that print black color + red the higher Y value of pixel/vertex is. (but it require little shader knowledge or some research/play in shader)

ofc, you can still do with custom mesh /vertex color, but if you have problems making it work, i just add another suggestion you can use :slight_smile:

I really appreciate your help on this matter. I’ll try and implement your idea into my programm.

The reason why i went for vertex colors was because there was a thread
this one


which lead me to trying it out with vertex colors, it looks stunning so i thought why not try it. But if you say that i can get the same or somewhat the same result with shaders ill give it a try

Thanks again :slight_smile:

You said that i could still do it with custom meshes. Do you know how i could do a mesh which consits of 4 patches. Or what do i have to do for the mesh to go over the whole terrain.

consits of 4 patches

well not sure what you mean.

Anyway i just made a simple shader:

is this video what you want?

in vertex shader:

varying float height;

and:

vec4 modelSpacePos = vec4(inPosition, 1.0);
gl_Position = TransformWorldViewProjection(modelSpacePos);
texCoord = inTexCoord;
wPosition = TransformWorld(modelSpacePos).xyz;

and:

height = wPosition.y;

in fragment shader:

varying float height;

and:

gl_FragColor.rgb = vec3(height/50,0,0);

(50 value depends)

Yes that’s what i want. :+1:
Only that the coords of the “Mountains” is being applied from the database and not by mouseclick.

And with shaders would there be a possibility to make it fade from green to red?

Where should i implement the code you wrote?

you can look at JME tutorials about JME Terrain, (in forum on left up is Wiki, search there)

so instead of loading heightmap from image/file you do it like:

AbstractHeightMap heightmap = new AbstractHeightMap() {
    @Override
    public boolean load() {
        size = terrainSize;
        // clean up data if needed.
        if (null != heightData) {
            unloadHeightMap();
        }
            heightData = new float[size * size];
            // transfer temporary buffer to final heightmap
            for (int i = 0; i < size; i++) {
                for (int j = 0; j < size; j++) {
                    setHeightAtPoint(GETYOURHEIGHT(j,i), j, i);
                }
            }
            normalizeTerrain(NORMALIZE_RANGE);
        }
        return true;
    }
};
heightmap.load();

where:

setHeightAtPoint(GETYOURHEIGHT(j,i), j, i);

GETYOURHEIGHT will be some of your method that get height for j,i coords

So should i put the AbstractHeightMap into the constructor?

What am i supposed to do with the vertex and fragment shaders, or where should i put the things that you wrote there

I’m a bit confused right now :smiley:

Where should i implement the code you wrote?

well i cant do all school project for you, because it would be not fair :slight_smile:

  • everything you have in wiki or in forum posts. like i said when you will look at JME Terrain Wiki -you will know where you need put this code.
  • Also when you will look at simple shaders, you will know where put this lines too.

And with shaders would there be a possibility to make it fade from green to red?

just change line about color to:

gl_FragColor.rgb = vec3(height/20,1 - height/20,0);

video:

1 Like

Thank you :slight_smile:

Is there any difference when doing it with the mouse or just putting in coordinates in the heightmap?

Ill try to find out where I have to put the lines in, wish me luck :smiley:

no there is no difference.

the only difference as you see i got there “Grid” and i got bloom filter that make tip of mountain yellow color arround. you dont need it and you will not have it so dont worry.

How do i get the bloom filter with the yellow color?
I think it makes the whole heatmap better.

Again a big thank you for your help :pray:

    FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
    viewPort.addProcessor(fpp);
    BloomFilter bloom = new BloomFilter(BloomFilter.GlowMode.Scene);
    bloom.setExposurePower(80);
    bloom.setBloomIntensity(3f);
    bloom.setDownSamplingFactor(1.5f);
    bloom.setBlurScale(2f);
    bloom.setExposureCutOff(0);
    fpp.addFilter(bloom);

just put this in simpleInitApp, should work fine. Anyway i dont think its proper for heightmap :slight_smile:

if you need yellow color between green / red, you need again change:

gl_FragColor.rgb = vec3(height/20,1 - height/20,0);

and setup color you need.

I cant say it enough THANK YOU :heart: