Fandom

Urho3D Wiki

Expand default terrain material to 6 textures using the six primary&secondary colors

69pages on
this wiki
Add New Page
Comments0 Share

Here I'm using the "six basic colors" red, yellow, green, cyan, blue and magenta to get six separate color weight.

Urho is currently per default limited to five textures. You need to modify the file "Source/Urho3D/Graphics/GraphicsDefs.h" first to increase this limit:

...
/// Texture units.
enum TextureUnit
{
    TU_DIFFUSE = 0,
    TU_ALBEDOBUFFER = 0,
    TU_NORMAL = 1,
    TU_NORMALBUFFER = 1,
    TU_SPECULAR = 2,
    TU_EMISSIVE = 3,
    TU_ENVIRONMENT = 4,
    MAX_MATERIAL_TEXTURE_UNITS = 8,  // this and the following numbers have been increased
    TU_LIGHTRAMP = 8,                // by 3 each to increase the texture limit from 5 to 8
    TU_LIGHTSHAPE = 9,
    TU_SHADOWMAP = 10,
    TU_FACESELECT = 11,
    TU_INDIRECTION = 12,
    TU_DEPTHBUFFER = 13,
    TU_LIGHTBUFFER = 14,
    TU_VOLUMEMAP = 15,
    TU_ZONE = 16,
    MAX_NAMED_TEXTURE_UNITS = 17,
    MAX_TEXTURE_UNITS = 19
};
...

Rebuild Urho3D and your project after doing this!

The Material.xml:

<material>
    <technique name="Techniques/TerrainBlend_6splat.xml" />
    <texture unit="0" name="Textures/TerrainWeights.png" />
    <texture unit="1" name="Textures/red.png" />
    <texture unit="2" name="Textures/yellow.png" />
    <texture unit="3" name="Textures/green.png" />
    <texture unit="4" name="Textures/cyan.png" />
    <texture unit="5" name="Textures/blue.png" />
    <texture unit="6" name="Textures/magenta.png" />
    <parameter name="MatSpecColor" value="0.6 0.6 0.6 16" />
    <parameter name="DetailTiling" value="64 64" />
</material>

The technique.xml file is identical to the default terrain technique.

GLSL Shader:

...
uniform sampler2D sWeightMap0;
uniform sampler2D sDetailMap1;
uniform sampler2D sDetailMap2;
uniform sampler2D sDetailMap3;
uniform sampler2D sDetailMap4;  // the default terrain shader had only 4 maps (one weight texture + three splatting textures). Added these 3.
uniform sampler2D sDetailMap5;
uniform sampler2D sDetailMap6;
...
void PS()
{
    vec3 colors = texture2D(sWeightMap0,vTexCoord).rgb;   // I added a second vec3 called weights_ycm to get six weights in total
    vec3 weights_rgb = colors;
    vec3 weights_ycm = colors;

    weights_rgb.r=clamp(colors.r-(colors.g+colors.b),0.0,1.0);   // it took a while to figure out how to separate the six colors
    weights_rgb.g=clamp(colors.g-(colors.b+colors.r),0.0,1.0);   // it's basically using each RGB colors difference to the other two.
    weights_rgb.b=clamp(colors.b-(colors.r+colors.g),0.0,1.0);
    weights_ycm.r=clamp(colors.r*colors.g,0.0,1.0);       // yellow
    weights_ycm.g=clamp(colors.g*colors.b,0.0,1.0);       // cyan
    weights_ycm.b=clamp(colors.b*colors.r,0.0,1.0);       // magenta
                                                          // the "sumWeights" stuff is gone
    vec4 diffColor = cMatDiffColor * (
        weights_rgb.r * texture2D(sDetailMap2, vDetailTexCoord) +  // red
        weights_ycm.r * texture2D(sDetailMap3, vDetailTexCoord) +  // yellow
        weights_rgb.g * texture2D(sDetailMap4, vDetailTexCoord) +  // green
        weights_ycm.g * texture2D(sDetailMap5, vDetailTexCoord) +  // cyan
        weights_rgb.b * texture2D(sDetailMap6, vDetailTexCoord) +  // blue
        weights_ycm.b * texture2D(sDetailMap7, vDetailTexCoord)    // magenta
    );
...

Result:

Terrain 6splat-0

Left is the used weight map with the six colors and right the resulting terrain in Urho. It should be possible to also use the alpha channel for another color weight or to separate these six colors again to get twelve color weights.

This technique with the six colors has one disadvantage: It's not possible to directly/clearly fade from some colors to colors on the opposite site (like red<->green) as you can see on the screenshot. Depending on your settings when drawing the weight map, you can also achieve a black "border" when, for example, drawing red over green instead of the yellow border here.

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.