GLSL Shinder: Interpolate between more than two textures

I implemented a height map in OpenGL. So far this is just a sinusoidal curve. I am currently interpolating between white “ice” and a darker “stone” texture. This is done as follows:

color = mix(texture2D(ice_layer_tex, texcoord), texture2D(stone_layer_tex, texcoord), (vertex.y + amplitude) / (amplitude * 2))

Result:

from top

from bottom

It works fine, but what should I do if I want to add more textures, like grass texture, so the interpolation order is “ice, stone, grass”? I think there is no type function mix(sampler2D[], percentages[])? How can I write a GLSL method following this logic?

+4
source share
3 answers

mix() -, . :

mix(v1, v2, a) = v1 * (1 - a) + v2 * a

, , v1 v2 w1 w2, 0.0 1.0, w1 + w2 = 1.0:

v1 * w1 + v2 * w2

, 2 . , 3 v1, v2 v3 3 w1, w2 v3, w1 + w2 + w3 = 1.0, :

v1 * w1 + v2 * w2 + v3 * w3

, , - :

weightIce = ...;
weightStone = ...;
weightGrass = 1.0 - weightIce - weightStone;
color = texture2D(ice_layer_tex, texcoord) * weightIce +
        texture2D(stone_layer_tex, texcoord) * weightStone +
        texture2D(grass_layer_tex, texcoord) * weightGrass;
+19

genralized mix(), . , " (, , )". , , , + + , + + + . , 3D- () . 2D- 3D-. texcoords , , . texcoords [0,1], . "" i -

p=i/num_layers + 1/(2*num_layers)

, 3 , . ,

0/3+1/6 = 0.16667       100% ice  
1/3+1/6 = 0.5           100% stone
2/3+1/6 = 0.83333       100% grass

,

1/3 = 0.3333            50% ice + 50% stone  
      0.6               70% stone  + 30% grass
...
+2

No, according to the GLSL documentation for mix () there are only overloads for interpolation between two parameters.

Would it be acceptable for you to simply interpolate “ice” and “stone” and then mix the result with the texture of “grass”?

vec4 ice_color   = texture2D(ice_layer_tex,   texcoord);
vec4 stone_color = texture2D(stone_layer_tex, texcoord);
vec4 grass_color = texture2D(grass_layer_tex, texcoord);

vec4 tmp = mix(ice_color, stone_color, pct);
vec4 final_color = mix(tmp, grass_color, pct);
+1
source

Source: https://habr.com/ru/post/1545594/


All Articles