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

Texture3D.js 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. import Cartesian3 from "../Core/Cartesian3.js";
  2. import Check from "../Core/Check.js";
  3. import createGuid from "../Core/createGuid.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 PixelFormat from "../Core/PixelFormat.js";
  9. import ContextLimits from "./ContextLimits.js";
  10. import MipmapHint from "./MipmapHint.js";
  11. import PixelDatatype from "./PixelDatatype.js";
  12. import Sampler from "./Sampler.js";
  13. import TextureMagnificationFilter from "./TextureMagnificationFilter.js";
  14. import TextureMinificationFilter from "./TextureMinificationFilter.js";
  15. /**
  16. * @typedef {object} Texture3D.Source
  17. * @property {number} width The width (in pixels) of the 3D texture source data.
  18. * @property {number} height The height (in pixels) of the 3D texture source data.
  19. * @property {number} depth The depth (in pixels) of the 3D texture source data.
  20. * @property {TypedArray|DataView} arrayBufferView The source data for a 3D texture. The type of each element needs to match the pixelDatatype.
  21. * @property {TypedArray|DataView} [mipLevels] An array of mip level data. Each element in the array should be a TypedArray or DataView that matches the pixelDatatype.
  22. */
  23. /**
  24. * @typedef {object} Texture3D.ConstructorOptions
  25. *
  26. * @property {Context} context
  27. * @property {Texture3D.Source} [source] The source for texel values to be loaded into the 3D texture.
  28. * @property {PixelFormat} [pixelFormat=PixelFormat.RGBA] The format of each pixel, i.e., the number of components it has and what they represent.
  29. * @property {PixelDatatype} [pixelDatatype=PixelDatatype.UNSIGNED_BYTE] The data type of each pixel.
  30. * @property {boolean} [flipY=true] If true, the source values will be read as if the y-axis is inverted (y=0 at the top).
  31. * @property {boolean} [skipColorSpaceConversion=false] If true, color space conversions will be skipped when reading the texel values.
  32. * @property {Sampler} [sampler] Information about how to sample the 3D texture.
  33. * @property {number} [width] The width (in pixels) of the 3D texture. If not supplied, must be available from the source.
  34. * @property {number} [height] The height (in pixels) of the 3D texture. If not supplied, must be available from the source.
  35. * @property {number} [depth] The depth (in pixels) of the 3D texture. If not supplied, must be available from the source.
  36. * @property {boolean} [preMultiplyAlpha] If true, the alpha channel will be multiplied into the other channels.
  37. * @property {string} [id] A unique identifier for the 3D texture. If this is not given, then a GUID will be created.
  38. *
  39. * @private
  40. */
  41. /**
  42. * A wrapper for a {@link https://developer.mozilla.org/en-US/docs/Web/API/WebGLTexture|WebGLTexture}
  43. * to abstract away the verbose GL calls associated with setting up a texture3D.
  44. *
  45. * @alias Texture3D
  46. * @constructor
  47. *
  48. * @param {Texture3D.ConstructorOptions} options
  49. * @private
  50. */
  51. function Texture3D(options) {
  52. options = options ?? Frozen.EMPTY_OBJECT;
  53. //>>includeStart('debug', pragmas.debug);
  54. Check.defined("options.context", options.context);
  55. //>>includeEnd('debug');
  56. const {
  57. context,
  58. source,
  59. pixelFormat = PixelFormat.RGBA,
  60. pixelDatatype = PixelDatatype.UNSIGNED_BYTE,
  61. flipY = true,
  62. skipColorSpaceConversion = false,
  63. sampler = new Sampler(),
  64. } = options;
  65. // 3D textures are not supported in a WebGL1 context. But we allow a stub context for testing.
  66. if (!context.webgl2 && !defined(context.options.getWebGLStub)) {
  67. throw new DeveloperError(
  68. "WebGL1 does not support texture3D. Please use a WebGL2 context.",
  69. );
  70. }
  71. let { width, height, depth } = options;
  72. if (defined(source)) {
  73. // Make sure we are using the element's intrinsic width and height where available
  74. if (!defined(width)) {
  75. width = source.width;
  76. }
  77. if (!defined(height)) {
  78. height = source.height;
  79. }
  80. // depth is not used for 2D textures, but is required for 3D textures
  81. if (!defined(depth)) {
  82. depth = source.depth;
  83. }
  84. }
  85. // Use premultiplied alpha for opaque textures should perform better on Chrome:
  86. // http://media.tojicode.com/webglCamp4/#20
  87. const preMultiplyAlpha =
  88. options.preMultiplyAlpha ||
  89. pixelFormat === PixelFormat.RGB ||
  90. pixelFormat === PixelFormat.LUMINANCE;
  91. const internalFormat = PixelFormat.toInternalFormat(
  92. pixelFormat,
  93. pixelDatatype,
  94. context,
  95. );
  96. const isCompressed = PixelFormat.isCompressedFormat(internalFormat);
  97. //>>includeStart('debug', pragmas.debug);
  98. if (!defined(width) || !defined(height) || !defined(depth)) {
  99. throw new DeveloperError(
  100. "options requires a source field to create an initialized texture3D or width, height and depth fields to create a blank texture3D.",
  101. );
  102. }
  103. Check.typeOf.number.greaterThan("width", width, 0);
  104. if (width > ContextLimits.maximum3DTextureSize) {
  105. throw new DeveloperError(
  106. `Width must be less than or equal to the maximum texture3D size (${ContextLimits.maximum3DTextureSize}). Check maximum3DTextureSize.`,
  107. );
  108. }
  109. Check.typeOf.number.greaterThan("height", height, 0);
  110. if (height > ContextLimits.maximum3DTextureSize) {
  111. throw new DeveloperError(
  112. `Height must be less than or equal to the maximum texture3D size (${ContextLimits.maximum3DTextureSize}). Check maximum3DTextureSize.`,
  113. );
  114. }
  115. Check.typeOf.number.greaterThan("depth", depth, 0);
  116. if (depth > ContextLimits.maximum3DTextureSize) {
  117. throw new DeveloperError(
  118. `Depth must be less than or equal to the maximum texture3D size (${ContextLimits.maximum3DTextureSize}). Check maximum3DTextureSize.`,
  119. );
  120. }
  121. if (!PixelFormat.validate(pixelFormat)) {
  122. throw new DeveloperError("Invalid options.pixelFormat.");
  123. }
  124. if (!isCompressed && !PixelDatatype.validate(pixelDatatype)) {
  125. throw new DeveloperError("Invalid options.pixelDatatype.");
  126. }
  127. if (
  128. pixelFormat === PixelFormat.DEPTH_COMPONENT &&
  129. pixelDatatype !== PixelDatatype.UNSIGNED_SHORT &&
  130. pixelDatatype !== PixelDatatype.UNSIGNED_INT
  131. ) {
  132. throw new DeveloperError(
  133. "When options.pixelFormat is DEPTH_COMPONENT, options.pixelDatatype must be UNSIGNED_SHORT or UNSIGNED_INT.",
  134. );
  135. }
  136. if (
  137. pixelFormat === PixelFormat.DEPTH_STENCIL &&
  138. pixelDatatype !== PixelDatatype.UNSIGNED_INT_24_8
  139. ) {
  140. throw new DeveloperError(
  141. "When options.pixelFormat is DEPTH_STENCIL, options.pixelDatatype must be UNSIGNED_INT_24_8.",
  142. );
  143. }
  144. if (pixelDatatype === PixelDatatype.FLOAT && !context.floatingPointTexture) {
  145. throw new DeveloperError(
  146. "When options.pixelDatatype is FLOAT, this WebGL implementation must support the OES_texture_float extension. Check context.floatingPointTexture.",
  147. );
  148. }
  149. if (
  150. pixelDatatype === PixelDatatype.HALF_FLOAT &&
  151. !context.halfFloatingPointTexture
  152. ) {
  153. throw new DeveloperError(
  154. "When options.pixelDatatype is HALF_FLOAT, this WebGL implementation must support the OES_texture_half_float extension. Check context.halfFloatingPointTexture.",
  155. );
  156. }
  157. if (PixelFormat.isDepthFormat(pixelFormat)) {
  158. if (defined(source)) {
  159. throw new DeveloperError(
  160. "When options.pixelFormat is DEPTH_COMPONENT or DEPTH_STENCIL, source cannot be provided.",
  161. );
  162. }
  163. if (!context.depthTexture) {
  164. throw new DeveloperError(
  165. "When options.pixelFormat is DEPTH_COMPONENT or DEPTH_STENCIL, this WebGL implementation must support WEBGL_depth_texture. Check context.depthTexture.",
  166. );
  167. }
  168. }
  169. if (isCompressed) {
  170. throw new DeveloperError(
  171. "Texture3D does not currently support compressed formats.",
  172. );
  173. }
  174. //>>includeEnd('debug');
  175. const gl = context._gl;
  176. const sizeInBytes = PixelFormat.texture3DSizeInBytes(
  177. pixelFormat,
  178. pixelDatatype,
  179. width,
  180. height,
  181. depth,
  182. );
  183. this._id = options.id ?? createGuid();
  184. this._context = context;
  185. this._textureFilterAnisotropic = context._textureFilterAnisotropic;
  186. this._textureTarget = gl.TEXTURE_3D;
  187. this._texture = gl.createTexture();
  188. this._internalFormat = internalFormat;
  189. this._pixelFormat = pixelFormat;
  190. this._pixelDatatype = pixelDatatype;
  191. this._width = width;
  192. this._height = height;
  193. this._depth = depth;
  194. this._dimensions = new Cartesian3(width, height, depth);
  195. this._hasMipmap = false;
  196. this._sizeInBytes = sizeInBytes;
  197. this._preMultiplyAlpha = preMultiplyAlpha;
  198. this._flipY = flipY;
  199. this._initialized = false;
  200. this._sampler = undefined;
  201. this._sampler = sampler;
  202. setupSampler(this, sampler);
  203. gl.activeTexture(gl.TEXTURE0);
  204. gl.bindTexture(this._textureTarget, this._texture);
  205. if (defined(source)) {
  206. if (skipColorSpaceConversion) {
  207. gl.pixelStorei(gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.NONE);
  208. } else {
  209. gl.pixelStorei(
  210. gl.UNPACK_COLORSPACE_CONVERSION_WEBGL,
  211. gl.BROWSER_DEFAULT_WEBGL,
  212. );
  213. }
  214. if (!defined(source.arrayBufferView)) {
  215. throw new DeveloperError(
  216. "For Texture3D, options.source.arrayBufferView must be defined",
  217. );
  218. }
  219. loadBufferSource(this, source);
  220. this._initialized = true;
  221. } else {
  222. loadNull(this);
  223. }
  224. gl.bindTexture(this._textureTarget, null);
  225. }
  226. /**
  227. * Load texel data from a buffer into a texture3D.
  228. *
  229. * @param {Texture3D} texture3D The texture3D to which texel values will be loaded.
  230. * @param {Texture3D.Source} source The source for texel values to be loaded into the texture3D.
  231. *
  232. * @private
  233. */
  234. function loadBufferSource(texture3D, source) {
  235. const context = texture3D._context;
  236. const gl = context._gl;
  237. const textureTarget = texture3D._textureTarget;
  238. const internalFormat = texture3D._internalFormat;
  239. const { width, height, depth, pixelFormat, pixelDatatype, flipY } = texture3D;
  240. const unpackAlignment = PixelFormat.alignmentInBytes(
  241. pixelFormat,
  242. pixelDatatype,
  243. width,
  244. );
  245. gl.pixelStorei(gl.UNPACK_ALIGNMENT, unpackAlignment);
  246. gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
  247. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
  248. const { arrayBufferView } = source;
  249. if (flipY) {
  250. console.warn("texture3D.flipY is not supported.");
  251. }
  252. let levels = 1;
  253. if (source.mipLevels && source.mipLevels.length) {
  254. levels = source.mipLevels.length + 1;
  255. }
  256. gl.texStorage3D(textureTarget, levels, internalFormat, width, height, depth);
  257. gl.texSubImage3D(
  258. textureTarget,
  259. 0,
  260. 0,
  261. 0,
  262. 0,
  263. width,
  264. height,
  265. depth,
  266. pixelFormat,
  267. PixelDatatype.toWebGLConstant(pixelDatatype, context),
  268. arrayBufferView,
  269. );
  270. if (levels > 1) {
  271. let mipWidth = width;
  272. let mipHeight = height;
  273. let mipDepth = depth;
  274. for (let i = 0; i < source.mipLevels.length; ++i) {
  275. mipWidth = nextMipSize(mipWidth);
  276. mipHeight = nextMipSize(mipHeight);
  277. mipDepth = nextMipSize(mipDepth);
  278. gl.texSubImage3D(
  279. textureTarget,
  280. i + 1,
  281. 0,
  282. 0,
  283. 0,
  284. mipWidth,
  285. mipHeight,
  286. mipDepth,
  287. pixelFormat,
  288. PixelDatatype.toWebGLConstant(pixelDatatype, context),
  289. source.mipLevels[i],
  290. );
  291. }
  292. }
  293. }
  294. /**
  295. * Copy new image data into this texture, from a source object with width, height, depth, and arrayBufferView properties.
  296. * @param {object} options Object with the following properties:
  297. * @param {object} options.source The source object with width, height, depth, and arrayBufferView properties.
  298. * @param {number} [options.xOffset=0] The offset in the x direction within the texture to copy into.
  299. * @param {number} [options.yOffset=0] The offset in the y direction within the texture to copy into.
  300. * @param {number} [options.zOffset=0] The offset in the z direction within the texture to copy into.
  301. * @param {boolean} [options.skipColorSpaceConversion=false] If true, any custom gamma or color profiles in the texture will be ignored.
  302. *
  303. * @exception {DeveloperError} Unsupported copyFrom with a compressed texture pixel format.
  304. * @exception {DeveloperError} xOffset must be greater than or equal to zero.
  305. * @exception {DeveloperError} yOffset must be greater than or equal to zero.
  306. * @exception {DeveloperError} zOffset must be greater than or equal to zero.
  307. * @exception {DeveloperError} xOffset + source.width must be less than or equal to width.
  308. * @exception {DeveloperError} yOffset + source.height must be less than or equal to height.
  309. * @exception {DeveloperError} zOffset + source.depth must be less than or equal to depth.
  310. * @exception {DeveloperError} This texture was destroyed, i.e., destroy() was called.
  311. * @private
  312. * @example
  313. * texture.copyFrom({
  314. * source: {
  315. * width : 1,
  316. * height : 1,
  317. * depth : 1,
  318. * arrayBufferView : new Uint8Array([255, 0, 0, 255])
  319. * }
  320. * });
  321. */
  322. Texture3D.prototype.copyFrom = function (options) {
  323. options = options ?? Frozen.EMPTY_OBJECT;
  324. const { source, xOffset = 0, yOffset = 0, zOffset = 0 } = options;
  325. //>>includeStart('debug', pragmas.debug);
  326. Check.defined("options.source", source);
  327. Check.defined("options.source.arrayBufferView", source.arrayBufferView);
  328. if (PixelFormat.isCompressedFormat(this._pixelFormat)) {
  329. throw new DeveloperError(
  330. "Unsupported copyFrom with a compressed texture pixel format.",
  331. );
  332. }
  333. Check.typeOf.number.greaterThanOrEquals("xOffset", xOffset, 0);
  334. Check.typeOf.number.greaterThanOrEquals("yOffset", yOffset, 0);
  335. Check.typeOf.number.greaterThanOrEquals("zOffset", zOffset, 0);
  336. Check.typeOf.number.lessThanOrEquals(
  337. "xOffset + options.source.width",
  338. xOffset + source.width,
  339. this._width,
  340. );
  341. Check.typeOf.number.lessThanOrEquals(
  342. "yOffset + options.source.height",
  343. yOffset + source.height,
  344. this._height,
  345. );
  346. Check.typeOf.number.lessThanOrEquals(
  347. "zOffset + options.source.depth",
  348. zOffset + source.depth,
  349. this._depth,
  350. );
  351. //>>includeEnd('debug');
  352. const context = this._context;
  353. const gl = context._gl;
  354. const target = this._textureTarget;
  355. gl.activeTexture(gl.TEXTURE0);
  356. gl.bindTexture(target, this._texture);
  357. const { width, height, depth } = source;
  358. let uploaded = false;
  359. if (!this._initialized) {
  360. if (
  361. xOffset === 0 &&
  362. yOffset === 0 &&
  363. zOffset === 0 &&
  364. width === this._width &&
  365. height === this._height &&
  366. depth === this._depth
  367. ) {
  368. loadBufferSource(this, source);
  369. uploaded = true;
  370. } else {
  371. gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
  372. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
  373. loadNull(this);
  374. }
  375. this._initialized = true;
  376. }
  377. if (!uploaded) {
  378. loadPartialBufferSource(
  379. this,
  380. source.arrayBufferView,
  381. xOffset,
  382. yOffset,
  383. zOffset,
  384. width,
  385. height,
  386. depth,
  387. );
  388. }
  389. gl.bindTexture(target, null);
  390. };
  391. /**
  392. * Load texel data from a buffer into part of a 3D texture
  393. *
  394. * @param {Texture3D} texture3D The texture3D to which texel values will be loaded.
  395. * @param {TypedArray} arrayBufferView The texel values to be loaded into the texture3D.
  396. * @param {number} xOffset The texel x coordinate of the lower left corner of the subregion of the texture to be updated.
  397. * @param {number} yOffset The texel y coordinate of the lower left corner of the subregion of the texture to be updated.
  398. * @param {number} zOffset The texel z coordinate of the lower left corner of the subregion of the texture to be updated.
  399. * @param {number} width The width of the source data, in pixels.
  400. * @param {number} height The height of the source data, in pixels.
  401. * @param {number} depth The depth of the source data, in pixels.
  402. *
  403. * @private
  404. */
  405. function loadPartialBufferSource(
  406. texture3D,
  407. arrayBufferView,
  408. xOffset,
  409. yOffset,
  410. zOffset,
  411. width,
  412. height,
  413. depth,
  414. ) {
  415. const context = texture3D._context;
  416. const gl = context._gl;
  417. const { pixelFormat, pixelDatatype } = texture3D;
  418. const unpackAlignment = PixelFormat.alignmentInBytes(
  419. pixelFormat,
  420. pixelDatatype,
  421. width,
  422. );
  423. gl.pixelStorei(gl.UNPACK_ALIGNMENT, unpackAlignment);
  424. gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
  425. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
  426. gl.texSubImage3D(
  427. texture3D._textureTarget,
  428. 0,
  429. xOffset,
  430. yOffset,
  431. zOffset,
  432. width,
  433. height,
  434. depth,
  435. pixelFormat,
  436. PixelDatatype.toWebGLConstant(pixelDatatype, context),
  437. arrayBufferView,
  438. );
  439. }
  440. /**
  441. * Compute a dimension of the image for the next mip level.
  442. *
  443. * @param {number} currentSize The size of the current mip level.
  444. * @returns {number} The size of the next mip level.
  445. *
  446. * @private
  447. */
  448. function nextMipSize(currentSize) {
  449. const nextSize = Math.floor(currentSize / 2) | 0;
  450. return Math.max(nextSize, 1);
  451. }
  452. /**
  453. * Allocate a texture3D in GPU memory, without providing any image data.
  454. *
  455. * @param {Texture3D} texture3D The texture3D to be initialized with null values.
  456. *
  457. * @private
  458. */
  459. function loadNull(texture3D) {
  460. const context = texture3D._context;
  461. context._gl.texImage3D(
  462. texture3D._textureTarget,
  463. 0,
  464. texture3D._internalFormat,
  465. texture3D._width,
  466. texture3D._height,
  467. texture3D._depth,
  468. 0,
  469. texture3D._pixelFormat,
  470. PixelDatatype.toWebGLConstant(texture3D._pixelDatatype, context),
  471. null,
  472. );
  473. }
  474. /**
  475. * This function is identical to using the Texture3D constructor except that it can be
  476. * replaced with a mock/spy in tests.
  477. * @private
  478. */
  479. Texture3D.create = function (options) {
  480. return new Texture3D(options);
  481. };
  482. Object.defineProperties(Texture3D.prototype, {
  483. /**
  484. * A unique id for the texture3D
  485. * @memberof Texture3D.prototype
  486. * @type {string}
  487. * @readonly
  488. * @private
  489. */
  490. id: {
  491. get: function () {
  492. return this._id;
  493. },
  494. },
  495. /**
  496. * The sampler to use when sampling this texture3D.
  497. * Create a sampler by calling {@link Sampler}. If this
  498. * parameter is not specified, a default sampler is used. The default sampler clamps texture3D
  499. * coordinates in both directions, uses linear filtering for both magnification and minification,
  500. * and uses a maximum anisotropy of 1.0.
  501. * @memberof Texture3D.prototype
  502. * @type {Sampler}
  503. * @private
  504. */
  505. sampler: {
  506. get: function () {
  507. return this._sampler;
  508. },
  509. set: function (sampler) {
  510. setupSampler(this, sampler);
  511. this._sampler = sampler;
  512. },
  513. },
  514. pixelFormat: {
  515. get: function () {
  516. return this._pixelFormat;
  517. },
  518. },
  519. pixelDatatype: {
  520. get: function () {
  521. return this._pixelDatatype;
  522. },
  523. },
  524. dimensions: {
  525. get: function () {
  526. return this._dimensions;
  527. },
  528. },
  529. preMultiplyAlpha: {
  530. get: function () {
  531. return this._preMultiplyAlpha;
  532. },
  533. },
  534. flipY: {
  535. get: function () {
  536. return this._flipY;
  537. },
  538. },
  539. width: {
  540. get: function () {
  541. return this._width;
  542. },
  543. },
  544. height: {
  545. get: function () {
  546. return this._height;
  547. },
  548. },
  549. depth: {
  550. get: function () {
  551. return this._depth;
  552. },
  553. },
  554. sizeInBytes: {
  555. get: function () {
  556. if (this._hasMipmap) {
  557. return Math.floor((this._sizeInBytes * 8) / 7);
  558. }
  559. return this._sizeInBytes;
  560. },
  561. },
  562. _target: {
  563. get: function () {
  564. return this._textureTarget;
  565. },
  566. },
  567. });
  568. /**
  569. * Set up a sampler for use with a texture3D
  570. * @param {Texture3D} texture3D The texture3D to be sampled by this sampler
  571. * @param {Sampler} sampler Information about how to sample the texture3D
  572. * @private
  573. */
  574. function setupSampler(texture3D, sampler) {
  575. let { minificationFilter, magnificationFilter } = sampler;
  576. const mipmap = [
  577. TextureMinificationFilter.NEAREST_MIPMAP_NEAREST,
  578. TextureMinificationFilter.NEAREST_MIPMAP_LINEAR,
  579. TextureMinificationFilter.LINEAR_MIPMAP_NEAREST,
  580. TextureMinificationFilter.LINEAR_MIPMAP_LINEAR,
  581. ].includes(minificationFilter);
  582. const context = texture3D._context;
  583. const pixelFormat = texture3D._pixelFormat;
  584. const pixelDatatype = texture3D._pixelDatatype;
  585. // float textures only support nearest filtering unless the linear extensions are supported
  586. if (
  587. (pixelDatatype === PixelDatatype.FLOAT && !context.textureFloatLinear) ||
  588. (pixelDatatype === PixelDatatype.HALF_FLOAT &&
  589. !context.textureHalfFloatLinear)
  590. ) {
  591. // override the sampler's settings
  592. minificationFilter = mipmap
  593. ? TextureMinificationFilter.NEAREST_MIPMAP_NEAREST
  594. : TextureMinificationFilter.NEAREST;
  595. magnificationFilter = TextureMagnificationFilter.NEAREST;
  596. }
  597. // WebGL 2 depth texture3D only support nearest filtering. See section 3.8.13 OpenGL ES 3 spec
  598. if (PixelFormat.isDepthFormat(pixelFormat)) {
  599. minificationFilter = TextureMinificationFilter.NEAREST;
  600. magnificationFilter = TextureMagnificationFilter.NEAREST;
  601. }
  602. const gl = context._gl;
  603. const target = texture3D._textureTarget;
  604. gl.activeTexture(gl.TEXTURE0);
  605. gl.bindTexture(target, texture3D._texture);
  606. gl.texParameteri(target, gl.TEXTURE_MIN_FILTER, minificationFilter);
  607. gl.texParameteri(target, gl.TEXTURE_MAG_FILTER, magnificationFilter);
  608. gl.texParameteri(target, gl.TEXTURE_WRAP_R, sampler.wrapR);
  609. gl.texParameteri(target, gl.TEXTURE_WRAP_S, sampler.wrapS);
  610. gl.texParameteri(target, gl.TEXTURE_WRAP_T, sampler.wrapT);
  611. if (defined(texture3D._textureFilterAnisotropic)) {
  612. gl.texParameteri(
  613. target,
  614. texture3D._textureFilterAnisotropic.TEXTURE_MAX_ANISOTROPY_EXT,
  615. sampler.maximumAnisotropy,
  616. );
  617. }
  618. gl.bindTexture(target, null);
  619. }
  620. /**
  621. * @param {MipmapHint} [hint=MipmapHint.DONT_CARE] optional.
  622. * @private
  623. * @exception {DeveloperError} Cannot call generateMipmap when the texture3D pixel format is DEPTH_COMPONENT or DEPTH_STENCIL.
  624. * @exception {DeveloperError} Cannot call generateMipmap when the texture3D pixel format is a compressed format.
  625. * @exception {DeveloperError} hint is invalid.
  626. * @exception {DeveloperError} This texture3D's width must be a power of two to call generateMipmap() in a WebGL1 context.
  627. * @exception {DeveloperError} This texture3D's height must be a power of two to call generateMipmap() in a WebGL1 context.
  628. * @exception {DeveloperError} This texture3D was destroyed, i.e., destroy() was called.
  629. */
  630. Texture3D.prototype.generateMipmap = function (hint) {
  631. hint = hint ?? MipmapHint.DONT_CARE;
  632. //>>includeStart('debug', pragmas.debug);
  633. if (PixelFormat.isDepthFormat(this._pixelFormat)) {
  634. throw new DeveloperError(
  635. "Cannot call generateMipmap when the texture3D pixel format is DEPTH_COMPONENT or DEPTH_STENCIL.",
  636. );
  637. }
  638. if (PixelFormat.isCompressedFormat(this._pixelFormat)) {
  639. throw new DeveloperError(
  640. "Cannot call generateMipmap with a compressed pixel format.",
  641. );
  642. }
  643. if (!MipmapHint.validate(hint)) {
  644. throw new DeveloperError("hint is invalid.");
  645. }
  646. //>>includeEnd('debug');
  647. this._hasMipmap = true;
  648. const gl = this._context._gl;
  649. const target = this._textureTarget;
  650. gl.hint(gl.GENERATE_MIPMAP_HINT, hint);
  651. gl.activeTexture(gl.TEXTURE0);
  652. gl.bindTexture(target, this._texture);
  653. gl.generateMipmap(target);
  654. gl.bindTexture(target, null);
  655. };
  656. Texture3D.prototype.isDestroyed = function () {
  657. return false;
  658. };
  659. Texture3D.prototype.destroy = function () {
  660. this._context._gl.deleteTexture(this._texture);
  661. return destroyObject(this);
  662. };
  663. export default Texture3D;