0
我們有一個紋理圖集,其中不包含方形子圖層,我們希望用它作爲貼圖紋理來填充遊戲中的場景。我們需要使用不是正方形的瓷磚,因爲我們二維遊戲中具體的形狀和需要爲不同的手機屏幕提供許多圖形包。所以將地面紋理的大小與兩個冪關聯起來並不方便。如何在opengl 2.0中重複非POT subtexture
在google中找不到任何有效的着色器或解釋。任何幫助將非常感激。
我們有一個紋理圖集,其中不包含方形子圖層,我們希望用它作爲貼圖紋理來填充遊戲中的場景。我們需要使用不是正方形的瓷磚,因爲我們二維遊戲中具體的形狀和需要爲不同的手機屏幕提供許多圖形包。所以將地面紋理的大小與兩個冪關聯起來並不方便。如何在opengl 2.0中重複非POT subtexture
在google中找不到任何有效的着色器或解釋。任何幫助將非常感激。
有關如何在着色器中使用這些數據的一些信息會很好。
我通常喜歡的是在着色器中獲取紋理座標,就好像沒有圖集一樣。接下來,我將添加一個「框架」,以表明子紋理在實際紋理上的位置。重複必須在片段着色器中手動完成。 (頂點着色器不合適,因爲它只會導致拉伸紋理)。
所以基本上你需要修改輸入座標在[0,1]
範圍內並使用它們。因此,如果座標爲12.12
,則需要將其用作.12
或x=x-(1/x)
用於正數,而將x=x-((1/x)-1)
用於負數。
看看這個代碼(只是一個概念,我沒有測試):
uniform lowp vec4 subtextureRelativeCoordinates;// Represents a part of the texture where the sub texture lies. Origin is .xy, size is .zw
varying highp vTextureCoordinates; // actual texture coordinates which you would use if no atlasing was present
main() {
// resample texture coordinates to be in range [0,1] as if they need to repeat
int xCount = (int)(1.0/vTextureCoordinates.x); // might also be negative
int yCount = (int)(1.0/vTextureCoordinates.y); // might also be negative
// a negative coordinates will always have +1. For instance -1.5 will return xCount=-1 but the result must be -1.5 - (-1-1) = .5
xCount += vTextureCoordinates.x<.0?-1:0;
yCount += vTextureCoordinates.y<.0?-1:0;
vTextureCoordinates -= vec2(xCount, yCount);
lowp vec2 textureCoordinate = textureCoordinates*subtextureRelativeCoordinates.zw + subtextureRelativeCoordinates.xy;
...
}
謝謝!我們已經實現了非常相似的東西! –