I have a webgl program that sometimes renders textures, but mostly renders triangles or lines. When I am not rendering a texture, webgl gives the warning, "RENDER WARNING: there is no texture bound to the unit 0
". Here is some code from my rendering loop:
if (thing.hasOwnProperty('texCoords')) {
gl.uniform1i(shaderProgram.useTexture, true);
gl.bindBuffer(gl.ARRAY_BUFFER, thing.texCoords);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, thing.texture);
gl.uniform1i(shaderProgram.tex_uniform, 0);
gl.enableVertexAttribArray(shaderProgram.vertexPositionTex);
gl.vertexAttribPointer(shaderProgram.vertexPositionTex, 2, gl.FLOAT, false, 0, 0);
} else {
gl.uniform1i(shaderProgram.useTexture, false);
gl.disableVertexAttribArray(shaderProgram.vertexPositionTex);
}
If a thing
doesn't render a texture, there is code to set a flag in my shaders not to consult the sampler2d
for texture values and instead just draw polygons.
I suppose "there is no texture bound to the unit 0
" is correct: there isn't a texture bound since the last call to drawArrays
, but why would that issue a warning when I don't try to access TEXTURE0
?
useTexture
is true. What matters is the shader uses a texture at all. So keep a texture bound or follow best practices and either (a) use two shaders, one that uses a texture and one that does not or (b) multiply the texture by the colorgl_FragColor = uniformColor * textureColor;
and when you don't want to use one or the other set it it white. If you don't want to use a texture then bind a 1 pixel white texture. If you don't want to use the color set it to white. \$\endgroup\$