diff --git a/src/cpu/main.ts b/src/cpu/main.ts index 27fa8b8..5a764db 100644 --- a/src/cpu/main.ts +++ b/src/cpu/main.ts @@ -5,17 +5,13 @@ import polygonize from "./marching-cubes"; import { moveXY, pinchOrbit, rotateOrbit } from "../orbital"; import rawURL from "../u8-mri-200x160x160.raw?url"; -const projection = mat4.create(); -const target = vec3.fromValues(100, 80, 80); -const view = mat4.create(); -const viewProjection = mat4.identity(mat4.create()); const buffer = await fetch(rawURL).then((res) => res.arrayBuffer()); const original = new Uint8Array(buffer); const field = { width: 200, height: 160, depth: 160, - src: new Uint8Array(original.length), + src: new Uint8Array(original), }; for (let z = 0; z < field.depth; z += 1) { @@ -31,6 +27,11 @@ for (let z = 0; z < field.depth; z += 1) { } } +const projection = mat4.create(); +const target = vec3.fromValues(100, 80, 80); +const view = mat4.lookAt(mat4.create(), [100, 80, -320], target, up); +const viewProjection = mat4.identity(mat4.create()); + console.time("Marching Cubes"); const [position, normal] = polygonize(field, 90); console.timeEnd("Marching Cubes"); diff --git a/src/gpu/main.ts b/src/gpu/main.ts index 9a78f89..300ceb5 100644 --- a/src/gpu/main.ts +++ b/src/gpu/main.ts @@ -6,21 +6,18 @@ import { moveXY, pinchOrbit, rotateOrbit } from "../orbital"; import rawURL from "../u8-mri-200x160x160.raw?url"; import triTableURL from "../u8-tri-table-256x16.bin?url"; -const projection = mat4.create(); -const target = vec3.fromValues(100, 80, 80); -const view = mat4.create(); -const viewProjection = mat4.identity(mat4.create()); +const triTable = await fetch(triTableURL).then(async (res) => { + return new Uint8Array(await res.arrayBuffer()); +}); + const buffer = await fetch(rawURL).then((res) => res.arrayBuffer()); const original = new Uint8Array(buffer); const field = { width: 200, height: 160, depth: 160, - src: new Float32Array(original.length), + src: new Uint8Array(original), }; -export const triTable = await fetch(triTableURL).then(async (res) => { - return new Uint8Array(await res.arrayBuffer()); -}); for (let z = 0; z < field.depth; z += 1) { for (let y = 0; y < field.height; y += 1) { @@ -35,9 +32,12 @@ for (let z = 0; z < field.depth; z += 1) { } } -const canvas = document.getElementById("screen") as HTMLCanvasElement; +const projection = mat4.create(); +const target = vec3.fromValues(100, 80, 80); +const view = mat4.lookAt(mat4.create(), [100, 80, -320], target, up); +const viewProjection = mat4.identity(mat4.create()); -mat4.lookAt(view, [100, 80, -320], target, up); +const canvas = document.getElementById("screen") as HTMLCanvasElement; listenInputEvents(canvas, ({ keys, delta, buttons }) => { if ((keys.Space && keys.ShiftLeft) || buttons === 5) { @@ -134,13 +134,13 @@ gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_WRAP_R, gl.CLAMP_TO_EDGE); gl.texImage3D( gl.TEXTURE_3D, 0, - gl.R32F, + gl.R8UI, field.width, field.height, field.depth, 0, - gl.RED, - gl.FLOAT, + gl.RED_INTEGER, + gl.UNSIGNED_BYTE, field.src, ); gl.uniform1i(fieldLoc, 1); diff --git a/src/gpu/marching-cubes.vert b/src/gpu/marching-cubes.vert index a325f3a..e7160b3 100644 --- a/src/gpu/marching-cubes.vert +++ b/src/gpu/marching-cubes.vert @@ -1,7 +1,7 @@ #version 300 es uniform highp usampler2D triTable; -uniform highp sampler3D field; +uniform highp usampler3D field; uniform mat4 viewProjection; uniform float isolevel; uniform ivec3 fieldSize; @@ -11,10 +11,10 @@ out vec3 vNormal; vec4 getValue(ivec3 p) { vec4 value; - value.w = (texelFetch(field, ivec3(p.x, p.y, p.z), 0).x); - value.x = float(texelFetch(field, ivec3(p.x + 1, p.y, p.z), 0).x) - (texelFetch(field, ivec3(p.x - 1, p.y, p.z), 0).x); - value.y = float(texelFetch(field, ivec3(p.x, p.y + 1, p.z), 0).x) - (texelFetch(field, ivec3(p.x, p.y - 1, p.z), 0).x); - value.z = float(texelFetch(field, ivec3(p.x, p.y, p.z + 1), 0).x) - (texelFetch(field, ivec3(p.x, p.y, p.z - 1), 0).x); + value.w = float(texelFetch(field, ivec3(p.x, p.y, p.z), 0).x); + value.x = float(texelFetch(field, ivec3(p.x + 1, p.y, p.z), 0).x) - float(texelFetch(field, ivec3(p.x - 1, p.y, p.z), 0).x); + value.y = float(texelFetch(field, ivec3(p.x, p.y + 1, p.z), 0).x) - float(texelFetch(field, ivec3(p.x, p.y - 1, p.z), 0).x); + value.z = float(texelFetch(field, ivec3(p.x, p.y, p.z + 1), 0).x) - float(texelFetch(field, ivec3(p.x, p.y, p.z - 1), 0).x); return value; }