2014-11-05 63 views
0

我使用貼圖爾塔斯和這個頂點着色渲染在四紋理:opengles抵消紋理座標的着色器

"uniform mat4 uMVPMatrix;" + 
    "attribute vec4 vPosition;" + 
    "attribute vec4 a_Color;" + 
    "attribute vec2 a_texCoord;" + 
    "varying vec4 v_Color;" + 
    "varying vec2 v_texCoord;" + 
    "uniform vec4 u_newColor;" + 

    "void main() {" + 
    " float offset = 0.33;" + 
    " gl_Position = uMVPMatrix * vPosition;" + 
    " v_texCoord.x = a_texCoord.x + offset;" + 
    " v_texCoord.y = a_texCoord.y + offset;" + 
    " v_Color = u_newColor;" + 
    "}"; 

一切工作正常,我呈現了三分之一,至顯示9的一箇中間瓦片的紋理在地圖集。除了我想'偏移量'是一個變量,即我想根據紋理圖集中需要的'瓦片'來改變它。

這怎麼可能?

回答

0

添加均勻,使用:

"uniform mat4 uMVPMatrix;" + 
"attribute vec4 vPosition;" + 
"attribute vec4 a_Color;" + 
"attribute vec2 a_texCoord;" + 
"varying vec4 v_Color;" + 
"varying vec2 v_texCoord;" + 
"uniform vec4 u_newColor;" + 
"uniform vec2 u_offset ;" + //vec2 so there is a x and y component that you can set 

"void main() {" + 
" gl_Position = uMVPMatrix * vPosition;" + 
" v_texCoord = a_texCoord + u_offset;" + 
" v_Color = u_newColor;" + 
"}"; 

,並設置與

glUniform2f(u_offset_location, xTile, yTile); 
+0

感謝您的快速回復均勻。這正是我所嘗試的,但無論我設置的值如何,紋理都不再可見。我確實在某處讀到'抵消'必須是一個常數? – user3725725 2014-11-05 11:28:57

+0

偏移量應該介於0和1之間,以獲得最初爲'xTile'和'yTile'值設置'0.33,0.33'的結果 – 2014-11-05 11:35:36