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.

ThreeViewer.ts 56KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. import {
  2. BaseEvent,
  3. CanvasTexture,
  4. Color,
  5. Event,
  6. EventDispatcher,
  7. LinearSRGBColorSpace,
  8. Object3D,
  9. Quaternion,
  10. Scene,
  11. Vector2,
  12. Vector3,
  13. } from 'three'
  14. import {Class, createCanvasElement, downloadBlob, onChange, serialize, ValOrArr} from 'ts-browser-helpers'
  15. import {TViewerScreenShader} from '../postprocessing'
  16. import {
  17. AddObjectOptions,
  18. IAnimationLoopEvent,
  19. IMaterial,
  20. IObject3D,
  21. IObjectProcessor,
  22. ITexture,
  23. PerspectiveCamera2,
  24. RootScene,
  25. TCameraControlsMode,
  26. } from '../core'
  27. import {ViewerRenderManager} from './ViewerRenderManager'
  28. import {
  29. convertArrayBufferToStringsInMeta,
  30. EasingFunctionType,
  31. getEmptyMeta,
  32. GLStatsJS,
  33. IDialogWrapper,
  34. jsonToBlob,
  35. metaFromResources,
  36. MetaImporter,
  37. metaToResources,
  38. SerializationMetaType,
  39. SerializationResourcesType,
  40. ThreeSerialization,
  41. windowDialogWrapper,
  42. } from '../utils'
  43. import {
  44. AssetManager,
  45. AssetManagerOptions,
  46. BlobExt,
  47. ExportFileOptions,
  48. IAsset,
  49. ImportAddOptions,
  50. ImportAssetOptions,
  51. ImportResult,
  52. RootSceneImportResult,
  53. } from '../assetmanager'
  54. import {IViewerPlugin, IViewerPluginSync} from './IViewerPlugin'
  55. import {uiConfig, UiObjectConfig, uiPanelContainer} from 'uiconfig.js'
  56. import {IRenderTarget} from '../rendering'
  57. import type {CanvasSnapshotPlugin, FileTransferPlugin} from '../plugins'
  58. import {CameraViewPlugin, ProgressivePlugin} from '../plugins'
  59. // noinspection ES6PreferShortImport
  60. import {DropzonePlugin, DropzonePluginOptions} from '../plugins/interaction/DropzonePlugin'
  61. // noinspection ES6PreferShortImport
  62. import {TonemapPlugin} from '../plugins/postprocessing/TonemapPlugin'
  63. import {VERSION} from './version'
  64. import {Easing} from 'popmotion'
  65. import {OrbitControls3} from '../three'
  66. export interface IViewerEvent extends BaseEvent, Partial<IAnimationLoopEvent> {
  67. type: '*'|'update'|'preRender'|'postRender'|'preFrame'|'postFrame'|'dispose'|'addPlugin'|'renderEnabled'|'renderDisabled'
  68. eType?: '*'|'update'|'preRender'|'postRender'|'preFrame'|'postFrame'|'dispose'|'addPlugin'|'renderEnabled'|'renderDisabled'
  69. [p: string]: any
  70. }
  71. export type IViewerEventTypes = IViewerEvent['type']
  72. export interface ISerializedConfig {
  73. assetType: 'config',
  74. type: string,
  75. metadata?: {
  76. generator: string,
  77. version: number,
  78. [key: string]: any
  79. },
  80. [key: string]: any
  81. }
  82. export interface ISerializedViewerConfig extends ISerializedConfig{
  83. type: 'ThreeViewer'|'ViewerApp',
  84. version: string,
  85. plugins: ISerializedConfig[],
  86. resources?: Partial<SerializationResourcesType> | SerializationMetaType
  87. renderManager?: any // todo
  88. scene?: any
  89. [key: string]: any
  90. }
  91. export type IConsoleWrapper = Partial<Console> & Pick<Console, 'log'|'warn'|'error'>
  92. /**
  93. * Options for the ThreeViewer creation.
  94. * @category Viewer
  95. */
  96. export interface ThreeViewerOptions {
  97. /**
  98. * The canvas element to use for rendering. Only one of container and canvas must be specified.
  99. */
  100. canvas?: HTMLCanvasElement,
  101. /**
  102. * The container for the canvas. A new canvas will be created in this container. Only one of container and canvas must be specified.
  103. */
  104. container?: HTMLElement,
  105. /**
  106. * The fragment shader snippet to render on screen.
  107. */
  108. screenShader?: TViewerScreenShader,
  109. /**
  110. * Use MSAA.
  111. */
  112. msaa?: boolean,
  113. /**
  114. * Use Uint8 RGBM HDR Render Pipeline.
  115. * Provides better performance with post-processing.
  116. * RenderManager Uses Half-float if set to false.
  117. */
  118. rgbm?: boolean
  119. /**
  120. * Use rendered gbuffer as depth-prepass / z-prepass. (Requires DepthBufferPlugin/GBufferPlugin)
  121. * todo: It will be disabled when there are any transparent/transmissive objects with render to depth buffer enabled.
  122. */
  123. zPrepass?: boolean
  124. /**
  125. * Force z-prepass even if there are transparent/transmissive objects with render to depth buffer enabled.
  126. */
  127. forceZPrepass?: boolean // todo
  128. /*
  129. * Render scale, 1 = full resolution, 0.5 = half resolution, 2 = double resolution.
  130. * Same as pixelRatio in three.js
  131. * Can be set to `window.devicePixelRatio` to render at device resolution in browsers.
  132. * An optimal value is `Math.min(2, window.devicePixelRatio)` to prevent issues on mobile. This is set when 'auto' is passed.
  133. */
  134. renderScale?: number | 'auto'
  135. debug?: boolean
  136. /**
  137. * Add initial plugins.
  138. */
  139. plugins?: (IViewerPluginSync | Class<IViewerPluginSync>)[]
  140. load?: {
  141. /**
  142. * Load one or more source files
  143. */
  144. src?: ValOrArr<string | IAsset | null>
  145. /**
  146. * Load environment map
  147. */
  148. environment?: string | IAsset | ITexture | undefined | null
  149. /**
  150. * Load background map
  151. */
  152. background?: string | IAsset | ITexture | undefined | null
  153. }
  154. onLoad?: (results: any) => void
  155. /**
  156. * TonemapPlugin is added to the viewer if this is true.
  157. * @default true
  158. */
  159. tonemap?: boolean
  160. camera?: {
  161. controlsMode?: TCameraControlsMode,
  162. position?: Vector3,
  163. target?: Vector3,
  164. }
  165. /**
  166. * Options for the asset manager.
  167. */
  168. assetManager?: AssetManagerOptions
  169. /**
  170. * Add the dropzone plugin to the viewer, allowing to drag and drop files into the viewer over the canvas/container.
  171. * Set to true/false to enable/disable the plugin, or pass options to configure the plugin. Assuming true if options are passed.
  172. * @default - false
  173. */
  174. dropzone?: boolean|DropzonePluginOptions
  175. /**
  176. * @deprecated use {@link msaa} instead
  177. */
  178. isAntialiased?: boolean,
  179. /**
  180. * @deprecated use {@link rgbm} instead
  181. */
  182. useRgbm?: boolean
  183. /**
  184. * @deprecated use {@link zPrepass} instead
  185. */
  186. useGBufferDepth?: boolean
  187. }
  188. /**
  189. * Three Viewer
  190. *
  191. * The ThreeViewer is the main class in the framework to manage a scene, render and add plugins to it.
  192. * @category Viewer
  193. */
  194. @uiPanelContainer('Viewer')
  195. export class ThreeViewer extends EventDispatcher<IViewerEvent, IViewerEventTypes> {
  196. public static readonly VERSION = VERSION
  197. public static readonly ConfigTypeSlug = 'vjson'
  198. uiConfig!: UiObjectConfig
  199. static Console: IConsoleWrapper = {
  200. log: console.log.bind(console),
  201. warn: console.warn.bind(console),
  202. error: console.error.bind(console),
  203. }
  204. static Dialog: IDialogWrapper = windowDialogWrapper
  205. /**
  206. * If the viewer is enabled. Set this `false` to disable RAF loop.
  207. * @type {boolean}
  208. */
  209. enabled = true
  210. /**
  211. * Enable or disable all rendering, Animation loop including any frame/render events won't be fired when this is false.
  212. */
  213. @onChange(ThreeViewer.prototype._renderEnabledChanged)
  214. renderEnabled = true
  215. renderStats: GLStatsJS
  216. readonly assetManager: AssetManager
  217. @uiConfig() @serialize('renderManager')
  218. readonly renderManager: ViewerRenderManager
  219. get materialManager() {
  220. return this.assetManager.materials
  221. }
  222. public readonly plugins: Record<string, IViewerPlugin> = {}
  223. /**
  224. * Scene with object hierarchy used for rendering
  225. */
  226. get scene(): RootScene&Scene {
  227. return this._scene as RootScene&Scene
  228. }
  229. /**
  230. * Specifies how many frames to render in a single request animation frame. Keep to 1 for realtime rendering.
  231. * Note: should be max (screen refresh rate / animation frame rate) like 60Hz / 30fps
  232. * @type {number}
  233. */
  234. public maxFramePerLoop = 1
  235. readonly debug: boolean
  236. /**
  237. * Number of times to run composer render. If set to more than 1, preRender and postRender events will also be called multiple times.
  238. */
  239. rendersPerFrame = 1
  240. /**
  241. * Get the HTML Element containing the canvas
  242. * @returns {HTMLElement}
  243. */
  244. get container(): HTMLElement {
  245. // todo console.warn('container is deprecated, NOTE: subscribe to events when the canvas is moved to another container')
  246. if (this._canvas.parentElement !== this._container) {
  247. this.console.error('ThreeViewer: Canvas is not in the container, this might cause issues with some plugins.')
  248. }
  249. return this._container
  250. }
  251. /**
  252. * Get the HTML Canvas Element where the viewer is rendering
  253. * @returns {HTMLCanvasElement}
  254. */
  255. get canvas(): HTMLCanvasElement {
  256. return this._canvas
  257. }
  258. get console(): IConsoleWrapper {
  259. return ThreeViewer.Console
  260. }
  261. get dialog(): IDialogWrapper {
  262. return ThreeViewer.Dialog
  263. }
  264. @serialize() readonly type = 'ThreeViewer'
  265. /**
  266. * The ResizeObserver observing the canvas element. Add more elements to this observer to resize viewer on their size change.
  267. * @type {ResizeObserver | undefined}
  268. */
  269. readonly resizeObserver = window?.ResizeObserver ? new window.ResizeObserver(_ => this.resize()) : undefined
  270. private readonly _canvas: HTMLCanvasElement
  271. // this can be used by other plugins to add ui elements alongside the canvas
  272. private readonly _container: HTMLElement // todo: add a way to move the canvas to a new container... and dispatch event...
  273. /**
  274. * The Scene attached to the viewer, this cannot be changed.
  275. * @type {RootScene}
  276. */
  277. @uiConfig() @serialize('scene')
  278. private readonly _scene: RootScene
  279. private _needsResize = false
  280. private _isRenderingFrame = false
  281. private _objectProcessor: IObjectProcessor = {
  282. processObject: (object: IObject3D)=>{
  283. if (object.material) {
  284. if (Array.isArray(object.material)) this.assetManager.materials.registerMaterials(object.material)
  285. else this.assetManager.materials.registerMaterial(object.material)
  286. }
  287. },
  288. }
  289. private _needsReset = true // renderer needs reset
  290. // Helpers for tracking main camera change and setting dirty automatically
  291. private _lastCameraPosition: Vector3 = new Vector3()
  292. private _lastCameraQuat: Quaternion = new Quaternion()
  293. private _lastCameraTarget: Vector3 = new Vector3()
  294. private _tempVec: Vector3 = new Vector3()
  295. private _tempQuat: Quaternion = new Quaternion()
  296. /**
  297. * If any of the viewers are in debug mode, this will be true.
  298. * This is required for debugging/logging in some cases.
  299. */
  300. public static ViewerDebugging = false // todo use in shaderReplaceString
  301. /**
  302. * Create a viewer instance for using the webgi viewer SDK.
  303. * @param options - {@link ThreeViewerOptions}
  304. */
  305. constructor({debug = false, ...options}: ThreeViewerOptions) {
  306. super()
  307. this.debug = debug
  308. if (debug) ThreeViewer.ViewerDebugging = true
  309. this._canvas = options.canvas || createCanvasElement()
  310. let container = options.container
  311. if (container && !options.canvas) container.appendChild(this._canvas)
  312. if (!container) container = this._canvas.parentElement ?? undefined
  313. if (!container) throw new Error('No container(or canvas).')
  314. this._container = container
  315. this.setDirty = this.setDirty.bind(this)
  316. this._animationLoop = this._animationLoop.bind(this)
  317. this._setActiveCameraView = this._setActiveCameraView.bind(this)
  318. this.renderStats = new GLStatsJS(this._container)
  319. if (debug) this.renderStats.show()
  320. if (!(window as any).threeViewers) (window as any).threeViewers = [];
  321. (window as any).threeViewers.push(this)
  322. // camera
  323. const camera = new PerspectiveCamera2(options.camera?.controlsMode ?? 'orbit', this._canvas)
  324. camera.name = 'Default Camera'
  325. options.camera?.position ? camera.position.copy(options.camera.position) : camera.position.set(0, 0, 5)
  326. options.camera?.target ? camera.target.copy(options.camera.target) : camera.target.set(0, 0, 0)
  327. camera.setDirty()
  328. camera.userData.autoLookAtTarget = true // only for when controls are disabled / not available
  329. // Update camera controls postFrame if allowed to interact
  330. this.addEventListener('postFrame', () => { // todo: move inside RootScene.
  331. const cam = this._scene.mainCamera
  332. if (cam && cam.canUserInteract) {
  333. const d = this.getPlugin<ProgressivePlugin>('ProgressivePlugin')?.postFrameConvergedRecordingDelta()
  334. // if (d && d > 0) delta = d
  335. if (d !== undefined && d === 0) return // not converged yet.
  336. // if d < 0 or undefined: not recording, do nothing
  337. cam.controls?.update()
  338. }
  339. })
  340. // if camera position or target changed in last frame, call setDirty on camera
  341. this.addEventListener('preFrame', () => { // todo: move inside RootScene.
  342. const cam = this._scene.mainCamera
  343. if (
  344. cam.getWorldPosition(this._tempVec).sub(this._lastCameraPosition).lengthSq() // position is in local space
  345. + this._tempVec.subVectors(cam.target, this._lastCameraTarget).lengthSq() // target is in world space
  346. + cam.getWorldQuaternion(this._tempQuat).angleTo(this._lastCameraQuat)
  347. > 0.000001) cam.setDirty()
  348. })
  349. // scene
  350. this._scene = new RootScene(camera, this._objectProcessor)
  351. this._scene.setBackgroundColor('#ffffff')
  352. // this._scene.addEventListener('addSceneObject', this._addSceneObject)
  353. this._scene.addEventListener('setView', this._setActiveCameraView)
  354. this._scene.addEventListener('activateMain', this._setActiveCameraView)
  355. this._scene.addEventListener('materialUpdate', (e) => this.setDirty(this._scene, e))
  356. this._scene.addEventListener('materialChanged', (e) => this.setDirty(this._scene, e))
  357. this._scene.addEventListener('objectUpdate', (e) => this.setDirty(this._scene, e))
  358. this._scene.addEventListener('textureUpdate', (e) => this.setDirty(this._scene, e))
  359. this._scene.addEventListener('sceneUpdate', (e) => {
  360. this.setDirty(this._scene, e)
  361. if (e.geometryChanged === false) return
  362. this.renderManager.resetShadows()
  363. })
  364. this._scene.addEventListener('mainCameraUpdate', () => {
  365. this._scene.mainCamera.getWorldPosition(this._lastCameraPosition)
  366. this._lastCameraTarget.copy(this._scene.mainCamera.target)
  367. this._scene.mainCamera.getWorldQuaternion(this._lastCameraQuat)
  368. })
  369. // render manager
  370. if (options.isAntialiased !== undefined || options.useRgbm !== undefined || options.useGBufferDepth !== undefined) {
  371. this.console.warn('isAntialiased, useRgbm and useGBufferDepth are deprecated, use msaa, rgbm and zPrepass instead.')
  372. }
  373. this.renderManager = new ViewerRenderManager({
  374. canvas: this._canvas,
  375. msaa: options.msaa ?? options.isAntialiased ?? false,
  376. rgbm: options.rgbm ?? options.useRgbm ?? false,
  377. zPrepass: options.zPrepass ?? options.useGBufferDepth ?? false,
  378. depthBuffer: !(options.zPrepass ?? options.useGBufferDepth ?? false),
  379. screenShader: options.screenShader,
  380. renderScale: typeof options.renderScale === 'string' ? options.renderScale === 'auto' ?
  381. Math.min(2, window.devicePixelRatio) : parseFloat(options.renderScale) :
  382. options.renderScale,
  383. })
  384. this.renderManager.addEventListener('animationLoop', this._animationLoop as any)
  385. this.renderManager.addEventListener('resize', ()=> this._scene.mainCamera.refreshAspect())
  386. this.renderManager.addEventListener('update', (e) => {
  387. if (e.change === 'registerPass' && e.pass?.materialExtension)
  388. this.assetManager.materials.registerMaterialExtension(e.pass.materialExtension)
  389. else if (e.change === 'unregisterPass' && e.pass?.materialExtension)
  390. this.assetManager.materials.unregisterMaterialExtension(e.pass.materialExtension)
  391. this.setDirty(this.renderManager, e)
  392. })
  393. this.assetManager = new AssetManager(this, options.assetManager)
  394. if (this.resizeObserver) this.resizeObserver.observe(this._canvas)
  395. // sometimes resize observer is late, so extra check
  396. window && window.addEventListener('resize', this.resize)
  397. this._canvas.addEventListener('webglcontextrestored', this._onContextRestore, false)
  398. this._canvas.addEventListener('webglcontextlost', this._onContextLost, false)
  399. if (options.dropzone) {
  400. this.addPluginSync(new DropzonePlugin(typeof options.dropzone === 'object' ? options.dropzone : undefined))
  401. }
  402. if (options.tonemap !== false) {
  403. this.addPluginSync(new TonemapPlugin())
  404. }
  405. for (const p of options.plugins ?? []) this.addPluginSync(p)
  406. this.console.log('ThreePipe Viewer instance initialized, version: ', ThreeViewer.VERSION)
  407. if (options.load) {
  408. const sources = [options.load.src].flat().filter(s=> s)
  409. const promises: Promise<any>[] = sources.map(async s=> s && this.load(s))
  410. if (options.load.environment) promises.push(this.setEnvironmentMap(options.load.environment))
  411. if (options.load.background) promises.push(this.setBackgroundMap(options.load.background))
  412. Promise.all(promises).then(options.onLoad)
  413. }
  414. }
  415. /**
  416. * Add an object/model/material/viewer-config/plugin-preset/... to the viewer scene from url or an {@link IAsset} object.
  417. * Same as {@link AssetManager.addAssetSingle}
  418. * @param obj
  419. * @param options
  420. */
  421. async load<T extends ImportResult = ImportResult>(obj: string | IAsset | File | null, options?: ImportAddOptions) {
  422. if (!obj) return
  423. return await this.assetManager.addAssetSingle<T>(obj, options)
  424. }
  425. /**
  426. * Imports an object/model/material/texture/viewer-config/plugin-preset/... to the viewer scene from url or an {@link IAsset} object.
  427. * Same as {@link AssetImporter.importSingle}
  428. * @param obj
  429. * @param options
  430. */
  431. async import<T extends ImportResult = ImportResult>(obj: string | IAsset | null, options?: ImportAddOptions) {
  432. if (!obj) return
  433. return await this.assetManager.importer.importSingle<T>(obj, options)
  434. }
  435. /**
  436. * Set the environment map of the scene from url or an {@link IAsset} object.
  437. * @param map
  438. * @param setBackground - Set the background image of the scene from the same map.
  439. * @param options - Options for importing the asset. See {@link ImportAssetOptions}
  440. */
  441. async setEnvironmentMap(map: string | IAsset | null | ITexture | undefined, {setBackground = false, ...options}: ImportAssetOptions&{setBackground?: boolean} = {}): Promise<ITexture | null> {
  442. this._scene.environment = map && !(<ITexture>map).isTexture ? await this.assetManager.importer.importSingle<ITexture>(map as string|IAsset, options) || null : <ITexture>map || null
  443. if (setBackground) return this.setBackgroundMap(this._scene.environment)
  444. return this._scene.environment
  445. }
  446. /**
  447. * Set the background image of the scene from url or an {@link IAsset} object.
  448. * @param map
  449. * @param setEnvironment - Set the environment map of the scene from the same map.
  450. * @param options - Options for importing the asset. See {@link ImportAssetOptions}
  451. */
  452. async setBackgroundMap(map: string | IAsset | null | ITexture | undefined, {setEnvironment = false, ...options}: ImportAssetOptions&{setBackground?: boolean} = {}): Promise<ITexture | null> {
  453. this._scene.background = map && !(<ITexture>map).isTexture ? await this.assetManager.importer.importSingle<ITexture>(map as string|IAsset, options) || null : <ITexture>map || null
  454. if (setEnvironment) return this.setEnvironmentMap(this._scene.background)
  455. return this._scene.background
  456. }
  457. /**
  458. * Exports an object/mesh/material/texture/render-target/plugin-preset/viewer to a blob.
  459. * If no object is given, a glb is exported with the current viewer state.
  460. * @param obj
  461. * @param options
  462. */
  463. async export(obj?: IObject3D|IMaterial|ITexture|IRenderTarget|IViewerPlugin|(typeof this), options?: ExportFileOptions) {
  464. if (!obj) obj = this._scene // this will export the glb with the scene and viewer config
  465. if ((<typeof this>obj).type === this.type) return jsonToBlob((<typeof this>obj).exportConfig())
  466. if ((<IViewerPlugin>obj).constructor?.PluginType) return jsonToBlob(this.exportPluginConfig(<IViewerPlugin>obj))
  467. return await this.assetManager.exporter.exportObject(<IObject3D|IMaterial|ITexture|IRenderTarget>obj, options)
  468. }
  469. /**
  470. * Export the scene to a file (default: glb with viewer config) and return a blob
  471. * @param options
  472. */
  473. async exportScene(options?: ExportFileOptions): Promise<BlobExt | undefined> {
  474. return this.assetManager.exporter.exportObject(this._scene.modelRoot, options)
  475. }
  476. async getScreenshotBlob({mimeType = 'image/jpeg', quality = 90} = {}): Promise<Blob | null | undefined> {
  477. const plugin = this.getPlugin<CanvasSnapshotPlugin>('CanvasSnapshotPlugin')
  478. if (plugin) {
  479. return plugin.getFile('snapshot.' + mimeType.split('/')[1], {mimeType, quality, waitForProgressive: true})
  480. }
  481. const blobPromise = async()=> new Promise<Blob|null>((resolve) => {
  482. this._canvas.toBlob((blob) => {
  483. resolve(blob)
  484. }, mimeType, quality)
  485. })
  486. if (!this.renderEnabled) return blobPromise()
  487. return await this.doOnce('postFrame', async() => {
  488. this.renderEnabled = false
  489. const blob = await blobPromise()
  490. this.renderEnabled = true
  491. return blob
  492. })
  493. }
  494. async getScreenshotDataUrl({mimeType = 'image/jpeg', quality = 0.9} = {}): Promise<string | null | undefined> {
  495. if (!this.renderEnabled) return this._canvas.toDataURL(mimeType, quality)
  496. return await this.doOnce('postFrame', () => this._canvas.toDataURL(mimeType, quality))
  497. }
  498. /**
  499. * Disposes the viewer and frees up all resource and events. Do not use the viewer after calling dispose.
  500. * @note - If you want to reuse the viewer, set viewer.enabled to false instead, then set it to true again when required. To dispose all the objects, materials in the scene use `viewer.scene.disposeSceneModels()`
  501. * This function is not fully implemented yet. There might be some memory leaks.
  502. * @todo - return promise?
  503. */
  504. public dispose(): void {
  505. // todo: dispose stuff from constructor etc
  506. for (const plugin of [...Object.values(this.plugins)]) {
  507. this.removePlugin(plugin, true)
  508. }
  509. this._scene.dispose()
  510. this.renderManager.dispose()
  511. this._canvas.removeEventListener('webglcontextrestored', this._onContextRestore, false)
  512. this._canvas.removeEventListener('webglcontextlost', this._onContextLost, false)
  513. ;(window as any).threeViewers?.splice((window as any).threeViewers.indexOf(this), 1)
  514. if (this.resizeObserver) this.resizeObserver.unobserve(this._canvas)
  515. else window.removeEventListener('resize', this.resize)
  516. this.dispatchEvent({type: 'dispose'})
  517. }
  518. /**
  519. * Mark that the canvas is resized. If the size is changed, the renderer and all render targets are resized. This happens before the render of the next frame.
  520. */
  521. resize = () => {
  522. this._needsResize = true
  523. this.setDirty()
  524. }
  525. /**
  526. * Set the viewer to dirty and trigger render of the next frame.
  527. * @param source - The source of the dirty event. like plugin or 3d object
  528. * @param event - The event that triggered the dirty event.
  529. */
  530. setDirty(source?: any, event?: Event): void {
  531. this._needsReset = true
  532. source = source ?? this
  533. this.dispatchEvent({...event ?? {}, type: 'update', source})
  534. }
  535. protected _animationLoop(event: IAnimationLoopEvent): void {
  536. if (!this.enabled || !this.renderEnabled) return
  537. if (this._isRenderingFrame) {
  538. this.console.warn('animation loop: frame skip') // not possible actually, since this is not async
  539. return
  540. }
  541. this._isRenderingFrame = true
  542. this.renderStats.begin()
  543. for (let i = 0; i < this.maxFramePerLoop; i++) {
  544. if (this._needsReset) {
  545. this.renderManager.reset()
  546. this._needsReset = false
  547. }
  548. if (this._needsResize) {
  549. const size = [this._canvas.clientWidth, this._canvas.clientHeight]
  550. if (event.xrFrame) { // todo: find a better way to resize for XR.
  551. const cam = this.renderManager.webglRenderer.xr.getCamera()?.cameras[0]?.viewport
  552. if (cam) {
  553. if (cam.x !== 0 || cam.y !== 0) {
  554. this.console.warn('x and y must be 0?')
  555. }
  556. size[0] = cam.width
  557. size[1] = cam.height
  558. this.console.log('resize for xr', size)
  559. } else {
  560. this._needsResize = false
  561. }
  562. }
  563. if (this._needsResize) {
  564. this.renderManager.setSize(...size)
  565. this._needsResize = false
  566. }
  567. }
  568. this.dispatchEvent({...event, type: 'preFrame', target: this}) // event will have time, deltaTime and xrFrame
  569. const dirtyPlugins = Object.values(this.plugins).filter(value => value.dirty)
  570. if (dirtyPlugins.length > 0) {
  571. // console.log('dirty plugins', dirtyPlugins)
  572. this.setDirty(dirtyPlugins)
  573. }
  574. if (this._needsReset) {
  575. this.renderManager.reset()
  576. this._needsReset = false
  577. }
  578. // Check if the renderManger is dirty, which happens when it's reset above or if any pass in the composer is dirty
  579. const needsRender = this.renderManager.needsRender
  580. if (needsRender) {
  581. for (let j = 0; j < this.rendersPerFrame; j++) {
  582. this.dispatchEvent({type: 'preRender', target: this})
  583. try {
  584. const cam = this._scene.mainCamera
  585. this._scene.renderCamera = cam
  586. if (cam.visible) this.renderManager.render(this._scene, this.renderManager.defaultRenderToScreen)
  587. } catch (e) {
  588. this.console.error(e)
  589. if (this.debug) throw e
  590. // this.enabled = false
  591. }
  592. this.dispatchEvent({type: 'postRender', target: this})
  593. }
  594. }
  595. this.dispatchEvent({type: 'postFrame', target: this})
  596. this.renderManager.onPostFrame()
  597. if (!needsRender) // break if no frame rendered
  598. break
  599. }
  600. this.renderStats.end()
  601. this._isRenderingFrame = false
  602. }
  603. /**
  604. * Get the Plugin by a constructor type or by the string type.
  605. * Use string type if the plugin is not a dependency and you don't want to bundle the plugin.
  606. * @param type - The class of the plugin to get, or the string type of the plugin to get which is in the static PluginType property of the plugin
  607. * @returns {T | undefined} - The plugin of the specified type.
  608. */
  609. getPlugin<T extends IViewerPlugin>(type: Class<T>|string): T | undefined {
  610. return this.plugins[typeof type === 'string' ? type : (type as any).PluginType] as T | undefined
  611. }
  612. /**
  613. * Get the Plugin by a constructor type or add a new plugin of the specified type if it doesn't exist.
  614. * @param type
  615. * @param args - arguments for the constructor of the plugin, used when a new plugin is created.
  616. */
  617. async getOrAddPlugin<T extends IViewerPlugin>(type: Class<T>, ...args: ConstructorParameters<Class<T>>): Promise<T> {
  618. const plugin = this.getPlugin(type)
  619. if (plugin) return plugin
  620. return this.addPlugin(type, ...args)
  621. }
  622. /**
  623. * Get the Plugin by a constructor type or add a new plugin to the viewer of the specified type if it doesn't exist(sync).
  624. * @param type
  625. * @param args - arguments for the constructor of the plugin, used when a new plugin is created.
  626. */
  627. getOrAddPluginSync<T extends IViewerPluginSync>(type: Class<T>, ...args: ConstructorParameters<Class<T>>): T {
  628. const plugin = this.getPlugin(type)
  629. if (plugin) return plugin
  630. return this.addPluginSync(type, ...args)
  631. }
  632. /**
  633. * Add a plugin to the viewer.
  634. * @param plugin - The instance of the plugin to add or the class of the plugin to add.
  635. * @param args - Arguments for the constructor of the plugin, in case a class is passed.
  636. * @returns {Promise<T>} - The plugin added.
  637. */
  638. async addPlugin<T extends IViewerPlugin>(plugin: T | Class<T>, ...args: ConstructorParameters<Class<T>>): Promise<T> {
  639. const p = this._resolvePluginOrClass(plugin, ...args)
  640. const type = p.constructor.PluginType
  641. if (!p.constructor.PluginType) {
  642. this.console.error('PluginType is not defined for', p)
  643. return p
  644. }
  645. for (const d of p.dependencies || []) {
  646. await this.getOrAddPlugin(d)
  647. }
  648. if (this.plugins[type]) {
  649. this.console.error(`Plugin of type ${type} already exists, removing and disposing old plugin. This might break functionality, ensure only one plugin of a type is added`, this.plugins[type], p)
  650. await this.removePlugin(this.plugins[type])
  651. }
  652. this.plugins[type] = p
  653. await p.onAdded(this)
  654. this.dispatchEvent({type: 'addPlugin', target: this, plugin: p})
  655. this.setDirty(p)
  656. return p
  657. }
  658. /**
  659. * Add a plugin to the viewer(sync).
  660. * @param plugin
  661. * @param args
  662. */
  663. addPluginSync<T extends IViewerPluginSync>(plugin: T|Class<T>, ...args: ConstructorParameters<Class<T>>): T {
  664. const p = this._resolvePluginOrClass(plugin, ...args)
  665. const type = p.constructor.PluginType
  666. if (!p.constructor.PluginType) {
  667. this.console.error('PluginType is not defined for', p)
  668. return p
  669. }
  670. for (const d of p.dependencies || []) {
  671. this.getOrAddPluginSync(d)
  672. }
  673. if (this.plugins[type]) {
  674. this.console.error(`Plugin of type ${type} already exists, removing and disposing old plugin. This might break functionality, ensure only one plugin of a type is added`, this.plugins[type], p)
  675. this.removePluginSync(this.plugins[type])
  676. }
  677. this.plugins[type] = p
  678. p.onAdded(this)
  679. this.dispatchEvent({type: 'addPlugin', target: this, plugin: p})
  680. this.setDirty(p)
  681. return p
  682. }
  683. /**
  684. * Add multiple plugins to the viewer.
  685. * @param plugins - List of plugin instances or classes
  686. */
  687. async addPlugins(plugins: (IViewerPlugin | Class<IViewerPlugin>)[]): Promise<void> {
  688. for (const p of plugins) await this.addPlugin(p)
  689. }
  690. /**
  691. * Add multiple plugins to the viewer(sync).
  692. * @param plugins - List of plugin instances or classes
  693. */
  694. addPluginsSync(plugins: (IViewerPluginSync | Class<IViewerPluginSync>)[]): void {
  695. for (const p of plugins) this.addPluginSync(p)
  696. }
  697. /**
  698. * Remove a plugin instance or a plugin class. Works similar to {@link ThreeViewer.addPlugin}
  699. * @param p
  700. * @param dispose
  701. * @returns {Promise<void>}
  702. */
  703. async removePlugin(p: IViewerPlugin<ThreeViewer, false>, dispose = true): Promise<void> {
  704. const type = p.constructor.PluginType
  705. if (!this.plugins[type]) return
  706. await p.onRemove(this)
  707. delete this.plugins[type]
  708. if (dispose) await p.dispose() // todo await?
  709. this.setDirty(p)
  710. }
  711. /**
  712. * Remove a plugin instance or a plugin class(sync). Works similar to {@link ThreeViewer.addPluginSync}
  713. * @param p
  714. * @param dispose
  715. */
  716. removePluginSync(p: IViewerPluginSync, dispose = true): void {
  717. const type = p.constructor.PluginType
  718. if (!this.plugins[type]) return
  719. p.onRemove(this)
  720. delete this.plugins[type]
  721. if (dispose) p.dispose()
  722. this.setDirty(p)
  723. }
  724. /**
  725. * Set size of the canvas and update the renderer.
  726. * If no size or width/height is passed, canvas is set to 100% of the container.
  727. *
  728. * See also {@link ThreeViewer.setRenderSize} to set the size of the render target by automatically calculating the renderScale and fitting in container.
  729. *
  730. * Note: Apps using this should ideally set `max-width: 100%` for the canvas in css.
  731. * @param size
  732. */
  733. setSize(size?: {width?: number, height?: number}) {
  734. this._canvas.style.width = size?.width ? size.width + 'px' : '100%'
  735. this._canvas.style.height = size?.height ? size.height + 'px' : '100%'
  736. // this._canvas.style.maxWidth = '100%' // this is upto the app to do.
  737. // this._canvas.style.maxHeight = '100%'
  738. this.resize()
  739. }
  740. // todo make an example for this.
  741. // todo make a constructor parameter for renderSize
  742. // todo make getRenderSize or get renderSize
  743. /**
  744. * Set the render size of the viewer to fit in the container according to the specified mode, maintaining aspect ratio.
  745. * Changes the renderScale accordingly.
  746. * Note: the canvas needs to be centered in the container to work properly, this can be done with the following css on the container:
  747. * ```css
  748. * display: flex;
  749. * justify-content: center;
  750. * align-items: center;
  751. * ```
  752. * or in js:
  753. * ```js
  754. * viewer.container.style.display = 'flex';
  755. * viewer.container.style.justifyContent = 'center';
  756. * viewer.container.style.alignItems = 'center';
  757. * ```
  758. * Modes:
  759. * 'contain': The canvas is scaled to fit within the container while maintaining its aspect ratio. The canvas will be fully visible, but there may be empty space around it.
  760. * 'cover': The canvas is scaled to fill the entire container while maintaining its aspect ratio. Part of the canvas may be clipped to fit the container.
  761. * 'fill': The canvas is stretched to completely fill the container, ignoring its aspect ratio.
  762. * 'scale-down': The canvas is scaled down to fit within the container while maintaining its aspect ratio, but it won't be scaled up if it's smaller than the container.
  763. * 'none': container size is ignored, but devicePixelRatio is used
  764. * @param size - The size to set the render to. The canvas will render to this size.
  765. * @param mode - 'contain', 'cover', 'fill', 'scale-down' or 'none'. Default is 'contain'.
  766. * @param devicePixelRatio - typically set to `window.devicePixelRatio`, or `Math.min(1.5, window.devicePixelRatio)` for performance. Use this only when size is derived from dom elements.
  767. * @param containerSize - (optional) The size of the container, if not passed, the bounding client rect of the container is used.
  768. */
  769. setRenderSize(size: {width: number, height: number},
  770. mode: 'contain' | 'cover' | 'fill' | 'scale-down' | 'none' = 'contain',
  771. devicePixelRatio = 1,
  772. containerSize?: {width: number, height: number}) {
  773. // todo what about container resize?
  774. const containerRect = containerSize || this.container.getBoundingClientRect()
  775. const containerHeight = containerRect.height
  776. const containerWidth = containerRect.width
  777. const width = size.width
  778. const height = size.height
  779. const aspect = width / height
  780. const containerAspect = containerWidth / containerHeight
  781. const dpr = devicePixelRatio
  782. let renderWidth, renderHeight
  783. switch (mode) {
  784. case 'contain':
  785. if (containerAspect > aspect) {
  786. renderWidth = containerHeight * aspect
  787. renderHeight = containerHeight
  788. } else {
  789. renderWidth = containerWidth
  790. renderHeight = containerWidth / aspect
  791. }
  792. break
  793. case 'cover':
  794. if (containerAspect > aspect) {
  795. renderWidth = containerWidth
  796. renderHeight = containerWidth / aspect
  797. } else {
  798. renderWidth = containerHeight * aspect
  799. renderHeight = containerHeight
  800. }
  801. break
  802. case 'fill':
  803. renderWidth = containerWidth
  804. renderHeight = containerHeight
  805. break
  806. case 'scale-down':
  807. if (width < containerWidth && height < containerHeight) {
  808. renderWidth = width
  809. renderHeight = height
  810. } else if (containerAspect > aspect) {
  811. renderWidth = containerHeight * aspect
  812. renderHeight = containerHeight
  813. } else {
  814. renderWidth = containerWidth
  815. renderHeight = containerWidth / aspect
  816. }
  817. break
  818. case 'none':
  819. renderWidth = width
  820. renderHeight = height
  821. break
  822. default:
  823. throw new Error(`Invalid mode: ${mode}`)
  824. }
  825. this.setSize({width: renderWidth, height: renderHeight})
  826. this.renderManager.renderScale = dpr * height / renderHeight
  827. }
  828. /**
  829. * Traverse all objects in scene model root.
  830. * @param callback
  831. */
  832. traverseSceneObjects<T extends IObject3D = IObject3D>(callback: (o: T)=>void): void {
  833. this._scene.modelRoot.traverse(callback)
  834. }
  835. /**
  836. * Add an object to the scene model root.
  837. * If an imported scene model root is passed, it will be loaded with viewer configuration, unless importConfig is false
  838. * @param imported
  839. * @param options
  840. */
  841. async addSceneObject<T extends IObject3D|Object3D|RootSceneImportResult = RootSceneImportResult>(imported: T, options?: AddObjectOptions): Promise<T> {
  842. if (imported.userData?.rootSceneModelRoot) {
  843. const obj = <RootSceneImportResult>imported
  844. if (obj.importedViewerConfig && options?.importConfig !== false) await this.importConfig(obj.importedViewerConfig)
  845. this._scene.loadModelRoot(obj, options)
  846. return this._scene.modelRoot as T
  847. }
  848. this._scene.addObject(imported, options)
  849. return imported
  850. }
  851. /**
  852. * Serialize all the plugins and their settings to save or create presets. Used in {@link toJSON}.
  853. * @param meta - The meta object.
  854. * @param filter - List of PluginType for the to include. If empty, no plugins will be serialized. If undefined, all plugins will be serialized.
  855. * @returns {any[]}
  856. */
  857. serializePlugins(meta: SerializationMetaType, filter?: string[]): any[] {
  858. if (filter && filter.length === 0) return []
  859. return Object.entries(this.plugins).map(p=> {
  860. if (filter && !filter.includes(p[1].constructor.PluginType)) return
  861. // if (!p[1].toJSON) this.console.log(`Plugin of type ${p[0]} is not serializable`)
  862. return p[1].serializeWithViewer !== false ? p[1].toJSON?.(meta) : undefined
  863. }).filter(p=> !!p)
  864. }
  865. /**
  866. * Deserialize all the plugins and their settings from a preset. Used in {@link fromJSON}.
  867. * @param plugins - The output of {@link serializePlugins}.
  868. * @param meta - The meta object.
  869. * @returns {this}
  870. */
  871. deserializePlugins(plugins: any[], meta?: SerializationMetaType): this {
  872. plugins.forEach(p=>{
  873. if (!p.type) {
  874. this.console.warn('Invalid plugin to import ', p)
  875. return
  876. }
  877. const plugin = this.getPlugin(p.type)
  878. if (!plugin) {
  879. // this.console.warn(`Plugin of type ${p.type} is not added, cannot deserialize`)
  880. return
  881. }
  882. plugin.fromJSON?.(p, meta)
  883. })
  884. return this
  885. }
  886. /**
  887. * Serialize a single plugin settings.
  888. */
  889. exportPluginConfig(plugin?: string|Class<IViewerPlugin>|IViewerPlugin): ISerializedConfig | Record<string, never> {
  890. if (plugin && typeof plugin === 'string' || (plugin as any).PluginType) plugin = this.getPlugin(plugin as any)
  891. if (!plugin) return {}
  892. const meta = getEmptyMeta()
  893. const data = (<IViewerPlugin>plugin).toJSON?.(meta)
  894. if (!data) return {}
  895. data.resources = metaToResources(meta)
  896. return data
  897. }
  898. /**
  899. * Deserialize and import a single plugin settings.
  900. * Can also use {@link ThreeViewer.importConfig} to import only plugin config.
  901. * @param json
  902. * @param plugin
  903. */
  904. async importPluginConfig(json: ISerializedConfig, plugin?: IViewerPlugin) {
  905. // this.console.log('importing plugin preset', json, plugin)
  906. const type = json.type
  907. plugin = plugin || this.getPlugin(type)
  908. if (!plugin) {
  909. this.console.warn(`No plugin found for type ${type} to import config`)
  910. return undefined
  911. }
  912. if (!plugin.fromJSON) {
  913. this.console.warn(`Plugin ${type} does not support importing presets`)
  914. return undefined
  915. }
  916. const resources = json.resources || {}
  917. if (json.resources) delete json.resources
  918. const meta = await this.loadConfigResources(resources)
  919. await plugin.fromJSON(json, meta)
  920. if (meta) json.resources = meta
  921. return plugin
  922. }
  923. /**
  924. * Serialize multiple plugin settings.
  925. * @param filter - List of PluginType to include. If empty, no plugins will be serialized. If undefined, all plugins will be serialized.
  926. */
  927. exportPluginsConfig(filter?: string[]): ISerializedViewerConfig {
  928. const meta = getEmptyMeta()
  929. const plugins = this.serializePlugins(meta, filter)
  930. convertArrayBufferToStringsInMeta(meta) // assuming not binary
  931. return {
  932. ...this._defaultConfig,
  933. plugins, resources: metaToResources(meta),
  934. }
  935. }
  936. /**
  937. * Serialize all the viewer and plugin settings.
  938. * @param binary - Indicate that the output will be converted and saved as binary data. (default: false)
  939. * @param pluginFilter - List of PluginType to include. If empty, no plugins will be serialized. If undefined, all plugins will be serialized.
  940. */
  941. exportConfig(binary = false, pluginFilter?: string[]) {
  942. return this.toJSON(binary, pluginFilter)
  943. }
  944. /**
  945. * Deserialize and import all the viewer and plugin settings, exported with {@link exportConfig}.
  946. */
  947. async importConfig(json: ISerializedConfig|ISerializedViewerConfig) {
  948. if (json.type !== this.type && <string>json.type !== 'ViewerApp') {
  949. if (this.getPlugin(json.type)) {
  950. return this.importPluginConfig(json)
  951. } else {
  952. this.console.error(`Unknown config type ${json.type} to import`)
  953. return undefined
  954. }
  955. }
  956. const resources = await this.loadConfigResources(json.resources || {})
  957. this.fromJSON(<ISerializedViewerConfig>json, resources)
  958. }
  959. /**
  960. * Serialize all the viewer and plugin settings and versions.
  961. * @param binary - Indicate that the output will be converted and saved as binary data. (default: true)
  962. * @param pluginFilter - List of PluginType to include. If empty, no plugins will be serialized. If undefined, all plugins will be serialized.
  963. * @returns {any} - Serializable JSON object.
  964. */
  965. toJSON(binary = true, pluginFilter?: string[]): ISerializedViewerConfig {
  966. const meta = getEmptyMeta()
  967. const data: ISerializedViewerConfig = Object.assign({
  968. ...this._defaultConfig,
  969. plugins: this.serializePlugins(meta, pluginFilter),
  970. }, ThreeSerialization.Serialize(this, meta, true))
  971. // this.console.log(dat)
  972. if (!binary) convertArrayBufferToStringsInMeta(meta)
  973. data.resources = metaToResources(meta)
  974. return data
  975. }
  976. /**
  977. * Deserialize all the viewer and plugin settings.
  978. * @note use async {@link ThreeViewer.importConfig} to import a json/config exported with {@link ThreeViewer.exportConfig} or {@link ThreeViewer.toJSON}.
  979. * @param data - The serialized JSON object retured from {@link toJSON}.
  980. * @param meta - The meta object
  981. * @returns {this}
  982. */
  983. fromJSON(data: ISerializedViewerConfig, meta?: SerializationMetaType): this|null {
  984. const data2: Partial<ISerializedViewerConfig> = {...data} // shallow copy
  985. // region legacy
  986. if (data2.backgroundIntensity !== undefined && data2.scene?.backgroundIntensity === undefined) {
  987. this.console.warn('old file format, backgroundIntensity moved to RootScene')
  988. this._scene.backgroundIntensity = data2.backgroundIntensity
  989. delete data2.backgroundIntensity
  990. }
  991. if (data2.useLegacyLights !== undefined && data2.renderManager?.useLegacyLights === undefined) {
  992. this.console.warn('old file format, useLegacyLights moved to RenderManager')
  993. this.renderManager.useLegacyLights = data2.useLegacyLights
  994. delete data2.useLegacyLights
  995. }
  996. if (data2.background !== undefined && data2.scene?.background === undefined) {
  997. this.console.warn('old file format, background moved to RootScene')
  998. if (data2.background === 'envMapBackground') data2.background = 'environment'
  999. else if (typeof data2.background === 'number')
  1000. data2.background = new Color().setHex(data2.background, LinearSRGBColorSpace)
  1001. else if (typeof data2.background === 'string')
  1002. data2.background = new Color().setStyle(data2.background, LinearSRGBColorSpace)
  1003. else if (data2.background?.isColor) data2.background = new Color(data2.background)
  1004. if (data2.background?.isColor) { // color
  1005. this._scene.backgroundColor = data2.background
  1006. this._scene.background = null
  1007. } else if (!data2.background) { // null
  1008. this._scene.backgroundColor = null
  1009. this._scene.background = null
  1010. } else { // texture or 'environment'
  1011. this._scene.backgroundColor = new Color('#ffffff')
  1012. if (!data2.scene) data2.scene = {}
  1013. data2.scene.background = data2.background
  1014. }
  1015. delete data2.background
  1016. }
  1017. // endregion
  1018. if (!meta && data2.resources && data2.resources.__isLoadedResources) {
  1019. meta = data2.resources as SerializationMetaType
  1020. delete data2.resources
  1021. }
  1022. if (!meta?.__isLoadedResources) {
  1023. this.console.error('meta in fromJSON is not available or is not loaded resources, call viewer.loadConfigResources first, or directly use viewer.importConfig')
  1024. return null
  1025. }
  1026. if (Array.isArray(data2.plugins)) {
  1027. this.deserializePlugins(data2.plugins, meta)
  1028. delete data2.plugins
  1029. }
  1030. // meta = meta || data.resources
  1031. ThreeSerialization.Deserialize(data2, this, meta, true)
  1032. // todo: handle
  1033. // __useCount set in ThreeSerialization while deserializing resources
  1034. // for (const mat of Object.values(resources.materials) as any) {
  1035. // if (!mat.__useCount) this.materialManager?.unregisterMaterial(mat) // todo: also dispose?
  1036. // else delete mat.__useCount
  1037. // }
  1038. // for (const tex of Object.values(resources.textures) as any) {
  1039. // if (!tex.__useCount) {
  1040. // // todo: dispose?
  1041. // } else {
  1042. // delete tex.__useCount
  1043. // }
  1044. // }
  1045. return this
  1046. }
  1047. loadConfigResources = async(json: Partial<SerializationMetaType>, extraResources?: Partial<SerializationResourcesType>): Promise<any> => {
  1048. // this.console.log(json)
  1049. if (json.__isLoadedResources) return json
  1050. const meta = metaFromResources(json, this)
  1051. return await MetaImporter.ImportMeta(meta, extraResources)
  1052. }
  1053. async doOnce<TRet>(event: IViewerEventTypes, func?: (...args: any[]) => TRet): Promise<TRet|undefined> {
  1054. return new Promise((resolve) => {
  1055. const listener = async(...args: any[]) => {
  1056. this.removeEventListener(event, listener)
  1057. resolve(await func?.(...args))
  1058. }
  1059. this.addEventListener(event, listener)
  1060. })
  1061. }
  1062. dispatchEvent(event: IViewerEvent) {
  1063. super.dispatchEvent(event)
  1064. super.dispatchEvent({...event, type: '*', eType: event.type})
  1065. }
  1066. /**
  1067. * Uses the {@link FileTransferPlugin} to export a blob. If the plugin is not available, it will download the blob.
  1068. * FileTransferPlugin can be configured by other plugins to export the blob to a specific location like local file system, cloud storage, etc.
  1069. * @param blob - The blob or file to export/download
  1070. * @param name
  1071. */
  1072. async exportBlob(blob: Blob|File, name?: string) {
  1073. const tr = this.getPlugin<FileTransferPlugin>('FileTransferPlugin')
  1074. name = name ?? (blob as File).name ?? 'file'
  1075. if (!tr) {
  1076. downloadBlob(blob, name)
  1077. return
  1078. }
  1079. await tr.exportFile(blob, name)
  1080. }
  1081. private _setActiveCameraView(event: any = {}): void {
  1082. if (event.type === 'setView') {
  1083. if (!event.camera) {
  1084. this.console.warn('Cannot find camera', event)
  1085. return
  1086. }
  1087. const camera = this._scene.mainCamera
  1088. camera.setViewFromCamera(event.camera) // default is worldSpace
  1089. } else if (event.type === 'activateMain')
  1090. this._scene.mainCamera = event.camera || undefined // event.camera should have been upgraded when added to the scene.
  1091. }
  1092. private _resolvePluginOrClass<T extends IViewerPlugin>(plugin: T | Class<T>, ...args: ConstructorParameters<Class<T>>): T {
  1093. let p: T
  1094. if ((plugin as Class<IViewerPlugin>).prototype) {
  1095. const p1 = this.getPlugin(plugin as Class<T>)
  1096. if (p1) {
  1097. this.console.error(`Plugin of type ${p1.constructor.PluginType} already exists, no new plugin created`, p1)
  1098. return p1
  1099. }
  1100. p = new (plugin as Class<T>)(...args)
  1101. } else p = plugin as T
  1102. return p
  1103. }
  1104. private _renderEnabledChanged(): void {
  1105. this.dispatchEvent({type: this.renderEnabled ? 'renderEnabled' : 'renderDisabled'})
  1106. }
  1107. private readonly _defaultConfig: ISerializedViewerConfig = {
  1108. assetType: 'config',
  1109. type: this.type,
  1110. version: ThreeViewer.VERSION,
  1111. metadata: {
  1112. generator: 'ThreePipe',
  1113. version: 1,
  1114. },
  1115. plugins: [],
  1116. }
  1117. // todo: find a better fix for context loss and restore?
  1118. private _lastSize = new Vector2()
  1119. private _onContextRestore = (_: Event) => {
  1120. this.enabled = true
  1121. this._canvas.width = this._lastSize.width
  1122. this._canvas.height = this._lastSize.height
  1123. this.resize()
  1124. this._scene.setDirty({refreshScene: true, frameFade: false})
  1125. }
  1126. private _onContextLost = (_: Event) => {
  1127. this._lastSize.set(this._canvas.width, this._canvas.height)
  1128. this._canvas.width = 2
  1129. this._canvas.height = 2
  1130. this.resize()
  1131. this.enabled = false
  1132. }
  1133. // private _addSceneObject = (e: IEvent<any>) => {
  1134. // if (!e || !e.object) return
  1135. // const config = e.object.__importedViewerConfig // this is set in gltf.ts when gltf file is imported. This is done here so that scene settings are applied whenever the imported object is added to scene.
  1136. // if (!config) return
  1137. // this.fromJSON(config, config.resources)
  1138. // }
  1139. public async fitToView(selected?: Object3D, distanceMultiplier = 1.5, duration?: number, ease?: Easing|EasingFunctionType) {
  1140. const camViews = this.getPlugin<CameraViewPlugin>('CameraViews')
  1141. if (!camViews) {
  1142. this.console.error('CameraViewPlugin (CameraViews) is required for fitToView to work')
  1143. return
  1144. }
  1145. await camViews?.animateToFitObject(selected, distanceMultiplier, duration, ease, {min: ((<OrbitControls3> this.scene.mainCamera.controls)?.minDistance ?? 0.5) + 0.5, max: 1000.0})
  1146. }
  1147. private _canvasTexture?: CanvasTexture&ITexture
  1148. /**
  1149. * Create and get a three.js CanvasTexture from the viewer's canvas.
  1150. */
  1151. get canvasTexture(): CanvasTexture {
  1152. if (!this._canvas) throw new Error('Canvas not found')
  1153. if (!this._canvasTexture) {
  1154. this._canvasTexture = new CanvasTexture(this._canvas)
  1155. this._canvasTexture.flipY = false
  1156. this._canvasTexture.needsUpdate = true
  1157. }
  1158. return this._canvasTexture
  1159. }
  1160. // todo: create/load texture utils
  1161. // region legacy creation functions
  1162. // /**
  1163. // * Converts a three.js Camera instance to be used in the viewer.
  1164. // * @param camera - The three.js OrthographicCamera or PerspectiveCamera instance
  1165. // * @returns {CameraController} - A wrapper around the camera with some useful methods and properties.
  1166. // */
  1167. // createCamera(camera: OrthographicCamera | PerspectiveCamera): CameraController {
  1168. // const cam: CameraController = camera.userData.iCamera ?? new CameraController(camera, {
  1169. // controlsMode: '',
  1170. // controlsEnabled: false,
  1171. // }, this._canvas)
  1172. // if (camera.userData.autoLookAtTarget === undefined) {
  1173. // cam.autoLookAtTarget = false
  1174. // camera.userData.autoLookAtTarget = false
  1175. // } else {
  1176. // cam.autoLookAtTarget = camera.userData.autoLookAtTarget
  1177. // }
  1178. // return cam
  1179. // }
  1180. // /**
  1181. // * Create a new empty object in the scene or add an existing three.js object to the scene.
  1182. // * @param object
  1183. // */
  1184. // async createObject3D(object?: Object3D): Promise<Object3DModel | undefined> {
  1185. // return this.getManager()?.addImportedSingle<Object3DModel>(object || new Object3D(), {autoScale: false, pseudoCenter: false})
  1186. // }
  1187. // /**
  1188. // * Create a new physical material from a template or another material. It returns the same material if a material is passed created by the material manager.
  1189. // * @param material
  1190. // */
  1191. // createPhysicalMaterial(material?: Material|MeshPhysicalMaterialParameters): MeshStandardMaterial2 | undefined {
  1192. // return this.createMaterial<MeshStandardMaterial2>('standard', material)
  1193. // }
  1194. // /**
  1195. // * Create a new material from a template or another material. It returns the same material if a material is passed created by the material manager.
  1196. // * @param template - template name registered in MaterialManager
  1197. // * @param material - three.js material object or material params to create a new material
  1198. // */
  1199. // createMaterial<T extends IMaterial<any>>(template: 'standard' | 'basic' | 'diamond' | string, material?: Material|any): T | undefined {
  1200. // if ((material as Material)?.isMaterial) {
  1201. // const f = this.getManager()?.materials?.findMaterial((material as Material).uuid)
  1202. // if (f) return f as T
  1203. // }
  1204. // return this.getManager()?.materials?.generateFromTemplate(template, material) as T
  1205. // }
  1206. // endregion
  1207. /**
  1208. * The renderer for the viewer that's attached to the canvas. This is wrapper around WebGLRenderer and EffectComposer and manages post-processing passes and rendering logic
  1209. * @deprecated - use {@link renderManager} instead
  1210. */
  1211. get renderer(): ViewerRenderManager {
  1212. this.console.error('renderer is deprecated, use renderManager instead')
  1213. return this.renderManager
  1214. }
  1215. /**
  1216. * @deprecated use {@link assetManager} instead.
  1217. * Gets the Asset manager, contains useful functions for managing, loading and inserting assets.
  1218. */
  1219. getManager(): AssetManager|undefined {
  1220. return this.assetManager
  1221. }
  1222. /**
  1223. * Get the Plugin by the string type.
  1224. * @deprecated - Use {@link getPlugin} instead.
  1225. * @param type
  1226. * @returns {T | undefined}
  1227. */
  1228. getPluginByType<T extends IViewerPlugin>(type: string): T | undefined {
  1229. return this.plugins[type] as T | undefined
  1230. }
  1231. }