loadCubeMap.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. * @exports 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 ((!defined(urls)) ||
  44. (!defined(urls.positiveX)) ||
  45. (!defined(urls.negativeX)) ||
  46. (!defined(urls.positiveY)) ||
  47. (!defined(urls.negativeY)) ||
  48. (!defined(urls.positiveZ)) ||
  49. (!defined(urls.negativeZ))) {
  50. throw new DeveloperError('urls is required and must have positiveX, negativeX, positiveY, negativeY, positiveZ, and negativeZ properties.');
  51. }
  52. //>>includeEnd('debug');
  53. // PERFORMANCE_IDEA: Given the size of some cube maps, we should consider tiling them, which
  54. // would prevent hiccups when uploading, for example, six 4096x4096 textures to the GPU.
  55. //
  56. // Also, it is perhaps acceptable to use the context here in the callbacks, but
  57. // ideally, we would do it in the primitive's update function.
  58. var flipOptions = {
  59. flipY : true,
  60. preferImageBitmap: true
  61. };
  62. var facePromises = [
  63. Resource.createIfNeeded(urls.positiveX).fetchImage(flipOptions),
  64. Resource.createIfNeeded(urls.negativeX).fetchImage(flipOptions),
  65. Resource.createIfNeeded(urls.positiveY).fetchImage(flipOptions),
  66. Resource.createIfNeeded(urls.negativeY).fetchImage(flipOptions),
  67. Resource.createIfNeeded(urls.positiveZ).fetchImage(flipOptions),
  68. Resource.createIfNeeded(urls.negativeZ).fetchImage(flipOptions)
  69. ];
  70. return when.all(facePromises, function(images) {
  71. return new CubeMap({
  72. context : context,
  73. source : {
  74. positiveX : images[0],
  75. negativeX : images[1],
  76. positiveY : images[2],
  77. negativeY : images[3],
  78. positiveZ : images[4],
  79. negativeZ : images[5]
  80. }
  81. });
  82. });
  83. }
  84. export default loadCubeMap;