2016-03-26 137 views
1

在我的頂點着色器中,我希望修改片段着色器中共享的attribute vec2 a_position變量。通過這個修改,我應該將圖像轉化爲圓柱投影。
這是我在做什麼,我着色器:修改屬性vec2變量

<!-- vertex shader --> 
<script id="2d-vertex-shader" type="x-shader/x-vertex"> 
    attribute vec2 a_position; 

    uniform vec2 u_resolution; 
    uniform mat3 u_matrix; 

    varying vec2 v_texCoord; 

    void main() { 
     // modifying START 
     float angle = atan(a_position.y, a_position.x); 
     float r = sqrt(a_position.x*a_position.x + a_position.y*a_position.y); 
     a_position.x = r*cos(angle); 
     a_position.y = r*sin(angle); 
     // modifying STOP 
     gl_Position = vec4(u_matrix * vec3(a_position, 1), 1); 
     v_texCoord = a_position; 
    } 
</script> 

<!-- fragment shader --> 
<script id="2d-fragment-shader" type="x-shader/x-fragment"> 
    precision mediump float; 

    // our texture 
    uniform sampler2D u_image; 

    // the texCoords passed in from the vertex shader. 
    varying vec2 v_texCoord; 

    void main() { 
     gl_FragColor = texture2D(u_image, v_texCoord); 
    } 
</script> 

但我發現了這個錯誤:

compiling shader '[object WebGLShader]':ERROR: 0:12: 'assign' : l-value required "a_position" (can't modify an attribute) ERROR: 0:13: 'assign' : l-value required "a_position" (can't modify an attribute)

你沒有任何想法如何解決呢?

回答

3

只需使用另一個變量

attribute vec2 a_position; 

uniform vec2 u_resolution; 
uniform mat3 u_matrix; 

varying vec2 v_texCoord; 

void main() { 
    // modifying START 
    float angle = atan(a_position.y, a_position.x); 
    float r = sqrt(a_position.x*a_position.x + a_position.y*a_position.y); 
    vec3 p = vec3(
    r*cos(angle), 
    r*sin(angle), 
    1); 
    // modifying STOP 
    gl_Position = vec4(u_matrix * p, 1); 
    v_texCoord = a_position; 
} 
+0

偉大的作品,謝謝! –