-
-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathexample-external-gl-context.html
More file actions
378 lines (345 loc) · 10.1 KB
/
Copy pathexample-external-gl-context.html
File metadata and controls
378 lines (345 loc) · 10.1 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>three-cad-viewer + Pixi (shared WebGL context)</title>
<link rel="stylesheet" href="./dist/three-cad-viewer.css" />
<style>
body {
margin: 0;
font-family:
Inter,
system-ui,
-apple-system,
Segoe UI,
Roboto,
sans-serif;
background: #444444;
color: #e5e7eb;
}
.page {
max-width: 1400px;
margin: 0 auto;
padding: 16px;
}
h1 {
margin: 0 0 8px;
font-size: 20px;
}
p {
margin: 0 0 12px;
color: #cbd5e1;
}
#cad_view {
overflow: hidden;
}
.hint {
margin-top: 10px;
font-size: 13px;
color: #94a3b8;
}
code {
background: #0f172a;
color: #e2e8f0;
padding: 2px 5px;
border-radius: 4px;
}
</style>
<script src="./dist/three-cad-viewer.js"></script>
<script src="https://cdn.jsdelivr.net/npm/pixi.js@8/dist/pixi.min.js"></script>
</head>
<body>
<div class="page">
<h1>
Shared WebGL context demo: <code>three-cad-viewer</code> +
<code>pixi.js</code>
</h1>
<p>
This page creates one canvas/context, injects it into
<code>three-cad-viewer</code>, and renders a Pixi overlay in
<code>viewer.onAfterRender</code>.
</p>
<div id="cad_view"></div>
<div class="hint">
Move the CAD model with the mouse: the yellow Pixi panel is rendered on
top using the same GL context.
</div>
</div>
<script>
const { Viewer, Display } = CadViewer;
const { Container, Graphics, Text, WebGLRenderer } = PIXI;
(async () => {
const totalWidth = Math.min(1300, window.innerWidth - 32);
const cadWidth = Math.max(640, totalWidth - 260);
const treeWidth = totalWidth - cadWidth;
const cadHeight = 620;
// 1) Create and own the WebGL context ourselves.
const sharedCanvas = document.createElement("canvas");
const gl =
sharedCanvas.getContext("webgl2", {
alpha: true,
antialias: true,
stencil: true,
preserveDrawingBuffer: false,
}) ||
sharedCanvas.getContext("webgl", {
alpha: true,
antialias: true,
stencil: true,
preserveDrawingBuffer: false,
});
if (!gl) {
throw new Error("WebGL is not available in this browser.");
}
const displayOptions = {
cadWidth,
height: cadHeight,
treeWidth,
treeHeight: Math.floor(cadHeight * 0.72),
theme: "browser",
pinning: false,
glass: false,
tools: false,
measureTools: false,
measurementDebug: false,
selectTool: true,
explodeTool: true,
zscaleTool: false,
zebraTool: true,
keymap: {
shift: "shiftKey",
ctrl: "ctrlKey",
meta: "metaKey",
alt: "altKey",
},
// 2) Inject shared canvas + context into three-cad-viewer.
canvas: sharedCanvas,
gl,
};
const renderOptions = {
ambientIntensity: 1.0,
directIntensity: 1.1,
metalness: 0.25,
roughness: 0.65,
edgeColor: 0x707070,
defaultOpacity: 0.5,
normalLen: 0,
};
const viewerOptions = {
control: "orbit",
up: "Z",
ortho: true,
ticks: 5,
};
const container = document.getElementById("cad_view");
const display = new Display(container, displayOptions);
const viewer = new Viewer(display, displayOptions, () => {});
const shapes = {
version: 3,
name: "Scene",
id: "/Scene",
loc: [
[0, 0, 0],
[0, 0, 0, 1],
],
bb: {
xmin: -0.8,
xmax: 0.8,
ymin: -0.8,
ymax: 0.8,
zmin: -0.8,
zmax: 0.8,
},
parts: [
{
version: 3,
id: "/Scene/Box",
name: "Box",
type: "shapes",
subtype: "solid",
shape: makeBox(0, 0, 0, 1.6, 1.6, 1.6),
state: [1, 1],
color: "#5b9bd5",
alpha: 1,
texture: null,
loc: [
[0, 0, 0],
[0, 0, 0, 1],
],
renderback: false,
accuracy: null,
bb: null,
},
],
};
viewer.render(shapes, renderOptions, viewerOptions);
viewer.setView("iso", true);
// 3) Pixi renderer uses the very same WebGL context.
const pixiRenderer = new WebGLRenderer();
await pixiRenderer.init({
context: gl,
width: cadWidth,
height: cadHeight,
clearBeforeRender: false,
});
const stage = new Container();
const uiCard = new Graphics()
.roundRect(16, 16, 360, 64, 10)
.fill(0xffd84d);
const text = new Text({
text: "Pixi overlay (shared context)",
style: {
fill: "#0f172a",
fontFamily: "Arial",
fontSize: 20,
fontWeight: "700",
},
});
text.x = 28;
text.y = 36;
stage.addChild(uiCard);
stage.addChild(text);
// 4) Draw Pixi after each CAD render pass.
viewer.onAfterRender = () => {
uiCard.x = 16 + Math.sin(performance.now() * 0.0012) * 10;
text.x = 28 + Math.sin(performance.now() * 0.0012) * 10;
pixiRenderer.resetState();
pixiRenderer.render({ container: stage, clear: false });
};
// Keep frames flowing so the Pixi overlay animation is visible.
viewer.toggleAnimationLoop(true);
window.addEventListener("resize", () => {
const width = Math.min(1300, window.innerWidth - 32);
const nextCadWidth = Math.max(640, width - 260);
const nextTreeWidth = width - nextCadWidth;
viewer.resizeCadView(nextCadWidth, nextTreeWidth, cadHeight, false);
pixiRenderer.resize(nextCadWidth, cadHeight);
});
window.addEventListener("beforeunload", () => {
viewer.toggleAnimationLoop(false);
viewer.dispose();
});
function makeBox(cx, cy, cz, w, h, d) {
const hw = w / 2;
const hh = h / 2;
const hd = d / 2;
const faces = [
{
n: [-1, 0, 0],
v: [
[cx - hw, cy - hh, cz - hd],
[cx - hw, cy + hh, cz - hd],
[cx - hw, cy + hh, cz + hd],
[cx - hw, cy - hh, cz + hd],
],
},
{
n: [1, 0, 0],
v: [
[cx + hw, cy - hh, cz - hd],
[cx + hw, cy - hh, cz + hd],
[cx + hw, cy + hh, cz + hd],
[cx + hw, cy + hh, cz - hd],
],
},
{
n: [0, -1, 0],
v: [
[cx - hw, cy - hh, cz - hd],
[cx - hw, cy - hh, cz + hd],
[cx + hw, cy - hh, cz + hd],
[cx + hw, cy - hh, cz - hd],
],
},
{
n: [0, 1, 0],
v: [
[cx - hw, cy + hh, cz - hd],
[cx + hw, cy + hh, cz - hd],
[cx + hw, cy + hh, cz + hd],
[cx - hw, cy + hh, cz + hd],
],
},
{
n: [0, 0, -1],
v: [
[cx - hw, cy - hh, cz - hd],
[cx + hw, cy - hh, cz - hd],
[cx + hw, cy + hh, cz - hd],
[cx - hw, cy + hh, cz - hd],
],
},
{
n: [0, 0, 1],
v: [
[cx - hw, cy - hh, cz + hd],
[cx - hw, cy + hh, cz + hd],
[cx + hw, cy + hh, cz + hd],
[cx + hw, cy - hh, cz + hd],
],
},
];
const vertices = [];
const normals = [];
const triangles = [];
const face_types = [];
const triangles_per_face = [];
faces.forEach((face, i) => {
const base = i * 4;
face.v.forEach((point) => {
vertices.push(...point);
normals.push(...face.n);
});
triangles.push(base, base + 1, base + 2, base, base + 2, base + 3);
face_types.push(0);
triangles_per_face.push(2);
});
const corners = [];
for (const sx of [-1, 1]) {
for (const sy of [-1, 1]) {
for (const sz of [-1, 1]) {
corners.push([cx + sx * hw, cy + sy * hh, cz + sz * hd]);
}
}
}
const edgePairs = [
[0, 1],
[2, 3],
[4, 5],
[6, 7],
[0, 2],
[1, 3],
[4, 6],
[5, 7],
[0, 4],
[1, 5],
[2, 6],
[3, 7],
];
const edges = [];
const edge_types = [];
const segments_per_edge = [];
edgePairs.forEach(([a, b]) => {
edges.push(...corners[a], ...corners[b]);
edge_types.push(0);
segments_per_edge.push(1);
});
return {
vertices,
triangles,
normals,
edges,
obj_vertices: corners.flat(),
face_types,
edge_types,
triangles_per_face,
segments_per_edge,
};
}
})().catch((error) => {
console.error("Failed to initialize shared-context demo:", error);
});
</script>
</body>
</html>