Skip to content

Commit 075c0c9

Browse files
committed
feat: clean FCamera options in 3d
1 parent d983387 commit 075c0c9

File tree

10 files changed

+66
-32
lines changed

10 files changed

+66
-32
lines changed

apps/playground-3d/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ import MyCustomCube from './classes/MyCustomCube'
124124
scene.addComponent(character)
125125

126126
// Attach a camera to the character
127-
scene.camera = new FGameCamera(character)
127+
scene.camera = new FGameCamera({ target: character })
128128

129129
/**
130130
* Create other objects

docs/getting-started/introduction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ Moreover, we would really appreciate your feedback, so if you have any suggestio
3333

3434
We are doing our best to make a great tool so developers can create amazing experiences, and your feedback is really important to us.
3535

36-
Ready to take off ? You can go the next section to learn how to get started with Fibbo.
36+
Ready to take off ? You can go to the next section to learn how to get started with Fibbo.
3737

3838
Have fun !

packages/2d/src/cameras/FCamera.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { FCamera as FCameraCore } from '@fibbojs/core'
2-
import type * as PIXI from 'pixi.js'
32
import type { FScene } from '../FScene'
43

54
export interface FCameraOptions {
6-
position?: PIXI.PointData
5+
position?: { x: number, y: number }
76
}
87

98
/**
@@ -21,11 +20,11 @@ export abstract class FCamera extends FCameraCore {
2120
*/
2221
scene: FScene
2322

24-
// Transform
23+
// FTransform isn't used as a 2D camera doesn't need scale or rotation
2524
/**
2625
* Position of the camera.
2726
*/
28-
position: PIXI.PointData
27+
position: { x: number, y: number }
2928

3029
/**
3130
* @description Create a new 2D camera.

packages/3d/src/FScene.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as THREE from 'three'
2-
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'
32
import { FScene as FSceneCore } from '@fibbojs/core'
43
import type RAPIER from '@dimforge/rapier3d'
54
import type { FComponent } from './FComponent'
@@ -60,7 +59,6 @@ export class FScene extends FSceneCore {
6059
declare scene: THREE.Scene
6160
declare renderer: THREE.WebGLRenderer
6261
declare camera: FCamera
63-
declare controls?: OrbitControls
6462
// Rapier
6563
gravity: { x: number, y: number, z: number }
6664
declare world: RAPIER.World
@@ -107,9 +105,6 @@ export class FScene extends FSceneCore {
107105
this.renderer.setSize((window as any).innerWidth, (window as any).innerHeight)
108106
// Create a default camera
109107
this.camera = new FFixedCamera()
110-
this.camera.position.set(5, 5, 5)
111-
// Orbit controls for the camera
112-
this.controls = new OrbitControls(this.camera, this.renderer.domElement)
113108

114109
// Add renderer to DOM
115110
this.__DOM_NODE__.appendChild(this.renderer.domElement)

packages/3d/src/cameras/FAttachedCamera.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import * as THREE from 'three'
22
import type { FComponent } from '../FComponent'
3+
import type { FCameraOptions } from './FCamera'
34
import { FCamera } from './FCamera'
45

6+
export interface FAttachedCameraOptions extends FCameraOptions {
7+
target: FComponent
8+
}
9+
510
/**
611
* @description A camera that can be attached to a FComponent.
712
* @category Camera

packages/3d/src/cameras/FCamera.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import * as THREE from 'three'
22
import type { FCamera as FCameraCore } from '@fibbojs/core'
3+
import type { FTransformOptions } from '../FTransform'
4+
import { FTransform } from '../FTransform'
5+
6+
export interface FCameraOptions extends FTransformOptions {}
37

48
/**
59
* @description The base class for cameras in Fibbo.
@@ -15,8 +19,36 @@ export abstract class FCamera extends THREE.PerspectiveCamera implements FCamera
1519
declare public __ID__: number
1620
public __CALLBACKS_ON_COLLISION__: { [key: string]: (() => void)[] } = {}
1721

18-
constructor() {
22+
/**
23+
* Transform of the camera.
24+
*/
25+
transform: FTransform
26+
27+
constructor(options?: FCameraOptions) {
1928
super(75, window.innerWidth / window.innerHeight, 0.1, 1000)
29+
30+
// Define default options
31+
const DEFAULT_OPTIONS = {
32+
position: { x: 5, y: 5, z: 5 },
33+
rotation: { x: 0, y: 0.7853981634, z: 0 },
34+
}
35+
// Apply default options
36+
options = { ...DEFAULT_OPTIONS, ...options }
37+
// Validate options
38+
if (!options.position || (!options.rotation && !options.rotationDegree))
39+
throw new Error('FibboError: FCamera requires position and rotation options')
40+
41+
// Store transform
42+
this.transform = new FTransform({
43+
position: options.position,
44+
rotation: options.rotation,
45+
rotationDegree: options.rotationDegree,
46+
})
47+
48+
// Set the position
49+
this.position.set(this.transform.position.x, this.transform.position.y, this.transform.position.z)
50+
// Set the rotation
51+
this.rotation.set(this.transform.rotation.x, this.transform.rotation.y, this.transform.rotation.z)
2052
}
2153

