1
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?

3
  • varying parameters have an initial value that is predetermined by interpolating the matching varying parameter from the vertex shader. uniform parameters are set externally from the shader before it is used for rendering. Commented Jun 23, 2020 at 20:30
  • So can we know what the initial value is? Commented Jun 24, 2020 at 9:36
  • Initial value is zero meaning texture00. I have posted the answer, read it. Commented Jun 24, 2020 at 12:02

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.