2016-07-29 2 views
0

Когда я пытаюсь связать свои вершинные и фрагмент шейдеры в программу, WebGL бросает Varyings with the same name but different type, or statically used varyings in fragment shader are not declared in vertex shader: textureCoordinatesПочему мой шейдер WebGL не позволяет мне использовать изменения?

У меня есть varying vec2 test в обеих моих вершин и фрагментов шейдеров, и не вижу никаких причин, почему компилятор не сможет найти то же самое varying в обоих.

Vertex Shader:

varying vec2 test; 
void main(void) { 
    gl_Position = vec4(0.0, 0.0, 0.0, 0.0); 
    test = vec2(1.0, 0.0); 
} 

Фрагмент Shader: Код

precision highp float; 
varying vec2 test; 
void main(void) { 
    gl_FragColor = vec4(test.xy, 0.0, 1.0); 
} 

Тест:

const canvas = document.createElement('canvas'); 
gl = canvas.getContext('webgl') 
let vert = gl.createShader(gl.VERTEX_SHADER); 
gl.shaderSource(vert, "varying vec2 test;\nvoid main(void) {\n gl_Position = vec4(0.0, 0.0, 0.0, 0.0);\n test = vec2(1.0, 0.0);\n}"); 
gl.compileShader(vert); 

let frag = gl.createShader(gl.FRAGMENT_SHADER); 
gl.shaderSource(frag, "precision highp float;\nvarying vec2 test;\nvoid main() {\n\tgl_FragColor = vec4(test.xy, 0.0, 1.0);\n}"); 
gl.compileShader(frag); 

let program = gl.createProgram(); 
gl.attachShader(program, vert); 
gl.attachShader(program, frag); 

gl.linkProgram(program); 
gl.useProgram(program); 
+0

Какой браузер вы используете? Я не получаю никаких ошибок с Chrome для Windows v51.0.2704.103m (64-разрядная версия). Вот [точный код] (http://pastebin.com/6EXAetZ2), который я выполняю. – Exide

+0

@ При обновлении моего браузера исправлена ​​проблема. Weird. –

ответ

0

Только предположение, но мне интересно, если это потому, что вы не используете textureCoordinates в вашем шейдере фрагмента. Имена типов & соответствуют только штрафу, поэтому я не думаю, что это проблема. Я сделал то же самое здесь:

Frag:

// The fragment shader is the rasterization process of webgl 

// use float precision for this shader 
precision mediump float; 

// the input texture coordinate values from the vertex shader 
varying vec2 vTextureCoord; 
// the texture data, this is bound via gl.bindTexture() 
uniform sampler2D texture; 
// the colour uniform 
uniform vec3 color; 

void main(void) { 
    // gl_FragColor is the output colour for a particular pixel. 
    // use the texture data, specifying the texture coordinate, and modify it by the colour value. 
    gl_FragColor = texture2D(texture, vec2(vTextureCoord.s, vTextureCoord.t)) * vec4(color, 1.0); 
} 

Vert:

// setup passable attributes for the vertex position & texture coordinates 
attribute vec3 aVertexPosition; 
attribute vec2 aTextureCoord; 

// setup a uniform for our perspective * lookat * model view matrix 
uniform mat4 uMatrix; 
// setup an output variable for our texture coordinates 
varying vec2 vTextureCoord; 
void main() { 
    // take our final matrix to modify the vertex position to display the data on screen in a perspective way 
    // With shader code here, you can modify the look of an image in all sorts of ways 
    // the 4th value here is the w coordinate, and it is called Homogeneous coordinates, (x,y,z,w). 
    // It effectively allows the perspective math to work. With 3d graphics, it should be set to 1. Less than 1 will appear too big 
    // Greater than 1 will appear too small 
    gl_Position = uMatrix * vec4(aVertexPosition, 1); 
    vTextureCoord = aTextureCoord; 
} 
+0

Я изменил свой первоначальный вопрос, чтобы содержать минимальный рабочий пример с ошибкой кода. Кажется, что он терпит неудачу, даже если я использую переменную. –

0

Проблема была решена путем обновления Chrome для OSX от v51.something до 52.0.2743.82 (64- бит) Странно.

Смежные вопросы