GLSL shader: Interpolate between more than two textures

后端 未结 3 1292
傲寒
傲寒 2021-02-03 14:54

I\'ve implemented a heightmap in OpenGL. For now it is just a sine/cosine curved terrain. At the moment I am interpolating between the white \"ice\" and the darker \"stone\" te

3条回答
  •  一整个雨季
    2021-02-03 15:27

    mix() is really just a convenience function for something you can easily write yourself. The definition is:

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

    Or putting it differently, it calculates a weighted average of v1 and v2, with two weights w1 and w2 that are float values between 0.0 and 1.0 meeting the constraint w1 + w2 = 1.0:

    v1 * w1 + v2 * w2
    

    You can directly generalize this to calculate a weighted average of more than 2 inputs. For example, for 3 inputs v1, v2 and v3, you would use 3 weights w1, w2 and v3 meeting the constraint w1 + w2 + w3 = 1.0, and calculate the weighted average as:

    v1 * w1 + v2 * w2 + v3 * w3
    

    For your example, determine the weights you want to use for each of the 3 textures, and then use something 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;
    

提交回复
热议问题