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.

IObject.ts 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. import {IMaterial, IMaterialEventMap} from './IMaterial'
  2. import {Box3, EventListener2, Object3D, Object3DEventMap, Sphere, Vector3} from 'three'
  3. import {ChangeEvent, IUiConfigContainer, UiObjectConfig} from 'uiconfig.js'
  4. import {IGeometry, IGeometryEventMap} from './IGeometry'
  5. import {IImportResultUserData} from '../assetmanager'
  6. import {GLTF} from 'three/examples/jsm/loaders/GLTFLoader.js'
  7. import {ICamera, type ICameraSetDirtyOptions} from './ICamera'
  8. // export type IObject3DEventTypes = 'dispose' | 'materialUpdate' | 'objectUpdate' | 'textureUpdate' | 'geometryChanged' |
  9. // 'materialChanged' | 'geometryUpdate' | 'added' | 'removed' | 'select' | 'beforeDeserialize' |
  10. // 'setView' | 'activateMain' | 'cameraUpdate' // from camera
  11. // | string
  12. // export interface IObject3DEvent<T extends string = IObject3DEventTypes> extends Event {
  13. // type: T
  14. // object?: IObject3D // object that triggered the event, target might be parent in case of bubbleToParent
  15. // bubbleToParent?: boolean // bubble event to parent root
  16. // change?: string // todo - add to new type...
  17. // material?: IMaterial|undefined|IMaterial[] // from materialUpdate and materialChanged
  18. // oldMaterial?: IMaterial|undefined|IMaterial[] // from materialChanged
  19. // geometry?: IGeometry|undefined // from geometryUpdate, geometryChanged
  20. // oldGeometry?: IGeometry|undefined // from geometryChanged
  21. // source?: any // todo - add to new type...
  22. // }
  23. declare module 'three'{
  24. export interface Object3DEventMap{
  25. select: { // todo
  26. ui?: boolean
  27. focusCamera?: boolean
  28. bubbleToParent?: boolean
  29. object: IObject3D
  30. value?: IObject3D /* | Material*/ // todo is this required?
  31. source?: string // who is triggering the event. so that recursive events can be prevented
  32. } /* & IObjectSetDirtyOptions*/
  33. }
  34. }
  35. // [key: keyof Object3DEventMap]: Object3DEventMap[key] & {
  36. // bubbleToParent?: boolean
  37. // }
  38. export interface IObject3DEventMap extends Object3DEventMap{
  39. dispose: {
  40. // object: IObject3D
  41. // todo
  42. bubbleToParent: false
  43. }
  44. materialUpdate: {
  45. // object: IObject3D
  46. material: IMaterial|IMaterial[]
  47. }
  48. objectUpdate: {
  49. object: IObject3D
  50. change?: string
  51. args?: any[]
  52. bubbleToParent: boolean
  53. }
  54. textureUpdate: {
  55. // object: IObject3D
  56. // todo
  57. }
  58. geometryChanged: {
  59. object: IObject3D
  60. geometry: IGeometry|null
  61. oldGeometry: IGeometry|null
  62. bubbleToParent: boolean
  63. }
  64. materialChanged: {
  65. object: IObject3D
  66. material: IMaterial|IMaterial[]|null
  67. oldMaterial: IMaterial|IMaterial[]|null
  68. bubbleToParent: boolean
  69. }
  70. geometryUpdate: {
  71. object: IObject3D
  72. geometry: IGeometry
  73. // oldGeometry: IGeometry
  74. bubbleToParent: boolean
  75. }
  76. added: {
  77. // object: IObject3D
  78. // todo
  79. }
  80. removed: {
  81. // object: IObject3D
  82. // todo
  83. }
  84. beforeDeserialize: { // from material
  85. material: IMaterial
  86. // todo
  87. } & IMaterialEventMap['beforeDeserialize']
  88. setView: {
  89. ui?: boolean
  90. camera: ICamera
  91. bubbleToParent: boolean
  92. // object: IObject3D
  93. // todo
  94. }
  95. activateMain: {
  96. ui?: boolean
  97. camera?: ICamera | null
  98. bubbleToParent: boolean
  99. // object: IObject3D
  100. }
  101. cameraUpdate: {
  102. ui?: boolean
  103. camera?: ICamera
  104. // object: IObject3D
  105. bubbleToParent: boolean
  106. // todo
  107. } & ICameraSetDirtyOptions
  108. }
  109. // Record<keyof IObject3DEventMap0, IObject3DEventMap0[keyof IObject3DEventMap0] & {
  110. // // bubbleToParent?: boolean
  111. // }>
  112. export interface ISetDirtyCommonOptions {
  113. /**
  114. * Trigger UI Config Refresh along with setDirty.
  115. * Default `true`. Set to `false` to prevent UI Config refresh.
  116. */
  117. refreshUi?: boolean
  118. /**
  119. * Enable/disable frame fade using {@link FrameFadePlugin}
  120. * Default `true`. when the plugin is enabled and has corresponding flags enabled
  121. */
  122. frameFade?: boolean // for plugins
  123. /**
  124. * Duration for `frameFade` in ms. Check {@link FrameFadePlugin} for more details.
  125. */
  126. fadeDuration?: number // for plugins
  127. /**
  128. * Event from uiconfig.js when some value changes from the UI.
  129. */
  130. uiChangeEvent?: ChangeEvent,
  131. }
  132. export interface IObjectSetDirtyOptions extends ISetDirtyCommonOptions{
  133. /**
  134. * Bubble event to parent root(scene).
  135. */
  136. bubbleToParent?: boolean
  137. /**
  138. * Change identifier that triggered the setDirty call.
  139. */
  140. change?: string | keyof IObject3D
  141. /**
  142. * Update scene(bounds, shadows, plugins, etc) after setting dirty.
  143. */
  144. refreshScene?: boolean
  145. /**
  146. * Indicate whether the geometry has been changed to properly refresh plugins like ground, shadows.
  147. */
  148. geometryChanged?: boolean
  149. /**
  150. * update scene after setting dirty
  151. *
  152. * @deprecated use {@link refreshScene} instead
  153. */
  154. sceneUpdate?: boolean
  155. /**
  156. * Source identifier of who is triggering the event. so that recursive events can be prevented
  157. */
  158. source?: string
  159. /**
  160. * Key to identify the change. This is used to identify the change in the event. Can be used interchangeably with {@link change}.
  161. * Set from `onChange3` etc.
  162. */
  163. key?: string
  164. /**
  165. * Set to true if this is the last value in a user input chain. (like when mouse up on slider)
  166. */
  167. last?: boolean
  168. /**
  169. * Indicates that this change in from an `undo` operation.
  170. */
  171. undo?: boolean
  172. // value: any;
  173. // oldValue: any;
  174. // [key: string]: any
  175. }
  176. export interface IObjectProcessor { // todo, should be viewer
  177. processObject: (object: IObject3D) => void
  178. }
  179. export interface IObject3DUserData extends IImportResultUserData {
  180. uuid?: string
  181. /**
  182. * When true, this object will not be exported when exporting the scene with {@link AssetExporter.exportObject}
  183. */
  184. excludeFromExport?: boolean
  185. autoCentered?: boolean
  186. isCentered?: boolean
  187. autoScaleRadius?: number
  188. autoScaled?: boolean
  189. geometriesCentered?: boolean
  190. /**
  191. * should this object be taken into account when calculating bounding box, default true
  192. */
  193. bboxVisible?: boolean
  194. /**
  195. * Is centered in a parent object.
  196. */
  197. pseudoCentered?: boolean
  198. license?: string
  199. /**
  200. * When false, this object will not be selectable when clicking on it.
  201. */
  202. userSelectable?: boolean
  203. /**
  204. * Disables `bubbleToParent` in setDirty calls on the object. As an effect scene, viewer are not updated on property change. See progressive-hdr-shadows-exp or any baker.
  205. */
  206. autoUpdateParent?: boolean
  207. /**
  208. * For Physics plugins
  209. */
  210. physicsMass?: number
  211. /**
  212. * see {@link GLTFAnimationPlugin}
  213. */
  214. // eslint-disable-next-line @typescript-eslint/naming-convention
  215. gltfAnim_SyncMaxDuration?: boolean
  216. // region root scene model root
  217. /**
  218. * is it modelRoot in RootScene, used during serialization nad traversing ancestors
  219. */
  220. rootSceneModelRoot?: boolean
  221. __gltfAsset?: GLTF['asset']
  222. __gltfExtras?: GLTF['userData']
  223. // endregion
  224. __objectSetup?: boolean
  225. __meshSetup?: boolean
  226. // [key: string]: any // commented for noe
  227. // legacy
  228. /**
  229. * @deprecated
  230. */
  231. dispose?: any
  232. /**
  233. * @deprecated
  234. */
  235. setMaterial?: any
  236. /**
  237. * @deprecated
  238. */
  239. setGeometry?: any
  240. /**
  241. * @deprecated
  242. */
  243. setDirty?: any
  244. /**
  245. * Used in {@link GLTFObject3DExtrasExtension} and {@link iObjectCommons.upgradeObject3D}
  246. */
  247. __keepShadowDef?: boolean
  248. /**
  249. * Events that should be bubbled to parent root without the need to set bubbleToParent in the event.
  250. * todo: remove support for this
  251. */
  252. __autoBubbleToParentEvents?: string[]
  253. [key: string]: any
  254. }
  255. export interface IObject3D<TE extends IObject3DEventMap = IObject3DEventMap> extends Object3D<TE>, IUiConfigContainer {
  256. assetType: 'model' | 'light' | 'camera' | 'widget'
  257. isLight?: boolean
  258. isCamera?: boolean
  259. isMesh?: boolean
  260. isMeshLine?: boolean
  261. isLine?: boolean
  262. isLine2?: boolean
  263. isLineSegments?: boolean
  264. isLineSegments2?: boolean
  265. // isGroup?: boolean
  266. isScene?: boolean
  267. // isHelper?: boolean
  268. isWidget?: boolean
  269. readonly isObject3D: true
  270. material?: IMaterial | IMaterial[]
  271. /**
  272. * Same as material but always returns an array.
  273. * To set, just set `material` property
  274. */
  275. readonly materials?: IMaterial[]
  276. // eslint-disable-next-line @typescript-eslint/naming-convention
  277. _currentMaterial?: IMaterial | IMaterial[] | null
  278. geometry?: IGeometry
  279. morphTargetDictionary?: Record<string, number>
  280. morphTargetInfluences?: number[]
  281. updateMorphTargets?(): void
  282. // eslint-disable-next-line @typescript-eslint/naming-convention
  283. _currentGeometry?: IGeometry | null
  284. /**
  285. * Dispatches 'objectUpdate' event on object.
  286. * @param e
  287. */
  288. setDirty(e?: IObjectSetDirtyOptions): void
  289. /**
  290. * Parent/Ancestor of this object to bubble events to. This is set internally by setupObject3D.
  291. */
  292. parentRoot?: IObject3D | null
  293. uiConfig?: UiObjectConfig
  294. refreshUi(): void
  295. // 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
  296. userData: IObject3DUserData
  297. /**
  298. * Scales the object to fit the given radius.
  299. *
  300. * @param autoScaleRadius - optional (taken from userData.autoScaleRadius by default)
  301. * @param isCentered - optional (taken from userData.isCentered by default)
  302. * @param setDirty - true by default
  303. * @param undo - undo any previous autoScale operation
  304. */
  305. autoScale?(autoScaleRadius?: number, isCentered?: boolean, setDirty?: boolean, undo?: boolean): this
  306. /**
  307. * Moves the bounding box center of the object to the center of the world
  308. *
  309. * @param setDirty - calls {@link setDirty} @default true
  310. * @param undo - undo any previous autoCenter operation
  311. */
  312. autoCenter?(setDirty?: boolean, undo?: boolean): this
  313. /**
  314. * Moves the object pivot to the center of the bounding box.
  315. *
  316. * The object will rotate around the new pivot.
  317. *
  318. * @param setDirty - calls {@link setDirty} @default true
  319. * @returns undo function
  320. */
  321. pivotToBoundsCenter?(setDirty?: boolean): () => void
  322. /**
  323. * Moves the object pivot to the given point
  324. *
  325. * The object will rotate around the new pivot.
  326. *
  327. * @param point - point to move the pivot to
  328. * @param setDirty - calls {@link setDirty} @default true
  329. * @returns undo function
  330. */
  331. pivotToPoint?(point: Vector3, setDirty?: boolean): this
  332. /**
  333. * @deprecated use object directly
  334. */
  335. modelObject: this
  336. // eslint-disable-next-line @typescript-eslint/naming-convention
  337. _onGeometryUpdate?: EventListener2<'geometryUpdate', IGeometryEventMap, IGeometry>
  338. objectProcessor?: IObjectProcessor
  339. // __disposed?: boolean
  340. /**
  341. * @param removeFromParent - remove from parent. Default true
  342. */
  343. dispose(removeFromParent?: boolean): void;
  344. /**
  345. * A promise can be set by the object to indicate that the object is loading.
  346. * This can be used by the scene, viewer, plugins to defer actions until the object is loaded.
  347. */
  348. _loadingPromise?: Promise<void>
  349. /**
  350. * For InstancedMesh, SkinnedMesh etc
  351. */
  352. boundingBox?: Box3 | null
  353. /**
  354. * For InstancedMesh, SkinnedMesh etc
  355. */
  356. boundingSphere?: Sphere | null
  357. /**
  358. * For InstancedMesh, SkinnedMesh etc
  359. * Computes bounding box, updating {@link boundingBox | .boundingBox} attribute.
  360. * @remarks Bounding boxes aren't computed by default. They need to be explicitly computed, otherwise they are `null`.
  361. */
  362. computeBoundingBox?(): void;
  363. /**
  364. * For InstancedMesh, SkinnedMesh etc
  365. * Computes bounding sphere, updating {@link boundingSphere | .boundingSphere} attribute.
  366. * @remarks bounding spheres aren't computed by default. They need to be explicitly computed, otherwise they are `null`.
  367. */
  368. computeBoundingSphere?(): void;
  369. /**
  370. * For LineSegments, Line2 etc
  371. */
  372. computeLineDistances?(): void
  373. /**
  374. * Set to `false` to disable propagation of any events from its children.
  375. */
  376. acceptChildEvents?: boolean
  377. /**
  378. * Set to `false` to disable automatic call of `upgradeObject3D` when a child is added.
  379. */
  380. autoUpgradeChildren?: boolean
  381. // region inherited type fixes
  382. traverse(callback: (object: IObject3D) => void): void
  383. traverseVisible(callback: (object: IObject3D) => void): void
  384. traverseAncestors(callback: (object: IObject3D) => void): void
  385. getObjectById<T extends IObject3D = IObject3D>(id: number): T | undefined
  386. getObjectByName<T extends IObject3D = IObject3D>(name: string): T | undefined
  387. getObjectByProperty<T extends IObject3D = IObject3D>(name: string, value: string): T | undefined
  388. copy(source: this, recursive?: boolean, ...args: any[]): this
  389. clone(recursive?: boolean): this
  390. add(...object: Object3D[]): this
  391. remove(...object: IObject3D[]): this
  392. parent: IObject3D | null
  393. children: IObject3D[]
  394. // endregion
  395. }