|
@@ -26,7 +26,7 @@ export interface OrthographicCameraOptions {
|
|
}
|
|
}
|
|
|
|
|
|
interface UseCameraReturn {
|
|
interface UseCameraReturn {
|
|
- activeCamera: Ref<Camera>
|
|
|
|
|
|
+ activeCamera: Ref<Camera | undefined>
|
|
createCamera: (cameraType?: CameraType, options?: PerspectiveCameraOptions | OrthographicCameraOptions) => Camera
|
|
createCamera: (cameraType?: CameraType, options?: PerspectiveCameraOptions | OrthographicCameraOptions) => Camera
|
|
updateCamera: () => void
|
|
updateCamera: () => void
|
|
pushCamera: (camera: Camera) => void
|
|
pushCamera: (camera: Camera) => void
|
|
@@ -36,9 +36,35 @@ interface UseCameraReturn {
|
|
const VERTICAL_FIELD_OF_VIEW = 45
|
|
const VERTICAL_FIELD_OF_VIEW = 45
|
|
let camera: Camera
|
|
let camera: Camera
|
|
|
|
|
|
|
|
+/**
|
|
|
|
+ * Create and update cameras
|
|
|
|
+ *
|
|
|
|
+ * ```ts
|
|
|
|
+ * import { useCamera } from '@tresjs/core'
|
|
|
|
+ * const { createCamera, updateCamera } = useCamera()
|
|
|
|
+ * const camera = createCamera(new PerspectiveCamera(45, 1, 0.1, 1000))
|
|
|
|
+ * updateCamera()
|
|
|
|
+ * ```
|
|
|
|
+ *
|
|
|
|
+ * @export
|
|
|
|
+ * @return {*} {UseCameraReturn}
|
|
|
|
+ */
|
|
export function useCamera(): UseCameraReturn {
|
|
export function useCamera(): UseCameraReturn {
|
|
const { state, setState } = useTres()
|
|
const { state, setState } = useTres()
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Create camera and push to Tres `state.cameras` array
|
|
|
|
+ *
|
|
|
|
+ * ```ts
|
|
|
|
+ * import { useCamera } from '@tresjs/core'
|
|
|
|
+ * const { createCamera } = useCamera()
|
|
|
|
+ * const camera = createCamera(new PerspectiveCamera(45, 1, 0.1, 1000))
|
|
|
|
+ * ```
|
|
|
|
+ *
|
|
|
|
+ * @param {*} [cameraType=CameraType.Perspective]
|
|
|
|
+ * @param {(PerspectiveCameraOptions | OrthographicCameraOptions)} [options]
|
|
|
|
+ * @return {*}
|
|
|
|
+ */
|
|
function createCamera(
|
|
function createCamera(
|
|
cameraType = CameraType.Perspective,
|
|
cameraType = CameraType.Perspective,
|
|
options?: PerspectiveCameraOptions | OrthographicCameraOptions,
|
|
options?: PerspectiveCameraOptions | OrthographicCameraOptions,
|
|
@@ -50,7 +76,7 @@ export function useCamera(): UseCameraReturn {
|
|
fov: VERTICAL_FIELD_OF_VIEW,
|
|
fov: VERTICAL_FIELD_OF_VIEW,
|
|
}
|
|
}
|
|
camera = new PerspectiveCamera(fov, state.aspectRatio?.value || 1, near, far)
|
|
camera = new PerspectiveCamera(fov, state.aspectRatio?.value || 1, near, far)
|
|
- state.cameras.push(camera as PerspectiveCamera)
|
|
|
|
|
|
+ state.cameras?.push(camera as PerspectiveCamera)
|
|
} else {
|
|
} else {
|
|
const { left, right, top, bottom, near, far } = (options as OrthographicCameraOptions) || {
|
|
const { left, right, top, bottom, near, far } = (options as OrthographicCameraOptions) || {
|
|
left: -100,
|
|
left: -100,
|
|
@@ -61,7 +87,7 @@ export function useCamera(): UseCameraReturn {
|
|
far: 1000,
|
|
far: 1000,
|
|
}
|
|
}
|
|
camera = new OrthographicCamera(left, right, top, bottom, near, far)
|
|
camera = new OrthographicCamera(left, right, top, bottom, near, far)
|
|
- state.cameras.push(camera as OrthographicCamera)
|
|
|
|
|
|
+ state.cameras?.push(camera as OrthographicCamera)
|
|
}
|
|
}
|
|
state.camera = camera
|
|
state.camera = camera
|
|
return camera
|
|
return camera
|
|
@@ -69,25 +95,34 @@ export function useCamera(): UseCameraReturn {
|
|
|
|
|
|
setState('camera', state.camera)
|
|
setState('camera', state.camera)
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Update camera aspect ratio and projection matrix
|
|
|
|
+ *
|
|
|
|
+ */
|
|
function updateCamera() {
|
|
function updateCamera() {
|
|
if (state.camera instanceof PerspectiveCamera && state.aspectRatio) {
|
|
if (state.camera instanceof PerspectiveCamera && state.aspectRatio) {
|
|
state.camera.aspect = state.aspectRatio.value
|
|
state.camera.aspect = state.aspectRatio.value
|
|
}
|
|
}
|
|
- state.camera.updateProjectionMatrix()
|
|
|
|
|
|
+ state.camera?.updateProjectionMatrix()
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Push camera to cameras array and update aspect ratio if camera is PerspectiveCamera
|
|
|
|
+ *
|
|
|
|
+ * @param {Camera} camera
|
|
|
|
+ */
|
|
function pushCamera(camera: Camera): void {
|
|
function pushCamera(camera: Camera): void {
|
|
- /* if (camera && currentCamera) {
|
|
|
|
- currentCamera.value = camera
|
|
|
|
- setState('camera', currentCamera)
|
|
|
|
- } */
|
|
|
|
- state.cameras.push(camera)
|
|
|
|
|
|
+ state.cameras?.push(camera)
|
|
if (camera instanceof PerspectiveCamera && state.aspectRatio) {
|
|
if (camera instanceof PerspectiveCamera && state.aspectRatio) {
|
|
camera.aspect = state.aspectRatio.value
|
|
camera.aspect = state.aspectRatio.value
|
|
}
|
|
}
|
|
camera.updateProjectionMatrix()
|
|
camera.updateProjectionMatrix()
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Clear cameras array
|
|
|
|
+ *
|
|
|
|
+ */
|
|
function clearCameras() {
|
|
function clearCameras() {
|
|
state.cameras = []
|
|
state.cameras = []
|
|
}
|
|
}
|