Skip to content

Commit

Permalink
Fix model centering
Browse files Browse the repository at this point in the history
  • Loading branch information
gmlewis committed Sep 7, 2020
1 parent 3aaf8f2 commit 78114f7
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 58 deletions.
7 changes: 5 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#!/bin/bash -ex
# Note that this hack keeps changing for every release of Go.
# This script is currently for use with Go 1.15.1:
cat $(go env GOROOT)/misc/wasm/wasm_exec.js |
sed -e 's/global.require.main === module/false/' \
-e 's/global.fs = require.*$/try { & } catch (err) {}/' \
-e 's/const fs = require.*$/try { &/' \
-e 's/global.fs = fs;/& } } catch (err) {} {/' \
> js/wasm_exec.js
# tinygo 0.12.0 doesn't build this app without errors. Use the Go compiler instead.
# tinygo 0.14.1 doesn't build this app without errors. Use the Go compiler instead.
# tinygo build -o main.wasm -target wasm .
GOARCH=wasm GOOS=js go build -o main.wasm
56 changes: 36 additions & 20 deletions js/startup.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,15 @@ function copyUniforms() {
let modelCentroidNull = null;
let compilerSource = '';
function loadNewModel(source) {
console.log("Compiling new model.");
console.log('Compiling new model.');
let firstRun = (compilerSource == '');
compilerSource = source;
uniformsChanged();
let lookAt = getLookAt();
controls.target0 = new THREE.Vector3(lookAt[0], lookAt[1], lookAt[2]);
if (firstRun) {
viewCallbacks[6](); // Reset to default view.
}
render();
}

Expand All @@ -408,23 +414,32 @@ function rangeValuesChanged() {
const urz = rangeValues.urz;
uniforms.u_ll.value.set(llx, lly, llz);
uniforms.u_ur.value.set(urx, ury, urz);
let maxval = (urx > ury) ? ury : ury;
maxval = (maxval > urz) ? maxval : urz;
let maxval = ((urx - llx) > (ury - lly)) ? (urx - llx) : (ury - lly);
maxval = (maxval > (urz - llz)) ? maxval : (urz - llz);
resetCameraD = maxval;
// console.log('rangeValuesChanged: resetCameraD=' + resetCameraD.toString());

const ll = new THREE.Vector3(llx, lly, llz);
const ur = new THREE.Vector3(urx, ury, urz);
const minD = -ll.length();
const maxD = ur.length();
const lookAt = getLookAt();
const center = new THREE.Vector3(lookAt[0], lookAt[1], lookAt[2]);
const minD = -(new THREE.Vector3().subVectors(center, ll)).length();
const maxD = (new THREE.Vector3().subVectors(ur, center)).length();
let diagonal = maxD - minD;
if (diagonal <= 0.0) {
diagonal = 1.0; // Avoid divide-by-zero.
}
// console.log('rangeValuesChanged: minD=' + minD.toString() + ', maxD=' + maxD.toString() + ', diagonal=' + diagonal.toString());

scene.dispose(); // This alone is not enough. Need to create a brand new scene.
scene = new THREE.Scene(); // Eventually add a light?

modelCentroidNull = new THREE.Object3D()
modelCentroidNull.translateX(lookAt[0]);
modelCentroidNull.translateY(lookAt[1]);
modelCentroidNull.translateZ(lookAt[2]);
// console.log('rangeValuesChanged: modelCentroidNull.position=', modelCentroidNull.position);

scene.add(modelCentroidNull);
// modelCentroidNull.add(new THREE.AxesHelper(diagonal)); // for debugging
// TODO: make this a GUI option?
Expand All @@ -434,6 +449,7 @@ function rangeValuesChanged() {
for (let d = minD + dStep; d < maxD; d += dStep) {
let myUniforms = copyUniforms();
myUniforms.u_d.value = (d - minD) / (maxD - dStep - minD);
// console.log('d=' + d.toString() + ', u_d=' + myUniforms.u_d.value.toString());
let material = new THREE.ShaderMaterial({ uniforms: myUniforms, vertexShader: vs, fragmentShader: fsHeader + compilerSource, side: THREE.DoubleSide, transparent: true });
let plane = new THREE.PlaneBufferGeometry(diagonal, diagonal); // Should this always fill the viewport?
let mesh = new THREE.Mesh(plane, material);
Expand Down Expand Up @@ -462,20 +478,20 @@ const viewRotations = [[0, halfPi, 0], [0, -halfPi, 0], [-halfPi, 0, 0], [halfPi
[quarterPi, 0, quarterPi, 'ZYX'], [-quarterPi, 0, -quarterPi, 'ZYX'], [-quarterPi, 0, quarterPi, 'ZYX'], [quarterPi, 0, -quarterPi, 'ZYX'],
[-quarterPi, 0, quarterPi, 'ZYX'], [quarterPi, 0, -quarterPi, 'ZYX'], [quarterPi, 0, quarterPi, 'ZYX'], [-quarterPi, 0, -quarterPi, 'ZYX']];
const viewCallbacks = [
function () { toOrtho(rightView); controls.position0.set(resetCameraD, 0, 0); controls.up0.set(0, 0, 1); controls.reset(); }, // right
function () { toOrtho(leftView); controls.position0.set(-resetCameraD, 0, 0); controls.up0.set(0, 0, 1); controls.reset(); }, // left
function () { toOrtho(backView); controls.position0.set(0, resetCameraD, 0); controls.up0.set(0, 0, 1); controls.reset(); }, // back
function () { toOrtho(frontView); controls.position0.set(0, -resetCameraD, 0); controls.up0.set(0, 0, 1); controls.reset(); }, // front
function () { toOrtho(topView); controls.position0.set(0, 0, resetCameraD); controls.up0.set(0, 1, 0); controls.reset(); }, // top
function () { toOrtho(bottomView); controls.position0.set(0, 0, -resetCameraD); controls.up0.set(0, -1, 0); controls.reset(); }, // bottom
function () { toPersp(); controls.position0.set(resetCameraD, -resetCameraD, resetCameraD); controls.up0.set(0, 0, 1); controls.reset(); },
function () { toPersp(); controls.position0.set(resetCameraD, resetCameraD, resetCameraD); controls.up0.set(0, 0, 1); controls.reset(); },
function () { toPersp(); controls.position0.set(-resetCameraD, resetCameraD, resetCameraD); controls.up0.set(0, 0, 1); controls.reset(); },
function () { toPersp(); controls.position0.set(-resetCameraD, -resetCameraD, resetCameraD); controls.up0.set(0, 0, 1); controls.reset(); },
function () { toPersp(); controls.position0.set(resetCameraD, -resetCameraD, -resetCameraD); controls.up0.set(0, 0, 1); controls.reset(); },
function () { toPersp(); controls.position0.set(resetCameraD, resetCameraD, -resetCameraD); controls.up0.set(0, 0, 1); controls.reset(); },
function () { toPersp(); controls.position0.set(-resetCameraD, resetCameraD, -resetCameraD); controls.up0.set(0, 0, 1); controls.reset(); },
function () { toPersp(); controls.position0.set(-resetCameraD, -resetCameraD, -resetCameraD); controls.up0.set(0, 0, 1); controls.reset(); }
function () { toOrtho(rightView); let p = getLookAt(); controls.position0.set(p[0] + resetCameraD, p[1] + 0, p[2] + 0); controls.up0.set(0, 0, 1); controls.reset(); }, // right
function () { toOrtho(leftView); let p = getLookAt(); controls.position0.set(p[0] + -resetCameraD, p[1] + 0, p[2] + 0); controls.up0.set(0, 0, 1); controls.reset(); }, // left
function () { toOrtho(backView); let p = getLookAt(); controls.position0.set(p[0] + 0, p[1] + resetCameraD, p[2] + 0); controls.up0.set(0, 0, 1); controls.reset(); }, // back
function () { toOrtho(frontView); let p = getLookAt(); controls.position0.set(p[0] + 0, p[1] + -resetCameraD, p[2] + 0); controls.up0.set(0, 0, 1); controls.reset(); }, // front
function () { toOrtho(topView); let p = getLookAt(); controls.position0.set(p[0] + 0, p[1] + 0, p[2] + resetCameraD); controls.up0.set(0, 1, 0); controls.reset(); }, // top
function () { toOrtho(bottomView); let p = getLookAt(); controls.position0.set(p[0] + 0, p[1] + 0, p[2] + -resetCameraD); controls.up0.set(0, -1, 0); controls.reset(); }, // bottom
function () { toPersp(); let p = getLookAt(); controls.position0.set(p[0] + resetCameraD, p[1] + -resetCameraD, p[2] + resetCameraD); controls.up0.set(0, 0, 1); controls.reset(); },
function () { toPersp(); let p = getLookAt(); controls.position0.set(p[0] + resetCameraD, p[1] + resetCameraD, p[2] + resetCameraD); controls.up0.set(0, 0, 1); controls.reset(); },
function () { toPersp(); let p = getLookAt(); controls.position0.set(p[0] + -resetCameraD, p[1] + resetCameraD, p[2] + resetCameraD); controls.up0.set(0, 0, 1); controls.reset(); },
function () { toPersp(); let p = getLookAt(); controls.position0.set(p[0] + -resetCameraD, p[1] + -resetCameraD, p[2] + resetCameraD); controls.up0.set(0, 0, 1); controls.reset(); },
function () { toPersp(); let p = getLookAt(); controls.position0.set(p[0] + resetCameraD, p[1] + -resetCameraD, p[2] + -resetCameraD); controls.up0.set(0, 0, 1); controls.reset(); },
function () { toPersp(); let p = getLookAt(); controls.position0.set(p[0] + resetCameraD, p[1] + resetCameraD, p[2] + -resetCameraD); controls.up0.set(0, 0, 1); controls.reset(); },
function () { toPersp(); let p = getLookAt(); controls.position0.set(p[0] + -resetCameraD, p[1] + resetCameraD, p[2] + -resetCameraD); controls.up0.set(0, 0, 1); controls.reset(); },
function () { toPersp(); let p = getLookAt(); controls.position0.set(p[0] + -resetCameraD, p[1] + -resetCameraD, p[2] + -resetCameraD); controls.up0.set(0, 0, 1); controls.reset(); }
];

function commonViewCalc(left, right, top, bottom) {
Expand All @@ -489,7 +505,7 @@ function commonViewCalc(left, right, top, bottom) {
frustumSize = fs * width / aspectRatio;
resetCameraD = 0.5 * width;
}
// console.log('aspectRatio=' + aspectRatio.toString() + ', width=' + width.toString() + ', height=' + height.toString() + ', frustumSize=' + frustumSize.toString() + ', resetCameraD=' + resetCameraD.toString());
console.log('commonViewCalc: aspectRatio=' + aspectRatio.toString() + ', width=' + width.toString() + ', height=' + height.toString() + ', frustumSize=' + frustumSize.toString() + ', resetCameraD=' + resetCameraD.toString());
return {
left: -aspectRatio * frustumSize,
right: aspectRatio * frustumSize,
Expand Down
59 changes: 28 additions & 31 deletions js/wasm_exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
}

if (!global.fs && global.require) {
try { global.fs = require("fs"); } catch (err) {}
try { const fs = require("fs");
if (Object.keys(fs) !== 0) {
global.fs = fs; } } catch (err) {} {
}
}

const enosys = () => {
Expand Down Expand Up @@ -172,37 +175,19 @@
const storeValue = (addr, v) => {
const nanHead = 0x7FF80000;

if (typeof v === "number") {
if (typeof v === "number" && v !== 0) {
if (isNaN(v)) {
this.mem.setUint32(addr + 4, nanHead, true);
this.mem.setUint32(addr, 0, true);
return;
}
if (v === 0) {
this.mem.setUint32(addr + 4, nanHead, true);
this.mem.setUint32(addr, 1, true);
return;
}
this.mem.setFloat64(addr, v, true);
return;
}

switch (v) {
case undefined:
this.mem.setFloat64(addr, 0, true);
return;
case null:
this.mem.setUint32(addr + 4, nanHead, true);
this.mem.setUint32(addr, 2, true);
return;
case true:
this.mem.setUint32(addr + 4, nanHead, true);
this.mem.setUint32(addr, 3, true);
return;
case false:
this.mem.setUint32(addr + 4, nanHead, true);
this.mem.setUint32(addr, 4, true);
return;
if (v === undefined) {
this.mem.setFloat64(addr, 0, true);
return;
}

let id = this._ids.get(v);
Expand All @@ -216,8 +201,13 @@
this._ids.set(v, id);
}
this._goRefCounts[id]++;
let typeFlag = 1;
let typeFlag = 0;
switch (typeof v) {
case "object":
if (v !== null) {
typeFlag = 1;
}
break;
case "string":
typeFlag = 2;
break;
Expand Down Expand Up @@ -440,14 +430,14 @@

// func valueInstanceOf(v ref, t ref) bool
"syscall/js.valueInstanceOf": (sp) => {
this.mem.setUint8(sp + 24, loadValue(sp + 8) instanceof loadValue(sp + 16));
this.mem.setUint8(sp + 24, (loadValue(sp + 8) instanceof loadValue(sp + 16)) ? 1 : 0);
},

// func copyBytesToGo(dst []byte, src ref) (int, bool)
"syscall/js.copyBytesToGo": (sp) => {
const dst = loadSlice(sp + 8);
const src = loadValue(sp + 32);
if (!(src instanceof Uint8Array)) {
if (!(src instanceof Uint8Array || src instanceof Uint8ClampedArray)) {
this.mem.setUint8(sp + 48, 0);
return;
}
Expand All @@ -461,7 +451,7 @@
"syscall/js.copyBytesToJS": (sp) => {
const dst = loadValue(sp + 8);
const src = loadSlice(sp + 16);
if (!(dst instanceof Uint8Array)) {
if (!(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)) {
this.mem.setUint8(sp + 48, 0);
return;
}
Expand Down Expand Up @@ -490,10 +480,17 @@
global,
this,
];
this._goRefCounts = []; // number of references that Go has to a JS value, indexed by reference id
this._ids = new Map(); // mapping from JS values to reference ids
this._idPool = []; // unused ids that have been garbage collected
this.exited = false; // whether the Go program has exited
this._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id
this._ids = new Map([ // mapping from JS values to reference ids
[0, 1],
[null, 2],
[true, 3],
[false, 4],
[global, 5],
[this, 6],
]);
this._idPool = []; // unused ids that have been garbage collected
this.exited = false; // whether the Go program has exited

// Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory.
let offset = 4096;
Expand Down
9 changes: 4 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,20 +538,19 @@ func logf(fmtStr string, args ...interface{}) {
const startupShader = `/*{
irmf: "1.0",
materials: ["PLA"],
max: [5,5,5],
min: [-5,-5,-5],
max: [10,10,10],
min: [0,0,0],
units: "mm",
}*/
float sphere(in vec3 pos, in float radius, in vec3 xyz) {
xyz -= pos; // Move sphere into place.
float sphere(in float radius, in vec3 xyz) {
float r = length(xyz);
return r <= radius ? 1.0 : 0.0;
}
void mainModel4(out vec4 materials, in vec3 xyz ) {
const float radius = 6.0;
materials[0] = 1.0 - sphere(vec3(0), radius, xyz); // 1.0 represents the cube.
materials[0] = 1.0 - sphere(radius, xyz-vec3(5)); // 1.0 represents the cube.
}
`

Expand Down

0 comments on commit 78114f7

Please sign in to comment.