Edwards Nick
Edwards Nick

Reputation: 95

WebGL Shader Variables

var fragmentShaderSource = '\
            varying highp vec4 color; \
            varying mediump vec2 texCoord;\
            varying highp vec3 v; \
            varying highp vec3 n; \
            uniform sampler2D sampler2d;\
            void main(void) \
            { \
                gl_FragColor = color + 0.0 * texture2D(sampler2d, texCoord); \
                gl_FragColor.a = 1.0; \
                console.log(color); \
            } \
    ';

I'm new to WebGL, and the strange thing about this is, how is it possible to use variables like color or sampler2d although I didn't Initialize anything? Is there a default value?

Upvotes: 1

Views: 139

Answers (1)

Pravin Poudel
Pravin Poudel

Reputation: 1538

Webgl has a term called texture unit which is integer number. In your code above, you created a sampler and default unit is 0. What does it mean is that the sampler will act on texture associated with TEXTURE00.

If you create a texture object and bind it without making active and with out associating with any texture unit it will be binded as TEXTURE_2D having gl.TEXTURE00 as being active texture.

Suppose you created a trexture object checkerTexture and it is TEXTURE0 which is in ACTIVE_TEXTURE in image and the texture object name checkerTexture is in top of list. Now in your code if you haven't set uniform value i.e integer, default value is zero. That mean your shader will sample checkerTexture as default.


enter image description here

So if you have multiple texture and you want second texture to be sampled and taken to read the data. If you don't set uniform sampler a texture unit, it will sample TEXTURE00 which mayn't be required case every time.

So with code:

   gl.uniform1i(sampler2dLocation, 1);

This will let you take second texture object by that sampler.

Upvotes: 2

Related Questions