textureDome.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import { Scene } from "../scene";
  2. import { TransformNode } from "../Meshes/transformNode";
  3. import { Mesh } from "../Meshes/mesh";
  4. import { Texture } from "../Materials/Textures/texture";
  5. import { BackgroundMaterial } from "../Materials/Background/backgroundMaterial";
  6. import "../Meshes/Builders/sphereBuilder";
  7. import { Nullable } from "../types";
  8. import { Observer, Observable } from "../Misc/observable";
  9. import { Vector3 } from "../Maths/math.vector";
  10. import { Axis } from "../Maths/math";
  11. import { SphereBuilder } from "../Meshes/Builders/sphereBuilder";
  12. declare type Camera = import("../Cameras/camera").Camera;
  13. /**
  14. * Display a 360/180 degree texture on an approximately spherical surface, useful for VR applications or skyboxes.
  15. * As a subclass of TransformNode, this allow parenting to the camera or multiple textures with different locations in the scene.
  16. * This class achieves its effect with a Texture and a correctly configured BackgroundMaterial on an inverted sphere.
  17. * Potential additions to this helper include zoom and and non-infinite distance rendering effects.
  18. */
  19. export abstract class TextureDome<T extends Texture> extends TransformNode {
  20. /**
  21. * Define the source as a Monoscopic panoramic 360/180.
  22. */
  23. public static readonly MODE_MONOSCOPIC = 0;
  24. /**
  25. * Define the source as a Stereoscopic TopBottom/OverUnder panoramic 360/180.
  26. */
  27. public static readonly MODE_TOPBOTTOM = 1;
  28. /**
  29. * Define the source as a Stereoscopic Side by Side panoramic 360/180.
  30. */
  31. public static readonly MODE_SIDEBYSIDE = 2;
  32. private _halfDome: boolean = false;
  33. protected _useDirectMapping = false;
  34. /**
  35. * The texture being displayed on the sphere
  36. */
  37. protected _texture: T;
  38. /**
  39. * Gets the texture being displayed on the sphere
  40. */
  41. public get texture(): T {
  42. return this._texture;
  43. }
  44. /**
  45. * Sets the texture being displayed on the sphere
  46. */
  47. public set texture(newTexture: T) {
  48. if (this._texture === newTexture) {
  49. return;
  50. }
  51. this._texture = newTexture;
  52. if (this._useDirectMapping) {
  53. this._texture.wrapU = Texture.CLAMP_ADDRESSMODE;
  54. this._texture.wrapV = Texture.CLAMP_ADDRESSMODE;
  55. this._material.diffuseTexture = this._texture;
  56. } else {
  57. this._texture.coordinatesMode = Texture.FIXED_EQUIRECTANGULAR_MIRRORED_MODE; // matches orientation
  58. this._texture.wrapV = Texture.CLAMP_ADDRESSMODE;
  59. this._material.reflectionTexture = this._texture;
  60. }
  61. }
  62. /**
  63. * The skybox material
  64. */
  65. protected _material: BackgroundMaterial;
  66. /**
  67. * The surface used for the dome
  68. */
  69. protected _mesh: Mesh;
  70. /**
  71. * Gets the mesh used for the dome.
  72. */
  73. public get mesh(): Mesh {
  74. return this._mesh;
  75. }
  76. /**
  77. * A mesh that will be used to mask the back of the dome in case it is a 180 degree movie.
  78. */
  79. private _halfDomeMask: Mesh;
  80. /**
  81. * The current fov(field of view) multiplier, 0.0 - 2.0. Defaults to 1.0. Lower values "zoom in" and higher values "zoom out".
  82. * Also see the options.resolution property.
  83. */
  84. public get fovMultiplier(): number {
  85. return this._material.fovMultiplier;
  86. }
  87. public set fovMultiplier(value: number) {
  88. this._material.fovMultiplier = value;
  89. }
  90. protected _textureMode = TextureDome.MODE_MONOSCOPIC;
  91. /**
  92. * Gets or set the current texture mode for the texture. It can be:
  93. * * TextureDome.MODE_MONOSCOPIC : Define the texture source as a Monoscopic panoramic 360.
  94. * * TextureDome.MODE_TOPBOTTOM : Define the texture source as a Stereoscopic TopBottom/OverUnder panoramic 360.
  95. * * TextureDome.MODE_SIDEBYSIDE : Define the texture source as a Stereoscopic Side by Side panoramic 360.
  96. */
  97. public get textureMode(): number {
  98. return this._textureMode;
  99. }
  100. /**
  101. * Sets the current texture mode for the texture. It can be:
  102. * * TextureDome.MODE_MONOSCOPIC : Define the texture source as a Monoscopic panoramic 360.
  103. * * TextureDome.MODE_TOPBOTTOM : Define the texture source as a Stereoscopic TopBottom/OverUnder panoramic 360.
  104. * * TextureDome.MODE_SIDEBYSIDE : Define the texture source as a Stereoscopic Side by Side panoramic 360.
  105. */
  106. public set textureMode(value: number) {
  107. if (this._textureMode === value) {
  108. return;
  109. }
  110. this._changeTextureMode(value);
  111. }
  112. /**
  113. * Is it a 180 degrees dome (half dome) or 360 texture (full dome)
  114. */
  115. public get halfDome(): boolean {
  116. return this._halfDome;
  117. }
  118. /**
  119. * Set the halfDome mode. If set, only the front (180 degrees) will be displayed and the back will be blacked out.
  120. */
  121. public set halfDome(enabled: boolean) {
  122. this._halfDome = enabled;
  123. this._halfDomeMask.setEnabled(enabled);
  124. }
  125. /**
  126. * Oberserver used in Stereoscopic VR Mode.
  127. */
  128. private _onBeforeCameraRenderObserver: Nullable<Observer<Camera>> = null;
  129. /**
  130. * Observable raised when an error occured while loading the 360 image
  131. */
  132. public onLoadErrorObservable = new Observable<string>();
  133. /**
  134. * Create an instance of this class and pass through the parameters to the relevant classes- Texture, StandardMaterial, and Mesh.
  135. * @param name Element's name, child elements will append suffixes for their own names.
  136. * @param textureUrlOrElement defines the url(s) or the (video) HTML element to use
  137. * @param options An object containing optional or exposed sub element properties
  138. */
  139. constructor(
  140. name: string,
  141. textureUrlOrElement: string | string[] | HTMLVideoElement,
  142. options: {
  143. resolution?: number;
  144. clickToPlay?: boolean;
  145. autoPlay?: boolean;
  146. loop?: boolean;
  147. size?: number;
  148. poster?: string;
  149. faceForward?: boolean;
  150. useDirectMapping?: boolean;
  151. halfDomeMode?: boolean;
  152. },
  153. scene: Scene,
  154. protected onError: Nullable<(message?: string, exception?: any) => void> = null
  155. ) {
  156. super(name, scene);
  157. scene = this.getScene();
  158. // set defaults and manage values
  159. name = name || "textureDome";
  160. options.resolution = Math.abs(options.resolution as any) | 0 || 32;
  161. options.clickToPlay = Boolean(options.clickToPlay);
  162. options.autoPlay = options.autoPlay === undefined ? true : Boolean(options.autoPlay);
  163. options.loop = options.loop === undefined ? true : Boolean(options.loop);
  164. options.size = Math.abs(options.size as any) || (scene.activeCamera ? scene.activeCamera.maxZ * 0.48 : 1000);
  165. if (options.useDirectMapping === undefined) {
  166. this._useDirectMapping = true;
  167. } else {
  168. this._useDirectMapping = options.useDirectMapping;
  169. }
  170. if (options.faceForward === undefined) {
  171. options.faceForward = true;
  172. }
  173. this._setReady(false);
  174. this._mesh = Mesh.CreateSphere(name + "_mesh", options.resolution, options.size, scene, false, Mesh.BACKSIDE);
  175. // configure material
  176. let material = (this._material = new BackgroundMaterial(name + "_material", scene));
  177. material.useEquirectangularFOV = true;
  178. material.fovMultiplier = 1.0;
  179. material.opacityFresnel = false;
  180. const texture = this._initTexture(textureUrlOrElement, scene, options);
  181. this.texture = texture;
  182. // configure mesh
  183. this._mesh.material = material;
  184. this._mesh.parent = this;
  185. // create a (disabled until needed) mask to cover unneeded segments of 180 texture.
  186. this._halfDomeMask = SphereBuilder.CreateSphere("", { slice: 0.5, diameter: options.size * 0.98, segments: options.resolution * 2, sideOrientation: Mesh.BACKSIDE }, scene);
  187. this._halfDomeMask.rotate(Axis.X, -Math.PI / 2);
  188. // set the parent, so it will always be positioned correctly AND will be disposed when the main sphere is disposed
  189. this._halfDomeMask.parent = this._mesh;
  190. this._halfDome = !!options.halfDomeMode;
  191. // enable or disable according to the settings
  192. this._halfDomeMask.setEnabled(this._halfDome);
  193. // create
  194. this._texture.anisotropicFilteringLevel = 1;
  195. this._texture.onLoadObservable.addOnce(() => {
  196. this._setReady(true);
  197. });
  198. // Initial rotation
  199. if (options.faceForward && scene.activeCamera) {
  200. let camera = scene.activeCamera;
  201. let forward = Vector3.Forward();
  202. var direction = Vector3.TransformNormal(forward, camera.getViewMatrix());
  203. direction.normalize();
  204. this.rotation.y = Math.acos(Vector3.Dot(forward, direction));
  205. }
  206. this._changeTextureMode(this._textureMode);
  207. }
  208. protected abstract _initTexture(urlsOrElement: string | string[] | HTMLElement, scene: Scene, options: any): T;
  209. protected _changeTextureMode(value: number): void {
  210. this._scene.onBeforeCameraRenderObservable.remove(this._onBeforeCameraRenderObserver);
  211. this._textureMode = value;
  212. // Default Setup and Reset.
  213. this._texture.uScale = 1;
  214. this._texture.vScale = 1;
  215. this._texture.uOffset = 0;
  216. this._texture.vOffset = 0;
  217. switch (value) {
  218. case TextureDome.MODE_MONOSCOPIC:
  219. if (this._halfDome) {
  220. this._texture.uScale = 2;
  221. this._texture.uOffset = -1;
  222. }
  223. break;
  224. case TextureDome.MODE_SIDEBYSIDE:
  225. // in half-dome mode the uScale should be double of 360 texture
  226. // Use 0.99999 to boost perf by not switching program
  227. this._texture.uScale = this._halfDome ? 0.99999 : 0.5;
  228. const rightOffset = this._halfDome ? 0.0 : 0.5;
  229. const leftOffset = this._halfDome ? 0.5 : 0.0;
  230. this._onBeforeCameraRenderObserver = this._scene.onBeforeCameraRenderObservable.add((camera) => {
  231. this._texture.uOffset = camera.isRightCamera ? rightOffset : leftOffset;
  232. });
  233. break;
  234. case TextureDome.MODE_TOPBOTTOM:
  235. // in half-dome mode the vScale should be double of 360 texture
  236. // Use 0.99999 to boost perf by not switching program
  237. this._texture.vScale = this._halfDome ? 0.99999 : 0.5;
  238. this._onBeforeCameraRenderObserver = this._scene.onBeforeCameraRenderObservable.add((camera) => {
  239. this._texture.vOffset = camera.isRightCamera ? 0.5 : 0.0;
  240. });
  241. break;
  242. }
  243. }
  244. /**
  245. * Releases resources associated with this node.
  246. * @param doNotRecurse Set to true to not recurse into each children (recurse into each children by default)
  247. * @param disposeMaterialAndTextures Set to true to also dispose referenced materials and textures (false by default)
  248. */
  249. public dispose(doNotRecurse?: boolean, disposeMaterialAndTextures = false): void {
  250. this._texture.dispose();
  251. this._mesh.dispose();
  252. this._material.dispose();
  253. this._scene.onBeforeCameraRenderObservable.remove(this._onBeforeCameraRenderObserver);
  254. this.onLoadErrorObservable.clear();
  255. super.dispose(doNotRecurse, disposeMaterialAndTextures);
  256. }
  257. }