image.domain\texture\texture.mbt
84.7% · 83 / 98 points · 314 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 纹理 texture/texture - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 纹理映射 — UV/烘焙/采样 - 7
// Texture mapping: UV / baking / sampling - 8
// - 9
// Phase 18 v408: 3D视觉与渲染大扩展 — 纹理映射。 - 10
- 11
///| - 12
/// 纹理过滤模式。 - 13
/// 描述 Texture filter mode. - 14
/// `#non_exhaustive` 表示未来可能添加新构造器 - 15
#derive(Eq, @debug.Debug) - 16
#non_exhaustive - 17
pub(all) enum TextureFilterMode { - 18
NearestFilter // 最近邻 - 19
BilinearFilter // 双线性 - 20
TrilinearFilter // 三线性(简化为双线性) - 21
} - 22
- 23
///| - 24
pub extend TextureFilterMode with @debug.Debug::{to_repr} - 25
- 26
///| - 27
pub extend TextureFilterMode with Eq::{not_equal, equal} - 28
- 29
///| - 30
/// 纹理映射方法。 - 31
/// 描述 Texture mapping method. - 32
/// `#non_exhaustive` 表示未来可能添加新构造器 - 33
#derive(Eq, @debug.Debug) - 34
#non_exhaustive - 35
pub(all) enum TextureMappingMethod { - 36
PlanarMapping // 平面映射 - 37
SphericalMapping // 球面映射 - 38
CylindricalMapping // 柱面映射 - 39
} - 40
- 41
///| - 42
pub extend TextureMappingMethod with @debug.Debug::{to_repr} - 43
- 44
///| - 45
pub extend TextureMappingMethod with Eq::{not_equal, equal} - 46
- 47
///| - 48
/// 纹理坐标。 - 49
/// 描述 Texture coordinate. - 50
#derive(Eq, @debug.Debug) - 51
pub(all) struct TextureCoord { - 52
u : Double - 53
v : Double - 54
} - 55
- 56
///| - 57
pub extend TextureCoord with @debug.Debug::{to_repr} - 58
- 59
///| - 60
pub extend TextureCoord with Eq::{not_equal, equal} - 61
- 62
///| - 63
/// 2D纹理。 - 64
/// 二维纹理 2D texture. - 65
#derive(Eq, @debug.Debug) - 66
pub(all) struct Texture2D { - 67
image : @types.Image - 68
filter_mode : TextureFilterMode - 69
wrap_u : Bool // U方向是否循环 - 70
wrap_v : Bool // V方向是否循环 - 71
} - 72
- 73
///| - 74
pub extend Texture2D with @debug.Debug::{to_repr} - 75
- 76
///| - 77
pub extend Texture2D with Eq::{not_equal, equal} - 78
- 79
///| - 80
/// 纹理配置。 - 81
/// 描述 Texture configuration. - 82
#derive(Eq, @debug.Debug) - 83
pub(all) struct TextureConfig { - 84
mapping_method : TextureMappingMethod - 85
filter_mode : TextureFilterMode - 86
wrap_u : Bool - 87
wrap_v : Bool - 88
} - 89
- 90
///| - 91
pub extend TextureConfig with @debug.Debug::{to_repr} - 92
- 93
///| - 94
pub extend TextureConfig with Eq::{not_equal, equal} - 95
- 96
///| - 97
/// 默认纹理配置。 - 98
/// 描述 Default texture configuration. - 99
pub fn default_texture_config() -> TextureConfig { - 100●
{ - 101
mapping_method: PlanarMapping, - 102
filter_mode: BilinearFilter, - 103
wrap_u: true, - 104
wrap_v: true, - 105
} - 106
} - 107
- 108
///| - 109
/// 从图像创建纹理。 - 110
/// 创建 Create texture from image. - 111
pub fn create_texture(img : @types.Image, config : TextureConfig) -> Texture2D { - 112●
{ - 113
image: img, - 114
filter_mode: config.filter_mode, - 115
wrap_u: config.wrap_u, - 116
wrap_v: config.wrap_v, - 117
} - 118
} - 119
- 120
///| - 121
/// 包装坐标:处理超出[0,1)范围的坐标。 - 122
/// 环绕 Wrap coordinate: handle coordinates outside [0,1). - 123
fn wrap_coord(coord : Double, wrap : Bool) -> Double { - 124●
if wrap { - 125●
let c = coord % 1.0 - 126
if c < 0.0 { - 127○
c + 1.0 - 128
} else { - 129●
c - 130
} - 131
} else { - 132○
coord.clamp(min=0.0, max=0.9999) - 133
} - 134
} - 135
- 136
///| - 137
/// 最近邻采样。 - 138
/// 描述 Nearest neighbor sampling. - 139
pub fn nearest_sample( - 140
tex : Texture2D, - 141
u : Double, - 142
v : Double, - 143
) -> (Double, Double, Double) { - 144●
let w = tex.image.width - 145
let h = tex.image.height - 146
let ch = tex.image.channels - 147●
let uu = wrap_coord(u, tex.wrap_u) - 148●
let vv = wrap_coord(v, tex.wrap_v) - 149●
let x = (uu * w.to_double()).to_int().clamp(min=0, max=w - 1) - 150●
let y = (vv * h.to_double()).to_int().clamp(min=0, max=h - 1) - 151
let idx = (y * w + x) * ch - 152●
let r = tex.image.data[idx].to_double() - 153◐
let g = if ch > 1 { tex.image.data[idx + 1].to_double() } else { r } - 154◐
let b = if ch > 2 { tex.image.data[idx + 2].to_double() } else { r } - 155
(r, g, b) - 156
} - 157
- 158
///| - 159
/// 双线性采样。 - 160
/// 描述 Bilinear sampling. - 161
pub fn bilinear_sample( - 162
tex : Texture2D, - 163
u : Double, - 164
v : Double, - 165
) -> (Double, Double, Double) { - 166●
let w = tex.image.width - 167
let h = tex.image.height - 168
let ch = tex.image.channels - 169●
let uu = wrap_coord(u, tex.wrap_u) - 170●
let vv = wrap_coord(v, tex.wrap_v) - 171●
let fx = uu * w.to_double() - 172●
let fy = vv * h.to_double() - 173●
let x0 = fx.to_int().clamp(min=0, max=w - 1) - 174●
let y0 = fy.to_int().clamp(min=0, max=h - 1) - 175◐
let x1 = if x0 + 1 < w { x0 + 1 } else { x0 } - 176◐
let y1 = if y0 + 1 < h { y0 + 1 } else { y0 } - 177●
let dx = fx - x0.to_double() - 178●
let dy = fy - y0.to_double() - 179
let idx00 = (y0 * w + x0) * ch - 180
let idx01 = (y0 * w + x1) * ch - 181
let idx10 = (y1 * w + x0) * ch - 182
let idx11 = (y1 * w + x1) * ch - 183
let sample_ch = fn(c : Int32) -> Double { - 184●
let v00 = tex.image.data[idx00 + c].to_double() - 185●
let v01 = tex.image.data[idx01 + c].to_double() - 186●
let v10 = tex.image.data[idx10 + c].to_double() - 187●
let v11 = tex.image.data[idx11 + c].to_double() - 188
v00 * (1.0 - dx) * (1.0 - dy) + - 189
v01 * dx * (1.0 - dy) + - 190
v10 * (1.0 - dx) * dy + - 191
v11 * dx * dy - 192
} - 193●
let r = sample_ch(0) - 194◐
let g = if ch > 1 { sample_ch(1) } else { r } - 195◐
let b = if ch > 2 { sample_ch(2) } else { r } - 196
(r, g, b) - 197
} - 198
- 199
///| - 200
/// 纹理采样主流程。 - 201
/// 描述 Texture sampling pipeline. - 202
pub fn sample_texture( - 203
tex : Texture2D, - 204
u : Double, - 205
v : Double, - 206
) -> (Double, Double, Double) { - 207●
match tex.filter_mode { - 208○
NearestFilter => nearest_sample(tex, u, v) - 209●
BilinearFilter => bilinear_sample(tex, u, v) - 210○
TrilinearFilter => bilinear_sample(tex, u, v) - 211
} - 212
} - 213
- 214
///| - 215
/// 平面映射:将3D坐标映射到UV。 - 216
/// 描述 Planar mapping: map 3D coordinates to UV. - 217
pub fn planar_mapping(x : Double, y : Double, _z : Double) -> TextureCoord { - 218●
{ u: (x + 1.0) / 2.0, v: (y + 1.0) / 2.0, } - 219
} - 220
- 221
///| - 222
/// 球面映射:将3D坐标映射到UV。 - 223
/// 描述 Spherical mapping: map 3D coordinates to UV. - 224
pub fn spherical_mapping(x : Double, y : Double, z : Double) -> TextureCoord { - 225●
let r = (x * x + y * y + z * z).sqrt() - 226
if r < 0.0001 { - 227○
return { u: 0.0, v: 0.0, } - 228
} - 229●
let theta = @math.acos(z / r) - 230●
let phi = @math.atan2(y, x) - 231
{ u: (phi / 3.141592653589793 + 1.0) / 2.0, v: theta / 3.141592653589793, } - 232
} - 233
- 234
///| - 235
/// 柱面映射:将3D坐标映射到UV。 - 236
/// 描述 Cylindrical mapping: map 3D coordinates to UV. - 237
pub fn cylindrical_mapping(x : Double, y : Double, z : Double) -> TextureCoord { - 238●
let theta = @math.atan2(y, x) - 239
{ u: (theta / 3.141592653589793 + 1.0) / 2.0, v: (z + 1.0) / 2.0, } - 240
} - 241
- 242
///| - 243
/// 计算UV映射。 - 244
/// 计算 Compute UV mapping. - 245
pub fn compute_uv_mapping( - 246
vertices : Array[(Double, Double, Double)], - 247
map_method : TextureMappingMethod, - 248
) -> Array[TextureCoord] { - 249●
let result : Array[TextureCoord] = [] - 250
for v in vertices { - 251●
let (x, y, z) = v - 252●
let coord = match map_method { - 253●
PlanarMapping => planar_mapping(x, y, z) - 254○
SphericalMapping => spherical_mapping(x, y, z) - 255○
CylindricalMapping => cylindrical_mapping(x, y, z) - 256
} - 257●
result.push(coord) - 258
} - 259
result - 260
} - 261
- 262
///| - 263
/// 烘焙纹理:将顶点颜色烘焙到纹理。 - 264
/// 描述 Bake texture: bake vertex colors to texture. - 265
pub fn bake_texture( - 266
vertices : Array[(Double, Double, Double)], - 267
colors : Array[(Double, Double, Double)], - 268
uv_coords : Array[TextureCoord], - 269
tex_width : Int32, - 270
tex_height : Int32, - 271
) -> @types.Image { - 272●
let data : Array[Byte] = [] - 273
for _ in 0..<(tex_width * tex_height * 3) { - 274●
data.push(0) - 275
} - 276●
let n = vertices.length() - 277
for i in 0..<n { - 278●
if i >= uv_coords.length() || i >= colors.length() { - 279○
continue - 280
} - 281●
let u = uv_coords[i].u.clamp(min=0.0, max=0.9999) - 282●
let v = uv_coords[i].v.clamp(min=0.0, max=0.9999) - 283●
let x = (u * tex_width.to_double()).to_int().clamp(min=0, max=tex_width - 1) - 284●
let y = (v * tex_height.to_double()) - 285●
.to_int() - 286●
.clamp(min=0, max=tex_height - 1) - 287
let (r, g, b) = colors[i] - 288
let idx = (y * tex_width + x) * 3 - 289●
data[idx] = r.clamp(min=0.0, max=255.0).to_int().to_byte() - 290●
data[idx + 1] = g.clamp(min=0.0, max=255.0).to_int().to_byte() - 291●
data[idx + 2] = b.clamp(min=0.0, max=255.0).to_int().to_byte() - 292
} - 293
{ - 294
width: tex_width, - 295
height: tex_height, - 296
channels: 3, - 297●
data: Bytes::from_array(data), - 298
} - 299
} - 300
- 301
///| - 302
/// 纹理映射流水线:UV映射 → 烘焙 → 采样。 - 303
/// 描述 Texture mapping pipeline: UV mapping → bake → sample. - 304
pub fn texture_pipeline( - 305
vertices : Array[(Double, Double, Double)], - 306
colors : Array[(Double, Double, Double)], - 307
config : TextureConfig, - 308
tex_width : Int32, - 309
tex_height : Int32, - 310
) -> (Array[TextureCoord], @types.Image) { - 311●
let uv_coords = compute_uv_mapping(vertices, config.mapping_method) - 312●
let baked = bake_texture(vertices, colors, uv_coords, tex_width, tex_height) - 313
(uv_coords, baked) - 314
}