-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
250 lines (190 loc) · 7.17 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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="styles.css">
<title>Components | Hello world</title>
</head>
<body>
<div class="full-screen" id="container"></div>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/[email protected]/build/three.module.js",
"three/examples/jsm/lines/LineMaterial": "https://unpkg.com/[email protected]/examples/jsm/lines/LineMaterial.js",
"three/examples/jsm/libs/lil-gui.module.min": "https://unpkg.com/[email protected]/examples/jsm/libs/lil-gui.module.min.js",
"stats.js/src/Stats.js": "https://unpkg.com/[email protected]/src/Stats.js",
"unzipit": "https://unpkg.com/[email protected]/dist/unzipit.module.js",
"client-zip": "https://unpkg.com/[email protected]/index.js"
}
}
</script>
<script type="module">
import { unzip } from 'unzipit';
import * as THREE from 'three';
import { LineMaterial } from 'three/examples/jsm/lines/LineMaterial';
import Stats from 'stats.js/src/Stats.js';
import * as OBC from './openbim-components.js';
import * as dat from 'three/examples/jsm/libs/lil-gui.module.min';
const container = document.getElementById('container');
const components = new OBC.Components();
components.scene = new OBC.SimpleScene(components);
const renderer = new OBC.PostproductionRenderer(components, container);
components.renderer = renderer;
renderer.postproduction.outlineColor = 0x999999;
const camera = new OBC.OrthoPerspectiveCamera(components);
components.camera = camera;
renderer.postproduction.setup(camera.controls);
components.raycaster = new OBC.SimpleRaycaster(components);
components.init();
const scene = components.scene.get();
const shadows = new OBC.ShadowDropper(components);
const directionalLight = new THREE.DirectionalLight();
directionalLight.position.set(5, 10, 3)
directionalLight.intensity = 0.5;
scene.add(directionalLight)
const ambientLight = new THREE.AmbientLight();
ambientLight.intensity = 0.5;
scene.add(ambientLight)
// Add some components
const grid = new OBC.SimpleGrid(components);
components.tools.add(grid);
// renderer.postproduction.excludedItems.add(grid.grid);
const clipper = new OBC.EdgesClipper(components, OBC.EdgesPlane);
components.tools.add(clipper);
clipper.enabled = true;
const dimensions = new OBC.SimpleDimensions(components);
components.tools.add(dimensions)
const createClippingPlane = (event) => {
if(event.code === "KeyP"){
const clipper = getClipper();
if (clipper){
clipper.create();
}
}
}
function getClipper() {
return components.tools.get("EdgesClipper");
}
window.addEventListener("keydown", createClippingPlane);
// Set up stats
const stats = new Stats();
stats.showPanel(2);
document.body.append(stats.dom);
stats.dom.style.right = 'auto';
components.renderer.beforeUpdate.on(() => stats.begin());
components.renderer.afterUpdate.on(() => stats.end());
const fragments = new OBC.Fragments(components);
// Set up fragment culler update
container.addEventListener("mouseup", () => fragments.culler.needsUpdate = true);
container.addEventListener("wheel", () => fragments.culler.needsUpdate = true);
// Bind postproduction update with fragment culler update
fragments.culler.viewUpdated.on(() => setTimeout(() => renderer.postproduction.update(), 300));
// Floor plan data
const floorNav = new OBC.PlanNavigator(clipper, camera);
let wasFloorplanActive = false;
const baseMaterial = new THREE.MeshBasicMaterial();
const backgroundColor = scene.background;
const whiteColor = new THREE.Color(0xffffff);
// Load fragments
await loadFragments();
async function loadFragments() {
const { entries } = await unzip('./small.zip');
const fileNames = Object.keys(entries);
const modelTypes = await entries['model-types.json'].json();
const allTypes = await entries['all-types.json'].json();
const thickTypes = ["IFCWALL", "IFCWALLSTANDARDCASE", "IFCSLAB", "IFCSTAIRFLIGHT", "IFCSTAIR", "IFCROOF", "IFCFOOTING",
"IFCCOLUMN", "IFCBEAM"];
const thickItems = [];
const thinItems = [];
for (let i = 0; i < fileNames.length; i++) {
const name = fileNames[i];
if (!name.includes('.glb')) continue;
// Load data
const geometryName = fileNames[i];
const geometry = await entries[geometryName].blob();
const geometryURL = URL.createObjectURL(geometry);
const dataName = geometryName.substring(0, geometryName.indexOf('.glb')) + '.json';
const dataBlob = await entries[dataName].blob();
const data = await entries[dataName].json();
const dataURL = URL.createObjectURL(dataBlob);
const fragment = await fragments.load(geometryURL, dataURL);
fragments.culler.needsUpdate = true;
const lines = fragments.edges.generate(fragment);
lines.removeFromParent();
const firstID = data.ids[0];
const categoryID = modelTypes[firstID];
const category = allTypes[categoryID];
if (thickTypes.includes(category)) thickItems.push(fragment.mesh);
else thinItems.push(fragment.mesh);
}
// Clipping edges
await clipper.styles.create('thick', thickItems, new LineMaterial({
color: 0x333333,
linewidth: 0.002,
}));
await clipper.styles.create('thin', thinItems, new LineMaterial({
color: 0x333333,
linewidth: 0.001,
}));
// Floor plans
const levelsProperties = await entries['levels-properties.json'].json();
const levelOffset = 1.5;
for (const levelProps of levelsProperties) {
const elevation = levelProps.SceneHeight + levelOffset;
// Create floorplan
await floorNav.create({
id: levelProps.expressID,
ortho: true,
normal: new THREE.Vector3(0, -1, 0),
point: new THREE.Vector3(0, elevation, 0),
data: { name: levelProps.Name.value },
rotation: 0
});
}
}
function toggleEdges(visible) {
const edges = Object.values(fragments.edges.edgesList);
for (const edge of edges) {
if (visible) scene.add(edge);
else edge.removeFromParent();
}
}
// Set up GUI
const gui = new dat.GUI();
const levelsFolder = gui.addFolder("Levels");
const helper = {};
// Go to floor plan mode
const floors = floorNav.get();
for (const id in floors) {
const name = floors[id].data.name;
helper[id] = async () => {
if (!wasFloorplanActive) {
toggleEdges(true);
scene.background = whiteColor;
}
fragments.materials.apply(baseMaterial);
fragments.culler.needsUpdate = true;
wasFloorplanActive = true;
grid.visible = false;
await floorNav.goTo(id);
}
levelsFolder.add(helper, id).name(name);
}
// Exit floor plan mode
helper["exit"] = async () => {
grid.visible = true;
fragments.materials.reset();
await floorNav.exitPlanView();
fragments.culler.needsUpdate = true;
wasFloorplanActive = false;
toggleEdges(false);
scene.background = backgroundColor;
}
levelsFolder.add(helper, "exit").name("Exit floor plans");
</script>
</body>
</html>