<!DOCTYPE html>
<html>
<head>
<base target="_top">
<style>
body { margin: 0; overflow: hidden; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; }
#ui { position: absolute; top: 15px; left: 15px; background: rgba(255,255,255,0.9); padding: 15px; border-radius: 8px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); z-index: 100;}
h3 { margin: 0 0 10px 0; color: #1a73e8; }
select { padding: 8px; font-size: 14px; border: 1px solid #ccc; border-radius: 4px; width: 100%;}
canvas { display: block; }
</style>
<!-- Nhúng Thư viện Three.js để vẽ 3D -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<!-- Nhúng công cụ dùng chuột xoay góc nhìn -->
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.js"></script>
</head>
<body>
<div id="ui">
<h3>Mô phỏng 3D Container</h3>
<select id="containerSelect" onchange="renderContainer(this.value)">
<option value="">Đang tải dữ liệu...</option>
</select>
<p style="font-size: 12px; color: #555; margin-bottom: 0;">Chuột trái: Xoay | Cuộn: Zoom | Chuột phải: Kéo</p>
</div>
<script>
let scene, camera, renderer, controls;
let allData = null;
let currentBoxes = [];
function init() {
// 1. Khởi tạo Không gian (Scene), Camera và Trình xuất hình (Renderer)
scene = new THREE.Scene();
scene.background = new THREE.Color(0xe0e0e0);
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 50000);
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
controls = new THREE.OrbitControls(camera, renderer.domElement);
// 2. Thiết lập Ánh sáng (để thấy rõ khối hộp)
scene.add(new THREE.AmbientLight(0x888888));
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(1, 1, 0.5).normalize();
scene.add(directionalLight);
window.addEventListener('resize', onWindowResize, false);
// 3. Gọi hàm bên Apps Script để lấy dữ liệu Tọa độ
google.script.run.withSuccessHandler(onDataLoaded).getContainerData();
animate();
}
function onDataLoaded(data) {
if(!data || Object.keys(data.placements).length === 0) {
document.getElementById("containerSelect").innerHTML = "<option>Không có dữ liệu!</option>";
return;
}
allData = data;
// Cập nhật danh sách Container vào Dropdown
const select = document.getElementById("containerSelect");
select.innerHTML = "";
const cIds = Object.keys(data.placements);
cIds.forEach(id => {
const opt = document.createElement("option");
opt.value = id; opt.innerText = id;
select.appendChild(opt);
});
if(cIds.length > 0) renderContainer(cIds[0]); // Vẽ Container đầu tiên
}
function renderContainer(cId) {
// Xóa tất cả các khối cũ trên màn hình
currentBoxes.forEach(box => scene.remove(box));
currentBoxes = [];
const cDim = allData.containerDim;
const boxes = allData.placements[cId];
// HỆ TỌA ĐỘ THREE.JS: Trục Y là hướng Lên Trời (Cao), X là Sang Phải, Z là Đâm về phía người nhìn.
// Do đó: X = L, Z = W, Y = H (Cao)
// Vẽ lồng dây thép của Container (Wireframe)
const cGeo = new THREE.BoxGeometry(cDim.L, cDim.H, cDim.W);
const cEdges = new THREE.EdgesGeometry(cGeo);
const cLine = new THREE.LineSegments(cEdges, new THREE.LineBasicMaterial({ color: 0x000000, linewidth: 2 }));
// Đặt gốc tọa độ (0,0,0) nằm ở 1 góc lồng
cLine.position.set(cDim.L/2, cDim.H/2, cDim.W/2);
scene.add(cLine);
currentBoxes.push(cLine);
// Bảng màu phân biệt các thùng
const colors = [0xff5252, 0x448aff, 0x69f0ae, 0xffd740, 0xe040fb, 0xffab40, 0x00b0ff, 0xff6e40];
// Vẽ từng thùng hàng
boxes.forEach((b, index) => {
const geo = new THREE.BoxGeometry(b.L, b.H, b.W);
const mat = new THREE.MeshLambertMaterial({
color: colors[index % colors.length],
transparent: true, opacity: 0.85, // Hơi trong suốt để nhìn xuyên thấu
polygonOffset: true, polygonOffsetFactor: 1, polygonOffsetUnits: 1
});
const mesh = new THREE.Mesh(geo, mat);
// Vẽ viền đen cho dễ nhìn ranh giới từng thùng
const edges = new THREE.EdgesGeometry(geo);
const line = new THREE.LineSegments(edges, new THREE.LineBasicMaterial({ color: 0x333333 }));
mesh.add(line);
// Tọa độ đặt = Tọa độ gốc X, Y, Z + 1/2 kích thước thùng
mesh.position.set(b.X + b.L/2, b.Z + b.H/2, b.Y + b.W/2);
scene.add(mesh);
currentBoxes.push(mesh);
});
// Thiết lập vị trí Camera nhìn bao quát từ góc trên cao
controls.target.set(cDim.L/2, cDim.H/2, cDim.W/2);
camera.position.set(cDim.L * 1.5, cDim.H * 1.2, cDim.W * 1.8);
controls.update();
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
window.onload = init;
</script>
</body>
</html>