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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. import BoxGeometry from "../Core/BoxGeometry.js";
  2. import Cartesian3 from "../Core/Cartesian3.js";
  3. import Check from "../Core/Check.js";
  4. import Frozen from "../Core/Frozen.js";
  5. import defined from "../Core/defined.js";
  6. import destroyObject from "../Core/destroyObject.js";
  7. import DeveloperError from "../Core/DeveloperError.js";
  8. import GeometryPipeline from "../Core/GeometryPipeline.js";
  9. import CesiumMath from "../Core/Math.js";
  10. import PixelFormat from "../Core/PixelFormat.js";
  11. import VertexFormat from "../Core/VertexFormat.js";
  12. import BufferUsage from "./BufferUsage.js";
  13. import ContextLimits from "./ContextLimits.js";
  14. import CubeMapFace from "./CubeMapFace.js";
  15. import Framebuffer from "./Framebuffer.js";
  16. import MipmapHint from "./MipmapHint.js";
  17. import PixelDatatype from "./PixelDatatype.js";
  18. import Sampler from "./Sampler.js";
  19. import TextureMagnificationFilter from "./TextureMagnificationFilter.js";
  20. import TextureMinificationFilter from "./TextureMinificationFilter.js";
  21. import VertexArray from "./VertexArray.js";
  22. /**
  23. * @typedef CubeMap.BufferSource
  24. *
  25. * @property {TypedArray} arrayBufferView A view of a binary data buffer containing pixel values.
  26. * @property {number} width The width of one face of the cube map, in pixels. Must be equal to height.
  27. * @property {number} height The height of one face of the cube map, in pixels. Must be equal to width.
  28. *
  29. * @private
  30. */
  31. /**
  32. * @typedef CubeMap.Source
  33. *
  34. * @property {ImageData|HTMLImageElement|HTMLCanvasElement|HTMLVideoElement|CubeMap.BufferSource} positiveX
  35. * @property {ImageData|HTMLImageElement|HTMLCanvasElement|HTMLVideoElement|CubeMap.BufferSource} negativeX
  36. * @property {ImageData|HTMLImageElement|HTMLCanvasElement|HTMLVideoElement|CubeMap.BufferSource} positiveY
  37. * @property {ImageData|HTMLImageElement|HTMLCanvasElement|HTMLVideoElement|CubeMap.BufferSource} negativeY
  38. * @property {ImageData|HTMLImageElement|HTMLCanvasElement|HTMLVideoElement|CubeMap.BufferSource} positiveZ
  39. * @property {ImageData|HTMLImageElement|HTMLCanvasElement|HTMLVideoElement|CubeMap.BufferSource} negativeZ
  40. */
  41. /**
  42. * @typedef CubeMap.ConstructorOptions
  43. *
  44. * @property {Context} context
  45. * @property {CubeMap.Source} [source] The source for texel values to be loaded into the texture.
  46. * @property {PixelFormat} [pixelFormat=PixelFormat.RGBA] The format of each pixel, i.e., the number of components it has and what they represent.
  47. * @property {PixelDatatype} [pixelDatatype=PixelDatatype.UNSIGNED_BYTE] The data type of each pixel.
  48. * @property {boolean} [flipY=true] If true, the source values will be read as if the y-axis is inverted (y=0 at the top).
  49. * @property {boolean} [skipColorSpaceConversion=false] If true, color space conversions will be skipped when reading the texel values.
  50. * @property {Sampler} [sampler] Information about how to sample the cubemap texture.
  51. * @property {number} [width] The pixel width of the texture. If not supplied, must be available from the source. Must be equal to height.
  52. * @property {number} [height] The pixel height of the texture. If not supplied, must be available from the source. Must be equal to width.
  53. * @property {boolean} [preMultiplyAlpha] If true, the alpha channel will be multiplied into the other channels.
  54. *
  55. * @private
  56. */
  57. /**
  58. * A wrapper for a {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLTexture|WebGLTexture}
  59. * used as a cube map, to abstract away the verbose GL calls associated with setting up a texture.
  60. *
  61. * @alias CubeMap
  62. * @constructor
  63. *
  64. * @param {CubeMap.ConstructorOptions} options An object describing initialization options.
  65. * @private
  66. */
  67. function CubeMap(options) {
  68. options = options ?? Frozen.EMPTY_OBJECT;
  69. //>>includeStart('debug', pragmas.debug);
  70. Check.defined("options.context", options.context);
  71. //>>includeEnd('debug');
  72. const {
  73. context,
  74. source,
  75. pixelFormat = PixelFormat.RGBA,
  76. pixelDatatype = PixelDatatype.UNSIGNED_BYTE,
  77. flipY = true,
  78. skipColorSpaceConversion = false,
  79. sampler = new Sampler(),
  80. } = options;
  81. // Use premultiplied alpha for opaque textures should perform better on Chrome:
  82. // http://media.tojicode.com/webglCamp4/#20
  83. const preMultiplyAlpha =
  84. options.preMultiplyAlpha ||
  85. pixelFormat === PixelFormat.RGB ||
  86. pixelFormat === PixelFormat.LUMINANCE;
  87. let { width, height } = options;
  88. if (defined(source)) {
  89. //>>includeStart('debug', pragmas.debug);
  90. if (
  91. !Object.values(CubeMap.FaceName).every((faceName) =>
  92. defined(source[faceName]),
  93. )
  94. ) {
  95. throw new DeveloperError(
  96. `options.source requires faces ${Object.values(CubeMap.FaceName).join(
  97. ", ",
  98. )}.`,
  99. );
  100. }
  101. //>>includeEnd('debug');
  102. ({ width, height } = source.positiveX);
  103. //>>includeStart('debug', pragmas.debug);
  104. for (const faceName of CubeMap.faceNames()) {
  105. const face = source[faceName];
  106. if (Number(face.width) !== width || Number(face.height) !== height) {
  107. throw new DeveloperError(
  108. "Each face in options.source must have the same width and height.",
  109. );
  110. }
  111. }
  112. //>>includeEnd('debug');
  113. }
  114. const size = width;
  115. //>>includeStart('debug', pragmas.debug);
  116. if (!defined(width) || !defined(height)) {
  117. throw new DeveloperError(
  118. "options requires a source field to create an initialized cube map or width and height fields to create a blank cube map.",
  119. );
  120. }
  121. if (width !== height) {
  122. throw new DeveloperError("Width must equal height.");
  123. }
  124. if (size <= 0) {
  125. throw new DeveloperError("Width and height must be greater than zero.");
  126. }
  127. if (size > ContextLimits.maximumCubeMapSize) {
  128. throw new DeveloperError(
  129. `Width and height must be less than or equal to the maximum cube map size (${ContextLimits.maximumCubeMapSize}). Check maximumCubeMapSize.`,
  130. );
  131. }
  132. if (!PixelFormat.validate(pixelFormat)) {
  133. throw new DeveloperError("Invalid options.pixelFormat.");
  134. }
  135. if (PixelFormat.isDepthFormat(pixelFormat)) {
  136. throw new DeveloperError(
  137. "options.pixelFormat cannot be DEPTH_COMPONENT or DEPTH_STENCIL.",
  138. );
  139. }
  140. if (!PixelDatatype.validate(pixelDatatype)) {
  141. throw new DeveloperError("Invalid options.pixelDatatype.");
  142. }
  143. if (pixelDatatype === PixelDatatype.FLOAT && !context.floatingPointTexture) {
  144. throw new DeveloperError(
  145. "When options.pixelDatatype is FLOAT, this WebGL implementation must support the OES_texture_float extension.",
  146. );
  147. }
  148. if (
  149. pixelDatatype === PixelDatatype.HALF_FLOAT &&
  150. !context.halfFloatingPointTexture
  151. ) {
  152. throw new DeveloperError(
  153. "When options.pixelDatatype is HALF_FLOAT, this WebGL implementation must support the OES_texture_half_float extension.",
  154. );
  155. }
  156. //>>includeEnd('debug');
  157. const sizeInBytes =
  158. PixelFormat.textureSizeInBytes(pixelFormat, pixelDatatype, size, size) * 6;
  159. const internalFormat = PixelFormat.toInternalFormat(
  160. pixelFormat,
  161. pixelDatatype,
  162. context,
  163. );
  164. const gl = context._gl;
  165. const textureTarget = gl.TEXTURE_CUBE_MAP;
  166. const texture = gl.createTexture();
  167. this._context = context;
  168. this._textureFilterAnisotropic = context._textureFilterAnisotropic;
  169. this._textureTarget = textureTarget;
  170. this._texture = texture;
  171. this._pixelFormat = pixelFormat;
  172. this._pixelDatatype = pixelDatatype;
  173. this._size = size;
  174. this._hasMipmap = false;
  175. this._sizeInBytes = sizeInBytes;
  176. this._preMultiplyAlpha = preMultiplyAlpha;
  177. this._flipY = flipY;
  178. const initialized = defined(source);
  179. function constructFace(targetFace) {
  180. return new CubeMapFace(
  181. context,
  182. texture,
  183. textureTarget,
  184. targetFace,
  185. internalFormat,
  186. pixelFormat,
  187. pixelDatatype,
  188. size,
  189. preMultiplyAlpha,
  190. flipY,
  191. initialized,
  192. );
  193. }
  194. this._positiveX = constructFace(gl.TEXTURE_CUBE_MAP_POSITIVE_X);
  195. this._negativeX = constructFace(gl.TEXTURE_CUBE_MAP_NEGATIVE_X);
  196. this._positiveY = constructFace(gl.TEXTURE_CUBE_MAP_POSITIVE_Y);
  197. this._negativeY = constructFace(gl.TEXTURE_CUBE_MAP_NEGATIVE_Y);
  198. this._positiveZ = constructFace(gl.TEXTURE_CUBE_MAP_POSITIVE_Z);
  199. this._negativeZ = constructFace(gl.TEXTURE_CUBE_MAP_NEGATIVE_Z);
  200. this._sampler = sampler;
  201. setupSampler(this, sampler);
  202. gl.activeTexture(gl.TEXTURE0);
  203. gl.bindTexture(textureTarget, texture);
  204. if (skipColorSpaceConversion) {
  205. gl.pixelStorei(gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.NONE);
  206. } else {
  207. gl.pixelStorei(
  208. gl.UNPACK_COLORSPACE_CONVERSION_WEBGL,
  209. gl.BROWSER_DEFAULT_WEBGL,
  210. );
  211. }
  212. for (const faceName of CubeMap.faceNames()) {
  213. loadFace(this[faceName], source?.[faceName], 0);
  214. }
  215. gl.bindTexture(textureTarget, null);
  216. }
  217. /**
  218. * Copy an existing texture to a cubemap face.
  219. * @param {FrameState} frameState The current rendering frameState
  220. * @param {Texture} texture Texture being copied
  221. * @param {CubeMap.FaceName} face The face to which to copy
  222. * @param {number} [mipLevel=0] The mip level at which to copy
  223. */
  224. CubeMap.prototype.copyFace = function (frameState, texture, face, mipLevel) {
  225. const context = frameState.context;
  226. const framebuffer = new Framebuffer({
  227. context: context,
  228. colorTextures: [texture],
  229. destroyAttachments: false,
  230. });
  231. framebuffer._bind();
  232. this[face].copyMipmapFromFramebuffer(
  233. 0,
  234. 0,
  235. texture.width,
  236. texture.height,
  237. mipLevel ?? 0,
  238. );
  239. framebuffer._unBind();
  240. framebuffer.destroy();
  241. };
  242. /**
  243. * An enum defining the names of the faces of a cube map.
  244. * @alias {CubeMap.FaceName}
  245. * @enum {string}
  246. * @private
  247. */
  248. CubeMap.FaceName = Object.freeze({
  249. POSITIVEX: "positiveX",
  250. NEGATIVEX: "negativeX",
  251. POSITIVEY: "positiveY",
  252. NEGATIVEY: "negativeY",
  253. POSITIVEZ: "positiveZ",
  254. NEGATIVEZ: "negativeZ",
  255. });
  256. function* makeFaceNamesIterator() {
  257. yield CubeMap.FaceName.POSITIVEX;
  258. yield CubeMap.FaceName.NEGATIVEX;
  259. yield CubeMap.FaceName.POSITIVEY;
  260. yield CubeMap.FaceName.NEGATIVEY;
  261. yield CubeMap.FaceName.POSITIVEZ;
  262. yield CubeMap.FaceName.NEGATIVEZ;
  263. }
  264. /**
  265. * Creates an iterator for looping over the cubemap faces.
  266. * @type {Iterable<CubeMap.FaceName>}
  267. * @private
  268. */
  269. CubeMap.faceNames = function () {
  270. return makeFaceNamesIterator();
  271. };
  272. /**
  273. * Load texel data into one face of a cube map.
  274. * @param {CubeMapFace} cubeMapFace The face to which texel values will be loaded.
  275. * @param {ImageData|HTMLImageElement|HTMLCanvasElement|HTMLVideoElement|CubeMap.BufferSource} [source] The source for texel values to be loaded into the texture.
  276. * @param {number} [mipLevel=0] The mip level to which the texel values will be loaded.
  277. * @private
  278. */
  279. function loadFace(cubeMapFace, source, mipLevel) {
  280. mipLevel = mipLevel ?? 0;
  281. const targetFace = cubeMapFace._targetFace;
  282. const size = Math.max(Math.floor(cubeMapFace._size / 2 ** mipLevel), 1);
  283. const pixelFormat = cubeMapFace._pixelFormat;
  284. const pixelDatatype = cubeMapFace._pixelDatatype;
  285. const internalFormat = cubeMapFace._internalFormat;
  286. const flipY = cubeMapFace._flipY;
  287. const preMultiplyAlpha = cubeMapFace._preMultiplyAlpha;
  288. const context = cubeMapFace._context;
  289. const gl = context._gl;
  290. if (!defined(source)) {
  291. gl.texImage2D(
  292. targetFace,
  293. mipLevel,
  294. internalFormat,
  295. size,
  296. size,
  297. 0,
  298. pixelFormat,
  299. PixelDatatype.toWebGLConstant(pixelDatatype, context),
  300. null,
  301. );
  302. return;
  303. }
  304. let { arrayBufferView } = source;
  305. let unpackAlignment = 4;
  306. if (defined(arrayBufferView)) {
  307. unpackAlignment = PixelFormat.alignmentInBytes(
  308. pixelFormat,
  309. pixelDatatype,
  310. size,
  311. );
  312. }
  313. gl.pixelStorei(gl.UNPACK_ALIGNMENT, unpackAlignment);
  314. if (defined(arrayBufferView)) {
  315. gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
  316. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
  317. if (flipY) {
  318. arrayBufferView = PixelFormat.flipY(
  319. arrayBufferView,
  320. pixelFormat,
  321. pixelDatatype,
  322. size,
  323. size,
  324. );
  325. }
  326. gl.texImage2D(
  327. targetFace,
  328. mipLevel,
  329. internalFormat,
  330. size,
  331. size,
  332. 0,
  333. pixelFormat,
  334. PixelDatatype.toWebGLConstant(pixelDatatype, context),
  335. arrayBufferView,
  336. );
  337. } else {
  338. // Only valid for DOM-Element uploads
  339. gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, preMultiplyAlpha);
  340. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY);
  341. gl.texImage2D(
  342. targetFace,
  343. mipLevel,
  344. internalFormat,
  345. pixelFormat,
  346. PixelDatatype.toWebGLConstant(pixelDatatype, context),
  347. source,
  348. );
  349. }
  350. }
  351. CubeMap.loadFace = loadFace;
  352. Object.defineProperties(CubeMap.prototype, {
  353. positiveX: {
  354. get: function () {
  355. return this._positiveX;
  356. },
  357. },
  358. negativeX: {
  359. get: function () {
  360. return this._negativeX;
  361. },
  362. },
  363. positiveY: {
  364. get: function () {
  365. return this._positiveY;
  366. },
  367. },
  368. negativeY: {
  369. get: function () {
  370. return this._negativeY;
  371. },
  372. },
  373. positiveZ: {
  374. get: function () {
  375. return this._positiveZ;
  376. },
  377. },
  378. negativeZ: {
  379. get: function () {
  380. return this._negativeZ;
  381. },
  382. },
  383. sampler: {
  384. get: function () {
  385. return this._sampler;
  386. },
  387. set: function (sampler) {
  388. setupSampler(this, sampler);
  389. this._sampler = sampler;
  390. },
  391. },
  392. pixelFormat: {
  393. get: function () {
  394. return this._pixelFormat;
  395. },
  396. },
  397. pixelDatatype: {
  398. get: function () {
  399. return this._pixelDatatype;
  400. },
  401. },
  402. width: {
  403. get: function () {
  404. return this._size;
  405. },
  406. },
  407. height: {
  408. get: function () {
  409. return this._size;
  410. },
  411. },
  412. sizeInBytes: {
  413. get: function () {
  414. if (this._hasMipmap) {
  415. return Math.floor((this._sizeInBytes * 4) / 3);
  416. }
  417. return this._sizeInBytes;
  418. },
  419. },
  420. preMultiplyAlpha: {
  421. get: function () {
  422. return this._preMultiplyAlpha;
  423. },
  424. },
  425. flipY: {
  426. get: function () {
  427. return this._flipY;
  428. },
  429. },
  430. _target: {
  431. get: function () {
  432. return this._textureTarget;
  433. },
  434. },
  435. });
  436. /**
  437. * Get a vector representing the cubemap face direction
  438. * @param {CubeMap.FaceName} face The relevant face
  439. * @param {Cartesian3} [result] The object onto which to store the result.
  440. * @returns {Cartesian3} The vector representing the cubemap face direction
  441. */
  442. CubeMap.getDirection = function (face, result) {
  443. switch (face) {
  444. case CubeMap.FaceName.POSITIVEX:
  445. return Cartesian3.clone(Cartesian3.UNIT_X, result);
  446. case CubeMap.FaceName.NEGATIVEX:
  447. return Cartesian3.negate(Cartesian3.UNIT_X, result);
  448. case CubeMap.FaceName.POSITIVEY:
  449. return Cartesian3.clone(Cartesian3.UNIT_Y, result);
  450. case CubeMap.FaceName.NEGATIVEY:
  451. return Cartesian3.negate(Cartesian3.UNIT_Y, result);
  452. case CubeMap.FaceName.POSITIVEZ:
  453. return Cartesian3.clone(Cartesian3.UNIT_Z, result);
  454. case CubeMap.FaceName.NEGATIVEZ:
  455. return Cartesian3.negate(Cartesian3.UNIT_Z, result);
  456. }
  457. };
  458. /**
  459. * Set up a sampler for use with a cube map.
  460. * @param {CubeMap} cubeMap The cube map containing the texture to be sampled by this sampler.
  461. * @param {Sampler} sampler Information about how to sample the cubemap texture.
  462. * @private
  463. */
  464. function setupSampler(cubeMap, sampler) {
  465. let { minificationFilter, magnificationFilter } = sampler;
  466. const mipmap = [
  467. TextureMinificationFilter.NEAREST_MIPMAP_NEAREST,
  468. TextureMinificationFilter.NEAREST_MIPMAP_LINEAR,
  469. TextureMinificationFilter.LINEAR_MIPMAP_NEAREST,
  470. TextureMinificationFilter.LINEAR_MIPMAP_LINEAR,
  471. ].includes(minificationFilter);
  472. const context = cubeMap._context;
  473. const pixelDatatype = cubeMap._pixelDatatype;
  474. // float textures only support nearest filtering unless the linear extensions are supported
  475. if (
  476. (pixelDatatype === PixelDatatype.FLOAT && !context.textureFloatLinear) ||
  477. (pixelDatatype === PixelDatatype.HALF_FLOAT &&
  478. !context.textureHalfFloatLinear)
  479. ) {
  480. // override the sampler's settings
  481. minificationFilter = mipmap
  482. ? TextureMinificationFilter.NEAREST_MIPMAP_NEAREST
  483. : TextureMinificationFilter.NEAREST;
  484. magnificationFilter = TextureMagnificationFilter.NEAREST;
  485. }
  486. const gl = context._gl;
  487. const target = cubeMap._textureTarget;
  488. gl.activeTexture(gl.TEXTURE0);
  489. gl.bindTexture(target, cubeMap._texture);
  490. gl.texParameteri(target, gl.TEXTURE_MIN_FILTER, minificationFilter);
  491. gl.texParameteri(target, gl.TEXTURE_MAG_FILTER, magnificationFilter);
  492. gl.texParameteri(target, gl.TEXTURE_WRAP_S, sampler.wrapS);
  493. gl.texParameteri(target, gl.TEXTURE_WRAP_T, sampler.wrapT);
  494. if (defined(cubeMap._textureFilterAnisotropic)) {
  495. gl.texParameteri(
  496. target,
  497. cubeMap._textureFilterAnisotropic.TEXTURE_MAX_ANISOTROPY_EXT,
  498. sampler.maximumAnisotropy,
  499. );
  500. }
  501. gl.bindTexture(target, null);
  502. }
  503. /**
  504. * Load a complete mipmap chain for each cubemap face.
  505. *
  506. * @param {CubeMap.Source[]} source The source data for each mip level, beginning at level 1.
  507. * @param {boolean} [skipColorSpaceConversion=false] If true, color space conversions will be skipped when reading the texel values.
  508. *
  509. * @private
  510. */
  511. CubeMap.prototype.loadMipmaps = function (source, skipColorSpaceConversion) {
  512. //>>includeStart('debug', pragmas.debug);
  513. Check.defined("source", source);
  514. if (!Array.isArray(source)) {
  515. throw new DeveloperError(`source must be an array`);
  516. }
  517. const mipCount = Math.log2(this._size);
  518. if (source.length !== mipCount) {
  519. throw new DeveloperError(`all mip levels must be defined`);
  520. }
  521. //>>includeEnd('debug');
  522. skipColorSpaceConversion = skipColorSpaceConversion ?? false;
  523. const gl = this._context._gl;
  524. const texture = this._texture;
  525. const textureTarget = this._textureTarget;
  526. gl.activeTexture(gl.TEXTURE0);
  527. gl.bindTexture(textureTarget, texture);
  528. if (skipColorSpaceConversion) {
  529. gl.pixelStorei(gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.NONE);
  530. } else {
  531. gl.pixelStorei(
  532. gl.UNPACK_COLORSPACE_CONVERSION_WEBGL,
  533. gl.BROWSER_DEFAULT_WEBGL,
  534. );
  535. }
  536. for (let i = 0; i < source.length; i++) {
  537. const mipSource = source[i];
  538. // mipLevel 0 was the base layer, already loaded when the CubeMap was constructed.
  539. const mipLevel = i + 1;
  540. for (const faceName of CubeMap.faceNames()) {
  541. loadFace(this[faceName], mipSource[faceName], mipLevel);
  542. }
  543. }
  544. gl.bindTexture(textureTarget, null);
  545. this._hasMipmap = true;
  546. };
  547. /**
  548. * Generates a complete mipmap chain for each cubemap face.
  549. *
  550. * @param {MipmapHint} [hint=MipmapHint.DONT_CARE] A performance vs. quality hint.
  551. *
  552. * @exception {DeveloperError} hint is invalid.
  553. * @exception {DeveloperError} This CubeMap's width must be a power of two to call generateMipmap().
  554. * @exception {DeveloperError} This CubeMap's height must be a power of two to call generateMipmap().
  555. * @exception {DeveloperError} This CubeMap was destroyed, i.e., destroy() was called.
  556. *
  557. * @example
  558. * // Generate mipmaps, and then set the sampler so mipmaps are used for
  559. * // minification when the cube map is sampled.
  560. * cubeMap.generateMipmap();
  561. * cubeMap.sampler = new Sampler({
  562. * minificationFilter : Cesium.TextureMinificationFilter.NEAREST_MIPMAP_LINEAR
  563. * });
  564. */
  565. CubeMap.prototype.generateMipmap = function (hint) {
  566. hint = hint ?? MipmapHint.DONT_CARE;
  567. //>>includeStart('debug', pragmas.debug);
  568. if (this._size > 1 && !CesiumMath.isPowerOfTwo(this._size)) {
  569. throw new DeveloperError(
  570. "width and height must be a power of two to call generateMipmap().",
  571. );
  572. }
  573. if (!MipmapHint.validate(hint)) {
  574. throw new DeveloperError("hint is invalid.");
  575. }
  576. //>>includeEnd('debug');
  577. this._hasMipmap = true;
  578. const gl = this._context._gl;
  579. const target = this._textureTarget;
  580. gl.hint(gl.GENERATE_MIPMAP_HINT, hint);
  581. gl.activeTexture(gl.TEXTURE0);
  582. gl.bindTexture(target, this._texture);
  583. gl.generateMipmap(target);
  584. gl.bindTexture(target, null);
  585. };
  586. /**
  587. * Create a vertex array that can be used for cubemap shaders.
  588. * @param {Context} context The rendering context
  589. * @returns {VertexArray} The created vertex array
  590. */
  591. CubeMap.createVertexArray = function (context) {
  592. const geometry = BoxGeometry.createGeometry(
  593. BoxGeometry.fromDimensions({
  594. dimensions: new Cartesian3(2.0, 2.0, 2.0),
  595. vertexFormat: VertexFormat.POSITION_ONLY,
  596. }),
  597. );
  598. const attributeLocations = (this._attributeLocations =
  599. GeometryPipeline.createAttributeLocations(geometry));
  600. return VertexArray.fromGeometry({
  601. context: context,
  602. geometry: geometry,
  603. attributeLocations: attributeLocations,
  604. bufferUsage: BufferUsage.STATIC_DRAW,
  605. });
  606. };
  607. CubeMap.prototype.isDestroyed = function () {
  608. return false;
  609. };
  610. CubeMap.prototype.destroy = function () {
  611. this._context._gl.deleteTexture(this._texture);
  612. this._positiveX = destroyObject(this._positiveX);
  613. this._negativeX = destroyObject(this._negativeX);
  614. this._positiveY = destroyObject(this._positiveY);
  615. this._negativeY = destroyObject(this._negativeY);
  616. this._positiveZ = destroyObject(this._positiveZ);
  617. this._negativeZ = destroyObject(this._negativeZ);
  618. return destroyObject(this);
  619. };
  620. export default CubeMap;