11import "./style.css" ; // Basic styling
2- import { BitByBitBase , Inputs , initBitbybit , type InitBitbybitOptions } from "@bitbybit-dev/threejs" ;
3- import { OrbitControls } from "three/examples/jsm/controls/OrbitControls" ;
2+ import { BitByBitBase , Inputs , initBitbybit , type InitBitbybitOptions , type OrbitCameraController } from "@bitbybit-dev/threejs" ;
43import {
54 Color ,
65 HemisphereLight ,
7- PerspectiveCamera ,
86 Scene ,
97 WebGLRenderer ,
108} from "three" ;
119
10+ // Store the orbit camera controller globally so bitbybit can access it
11+ let orbitCameraController : OrbitCameraController | null = null ;
12+
1213// --- 1. Main Application Entry Point ---
1314start ( ) ;
1415
1516async function start ( ) {
1617 // Initialize basic Three.js scene
17- const { scene } = initThreeJS ( ) ;
18+ const { scene, renderer } = initThreeJS ( ) ;
1819
1920 // Create an instance of BitByBitBase for Three.js
2021 const bitbybit = new BitByBitBase ( ) ;
@@ -31,6 +32,27 @@ async function start() {
3132 // Initialize BitByBit in a single call - workers are created from CDN automatically!
3233 await initBitbybit ( scene , bitbybit , options ) ;
3334
35+ // --- 2.5. Setup BitByBit Orbit Camera ---
36+ // Setup orbit camera controls using bitbybit library
37+ const cameraOptions = new Inputs . ThreeJSCamera . OrbitCameraDto ( ) ;
38+ cameraOptions . distance = 120 ;
39+ cameraOptions . pitch = 25 ;
40+ cameraOptions . yaw = 45 ;
41+ cameraOptions . frameOnStart = false ;
42+ cameraOptions . inertiaFactor = 0.1 ;
43+ cameraOptions . distanceSensitivity = 0.15 ;
44+ cameraOptions . domElement = renderer . domElement ;
45+ orbitCameraController = bitbybit . three . camera . orbitCamera . create ( cameraOptions ) ;
46+
47+ // Update the render loop to use the new camera
48+ const animate = ( ) => {
49+ if ( orbitCameraController ) {
50+ orbitCameraController . update ( 0.016 ) ; // ~60fps delta time
51+ renderer . render ( scene , orbitCameraController . camera ) ;
52+ }
53+ } ;
54+ renderer . setAnimationLoop ( animate ) ;
55+
3456 // --- 3. Create Geometry with Active Kernels ---
3557 if ( options . enableOCCT ) {
3658 await createOCCTGeometry ( bitbybit , "#ff0000" ) ; // Red
@@ -55,12 +77,6 @@ async function start() {
5577function initThreeJS ( ) {
5678 const domNode = document . getElementById ( "three-canvas" ) as HTMLCanvasElement ;
5779
58- const camera = new PerspectiveCamera (
59- 70 ,
60- window . innerWidth / window . innerHeight ,
61- 0.1 , // Adjusted near plane for typical scenes
62- 1000
63- ) ;
6480 const scene = new Scene ( ) ;
6581 scene . background = new Color ( 0x1a1c1f ) ; // Set background color
6682
@@ -73,29 +89,16 @@ function initThreeJS() {
7389 // Higher values = sharper but more GPU intensive. Use 1 for performance, devicePixelRatio for quality.
7490 renderer . setPixelRatio ( window . devicePixelRatio ) ;
7591
76- camera . position . set ( 50 , 50 , 100 ) ; // Adjusted camera position
77- camera . lookAt ( 0 , 0 , 0 ) ;
78-
79- const controls = new OrbitControls ( camera , renderer . domElement ) ;
80- controls . enableDamping = true ;
81- controls . dampingFactor = 0.05 ; // Smoother damping
82- controls . target . set ( 0 , 0 , 0 ) ; // Ensure controls target the origin
83- controls . update ( ) ; // Initial update
84-
8592 const onWindowResize = ( ) => {
86- camera . aspect = window . innerWidth / window . innerHeight ;
87- camera . updateProjectionMatrix ( ) ;
93+ if ( orbitCameraController ) {
94+ orbitCameraController . camera . aspect = window . innerWidth / window . innerHeight ;
95+ orbitCameraController . camera . updateProjectionMatrix ( ) ;
96+ }
8897 renderer . setSize ( window . innerWidth , window . innerHeight ) ;
8998 } ;
9099 window . addEventListener ( "resize" , onWindowResize , false ) ;
91100
92- const animate = ( ) => {
93- controls . update ( ) ; // Important for damping
94- renderer . render ( scene , camera ) ;
95- } ;
96- renderer . setAnimationLoop ( animate ) ;
97-
98- return { scene, camera, renderer } ; // Return renderer and camera if needed elsewhere
101+ return { scene, renderer } ; // Return renderer for camera setup
99102}
100103
101104// // --- 5. Bitbybit Kernel Initialization Logic ---
0 commit comments