loadCubeMap.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import Check from "../Core/Check.js";
  2. import defined from "../Core/defined.js";
  3. import DeveloperError from "../Core/DeveloperError.js";
  4. import Resource from "../Core/Resource.js";
  5. import when from "../ThirdParty/when.js";
  6. import CubeMap from "./CubeMap.js";
  7. /**
  8. * Asynchronously loads six images and creates a cube map. Returns a promise that
  9. * will resolve to a {@link CubeMap} once loaded, or reject if any image fails to load.
  10. *
  11. * @function loadCubeMap
  12. *
  13. * @param {Context} context The context to use to create the cube map.
  14. * @param {Object} urls The source URL of each image. See the example below.
  15. * @returns {Promise.<CubeMap>} a promise that will resolve to the requested {@link CubeMap} when loaded.
  16. *
  17. * @exception {DeveloperError} context is required.
  18. * @exception {DeveloperError} urls is required and must have positiveX, negativeX, positiveY, negativeY, positiveZ, and negativeZ properties.
  19. *
  20. *
  21. * @example
  22. * Cesium.loadCubeMap(context, {
  23. * positiveX : 'skybox_px.png',
  24. * negativeX : 'skybox_nx.png',
  25. * positiveY : 'skybox_py.png',
  26. * negativeY : 'skybox_ny.png',
  27. * positiveZ : 'skybox_pz.png',
  28. * negativeZ : 'skybox_nz.png'
  29. * }).then(function(cubeMap) {
  30. * // use the cubemap
  31. * }).otherwise(function(error) {
  32. * // an error occurred
  33. * });
  34. *
  35. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  36. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  37. *
  38. * @private
  39. */
  40. function loadCubeMap(context, urls) {
  41. //>>includeStart('debug', pragmas.debug);
  42. Check.defined("context", context);
  43. if (
  44. !defined(urls) ||
  45. !defined(urls.positiveX) ||
  46. !defined(urls.negativeX) ||
  47. !defined(urls.positiveY) ||
  48. !defined(urls.negativeY) ||
  49. !defined(urls.positiveZ) ||
  50. !defined(urls.negativeZ)
  51. ) {
  52. throw new DeveloperError(
  53. "urls is required and must have positiveX, negativeX, positiveY, negativeY, positiveZ, and negativeZ properties."
  54. );
  55. }
  56. //>>includeEnd('debug');
  57. // PERFORMANCE_IDEA: Given the size of some cube maps, we should consider tiling them, which
  58. // would prevent hiccups when uploading, for example, six 4096x4096 textures to the GPU.
  59. //
  60. // Also, it is perhaps acceptable to use the context here in the callbacks, but
  61. // ideally, we would do it in the primitive's update function.
  62. var flipOptions = {
  63. flipY: true,
  64. preferImageBitmap: true,
  65. };
  66. var facePromises = [
  67. Resource.createIfNeeded(urls.positiveX).fetchImage(flipOptions),
  68. Resource.createIfNeeded(urls.negativeX).fetchImage(flipOptions),
  69. Resource.createIfNeeded(urls.positiveY).fetchImage(flipOptions),
  70. Resource.createIfNeeded(urls.negativeY).fetchImage(flipOptions),
  71. Resource.createIfNeeded(urls.positiveZ).fetchImage(flipOptions),
  72. Resource.createIfNeeded(urls.negativeZ).fetchImage(flipOptions),
  73. ];
  74. return when.all(facePromises, function (images) {
  75. return new CubeMap({
  76. context: context,
  77. source: {
  78. positiveX: images[0],
  79. negativeX: images[1],
  80. positiveY: images[2],
  81. negativeY: images[3],
  82. positiveZ: images[4],
  83. negativeZ: images[5],
  84. },
  85. });
  86. });
  87. }
  88. export default loadCubeMap;