-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
137 lines (115 loc) · 4.2 KB
/
script.js
File metadata and controls
137 lines (115 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import * as THREE from "three";
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
const imagePaths = {
canvas1: {
msb: "media/msblsb/msb.png",
lsb: "media/msblsb/lsb.png"
},
canvas2: {
msb: "media/msblsb/msb.png",
lsb: "media/msblsb/lsb.png"
}
};
const fx = 504.135, fy = 504.129;
const cx = 324.466, cy = 327.652;
const depthScale = 1 / 1000;
function createScene(canvasId) {
const container = document.getElementById(canvasId);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, container.clientWidth / container.clientHeight, 0.1, 1000);
camera.position.z = 0.1;
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(container.clientWidth, container.clientHeight);
container.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.1;
controls.rotateSpeed = 1.5;
controls.zoomSpeed = 2.0;
controls.panSpeed = 1.2;
controls.enablePan = true;
controls.minDistance = 0.05;
controls.maxDistance = 2000;
controls.touches = {
ONE: THREE.TOUCH.ROTATE,
TWO: THREE.TOUCH.DOLLY_PAN
};
controls.touchRotateSpeed = 2.0;
controls.touchZoomSpeed = 2.0;
controls.touchPanSpeed = 1.5;
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
animate();
window.addEventListener("resize", () => {
const newWidth = container.clientWidth;
const newHeight = container.clientHeight;
camera.aspect = newWidth / newHeight;
camera.updateProjectionMatrix();
renderer.setSize(newWidth, newHeight);
});
return { scene, camera, renderer };
}
const scenes = {
canvas1: createScene("canvas1"),
canvas2: createScene("canvas2")
};
function loadImage(imagePath) {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => resolve(img);
img.onerror = reject;
img.src = imagePath;
});
}
async function loadPointCloud(canvasId) {
const { msb, lsb } = imagePaths[canvasId];
try {
const [msbImg, lsbImg] = await Promise.all([loadImage(msb), loadImage(lsb)]);
const width = msbImg.width;
const height = msbImg.height;
const canvasMSB = document.createElement("canvas");
canvasMSB.width = width;
canvasMSB.height = height;
const ctxMSB = canvasMSB.getContext("2d");
ctxMSB.drawImage(msbImg, 0, 0, width, height);
const msbData = ctxMSB.getImageData(0, 0, width, height).data;
const canvasLSB = document.createElement("canvas");
canvasLSB.width = width;
canvasLSB.height = height;
const ctxLSB = canvasLSB.getContext("2d");
ctxLSB.drawImage(lsbImg, 0, 0, width, height);
const lsbData = ctxLSB.getImageData(0, 0, width, height).data;
const points = [];
for (let v = 0; v < height; v++) {
for (let u = 0; u < width; u++) {
const idx = (v * width + u) * 4;
const msb = msbData[idx];
const lsb = lsbData[idx];
const depthValue = msb * 256 + lsb;
const z = depthValue * depthScale;
if (z > 0) {
const x = ((u - cx) * z) / fx;
const y = -((v - cy) * z) / fy;
points.push(x, y, z);
}
}
}
const pointsArray = new Float32Array(points);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute("position", new THREE.BufferAttribute(pointsArray, 3));
const material = new THREE.PointsMaterial({
color: canvasId === "canvas1" ? 0x8C1D40 : 0xFFC627,
size: 0.0005
});
const pointCloud = new THREE.Points(geometry, material);
pointCloud.scale.z = -1;
scenes[canvasId].scene.add(pointCloud);
} catch (err) {
console.error(`Error loading images for ${canvasId}:`, err);
}
}
loadPointCloud("canvas1");
loadPointCloud("canvas2");