Skip to content
Merged
Show file tree
Hide file tree
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
16 changes: 0 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
"spark-internal-rs": "file:rust/spark-internal-rs/pkg"
},
"dependencies": {
"@jsquash/webp": "^1.5.0",
"fflate": "^0.8.2"
},
"keywords": ["3d", "three.js", "gsplats", "3dgs", "gaussian", "splats"]
Expand Down
52 changes: 49 additions & 3 deletions src/pcsogs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { decode as decodeWebp } from "@jsquash/webp";
import type { PcSogsJson } from "./SplatLoader";
import {
computeMaxSplats,
Expand Down Expand Up @@ -149,9 +148,56 @@ export async function unpackPcSogs(
return { packedArray, numSplats, extra };
}

// WebGL context for reading raw pixel data of WebP images
let offscreenGlContext: WebGL2RenderingContext | null = null;

async function decodeImage(fileBytes: ArrayBuffer) {
const { data: rgba, width, height } = await decodeWebp(fileBytes);
return { rgba, width, height };
if (!offscreenGlContext) {
const canvas = new OffscreenCanvas(1, 1);
offscreenGlContext = canvas.getContext("webgl2");
if (!offscreenGlContext) {
throw new Error("Failed to create WebGL2 context");
}
}

const imageBlob = new Blob([fileBytes]);
const bitmap = await createImageBitmap(imageBlob, {
premultiplyAlpha: "none",
});

const gl = offscreenGlContext;
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, bitmap);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);

const framebuffer = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
gl.framebufferTexture2D(
gl.FRAMEBUFFER,
gl.COLOR_ATTACHMENT0,
gl.TEXTURE_2D,
texture,
0,
);

const data = new Uint8Array(bitmap.width * bitmap.height * 4);
gl.readPixels(
0,
0,
bitmap.width,
bitmap.height,
gl.RGBA,
gl.UNSIGNED_BYTE,
data,
);

gl.deleteTexture(texture);
gl.deleteFramebuffer(framebuffer);

return { rgba: data, width: bitmap.width, height: bitmap.height };
}

async function decodeImageRgba(fileBytes: ArrayBuffer) {
Expand Down