babylon.viewer.d.ts 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. declare module BabylonViewer {
  2. export let disableInit: boolean;
  3. export function disposeAll(): void;
  4. export interface ITemplateConfiguration {
  5. location?: string;
  6. html?: string;
  7. id?: string;
  8. params?: {
  9. [key: string]: string | number | boolean | object;
  10. };
  11. events?: {
  12. pointerdown?: boolean | {
  13. [id: string]: boolean;
  14. };
  15. pointerup?: boolean | {
  16. [id: string]: boolean;
  17. };
  18. pointermove?: boolean | {
  19. [id: string]: boolean;
  20. };
  21. pointerover?: boolean | {
  22. [id: string]: boolean;
  23. };
  24. pointerout?: boolean | {
  25. [id: string]: boolean;
  26. };
  27. pointerenter?: boolean | {
  28. [id: string]: boolean;
  29. };
  30. pointerleave?: boolean | {
  31. [id: string]: boolean;
  32. };
  33. pointercancel?: boolean | {
  34. [id: string]: boolean;
  35. };
  36. click?: boolean | {
  37. [id: string]: boolean;
  38. };
  39. dragstart?: boolean | {
  40. [id: string]: boolean;
  41. };
  42. drop?: boolean | {
  43. [id: string]: boolean;
  44. };
  45. [key: string]: boolean | {
  46. [id: string]: boolean;
  47. } | undefined;
  48. };
  49. }
  50. /**
  51. * The object sent when an event is triggered
  52. */
  53. export interface EventCallback {
  54. /**
  55. * The native javascript event triggered
  56. */
  57. event: Event;
  58. /**
  59. * The template on which the event was triggered
  60. */
  61. template: Template;
  62. /**
  63. * The selector provided
  64. */
  65. selector: string;
  66. /**
  67. * Payload, if provided by the viewer
  68. */
  69. payload?: any;
  70. }
  71. /**
  72. * The template manager, a member of the viewer class, will manage the viewer's templates and generate the HTML.
  73. * The template manager managers a single viewer and can be seen as the collection of all sub-templates of the viewer.
  74. */
  75. interface TemplateManager {
  76. /**
  77. * The element to which all the templates wil be appended
  78. */
  79. containerElement: HTMLElement;
  80. /**
  81. * Will be triggered when any template is initialized
  82. */
  83. onTemplateInit: BABYLON.Observable<Template>;
  84. /**
  85. * Will be triggered when any template is fully loaded
  86. */
  87. onTemplateLoaded: BABYLON.Observable<Template>;
  88. /**
  89. * Will be triggered when a template state changes
  90. */
  91. onTemplateStateChange: BABYLON.Observable<Template>;
  92. /**
  93. * Will be triggered when all templates finished loading
  94. */
  95. onAllLoaded: BABYLON.Observable<TemplateManager>;
  96. /**
  97. * Will be triggered when any event on any template is triggered.
  98. */
  99. onEventTriggered: BABYLON.Observable<EventCallback>;
  100. /**
  101. * This template manager's event manager. In charge of callback registrations to native event types
  102. */
  103. eventManager: EventManager;
  104. /**
  105. * Initialize the template(s) for the viewer. Called bay the Viewer class
  106. * @param templates the templates to be used to initialize the main template
  107. */
  108. initTemplate(templates: {
  109. [key: string]: ITemplateConfiguration;
  110. }): Promise<void>;
  111. /**
  112. /**
  113. * Get the canvas in the template tree.
  114. * There must be one and only one canvas inthe template.
  115. */
  116. getCanvas(): HTMLCanvasElement | null;
  117. /**
  118. * Get a specific template from the template tree
  119. * @param name the name of the template to load
  120. */
  121. getTemplate(name: string): Template | undefined;
  122. /**
  123. * Dispose the template manager
  124. */
  125. dispose(): void;
  126. }
  127. /**
  128. * This class represents a single template in the viewer's template tree.
  129. * An example for a template is a single canvas, an overlay (containing sub-templates) or the navigation bar.
  130. * A template is injected using the template manager in the correct position.
  131. * The template is rendered using Handlebars and can use Handlebars' features (such as parameter injection)
  132. *
  133. * For further information please refer to the documentation page, https://doc.babylonjs.com
  134. */
  135. interface Template {
  136. /**
  137. * The name of the template
  138. */
  139. name: string;
  140. /**
  141. * Will be triggered when the template is loaded
  142. */
  143. onLoaded: BABYLON.Observable<Template>;
  144. /**
  145. * will be triggered when the template is appended to the tree
  146. */
  147. onAppended: BABYLON.Observable<Template>;
  148. /**
  149. * Will be triggered when the template's state changed (shown, hidden)
  150. */
  151. onStateChange: BABYLON.Observable<Template>;
  152. /**
  153. * Will be triggered when an event is triggered on ths template.
  154. * The event is a native browser event (like mouse or pointer events)
  155. */
  156. onEventTriggered: BABYLON.Observable<EventCallback>;
  157. /**
  158. * is the template loaded?
  159. */
  160. isLoaded: boolean;
  161. /**
  162. * This is meant to be used to track the show and hide functions.
  163. * This is NOT (!!) a flag to check if the element is actually visible to the user.
  164. */
  165. isShown: boolean;
  166. /**
  167. * Is this template a part of the HTML tree (the template manager injected it)
  168. */
  169. isInHtmlTree: boolean;
  170. /**
  171. * The HTML element containing this template
  172. */
  173. parent: HTMLElement;
  174. /**
  175. * A promise that is fulfilled when the template finished loading.
  176. */
  177. initPromise: Promise<Template>;
  178. /**
  179. * Some templates have parameters (like background color for example).
  180. * The parameters are provided to Handlebars which in turn generates the template.
  181. * This function will update the template with the new parameters
  182. *
  183. * @param params the new template parameters
  184. */
  185. updateParams(params: {
  186. [key: string]: string | number | boolean | object;
  187. }): void;
  188. /**
  189. * Get the template'S configuration
  190. */
  191. readonly configuration: ITemplateConfiguration;
  192. /**
  193. * A template can be a parent element for other templates or HTML elements.
  194. * This function will deliver all child HTML elements of this template.
  195. */
  196. getChildElements(): Array<string>;
  197. /**
  198. * Appending the template to a parent HTML element.
  199. * If a parent is already set and you wish to replace the old HTML with new one, forceRemove should be true.
  200. * @param parent the parent to which the template is added
  201. * @param forceRemove if the parent already exists, shoud the template be removed from it?
  202. */
  203. appendTo(parent: HTMLElement, forceRemove?: boolean): void;
  204. /**
  205. * Show the template using the provided visibilityFunction, or natively using display: flex.
  206. * The provided function returns a promise that should be fullfilled when the element is shown.
  207. * Since it is a promise async operations are more than possible.
  208. * See the default viewer for an opacity example.
  209. * @param visibilityFunction The function to execute to show the template.
  210. */
  211. show(visibilityFunction?: (template: Template) => Promise<Template>): Promise<Template>;
  212. /**
  213. * Hide the template using the provided visibilityFunction, or natively using display: none.
  214. * The provided function returns a promise that should be fullfilled when the element is hidden.
  215. * Since it is a promise async operations are more than possible.
  216. * See the default viewer for an opacity example.
  217. * @param visibilityFunction The function to execute to show the template.
  218. */
  219. hide(visibilityFunction?: (template: Template) => Promise<Template>): Promise<Template>;
  220. /**
  221. * Dispose this template
  222. */
  223. dispose(): void;
  224. }
  225. /**
  226. * The viewer manager is the container for all viewers currently registered on this page.
  227. * It is possible to have more than one viewer on a single page.
  228. */
  229. interface ViewerManager {
  230. /**
  231. * A callback that will be triggered when a new viewer was added
  232. */
  233. onViewerAdded: (viewer: AbstractViewer) => void;
  234. /**
  235. * Will notify when a new viewer was added
  236. */
  237. onViewerAddedObservable: BABYLON.Observable<AbstractViewer>;
  238. /**
  239. * Will notify when a viewer was removed (disposed)
  240. */
  241. onViewerRemovedObservable: BABYLON.Observable<string>;
  242. /**
  243. * Adding a new viewer to the viewer manager and start tracking it.
  244. * @param viewer the viewer to add
  245. */
  246. addViewer(viewer: AbstractViewer): void;
  247. /**
  248. * remove a viewer from the viewer manager
  249. * @param viewer the viewer to remove
  250. */
  251. removeViewer(viewer: AbstractViewer): void;
  252. /**
  253. * Get a viewer by its baseId (if the container element has an ID, it is the this is. if not, a random id was assigned)
  254. * @param id the id of the HTMl element (or the viewer's, if none provided)
  255. */
  256. getViewerById(id: string): AbstractViewer;
  257. /**
  258. * Get a viewer using a container element
  259. * @param element the HTML element to search viewers associated with
  260. */
  261. getViewerByHTMLElement(element: HTMLElement): AbstractViewer | undefined;
  262. /**
  263. * Get a promise that will fullfil when this viewer was initialized.
  264. * Since viewer initialization and template injection is asynchronous, using the promise will guaranty that
  265. * you will get the viewer after everything was already configured.
  266. * @param id the viewer id to find
  267. */
  268. getViewerPromiseById(id: string): Promise<AbstractViewer>;
  269. /**
  270. * dispose the manager and all of its associated viewers
  271. */
  272. dispose(): void;
  273. }
  274. export let viewerManager: ViewerManager;
  275. export const enum CameraBehavior {
  276. AUTOROTATION = 0,
  277. BOUNCING = 1,
  278. FRAMING = 2,
  279. }
  280. /*
  281. * Select all HTML tags on the page that match the selector and initialize a viewer
  282. *
  283. * @param selector the selector to initialize the viewer on (default is 'babylon')
  284. */
  285. export function InitTags(selector?: string): void;
  286. /**
  287. * The EventManager is in charge of registering user interctions with the viewer.
  288. * It is used in the TemplateManager
  289. */
  290. interface EventManager {
  291. /**
  292. * Register a new callback to a specific template.
  293. * The best example for the usage can be found in the DefaultViewer
  294. *
  295. * @param templateName the templateName to register the event to
  296. * @param callback The callback to be executed
  297. * @param eventType the type of event to register
  298. * @param selector an optional selector. if not defined the parent object in the template will be selected
  299. */
  300. registerCallback(templateName: string, callback: (eventData: EventCallback) => void, eventType?: string, selector?: string): void;
  301. /**
  302. * This will remove a registered event from the defined template.
  303. * Each one of the variables apart from the template name are optional, but one must be provided.
  304. *
  305. * @param templateName the templateName
  306. * @param callback the callback to remove (optional)
  307. * @param eventType the event type to remove (optional)
  308. * @param selector the selector from which to remove the event (optional)
  309. */
  310. unregisterCallback(templateName: string, callback: (eventData: EventCallback) => void, eventType?: string, selector?: string): void;
  311. /**
  312. * Dispose the event manager
  313. */
  314. dispose(): void;
  315. }
  316. /**
  317. * This is the mapper's interface. Implement this function to create your own mapper and register it at the mapper manager
  318. */
  319. export interface IMapper {
  320. map(rawSource: any): ViewerConfiguration;
  321. }
  322. interface MapperManager {
  323. /**
  324. * The default mapper is the JSON mapper.
  325. */
  326. DefaultMapper: string;
  327. /**
  328. * Get a specific configuration mapper.
  329. *
  330. * @param type the name of the mapper to load
  331. */
  332. getMapper(type: string): IMapper;
  333. /**
  334. * Use this functio to register your own configuration mapper.
  335. * After a mapper is registered, it can be used to parse the specific type fo configuration to the standard ViewerConfiguration.
  336. * @param type the name of the mapper. This will be used to define the configuration type and/or to get the mapper
  337. * @param mapper The implemented mapper
  338. */
  339. registerMapper(type: string, mapper: IMapper): void;
  340. /**
  341. * Dispose the mapper manager and all of its mappers.
  342. */
  343. dispose(): void;
  344. }
  345. export let mapperManager: MapperManager;
  346. interface ConfigurationLoader {
  347. /**
  348. * load a configuration object that is defined in the initial configuration provided.
  349. * The viewer configuration can extend different types of configuration objects and have an extra configuration defined.
  350. *
  351. * @param initConfig the initial configuration that has the definitions of further configuration to load.
  352. * @param callback an optional callback that will be called sync, if noconfiguration needs to be loaded or configuration is payload-only
  353. * @returns A promise that delivers the extended viewer configuration, when done.
  354. */
  355. loadConfiguration(initConfig?: ViewerConfiguration, callback?: (config: ViewerConfiguration) => void): Promise<ViewerConfiguration>;
  356. /**
  357. * Dispose the configuration loader. This will cancel file requests, if active.
  358. */
  359. dispose(): void;
  360. }
  361. export let configurationLoader: ConfigurationLoader;
  362. /////> configuration
  363. export interface ViewerConfiguration {
  364. // configuration version
  365. version?: string;
  366. extends?: string; // is this configuration extending an existing configuration?
  367. pageUrl?: string; // will be used for sharing and other fun stuff. This is the page showing the model (not the model's url!)
  368. configuration?: string | {
  369. url?: string;
  370. payload?: any;
  371. mapper?: string; // json (default), html, yaml, xml, etc'. if not provided, file extension will be used.
  372. };
  373. // names of functions in the window context.
  374. observers?: IObserversConfiguration;
  375. canvasElement?: string; // if there is a need to override the standard implementation - ID of HTMLCanvasElement
  376. model?: IModelConfiguration | string;
  377. scene?: ISceneConfiguration;
  378. optimizer?: ISceneOptimizerConfiguration | boolean;
  379. // at the moment, support only a single camera.
  380. camera?: ICameraConfiguration,
  381. skybox?: boolean | ISkyboxConfiguration;
  382. ground?: boolean | IGroundConfiguration;
  383. lights?: { [name: string]: boolean | ILightConfiguration },
  384. // engine configuration. optional!
  385. engine?: {
  386. antialiasing?: boolean;
  387. disableResize?: boolean;
  388. engineOptions?: { [key: string]: any };
  389. adaptiveQuality?: boolean;
  390. },
  391. //templateStructure?: ITemplateStructure,
  392. templates?: {
  393. main: ITemplateConfiguration,
  394. [key: string]: ITemplateConfiguration
  395. };
  396. customShaders?: {
  397. shaders?: {
  398. [key: string]: string;
  399. };
  400. includes?: {
  401. [key: string]: string;
  402. }
  403. }
  404. // features that are being tested.
  405. // those features' syntax will change and move out!
  406. // Don't use in production (or be ready to make the changes :) )
  407. lab?: {
  408. flashlight?: boolean | {
  409. exponent?: number;
  410. angle?: number;
  411. intensity?: number;
  412. diffuse?: { r: number, g: number, b: number };
  413. specular?: { r: number, g: number, b: number };
  414. }
  415. hideLoadingDelay?: number;
  416. }
  417. }
  418. export interface IModelConfiguration {
  419. url?: string;
  420. root?: string;
  421. loader?: string; // obj, gltf?
  422. position?: { x: number, y: number, z: number };
  423. rotation?: { x: number, y: number, z: number, w?: number };
  424. scaling?: { x: number, y: number, z: number };
  425. parentObjectIndex?: number; // the index of the parent object of the model in the loaded meshes array.
  426. castShadow?: boolean;
  427. normalize?: boolean | {
  428. center?: boolean;
  429. unitSize?: boolean;
  430. parentIndex?: number;
  431. }; // shoud the model be scaled to unit-size
  432. title?: string;
  433. subtitle?: string;
  434. thumbnail?: string; // URL or data-url
  435. animation?: {
  436. autoStart?: boolean | string;
  437. playOnce?: boolean;
  438. }
  439. }
  440. export interface ISkyboxConfiguration {
  441. cubeTexture?: {
  442. noMipMap?: boolean;
  443. gammaSpace?: boolean;
  444. url?: string | Array<string>;
  445. };
  446. color?: { r: number, g: number, b: number };
  447. pbr?: boolean; // deprecated
  448. scale?: number;
  449. blur?: number; // deprecated
  450. material?: {
  451. imageProcessingConfiguration?: IImageProcessingConfiguration;
  452. [propName: string]: any;
  453. };
  454. infiniteDIstance?: boolean;
  455. }
  456. export interface IGroundConfiguration {
  457. size?: number;
  458. receiveShadows?: boolean;
  459. shadowLevel?: number;
  460. shadowOnly?: boolean; // deprecated
  461. mirror?: boolean | {
  462. sizeRatio?: number;
  463. blurKernel?: number;
  464. amount?: number;
  465. fresnelWeight?: number;
  466. fallOffDistance?: number;
  467. textureType?: number;
  468. };
  469. texture?: string;
  470. color?: { r: number, g: number, b: number };
  471. opacity?: number;
  472. material?: { // deprecated!
  473. [propName: string]: any;
  474. };
  475. }
  476. export interface ISceneConfiguration {
  477. debug?: boolean;
  478. autoRotate?: boolean; // deprecated
  479. rotationSpeed?: number; // deprecated
  480. defaultCamera?: boolean; // deprecated
  481. defaultLight?: boolean; // deprecated
  482. clearColor?: { r: number, g: number, b: number, a: number };
  483. imageProcessingConfiguration?: IImageProcessingConfiguration;
  484. environmentTexture?: string;
  485. }
  486. export interface ISceneOptimizerConfiguration {
  487. targetFrameRate?: number;
  488. trackerDuration?: number;
  489. autoGeneratePriorities?: boolean;
  490. improvementMode?: boolean;
  491. degradation?: string; // low, moderate, high
  492. types?: {
  493. texture?: ISceneOptimizerParameters;
  494. hardwareScaling?: ISceneOptimizerParameters;
  495. shadow?: ISceneOptimizerParameters;
  496. postProcess?: ISceneOptimizerParameters;
  497. lensFlare?: ISceneOptimizerParameters;
  498. particles?: ISceneOptimizerParameters;
  499. renderTarget?: ISceneOptimizerParameters;
  500. mergeMeshes?: ISceneOptimizerParameters;
  501. }
  502. }
  503. export interface IObserversConfiguration {
  504. onEngineInit?: string;
  505. onSceneInit?: string;
  506. onModelLoaded?: string;
  507. }
  508. export interface ICameraConfiguration {
  509. position?: { x: number, y: number, z: number };
  510. rotation?: { x: number, y: number, z: number, w: number };
  511. fov?: number;
  512. fovMode?: number;
  513. minZ?: number;
  514. maxZ?: number;
  515. inertia?: number;
  516. behaviors?: {
  517. [name: string]: number | {
  518. type: number;
  519. [propName: string]: any;
  520. };
  521. };
  522. [propName: string]: any;
  523. }
  524. export interface ILightConfiguration {
  525. type: number;
  526. name?: string;
  527. disabled?: boolean;
  528. position?: { x: number, y: number, z: number };
  529. target?: { x: number, y: number, z: number };
  530. direction?: { x: number, y: number, z: number };
  531. diffuse?: { r: number, g: number, b: number };
  532. specular?: { r: number, g: number, b: number };
  533. intensity?: number;
  534. intensityMode?: number;
  535. radius?: number;
  536. shadownEnabled?: boolean; // only on specific lights!
  537. shadowConfig?: {
  538. useBlurExponentialShadowMap?: boolean;
  539. useKernelBlur?: boolean;
  540. blurKernel?: number;
  541. blurScale?: number;
  542. minZ?: number;
  543. maxZ?: number;
  544. frustumSize?: number;
  545. angleScale?: number;
  546. [propName: string]: any;
  547. }
  548. [propName: string]: any;
  549. // no behaviors for light at the moment, but allowing configuration for future reference.
  550. behaviors?: {
  551. [name: string]: number | {
  552. type: number;
  553. [propName: string]: any;
  554. };
  555. };
  556. }
  557. export interface ISceneOptimizerParameters {
  558. priority?: number;
  559. maximumSize?: number;
  560. step?: number;
  561. }
  562. export interface IImageProcessingConfiguration {
  563. colorGradingEnabled?: boolean;
  564. colorCurvesEnabled?: boolean;
  565. colorCurves?: {
  566. globalHue?: number;
  567. globalDensity?: number;
  568. globalSaturation?: number;
  569. globalExposure?: number;
  570. highlightsHue?: number;
  571. highlightsDensity?: number;
  572. highlightsSaturation?: number;
  573. highlightsExposure?: number;
  574. midtonesHue?: number;
  575. midtonesDensity?: number;
  576. midtonesSaturation?: number;
  577. midtonesExposure?: number;
  578. shadowsHue?: number;
  579. shadowsDensity?: number;
  580. shadowsSaturation?: number;
  581. shadowsExposure?: number;
  582. };
  583. colorGradingWithGreenDepth?: boolean;
  584. colorGradingBGR?: boolean;
  585. exposure?: number;
  586. toneMappingEnabled?: boolean;
  587. contrast?: number;
  588. vignetteEnabled?: boolean;
  589. vignetteStretch?: number;
  590. vignetteCentreX?: number;
  591. vignetteCentreY?: number;
  592. vignetteWeight?: number;
  593. vignetteColor?: { r: number, g: number, b: number, a?: number };
  594. vignetteCameraFov?: number;
  595. vignetteBlendMode?: number;
  596. vignetteM?: boolean;
  597. applyByPostProcess?: boolean;
  598. }
  599. /////>configuration
  600. /**
  601. * Animation play mode enum - is the animation looping or playing once
  602. */
  603. export enum AnimationPlayMode {
  604. ONCE = 0,
  605. LOOP = 1,
  606. }
  607. /**
  608. * An enum representing the current state of an animation object
  609. */
  610. export enum AnimationState {
  611. INIT = 0,
  612. PLAYING = 1,
  613. PAUSED = 2,
  614. STOPPED = 3,
  615. ENDED = 4,
  616. }
  617. /**
  618. * This interface can be implemented to define new types of ModelAnimation objects.
  619. */
  620. export interface IModelAnimation {
  621. /**
  622. * Current animation state (playing, stopped etc')
  623. */
  624. readonly state: AnimationState;
  625. /**
  626. * the name of the animation
  627. */
  628. readonly name: string;
  629. /**
  630. * Get the max numbers of frame available in the animation group
  631. *
  632. * In correlation to an arry, this would be ".length"
  633. */
  634. readonly frames: number;
  635. /**
  636. * Get the current frame playing right now.
  637. * This can be used to poll the frame currently playing (and, for exmaple, display a progress bar with the data)
  638. *
  639. * In correlation to an array, this would be the current index
  640. */
  641. readonly currentFrame: number;
  642. /**
  643. * Animation's FPS value
  644. */
  645. readonly fps: number;
  646. /**
  647. * Get or set the animation's speed ration (Frame-to-fps)
  648. */
  649. speedRatio: number;
  650. /**
  651. * Gets or sets the aimation's play mode.
  652. */
  653. playMode: AnimationPlayMode;
  654. /**
  655. * Start the animation
  656. */
  657. start(): any;
  658. /**
  659. * Stop the animation.
  660. * This will fail silently if the animation group is already stopped.
  661. */
  662. stop(): any;
  663. /**
  664. * Pause the animation
  665. * This will fail silently if the animation is not currently playing
  666. */
  667. pause(): any;
  668. /**
  669. * Reset this animation
  670. */
  671. reset(): any;
  672. /**
  673. * Restart the animation
  674. */
  675. restart(): any;
  676. /**
  677. * Go to a specific
  678. * @param frameNumber the frame number to go to
  679. */
  680. goToFrame(frameNumber: number): any;
  681. /**
  682. * Dispose this animation
  683. */
  684. dispose(): any;
  685. }
  686. export enum ModelState {
  687. INIT,
  688. LOADING,
  689. LOADED,
  690. CANCELED,
  691. ERROR
  692. }
  693. /**
  694. * An instance of the class is in charge of loading the model correctly.
  695. * This class will continously be expended with tasks required from the specific loaders Babylon has.
  696. *
  697. * A Model loader is unique per (Abstract)Viewer. It is being generated by the viewer
  698. */
  699. export class ModelLoader {
  700. private _viewer;
  701. private _loadId;
  702. private _disposed;
  703. private _loaders;
  704. /**
  705. * Create a new Model loader
  706. * @param _viewer the viewer using this model loader
  707. */
  708. constructor(_viewer: AbstractViewer);
  709. /**
  710. * Load a model using predefined configuration
  711. * @param modelConfiguration the modelConfiguration to use to load the model
  712. */
  713. load(modelConfiguration: IModelConfiguration): ViewerModel;
  714. cancelLoad(model: ViewerModel): void;
  715. /**
  716. * dispose the model loader.
  717. * If loaders are registered and are in the middle of loading, they will be disposed and the request(s) will be cancelled.
  718. */
  719. dispose(): void;
  720. }
  721. export class ViewerModel {
  722. /**
  723. * The viewer associated with this viewer model
  724. */
  725. protected _viewer: AbstractViewer;
  726. /**
  727. * The loader used to load this model.
  728. */
  729. loader: BABYLON.ISceneLoaderPlugin | BABYLON.ISceneLoaderPluginAsync;
  730. private _animations;
  731. /**
  732. * the list of meshes that are a part of this model
  733. */
  734. meshes: Array<BABYLON.AbstractMesh>;
  735. /**
  736. * This model's root mesh (the parent of all other meshes).
  737. * This mesh also exist in the meshes array.
  738. */
  739. rootMesh: BABYLON.AbstractMesh;
  740. /**
  741. * ParticleSystems connected to this model
  742. */
  743. particleSystems: Array<BABYLON.ParticleSystem>;
  744. /**
  745. * Skeletons defined in this model
  746. */
  747. skeletons: Array<BABYLON.Skeleton>;
  748. /**
  749. * The current model animation.
  750. * On init, this will be undefined.
  751. */
  752. currentAnimation: IModelAnimation;
  753. /**
  754. * Observers registered here will be executed when the model is done loading
  755. */
  756. onLoadedObservable: BABYLON.Observable<ViewerModel>;
  757. /**
  758. * Observers registered here will be executed when the loader notified of a progress event
  759. */
  760. onLoadProgressObservable: BABYLON.Observable<BABYLON.SceneLoaderProgressEvent>;
  761. /**
  762. * Observers registered here will be executed when the loader notified of an error.
  763. */
  764. onLoadErrorObservable: BABYLON.Observable<{
  765. message: string;
  766. exception: any;
  767. }>;
  768. /**
  769. * Observers registered here will be executed every time the model is being configured.
  770. * This can be used to extend the model's configuration without extending the class itself
  771. */
  772. onAfterConfigure: BABYLON.Observable<ViewerModel>;
  773. /**
  774. * The current model state (loaded, error, etc)
  775. */
  776. state: ModelState;
  777. /**
  778. * A loadID provided by the modelLoader, unique to ths (Abstract)Viewer instance.
  779. */
  780. loadId: number;
  781. private _loadedUrl;
  782. private _modelConfiguration;
  783. constructor(_viewer: AbstractViewer, modelConfiguration: IModelConfiguration);
  784. /**
  785. * Get the model's configuration
  786. */
  787. configuration: IModelConfiguration;
  788. /**
  789. * Update the current configuration with new values.
  790. * Configuration will not be overwritten, but merged with the new configuration.
  791. * Priority is to the new configuration
  792. * @param newConfiguration the configuration to be merged into the current configuration;
  793. */
  794. updateConfiguration(newConfiguration: Partial<IModelConfiguration>): void;
  795. initAnimations(): void;
  796. /**
  797. * Add a new animation group to this model.
  798. * @param animationGroup the new animation group to be added
  799. */
  800. addAnimationGroup(animationGroup: BABYLON.AnimationGroup): void;
  801. /**
  802. * Get the ModelAnimation array
  803. */
  804. getAnimations(): Array<IModelAnimation>;
  805. /**
  806. * Get the animations' names. Using the names you can play a specific animation.
  807. */
  808. getAnimationNames(): Array<string>;
  809. /**
  810. * Get an animation by the provided name. Used mainly when playing n animation.
  811. * @param name the name of the animation to find
  812. */
  813. protected _getAnimationByName(name: string): BABYLON.Nullable<IModelAnimation>;
  814. /**
  815. * Choose an initialized animation using its name and start playing it
  816. * @param name the name of the animation to play
  817. * @returns The model aniamtion to be played.
  818. */
  819. playAnimation(name: string): IModelAnimation;
  820. private _configureModel();
  821. /**
  822. * Dispose this model, including all of its associated assets.
  823. */
  824. dispose(): void;
  825. }
  826. /////<viewer
  827. export abstract class AbstractViewer {
  828. containerElement: HTMLElement;
  829. /**
  830. * The corresponsing template manager of this viewer.
  831. */
  832. templateManager: TemplateManager;
  833. /**
  834. * Babylon Engine corresponding with this viewer
  835. */
  836. engine: BABYLON.Engine;
  837. /**
  838. * The Babylon Scene of this viewer
  839. */
  840. scene: BABYLON.Scene;
  841. /**
  842. * The camera used in this viewer
  843. */
  844. camera: BABYLON.ArcRotateCamera;
  845. /**
  846. * Babylon's scene optimizer
  847. */
  848. sceneOptimizer: BABYLON.SceneOptimizer;
  849. /**
  850. * The ID of this viewer. it will be generated randomly or use the HTML Element's ID.
  851. */
  852. readonly baseId: string;
  853. /**
  854. * Models displayed in this viewer.
  855. */
  856. models: Array<ViewerModel>;
  857. /**
  858. * The last loader used to load a model.
  859. */
  860. lastUsedLoader: BABYLON.ISceneLoaderPlugin | BABYLON.ISceneLoaderPluginAsync;
  861. /**
  862. * The ModelLoader instance connected with this viewer.
  863. */
  864. modelLoader: ModelLoader;
  865. /**
  866. * the viewer configuration object
  867. */
  868. protected _configuration: ViewerConfiguration;
  869. /**
  870. * Babylon's environment helper of this viewer
  871. */
  872. environmentHelper: BABYLON.EnvironmentHelper;
  873. protected _defaultHighpTextureType: number;
  874. protected _shadowGeneratorBias: number;
  875. protected _defaultPipelineTextureType: number;
  876. /**
  877. * The maximum number of shadows supported by the curent viewer
  878. */
  879. protected _maxShadows: number;
  880. /**
  881. * is HDR supported?
  882. */
  883. private _hdrSupport;
  884. /**
  885. * is this viewer disposed?
  886. */
  887. protected _isDisposed: boolean;
  888. /**
  889. * Returns a boolean representing HDR support
  890. */
  891. readonly isHdrSupported: boolean;
  892. /**
  893. * Will notify when the scene was initialized
  894. */
  895. onSceneInitObservable: BABYLON.Observable<BABYLON.Scene>;
  896. /**
  897. * will notify when the engine was initialized
  898. */
  899. onEngineInitObservable: BABYLON.Observable<BABYLON.Engine>;
  900. /**
  901. * will notify after every model load
  902. */
  903. onModelLoadedObservable: BABYLON.Observable<ViewerModel>;
  904. /**
  905. * will notify when any model notify of progress
  906. */
  907. onModelLoadProgressObservable: BABYLON.Observable<BABYLON.SceneLoaderProgressEvent>;
  908. /**
  909. * will notify when any model load failed.
  910. */
  911. onModelLoadErrorObservable: BABYLON.Observable<{
  912. message: string;
  913. exception: any;
  914. }>;
  915. /**
  916. * will notify when a new loader was initialized.
  917. * Used mainly to know when a model starts loading.
  918. */
  919. onLoaderInitObservable: BABYLON.Observable<BABYLON.ISceneLoaderPlugin | BABYLON.ISceneLoaderPluginAsync>;
  920. /**
  921. * Observers registered here will be executed when the entire load process has finished.
  922. */
  923. onInitDoneObservable: BABYLON.Observable<AbstractViewer>;
  924. /**
  925. * The canvas associated with this viewer
  926. */
  927. protected _canvas: HTMLCanvasElement;
  928. /**
  929. * The (single) canvas of this viewer
  930. */
  931. readonly canvas: HTMLCanvasElement;
  932. /**
  933. * registered onBeforeRender functions.
  934. * This functions are also registered at the native scene. The reference can be used to unregister them.
  935. */
  936. protected _registeredOnBeforeRenderFunctions: Array<() => void>;
  937. /**
  938. * The configuration loader of this viewer
  939. */
  940. protected _configurationLoader: ConfigurationLoader;
  941. constructor(containerElement: HTMLElement, initialConfiguration?: ViewerConfiguration);
  942. /**
  943. * get the baseId of this viewer
  944. */
  945. getBaseId(): string;
  946. /**
  947. * Do we have a canvas to render on, and is it a part of the scene
  948. */
  949. isCanvasInDOM(): boolean;
  950. /**
  951. * The resize function that will be registered with the window object
  952. */
  953. protected _resize: () => void;
  954. /**
  955. * render loop that will be executed by the engine
  956. */
  957. protected _render: () => void;
  958. /**
  959. * Update the current viewer configuration with new values.
  960. * Only provided information will be updated, old configuration values will be kept.
  961. * If this.configuration was manually changed, you can trigger this function with no parameters,
  962. * and the entire configuration will be updated.
  963. * @param newConfiguration
  964. */
  965. updateConfiguration(newConfiguration?: Partial<ViewerConfiguration>): void;
  966. protected _configureEnvironment(skyboxConifguration?: ISkyboxConfiguration | boolean, groundConfiguration?: IGroundConfiguration | boolean): Promise<BABYLON.Scene> | undefined;
  967. /**
  968. * internally configure the scene using the provided configuration.
  969. * The scene will not be recreated, but just updated.
  970. * @param sceneConfig the (new) scene configuration
  971. */
  972. protected _configureScene(sceneConfig: ISceneConfiguration): void;
  973. /**
  974. * Configure the scene optimizer.
  975. * The existing scene optimizer will be disposed and a new one will be created.
  976. * @param optimizerConfig the (new) optimizer configuration
  977. */
  978. protected _configureOptimizer(optimizerConfig: ISceneOptimizerConfiguration | boolean): void;
  979. /**
  980. * this is used to register native functions using the configuration object.
  981. * This will configure the observers.
  982. * @param observersConfiguration observers configuration
  983. */
  984. protected _configureObservers(observersConfiguration: IObserversConfiguration): void;
  985. /**
  986. * (Re) configure the camera. The camera will only be created once and from this point will only be reconfigured.
  987. * @param cameraConfig the new camera configuration
  988. * @param model optionally use the model to configure the camera.
  989. */
  990. protected _configureCamera(cameraConfig: ICameraConfiguration, model?: ViewerModel): void;
  991. /**
  992. * configure the lights.
  993. *
  994. * @param lightsConfiguration the (new) light(s) configuration
  995. * @param model optionally use the model to configure the camera.
  996. */
  997. protected _configureLights(lightsConfiguration?: {
  998. [name: string]: ILightConfiguration | boolean;
  999. }, model?: ViewerModel): void;
  1000. /**
  1001. * configure all models using the configuration.
  1002. * @param modelConfiguration the configuration to use to reconfigure the models
  1003. */
  1004. protected _configureModel(modelConfiguration: Partial<IModelConfiguration>): void;
  1005. /**
  1006. * Dispoe the entire viewer including the scene and the engine
  1007. */
  1008. dispose(): void;
  1009. /**
  1010. * This will prepare the container element for the viewer
  1011. */
  1012. protected abstract _prepareContainerElement(): any;
  1013. /**
  1014. * This function will execute when the HTML templates finished initializing.
  1015. * It should initialize the engine and continue execution.
  1016. *
  1017. * @returns {Promise<AbstractViewer>} The viewer object will be returned after the object was loaded.
  1018. */
  1019. protected _onTemplatesLoaded(): Promise<AbstractViewer>;
  1020. /**
  1021. * Initialize the engine. Retruns a promise in case async calls are needed.
  1022. *
  1023. * @protected
  1024. * @returns {Promise<Engine>}
  1025. * @memberof Viewer
  1026. */
  1027. protected _initEngine(): Promise<BABYLON.Engine>;
  1028. /**
  1029. * initialize the scene. Calling thsi function again will dispose the old scene, if exists.
  1030. */
  1031. protected _initScene(): Promise<BABYLON.Scene>;
  1032. /**
  1033. * Initialize a model loading. The returns object (a ViewerModel object) will be loaded in the background.
  1034. * The difference between this and loadModel is that loadModel will fulfill the promise when the model finished loading.
  1035. *
  1036. * @param modelConfig model configuration to use when loading the model.
  1037. * @param clearScene should the scene be cleared before loading this model
  1038. * @returns a ViewerModel object that is not yet fully loaded.
  1039. */
  1040. initModel(modelConfig: IModelConfiguration, clearScene?: boolean): ViewerModel;
  1041. /**
  1042. * load a model using the provided configuration
  1043. *
  1044. * @param modelConfig the model configuration or URL to load.
  1045. * @param clearScene Should the scene be cleared before loading the model
  1046. * @returns a Promise the fulfills when the model finished loading successfully.
  1047. */
  1048. loadModel(modelConfig?: any, clearScene?: boolean): Promise<ViewerModel>;
  1049. /**
  1050. * initialize the environment for a specific model.
  1051. * Per default it will use the viewer'S configuration.
  1052. * @param model the model to use to configure the environment.
  1053. * @returns a Promise that will resolve when the configuration is done.
  1054. */
  1055. protected _initEnvironment(model?: ViewerModel): Promise<BABYLON.Scene>;
  1056. /**
  1057. * Alters render settings to reduce features based on hardware feature limitations
  1058. * @param options Viewer options to modify
  1059. */
  1060. protected _handleHardwareLimitations(): void;
  1061. /**
  1062. * Injects all the spectre shader in the babylon shader store
  1063. */
  1064. protected _injectCustomShaders(): void;
  1065. /**
  1066. * This will extend an object with configuration values.
  1067. * What it practically does it take the keys from the configuration and set them on the object.
  1068. * I the configuration is a tree, it will traverse into the tree.
  1069. * @param object the object to extend
  1070. * @param config the configuration object that will extend the object
  1071. */
  1072. protected _extendClassWithConfig(object: any, config: any): void;
  1073. }
  1074. export class DefaultViewer extends AbstractViewer {
  1075. containerElement: HTMLElement;
  1076. /**
  1077. * Create a new default viewer
  1078. * @param containerElement the element in which the templates will be rendered
  1079. * @param initialConfiguration the initial configuration. Defaults to extending the default configuration
  1080. */
  1081. constructor(containerElement: HTMLElement, initialConfiguration?: ViewerConfiguration);
  1082. /**
  1083. * Overriding the AbstractViewer's _initScene fcuntion
  1084. */
  1085. protected _initScene(): Promise<BABYLON.Scene>;
  1086. /**
  1087. * This will be executed when the templates initialize.
  1088. */
  1089. protected _onTemplatesLoaded(): Promise<AbstractViewer>;
  1090. private _initNavbar();
  1091. /**
  1092. * Preparing the container element to present the viewer
  1093. */
  1094. protected _prepareContainerElement(): void;
  1095. /**
  1096. * This function will configure the templates and update them after a model was loaded
  1097. * It is mainly responsible to changing the title and subtitle etc'.
  1098. * @param model the model to be used to configure the templates by
  1099. */
  1100. protected _configureTemplate(model: ViewerModel): void;
  1101. /**
  1102. * This will load a new model to the default viewer
  1103. * overriding the AbstractViewer's loadModel.
  1104. * The scene will automatically be cleared of the old models, if exist.
  1105. * @param model the configuration object (or URL) to load.
  1106. */
  1107. loadModel(model?: any): Promise<ViewerModel>;
  1108. private _onModelLoaded;
  1109. /**
  1110. * Show the overlay and the defined sub-screen.
  1111. * Mainly used for help and errors
  1112. * @param subScreen the name of the subScreen. Those can be defined in the configuration object
  1113. */
  1114. showOverlayScreen(subScreen: string): Promise<string> | Promise<Template>;
  1115. /**
  1116. * Hide the overlay screen.
  1117. */
  1118. hideOverlayScreen(): Promise<string> | Promise<Template>;
  1119. /**
  1120. * Show the loading screen.
  1121. * The loading screen can be configured using the configuration object
  1122. */
  1123. showLoadingScreen(): Promise<string> | Promise<Template>;
  1124. /**
  1125. * Hide the loading screen
  1126. */
  1127. hideLoadingScreen(): Promise<string> | Promise<Template>;
  1128. /**
  1129. * An extension of the light configuration of the abstract viewer.
  1130. * @param lightsConfiguration the light configuration to use
  1131. * @param model the model that will be used to configure the lights (if the lights are model-dependant)
  1132. */
  1133. protected _configureLights(lightsConfiguration: {
  1134. [name: string]: boolean | ILightConfiguration;
  1135. } | undefined, model: ViewerModel): void;
  1136. }
  1137. }