Search code examples
javaopenglglsllwjglmultitexturing

How can I merge the colour of 3D foliage with the texture beneath it?


I have got a 2D texture, such as this one, which I am currently just dropping directly onto the terrain. enter image description here

I am then rendering foliage on top of that texture. enter image description here

I would like to (using the fragment shader), get the colour of the texture beneath the grass, and set the grass to be the colour of the texture beneath it.

To do this, I need to get the position of each grass vertex on the terrain relative to the texture. enter image description here

I then need to get the colour of the texture corresponding the the location of the object, and blend it with the current grass colour (let's say I have red grass for demonstration purposes). enter image description here

I am currently not sure how to start. I know I need to get the grasses' X and Z position in the world (which I currently do not have) through the shaders, but I then need to work out the corresponding texture pixel, which I currently do not know how to do.

Any advice to help solving this would be appreciated.


Solution

  • The main idea is that you pass position coordinates as texture coordinates.

    Hoping I've calculated right

    You have a rectangle where the height map lays, let the position coordinates be relative to that. In this case, if your height map is NxM, and texture coordinates are 1x1, you may just normalise the positions to 0..1, and then you have got a texture coordinate.

    With this texture coordinate now you can get the colour passing the texture as a uniform.

    I hope it was clear and satisfies your question, but also I'm willing to see clarifications.