Skip to main content

three_rs/textures/
compressed_texture.rs

1//! Ports of `three.js/src/textures/CompressedTexture.js` and
2//! `CompressedArrayTexture.js`.
3//!
4//! Upstream both are `Texture` subclasses whose `image` is `{ width, height
5//! [, depth] }` and whose pixels live in `mipmaps`. Here they are constructors
6//! of the one [`Texture`] handle, the way `DataTexture`'s float variants are
7//! (`Texture::data_rgba16float`): a compressed texture has to fit every slot a
8//! `Texture` fits — a glTF `KHR_texture_basisu` base colour map is a
9//! `MeshStandardMaterial.map` like any PNG — so it cannot be a separate type.
10//! What makes it compressed is that [`TextureInner::mipmaps`] is filled and
11//! `format` is a block-compressed `wgpu::TextureFormat`; what makes it an
12//! array is [`TextureInner::depth`].
13//!
14//! [`TextureInner::mipmaps`]: super::TextureInner::mipmaps
15//! [`TextureInner::depth`]: super::TextureInner::depth
16
17use super::texture::{Mipmap, Texture};
18use super::{MinFilter, TextureFilter};
19
20impl Texture {
21    /// `new CompressedTexture( mipmaps, width, height, format, type )`.
22    ///
23    /// The constructor's own differences from `Texture` (`CompressedTexture.js`):
24    /// `flipY = false` — "can't flip compressed textures" — and
25    /// `generateMipmaps = false` — "can't generate mipmaps for compressed
26    /// textures". The filters stay at the `Texture` defaults; `KTX2Loader`
27    /// overrides them.
28    ///
29    /// `format` is the GPU format the renderer creates the texture with —
30    /// the port's stand-in for three's `( format, type, colorSpace )` triple,
31    /// which `WebGPUTextureUtils.getFormat()` turns into one — and every
32    /// mip's `data` must be laid out in it.
33    pub fn compressed(
34        mipmaps: Vec<Mipmap>,
35        width: u32,
36        height: u32,
37        format: wgpu::TextureFormat,
38    ) -> Self {
39        assert!(
40            !mipmaps.is_empty(),
41            "three-rs: a CompressedTexture needs at least one mip level"
42        );
43        let texture = Texture::new(width, height, None);
44        {
45            let mut inner = texture.borrow_mut_inner();
46            inner.flip_y = false;
47            inner.generate_mipmaps = false;
48            inner.format = format;
49            inner.mipmaps = mipmaps;
50        }
51        texture
52    }
53
54    /// `new CompressedArrayTexture( mipmaps, width, height, depth, format, type )`
55    /// — `image.depth` layers, each mip's `data` holding every layer of the
56    /// level in order. `wrapR` is `ClampToEdgeWrapping` upstream and has no
57    /// sampler field to go to in a 2-D-array view, so it is not stored.
58    pub fn compressed_array(
59        mipmaps: Vec<Mipmap>,
60        width: u32,
61        height: u32,
62        depth: u32,
63        format: wgpu::TextureFormat,
64    ) -> Self {
65        assert!(
66            depth >= 1,
67            "three-rs: a CompressedArrayTexture has at least one layer"
68        );
69        let texture = Texture::compressed(mipmaps, width, height, format);
70        texture.borrow_mut_inner().depth = depth;
71        texture
72    }
73
74    /// `texture.minFilter = min; texture.magFilter = mag`.
75    pub fn set_filters(&self, min_filter: MinFilter, mag_filter: TextureFilter) {
76        let mut inner = self.borrow_mut_inner();
77        inner.min_filter = min_filter;
78        inner.mag_filter = mag_filter;
79    }
80
81    /// `texture.premultiplyAlpha = value`.
82    pub fn set_premultiply_alpha(&self, premultiply_alpha: bool) {
83        self.borrow_mut_inner().premultiply_alpha = premultiply_alpha;
84    }
85
86    /// `texture.isCompressedArrayTexture` — sampled with a layer index
87    /// through a `texture_2d_array<f32>` binding.
88    pub fn is_array(&self) -> bool {
89        self.borrow().depth > 0
90    }
91}