Skip to content

Commit

Permalink
api/texture: Have the first mip level be a reference to the base image
Browse files Browse the repository at this point in the history
  • Loading branch information
leikareipa committed Aug 26, 2023
1 parent 4f153e9 commit 6ec121c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
8 changes: 7 additions & 1 deletion samples/texture-painting/texture-painting.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,11 @@ function paint()
realY += deltaY;
}

this.texture.refresh();
// Since we modified the texture's pixels, we should call texture.refresh() to
// update all of the texture's mip levels with the new pixel data. However, in
// this rendering sample, we only ever display the first mip level (which is
// the base image), so we can save some performance by not refreshing the other
// mip levels.
//
//this.texture.refresh();
}
21 changes: 17 additions & 4 deletions src/api/texture.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,11 @@ const schema = {
"width": ["number"],
"height": ["number"],
"pixels": ["Uint8ClampedArray"],
"mipLevels": [["object"]],
"mipLevels": [["object", "Texture"]],
},
},
};

// Texture with 32-bit color.
export function Texture(data = {})
{
data = structuredClone(data);
Expand Down Expand Up @@ -206,9 +205,23 @@ function decode_pixel_data(data)
// full-resolution texture, and mipmaps[mipmaps.length-1] is the smallest, 1 x 1 texture.
function regenerate_mipmaps()
{
this.mipLevels = [];
Assert?.(
this.hasOwnProperty("$constructor") &&
(this.$constructor === "Texture"),
"Expected 'this' to point to a Texture"
);

// The first mip level is the base image, so we can just reference the base data.
this.mipLevels = [this];

if (
(this.width === 1) &&
(this.height === 1)
){
return;
}

for (let m = 0; ; m++)
for (let m = 1; ; m++)
{
const mipWidth = Math.max(1, Math.floor(this.width / Math.pow(2, m)));
const mipHeight = Math.max(1, Math.floor(this.height / Math.pow(2, m)));
Expand Down

0 comments on commit 6ec121c

Please sign in to comment.