mirror of
https://github.com/LucasDower/ObjToSchematic.git
synced 2025-12-11 20:15:30 +01:00
Minor texture sampling optimisation
This commit is contained in:
parent
48e884c4e6
commit
c6da004f6e
@ -88,11 +88,12 @@ export namespace RGBAUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function lerp(a: RGBA, b: RGBA, alpha: number) {
|
export function lerp(a: RGBA, b: RGBA, alpha: number) {
|
||||||
|
const invAlpha = 1 - alpha;
|
||||||
return {
|
return {
|
||||||
r: a.r * (1 - alpha) + b.r * alpha,
|
r: a.r * invAlpha + b.r * alpha,
|
||||||
g: a.g * (1 - alpha) + b.g * alpha,
|
g: a.g * invAlpha + b.g * alpha,
|
||||||
b: a.b * (1 - alpha) + b.b * alpha,
|
b: a.b * invAlpha + b.b * alpha,
|
||||||
a: a.a * (1 - alpha) + b.a * alpha,
|
a: a.a * invAlpha + b.a * alpha,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -143,13 +143,15 @@ export class Texture {
|
|||||||
return RGBAColours.MAGENTA;
|
return RGBAColours.MAGENTA;
|
||||||
}
|
}
|
||||||
|
|
||||||
const A = Texture._sampleImage(xLeft, yUp, image);
|
const top = Texture._sampleImagePair(xLeft, yUp, image);
|
||||||
const B = Texture._sampleImage(xRight, yUp, image);
|
//const A = Texture._sampleImage(xLeft, yUp, image);
|
||||||
const AB = RGBAUtil.lerp(A, B, u);
|
//const B = Texture._sampleImage(xRight, yUp, image);
|
||||||
|
const AB = RGBAUtil.lerp(top.left, top.right, u);
|
||||||
|
|
||||||
const C = Texture._sampleImage(xLeft, yDown, image);
|
const bottom = Texture._sampleImagePair(xLeft, yDown, image);
|
||||||
const D = Texture._sampleImage(xRight, yDown, image);
|
//const C = Texture._sampleImage(xLeft, yDown, image);
|
||||||
const CD = RGBAUtil.lerp(C, D, u);
|
//const D = Texture._sampleImage(xRight, yDown, image);
|
||||||
|
const CD = RGBAUtil.lerp(bottom.left, bottom.right, u);
|
||||||
|
|
||||||
return RGBAUtil.lerp(AB, CD, v);
|
return RGBAUtil.lerp(AB, CD, v);
|
||||||
}
|
}
|
||||||
@ -209,6 +211,30 @@ export class Texture {
|
|||||||
a: image.data[index + 3] / 255,
|
a: image.data[index + 3] / 255,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static _sampleImagePair(x: number, y: number, image: ImageData) {
|
||||||
|
const cx1 = clamp(x, 0, image.width - 1);
|
||||||
|
const cx2 = clamp(x + 1, 0, image.width - 1);
|
||||||
|
const cy = clamp(y, 0, image.height - 1);
|
||||||
|
|
||||||
|
const index1 = 4 * (image.width * cy + cx1);
|
||||||
|
const index2 = 4 * (image.width * cy + cx2);
|
||||||
|
|
||||||
|
return {
|
||||||
|
left: {
|
||||||
|
r: image.data[index1 + 0] / 255,
|
||||||
|
g: image.data[index1 + 1] / 255,
|
||||||
|
b: image.data[index1 + 2] / 255,
|
||||||
|
a: image.data[index1 + 3] / 255,
|
||||||
|
},
|
||||||
|
right: {
|
||||||
|
r: image.data[index2 + 0] / 255,
|
||||||
|
g: image.data[index2 + 1] / 255,
|
||||||
|
b: image.data[index2 + 2] / 255,
|
||||||
|
a: image.data[index2 + 3] / 255,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class TextureConverter {
|
export class TextureConverter {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user