Skip to content

Commit

Permalink
feat: clean FCamera options in 3d
Browse files Browse the repository at this point in the history
  • Loading branch information
Gugustinette committed Aug 30, 2024
1 parent d983387 commit 075c0c9
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 32 deletions.
2 changes: 1 addition & 1 deletion apps/playground-3d/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ import MyCustomCube from './classes/MyCustomCube'
scene.addComponent(character)

// Attach a camera to the character
scene.camera = new FGameCamera(character)
scene.camera = new FGameCamera({ target: character })

/**
* Create other objects
Expand Down
2 changes: 1 addition & 1 deletion docs/getting-started/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ Moreover, we would really appreciate your feedback, so if you have any suggestio

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

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

Have fun !
7 changes: 3 additions & 4 deletions packages/2d/src/cameras/FCamera.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { FCamera as FCameraCore } from '@fibbojs/core'
import type * as PIXI from 'pixi.js'
import type { FScene } from '../FScene'

export interface FCameraOptions {
position?: PIXI.PointData
position?: { x: number, y: number }
}

/**
Expand All @@ -21,11 +20,11 @@ export abstract class FCamera extends FCameraCore {
*/
scene: FScene

// Transform
// FTransform isn't used as a 2D camera doesn't need scale or rotation
/**
* Position of the camera.
*/
position: PIXI.PointData
position: { x: number, y: number }

/**
* @description Create a new 2D camera.
Expand Down
5 changes: 0 additions & 5 deletions packages/3d/src/FScene.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as THREE from 'three'
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'
import { FScene as FSceneCore } from '@fibbojs/core'
import type RAPIER from '@dimforge/rapier3d'
import type { FComponent } from './FComponent'
Expand Down Expand Up @@ -60,7 +59,6 @@ export class FScene extends FSceneCore {
declare scene: THREE.Scene
declare renderer: THREE.WebGLRenderer
declare camera: FCamera
declare controls?: OrbitControls
// Rapier
gravity: { x: number, y: number, z: number }
declare world: RAPIER.World
Expand Down Expand Up @@ -107,9 +105,6 @@ export class FScene extends FSceneCore {
this.renderer.setSize((window as any).innerWidth, (window as any).innerHeight)
// Create a default camera
this.camera = new FFixedCamera()
this.camera.position.set(5, 5, 5)
// Orbit controls for the camera
this.controls = new OrbitControls(this.camera, this.renderer.domElement)

// Add renderer to DOM
this.__DOM_NODE__.appendChild(this.renderer.domElement)
Expand Down
5 changes: 5 additions & 0 deletions packages/3d/src/cameras/FAttachedCamera.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import * as THREE from 'three'
import type { FComponent } from '../FComponent'
import type { FCameraOptions } from './FCamera'
import { FCamera } from './FCamera'

export interface FAttachedCameraOptions extends FCameraOptions {
target: FComponent
}

/**
* @description A camera that can be attached to a FComponent.
* @category Camera
Expand Down
34 changes: 33 additions & 1 deletion packages/3d/src/cameras/FCamera.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import * as THREE from 'three'
import type { FCamera as FCameraCore } from '@fibbojs/core'
import type { FTransformOptions } from '../FTransform'
import { FTransform } from '../FTransform'

export interface FCameraOptions extends FTransformOptions {}

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

constructor() {
/**
* Transform of the camera.
*/
transform: FTransform

constructor(options?: FCameraOptions) {
super(75, window.innerWidth / window.innerHeight, 0.1, 1000)

// Define default options
const DEFAULT_OPTIONS = {
position: { x: 5, y: 5, z: 5 },
rotation: { x: 0, y: 0.7853981634, z: 0 },
}
// Apply default options
options = { ...DEFAULT_OPTIONS, ...options }
// Validate options
if (!options.position || (!options.rotation && !options.rotationDegree))
throw new Error('FibboError: FCamera requires position and rotation options')

// Store transform
this.transform = new FTransform({
position: options.position,
rotation: options.rotation,
rotationDegree: options.rotationDegree,
})

// Set the position
this.position.set(this.transform.position.x, this.transform.position.y, this.transform.position.z)
// Set the rotation
this.rotation.set(this.transform.rotation.x, this.transform.rotation.y, this.transform.rotation.z)
}

