Skip to content

Commit

Permalink
explicit error messaging if invalid image is provided (#229)
Browse files Browse the repository at this point in the history
* explicit error messaging if invalid image is provided

* smoothlerp

* param description update
  • Loading branch information
DougLilliequist authored Aug 28, 2024
1 parent 84a2dbb commit f9d10ae
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 17 deletions.
23 changes: 6 additions & 17 deletions src/extras/TextureLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,7 @@ export class TextureLoader {
}

// Stringify props
const cacheID =
src +
wrapS +
wrapT +
anisotropy +
format +
internalFormat +
generateMipmaps +
minFilter +
magFilter +
premultiplyAlpha +
unpackAlignment +
flipY +
gl.renderer.id;
const cacheID = src + wrapS + wrapT + anisotropy + format + internalFormat + generateMipmaps + minFilter + magFilter + premultiplyAlpha + unpackAlignment + flipY + gl.renderer.id;

// Check cache for existing texture
if (cache[cacheID]) return cache[cacheID];
Expand Down Expand Up @@ -183,17 +170,19 @@ function powerOfTwo(value) {
}

function decodeImage(src, flipY) {
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
if (isCreateImageBitmap()) {
fetch(src, { mode: 'cors' })
.then((r) => r.blob())
.then((b) => createImageBitmap(b, { imageOrientation: flipY ? 'flipY' : 'none', premultiplyAlpha: 'none' }))
.then(resolve);
.then(resolve)
.catch((err) => reject(err));
} else {
const img = new Image();

img.crossOrigin = '';
img.src = src;
img.onerror = ({ type }) => reject(`${type}: Loading image`);
img.onload = () => resolve(img);
}
});
Expand All @@ -208,4 +197,4 @@ function isCreateImageBitmap() {
return false;
}
return true;
}
}
5 changes: 5 additions & 0 deletions src/math/Vec2.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ export class Vec2 extends Array {
return this;
}

smoothLerp(v, decay, dt) {
Vec2Func.smoothLerp(this, this, v, decay, dt);
return this
}

clone() {
return new Vec2(this[0], this[1]);
}
Expand Down
5 changes: 5 additions & 0 deletions src/math/Vec3.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ export class Vec3 extends Array {
return this;
}

smoothLerp(v, decay, dt) {
Vec3Func.smoothLerp(this, this, v, decay, dt);
return this;
}

clone() {
return new Vec3(this[0], this[1], this[2]);
}
Expand Down
20 changes: 20 additions & 0 deletions src/math/functions/Vec2Func.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,26 @@ export function lerp(out, a, b, t) {
return out;
}

/**
* Performs a frame rate independant, linear interpolation between two vec2's
*
* @param {vec2} out the receiving vector
* @param {vec2} a the first operand
* @param {vec2} b the second operand
* @param {Number} decay decay constant for interpolation. useful range between 1 and 25, from slow to fast.
* @param {Number} dt delta time
* @returns {vec2} out
*/
export function smoothLerp(out, a, b, decay, dt) {
const exp = Math.exp(-decay * dt);
let ax = a[0];
let ay = a[1];

out[0] = b[0] + (ax - b[0]) * exp;
out[1] = b[1] + (ay - b[1]) * exp;
return out;
}

/**
* Transforms the vec2 with a mat2
*
Expand Down
22 changes: 22 additions & 0 deletions src/math/functions/Vec3Func.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,28 @@ export function lerp(out, a, b, t) {
return out;
}

/**
* Performs a frame rate independant, linear interpolation between two vec3's
*
* @param {vec3} out the receiving vector
* @param {vec3} a the first operand
* @param {vec3} b the second operand
* @param {Number} decay decay constant for interpolation. useful range between 1 and 25, from slow to fast.
* @param {Number} dt delta time
* @returns {vec3} out
*/
export function smoothLerp(out, a, b, decay, dt) {
const exp = Math.exp(-decay * dt);
let ax = a[0];
let ay = a[1];
let az = a[2];

out[0] = b[0] + (ax - b[0]) * exp;
out[1] = b[1] + (ay - b[1]) * exp;
out[2] = b[2] + (az - b[2]) * exp;
return out;
}

/**
* Transforms the vec3 with a mat4.
* 4th vector component is implicitly '1'
Expand Down

0 comments on commit f9d10ae

Please sign in to comment.