grid-apps-cmms/src/mode/cam/animate.js

182 lines
5.3 KiB
JavaScript
Raw Normal View History

2020-11-24 00:11:49 -05:00
/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
"use strict";
kiri.loader.push(function() {
2020-11-24 00:11:49 -05:00
let KIRI = self.kiri,
CAM = KIRI.driver.CAM,
2020-11-24 11:01:29 -05:00
API, WORLD,
meshes = {};
2020-11-24 00:11:49 -05:00
if (KIRI.client)
2020-11-24 11:01:29 -05:00
CAM.animate_clear = function(api) {
WORLD = (API = api).const.SPACE.platform.world;
$('layer-animate').innerHTML = '';
Object.keys(meshes).forEach(id => deleteMesh(id));
}
if (KIRI.client)
CAM.animate = function(api) {
WORLD = (API = api).const.SPACE.platform.world;
KIRI.client.animate_setup(API.conf.get(), data => {
checkMeshCommands(data);
const UC = API.uc;
const layer = $('layer-animate');
layer.innerHTML = '';
UC.setGroup(layer);
UC.newRow([
UC.newButton(null,play,{icon:'<i class="fas fa-play-circle"></i>'}),
UC.newButton(null,pause,{icon:'<i class="fas fa-pause-circle"></i>'})
]);
2020-11-24 00:48:45 -05:00
});
};
2020-11-24 11:01:29 -05:00
function checkMeshCommands(data) {
if (!data) {
return;
}
if (data.mesh_add) {
const { id, ind, pos } = data.mesh_add;
addMesh(id, ind, pos);
}
if (data.mesh_del) {
deleteMesh(data.mesh_del);
}
if (data.mesh_move) {
const { id, pos } = data.mesh_move;
const mesh = meshes[id];
mesh.position.x = pos.x;
mesh.position.y = pos.y;
mesh.position.z = pos.z;
}
}
function addMesh(id, ind, pos) {
const geo = new THREE.BufferGeometry();
geo.setAttribute('position', new THREE.BufferAttribute(pos, 3));
geo.setIndex(new THREE.BufferAttribute(new Uint16Array(ind), 1));
const mat = new THREE.LineBasicMaterial({
transparent: true,
opacity: 0.75,
color: 0
2020-11-24 00:48:45 -05:00
});
2020-11-24 11:01:29 -05:00
const mesh = new THREE.LineSegments(geo, mat);
WORLD.add(mesh);
meshes[id] = mesh;
}
function updateMesh(id) {
}
function deleteMesh(id) {
WORLD.remove(meshes[id]);
delete meshes[id];
}
function play() {
KIRI.client.animate({speed: 1}, handleGridUpdate);
}
function pause() {
KIRI.client.animate({speed: 0}, handleGridUpdate);
}
function handleGridUpdate(data) {
checkMeshCommands(data);
}
if (KIRI.client)
KIRI.client.animate_setup = function(settings, ondone) {
send("animate_setup", {settings}, ondone);
};
if (KIRI.client)
KIRI.client.animate = function(data, ondone) {
send("animate", data, ondone);
2020-11-24 00:48:45 -05:00
};
2020-11-24 11:01:29 -05:00
let path, pathIndex, grid, tool, tools, rez;
2020-11-24 00:48:45 -05:00
if (KIRI.worker)
2020-11-24 11:01:29 -05:00
KIRI.worker.animate_setup = function(data, send) {
2020-11-24 00:48:45 -05:00
const { settings } = data;
2020-11-24 11:01:29 -05:00
rez = 0.5;
tools = settings.tools;
path = current.print.output.flat();
pathIndex = 0;
2020-11-24 00:48:45 -05:00
const stock = settings.stock;
2020-11-24 11:01:29 -05:00
// const center = stock.center;
const step = rez;
const stepsX = Math.floor(stock.x / step) + 1;
const stepsY = Math.floor(stock.y / step) + 1;
2020-11-24 11:01:29 -05:00
const { pos, ind } = createGrid(stepsX, stepsY, stock, step);
send.done({ mesh_add: { id: 0, pos, ind } });
};
function createGrid(stepsX, stepsY, size, step) {
2020-11-24 00:11:49 -05:00
const gridPoints = stepsX * stepsY;
2020-11-24 11:01:29 -05:00
const pos = grid = new Float32Array(gridPoints * 3);
const ind = [];
2020-11-24 11:01:29 -05:00
const ox = size.x / 2;
const oy = size.y / 2;
2020-11-24 00:11:49 -05:00
// initialize grid points
for (let x=0, ai=0; x<stepsX; x++) {
for (let y=0; y<stepsY; y++) {
pos[ai++] = x * step - ox;
pos[ai++] = y * step - oy;
2020-11-24 11:01:29 -05:00
pos[ai++] = size.z;
2020-11-24 00:11:49 -05:00
if (y > 0) ind.appendAll([
(stepsY * x) + (y - 1),
(stepsY * x) + (y )
]);
if (x > 0) ind.appendAll([
(stepsY * (x - 1)) + y,
(stepsY * (x )) + y
]);
}
}
2020-11-24 11:01:29 -05:00
return { pos, ind };
}
if (KIRI.worker)
KIRI.worker.animate = function(data, send) {
if (data.speed) {
let next = path[pathIndex++];
if (next.tool && (!tool || tool.getNumber() !== next.tool)) {
updateTool(next.tool, send);
}
send.data({ mesh_move: { id: tool.getID(), pos: next.point }});
}
send.done();
2020-11-24 00:11:49 -05:00
};
2020-11-24 11:01:29 -05:00
function updateTool(toolnum, send) {
if (tool) {
send.data({ mesh_del: tool.getID() });
}
tool = new CAM.Tool({ tools }, undefined, toolnum);
tool.generateProfile(rez);
const flen = tool.fluteLength() || 30;
// const frad = tool.fluteDiameter() / 2;
const prof = tool.profile;
const { size, pix } = tool.profileDim;
const { pos, ind } = createGrid(pix, pix, {x:size, y:size, z:flen}, rez);
const mid = Math.floor(pix/2);
// deform mesh to fit tool profile
for (let i=0, il=prof.length; i < il; ) {
const dx = mid + prof[i++];
const dy = mid + prof[i++];
const dz = prof[i++];
pos[(dx * pix + dy) * 3 + 2] = -dz;
}
send.data({ mesh_add: { id:tool.getID(), pos, ind }});
}
});