Skip to content

Allow settings object for p5.Texture #5557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 50 additions & 9 deletions src/webgl/p5.Texture.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,62 @@ import * as constants from '../core/constants';
* will provide the GL context for this new p5.Texture
* @param {p5.Image|p5.Graphics|p5.Element|p5.MediaElement|ImageData} [obj] the
* object containing the image data to store in the texture.
* @param {Object} [settings] optional A javascript object containing texture
* settings.
* @param {Number} [settings.format] optional The internal color component
* format for the texture. Possible values for format include gl.RGBA,
* gl.RGB, gl.ALPHA, gl.LUMINANCE, gl.LUMINANCE_ALPHA. Defaults to gl.RBGA
* @param {Number} [settings.minFilter] optional The texture minification
* filter setting. Possible values are gl.NEAREST or gl.LINEAR. Defaults
* to gl.LINEAR. Note, Mipmaps are not implemented in p5.
* @param {Number} [settings.magFilter] optional The texture magnification
* filter setting. Possible values are gl.NEAREST or gl.LINEAR. Defaults
* to gl.LINEAR. Note, Mipmaps are not implemented in p5.
* @param {Number} [settings.wrapS] optional The texture wrap settings for
* the s coordinate, or x axis. Possible values are gl.CLAMP_TO_EDGE,
* gl.REPEAT, and gl.MIRRORED_REPEAT. The mirror settings are only available
* when using a power of two sized texture. Defaults to gl.CLAMP_TO_EDGE
* @param {Number} [settings.wrapT] optional The texture wrap settings for
* the t coordinate, or y axis. Possible values are gl.CLAMP_TO_EDGE,
* gl.REPEAT, and gl.MIRRORED_REPEAT. The mirror settings are only available
* when using a power of two sized texture. Defaults to gl.CLAMP_TO_EDGE
* @param {Number} [settings.dataType] optional The data type of the texel
* data. Possible values are gl.UNSIGNED_BYTE or gl.FLOAT. There are more
* formats that are not implemented in p5. Defaults to gl.UNSIGNED_BYTE.
*/
p5.Texture = function(renderer, obj) {
p5.Texture = function(renderer, obj, settings) {
this._renderer = renderer;

const gl = this._renderer.GL;

settings = settings || {};

if (settings.dataType === gl.FLOAT) {
const ext = gl.getExtension('OES_texture_float');
if (!ext) {
console.log(
"Oh no, your device doesn't support floating point textures!"
);
}

const linear = gl.getExtension('OES_texture_float_linear');
if (!linear) {
console.log(
"Ack! Your device doesn't support linear filtering for floating point textures"
);
}
}

this.src = obj;
this.glTex = undefined;
this.glTarget = gl.TEXTURE_2D;
this.glFormat = gl.RGBA;
this.glFormat = settings.format || gl.RGBA;
this.mipmaps = false;
this.glMinFilter = gl.LINEAR;
this.glMagFilter = gl.LINEAR;
this.glWrapS = gl.CLAMP_TO_EDGE;
this.glWrapT = gl.CLAMP_TO_EDGE;
this.glMinFilter = settings.minFilter || gl.LINEAR;
this.glMagFilter = settings.magFilter || gl.LINEAR;
this.glWrapS = settings.wrapS || gl.CLAMP_TO_EDGE;
this.glWrapT = settings.wrapT || gl.CLAMP_TO_EDGE;
this.glDataType = settings.dataType || gl.UNSIGNED_BYTE;

// used to determine if this texture might need constant updating
// because it is a video or gif.
Expand Down Expand Up @@ -110,7 +151,7 @@ p5.Texture.prototype.init = function(data) {
1,
0,
this.glFormat,
gl.UNSIGNED_BYTE,
this.glDataType,
tmpdata
);
} else {
Expand All @@ -120,7 +161,7 @@ p5.Texture.prototype.init = function(data) {
0,
this.glFormat,
this.glFormat,
gl.UNSIGNED_BYTE,
this.glDataType,
data
);
}
Expand Down Expand Up @@ -211,7 +252,7 @@ p5.Texture.prototype.update = function() {
0,
this.glFormat,
this.glFormat,
gl.UNSIGNED_BYTE,
this.glDataType,
textureData
);
}
Expand Down