-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
256 lines (238 loc) · 7.73 KB
/
app.js
File metadata and controls
256 lines (238 loc) · 7.73 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// CUDA Kernel dimensions
var sceneType = "grid";
// Cube parameters
var cubeSize = 0.8;
var blockMargin = 1; // Margin between blocks
// Camera parameters
const cameraDistance = 20;
const rotateSpeed = 0.05;
let blockRotation = { x: 0, y: 0, z: 0 };
// Three.js initialization
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const blockGroup = new THREE.Group();
scene.add(blockGroup);
function setScene() {
// Create a group to contain all cubes
document.getElementById("threadTotal").textContent =
"Thread total = " +
parseInt(document.getElementById("threadsInBlockx").value) *
parseInt(document.getElementById("threadsInBlocky").value) *
parseInt(document.getElementById("threadsInBlockz").value) *
parseInt(document.getElementById("blocksInGridx").value) *
parseInt(document.getElementById("blocksInGridy").value) *
parseInt(document.getElementById("blocksInGridz").value);
// Get the number of blocks in each dimension
const numBlocks = {
x: parseInt(document.getElementById("blocksInGridx").value),
y: parseInt(document.getElementById("blocksInGridy").value),
z: parseInt(document.getElementById("blocksInGridz").value),
};
const numThreads = {
x: parseInt(document.getElementById("threadsInBlockx").value),
y: parseInt(document.getElementById("threadsInBlocky").value),
z: parseInt(document.getElementById("threadsInBlockz").value),
};
// Create cubes representing blocks and add them to the group
if (sceneType === "grid") {
cubeSize = 0.8;
blockMargin = 1; // Margin between blocks
const blockMap = {};
for (let i = 0; i < numBlocks.x; i++) {
for (let j = 0; j < numBlocks.y; j++) {
for (let k = 0; k < numBlocks.z; k++) {
const geometry = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
const material = new THREE.MeshBasicMaterial({
color: 0x00ff00,
wireframe: true,
opacity: 1,
});
const cube = new THREE.Mesh(geometry, material);
cube.position.set(
i * (cubeSize + blockMargin),
j * (cubeSize + blockMargin),
k * (cubeSize + blockMargin)
);
blockGroup.add(cube);
blockMap[`${i}-${j}-${k}`] = cube;
}
}
}
} else {
cubeSize = 0.5;
blockMargin = 0.6; // Margin between blocks
// Only render the threads in one block as triangles
const threadMap = {};
for (let i = 0; i < numThreads.x; i++) {
for (let j = 0; j < numThreads.y; j++) {
for (let k = 0; k < numThreads.z; k++) {
const geometry = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
const material = new THREE.MeshBasicMaterial({
color: 0x00d5ff,
wireframe: true,
opacity: 1,
});
const cube = new THREE.Mesh(geometry, material);
cube.position.set(
i * (cubeSize + blockMargin),
j * (cubeSize + blockMargin),
k * (cubeSize + blockMargin)
);
blockGroup.add(cube);
threadMap[`${i}-${j}-${k}`] = cube;
}
}
}
// Add a green block that encloses all of the thread cubes
const enclosingGeometry = new THREE.BoxGeometry(
numThreads.x * cubeSize + (numThreads.x - 1) * blockMargin + 1,
numThreads.y * cubeSize + (numThreads.y - 1) * blockMargin + 1,
numThreads.z * cubeSize + (numThreads.z - 1) * blockMargin + 1
);
const enclosingMaterial = new THREE.MeshBasicMaterial({
color: 0x00ff00,
wireframe: true,
opacity: 1,
});
const enclosingCube = new THREE.Mesh(enclosingGeometry, enclosingMaterial);
enclosingCube.position.set(
((numThreads.x - 1) * (cubeSize + blockMargin)) / 2,
((numThreads.y - 1) * (cubeSize + blockMargin)) / 2,
((numThreads.z - 1) * (cubeSize + blockMargin)) / 2
);
blockGroup.add(enclosingCube);
}
camera.position.z = cameraDistance;
const axisIndicators = new THREE.AxesHelper(2);
scene.add(axisIndicators);
blockGroup.add(axisIndicators);
}
// Position camera
// Raycaster for hover detection
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
const hoveredBlock = new THREE.Vector3();
// Main render loop
function animate() {
requestAnimationFrame(animate);
rotateBlocks();
renderer.render(scene, camera);
}
setScene();
animate();
// Handle arrow key events for rotation
document.addEventListener("keydown", function (event) {
switch (event.key) {
case "ArrowLeft":
blockRotation.y += rotateSpeed;
break;
case "ArrowRight":
blockRotation.y -= rotateSpeed;
break;
case "ArrowUp":
blockRotation.x += rotateSpeed;
break;
case "ArrowDown":
blockRotation.x -= rotateSpeed;
break;
default:
return;
}
});
document.addEventListener("keyup", function (event) {
switch (event.key) {
case "ArrowLeft":
blockRotation.y = 0;
break;
case "ArrowRight":
blockRotation.y = 0;
break;
case "ArrowUp":
blockRotation.x = 0;
break;
case "ArrowDown":
blockRotation.x = 0;
break;
default:
return;
}
});
// Rotate the block group
function rotateBlocks() {
blockGroup.rotation.x += blockRotation.x;
blockGroup.rotation.y += blockRotation.y;
blockGroup.rotation.z += blockRotation.z;
}
// Event listener for mouse move
document.addEventListener("mousemove", onMouseMove);
function onMouseMove(event) {
// Calculate normalized device coordinates
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
// Raycast from camera
raycaster.setFromCamera(mouse, camera);
// Intersect blocks
const intersects = raycaster.intersectObjects(blockGroup.children);
if (intersects.length > 0) {
const intersect = intersects[0];
const position = intersect.object.position;
const blockIndex = `${Math.floor(
position.x / (cubeSize + blockMargin)
)}-${Math.floor(position.y / (cubeSize + blockMargin))}-${Math.floor(
position.z / (cubeSize + blockMargin)
)}`;
console.log("Block index:", blockIndex);
// You can display the block index however you want, for example:
document.getElementById(
"blockIndex"
).textContent = `Block index: ${blockIndex}`;
} else {
// No intersection
document.getElementById("blockIndex").textContent = "";
}
}
document.addEventListener("click", function (event) {
//get the cube that was clicked
const intersects = raycaster.intersectObjects(blockGroup.children);
if (intersects.length > 0) {
const intersect = intersects[0];
//remove all other blocks
blockGroup.children.forEach((block) => {
if (block !== intersect.object) {
block.visible = !block.visible;
}
});
}
});
function rerender() {
console.log("rerendering");
//clear the scene
const n = blockGroup.children.length - 1;
for (var i = n; i > -1; i--) {
blockGroup.remove(blockGroup.children[i]);
}
setScene();
//reset the scene
}
function toggleScene(type) {
if (type === "grid") {
sceneType = "grid";
document.getElementById("viewType").textContent = "Grid View";
document.getElementById("switchGrid").disabled = true;
document.getElementById("switchBlock").disabled = false;
} else {
sceneType = "block";
document.getElementById("viewType").textContent = "Block View";
document.getElementById("switchGrid").disabled = false;
document.getElementById("switchBlock").disabled = true;
}
rerender();
}