update
This commit is contained in:
@@ -24,7 +24,7 @@ const V3D = {
|
||||
ready: false,
|
||||
scene: null, camera: null, renderer: null, root: null,
|
||||
robotGroup: null, scanPts: null, cloudPts: null, mapMesh: null,
|
||||
voxelMesh: null, voxelCap: 0, voxelEpoch: -1,
|
||||
mapCloud: null, mapCloudCap: 0, mapCloudRes: 0,
|
||||
worldMesh: null, worldKey: null,
|
||||
meshWorld: null, // nhom mesh THAT doc tu .world; co thi khong dung khoi hop
|
||||
gtGroup: null, pathLine: null,
|
||||
@@ -497,91 +497,98 @@ function v3dSetCloud(arr, pose) {
|
||||
V3D.cloudPts.geometry.setAttribute('color', new THREE.BufferAttribute(col, 3));
|
||||
}
|
||||
|
||||
// --- Ban do voxel 3D -------------------------------------------------------
|
||||
// --- Ban do 3D da quet (dam may diem tich luy) -----------------------------
|
||||
//
|
||||
// Nguon: SLAM, khong phai bo mo phong. Day la "cai gi dung tren san" do chinh
|
||||
// robot do duoc, bu cho tam phang cua v3dSetMap ("da quet toi dau").
|
||||
// Nguon: SLAM, khong phai bo mo phong. Day la nhung gi CHINH CON ROBOT quet
|
||||
// duoc, giu lai va nan theo pose da toi uu -- phan "RViz" that su, bu cho tam
|
||||
// phang cua v3dSetMap ("da quet toi dau").
|
||||
//
|
||||
// Ba cho CO Y khac v3dSetWorld va v3dSetCloud, moi cho vi mot ly do:
|
||||
// VE BANG DIEM, khong phai bang hop. Du lieu ben duoi van la tap voxel (do la
|
||||
// cach khu trung lap va giu bang thong), nhung mot khoi hop 5-10 cm nhin ra
|
||||
// Minecraft chu khong ra mot ban quet. Diem cho dung cam giac cua dam may
|
||||
// camera, va dung bang mau voi lop cloud truc tiep nen hai thu doc nhu mot.
|
||||
//
|
||||
// 1. Dung lai theo NOI DUNG (`v.dirty`), khong theo kich thuoc. v3dSetWorld
|
||||
// cache theo `w x h @ ox,oy` -- dung cho mot ban do TINH, sai hoan toan o
|
||||
// day vi tap voxel doi lien tuc trong khi bao ngoai dung yen.
|
||||
// Ba cho CO Y khac v3dSetCloud (dam may TRUC TIEP, mot khung):
|
||||
//
|
||||
// 2. KHONG dispose/dung lai hinh moi khung nhu v3dSetCloud. InstancedMesh co
|
||||
// so luong co dinh, nen cap phat du `capacity` roi chi doi `count`. O
|
||||
// 100k the thi kieu cua cloud khong song noi.
|
||||
// 1. Dung lai theo NOI DUNG (`v.dirty`), khong phai moi khung. Tap nay doi
|
||||
// toi da ~4 lan/giay chu khong 8-10, va no to gap tram lan.
|
||||
//
|
||||
// 3. KHONG do bong. v3dSetWorld bat ca castShadow lan receiveShadow; nem
|
||||
// 100k the vao shadow map 2048^2 la mat frame rate. Dung "sua" lai.
|
||||
function v3dSetVoxels(v, visible) {
|
||||
// 2. Cap phat theo CAPACITY roi dung setDrawRange, khong dispose/dung lai
|
||||
// BufferGeometry moi lan. v3dSetCloud lam vay voi 2000 diem thi khong sao;
|
||||
// o 100k diem do la 1.2 MB rac moi lan cap nhat.
|
||||
//
|
||||
// 3. Toa do la TUYET DOI trong frame map -- khong nhan pose. Dam may truc
|
||||
// tiep nam o base_footprint nen phai xoay theo robot; tap nay da o he ban
|
||||
// do tu luc xuat.
|
||||
function v3dSetMapCloud(v, visible) {
|
||||
if (!V3D.ready) return;
|
||||
if (!v || !visible || v.set.size === 0) {
|
||||
if (V3D.voxelMesh) V3D.voxelMesh.visible = false;
|
||||
if (V3D.mapCloud) V3D.mapCloud.visible = false;
|
||||
return;
|
||||
}
|
||||
|
||||
const n = v.set.size;
|
||||
const needNew = !V3D.voxelMesh || n > V3D.voxelCap ||
|
||||
(V3D.voxelMesh && V3D.voxelRes !== v.res);
|
||||
const needNew = !V3D.mapCloud || n > V3D.mapCloudCap || V3D.mapCloudRes !== v.res;
|
||||
if (needNew) {
|
||||
if (V3D.voxelMesh) {
|
||||
V3D.root.remove(V3D.voxelMesh);
|
||||
V3D.voxelMesh.geometry.dispose();
|
||||
V3D.voxelMesh.material.dispose();
|
||||
if (V3D.mapCloud) {
|
||||
V3D.root.remove(V3D.mapCloud);
|
||||
V3D.mapCloud.geometry.dispose();
|
||||
V3D.mapCloud.material.dispose();
|
||||
}
|
||||
// Cap phat du de lon them mot doan ma khong phai cap phat lai moi khung.
|
||||
let cap = 1024;
|
||||
let cap = 4096;
|
||||
while (cap < Math.ceil(n * 1.5)) cap *= 2;
|
||||
const geo = new THREE.BoxGeometry(v.res, v.res, v.res);
|
||||
const mat = new THREE.MeshStandardMaterial({ roughness: 0.9, metalness: 0.0 });
|
||||
V3D.voxelMesh = new THREE.InstancedMesh(geo, mat, cap);
|
||||
// Bounding sphere cua InstancedMesh lay tu hinh GOC (mot hop 0.1 m), nen
|
||||
// de mac dinh thi three.js cull ca ban do ngay khi goc toa do ra khoi khung.
|
||||
V3D.voxelMesh.frustumCulled = false;
|
||||
V3D.voxelMesh.castShadow = false;
|
||||
V3D.voxelMesh.receiveShadow = false;
|
||||
V3D.voxelCap = cap;
|
||||
V3D.voxelRes = v.res;
|
||||
V3D.root.add(V3D.voxelMesh);
|
||||
v.dirty = true; // hinh moi -> phai do lai du lieu
|
||||
const geo = new THREE.BufferGeometry();
|
||||
geo.setAttribute('position', new THREE.BufferAttribute(new Float32Array(cap * 3), 3));
|
||||
geo.setAttribute('color', new THREE.BufferAttribute(new Float32Array(cap * 3), 3));
|
||||
// Co diem theo do phan giai: o 0.05 m thi 0.04 cho ra mot mat lien chu
|
||||
// khong phai mot ram cham roi rac.
|
||||
const mat = new THREE.PointsMaterial({ size: v.res * 0.8, vertexColors: true });
|
||||
V3D.mapCloud = new THREE.Points(geo, mat);
|
||||
// Bounding sphere tinh tu capacity con thua (toan so 0 o goc), nen cull se
|
||||
// sai. Tap nay trai ca toa nha, cull no khong tiet kiem duoc gi.
|
||||
V3D.mapCloud.frustumCulled = false;
|
||||
V3D.mapCloudCap = cap;
|
||||
V3D.mapCloudRes = v.res;
|
||||
V3D.root.add(V3D.mapCloud);
|
||||
v.dirty = true; // bo dem moi -> phai do lai
|
||||
}
|
||||
|
||||
V3D.voxelMesh.visible = true;
|
||||
V3D.mapCloud.visible = true;
|
||||
if (!v.dirty) return;
|
||||
v.dirty = false;
|
||||
|
||||
const m4 = new THREE.Matrix4();
|
||||
const col = new THREE.Color();
|
||||
const pos = V3D.mapCloud.geometry.attributes.position.array;
|
||||
const col = V3D.mapCloud.geometry.attributes.color.array;
|
||||
const half = v.res * 0.5;
|
||||
let i = 0;
|
||||
for (const key of v.set) {
|
||||
// Giai khoa tai cho: goi voxelUnpack() 100k lan moi lan dung lai la
|
||||
// 100k mang tam.
|
||||
// Giai khoa TAI CHO: goi voxelUnpack() 100k lan la 100k mang tam.
|
||||
const z = (key % 65536) - 32768;
|
||||
const k1 = (key - (z + 32768)) / 65536;
|
||||
const y = (k1 % 65536) - 32768;
|
||||
const x = (k1 - (y + 32768)) / 65536 - 32768;
|
||||
|
||||
// Tam o, khong phai goc o. ROS (x, y, z) -> three (x, z, -y) -- giong
|
||||
// toScene(), nhung noi thang vao day: toScene() cap phat mot Vector3 moi
|
||||
// lan goi, va 100k lan cap phat moi lan dung lai la mot con bao GC.
|
||||
// Tam o. ROS (x, y, z) -> three (x, z, -y), noi thang thay vi goi
|
||||
// toScene(): ham do cap phat mot Vector3 moi lan.
|
||||
const wx = v.ox + x * v.res + half;
|
||||
const wy = v.oy + y * v.res + half;
|
||||
const wz = v.oz + z * v.res + half;
|
||||
m4.makeTranslation(wx, wz, -wy);
|
||||
V3D.voxelMesh.setMatrixAt(i, m4);
|
||||
const b = i * 3;
|
||||
pos[b] = wx;
|
||||
pos[b + 1] = wz;
|
||||
pos[b + 2] = -wy;
|
||||
|
||||
// Cung thang mau voi point cloud, gian ra toi 2 m: hai lop phai doc nhu
|
||||
// mot he, khong phai hai he mau canh nhau.
|
||||
// Cung thang mau voi lop cloud truc tiep (v3dSetCloud), gian ra toi 2 m.
|
||||
const t = Math.max(0, Math.min(1, wz / 2.0));
|
||||
col.setRGB((80 + 175 * t) / 255, (200 - 90 * t) / 255, (120 + 60 * t) / 255);
|
||||
V3D.voxelMesh.setColorAt(i, col);
|
||||
col[b] = (80 + 175 * t) / 255;
|
||||
col[b + 1] = (200 - 90 * t) / 255;
|
||||
col[b + 2] = (120 + 60 * t) / 255;
|
||||
++i;
|
||||
}
|
||||
V3D.voxelMesh.count = i;
|
||||
V3D.voxelMesh.instanceMatrix.needsUpdate = true;
|
||||
if (V3D.voxelMesh.instanceColor) V3D.voxelMesh.instanceColor.needsUpdate = true;
|
||||
|
||||
V3D.mapCloud.geometry.setDrawRange(0, i);
|
||||
V3D.mapCloud.geometry.attributes.position.needsUpdate = true;
|
||||
V3D.mapCloud.geometry.attributes.color.needsUpdate = true;
|
||||
}
|
||||
|
||||
// The gioi 3D: o BI CHIEM -> hop, dung nhu Gazebo dung tuong tu ban do.
|
||||
@@ -939,12 +946,27 @@ function v3dSetOverlays(on) {
|
||||
});
|
||||
}
|
||||
|
||||
function v3dRender(canvas, follow) {
|
||||
// `splitSlam` = ve HAI viewport trong cung mot canvas:
|
||||
// trai = canh nhu dang co (the gioi + moi thu, theo lop nguoi dung chon)
|
||||
// phai = CHI nhung gi SLAM dung ra (the gioi bi ep an)
|
||||
//
|
||||
// Mot renderer, hai lan render, cat bang scissor -- KHONG mo WebGLRenderer thu
|
||||
// hai. Hai GL context tren cung mot trang la gap doi bo nho texture va mot
|
||||
// nguon loi "context lost" kinh dien tren Jetson.
|
||||
//
|
||||
// Camera dung CHUNG co y: hai khung cung goc nhin thi moi doi chieu duoc "ngoai
|
||||
// doi cho nay co cai ban, SLAM quet ra gi". Lech goc la mat ca y nghia.
|
||||
function v3dRender(canvas, follow, splitSlam) {
|
||||
if (!V3D.ready) return;
|
||||
const w = canvas.clientWidth, h = canvas.clientHeight;
|
||||
if (canvas.width !== w || canvas.height !== h) {
|
||||
V3D.renderer.setSize(w, h, false);
|
||||
V3D.camera.aspect = w / Math.max(1, h);
|
||||
}
|
||||
// Ti le khung hinh tinh theo NUA chieu rong khi chia doi, khong thi hinh bi
|
||||
// keo ngang o ca hai ben.
|
||||
const aspect = (splitSlam ? w / 2 : w) / Math.max(1, h);
|
||||
if (V3D.camera.aspect !== aspect) {
|
||||
V3D.camera.aspect = aspect;
|
||||
V3D.camera.updateProjectionMatrix();
|
||||
}
|
||||
|
||||
@@ -960,5 +982,50 @@ function v3dRender(canvas, follow) {
|
||||
o.dist * Math.sin(o.el),
|
||||
cz + o.dist * Math.cos(o.el) * Math.sin(o.az));
|
||||
V3D.camera.lookAt(cx, 0.2, cz);
|
||||
V3D.renderer.render(V3D.scene, V3D.camera);
|
||||
|
||||
const r = V3D.renderer;
|
||||
if (!splitSlam) {
|
||||
r.setScissorTest(false);
|
||||
r.setViewport(0, 0, w, h);
|
||||
r.render(V3D.scene, V3D.camera);
|
||||
return;
|
||||
}
|
||||
|
||||
const half = Math.floor(w / 2);
|
||||
r.setScissorTest(true);
|
||||
|
||||
// Hai nua an hai thu KHAC NHAU, va phep an nay chi la MOT CHIEU: no tat bot,
|
||||
// khong bao gio bat len. Nguoi dung tick lop nao thi lop do moi co cua de
|
||||
// hien -- khong dung v3dSetWorldVisible()/visible=true o day, vi mot cai
|
||||
// `= true` se lam nut tick tren thanh cong cu thanh noi doi.
|
||||
const hide = (list) => {
|
||||
const hid = [];
|
||||
for (const o of list) {
|
||||
if (o && o.visible) { o.visible = false; hid.push(o); }
|
||||
}
|
||||
return hid;
|
||||
};
|
||||
const show = (hid) => { for (const o of hid) o.visible = true; };
|
||||
|
||||
// --- Nua TRAI: THE GIOI, khong co dau ra cua SLAM ---
|
||||
//
|
||||
// Dam may da quet va tam ban do SLAM deu bi tat o day. Chung la thu SLAM
|
||||
// DUNG RA; de chung chong len su that thi khung trai het tra loi duoc cau
|
||||
// hoi cua no ("ngoai doi cho nay co gi"), va bat lop `cloud da quet` se thay
|
||||
// no sang o CA HAI ben -- dung cai da bi bao la sai.
|
||||
let hid = hide([V3D.mapCloud, V3D.mapMesh]);
|
||||
r.setViewport(0, 0, half, h);
|
||||
r.setScissor(0, 0, half, h);
|
||||
r.render(V3D.scene, V3D.camera);
|
||||
show(hid);
|
||||
|
||||
// --- Nua PHAI: chi SLAM, khong co the gioi ---
|
||||
hid = hide([V3D.meshWorld, V3D.worldMesh]);
|
||||
r.setViewport(half, 0, w - half, h);
|
||||
r.setScissor(half, 0, w - half, h);
|
||||
r.render(V3D.scene, V3D.camera);
|
||||
show(hid);
|
||||
|
||||
r.setScissorTest(false);
|
||||
r.setViewport(0, 0, w, h);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user