java - GLSL shader: Interpolate between more than two textures -



java - GLSL shader: Interpolate between more than two textures -

i've implemented heightmap in opengl. sine/cosine curved terrain. @ moment interpolating between white "ice" , darker "stone" texture. done this:

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

the result:

it works fine, if want add together more textures, illustration grass texture, interpolation order "ice, stone, grass"? think, there isn't function mix(sampler2d[], percentages[])? how write glsl method next logic?

mix() convenience function can write yourself. definition is:

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

or putting differently, calculates weighted average of v1 , v2, 2 weights w1 , w2 float values between 0.0 , 1.0 meeting constraint w1 + w2 = 1.0:

v1 * w1 + v2 * w2

you can straight generalize calculate weighted average of more 2 inputs. example, 3 inputs v1, v2 , v3, utilize 3 weights w1, w2 , v3 meeting constraint w1 + w2 + w3 = 1.0, , calculate weighted average as:

v1 * w1 + v2 * w2 + v3 * w3

for example, determine weights want utilize each of 3 textures, , utilize like:

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;

java opengl lwjgl interpolation heightmap

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -