Skip to content

Commit

Permalink
Remove worker
Browse files Browse the repository at this point in the history
  • Loading branch information
Avaer Kazmer committed Aug 5, 2019
1 parent e10dbb3 commit 43edd39
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 437 deletions.
283 changes: 3 additions & 280 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1651,289 +1651,12 @@
})();
scene.add(gridMesh); */

worker = (() => {
let cbs = [];
const worker = new Worker('mc-worker.js');
worker.onmessage = e => {
const {data} = e;
const {error, result} = data;
cbs.shift()(error, result);
};
worker.onerror = err => {
console.warn(err);
};
worker.request = (req, transfers) => new Promise((accept, reject) => {
worker.postMessage(req, transfers);

cbs.push((err, result) => {
if (!err) {
accept(result);
} else {
reject(err);
}
});
});
return worker;
})();
const _makeMiningPlaceholderMesh = (x, z, owner) => {
// console.log('make text mesh', token);
const mesh = _makeTextMesh('Parcel ' + x + ' : ' + z + ' owned by ' + owner, 30);
mesh.position.set(x * PARCEL_SIZE, 1, z * PARCEL_SIZE);
// mesh.token = token;
mesh.update = () => {
// XXX
};
mesh.intersect = () => null;
mesh.destroy = () => {
// XXX
};
const mesh = new THREE.Object3D();
return mesh;
};
const _makeMiningMesh = (x, z/*, token*/) => {
const terrainVsh = `
varying vec3 vViewPosition;
void main() {
vec4 mvPosition = modelMatrix * vec4( position.xyz, 1.0 );
gl_Position = projectionMatrix * modelViewMatrix * vec4( position.xyz, 1.0 );
vViewPosition = mvPosition.xyz;
}
`;
const terrainFsh = `
uniform vec3 uSelect;
varying vec3 vViewPosition;
vec4 color = vec4(${new THREE.Color(0x9ccc65).toArray().map(n => n.toFixed(8)).join(',')}, 1.0);
vec4 color2 = vec4(${new THREE.Color(0xec407a).toArray().map(n => n.toFixed(8)).join(',')}, 1.0);
bool inRange(vec3 pos, vec3 minPos, vec3 maxPos) {
return pos.x >= minPos.x &&
pos.y >= minPos.y &&
pos.z >= minPos.z &&
pos.x <= maxPos.x &&
pos.y <= maxPos.y &&
pos.z <= maxPos.z;
}
void main() {
vec3 fdx = vec3( dFdx( -vViewPosition.x ), dFdx( -vViewPosition.y ), dFdx( -vViewPosition.z ) );
vec3 fdy = vec3( dFdy( -vViewPosition.x ), dFdy( -vViewPosition.y ), dFdy( -vViewPosition.z ) );
vec3 normal = normalize( cross( fdx, fdy ) );
float dotNL = saturate( dot( normal, normalize(vec3(1.0, 1.0, 1.0))) );
float range = 1.01;
// float range = 2.01;
vec3 minPos = uSelect - range;
vec3 maxPos = minPos + (range*2.);
if (inRange(vViewPosition, minPos, maxPos)) {
gl_FragColor = color2;
} else {
gl_FragColor = color;
}
gl_FragColor.rgb += dotNL * 0.5;
}
`;

const geometry = new THREE.BufferGeometry();
const material = new THREE.ShaderMaterial({
uniforms: {
uSelect: {
type: 'v3',
value: new THREE.Vector3(NaN, NaN, NaN),
},
},
vertexShader: terrainVsh,
fragmentShader: terrainFsh,
extensions: {
derivatives: true,
},
});
const mesh = new THREE.Mesh(geometry, material);
mesh.frustumCulled = false;
mesh.visible = false;
// mesh.token = token;

const size = PARCEL_SIZE;
const dims = Float32Array.from([size, size, size]);
const potential = new Float32Array(size*size*size);
potential.fill(1);
for (let x = 0; x < size; x++) {
for (let y = 0; y < size; y++) {
for (let z = 0; z < size; z++) {
if (
x === 0 || y === 0 || z === 0 ||
x === (size-1) || y === (size-1) || z === (size-1)
) {
potential[x + y*size + z*size*size] = 0;
}
}
}
}
const shift = Float32Array.from([(x-0.5)*size, 0, (z-0.5)*size]);

mesh.x = x;
mesh.z = z;
mesh.potential = potential;
mesh.shift = shift;
mesh.contains = pos =>
pos.x >= shift[0] &&
pos.y >= shift[1] &&
pos.z >= shift[2] &&
pos.x < shift[0] + size &&
pos.y < shift[1] + size &&
pos.z < shift[2] + size;
mesh.getPotential = pos => {
const x = pos.x - shift[0];
const y = pos.y - shift[1];
const z = pos.z - shift[2];
return potential[x + y*size*size + z*size];
};
mesh.mine = pos => {
const x = Math.floor(pos.x - shift[0]);
const y = Math.floor(pos.y - shift[1]);
const z = Math.floor(pos.z - shift[2]);
const factor = 2;
const max = Math.sqrt(factor*factor*3);
for (let dx = -factor; dx <= factor; dx++) {
for (let dz = -factor; dz <= factor; dz++) {
for (let dy = -factor; dy <= factor; dy++) {
const ax = x + dx;
const ay = y + dy;
const az = z + dz;
if (
ax >= 0 &&
ay >= 0 &&
az >= 0 &&
ax < size &&
ay < size &&
az < size
) {
const index = ax + ay*size*size + az*size;
const d = (max - Math.sqrt(dx*dx + dy*dy + dz*dz)) / max * 2;
potential[index] = Math.max(potential[index] - d, 0);
}
}
}
}
worker.request({
method: 'march',
dims,
potential,
shift,
}).then(res => {
geometry.addAttribute('position', new THREE.BufferAttribute(res.positions, 3));
geometry.setIndex(new THREE.BufferAttribute(res.faces, 1));

if (potential.every(n => n <= 0)) {
const {x, z} = mesh;
fetch('https://djj7y2i5p8.execute-api.us-west-1.amazonaws.com/default/Webaverse', {
method: 'POST',
body: JSON.stringify({
addr: window.web3.eth.defaultAccount,
x,
y: z,
}),
})
.then(res => res.json())
.then(o => {
const {v, r, s} = o;
const addr = window.web3.eth.defaultAccount;
console.log('mintTokenFromSignature', {addr, x, z, v, r, s});
return user.execute({
method: 'mintTokenFromSignature',
data: {
addr,
x,
y: z,
v,
r,
s,
},
});
})
.then(() => {
console.log('mined', {x, z});

for (;;) {
const index = sceneMeshes.findIndex(sceneMesh => sceneMesh.token.coords.some(coord => coord[0] === x && coord[1] === z));
if (index !== -1) {
const sceneMesh = sceneMeshes[index];
sceneMesh.destroy();
scene.remove(sceneMesh);
sceneMeshes.splice(index, 1);
} else {
break;
}
}

const sceneMesh = _makeSceneObjectMesh();
sceneMesh.token = {
id: 0,
coords: [[x, z]],
apps: [],
owner: _getUserAddress(),
};
scene.add(sceneMesh);
sceneMeshes.push(sceneMesh);

selectedSceneToken = null;

lastSceneCoords[0] = NaN;
lastSceneCoords[1] = NaN;
});
}
});
};
mesh.destroy = () => {
geometry.dispose();
material.dispose();
};
let colliding = false;
mesh.update = () => {
if (!colliding && geometry.attributes.position) {
colliding = true;

const controllerMesh = controllerMeshes[1]; // XXX make this work for all controllers
worker.request({
method: 'collide',
positions: geometry.attributes.position.array,
indices: geometry.index.array,
origin: controllerMesh.ray.origin.toArray(new Float32Array(3)),
direction: controllerMesh.ray.direction.toArray(new Float32Array(3)),
})
.then(collision => {
material.uniforms.uSelect.value.fromArray(collision);
})
.catch(err => {
console.warn(err.stack);
})
.finally(() => {
colliding = false;
});
}
};
mesh.intersect = ray => {
if (isFinite(material.uniforms.uSelect.value.x)) {
const intersectionPoint = material.uniforms.uSelect.value.clone();
const distance = ray.origin.distanceTo(intersectionPoint);
return {
type: 'mine',
mesh,
intersectionPoint,
distance,
};
} else {
return null;
}
};

worker.request({
method: 'march',
dims,
potential,
shift,
}).then(res => {
geometry.addAttribute('position', new THREE.BufferAttribute(res.positions, 3));
geometry.setIndex(new THREE.BufferAttribute(res.faces, 1));
mesh.visible = true;
});

const _makeMiningMesh = (x, z) => {
const mesh = new THREE.Object3D();
return mesh;
};
let data;
Expand Down
Loading

0 comments on commit 43edd39

Please sign in to comment.