update
This commit is contained in:
@@ -12,7 +12,7 @@ const MAGIC = 0x314C5356; // 'VSL1' little-endian
|
||||
// Phien ban ma nguon giao dien. TANG khi doi app.js / app3d.js / style.css.
|
||||
// Hien ngay tren thanh tren cung de phan biet "chua sua" voi "trinh duyet con
|
||||
// cache ban cu".
|
||||
const UI_VERSION = '2026-09-19g';
|
||||
const UI_VERSION = '2026-09-23a';
|
||||
|
||||
// ⚠️ HAI bang mau, mot cho moi khung. Chung KHONG duoc giong nhau.
|
||||
//
|
||||
@@ -62,9 +62,12 @@ const PALETTE = {
|
||||
},
|
||||
};
|
||||
|
||||
const T_SCAN = 1, T_CLOUD = 2, T_MAP_FULL = 3, T_MAP_PATCH = 4, T_PATH = 5;
|
||||
const T_SCAN = 1, T_CLOUD = 2, T_MAP_FULL = 3, T_PATH = 5;
|
||||
// 4 = kMapPatch, DA BO o phia server. Giu lo trong, khong tai dung.
|
||||
// Ban do THE GIOI cua bo mo phong -- khung nhin "Gazebo". Tinh: gui mot lan.
|
||||
const T_WORLD_MAP = 6;
|
||||
// Ban do voxel 3D: tap day du (chia manh) va phan them/bot.
|
||||
const T_VOXEL_FULL = 7, T_VOXEL_DELTA = 8;
|
||||
|
||||
// Ba ngu nghia cua mot tia. Lan lon chung la lam sai ca man hinh.
|
||||
const R_NAN = 0xFFFF; // khong do duoc (duoi range_min)
|
||||
@@ -77,6 +80,14 @@ const state = {
|
||||
// anchorPose(). `scanDelta` den tu khung state, dung ngay khung scan ke tiep.
|
||||
cloudPose: null, scanPose: null, scanDelta: null,
|
||||
map: null, mapImage: null, mapVersion: 0,
|
||||
// Ban do voxel 3D. `set` la Set CAC SO (khong phai chuoi): o 100k phan tu
|
||||
// thi Set<number> re hon han Set<string> ca ve bo nho lan thoi gian.
|
||||
// `dirty` = co gi doi ke tu lan dung lai hinh gan nhat.
|
||||
voxels: {
|
||||
epoch: 0, res: 0.1, ox: 0, oy: 0, oz: 0,
|
||||
set: new Set(), total: 0, seq: 0, syncing: false, dirty: false,
|
||||
},
|
||||
voxelResyncAt: 0,
|
||||
bytes: 0, lastBytes: 0, wireRate: 0,
|
||||
};
|
||||
|
||||
@@ -127,6 +138,32 @@ function setConn(ok) {
|
||||
|
||||
// --- Khung nhi phan --------------------------------------------------------
|
||||
|
||||
// Khoa voxel dang SO. Ba chi so i16 -> mot so nguyen <= 2^48, van chinh xac
|
||||
// tuyet doi trong double.
|
||||
function voxelKey(x, y, z) {
|
||||
return ((x + 32768) * 65536 + (y + 32768)) * 65536 + (z + 32768);
|
||||
}
|
||||
|
||||
function voxelUnpack(k) {
|
||||
const z = (k % 65536) - 32768;
|
||||
k = (k - (z + 32768)) / 65536;
|
||||
const y = (k % 65536) - 32768;
|
||||
k = (k - (y + 32768)) / 65536;
|
||||
return [k - 32768, y, z];
|
||||
}
|
||||
|
||||
// Xin server gui lai ca tap. Ham han MOT lan moi giay: lech epoch thuong keo
|
||||
// theo mot chum khung delta, va moi khung lai goi vao day.
|
||||
function requestVoxelResync() {
|
||||
const now = Date.now();
|
||||
if (now - state.voxelResyncAt < 1000) return;
|
||||
state.voxelResyncAt = now;
|
||||
state.voxels.seq = 0;
|
||||
if (ws && ws.readyState === 1) {
|
||||
ws.send(JSON.stringify({ t: 'voxel_resync' }));
|
||||
}
|
||||
}
|
||||
|
||||
function onBinary(dv) {
|
||||
if (dv.byteLength < 16 || dv.getUint32(0, true) !== MAGIC) return;
|
||||
const type = dv.getUint8(4);
|
||||
@@ -212,6 +249,84 @@ function onBinary(dv) {
|
||||
return;
|
||||
}
|
||||
|
||||
// --- Ban do voxel 3D ---
|
||||
//
|
||||
// Khoa la mot SO, khong phai chuoi: `((x+32768)*65536 + (y+32768))*65536 +
|
||||
// (z+32768)` toi da 2^48, double bieu dien chinh xac tuyet doi. Set<string>
|
||||
// o 100k phan tu la mot con duong chac chan toi giat hinh.
|
||||
if (type === T_VOXEL_FULL) {
|
||||
const res = dv.getFloat32(off, true);
|
||||
const ox = dv.getFloat32(off + 4, true);
|
||||
const oy = dv.getFloat32(off + 8, true);
|
||||
const oz = dv.getFloat32(off + 12, true);
|
||||
const epoch = dv.getUint32(off + 16, true);
|
||||
const total = dv.getUint32(off + 20, true);
|
||||
const chunkIndex = dv.getUint16(off + 24, true);
|
||||
const chunkCount = dv.getUint16(off + 26, true);
|
||||
const n = dv.getUint32(off + 28, true);
|
||||
|
||||
const v = state.voxels;
|
||||
// Manh dau, hoac epoch la giua chung -> bo het lam lai. Tron hai epoch se
|
||||
// ra mot ban do khong ton tai o dau ca.
|
||||
if (chunkIndex === 0 || epoch !== v.epoch) {
|
||||
v.set = new Set();
|
||||
v.epoch = epoch;
|
||||
v.res = res;
|
||||
v.ox = ox; v.oy = oy; v.oz = oz;
|
||||
v.total = total;
|
||||
v.seq = 0;
|
||||
v.syncing = true;
|
||||
}
|
||||
for (let i = 0; i < n; i++) {
|
||||
const b = off + 32 + i * 6;
|
||||
v.set.add(voxelKey(dv.getInt16(b, true), dv.getInt16(b + 2, true),
|
||||
dv.getInt16(b + 4, true)));
|
||||
}
|
||||
v.dirty = true;
|
||||
|
||||
if (chunkIndex + 1 >= chunkCount) {
|
||||
v.syncing = false;
|
||||
// Dem lai truoc khi bao "xong": manh full-sync la khung THUONG, server
|
||||
// drop duoc chung khi hang doi day. Thieu mot manh ma van ack thi ban do
|
||||
// co mot lo hong khong bao gio duoc lap, va khong mot dau hieu nao.
|
||||
if (v.set.size !== v.total) {
|
||||
requestVoxelResync();
|
||||
} else if (ws && ws.readyState === 1) {
|
||||
ws.send(JSON.stringify({ t: 'ack_voxel', epoch: v.epoch }));
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (type === T_VOXEL_DELTA) {
|
||||
const epoch = dv.getUint32(off, true);
|
||||
const seq = dv.getUint32(off + 4, true);
|
||||
const nAdd = dv.getUint32(off + 8, true);
|
||||
const nDel = dv.getUint32(off + 12, true);
|
||||
|
||||
const v = state.voxels;
|
||||
// Lech epoch = server da dung lai ban do (vong dong). Hut so thu tu = mat
|
||||
// khung. Ca hai deu khong va duoc, phai xin lai ca tap.
|
||||
if (epoch !== v.epoch || (v.seq !== 0 && seq !== v.seq + 1)) {
|
||||
requestVoxelResync();
|
||||
return;
|
||||
}
|
||||
v.seq = seq;
|
||||
|
||||
let b = off + 16;
|
||||
for (let i = 0; i < nAdd; i++, b += 6) {
|
||||
v.set.add(voxelKey(dv.getInt16(b, true), dv.getInt16(b + 2, true),
|
||||
dv.getInt16(b + 4, true)));
|
||||
}
|
||||
for (let i = 0; i < nDel; i++, b += 6) {
|
||||
v.set.delete(voxelKey(dv.getInt16(b, true), dv.getInt16(b + 2, true),
|
||||
dv.getInt16(b + 4, true)));
|
||||
}
|
||||
v.total = v.set.size;
|
||||
v.dirty = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (type === T_PATH) {
|
||||
const n = dv.getUint32(off, true);
|
||||
const pts = new Float32Array(n * 2);
|
||||
@@ -1069,6 +1184,11 @@ function draw3d() {
|
||||
|
||||
// SLAM: lop phu tren san, hien DAN khi quet -- day la phan "RViz". O chua
|
||||
// quet trong suot nen tuong 3D van nhin xuyen qua.
|
||||
// VOXEL: "cai gi dung tren san", bu cho tam phang cua v3dSetMap ("da quet
|
||||
// toi dau"). Hai lop khong tranh nhau: tam phang o y = -0.002, voxel bat
|
||||
// dau tu z_min = 0.05.
|
||||
v3dSetVoxels(state.voxels, on('lyVoxel'));
|
||||
|
||||
if (state.map && on('lyMap')) {
|
||||
v3dSetMap({ ...state.map, data: state.map.cells, version: state.mapVersion });
|
||||
}
|
||||
|
||||
@@ -24,6 +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,
|
||||
worldMesh: null, worldKey: null,
|
||||
meshWorld: null, // nhom mesh THAT doc tu .world; co thi khong dung khoi hop
|
||||
gtGroup: null, pathLine: null,
|
||||
@@ -496,6 +497,93 @@ function v3dSetCloud(arr, pose) {
|
||||
V3D.cloudPts.geometry.setAttribute('color', new THREE.BufferAttribute(col, 3));
|
||||
}
|
||||
|
||||
// --- Ban do voxel 3D -------------------------------------------------------
|
||||
//
|
||||
// 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").
|
||||
//
|
||||
// Ba cho CO Y khac v3dSetWorld va v3dSetCloud, moi cho vi mot ly do:
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// 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) {
|
||||
if (!V3D.ready) return;
|
||||
if (!v || !visible || v.set.size === 0) {
|
||||
if (V3D.voxelMesh) V3D.voxelMesh.visible = false;
|
||||
return;
|
||||
}
|
||||
|
||||
const n = v.set.size;
|
||||
const needNew = !V3D.voxelMesh || n > V3D.voxelCap ||
|
||||
(V3D.voxelMesh && V3D.voxelRes !== v.res);
|
||||
if (needNew) {
|
||||
if (V3D.voxelMesh) {
|
||||
V3D.root.remove(V3D.voxelMesh);
|
||||
V3D.voxelMesh.geometry.dispose();
|
||||
V3D.voxelMesh.material.dispose();
|
||||
}
|
||||
// Cap phat du de lon them mot doan ma khong phai cap phat lai moi khung.
|
||||
let cap = 1024;
|
||||
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
|
||||
}
|
||||
|
||||
V3D.voxelMesh.visible = true;
|
||||
if (!v.dirty) return;
|
||||
v.dirty = false;
|
||||
|
||||
const m4 = new THREE.Matrix4();
|
||||
const col = new THREE.Color();
|
||||
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.
|
||||
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.
|
||||
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);
|
||||
|
||||
// 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.
|
||||
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);
|
||||
++i;
|
||||
}
|
||||
V3D.voxelMesh.count = i;
|
||||
V3D.voxelMesh.instanceMatrix.needsUpdate = true;
|
||||
if (V3D.voxelMesh.instanceColor) V3D.voxelMesh.instanceColor.needsUpdate = true;
|
||||
}
|
||||
|
||||
// The gioi 3D: o BI CHIEM -> hop, dung nhu Gazebo dung tuong tu ban do.
|
||||
//
|
||||
// ================== VI SAO INSTANCED ==================
|
||||
|
||||
@@ -11,6 +11,10 @@
|
||||
<div class="layers">
|
||||
<label><input type="checkbox" id="lyWorld" checked> the gioi</label>
|
||||
<label><input type="checkbox" id="lyMap" checked> map SLAM</label>
|
||||
<!-- Ban do voxel 3D. TAT mac dinh: no chi co du lieu khi slam.yaml dat
|
||||
camera_3d: true, va bat san se lam nguoi dung tuong tinh nang hong
|
||||
trong khi that ra no dang tat o cau hinh. -->
|
||||
<label><input type="checkbox" id="lyVoxel"> voxel 3D</label>
|
||||
<label><input type="checkbox" id="lyScan" checked> scan</label>
|
||||
<label><input type="checkbox" id="lyCloud" checked> cloud</label>
|
||||
<label><input type="checkbox" id="lyPath" checked> path</label>
|
||||
|
||||
Reference in New Issue
Block a user