This commit is contained in:
trung09122002
2026-09-19 15:33:52 +07:00
parent 48bd5377dc
commit 3b281174d0
22 changed files with 1913 additions and 119 deletions

View File

@@ -56,7 +56,9 @@ function v3dInit(canvas) {
// lai cua trang nay la nen toi, nhung khung 3D co bang mau RIENG -- vi muc
// tieu cua no la trong giong Gazebo, va tuong trang tren nen toi doc rat te:
// khong co bong, khong co chieu sau, moi khoi thanh mot mang bet.
V3D.scene.background = new THREE.Color(0xb8c6d4);
// ⚠️ Tone nay phai khop `PALETTE.world` trong app.js: hai khung cung the
// hien MOT the gioi, lech tone thi nhin nhu hai noi khac nhau.
V3D.scene.background = new THREE.Color(0xa9b8c6); // = PALETTE.world.bg
// Suong mu xa: ban do 2000x2000 o do phan giai 0.05 m la 100 m moi chieu,
// ve het se thanh mot vung nhieu xam. Suong mu cat no mem hon la cat cung.
// Mau suong = mau nen, neu khong se thay mot duong chan troi gia.
@@ -89,7 +91,7 @@ function v3dInit(canvas) {
V3D.scene.add(V3D.root);
// Luoi san 1 m, va truc goc toa do map.
V3D.gridHelper = new THREE.GridHelper(40, 40, 0x93a3b3, 0xa9b6c4);
V3D.gridHelper = new THREE.GridHelper(40, 40, 0x93a2b0, 0xa6b3c0);
V3D.root.add(V3D.gridHelper);
V3D.root.add(new THREE.AxesHelper(0.5));
@@ -109,13 +111,41 @@ function v3dBindMouse(canvas) {
const dx = e.clientX - V3D.dragging.x, dy = e.clientY - V3D.dragging.y;
V3D.dragging.x = e.clientX; V3D.dragging.y = e.clientY;
if (V3D.dragging.btn === 0) {
// ⚠️ CANH DI THEO TAY, va DAU CUA HAI TRUC KHONG GIONG NHAU.
//
// Suy luan tay o day rat de sai: phep xoay lam ca VI TRI lan HE TRUC cua
// camera doi cung luc, va phan he truc moi la phan quyet dinh. Toi da
// suy sai mot lan va dao ca hai truc, lam hong truc ngang trong khi
// dang sua truc doc.
//
// Dau duoi day duoc CHOT BANG PHEP DO: `test/www/test_orbit_camera.js`
// chieu mot diem the gioi qua dung phep camera cua v3dRender() roi xem
// no di ve phia nao. Doi dau o day ma khong chay bai do lai la quay ve
// dung cho cu.
V3D.orbit.az -= dx * 0.006;
// Chan o hai dau: qua thien dinh thi khung nhin lat nguoc va nguoi dung
// mat phuong huong.
V3D.orbit.el = Math.max(0.05, Math.min(1.45, V3D.orbit.el + dy * 0.006));
V3D.orbit.el = Math.max(0.05, Math.min(1.45, V3D.orbit.el - dy * 0.006));
} else {
V3D.orbit.tx -= dx * 0.01;
V3D.orbit.ty += dy * 0.01;
// Keo canh: phai di theo HUONG NHIN, khong theo truc the gioi. Khong doi
// he thi o mot goc bat ky, keo sang phai lam canh di cheo.
//
// Dau tru: `tx`/`ty` la TAM NHIN. Canh di sang phai nghia la tam nhin
// di sang TRAI.
const c = Math.cos(V3D.orbit.az), s = Math.sin(V3D.orbit.az);
const k = V3D.orbit.dist * 0.0016;
// ⚠️ Truc DOC phai bu do rut ngan theo goc nhin.
//
// Mat san nhin cheo bi rut ngan mot he so sin(el): doi tam nhin 1 m ve
// phia truoc lam canh dich it hon nhieu so voi 1 m sang ngang. Dung
// chung mot he so cho ca hai truc lam canh KEO HUT theo chieu doc -- do
// duoc: ti le do doi 0.27 trong khi ti le keo la 0.63.
//
// Chan mau o 0.25 cho goc nhin "canh": o do sin(el) -> 0 va phep bu se
// no ra vo cuc.
const kf = k / Math.max(Math.sin(V3D.orbit.el), 0.25);
V3D.orbit.tx -= dx * s * k + dy * c * kf;
V3D.orbit.ty -= -dx * c * k + dy * s * kf;
}
});
canvas.addEventListener('wheel', (e) => {
@@ -135,23 +165,36 @@ function v3dBuildRobot(r) {
// toa do map voi san la 0.
g.position.y = r.wheel_radius;
const bodyMat = new THREE.MeshLambertMaterial({ color: 0x3d6ea5 });
const bodyMat = new THREE.MeshLambertMaterial({ color: 0x1a6ea8 });
const darkMat = new THREE.MeshLambertMaterial({ color: 0x20262e });
// Than: tru tron ban kinh `radius`. Chieu cao chon theo ban kinh, khong
// phai mot hang so -- robot.yaml khong co chieu cao than.
const bodyH = r.radius * 1.2;
// Than: KHOI HOP, dung kich thuoc that tu robot.yaml.
//
// Ban truoc ve hinh tru ban kinh `robot_radius` vi robot.yaml chua co chieu
// cao than. Nhung robot that la mot hop 0.40 x 0.34 x 0.85 m -- hinh tru
// 0.24 m lam no trong lun va tron, khong nhan ra duoc huong.
//
// `robot_radius` van la thu dung cho va cham: mot hinh tron bao ngoai hop.
// Khong dung kich thuoc hop cho va cham -- lam vay thi phep va cham phu
// thuoc goc quay, va do la mot lop bug moi.
const bodyL = r.body_l || r.radius * 1.6;
const bodyW = r.body_w || r.radius * 1.4;
const bodyH = r.body_h || r.radius * 1.2;
const clear = r.clearance || 0.0;
const body = new THREE.Mesh(
new THREE.CylinderGeometry(r.radius, r.radius, bodyH, 28), bodyMat);
body.position.y = bodyH / 2;
new THREE.BoxGeometry(bodyL, bodyH, bodyW), bodyMat);
// `base_link` o tam truc banh; day hop cach SAN dung `clearance`, nen tam
// hop nam tren base_link mot doan (clearance + bodyH/2 - wheel_radius).
body.position.set(r.body_off_x || 0, clear + bodyH / 2 - r.wheel_radius, 0);
g.add(body);
// Mui chi huong +X, de nhin mot cai la biet dau la truoc.
const nose = new THREE.Mesh(
new THREE.ConeGeometry(r.radius * 0.28, r.radius * 0.5, 16),
new THREE.ConeGeometry(bodyW * 0.16, bodyL * 0.22, 16),
new THREE.MeshLambertMaterial({ color: 0x8ec5ff }));
nose.rotation.z = -Math.PI / 2;
nose.position.set(r.radius * 0.85, bodyH * 0.55, 0);
nose.position.set((r.body_off_x || 0) + bodyL / 2,
clear + bodyH * 0.85 - r.wheel_radius, 0);
g.add(nose);
// Hai banh chu dong, cach nhau `wheel_sep`.
@@ -402,7 +445,7 @@ function v3dSetGround(grid) {
const fw = grid.w * r, fh = grid.h * r;
V3D.floorMesh = new THREE.Mesh(
new THREE.PlaneGeometry(fw, fh),
new THREE.MeshStandardMaterial({ color: 0x9aa5ad, roughness: 1.0 }));
new THREE.MeshStandardMaterial({ color: 0xe8edf1, roughness: 1.0 }));
V3D.floorMesh.rotation.x = -Math.PI / 2;
V3D.floorMesh.position.set(grid.ox + fw / 2, -0.004, -(grid.oy + fh / 2));
V3D.floorMesh.receiveShadow = true;
@@ -416,7 +459,7 @@ function v3dSetGround(grid) {
V3D.scene.fog.far = span * 1.4;
if (V3D.gridHelper) V3D.root.remove(V3D.gridHelper);
V3D.gridHelper = new THREE.GridHelper(Math.ceil(span), Math.ceil(span),
0x93a3b3, 0xa9b6c4);
0x93a2b0, 0xa6b3c0);
V3D.root.add(V3D.gridHelper);
}
@@ -683,6 +726,27 @@ function v3dSetWorldVisible(on) {
if (V3D.worldMesh) V3D.worldMesh.visible = on;
}
// Dat khung nhin ve mot goc chuan.
//
// Ba goc nay la ba cau hoi khac nhau: "mat bang the nao" (tren), "cao thap ra
// sao" (canh), va "tong the" (cheo). Xoay tay toi dung mot trong ba goc do rat
// kho, nen chung la nut.
function v3dSetView(name) {
if (!V3D.ready) return;
const o = V3D.orbit;
if (name === 'top') {
o.el = 1.45; // gan thien dinh; 1.5708 lam khung nhin suy bien
o.az = -Math.PI / 2; // +X sang phai, +Y len tren -- nhu khung 2D
} else if (name === 'side') {
o.el = 0.06; // sat mat san
o.az = -Math.PI / 2;
} else {
o.el = 0.55;
o.az = -2.2;
}
o.tx = 0; o.ty = 0;
}
function v3dSetOverlays(on) {
if (!V3D.ready || !V3D.root) return;
V3D.root.traverse((o) => {