2013-01-31 246 views
0

我無法找到相互繪製紋理的模式。 我需要結果片段顏色等:OpenGL着色器 - 重疊多個紋理

tex1 +(1-tex1alpha)* TEX2 +(1-tex1alpha-tex2alpha)* tex3

不混合紋理,但放置一個比其它類似的層在圖像編輯器中。

+0

您的場景是否允許使用不同紋理多次繪製幾何圖形? –

+0

如果將它寫入頂點着色器會發生什麼? –

+0

我需要計算着色器中的片段顏色。我的頂級紋理只有黑色和透明度,所以當我使用color1 + color2時,黑色將不會顯現。 – user1993006

回答

0

我沒有真正使用OpenGL,所以GLSL代碼可能不完全有效。但是你正在尋找的着色器應該看起來像這樣。隨時糾正任何錯誤。

vec4 col; 
col = texture2D(tex3, uv) 
if(col.a == 1) { gl_FragColor = col; return; } 
col = texture2D(tex2, uv) 
if(col.a == 1) { gl_FragColor = col; return; } 
col = texture2D(tex1, uv) 
if(col.a == 1) { gl_FragColor = col; return; } 

這就是說,如果紋理的數量是固定的。如果你有一個可變數量的紋理,你可以把它們放到一個紋理數組,這樣做:

vec4 col; 
for(int i = nTextures - 1; i >= 0; --i) 
{ 
    col = texture2DArray(textures, vec3(uv, i)); 
    if(col.a == 1) 
    { 
     gl_FragColor = col; 
     return; 
    } 
} 
0

你可以寫你直接在着色器貼配方。以下將起作用:

uniform sampler2D sampler1; 
uniform sampler2D sampler2; 
uniform sampler2D sampler3; 

varying vec2 texCoords; 

void main() 
{ 
    vec4 tex1 = texture(sampler1, texCoords); 
    vec4 tex2 = texture(sampler2, texCoords); 
    vec4 tex3 = texture(sampler3, texCoords); 
    gl_FragColor = tex1 + (1 - tex1.a) * tex2 + (1 - tex1.a - tex2.a) * tex3; 
}; 

但是,第三個參數可能變爲負值,這會產生不希望的結果。最好是:

gl_FragColor = tex1 + (1 - tex1.a) * tex2 + max(1 - tex1.a - tex2.a, 0) * tex3;