Skip to content

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
olimot committed Oct 11, 2024
1 parent 41a86af commit 5595b75
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ gl.bindAttribLocation(program, 1, "NORMAL");
gl.linkProgram(program);
console.log(gl.getShaderInfoLog(vert));
console.log(gl.getShaderInfoLog(frag));

const viewProjectionLoc = gl.getUniformLocation(program, "viewProjection");

const vertexArray = gl.createVertexArray();
Expand Down Expand Up @@ -144,16 +145,15 @@ requestAnimationFrame(function frame() {
mat4.ortho(projection, -100, 100, -80, 80, 0, 320); // alternatively
mat4.perspective(projection, Math.PI / 4, aspectRatio, 0.01, +Infinity);
mat4.multiply(viewProjection, projection, view);

gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

gl.useProgram(program);
gl.bindVertexArray(vertexArray);
gl.uniformMatrix4fv(viewProjectionLoc, false, viewProjection);
gl.frontFace(gl.CCW);
gl.enable(gl.CULL_FACE);
gl.enable(gl.DEPTH_TEST);
gl.disable(gl.BLEND);
gl.drawArrays(gl.TRIANGLES, 0, position.length / 3);
gl.bindVertexArray(null);

Expand Down
54 changes: 27 additions & 27 deletions src/marching-cubes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,37 +370,37 @@ function runMarchingCubes(
if (e7 < isolevel) cubeindex += 128;
const edge = edgeTable[cubeindex];
if (edge !== 0) {
const norm0x = e1 - data.src[dataindex - dx];
const norm0y = e4 - data.src[dataindex - dy];
const norm0z = e3 - data.src[dataindex - dz];
const norm0x = data.src[dataindex - dx] - e1;
const norm0y = data.src[dataindex - dy] - e4;
const norm0z = data.src[dataindex - dz] - e3;

const norm1x = data.src[dataindex + dx * 2] - e0;
const norm1y = e5 - data.src[dataindex + dx - dy];
const norm1z = e2 - data.src[dataindex + dx - dz];
const norm1x = e0 - data.src[dataindex + dx * 2];
const norm1y = data.src[dataindex + dx - dy] - e5;
const norm1z = data.src[dataindex + dx - dz] - e2;

const norm2x = data.src[dataindex + dx * 2 + dz] - e3;
const norm2y = e6 - data.src[dataindex + dx - dy + dz];
const norm2z = data.src[dataindex + dx + dz * 2] - e1;
const norm2x = e3 - data.src[dataindex + dx * 2 + dz];
const norm2y = data.src[dataindex + dx - dy + dz] - e6;
const norm2z = e1 - data.src[dataindex + dx + dz * 2];

const norm3x = e2 - data.src[dataindex - dx + dz];
const norm3y = e7 - data.src[dataindex - dy + dz];
const norm3z = data.src[dataindex + dz * 2] - e0;
const norm3x = data.src[dataindex - dx + dz] - e2;
const norm3y = data.src[dataindex - dy + dz] - e7;
const norm3z = e0 - data.src[dataindex + dz * 2];

const norm4x = e5 - data.src[dataindex - dx + dy];
const norm4y = data.src[dataindex + dy * 2] - e0;
const norm4z = e7 - data.src[dataindex + dy - dz];
const norm4x = data.src[dataindex - dx + dy] - e5;
const norm4y = e0 - data.src[dataindex + dy * 2];
const norm4z = data.src[dataindex + dy - dz] - e7;

const norm5x = data.src[dataindex + dx * 2 + dy] - e4;
const norm5y = data.src[dataindex + dx + dy * 2] - e1;
const norm5z = e6 - data.src[dataindex + dx + dy - dz];
const norm5x = e4 - data.src[dataindex + dx * 2 + dy];
const norm5y = e1 - data.src[dataindex + dx + dy * 2];
const norm5z = data.src[dataindex + dx + dy - dz] - e6;

const norm6x = data.src[dataindex + dx * 2 + dy + dz] - e7;
const norm6y = data.src[dataindex + dx + dy * 2 + dz] - e2;
const norm6z = data.src[dataindex + dx + dy + dz * 2] - e5;
const norm6x = e7 - data.src[dataindex + dx * 2 + dy + dz];
const norm6y = e2 - data.src[dataindex + dx + dy * 2 + dz];
const norm6z = e5 - data.src[dataindex + dx + dy + dz * 2];

const norm7x = e6 - data.src[dataindex - dx + dy + dz];
const norm7y = data.src[dataindex + dy * 2 + dz] - e3;
const norm7z = data.src[dataindex + dy + dz * 2] - e4;
const norm7x = data.src[dataindex - dx + dy + dz] - e6;
const norm7y = e3 - data.src[dataindex + dy * 2 + dz];
const norm7z = e4 - data.src[dataindex + dy + dz * 2];

if (edge & 1) {
const mu = (isolevel - e0) / (e1 - e0);
Expand Down Expand Up @@ -517,13 +517,13 @@ function runMarchingCubes(
const e = edges[t[i]];
const n = normalEdges[t[i]];
vertices[vIdx] = e[0];
normals[vIdx] = -n[0];
normals[vIdx] = n[0];
vIdx++;
vertices[vIdx] = e[1];
normals[vIdx] = -n[1];
normals[vIdx] = n[1];
vIdx++;
vertices[vIdx] = e[2];
normals[vIdx] = -n[2];
normals[vIdx] = n[2];
vIdx++;
}
}
Expand Down

0 comments on commit 5595b75

Please sign in to comment.