Skip to content

Commit fc68fce

Browse files
simplifying bitbybit initialization process
1 parent 49bc50a commit fc68fce

16 files changed

Lines changed: 717 additions & 153 deletions

File tree

examples/vite/threejs/starter-template/src/main.ts

Lines changed: 134 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import "./style.css"; // Basic styling
2-
import { BitByBitBase, Inputs } from "@bitbybit-dev/threejs";
3-
import { OccStateEnum } from "@bitbybit-dev/occt-worker";
4-
import { JscadStateEnum } from "@bitbybit-dev/jscad-worker";
5-
import { ManifoldStateEnum } from "@bitbybit-dev/manifold-worker";
6-
2+
import { BitByBitBase, Inputs, initBitbybit, type InitBitbybitOptions } from "@bitbybit-dev/threejs";
73
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
84
import {
95
Color,
@@ -12,14 +8,6 @@ import {
128
Scene,
139
WebGLRenderer,
1410
} from "three";
15-
import { first, firstValueFrom, map } from "rxjs";
16-
17-
// Define an interface for kernel options
18-
interface KernelOptions {
19-
enableOCCT: boolean;
20-
enableJSCAD: boolean;
21-
enableManifold: boolean;
22-
}
2311

2412
// --- 1. Main Application Entry Point ---
2513
start();
@@ -31,32 +19,34 @@ async function start() {
3119
// Create an instance of BitByBitBase for Three.js
3220
const bitbybit = new BitByBitBase();
3321

34-
// --- 2. Configure and Initialize Kernels ---
35-
// Users can control which kernels are loaded
36-
const kernelOptions: KernelOptions = {
22+
// --- 2. Configure and Initialize BitByBit ---
23+
// Single options object for both workers and kernels
24+
const options: InitBitbybitOptions = {
3725
enableOCCT: true,
3826
enableJSCAD: true,
3927
enableManifold: true,
28+
// loadFonts: ["roboto"], // Optional: specify fonts to load, or omit to skip font loading
4029
};
41-
// Initialize Bitbybit with the selected kernels
42-
await initWithKernels(scene, bitbybit, kernelOptions);
30+
31+
// Initialize BitByBit in a single call - workers are created from CDN automatically!
32+
await initBitbybit(scene, bitbybit, options);
4333

4434
// --- 3. Create Geometry with Active Kernels ---
45-
if (kernelOptions.enableOCCT) {
35+
if (options.enableOCCT) {
4636
await createOCCTGeometry(bitbybit, "#ff0000"); // Red
4737
}
48-
if (kernelOptions.enableManifold) {
38+
if (options.enableManifold) {
4939
await createManifoldGeometry(bitbybit, "#00ff00"); // Green
5040
}
51-
if (kernelOptions.enableJSCAD) {
41+
if (options.enableJSCAD) {
5242
await createJSCADGeometry(bitbybit, "#0000ff"); // Blue
5343
}
5444

5545
// --- 4. Create Drawing Examples (Lines, Points, Curves, etc.) ---
5646
await createDrawingExamples(bitbybit);
5747

5848
// --- 5. Create Textured OCCT Cube Example ---
59-
if (kernelOptions.enableOCCT) {
49+
if (options.enableOCCT) {
6050
await createTexturedOCCTCube(bitbybit);
6151
}
6252
}
@@ -108,126 +98,126 @@ function initThreeJS() {
10898
return { scene, camera, renderer }; // Return renderer and camera if needed elsewhere
10999
}
110100

111-
// --- 5. Bitbybit Kernel Initialization Logic ---
112-
async function initWithKernels(
113-
scene: Scene,
114-
bitbybit: BitByBitBase,
115-
options: KernelOptions
116-
): Promise<{ message: string; initializedKernels: string[] }> {
117-
let occtWorkerInstance: Worker | undefined;
118-
let jscadWorkerInstance: Worker | undefined;
119-
let manifoldWorkerInstance: Worker | undefined;
120-
121-
// 1. Conditionally create worker instances
122-
if (options.enableOCCT) {
123-
occtWorkerInstance = new Worker(
124-
new URL("./workers/occt.worker.ts", import.meta.url),
125-
{ name: "OCC_WORKER", type: "module" }
126-
);
127-
}
128-
if (options.enableJSCAD) {
129-
jscadWorkerInstance = new Worker(
130-
new URL("./workers/jscad.worker.ts", import.meta.url),
131-
{ name: "JSCAD_WORKER", type: "module" }
132-
);
133-
}
134-
if (options.enableManifold) {
135-
manifoldWorkerInstance = new Worker(
136-
new URL("./workers/manifold.worker.ts", import.meta.url),
137-
{ name: "MANIFOLD_WORKER", type: "module" }
138-
);
139-
}
140-
141-
// 2. Initialize Bitbybit
142-
await bitbybit.init(
143-
scene,
144-
occtWorkerInstance,
145-
jscadWorkerInstance,
146-
manifoldWorkerInstance
147-
);
148-
149-
// 3. Collect promises for kernel initializations
150-
const initializationPromises: Promise<string>[] = [];
151-
let anyKernelSelectedForInit = false;
152-
153-
if (options.enableOCCT) {
154-
anyKernelSelectedForInit = true;
155-
if (bitbybit.occtWorkerManager) {
156-
initializationPromises.push(
157-
firstValueFrom(
158-
bitbybit.occtWorkerManager.occWorkerState$.pipe(
159-
first((s) => s.state === OccStateEnum.initialised),
160-
map(() => "OCCT")
161-
)
162-
)
163-
);
164-
} else {
165-
console.warn(
166-
"OCCT enabled in options, but occtWorkerManager not found after init."
167-
);
168-
}
169-
}
170-
171-
if (options.enableJSCAD) {
172-
anyKernelSelectedForInit = true;
173-
if (bitbybit.jscadWorkerManager) {
174-
initializationPromises.push(
175-
firstValueFrom(
176-
bitbybit.jscadWorkerManager.jscadWorkerState$.pipe(
177-
first((s) => s.state === JscadStateEnum.initialised),
178-
map(() => "JSCAD")
179-
)
180-
)
181-
);
182-
} else {
183-
console.warn(
184-
"JSCAD enabled in options, but jscadWorkerManager not found after init."
185-
);
186-
}
187-
}
188-
189-
if (options.enableManifold) {
190-
anyKernelSelectedForInit = true;
191-
if (bitbybit.manifoldWorkerManager && bitbybit.manifoldWorkerManager.manifoldWorkerState$) {
192-
initializationPromises.push(
193-
firstValueFrom(
194-
bitbybit.manifoldWorkerManager.manifoldWorkerState$.pipe(
195-
first((s) => s.state === ManifoldStateEnum.initialised),
196-
map(() => "Manifold")
197-
)
198-
)
199-
);
200-
} else {
201-
console.warn(
202-
"Manifold enabled in options, but manifoldWorkerManager not found after init."
203-
);
204-
}
205-
}
206-
207-
// 4. Wait for selected & available kernels or handle no selection/availability
208-
if (!anyKernelSelectedForInit) {
209-
console.log("No kernels selected for initialization.");
210-
return { message: "No kernels selected for initialization.", initializedKernels: [] };
211-
}
212-
213-
if (initializationPromises.length === 0) {
214-
// Kernels were selected, but none were awaitable (e.g., managers missing for all selected)
215-
console.log(
216-
"Kernels were selected, but none had managers available for awaiting initialization."
217-
);
218-
return {
219-
message: "Selected kernels were not awaitable for initialization state.",
220-
initializedKernels: [],
221-
};
222-
}
223-
224-
const initializedKernels = await Promise.all(initializationPromises);
225-
console.log("Kernels initialized:", initializedKernels.join(", "));
226-
return {
227-
message: `Successfully initialized: ${initializedKernels.join(", ")}`,
228-
initializedKernels,
229-
};
230-
}
101+
// // --- 5. Bitbybit Kernel Initialization Logic ---
102+
// async function initWithKernels(
103+
// scene: Scene,
104+
// bitbybit: BitByBitBase,
105+
// options: KernelOptions
106+
// ): Promise<{ message: string; initializedKernels: string[] }> {
107+
// let occtWorkerInstance: Worker | undefined;
108+
// let jscadWorkerInstance: Worker | undefined;
109+
// let manifoldWorkerInstance: Worker | undefined;
110+
111+
// // 1. Conditionally create worker instances
112+
// if (options.enableOCCT) {
113+
// occtWorkerInstance = new Worker(
114+
// new URL("./workers/occt.worker.ts", import.meta.url),
115+
// { name: "OCC_WORKER", type: "module" }
116+
// );
117+
// }
118+
// if (options.enableJSCAD) {
119+
// jscadWorkerInstance = new Worker(
120+
// new URL("./workers/jscad.worker.ts", import.meta.url),
121+
// { name: "JSCAD_WORKER", type: "module" }
122+
// );
123+
// }
124+
// if (options.enableManifold) {
125+
// manifoldWorkerInstance = new Worker(
126+
// new URL("./workers/manifold.worker.ts", import.meta.url),
127+
// { name: "MANIFOLD_WORKER", type: "module" }
128+
// );
129+
// }
130+
131+
// // 2. Initialize Bitbybit
132+
// await bitbybit.init(
133+
// scene,
134+
// occtWorkerInstance,
135+
// jscadWorkerInstance,
136+
// manifoldWorkerInstance
137+
// );
138+
139+
// // 3. Collect promises for kernel initializations
140+
// const initializationPromises: Promise<string>[] = [];
141+
// let anyKernelSelectedForInit = false;
142+
143+
// if (options.enableOCCT) {
144+
// anyKernelSelectedForInit = true;
145+
// if (bitbybit.occtWorkerManager) {
146+
// initializationPromises.push(
147+
// firstValueFrom(
148+
// bitbybit.occtWorkerManager.occWorkerState$.pipe(
149+
// first((s) => s.state === OccStateEnum.initialised),
150+
// map(() => "OCCT")
151+
// )
152+
// )
153+
// );
154+
// } else {
155+
// console.warn(
156+
// "OCCT enabled in options, but occtWorkerManager not found after init."
157+
// );
158+
// }
159+
// }
160+
161+
// if (options.enableJSCAD) {
162+
// anyKernelSelectedForInit = true;
163+
// if (bitbybit.jscadWorkerManager) {
164+
// initializationPromises.push(
165+
// firstValueFrom(
166+
// bitbybit.jscadWorkerManager.jscadWorkerState$.pipe(
167+
// first((s) => s.state === JscadStateEnum.initialised),
168+
// map(() => "JSCAD")
169+
// )
170+
// )
171+
// );
172+
// } else {
173+
// console.warn(
174+
// "JSCAD enabled in options, but jscadWorkerManager not found after init."
175+
// );
176+
// }
177+
// }
178+
179+
// if (options.enableManifold) {
180+
// anyKernelSelectedForInit = true;
181+
// if (bitbybit.manifoldWorkerManager && bitbybit.manifoldWorkerManager.manifoldWorkerState$) {
182+
// initializationPromises.push(
183+
// firstValueFrom(
184+
// bitbybit.manifoldWorkerManager.manifoldWorkerState$.pipe(
185+
// first((s) => s.state === ManifoldStateEnum.initialised),
186+
// map(() => "Manifold")
187+
// )
188+
// )
189+
// );
190+
// } else {
191+
// console.warn(
192+
// "Manifold enabled in options, but manifoldWorkerManager not found after init."
193+
// );
194+
// }
195+
// }
196+
197+
// // 4. Wait for selected & available kernels or handle no selection/availability
198+
// if (!anyKernelSelectedForInit) {
199+
// console.log("No kernels selected for initialization.");
200+
// return { message: "No kernels selected for initialization.", initializedKernels: [] };
201+
// }
202+
203+
// if (initializationPromises.length === 0) {
204+
// // Kernels were selected, but none were awaitable (e.g., managers missing for all selected)
205+
// console.log(
206+
// "Kernels were selected, but none had managers available for awaiting initialization."
207+
// );
208+
// return {
209+
// message: "Selected kernels were not awaitable for initialization state.",
210+
// initializedKernels: [],
211+
// };
212+
// }
213+
214+
// const initializedKernels = await Promise.all(initializationPromises);
215+
// console.log("Kernels initialized:", initializedKernels.join(", "));
216+
// return {
217+
// message: `Successfully initialized: ${initializedKernels.join(", ")}`,
218+
// initializedKernels,
219+
// };
220+
// }
231221

232222
// --- 6. Geometry Creation Functions (Examples) ---
233223
async function createOCCTGeometry(bitbybit: BitByBitBase, color: string) {
@@ -450,8 +440,8 @@ async function createDrawingExamples(bitbybit: BitByBitBase) {
450440
[
451441
[60, -20, -5],
452442
[70, -20, 5],
453-
] ,
454-
]as Inputs.Base.Segment3[];
443+
],
444+
] as Inputs.Base.Segment3[];
455445
const segmentsDrawOptions = new Inputs.Draw.DrawBasicGeometryOptions();
456446
segmentsDrawOptions.colours = ["#ffff00", "#ff00ff", "#00ffff"]; // Yellow, Magenta, Cyan
457447
segmentsDrawOptions.size = 2.5;

packages/dev/babylonjs/lib/api/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ export * from "./bitbybit";
22
export * from "./context";
33
export * from "./draw-helper";
44
export * from "./bitbybit-base";
5+
export * from "./init-kernels";
6+
export * from "./worker-urls";
57
export * as Inputs from "./inputs";

0 commit comments

Comments
 (0)