Skip to content

Commit

Permalink
Fix textureWrap REPEAT for WebGL Power Of Two Image Element
Browse files Browse the repository at this point in the history
  • Loading branch information
kate-grant committed Feb 1, 2023
1 parent 69702de commit 4079722
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/webgl/p5.Texture.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 334,21 @@ p5.Texture.prototype.setWrapMode = function(wrapX, wrapY) {
// if it isn't we will set the wrap mode to CLAMP
// webgl2 will support npot REPEAT and MIRROR but we don't check for it yet
const isPowerOfTwo = x => (x & (x - 1)) === 0;
const textureData = this._getTextureDataFromSource();

let wrapWidth;
let wrapHeight;

if (textureData.naturalWidth && textureData.naturalHeight) {
wrapWidth = textureData.naturalWidth;
wrapHeight = textureData.naturalHeight;
} else {
wrapWidth = this.width;
wrapHeight = this.height;
}

const widthPowerOfTwo = isPowerOfTwo(this.width);
const heightPowerOfTwo = isPowerOfTwo(this.height);
const widthPowerOfTwo = isPowerOfTwo(wrapWidth);
const heightPowerOfTwo = isPowerOfTwo(wrapHeight);

if (wrapX === constants.REPEAT) {
if (widthPowerOfTwo && heightPowerOfTwo) {
Expand Down
29 changes: 28 additions & 1 deletion test/unit/webgl/p5.Texture.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 2,9 @@ suite('p5.Texture', function() {
var myp5;
var texImg1;
var texImg2;
var texImg3;
var imgElementNotPowerOfTwo;
var imgElementPowerOfTwo;
var canvas;

if (!window.Modernizr.webgl) {
Expand All @@ -12,15 15,27 @@ suite('p5.Texture', function() {
setup(function(done) {
myp5 = new p5(function(p) {
p.preload = function() {
// texImg2 must have powerOfTwo dimensions
texImg2 = p.loadImage('unit/assets/target.gif');

// texImg3 must NOT have powerOfTwo dimensions
texImg3 = p.loadImage('unit/assets/nyan_cat.gif');
// texture object isn't created until it's used for something:
//p.box(70, 70, 70);
};
p.setup = function() {
canvas = p.createCanvas(100, 100, p.WEBGL);
texImg1 = p.createGraphics(2, 2, p.WEBGL);
p.texture(texImg1);
p.createImg(texImg2.canvas.toDataURL(), '', 'anonymous', el => {
el.size(50, 50);
imgElementPowerOfTwo = el;
p.texture(imgElementPowerOfTwo);
});
p.createImg(texImg3.canvas.toDataURL(), '', 'anonymous', el => {
el.size(50, 50);
imgElementNotPowerOfTwo = el;
p.texture(imgElementNotPowerOfTwo);
});
done();
};
});
Expand Down Expand Up @@ -87,6 102,18 @@ suite('p5.Texture', function() {
assert.deepEqual(tex.glWrapS, myp5._renderer.GL.MIRRORED_REPEAT);
assert.deepEqual(tex.glWrapT, myp5._renderer.GL.MIRRORED_REPEAT);
});
test('Set wrap mode REPEAT if src dimensions is powerOfTwo', function() {
const tex = myp5._renderer.getTexture(imgElementPowerOfTwo);
tex.setWrapMode(myp5.REPEAT, myp5.REPEAT);
assert.deepEqual(tex.glWrapS, myp5._renderer.GL.REPEAT);
assert.deepEqual(tex.glWrapT, myp5._renderer.GL.REPEAT);
});
test('Set default wrap mode CLAMP if src dimensions != powerOfTwo', function() {
const tex = myp5._renderer.getTexture(imgElementNotPowerOfTwo);
tex.setWrapMode(myp5.REPEAT, myp5.REPEAT);
assert.deepEqual(tex.glWrapS, myp5._renderer.GL.CLAMP_TO_EDGE);
assert.deepEqual(tex.glWrapT, myp5._renderer.GL.CLAMP_TO_EDGE);
});
test('Set textureMode to NORMAL', function() {
myp5.textureMode(myp5.NORMAL);
assert.deepEqual(myp5._renderer.textureMode, myp5.NORMAL);
Expand Down

0 comments on commit 4079722

Please sign in to comment.