-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteethTradBVH.html
More file actions
217 lines (167 loc) · 7.89 KB
/
teethTradBVH.html
File metadata and controls
217 lines (167 loc) · 7.89 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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Traditional BVH Pick</title>
<script src= "babylon.max.js"></script>
<script src = babylon.gui.min.js></script>
<script src= "babylon.stlFileLoader.js"></script>
<script src= "BVH_Acc_Structure_Iterative_Builder.js"></script>
<script src = "bvhray.js"></script>
<style>
html, body {
overflow: hidden;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#renderCanvas {
width: 100%;
height: 100%;
touch-action: none;
}
</style>
</head>
<body>
<canvas id="renderCanvas"></canvas>
<script>
if (BABYLON.Engine.isSupported()) {
var canvas = document.getElementById("renderCanvas");
var engine = new BABYLON.Engine(canvas, true);
var createScene = function () {
// This creates a basic Babylon Scene object (non-mesh)
var scene = new BABYLON.Scene(engine);
var camera = new BABYLON.ArcRotateCamera("Camera", 0, Math.PI / 2.2, 50, BABYLON.Vector3.Zero(), scene);
camera.attachControl(canvas, true);
// This creates a light, aiming 0,1,0 - to the sky (non-mesh)
var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);
// Default intensity is 1. Let's dim the light a small amount
light.intensity = 0.7;
mat = new BABYLON.StandardMaterial("", scene);
mat.diffuseColor = BABYLON.Color3.Red();
let totalWork;
let aabb_array = new Float32Array(2048 * 2048 * 4);
// 2048 = width of texture, 2048 = height of texture, 4 = r,g,b, and a components
BABYLON.SceneLoader.ImportMesh("", "https://dl.dropbox.com/s/669cwldog3kdssw/", "2020-05-25_002_002-LowerJaw.stl", scene, function (meshes) {
let helmet = meshes[0];
let positions = helmet.getVerticesData(BABYLON.VertexBuffer.PositionKind);
console.log("P", positions.length / 3);
let indices = helmet.getIndices();
console.log("I", indices)
; let total_number_of_triangles = indices.length / 3;
console.log("TRI", total_number_of_triangles)
totalWork = new Uint32Array(total_number_of_triangles);
var vp0 = BABYLON.Vector3.Zero();
var vp1 = BABYLON.Vector3.Zero();
var vp2 = BABYLON.Vector3.Zero();
let gui = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("myUI");
let button = BABYLON.GUI.Button.CreateSimpleButton("button", "Click to Generate BVH");
button.top = "0px";
button.left = "0px";
button.width = "150px";
button.height = "150px";
button.cornerRadius = 20;
button.thickness = 4;
button.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_RIGHT;
button.children[0].color = "#DFF9FB";
button.children[0].fontSize = 24;
button.color = "white";
button.background = "blue";
let text1 = new BABYLON.GUI.TextBlock();
text1.text = "";
text1.color = "white";
text1.fontSize = 24;
text1.background = "blue";
text1.width = "650px";
text1.height = "70px";
text1.cornerRadius = 20;
text1.thickness = 4;
text1.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_TOP;
text1.isVisible = false;
gui.addControl(text1);
let treeCluster;
button.onPointerClickObservable.add(function () {
button.isVisible = false
// Build the BVH acceleration structure, which places a bounding box ('root' of the tree) around all of the
// triangles of the entire mesh, then subdivides each box into 2 smaller boxes. It continues until it reaches 1 triangle,
// which it then designates as a 'leaf'
//console.log("TL", totalWork, "AABB", aabb_array)
let t0 = performance.now();
var triangle_b_box_min = BABYLON.Vector3.Zero();
var triangle_b_box_max = BABYLON.Vector3.Zero();
var triangle_b_box_centroid = BABYLON.Vector3.Zero();
for (let i = 0; i < total_number_of_triangles; i++) {
triangle_b_box_min.set(Infinity, Infinity, Infinity);
triangle_b_box_max.set(-Infinity, -Infinity, -Infinity);
index0 = indices[3 * i];
index1 = indices[3 * i + 1];
index2 = indices[3 * i + 2];
// record vertex positions
vp0.set(positions[3 * index0], positions[3 * index0 + 1], positions[3 * index0 + 2]);
vp1.set(positions[3 * index1], positions[3 * index1 + 1], positions[3 * index1 + 2]);
vp2.set(positions[3 * index2], positions[3 * index2 + 1], positions[3 * index2 + 2]);
triangle_b_box_min = BABYLON.Vector3.Minimize(triangle_b_box_min, vp0);
triangle_b_box_max = BABYLON.Vector3.Maximize(triangle_b_box_max, vp0);
triangle_b_box_min = BABYLON.Vector3.Minimize(triangle_b_box_min, vp1);
triangle_b_box_max = BABYLON.Vector3.Maximize(triangle_b_box_max, vp1);
triangle_b_box_min = BABYLON.Vector3.Minimize(triangle_b_box_min, vp2);
triangle_b_box_max = BABYLON.Vector3.Maximize(triangle_b_box_max, vp2);
triangle_b_box_centroid.set((triangle_b_box_min.x + triangle_b_box_max.x) * 0.5,
(triangle_b_box_min.y + triangle_b_box_max.y) * 0.5,
(triangle_b_box_min.z + triangle_b_box_max.z) * 0.5);
aabb_array[9 * i + 0] = triangle_b_box_min.x;
aabb_array[9 * i + 1] = triangle_b_box_min.y;
aabb_array[9 * i + 2] = triangle_b_box_min.z;
aabb_array[9 * i + 3] = triangle_b_box_max.x;
aabb_array[9 * i + 4] = triangle_b_box_max.y;
aabb_array[9 * i + 5] = triangle_b_box_max.z;
aabb_array[9 * i + 6] = triangle_b_box_centroid.x;
aabb_array[9 * i + 7] = triangle_b_box_centroid.y;
aabb_array[9 * i + 8] = triangle_b_box_centroid.z;
totalWork[i] = i;
}
BVH_Build_Iterative(totalWork, aabb_array);
let t1 = performance.now();
text1.text = `Time to Generate ${t1 - t0} milliseconds.`;
text1.isVisible = true;
console.log(buildnodes);
let counter = 0;
let totalTime = 0;
scene.onPointerDown = function() {
//let ray = new BABYLON.Ray(new BABYLON.Vector3(-8, 0.1, 0.1), BABYLON.Axis.X, 120);
let ray = scene.createPickingRay(scene.pointerX, scene.pointerY, BABYLON.Matrix.Identity(), camera);
//BABYLON.RayHelper.CreateAndShow(ray, scene, BABYLON.Color3.Red());
let positions = helmet.getVerticesData(BABYLON.VertexBuffer.PositionKind);
let indices = helmet.getIndices();
const t0 = performance.now();
let hits = [];
hits = bvhRayIntersect(ray, buildnodes, helmet);
const t1 = performance.now();
text1.text = `Time for ray intersection ${t1 - t0} milliseconds.`;
totalTime += t1 - t0;
counter++;
console.log("avg", totalTime / counter);
text1.isVisible = true;
for (let i = 0; i < hits.length; i++) {
box = BABYLON.MeshBuilder.CreateBox("", {size: 0.5}, scene);
box.position = ray.origin.add(ray.direction.normalize().scale(hits[i].distance))
box.material = mat;
}
};
});
gui.addControl(button);
});
return scene;
};
var scene = createScene();
engine.runRenderLoop(function () {
scene.render();
});
window.addEventListener("resize", function () {
engine.resize();
});
}
</script>
</body>
</html>