智慧水务管理系统 - 精河县供水工程综合管理平台

ImageBasedLighting.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. import Cartesian2 from "../Core/Cartesian2.js";
  2. import Check from "../Core/Check.js";
  3. import defined from "../Core/defined.js";
  4. import Frozen from "../Core/Frozen.js";
  5. import destroyObject from "../Core/destroyObject.js";
  6. import DeveloperError from "../Core/DeveloperError.js";
  7. import SpecularEnvironmentCubeMap from "./SpecularEnvironmentCubeMap.js";
  8. /**
  9. * Properties for managing image-based lighting on tilesets and models.
  10. * Also manages the necessary resources and textures.
  11. * <p>
  12. * If specular environment maps are used, {@link ImageBasedLighting#destroy} must be called
  13. * when the image-based lighting is no longer needed to clean up GPU resources properly.
  14. * If a model or tileset creates an instance of ImageBasedLighting, it will handle this.
  15. * Otherwise, the application is responsible for calling destroy().
  16. *</p>
  17. *
  18. * @alias ImageBasedLighting
  19. * @constructor
  20. *
  21. * @param {Cartesian2} [options.imageBasedLightingFactor=Cartesian2(1.0, 1.0)] Scales diffuse and specular image-based lighting from the earth, sky, atmosphere and star skybox.
  22. * @param {Cartesian3[]} [options.sphericalHarmonicCoefficients] The third order spherical harmonic coefficients used for the diffuse color of image-based lighting.
  23. * @param {string} [options.specularEnvironmentMaps] A URL to a KTX2 file that contains a cube map of the specular lighting and the convoluted specular mipmaps.
  24. */
  25. function ImageBasedLighting(options) {
  26. options = options ?? Frozen.EMPTY_OBJECT;
  27. const imageBasedLightingFactor = defined(options.imageBasedLightingFactor)
  28. ? Cartesian2.clone(options.imageBasedLightingFactor)
  29. : new Cartesian2(1.0, 1.0);
  30. //>>includeStart('debug', pragmas.debug);
  31. Check.typeOf.object(
  32. "options.imageBasedLightingFactor",
  33. imageBasedLightingFactor,
  34. );
  35. Check.typeOf.number.greaterThanOrEquals(
  36. "options.imageBasedLightingFactor.x",
  37. imageBasedLightingFactor.x,
  38. 0.0,
  39. );
  40. Check.typeOf.number.lessThanOrEquals(
  41. "options.imageBasedLightingFactor.x",
  42. imageBasedLightingFactor.x,
  43. 1.0,
  44. );
  45. Check.typeOf.number.greaterThanOrEquals(
  46. "options.imageBasedLightingFactor.y",
  47. imageBasedLightingFactor.y,
  48. 0.0,
  49. );
  50. Check.typeOf.number.lessThanOrEquals(
  51. "options.imageBasedLightingFactor.y",
  52. imageBasedLightingFactor.y,
  53. 1.0,
  54. );
  55. //>>includeEnd('debug');
  56. this._imageBasedLightingFactor = imageBasedLightingFactor;
  57. const sphericalHarmonicCoefficients = options.sphericalHarmonicCoefficients;
  58. //>>includeStart('debug', pragmas.debug);
  59. if (
  60. defined(sphericalHarmonicCoefficients) &&
  61. (!Array.isArray(sphericalHarmonicCoefficients) ||
  62. sphericalHarmonicCoefficients.length !== 9)
  63. ) {
  64. throw new DeveloperError(
  65. "options.sphericalHarmonicCoefficients must be an array of 9 Cartesian3 values.",
  66. );
  67. }
  68. //>>includeEnd('debug');
  69. this._sphericalHarmonicCoefficients = sphericalHarmonicCoefficients;
  70. // The specular environment map texture is created in update();
  71. this._specularEnvironmentMaps = options.specularEnvironmentMaps;
  72. this._specularEnvironmentCubeMap = undefined;
  73. this._specularEnvironmentCubeMapDirty = true;
  74. this._specularEnvironmentMapLoaded = false;
  75. this._previousSpecularEnvironmentMapLoaded = false;
  76. this._useDefaultSpecularMaps = false;
  77. this._useDefaultSphericalHarmonics = false;
  78. this._shouldRegenerateShaders = false;
  79. // Store the previous frame number to prevent redundant update calls
  80. this._previousFrameNumber = undefined;
  81. this._previousFrameContext = undefined;
  82. // Keeps track of the last values for use during update logic
  83. this._previousImageBasedLightingFactor = Cartesian2.clone(
  84. imageBasedLightingFactor,
  85. );
  86. this._previousSphericalHarmonicCoefficients = sphericalHarmonicCoefficients;
  87. this._removeErrorListener = undefined;
  88. }
  89. Object.defineProperties(ImageBasedLighting.prototype, {
  90. /**
  91. * Cesium adds lighting from the earth, sky, atmosphere, and star skybox.
  92. * This cartesian is used to scale the final diffuse and specular lighting
  93. * contribution from those sources to the final color. A value of 0.0 will
  94. * disable those light sources.
  95. *
  96. * @memberof ImageBasedLighting.prototype
  97. *
  98. * @type {Cartesian2}
  99. * @default Cartesian2(1.0, 1.0)
  100. */
  101. imageBasedLightingFactor: {
  102. get: function () {
  103. return this._imageBasedLightingFactor;
  104. },
  105. set: function (value) {
  106. //>>includeStart('debug', pragmas.debug);
  107. Check.typeOf.object("imageBasedLightingFactor", value);
  108. Check.typeOf.number.greaterThanOrEquals(
  109. "imageBasedLightingFactor.x",
  110. value.x,
  111. 0.0,
  112. );
  113. Check.typeOf.number.lessThanOrEquals(
  114. "imageBasedLightingFactor.x",
  115. value.x,
  116. 1.0,
  117. );
  118. Check.typeOf.number.greaterThanOrEquals(
  119. "imageBasedLightingFactor.y",
  120. value.y,
  121. 0.0,
  122. );
  123. Check.typeOf.number.lessThanOrEquals(
  124. "imageBasedLightingFactor.y",
  125. value.y,
  126. 1.0,
  127. );
  128. //>>includeEnd('debug');
  129. this._previousImageBasedLightingFactor = Cartesian2.clone(
  130. this._imageBasedLightingFactor,
  131. this._previousImageBasedLightingFactor,
  132. );
  133. this._imageBasedLightingFactor = Cartesian2.clone(
  134. value,
  135. this._imageBasedLightingFactor,
  136. );
  137. },
  138. },
  139. /**
  140. * The third order spherical harmonic coefficients used for the diffuse color of image-based lighting. When <code>undefined</code>, a diffuse irradiance
  141. * computed from the atmosphere color is used.
  142. * <p>
  143. * There are nine <code>Cartesian3</code> coefficients.
  144. * The order of the coefficients is: L<sub>0,0</sub>, L<sub>1,-1</sub>, L<sub>1,0</sub>, L<sub>1,1</sub>, L<sub>2,-2</sub>, L<sub>2,-1</sub>, L<sub>2,0</sub>, L<sub>2,1</sub>, L<sub>2,2</sub>
  145. * </p>
  146. *
  147. * These values can be obtained by preprocessing the environment map using the <code>cmgen</code> tool of
  148. * {@link https://github.com/google/filament/releases|Google's Filament project}.
  149. * Be sure to use the <code>--no-mirror</code> option in <code>cmgen</code>.
  150. *
  151. * @memberof ImageBasedLighting.prototype
  152. *
  153. * @type {Cartesian3[]}
  154. * @demo {@link https://sandcastle.cesium.com/index.html?id=image-based-lighting|Sandcastle Image Based Lighting Demo}
  155. * @see {@link https://graphics.stanford.edu/papers/envmap/envmap.pdf|An Efficient Representation for Irradiance Environment Maps}
  156. */
  157. sphericalHarmonicCoefficients: {
  158. get: function () {
  159. return this._sphericalHarmonicCoefficients;
  160. },
  161. set: function (value) {
  162. //>>includeStart('debug', pragmas.debug);
  163. if (defined(value) && (!Array.isArray(value) || value.length !== 9)) {
  164. throw new DeveloperError(
  165. "sphericalHarmonicCoefficients must be an array of 9 Cartesian3 values.",
  166. );
  167. }
  168. //>>includeEnd('debug');
  169. this._previousSphericalHarmonicCoefficients =
  170. this._sphericalHarmonicCoefficients;
  171. this._sphericalHarmonicCoefficients = value;
  172. },
  173. },
  174. /**
  175. * A URL to a KTX2 file that contains a cube map of the specular lighting and the convoluted specular mipmaps.
  176. *
  177. * @memberof ImageBasedLighting.prototype
  178. * @demo {@link https://sandcastle.cesium.com/index.html?id=image-based-lighting|Sandcastle Image Based Lighting Demo}
  179. * @type {string}
  180. * @see ImageBasedLighting#sphericalHarmonicCoefficients
  181. */
  182. specularEnvironmentMaps: {
  183. get: function () {
  184. return this._specularEnvironmentMaps;
  185. },
  186. set: function (value) {
  187. if (value !== this._specularEnvironmentMaps) {
  188. this._specularEnvironmentCubeMapDirty =
  189. this._specularEnvironmentCubeMapDirty ||
  190. value !== this._specularEnvironmentMaps;
  191. this._specularEnvironmentMapLoaded = false;
  192. }
  193. this._specularEnvironmentMaps = value;
  194. },
  195. },
  196. /**
  197. * Whether or not image-based lighting is enabled.
  198. *
  199. * @memberof ImageBasedLighting.prototype
  200. * @type {boolean}
  201. *
  202. * @private
  203. */
  204. enabled: {
  205. get: function () {
  206. return (
  207. this._imageBasedLightingFactor.x > 0.0 ||
  208. this._imageBasedLightingFactor.y > 0.0
  209. );
  210. },
  211. },
  212. /**
  213. * Whether or not the models that use this lighting should regenerate their shaders,
  214. * based on the properties and resources have changed.
  215. *
  216. * @memberof ImageBasedLighting.prototype
  217. * @type {boolean}
  218. *
  219. * @private
  220. */
  221. shouldRegenerateShaders: {
  222. get: function () {
  223. return this._shouldRegenerateShaders;
  224. },
  225. },
  226. /**
  227. * The texture atlas for the specular environment maps.
  228. *
  229. * @memberof ImageBasedLighting.prototype
  230. * @type {SpecularEnvironmentCubeMap}
  231. *
  232. * @private
  233. */
  234. specularEnvironmentCubeMap: {
  235. get: function () {
  236. return this._specularEnvironmentCubeMap;
  237. },
  238. },
  239. /**
  240. * Whether or not to use the default spherical harmonics coefficients.
  241. *
  242. * @memberof ImageBasedLighting.prototype
  243. * @type {boolean}
  244. *
  245. * @private
  246. */
  247. useDefaultSphericalHarmonics: {
  248. get: function () {
  249. return this._useDefaultSphericalHarmonics;
  250. },
  251. },
  252. /**
  253. * Whether or not to use the default specular environment maps.
  254. *
  255. * @memberof ImageBasedLighting.prototype
  256. * @type {boolean}
  257. *
  258. * @private
  259. */
  260. useDefaultSpecularMaps: {
  261. get: function () {
  262. return this._useDefaultSpecularMaps;
  263. },
  264. },
  265. /**
  266. * Whether or not the image-based lighting settings use specular environment maps.
  267. *
  268. * @memberof ImageBasedLighting.prototype
  269. * @type {boolean}
  270. *
  271. * @private
  272. */
  273. useSpecularEnvironmentMaps: {
  274. get: function () {
  275. return (
  276. (defined(this._specularEnvironmentCubeMap) &&
  277. this._specularEnvironmentCubeMap.ready) ||
  278. this._useDefaultSpecularMaps
  279. );
  280. },
  281. },
  282. });
  283. function createSpecularEnvironmentCubeMap(imageBasedLighting, context) {
  284. if (!SpecularEnvironmentCubeMap.isSupported(context)) {
  285. return;
  286. }
  287. imageBasedLighting._specularEnvironmentCubeMap =
  288. imageBasedLighting._specularEnvironmentCubeMap &&
  289. imageBasedLighting._specularEnvironmentCubeMap.destroy();
  290. if (defined(imageBasedLighting._specularEnvironmentMaps)) {
  291. const cubeMap = new SpecularEnvironmentCubeMap(
  292. imageBasedLighting._specularEnvironmentMaps,
  293. );
  294. imageBasedLighting._specularEnvironmentCubeMap = cubeMap;
  295. imageBasedLighting._removeErrorListener =
  296. cubeMap.errorEvent.addEventListener((error) => {
  297. console.error(`Error loading specularEnvironmentMaps: ${error}`);
  298. });
  299. }
  300. // Regenerate shaders so they do not use an environment map.
  301. // Will be set to true again if there was a new environment map and it is ready.
  302. imageBasedLighting._shouldRegenerateShaders = true;
  303. }
  304. ImageBasedLighting.prototype.update = function (frameState) {
  305. if (
  306. frameState.frameNumber === this._previousFrameNumber &&
  307. frameState.context === this._previousFrameContext
  308. ) {
  309. return;
  310. }
  311. this._previousFrameNumber = frameState.frameNumber;
  312. const context = (this._previousFrameContext = frameState.context);
  313. frameState.brdfLutGenerator.update(frameState);
  314. this._shouldRegenerateShaders = false;
  315. const iblFactor = this._imageBasedLightingFactor;
  316. const previousIBLFactor = this._previousImageBasedLightingFactor;
  317. if (!Cartesian2.equals(iblFactor, previousIBLFactor)) {
  318. this._shouldRegenerateShaders =
  319. (iblFactor.x > 0.0 && previousIBLFactor.x === 0.0) ||
  320. (iblFactor.x === 0.0 && previousIBLFactor.x > 0.0);
  321. this._shouldRegenerateShaders =
  322. this._shouldRegenerateShaders ||
  323. (iblFactor.y > 0.0 && previousIBLFactor.y === 0.0) ||
  324. (iblFactor.y === 0.0 && previousIBLFactor.y > 0.0);
  325. this._previousImageBasedLightingFactor = Cartesian2.clone(
  326. this._imageBasedLightingFactor,
  327. this._previousImageBasedLightingFactor,
  328. );
  329. }
  330. if (
  331. this._previousSphericalHarmonicCoefficients !==
  332. this._sphericalHarmonicCoefficients
  333. ) {
  334. this._shouldRegenerateShaders =
  335. this._shouldRegenerateShaders ||
  336. defined(this._previousSphericalHarmonicCoefficients) !==
  337. defined(this._sphericalHarmonicCoefficients);
  338. this._previousSphericalHarmonicCoefficients =
  339. this._sphericalHarmonicCoefficients;
  340. }
  341. this._shouldRegenerateShaders =
  342. this._shouldRegenerateShaders ||
  343. this._previousSpecularEnvironmentMapLoaded !==
  344. this._specularEnvironmentMapLoaded;
  345. this._previousSpecularEnvironmentMapLoaded =
  346. this._specularEnvironmentMapLoaded;
  347. if (this._specularEnvironmentCubeMapDirty) {
  348. createSpecularEnvironmentCubeMap(this, context);
  349. this._specularEnvironmentCubeMapDirty = false;
  350. }
  351. if (defined(this._specularEnvironmentCubeMap)) {
  352. this._specularEnvironmentCubeMap.update(frameState);
  353. if (this._specularEnvironmentCubeMap.ready) {
  354. this._specularEnvironmentMapLoaded = true;
  355. }
  356. }
  357. const recompileWithDefaultCubeMap =
  358. !defined(this._specularEnvironmentCubeMap) &&
  359. defined(frameState.specularEnvironmentMaps) &&
  360. !this._useDefaultSpecularMaps;
  361. const recompileWithoutDefaultCubeMap =
  362. !defined(frameState.specularEnvironmentMaps) &&
  363. this._useDefaultSpecularMaps;
  364. const recompileWithDefaultSHCoeffs =
  365. !defined(this._sphericalHarmonicCoefficients) &&
  366. defined(frameState.sphericalHarmonicCoefficients) &&
  367. !this._useDefaultSphericalHarmonics;
  368. const recompileWithoutDefaultSHCoeffs =
  369. !defined(frameState.sphericalHarmonicCoefficients) &&
  370. this._useDefaultSphericalHarmonics;
  371. this._shouldRegenerateShaders =
  372. this._shouldRegenerateShaders ||
  373. recompileWithDefaultCubeMap ||
  374. recompileWithoutDefaultCubeMap ||
  375. recompileWithDefaultSHCoeffs ||
  376. recompileWithoutDefaultSHCoeffs;
  377. this._useDefaultSpecularMaps =
  378. !defined(this._specularEnvironmentCubeMap) &&
  379. defined(frameState.specularEnvironmentMaps);
  380. this._useDefaultSphericalHarmonics =
  381. !defined(this._sphericalHarmonicCoefficients) &&
  382. defined(frameState.sphericalHarmonicCoefficients);
  383. };
  384. /**
  385. * Returns true if this object was destroyed; otherwise, false.
  386. * <br /><br />
  387. * If this object was destroyed, it should not be used; calling any function other than
  388. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  389. *
  390. * @returns {boolean} True if this object was destroyed; otherwise, false.
  391. *
  392. * @see ImageBasedLighting#destroy
  393. * @private
  394. */
  395. ImageBasedLighting.prototype.isDestroyed = function () {
  396. return false;
  397. };
  398. /**
  399. * Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
  400. * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
  401. * <br /><br />
  402. * Once an object is destroyed, it should not be used; calling any function other than
  403. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  404. * assign the return value (<code>undefined</code>) to the object as done in the example.
  405. *
  406. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  407. *
  408. * @example
  409. * imageBasedLighting = imageBasedLighting && imageBasedLighting.destroy();
  410. *
  411. * @see ImageBasedLighting#isDestroyed
  412. * @private
  413. */
  414. ImageBasedLighting.prototype.destroy = function () {
  415. this._specularEnvironmentCubeMap =
  416. this._specularEnvironmentCubeMap &&
  417. this._specularEnvironmentCubeMap.destroy();
  418. this._removeErrorListener =
  419. this._removeErrorListener && this._removeErrorListener();
  420. return destroyObject(this);
  421. };
  422. export default ImageBasedLighting;