threepipe
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ICamera.ts 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import {Camera, Vector3} from 'three'
  2. import {IObject3D, IObject3DEvent, IObject3DEventTypes, IObject3DUserData, IObjectSetDirtyOptions} from './IObject'
  3. import {IShaderPropertiesUpdater} from '../materials'
  4. import {ICameraControls, TControlsCtor} from './camera/ICameraControls'
  5. import {CameraView, ICameraView} from './camera/CameraView'
  6. /**
  7. * Available modes for {@link ICamera.controlsMode} property.
  8. * This is defined just for autocomplete, these and any other control type can be added by plugins
  9. */
  10. export type TCameraControlsMode = '' | 'orbit' | 'deviceOrientation' | 'threeFirstPerson' | 'pointerLock' | string
  11. export interface ICameraUserData extends IObject3DUserData {
  12. /**
  13. * Automatically calculate near and far planes based on the scene bounding box.
  14. */
  15. autoNearFar?: boolean
  16. /**
  17. * Minimum near plane distance. (when {@link autoNearFar} is true)
  18. * Or the near plane distance when {@link autoNearFar} is false.
  19. * @default 0.2
  20. */
  21. minNearPlane?: number
  22. /**
  23. * Maximum far plane distance. (when {@link autoNearFar} is true)
  24. * Or the far plane distance when {@link autoNearFar} is false.
  25. * @default 1000
  26. */
  27. maxFarPlane?: number
  28. /**
  29. * Automatically rotate camera to look at(lookAt) the target.
  30. * Only for when controls and interactions are disabled.
  31. * @default false
  32. */
  33. autoLookAtTarget?: boolean
  34. /**
  35. * Disable jitter for this camera. (for {@link SSAAPlugin})
  36. * @default false
  37. */
  38. disableJitter?: boolean
  39. __lastScale?: Vector3,
  40. __isMainCamera?: boolean,
  41. __cameraSetup?: boolean,
  42. // [key: string]: any // commented for noe
  43. }
  44. export interface ICamera<E extends ICameraEvent = ICameraEvent, ET extends ICameraEventTypes = ICameraEventTypes> extends Camera<E, ET>, IObject3D<E, ET>, IShaderPropertiesUpdater {
  45. assetType: 'camera'
  46. readonly isCamera: true
  47. setDirty(options?: ICameraSetDirtyOptions): void;
  48. readonly isMainCamera: boolean;
  49. readonly isPerspectiveCamera?: boolean;
  50. readonly isOrthographicCamera?: boolean;
  51. activateMain(options?: Partial<ICameraEvent>, _internal?: boolean, _refresh?: boolean): void;
  52. deactivateMain(options?: Partial<ICameraEvent>, _internal?: boolean, _refresh?: boolean): void;
  53. /**
  54. * @deprecated use `this` instead
  55. */
  56. cameraObject: this
  57. readonly controls: ICameraControls|undefined;
  58. // getControls<T extends TControls>(): T|undefined;
  59. refreshTarget(): void;
  60. refreshAspect(setDirty?: boolean): void;
  61. /**
  62. * Target of camera, in world(global) coordinates.
  63. */
  64. target: Vector3,
  65. /**
  66. * Local position of camera.
  67. */
  68. position: Vector3,
  69. readonly interactionsEnabled: boolean;
  70. setInteractions(enabled: boolean, by: string): void;
  71. /**
  72. * Check whether user can interact with this camera.
  73. * Interactions can be enabled/disabled in a variety of ways,
  74. * like {@link setInteractions}, {@link controlsMode}, {@link isMainCamera} property
  75. */
  76. readonly canUserInteract: boolean;
  77. zoom: number;
  78. /**
  79. * Camera frustum aspect ratio, window width divided by window height.
  80. * It can be managed internally if {@link autoAspect} is true.
  81. * @default 1
  82. */
  83. aspect: number;
  84. /**
  85. * Automatically manage aspect ratio based on window/canvas size.
  86. */
  87. autoAspect: boolean;
  88. controlsMode?: TCameraControlsMode; // todo add more.
  89. // controlsEnabled: boolean; // use controlsMode = '' instead
  90. /**
  91. * Automatically managed when {@link autoNearFar} is `true`. See also {@link minNearPlane}
  92. */
  93. near: number;
  94. /**
  95. * Automatically managed when {@link autoNearFar} is `true`. See also {@link maxFarPlane}
  96. */
  97. far: number;
  98. // also in userData
  99. autoNearFar: boolean // default = true
  100. minNearPlane: number // default = 0.2
  101. maxFarPlane: number // default = 1000
  102. // todo
  103. // Note: for userData: add _ in front of for private use, which is preserved while cloning but not serialisation, and __ for private use, which is not preserved while cloning and serialisation
  104. userData: ICameraUserData
  105. /**
  106. * @deprecated use {@link isMainCamera} instead
  107. */
  108. isActiveCamera: boolean;
  109. setControlsCtor(key: string, ctor: TControlsCtor, replace?: boolean): void;
  110. removeControlsCtor(key: string): void;
  111. refreshCameraControls(setDirty?: boolean): void
  112. updateProjectionMatrix(): void
  113. fov?: number
  114. getView<T extends ICameraView = CameraView>(worldSpace?: boolean, cameraView?: T): T
  115. setView(view: ICameraView): void
  116. /**
  117. * Set camera view from another camera.
  118. * @param camera
  119. * @param distanceFromTarget - default = 4
  120. * @param worldSpace - default = true
  121. */
  122. setViewFromCamera(camera: ICamera|Camera, distanceFromTarget?: number, worldSpace?: boolean): void
  123. /**
  124. * Dispatches the `setView` event which triggers the main camera to set its view to this camera's view.
  125. * @param eventOptions
  126. */
  127. setViewToMain(eventOptions: Partial<ICameraEvent>): void
  128. // region inherited type fixes
  129. // re-declaring from IObject3D because: https://github.com/microsoft/TypeScript/issues/16936
  130. traverse(callback: (object: IObject3D) => void): void
  131. traverseVisible(callback: (object: IObject3D) => void): void
  132. traverseAncestors(callback: (object: IObject3D) => void): void
  133. getObjectById<T extends IObject3D = IObject3D>(id: number): T | undefined
  134. getObjectByName<T extends IObject3D = IObject3D>(name: string): T | undefined
  135. getObjectByProperty<T extends IObject3D = IObject3D>(name: string, value: string): T | undefined
  136. copy(source: this, recursive?: boolean, distanceFromTarget?: number, worldSpace?: boolean, ...args: any[]): this
  137. clone(recursive?: boolean): this
  138. add(...object: IObject3D[]): this
  139. remove(...object: IObject3D[]): this
  140. parent: IObject3D | null
  141. children: IObject3D[]
  142. // endregion
  143. }
  144. export type ICameraEventTypes = IObject3DEventTypes | 'update'// | string
  145. export type ICameraEvent = Omit<IObject3DEvent, 'type'> & {
  146. type: ICameraEventTypes
  147. camera?: ICamera | null
  148. // change?: string
  149. }
  150. export type ICameraSetDirtyOptions = IObjectSetDirtyOptions