abstract onFrame(_delta: number): void
Expand Down
5 changes: 3 additions & 2 deletions packages/3d/src/cameras/FFixedCamera.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { FCameraOptions } from './FCamera'
import { FCamera } from './FCamera'

/**
Expand All @@ -15,8 +16,8 @@ import { FCamera } from './FCamera'
* ```
*/
export class FFixedCamera extends FCamera {
constructor() {
super()
constructor(options?: FCameraOptions) {
super(options)
}

onFrame(_delta: number): void {
Expand Down
12 changes: 6 additions & 6 deletions packages/3d/src/cameras/FGameCamera.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as THREE from 'three'
import type { FComponent } from '../FComponent'
import { FOrbitCamera } from './FOrbitCamera'
import type { FAttachedCameraOptions } from './FAttachedCamera'

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

/**
* @param attachedComponent Component that the camera is attached to
* @param options Options for the camera.
*/
constructor(attachedComponent: FComponent) {
super(attachedComponent)
this.previousModelPosition = attachedComponent.transform.position.clone()
constructor(options: FAttachedCameraOptions) {
super(options)
this.previousModelPosition = options.target.transform.position.clone()
this.setPosition(0, 5, 5)

this.controls.enableDamping = true
Expand All @@ -40,7 +40,7 @@ export class FGameCamera extends FOrbitCamera {
* Add event listeners
*/
// Request pointer lock when the canvas is clicked
attachedComponent.scene.renderer.domElement.addEventListener('click', () => {
this.attachedComponent.scene.renderer.domElement.addEventListener('click', () => {
this.attachedComponent.scene.renderer.domElement.requestPointerLock()
})
// Update the pointer lock flag when the pointer lock state changes
Expand Down
11 changes: 6 additions & 5 deletions packages/3d/src/cameras/FOrbitCamera.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'
import type { FComponent } from '../FComponent'
import { FCamera } from './FCamera'
import type { FAttachedCameraOptions } from './FAttachedCamera'

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

/**
* @param attachedComponent Component that the camera is attached to
* @param options Options for the camera.
*/
constructor(attachedComponent: FComponent) {
super()
this.attachedComponent = attachedComponent
constructor(options: FAttachedCameraOptions) {
super(options)
this.attachedComponent = options.target

// Create orbit controls
this.controls = new OrbitControls(this, attachedComponent.scene.renderer.domElement)
this.controls = new OrbitControls(this, this.attachedComponent.scene.renderer.domElement)
}

onFrame(_delta: number): void {
Expand Down
15 changes: 8 additions & 7 deletions packages/3d/src/cameras/FPointerLockCamera.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PointerLockControls } from 'three/addons/controls/PointerLockControls.js'
import type { FComponent } from '../FComponent'
import { FCamera } from './FCamera'
import type { FAttachedCameraOptions } from './FAttachedCamera'

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

/**
* @param attachedComponent Component that the camera is attached to
* @param options Options for the camera.
*/
constructor(attachedComponent: FComponent) {
super()
this.attachedComponent = attachedComponent
constructor(options: FAttachedCameraOptions) {
super(options)
this.attachedComponent = options.target

// Create Pointer Lock controls
this.controls = new PointerLockControls(this, attachedComponent.scene.renderer.domElement)
this.controls = new PointerLockControls(this, this.attachedComponent.scene.renderer.domElement)

attachedComponent.scene.scene.add(this.controls.getObject())
this.attachedComponent.scene.scene.add(this.controls.getObject())

// Lock controls when clicking on the renderer
attachedComponent.scene.renderer.domElement.addEventListener('click', () => {
this.attachedComponent.scene.renderer.domElement.addEventListener('click', () => {
this.controls.lock()
})
}
Expand Down

0 comments on commit 075c0c9

Please sign in to comment.