Skip to content

Commit b4bc318

Browse files
setup of pattern example
1 parent 51f2512 commit b4bc318

File tree

2 files changed

+166
-30
lines changed

2 files changed

+166
-30
lines changed

webpack/threejs/src/code/patterns.ts

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@ import { GUI } from 'lil-gui';
99

1010
function component() {
1111

12+
const showSpinner = () => {
13+
const element = document.createElement('div');
14+
element.id = "spinner";
15+
element.className = "lds-ellipsis";
16+
element.innerHTML = `
17+
<div></div>
18+
<div></div>
19+
<div></div>
20+
`;
21+
22+
document.body.appendChild(
23+
element
24+
);
25+
}
26+
27+
const hideSpinner = () => {
28+
const el = document.getElementById('spinner');
29+
if (el) {
30+
el.remove();
31+
}
32+
}
33+
1234
let current: { group: Group | undefined, ground: Mesh | undefined, gui: GUI | undefined } = {
1335
group: undefined,
1436
ground: undefined,
@@ -24,8 +46,6 @@ function component() {
2446
color: string,
2547
downloadSTL?: () => void,
2648
downloadStep?: () => void
27-
goHome?: () => void
28-
goBitbybit?: () => void
2949
}
3050

3151
const model = {
@@ -75,13 +95,12 @@ function component() {
7595
subdivideOptions.offsetFromBorderV = 0.01;
7696

7797
const withHoles = await bitbybit.occt.shapes.face.subdivideToRectangleHoles(subdivideOptions);
78-
const finalShape = await bitbybit.occt.operations.makeThickSolidSimple({ shape: withHoles[0], offset: 0.5 });
79-
// const box = await bitbybit.occt.shapes.solid.createBox({ width: 1, height: 10, length: 1, center: [0, 0, 0] });
98+
finalShape = await bitbybit.occt.operations.makeThickSolidSimple({ shape: withHoles[0], offset: 0.5 });
8099

81100
shapesToClean = [...wires, loft, translated, ...faces, ...withHoles, finalShape];
82101

83102
const options = new Inputs.Draw.DrawOcctShapeOptions();
84-
options.precision = 0.05;
103+
options.precision = 0.02;
85104
options.drawEdges = model.drawEdges;
86105
options.drawFaces = model.drawFaces;
87106
options.drawVertices = false;
@@ -101,15 +120,14 @@ function component() {
101120

102121
current.group = group;
103122

104-
finalShape;
105123
}
106124
}
107125

108126
const downloadStep = async () => {
109127
if (bitbybit && finalShape) {
110128
await bitbybit.occt.io.saveShapeSTEP({
111129
shape: finalShape,
112-
fileName: 'vase.stp',
130+
fileName: 'shape.stp',
113131
adjustYtoZ: true,
114132
tryDownload: true
115133
});
@@ -134,23 +152,26 @@ function component() {
134152
model.downloadStep = downloadStep;
135153

136154
const updateShape = async () => {
137-
toggleInputs();
155+
showSpinner();
156+
disableGUI();
138157
current.group?.traverse((obj) => {
139158
scene?.remove(obj);
140159
});
141160
await createShape(bitbybit, scene);
142-
toggleInputs();
161+
enableGUI();
162+
hideSpinner();
143163
}
144164

145165
const init = async () => {
166+
showSpinner();
146167
bitbybit = new BitByBitBase();
147168

148169
const domNode = document.getElementById('three-canvas') as HTMLCanvasElement;
149170
const occt = new Worker(new URL('../occ.worker', import.meta.url), { name: 'OCC', type: 'module' });
150171

151172
const camera = new PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 1000);
152173
scene = new Scene();
153-
scene.fog = new Fog(0xffffff, 15, 60);
174+
scene.fog = new Fog(0x1a1c1f, 15, 60);
154175
const light = new HemisphereLight(0xffffff, 0x000000, 10);
155176
scene.add(light);
156177
await bitbybit.init(scene, occt, undefined);
@@ -159,16 +180,15 @@ function component() {
159180
camera.aspect = window.innerWidth / window.innerHeight;
160181
camera.updateProjectionMatrix();
161182
renderer.setSize(window.innerWidth, window.innerHeight);
162-
// renderer.setSize(domNode.clientWidth, domNode.clientHeight);
163-
183+
renderer.setPixelRatio(window.devicePixelRatio / 1.5)
164184
const animation = (time: number) => {
165185
renderer.render(scene, camera);
166186
rotateGroup();
167187
controls.update();
168188
}
169189

170190
const controls = new OrbitControls(camera, renderer.domElement);
171-
camera.position.set(10, 10, 30);
191+
camera.position.set(10, 10, 20);
172192

173193
controls.update();
174194
controls.target = new Vector3(0, 5, 0);
@@ -186,7 +206,7 @@ function component() {
186206
}
187207
window.addEventListener("resize", onWindowResize, false);
188208

189-
renderer.setClearColor(new Color(0xffffff), 1);
209+
renderer.setClearColor(new Color(0x1a1c1f), 1);
190210

191211
bitbybit.occtWorkerManager.occWorkerState$.subscribe(async s => {
192212
if (s.state === OccStateEnum.initialised) {
@@ -222,30 +242,28 @@ function component() {
222242
scene.add(ground);
223243

224244
createGui();
245+
hideSpinner();
225246
}
226247
});
227248
}
228249

229-
const toggleInputs = () => {
230-
const allInputs = document.getElementsByTagName('input');
231-
for (let i = 0; i < allInputs.length; i++) {
232-
allInputs[i].disabled = !allInputs[i].disabled;
233-
}
250+
const disableGUI = () => {
251+
const lilGui = document.getElementsByClassName('lil-gui')[0] as HTMLElement;
252+
lilGui.style.pointerEvents = "none";
253+
lilGui.style.opacity = "0.5";
254+
}
255+
256+
const enableGUI = () => {
257+
const lilGui = document.getElementsByClassName('lil-gui')[0] as HTMLElement;
258+
lilGui.style.pointerEvents = "all";
259+
lilGui.style.opacity = "1";
234260
}
235261

236262
const createGui = () => {
237263

238264
const gui = new GUI();
239265
current.gui = gui;
240266
gui.$title.innerHTML = "Pattern";
241-
model.goHome = () => {
242-
window.location.href = "/";
243-
gui.destroy();
244-
};
245-
model.goBitbybit = () => {
246-
window.location.href = "https://bitbybit.dev";
247-
gui.destroy();
248-
};
249267

250268
gui
251269
.add(model, "uRec", 4, 32, 4)
@@ -301,12 +319,10 @@ function component() {
301319

302320
gui.add(model, "downloadSTL").name("Download STL");
303321
gui.add(model, "downloadStep").name("Download STEP");
304-
gui.add(model, "goHome").name("Examples");
305-
gui.add(model, "goBitbybit").name("Bitbybit.dev");
306322
}
307323

308324
init();
309-
325+
310326
}
311327

312328
component();

webpack/threejs/src/html/patterns/index.html

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,129 @@
66
<title>Patterns</title>
77
<meta name="viewport" content="width=device-width,initial-scale=1">
88
<script defer="defer" src="../patterns.0.18.0-alpha.4.bundle.js"></script>
9+
<style>
10+
body {
11+
background-color: rgb(26, 28, 31);
12+
margin: 0px;
13+
color: #ffffff;
14+
}
15+
16+
a {
17+
color: #f0cebb;
18+
}
19+
20+
a:visited {
21+
color: #f0cebb;
22+
;
23+
}
24+
25+
#three-canvas {
26+
display: block;
27+
overflow-y: hidden;
28+
overflow-x: hidden;
29+
width: 100%;
30+
height: 100%;
31+
}
32+
33+
.logo {
34+
padding: 20px;
35+
background-color: rgb(26, 28, 31, 0.8);
36+
font-family: Arial, Helvetica, sans-serif;
37+
font-size: 12px;
38+
position: absolute;
39+
bottom: 10px;
40+
left: 10px;
41+
}
42+
43+
.logo img {
44+
margin-bottom: 5px;
45+
}
46+
47+
.lds-ellipsis {
48+
z-index: 2;
49+
top: calc(50% - 32px);
50+
left: calc(50% - 32px);
51+
display: inline-block;
52+
position: absolute;
53+
width: 64px;
54+
height: 64px;
55+
}
56+
57+
.lds-ellipsis div {
58+
position: absolute;
59+
top: 27px;
60+
width: 11px;
61+
height: 11px;
62+
border-radius: 50%;
63+
background: rgba(255, 255, 255, 0.8);
64+
animation-timing-function: cubic-bezier(0, 1, 1, 0);
65+
}
66+
67+
.lds-ellipsis div:nth-child(1) {
68+
left: 6px;
69+
animation: lds-ellipsis1 0.6s infinite;
70+
}
71+
72+
.lds-ellipsis div:nth-child(2) {
73+
left: 6px;
74+
animation: lds-ellipsis2 0.6s infinite;
75+
}
76+
77+
.lds-ellipsis div:nth-child(3) {
78+
left: 26px;
79+
animation: lds-ellipsis2 0.6s infinite;
80+
}
81+
82+
.lds-ellipsis div:nth-child(4) {
83+
left: 45px;
84+
animation: lds-ellipsis3 0.6s infinite;
85+
}
86+
87+
@keyframes lds-ellipsis1 {
88+
0% {
89+
transform: scale(0);
90+
}
91+
92+
100% {
93+
transform: scale(1);
94+
}
95+
}
96+
97+
@keyframes lds-ellipsis3 {
98+
0% {
99+
transform: scale(1);
100+
}
101+
102+
100% {
103+
transform: scale(0);
104+
}
105+
}
106+
107+
@keyframes lds-ellipsis2 {
108+
0% {
109+
transform: translate(0, 0);
110+
}
111+
112+
100% {
113+
transform: translate(19px, 0);
114+
}
115+
}
116+
</style>
9117
</head>
10118

11119
<body>
120+
<div class="logo">
121+
<a href="/">
122+
<img src="https://bitbybit.dev/assets/logo-gold-small.png" width="50px" height="50px" />
123+
</a>
124+
<div>Made with <a href="https://bitbybit.dev">bitbybit.dev</a> & <a href="https://threejs.org">threejs.org</a>
125+
</div>
126+
<div>Source code on <a
127+
href="https://github.com/bitbybit-dev/app-examples/blob/main/webpack/threejs/src/code/patterns.ts">github</a>
128+
</div>
129+
<div>Npm package <a href="https://www.npmjs.com/package/@bitbybit-dev/threejs">bitbybit-dev/threejs</a>
130+
</div>
131+
</div>
12132
<canvas id="three-canvas">
13133
</canvas>
14134
</body>

0 commit comments

Comments
 (0)