Hello,
I have been searching for a method, which allows me to pass 2 textures to a material; a base texture and a overlay. The overlay should have a alpha channel, trough which the base texture is visible. My search led me to a 6 year old topic, with no straight answers.
My guess is that I have to make or customize a shader, but since I have no experience with shaders I’m looking for a easier and/or existing method.
I could load the 2 textures and merge them into 1 texture before passing it to the material, but I would like the overlay to be random (will be using it for dirt and wear). So if I generate all those combinations, I will have a lot of textures to cache.
Could someone push me into the right direction?
Thanks!
Look at Terrain.frag to see how to do “texture splatting”. For a more advanced version that handles lighting, look at TerrainLighting.frag (and the corresponding ,j3md files)
Thanks, I’ve been able to build something from that.
If other people want to do this, this is how I did it;
I used the ‘Lighting’ shader as a base. In the .j3md file, add this line below the DiffuseMap definition (#54).
Texture2D DiffuseOverlay
In the .j3md file, add this line under ‘Technique{ Defines {… } }’ (#162)
DIFFUSEOVERLAY : DiffuseOverlay
In the .frag file, add these lines below the DIFFUSEMAP definition (#29)
#ifdef DIFFUSEOVERLAY
uniform sampler2D m_DiffuseOverlay;
#endif
And finally, in the .frag file, add these lines below “vec4 diffuseColor = texture2D(m_DiffuseMap, newTexCoord);” (#170)
#ifdef DIFFUSEOVERLAY
vec4 alphaBlend = texture2D( m_DiffuseOverlay, texCoord.xy );
vec4 diffuseOverlay = texture2D(m_DiffuseOverlay, newTexCoord);
diffuseColor = mix( diffuseColor, diffuseOverlay, alphaBlend.a );
#endif
When creating a material, you can just pass the overlay texture like this
[java]material.setTexture(“DiffuseOverlay”, texture);[/java]
Very nicely articulated!