【发布时间】:2012-04-02 08:57:05
【问题描述】:
假设我们对四边形(两个三角形)进行纹理处理。我认为这个问题类似于下一个示例中的纹理飞溅
precision lowp float;
uniform sampler2D Terrain;
uniform sampler2D Grass;
uniform sampler2D Stone;
uniform sampler2D Rock;
varying vec2 tex_coord;
void main(void)
{
vec4 terrain = texture2D(Terrain, tex_coord);
vec4 tex0 = texture2D(Grass, tex_coord * 4.0); // Tile
vec4 tex1 = texture2D(Rock, tex_coord * 4.0); // Tile
vec4 tex2 = texture2D(Stone, tex_coord * 4.0); // Tile
tex0 *= terrain.r; // Red channel - puts grass
tex1 = mix( tex0, tex1, terrain.g ); // Green channel - puts rock and mix with grass
vec4 outColor = mix( tex1, tex2, terrain.b ); // Blue channel - puts stone and mix with others
gl_FragColor = outColor; //final color
}
但我只想在所需位置的基础四边形纹理上放置 1 个贴花。
算法是一样的,但我认为我们不需要带有 1 个填充层的额外纹理来保存贴花的位置(例如红色层!= 0),我们必须如何生成我们自己的“terrain.r”(这是浮动?)变量并混合基础纹理和贴花纹理。
precision lowp float;
uniform sampler2D base;
uniform sampler2D decal;
uniform vec2 decal_location; //where we want place decal (e.g. 0.5, 0.5 is center of quad)
varying vec2 base_tex_coord;
varying vec2 decal_tex_coord;
void main(void)
{
vec4 v_base = texture2D(base, base_tex_coord);
vec4 v_decal = texture2D(Grass, decal_tex_coord);
float decal_layer = /*somehow get our decal_layer based on decal_position*/
gl_FragColor = mix(v_base, v_decal, decal_layer);
}
如何实现这样的事情?
或者我可能只是在 opengl 端生成 splat 纹理并将其传递给第一个着色器?这将为我提供多达 4 种不同的贴花,但频繁更新会很慢(例如机枪撞墙)
【问题讨论】: