-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.html
272 lines (233 loc) · 9.6 KB
/
index.html
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style>
html, body {
padding: 0;
margin: 0;
width: 100%;
height: 100%;
text-align: center;
}
body {
padding-top: 25vh;
}
body.rendering {
padding-top: 0;
}
.msg {
display: none;
}
body.loading .msg {
display: block;
}
form {
display: inline-block;
padding: 1vw 5vw;
border: solid 1px #eee;
border-radius: 1vw;
}
form div {
margin-bottom: 3vw;
}
body.loading form,
body.rendering form {
display: none;
}
canvas {
display: none;
}
body.rendering canvas {
display: block;
}
#status1 {
font-size: 150%;
font-weight: bold;
}
#branding {
position: absolute;
top: 16px;
right: 16px;
text-align: right;
}
input[type='button'] {
padding: 8px 64px;
margin-top: 3vw;
}
</style>
</head>
<body class='loading'>
<div id='branding'>
<img src='http://ifcopenshell.org/static/ifcopenshell_logo.png' height='64' width='auto'>
<img src='https://upload.wikimedia.org/wikipedia/commons/1/1f/WebAssembly_Logo.svg' height='64' width='auto'>
<br>
<b>IfcOpenShell</b> WebAssembly
</div>
<div class='msg'>
<div id='status1'>Loading...</div>
<div id='status2'></div>
</div>
<form action='#' onsubmit="return false;">
<div>Client-side model viewer</div>
<input type='file' id='fileinput'><br>
<input type='button' id='btn' value='Load'>
</form>
<script type="text/javascript">document.querySelector("#status2").innerHTML = "Fetching pyodide";</script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/pyodide/v0.22.0a1/full/pyodide.js"></script>
<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
<script type="importmap">
{"imports": {"three": "https://unpkg.com/[email protected]/build/three.module.js",
"OrbitControls": "https://unpkg.com/[email protected]/examples/jsm/controls/OrbitControls.js"}}
</script>
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from 'OrbitControls';
THREE.Object3D.DefaultUp = new THREE.Vector3(0,0,1);
let pyodide = null;
async function main() {
document.querySelector("#status2").innerHTML = "Initializing pyodide";
pyodide = await loadPyodide();
document.querySelector("#status2").innerHTML = "Loading package manager";
await pyodide.loadPackage("micropip");
const micropip = pyodide.pyimport("micropip");
document.querySelector("#status2").innerHTML = "Loading IfcOpenShell (this may take a while)";
await micropip.install("IfcOpenShell-0.7.0-py3-none-any.whl");
console.log(await pyodide.runPython(`
import ifcopenshell
ifcopenshell.version`));
document.body.className = ''
document.querySelector("input[type=button]").onclick = async () => {
document.body.className = 'loading'
document.querySelector("#status2").innerHTML = "Generating geometry";
let file = document.querySelector("input").files[0];
let content = await file.text();
let ifcopenshell = pyodide.pyimport('ifcopenshell')
let ifc = ifcopenshell.file.from_string(content);
let ifcopenshell_geom = pyodide.pyimport('ifcopenshell.geom');
let s = ifcopenshell_geom.settings();
s.set(s.WELD_VERTICES, false);
/*
let it = ifcopenshell_geom.iterator.callKwargs({
'settings': s,
'file_or_filename': ifc,
'num_threads': 8});
'exclude':['IfcSpace', 'IfcOpeningElement']});
*/
let it = ifcopenshell_geom.iterator(s, ifc);
let last_mesh_id = null;
let geometries;
const renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
renderer.setClearColor(0x000000, 0);
const scene = new THREE.Scene();
var light = new THREE.DirectionalLight(0xFFFFFF);
light.position.set(20, 10, 30);
scene.add(light);
var light = new THREE.DirectionalLight(0xFFFFFF, 0.8);
light.position.set(-10, 1, -30);
scene.add(light);
scene.add(new THREE.AmbientLight(0x404050));
const camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
const controls = new OrbitControls( camera, renderer.domElement );
if (it.initialize()) {
while (true) {
let obj = it.get();
// obj.type appears to be overwritten by pyodide, returning the typename of the C++ class?
let ty = ifc.by_id(obj.id).is_a()
if (ty !== 'IfcOpeningElement' && ty !== 'IfcSpace') {
if (last_mesh_id != obj.geometry.id) {
geometries = [];
let materials = obj.geometry.materials.toJs().map(e => new THREE.MeshLambertMaterial({
color: new THREE.Color(...e.diffuse.toJs()),
opacity: 1.0 - e.transparency,
transparent: e.transparency > 1.e-5,
side: THREE.DoubleSide
}));
let mapping = {};
obj.geometry.material_ids.toJs().forEach((i, idx) => {
mapping[i] = mapping[i] || []
mapping[i].push(idx);
});
let vs = new Float32Array(obj.geometry.verts.toJs());
let ns = new Float32Array(obj.geometry.normals.toJs());
let fs = obj.geometry.faces.toJs();
// Default material
let offset = 0;
if (mapping[-1]) {
materials.unshift(new THREE.MeshLambertMaterial({
color: new THREE.Color(0.6,0.6,0.6),
side: THREE.DoubleSide
}));
offset = 1;
}
materials.forEach((m, mi) => {
let geometry = new THREE.BufferGeometry();
geometry.setIndex(mapping[mi - offset].flatMap(i => [fs[3*i+0], fs[3*i+1], fs[3*i+2]]));
geometry.setAttribute('position', new THREE.Float32BufferAttribute(vs, 3));
geometry.setAttribute('normal', new THREE.Float32BufferAttribute(ns, 3));
geometries.push([geometry, m]);
});
last_mesh_id = obj.geometry.id;
}
for (let gm of geometries) {
let [g, mat] = gm;
let mesh = new THREE.Mesh(g, mat);
var matrix = new THREE.Matrix4();
const m = obj.transformation.matrix.data.toJs();
matrix.set(m[0], m[1], m[2], 0, m[3], m[4], m[5], 0, m[6], m[7], m[8], 0, m[9], m[10], m[11], 1);
matrix.transpose();
mesh.matrixAutoUpdate = false;
mesh.matrix = matrix;
scene.add(mesh);
}
}
if (!it.next()) {
break;
}
}
}
document.body.className = 'rendering';
var boundingBox = new THREE.Box3();
boundingBox.setFromObject(scene);
var center = new THREE.Vector3();
boundingBox.getCenter(center);
controls.target = center;
var viewDistance = boundingBox.getSize(new THREE.Vector3()).length();
camera.position.copy(center.clone().add(
new THREE.Vector3(0.5, 0.25, 1).normalize().multiplyScalar(viewDistance)
));
camera.near = viewDistance / 100;
camera.far = viewDistance * 100;
controls.update();
camera.updateProjectionMatrix();
camera.updateMatrixWorld();
var fovFactor = Math.tan(camera.fov / 2 / 180 * 3.141592653);
var outside = 0.;
var largestAngle = 0.;
for (var i = 0; i < 8; i++) {
const v = new THREE.Vector3(
i & 1 ? boundingBox.min.x : boundingBox.max.x,
i & 2 ? boundingBox.min.y : boundingBox.max.y,
i & 4 ? boundingBox.min.z : boundingBox.max.z
);
v.applyMatrix4(camera.matrixWorldInverse);
outside = Math.max(outside, Math.abs(v.x / camera.aspect) - fovFactor * -v.z, Math.abs(v.y) - fovFactor * -v.z);
}
viewDistance += outside * 2;
camera.position.copy(center.clone().add(
new THREE.Vector3(0.5, 0.25, 1).normalize().multiplyScalar(viewDistance)
));
controls.update();
function render() {
requestAnimationFrame(render);
renderer.render(scene, camera);
}
render();
}
}
main();
</script>
</body>
</html>