@@ -1005,7 +1005,7 @@ impl Image {
1005
1005
///
1006
1006
/// Returns None if the provided coordinates are out of bounds.
1007
1007
///
1008
- /// For 2D textures, Z is ignored . For 1D textures, Y and Z are ignored.
1008
+ /// For 2D textures, Z is the layer number . For 1D textures, Y and Z are ignored.
1009
1009
#[ inline( always) ]
1010
1010
pub fn pixel_data_offset ( & self , coords : UVec3 ) -> Option < usize > {
1011
1011
let width = self . texture_descriptor . size . width ;
@@ -1014,18 +1014,12 @@ impl Image {
1014
1014
1015
1015
let pixel_size = self . texture_descriptor . format . pixel_size ( ) ;
1016
1016
let pixel_offset = match self . texture_descriptor . dimension {
1017
- TextureDimension :: D3 => {
1017
+ TextureDimension :: D3 | TextureDimension :: D2 => {
1018
1018
if coords. x >= width || coords. y >= height || coords. z >= depth {
1019
1019
return None ;
1020
1020
}
1021
1021
coords. z * height * width + coords. y * width + coords. x
1022
1022
}
1023
- TextureDimension :: D2 => {
1024
- if coords. x >= width || coords. y >= height {
1025
- return None ;
1026
- }
1027
- coords. y * width + coords. x
1028
- }
1029
1023
TextureDimension :: D1 => {
1030
1024
if coords. x >= width {
1031
1025
return None ;
@@ -1096,15 +1090,20 @@ impl Image {
1096
1090
self . get_color_at_internal ( UVec3 :: new ( x, y, 0 ) )
1097
1091
}
1098
1092
1099
- /// Read the color of a specific pixel (3D texture).
1093
+ /// Read the color of a specific pixel (2D texture with layers or 3D texture).
1100
1094
///
1101
1095
/// See [`get_color_at`](Self::get_color_at) for more details.
1102
1096
#[ inline( always) ]
1103
1097
pub fn get_color_at_3d ( & self , x : u32 , y : u32 , z : u32 ) -> Result < Color , TextureAccessError > {
1104
- if self . texture_descriptor . dimension != TextureDimension :: D3 {
1105
- return Err ( TextureAccessError :: WrongDimension ) ;
1098
+ match (
1099
+ self . texture_descriptor . dimension ,
1100
+ self . texture_descriptor . size . depth_or_array_layers ,
1101
+ ) {
1102
+ ( TextureDimension :: D3 , _) | ( TextureDimension :: D2 , 2 ..) => {
1103
+ self . get_color_at_internal ( UVec3 :: new ( x, y, z) )
1104
+ }
1105
+ _ => Err ( TextureAccessError :: WrongDimension ) ,
1106
1106
}
1107
- self . get_color_at_internal ( UVec3 :: new ( x, y, z) )
1108
1107
}
1109
1108
1110
1109
/// Change the color of a specific pixel (1D texture).
@@ -1148,7 +1147,7 @@ impl Image {
1148
1147
self . set_color_at_internal ( UVec3 :: new ( x, y, 0 ) , color)
1149
1148
}
1150
1149
1151
- /// Change the color of a specific pixel (3D texture).
1150
+ /// Change the color of a specific pixel (2D texture with layers or 3D texture).
1152
1151
///
1153
1152
/// See [`set_color_at`](Self::set_color_at) for more details.
1154
1153
#[ inline( always) ]
@@ -1159,10 +1158,15 @@ impl Image {
1159
1158
z : u32 ,
1160
1159
color : Color ,
1161
1160
) -> Result < ( ) , TextureAccessError > {
1162
- if self . texture_descriptor . dimension != TextureDimension :: D3 {
1163
- return Err ( TextureAccessError :: WrongDimension ) ;
1161
+ match (
1162
+ self . texture_descriptor . dimension ,
1163
+ self . texture_descriptor . size . depth_or_array_layers ,
1164
+ ) {
1165
+ ( TextureDimension :: D3 , _) | ( TextureDimension :: D2 , 2 ..) => {
1166
+ self . set_color_at_internal ( UVec3 :: new ( x, y, z) , color)
1167
+ }
1168
+ _ => Err ( TextureAccessError :: WrongDimension ) ,
1164
1169
}
1165
- self . set_color_at_internal ( UVec3 :: new ( x, y, z) , color)
1166
1170
}
1167
1171
1168
1172
#[ inline( always) ]
@@ -1659,4 +1663,25 @@ mod test {
1659
1663
Err ( TextureAccessError :: OutOfBounds { x: 5 , y: 10 , z: 0 } )
1660
1664
) ) ;
1661
1665
}
1666
+
1667
+ #[ test]
1668
+ fn get_set_pixel_2d_with_layers ( ) {
1669
+ let mut image = Image :: new_fill (
1670
+ Extent3d {
1671
+ width : 5 ,
1672
+ height : 10 ,
1673
+ depth_or_array_layers : 3 ,
1674
+ } ,
1675
+ TextureDimension :: D2 ,
1676
+ & [ 0 , 0 , 0 , 255 ] ,
1677
+ TextureFormat :: Rgba8Unorm ,
1678
+ RenderAssetUsages :: MAIN_WORLD ,
1679
+ ) ;
1680
+ image. set_color_at_3d ( 0 , 0 , 0 , Color :: WHITE ) . unwrap ( ) ;
1681
+ assert ! ( matches!( image. get_color_at_3d( 0 , 0 , 0 ) , Ok ( Color :: WHITE ) ) ) ;
1682
+ image. set_color_at_3d ( 2 , 3 , 1 , Color :: WHITE ) . unwrap ( ) ;
1683
+ assert ! ( matches!( image. get_color_at_3d( 2 , 3 , 1 ) , Ok ( Color :: WHITE ) ) ) ;
1684
+ image. set_color_at_3d ( 4 , 9 , 2 , Color :: WHITE ) . unwrap ( ) ;
1685
+ assert ! ( matches!( image. get_color_at_3d( 4 , 9 , 2 ) , Ok ( Color :: WHITE ) ) ) ;
1686
+ }
1662
1687
}
0 commit comments