Material using vertex colors with texture overlay

Hi everyone :wink:

In our game we are using vertex colors only for displaying objects (no tetxures at all) but only vertex colors look a bit boring. So I thought if we would have different textures with different patterns and multiply them with the vertex colors the objects would get some better looking skin. For example, having a standard grass pattern texture which gets multiplied with the green vertex colors of the grass, therefore making the grass look better.
Unfortunately, I am pretty new to 3D-Graphics so this question might sound stupid, but is there already a material for that in Jme or would I have to write one by myself (which would be a long task for me I guess with all the GLSL stuff to learn).

Yes. Both of the standard materials support this.

Well thanks. So I could keep using the default Lighting Material, but which parameters will I have to change in order to achieve that? The vertex color should still be the main color for the models, so setting the diffuse map wouldn´make sense right? So what do I have to change in order to multiply the texture with the vertex color?

Set both and flag UseVertexColor if I remember correct.

Really that easy? :wink:
Thanks I´ll try it and come back if it works or not.

And you always could look at shader code to figure out how it works. :chimpanzee_wink:

Or the documentation.

Yeah I already looked into it, but as I as I said I am pretty new to 3D-Graphics so that´s all a bit overwhealming looking at the code :wink:
But I found this line inside the Lighting material so I guess this piece of code is doing the magic I am searching for :slight_smile:

#ifdef VERTEX_COLOR
      AmbientSum *= inColor.rgb;
      DiffuseSum *= inColor;
#endif

Yeah, documentation is a faster way, but source code gives more experience :chimpanzee_winktongue:

Yeah i guess so :smile:
Unfortunately I dont have the time to dig deeper into GLSL and writting things my own as a learning experience at the moment.

So I´ve tried giving the material a diffuse map, but the pattern of the diffuse map isn´t displayed on the objects surface. Instead it just darkens the object a bit (so it indeed multiplies the textures color with the vertex color).
Have i forgotten a step, because I haven´t assigned UV-Coordinates anywhere. I´ve just set the materials diffuse map.

If your object doesn’t have UV coordinates then the texture isn’t going to map to anything. So you probably just get one texel smeared all over the object.

You may need to carefully craft your texture for this purpose - with default material colors do blend not in a most fancy way possible due to nature of color representation in digital world. And UV surely should be baked in model, when you make it in 3d-tool or dynamically generate mesh.

Thanks, already thought that this might be the problem.
So for the objects like trees, etc. I can apply the texture in blender. But for our terrain which is randomly generated and created using our own FlatShadedTerrainGenerator I don´t know how to proceed. Do I have to assign the UV-Coordinates when creating the terrain, like I assign the vertex color there?

Yes. Look at any standard mesh generation code for advice. For example, in Quad it looks like this:

setBuffer(Type.TexCoord, 2, new float[]{0, 1,
                                        1, 1,
                                        1, 0,
                                        0, 0});

Ok so that would be an option, but I dont know how I would go about repeating the texture then. Is there a way I can solve this with the Jme Terrain Material?

Well, you just need a bit more complex UV than Quad. Basically, you have to make one pair of UV coordinates per vertex in your mesh.

As for repeating - I don’t remember how it’s done in existing materials, but there are two main ways to do it

  • to have a mesh with repeating UV coordinates, requires some splits in vertices if texture size does not correspond to terrain chunk size.
  • to have shader code that will apply your texture multiple times per single UV unit.
    Both have pros and cons and what to choose highly depends on use case.

Ok seems I have to do some research on that subject. But would using the Jme Terrain Material be an solution? Like in the terrain example on the wiki, using a texture to define where which material should be apllied. Like red means grass, etc.

You’ll need to calculate a proper UV anyway. And for complex terrain it may be quite tricky.

If by “repeating” you actually mean “multitexture mixing” then I think Terrain Material is a way to go, but I never used it personally as I prefer to write custom shader code and do weird things like storing texture id and offset in a pixel.

Otherwise it will be an overkill. For example, you could have one big texture containing all terrain textures and just calculate proper UV for your vertices when generating a mesh - no need to use expensive mixing techniques or write custom shader - just clever mesh generation.

With repeating I meant that if I have a terrain texture which is 512x512 pixel, which should be used for the whole terrain, the texture shouldn´t be stretched on the whole terrain, but the texture should repeat itself. But how would something like this look in GLSL? Any short code piece would be appreciated :wink: