threepipe
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ThreeViewer.ts 57KB

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