MCPcopy Create free account
hub / github.com/processing/p5.js / setWebGLTextureParams

Function setWebGLTextureParams

src/webgl/utils.js:105–197  ·  view source on GitHub ↗
(texture, gl, webglVersion)

Source from the content-addressed store, hash-verified

103}
104
105export function setWebGLTextureParams(texture, gl, webglVersion) {
106 texture.bindTexture();
107 const glMinFilter =
108 texture.minFilter === constants.NEAREST ? gl.NEAREST :
109 texture.minFilter === constants.LINEAR_MIPMAP ? gl.LINEAR_MIPMAP_LINEAR : gl.LINEAR;
110 const glMagFilter =
111 texture.magFilter === constants.NEAREST ? gl.NEAREST : gl.LINEAR;
112
113 // for webgl 1 we need to check if the texture is power of two
114 // if it isn't we will set the wrap mode to CLAMP
115 // webgl2 will support npot REPEAT and MIRROR but we don't check for it yet
116 const isPowerOfTwo = (x) => (x & (x - 1)) === 0;
117 const textureData = texture._getTextureDataFromSource();
118
119 let wrapWidth;
120 let wrapHeight;
121
122 if (textureData.naturalWidth && textureData.naturalHeight) {
123 wrapWidth = textureData.naturalWidth;
124 wrapHeight = textureData.naturalHeight;
125 } else {
126 wrapWidth = texture.width;
127 wrapHeight = texture.height;
128 }
129
130 const widthPowerOfTwo = isPowerOfTwo(wrapWidth);
131 const heightPowerOfTwo = isPowerOfTwo(wrapHeight);
132 let glWrapS, glWrapT;
133
134 if (texture.wrapS === constants.REPEAT) {
135 if (
136 webglVersion === constants.WEBGL2 ||
137 (widthPowerOfTwo && heightPowerOfTwo)
138 ) {
139 glWrapS = gl.REPEAT;
140 } else {
141 console.warn(
142 "You tried to set the wrap mode to REPEAT but the texture size is not a power of two. Setting to CLAMP instead",
143 );
144 glWrapS = gl.CLAMP_TO_EDGE;
145 }
146 } else if (texture.wrapS === constants.MIRROR) {
147 if (
148 webglVersion === constants.WEBGL2 ||
149 (widthPowerOfTwo && heightPowerOfTwo)
150 ) {
151 glWrapS = gl.MIRRORED_REPEAT;
152 } else {
153 console.warn(
154 "You tried to set the wrap mode to MIRROR but the texture size is not a power of two. Setting to CLAMP instead",
155 );
156 glWrapS = gl.CLAMP_TO_EDGE;
157 }
158 } else {
159 // falling back to default if didn't get a proper mode
160 glWrapS = gl.CLAMP_TO_EDGE;
161 }
162

Callers 2

constructorMethod · 0.90
setTextureParamsMethod · 0.90

Calls 4

isPowerOfTwoFunction · 0.85
bindTextureMethod · 0.45
unbindTextureMethod · 0.45

Tested by

no test coverage detected