loadCubeMap.js 3.7 KB

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