TextureUniform.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import defaultValue from "../../Core/defaultValue.js";
  2. import defined from "../../Core/defined.js";
  3. import DeveloperError from "../../Core/DeveloperError.js";
  4. import Resource from "../../Core/Resource.js";
  5. import PixelFormat from "../../Core/PixelFormat.js";
  6. import PixelDatatype from "../../Renderer/PixelDatatype.js";
  7. import Sampler from "../../Renderer/Sampler.js";
  8. import TextureWrap from "../../Renderer/TextureWrap.js";
  9. /**
  10. * A simple struct that serves as a value of a <code>sampler2D</code>-valued
  11. * uniform. This is used with {@link CustomShader} and {@link TextureManager}
  12. *
  13. * @param {Object} options An object with the following properties:
  14. * @param {Uint8Array} [options.typedArray] A typed array storing the contents of a texture. Values are stored in row-major order. Since WebGL uses a y-up convention for textures, rows are listed from bottom to top.
  15. * @param {Number} [options.width] The width of the image. Required when options.typedArray is present
  16. * @param {Number} [options.height] The height of the image. Required when options.typedArray is present.
  17. * @param {String|Resource} [options.url] A URL string or resource pointing to a texture image.
  18. * @param {Boolean} [options.repeat=true] When defined, the texture sampler will be set to wrap in both directions
  19. * @param {PixelFormat} [options.pixelFormat=PixelFormat.RGBA] When options.typedArray is defined, this is used to determine the pixel format of the texture
  20. * @param {PixelDatatype} [options.pixelDatatype=PixelDatatype.UNSIGNED_BYTE] When options.typedArray is defined, this is the data type of pixel values in the typed array.
  21. * @param {TextureMinificationFilter} [options.minificationFilter=TextureMinificationFilter.LINEAR] The minification filter of the texture sampler.
  22. * @param {TextureMagnificationFilter} [options.magnificationFilter=TextureMagnificationFilter.LINEAR] The magnification filter of the texture sampler.
  23. * @param {Number} [options.maximumAnisotropy=1.0] The maximum anisotropy of the texture sampler
  24. *
  25. * @alias TextureUniform
  26. * @constructor
  27. *
  28. * @experimental This feature is using part of the 3D Tiles spec that is not final and is subject to change without Cesium's standard deprecation policy.
  29. */
  30. export default function TextureUniform(options) {
  31. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  32. //>>includeStart('debug', pragmas.debug);
  33. const hasTypedArray = defined(options.typedArray);
  34. const hasUrl = defined(options.url);
  35. if (hasTypedArray === hasUrl) {
  36. throw new DeveloperError(
  37. "exactly one of options.typedArray, options.url must be defined"
  38. );
  39. }
  40. if (hasTypedArray && (!defined(options.width) || !defined(options.height))) {
  41. throw new DeveloperError(
  42. "options.width and options.height are required when options.typedArray is defined"
  43. );
  44. }
  45. //>>includeEnd('debug');
  46. this.typedArray = options.typedArray;
  47. this.width = options.width;
  48. this.height = options.height;
  49. this.pixelFormat = defaultValue(options.pixelFormat, PixelFormat.RGBA);
  50. this.pixelDatatype = defaultValue(
  51. options.pixelDatatype,
  52. PixelDatatype.UNSIGNED_BYTE
  53. );
  54. let resource = options.url;
  55. if (typeof resource === "string") {
  56. resource = Resource.createIfNeeded(resource);
  57. }
  58. this.resource = resource;
  59. const repeat = defaultValue(options.repeat, true);
  60. const wrap = repeat ? TextureWrap.REPEAT : TextureWrap.CLAMP_TO_EDGE;
  61. this.sampler = new Sampler({
  62. wrapS: wrap,
  63. wrapT: wrap,
  64. minificationFilter: options.minificationFilter,
  65. magnificationFilter: options.magnificationFilter,
  66. maximumAnisotropy: options.maximumAnisotropy,
  67. });
  68. }