image.domain\texture\texture.mbt

84.7% · 83 / 98 points · 314 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

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