-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
191 lines (163 loc) · 5.5 KB
/
script.js
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
function initPointCloudScene(container) {
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x000000);
const width = container.clientWidth;
const height = container.clientHeight;
const camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 1000);
camera.position.set(0, 0, 3);
camera.lookAt(0, 0, 0);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(width, height);
container.appendChild(renderer.domElement);
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableZoom = true;
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 };
}
function loadDepthImageData(imagePath) {
return new Promise((resolve, reject) => {
const textureLoader = new THREE.TextureLoader();
textureLoader.load(
imagePath,
(texture) => {
const img = texture.image;
const canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
const ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
resolve({ imageData, width: canvas.width, height: canvas.height });
},
undefined,
(err) => {
reject(err);
}
);
});
}
function createPointCloudGeometry(imageData, width, height) {
const data = imageData.data;
const positions = [];
const colors = [];
const fx = 525;
const fy = 525;
const cx = width / 2;
const cy = height / 2;
const maxDepth = 10;
for (let v = 0; v < height; v++) {
for (let u = 0; u < width; u++) {
const index = (v * width + u) * 4;
const depthIntensity = data[index] / 255;
const z = depthIntensity * maxDepth;
const x = ((u - cx) * z) / fx;
const y = ((v - cy) * z) / fy;
positions.push(x, -y, z);
colors.push(depthIntensity, depthIntensity, depthIntensity);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(positions), 3)
);
geometry.setAttribute(
"color",
new THREE.BufferAttribute(new Float32Array(colors), 3)
);
return geometry;
}
function init() {
const canvasIds = ["canvas1", "canvas2"];
const depthImages = [
"media/capture/depth/depth_1.png",
"media/test_only/depth_frame_0006.png"
];
const scenes = [];
const fixedColors = [0x00ff00, 0xff0000];
canvasIds.forEach((id, index) => {
const container = document.getElementById(id);
const sceneObj = initPointCloudScene(container);
scenes.push(sceneObj);
loadDepthImageData(depthImages[index])
.then(({ imageData, width, height }) => {
const geometry = createPointCloudGeometry(imageData, width, height);
const material = new THREE.PointsMaterial({
size: 0.005,
color: new THREE.Color(fixedColors[index]),
vertexColors: false,
});
const pointCloud = new THREE.Points(geometry, material);
sceneObj.scene.add(pointCloud);
console.log(`Point cloud added to ${id}`);
})
.catch((err) => {
console.error(`Error loading depth image for ${id}:`, err);
});
});
}
init();
document.addEventListener("DOMContentLoaded", function () {
const btnColor = document.getElementById("btnColor");
const btnDepth = document.getElementById("btnDepth");
const btnPointCloud = document.getElementById("btnPointCloud");
const canvas1 = document.getElementById("canvas1");
const canvas2 = document.getElementById("canvas2");
const colorImages = {
canvas1: "media/logos/asulogo.png",
canvas2: "media/logos/meteorstudiologo.jpeg",
};
const depthImages = {
canvas1: "media/test_only/depth_frame_0006.png",
canvas2: "media/test_only/depth_frame_0006.png",
};
function showOverlay(imageSrc, canvas) {
let overlay = canvas.querySelector(".overlay");
if (!overlay) {
overlay = document.createElement("img");
overlay.classList.add("overlay");
canvas.appendChild(overlay);
}
overlay.src = imageSrc;
overlay.style.display = "block";
}
function removeOverlay(canvas) {
const overlay = canvas.querySelector(".overlay");
if (overlay) {
overlay.style.display = "none";
}
}
btnColor.addEventListener("click", function () {
showOverlay(colorImages.canvas1, canvas1);
showOverlay(colorImages.canvas2, canvas2);
btnColor.style.display = "none";
btnDepth.style.display = "none";
btnPointCloud.style.display = "inline-block";
});
btnDepth.addEventListener("click", function () {
showOverlay(depthImages.canvas1, canvas1);
showOverlay(depthImages.canvas2, canvas2);
btnColor.style.display = "none";
btnDepth.style.display = "none";
btnPointCloud.style.display = "inline-block";
});
btnPointCloud.addEventListener("click", function () {
removeOverlay(canvas1);
removeOverlay(canvas2);
btnPointCloud.style.display = "none";
btnColor.style.display = "inline-block";
btnDepth.style.display = "inline-block";
});
});