2254
abstract onFrame(_delta: number): void

packages/3d/src/cameras/FFixedCamera.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { FCameraOptions } from './FCamera'
12
import { FCamera } from './FCamera'
23

34
/**
@@ -15,8 +16,8 @@ import { FCamera } from './FCamera'
1516
* ```
1617
*/
1718
export class FFixedCamera extends FCamera {
18-
constructor() {
19-
super()
19+
constructor(options?: FCameraOptions) {
20+
super(options)
2021
}
2122

2223
onFrame(_delta: number): void {

packages/3d/src/cameras/FGameCamera.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type * as THREE from 'three'
2-
import type { FComponent } from '../FComponent'
32
import { FOrbitCamera } from './FOrbitCamera'
3+
import type { FAttachedCameraOptions } from './FAttachedCamera'
44

55
/**
66
* @description A camera that can be attached to a FComponent and orbits around it in a more game-like way.
@@ -26,11 +26,11 @@ export class FGameCamera extends FOrbitCamera {
2626
lastMouseMoveEvent: MouseEvent | undefined
2727

2828
/**
29-
* @param attachedComponent Component that the camera is attached to
29+
* @param options Options for the camera.
3030
*/
31-
constructor(attachedComponent: FComponent) {
32-
super(attachedComponent)
33-
this.previousModelPosition = attachedComponent.transform.position.clone()
31+
constructor(options: FAttachedCameraOptions) {
32+
super(options)
33+
this.previousModelPosition = options.target.transform.position.clone()
3434
this.setPosition(0, 5, 5)
3535

3636
this.controls.enableDamping = true
@@ -40,7 +40,7 @@ export class FGameCamera extends FOrbitCamera {
4040
* Add event listeners
4141
*/
4242
// Request pointer lock when the canvas is clicked
43-
attachedComponent.scene.renderer.domElement.addEventListener('click', () => {
43+
this.attachedComponent.scene.renderer.domElement.addEventListener('click', () => {
4444
this.attachedComponent.scene.renderer.domElement.requestPointerLock()
4545
})
4646
// Update the pointer lock flag when the pointer lock state changes

packages/3d/src/cameras/FOrbitCamera.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'
22
import type { FComponent } from '../FComponent'
33
import { FCamera } from './FCamera'
4+
import type { FAttachedCameraOptions } from './FAttachedCamera'
45

56
/**
67
* @description A camera that can be attached to a FComponent and orbits around it.
@@ -24,14 +25,14 @@ export class FOrbitCamera extends FCamera {
2425
controls: OrbitControls
2526

2627
/**
27-
* @param attachedComponent Component that the camera is attached to
28+
* @param options Options for the camera.
2829
*/
29-
constructor(attachedComponent: FComponent) {
30-
super()
31-
this.attachedComponent = attachedComponent
30+
constructor(options: FAttachedCameraOptions) {
31+
super(options)
32+
this.attachedComponent = options.target
3233

3334
// Create orbit controls
34-
this.controls = new OrbitControls(this, attachedComponent.scene.renderer.domElement)
35+
this.controls = new OrbitControls(this, this.attachedComponent.scene.renderer.domElement)
3536
}
3637

3738
onFrame(_delta: number): void {

packages/3d/src/cameras/FPointerLockCamera.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { PointerLockControls } from 'three/addons/controls/PointerLockControls.js'
22
import type { FComponent } from '../FComponent'
33
import { FCamera } from './FCamera'
4+
import type { FAttachedCameraOptions } from './FAttachedCamera'
45

56
/**
67
* @description A camera that can be attached to a FComponent and orbits around it.
@@ -24,19 +25,19 @@ export class FPointerLockCamera extends FCamera {
2425
controls: PointerLockControls
2526

2627
/**
27-
* @param attachedComponent Component that the camera is attached to
28+
* @param options Options for the camera.
2829
*/
29-
constructor(attachedComponent: FComponent) {
30-
super()
31-
this.attachedComponent = attachedComponent
30+
constructor(options: FAttachedCameraOptions) {
31+
super(options)
32+
this.attachedComponent = options.target
3233

3334
// Create Pointer Lock controls
34-
this.controls = new PointerLockControls(this, attachedComponent.scene.renderer.domElement)
35+
this.controls = new PointerLockControls(this, this.attachedComponent.scene.renderer.domElement)
3536

36-
attachedComponent.scene.scene.add(this.controls.getObject())
37+
this.attachedComponent.scene.scene.add(this.controls.getObject())
3738

3839
// Lock controls when clicking on the renderer
39-
attachedComponent.scene.renderer.domElement.addEventListener('click', () => {
40+
this.attachedComponent.scene.renderer.domElement.addEventListener('click', () => {
4041
this.controls.lock()
4142
})
4243
}

0 commit comments

Comments
 (0)