Skip to content

Commit ea0fa1d

Browse files
authored
Merge pull request #5557 from aferriss/float-texture
Allow settings object for p5.Texture
2 parents acc7c26 + 0d1573c commit ea0fa1d

File tree

1 file changed

+50
-9
lines changed

1 file changed

+50
-9
lines changed

src/webgl/p5.Texture.js

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,62 @@ import * as constants from '../core/constants';
1717
* will provide the GL context for this new p5.Texture
1818
* @param {p5.Image|p5.Graphics|p5.Element|p5.MediaElement|ImageData} [obj] the
1919
* object containing the image data to store in the texture.
20+
* @param {Object} [settings] optional A javascript object containing texture
21+
* settings.
22+
* @param {Number} [settings.format] optional The internal color component
23+
* format for the texture. Possible values for format include gl.RGBA,
24+
* gl.RGB, gl.ALPHA, gl.LUMINANCE, gl.LUMINANCE_ALPHA. Defaults to gl.RBGA
25+
* @param {Number} [settings.minFilter] optional The texture minification
26+
* filter setting. Possible values are gl.NEAREST or gl.LINEAR. Defaults
27+
* to gl.LINEAR. Note, Mipmaps are not implemented in p5.
28+
* @param {Number} [settings.magFilter] optional The texture magnification
29+
* filter setting. Possible values are gl.NEAREST or gl.LINEAR. Defaults
30+
* to gl.LINEAR. Note, Mipmaps are not implemented in p5.
31+
* @param {Number} [settings.wrapS] optional The texture wrap settings for
32+
* the s coordinate, or x axis. Possible values are gl.CLAMP_TO_EDGE,
33+
* gl.REPEAT, and gl.MIRRORED_REPEAT. The mirror settings are only available
34+
* when using a power of two sized texture. Defaults to gl.CLAMP_TO_EDGE
35+
* @param {Number} [settings.wrapT] optional The texture wrap settings for
36+
* the t coordinate, or y axis. Possible values are gl.CLAMP_TO_EDGE,
37+
* gl.REPEAT, and gl.MIRRORED_REPEAT. The mirror settings are only available
38+
* when using a power of two sized texture. Defaults to gl.CLAMP_TO_EDGE
39+
* @param {Number} [settings.dataType] optional The data type of the texel
40+
* data. Possible values are gl.UNSIGNED_BYTE or gl.FLOAT. There are more
41+
* formats that are not implemented in p5. Defaults to gl.UNSIGNED_BYTE.
2042
*/
21-
p5.Texture = function(renderer, obj) {
43+
p5.Texture = function(renderer, obj, settings) {
2244
this._renderer = renderer;
2345

2446
const gl = this._renderer.GL;
2547

48+
settings = settings || {};
49+
50+
if (settings.dataType === gl.FLOAT) {
51+
const ext = gl.getExtension('OES_texture_float');
52+
if (!ext) {
53+
console.log(
54+
"Oh no, your device doesn't support floating point textures!"
55+
);
56+
}
57+
58+
const linear = gl.getExtension('OES_texture_float_linear');
59+
if (!linear) {
60+
console.log(
61+
"Ack! Your device doesn't support linear filtering for floating point textures"
62+
);
63+
}
64+
}
65+
2666
this.src = obj;
2767
this.glTex = undefined;
2868
this.glTarget = gl.TEXTURE_2D;
29-
this.glFormat = gl.RGBA;
69+
this.glFormat = settings.format || gl.RGBA;
3070
this.mipmaps = false;
31-
this.glMinFilter = gl.LINEAR;
32-
this.glMagFilter = gl.LINEAR;
33-
this.glWrapS = gl.CLAMP_TO_EDGE;
34-
this.glWrapT = gl.CLAMP_TO_EDGE;
71+
this.glMinFilter = settings.minFilter || gl.LINEAR;
72+
this.glMagFilter = settings.magFilter || gl.LINEAR;
73+
this.glWrapS = settings.wrapS || gl.CLAMP_TO_EDGE;
74+
this.glWrapT = settings.wrapT || gl.CLAMP_TO_EDGE;
75+
this.glDataType = settings.dataType || gl.UNSIGNED_BYTE;
3576

3677
// used to determine if this texture might need constant updating
3778
// because it is a video or gif.
@@ -110,7 +151,7 @@ p5.Texture.prototype.init = function(data) {
110151
1,
111152
0,
112153
this.glFormat,
113-
gl.UNSIGNED_BYTE,
154+
this.glDataType,
114155
tmpdata
115156
);
116157
} else {
@@ -120,7 +161,7 @@ p5.Texture.prototype.init = function(data) {
120161
0,
121162
this.glFormat,
122163
this.glFormat,
123-
gl.UNSIGNED_BYTE,
164+
this.glDataType,
124165
data
125166
);
126167
}
@@ -211,7 +252,7 @@ p5.Texture.prototype.update = function() {
211252
0,
212253
this.glFormat,
213254
this.glFormat,
214-
gl.UNSIGNED_BYTE,
255+
this.glDataType,
215256
textureData
216257
);
217258
}

0 commit comments

Comments
 (0)