greggman / twgl.js

A Tiny WebGL helper Library
http://twgljs.org
MIT License
2.61k stars 258 forks source link

Unsetting a texture in webgl2 #217

Closed zackgomez closed 1 year ago

zackgomez commented 1 year ago

Hi,

I have a shader with an optional normal texture param. The shader samples the texture and uses a default normal on a "black" texel.

This works fine with gl.bindTexture(..., null) in raw webgl2, but when using twgl I am unable to bind the optional texture to null, with setUniforms or the uniform setter directly.

      twglProgram.uniformSetters.uNormalTexture(null);

Here is a stack trace image

Now of course I could (and perhaps should) pass another uniform instead of reading the default value and manually unbinding the texture unit but this seems like something that ought to be supported by twgl.

I believe the fix is as easy as changing the if statement to be textureOrPair == null || isTexture(...).

Thank you for your time.

zackgomez commented 1 year ago

Further note that the webgl1 path would support binding a null texture

image

greggman commented 1 year ago

good point!

https://github.com/greggman/twgl.js/commit/f113b4dcca398c1f0b1f69eab475493c122fcadc

zackgomez commented 1 year ago

Wow, thanks for the quick fix.

👍