Skip to content

Commit

Permalink
organize code and revert test code
Browse files Browse the repository at this point in the history
  • Loading branch information
olimot committed Oct 12, 2024
1 parent 553f6db commit e04a4f8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
11 changes: 6 additions & 5 deletions src/cpu/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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");
Expand Down
26 changes: 13 additions & 13 deletions src/gpu/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions src/gpu/marching-cubes.vert
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}

Expand Down

0 comments on commit e04a4f8

Please sign in to comment.