-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: clean camera + support pointer lock API
- Loading branch information
1 parent
b244883
commit 2999abd
Showing
8 changed files
with
129 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { PointerLockControls } from 'three/addons/controls/PointerLockControls.js' | ||
import type { FComponent3d } from '../FComponent3d' | ||
import { FCamera3d } from './FCamera3d' | ||
|
||
/** | ||
* @description A camera that can be attached to a FComponent3d and orbits around it. | ||
* @category Camera | ||
* @example | ||
* ```ts | ||
* import { FScene3d, FPointerLockCamera, FCube } from '@fibbojs/3d' | ||
* | ||
* const scene = new FScene3d() | ||
* | ||
* const cube = new FCube(scene) | ||
* scene.addComponent(cube) | ||
* | ||
* scene.camera = new FPointerLockCamera(cube) | ||
* ``` | ||
*/ | ||
export class FPointerLockCamera extends FCamera3d { | ||
// Model that the camera is attached to | ||
attachedComponent: FComponent3d | ||
// Pointer Lock controls | ||
controls: PointerLockControls | ||
|
||
/** | ||
* @param attachedComponent Component that the camera is attached to | ||
*/ | ||
constructor(attachedComponent: FComponent3d) { | ||
super() | ||
this.attachedComponent = attachedComponent | ||
|
||
// Create Pointer Lock controls | ||
this.controls = new PointerLockControls(this, attachedComponent.scene.renderer.domElement) | ||
|
||
attachedComponent.scene.scene.add(this.controls.getObject()) | ||
|
||
// Lock controls when clicking on the renderer | ||
attachedComponent.scene.renderer.domElement.addEventListener('click', () => { | ||
this.controls.lock() | ||
}) | ||
} | ||
|
||
onFrame(_delta: number): void { | ||
if (this.attachedComponent.mesh === undefined) | ||
return | ||
|
||
// Position the camera at the model's position + offset | ||
this.position.x = this.attachedComponent.mesh.position.x | ||
this.position.y = this.attachedComponent.mesh.position.y | ||
this.position.z = this.attachedComponent.mesh.position.z | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters