Compare commits

...
27 Commits
Author SHA1 Message Date
dengqn 3343133e9d triangle 2025-08-20 15:44:52 +08:00
dengqn 27905c888f feat: triangle 2025-08-20 15:44:09 +08:00
dengqn ff7cde8758 multi threading 2025-08-19 11:54:06 +08:00
dengqn 0e62a20775 rust warms 2025-08-19 08:53:07 +08:00
dengqn ff4132edff Merge branch 'master' of https://git.dengqn.com/dqn/ray-trace-w1 2025-08-19 08:33:43 +08:00
dengqn 06c5528599 14.1 A Final Render 2025-08-19 08:33:05 +08:00
dengqn 02981da7bf 13.Defocus Blur 2025-08-15 01:41:29 +08:00
dengqn caa21505ba 12.2.Positioning and Orienting the Camera 2025-08-14 19:16:08 +08:00
dengqn 5c1c9a3edd 12.1.Camera Viewing Geometry 2025-08-13 17:16:53 +08:00
dengqn 5dfdf5e32e bugfix 2025-08-13 00:34:42 +08:00
dengqn 37c4cbb666 Dielectric bug 2025-08-12 23:52:23 +08:00
dengqn b28a6cc7f5 10.6.Fuzzy Reflection 2025-08-12 17:37:25 +08:00
dengqn 37b793287d chore: clean code 2025-08-12 17:04:01 +08:00
dengqn 4a58168482 ignorefile 2025-08-12 16:59:40 +08:00
dengqn 2018b02e02 bug fix 2025-08-12 16:58:30 +08:00
dengqn cbd072af4a chore 2025-08-09 16:49:47 +08:00
dengqn ba56d1a2dd Merge remote-tracking branch 'origin/master'
# Conflicts:
#	src/camera.rs
#	src/math_utils.rs
2025-08-09 16:22:43 +08:00
dengqn bceb4ae244 10.5.A Scene with Metal Spheres 2025-08-09 16:10:52 +08:00
dengqn 11245af112 chore: hit record 2025-08-09 01:19:01 +08:00
dengqn 33b3707064 gamma 2025-08-06 10:57:11 +08:00
dengqn 89ad39b3e4 perf:writer 2025-08-05 19:04:27 +08:00
dengqn f60a06d84b perf: writer 2025-08-05 14:13:47 +08:00
dengqn 71ae83d205 9.Diffuse Materials 2025-08-05 01:47:04 +08:00
dengqn edd518b54e 7.Moving Camera Code Into Its Own Class
8.Antialiasing
2025-08-03 22:38:13 +08:00
dengqn 8c58296849 add Camerax2 2025-08-03 15:28:24 +08:00
dengqn 59b169507c add Camera 2025-08-03 14:52:47 +08:00
dengqn 5a77b92d9e HittableList 2025-08-03 02:16:05 +08:00
17 changed files with 1185 additions and 122 deletions
+2
View File
@@ -1 +1,3 @@
/target
.idea/
.vscode/
+8
View File
@@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
Generated
+202
View File
@@ -2,6 +2,208 @@
# It is not intended for manual editing.
version = 4
[[package]]
name = "bitflags"
version = "2.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
[[package]]
name = "cfg-if"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268"
[[package]]
name = "crossbeam-deque"
version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51"
dependencies = [
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
[[package]]
name = "either"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
[[package]]
name = "getrandom"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4"
dependencies = [
"cfg-if",
"libc",
"r-efi",
"wasi",
]
[[package]]
name = "libc"
version = "0.2.174"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776"
[[package]]
name = "ppv-lite86"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
dependencies = [
"zerocopy",
]
[[package]]
name = "proc-macro2"
version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
dependencies = [
"proc-macro2",
]
[[package]]
name = "r-efi"
version = "5.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
[[package]]
name = "rand"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1"
dependencies = [
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38"
dependencies = [
"getrandom",
]
[[package]]
name = "ray-trace-w1"
version = "0.1.0"
dependencies = [
"rand",
"rayon",
]
[[package]]
name = "rayon"
version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "368f01d005bf8fd9b1206fb6fa653e6c4a81ceb1466406b81792d87c5677a58f"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91"
dependencies = [
"crossbeam-deque",
"crossbeam-utils",
]
[[package]]
name = "syn"
version = "2.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]]
name = "wasi"
version = "0.14.2+wasi-0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3"
dependencies = [
"wit-bindgen-rt",
]
[[package]]
name = "wit-bindgen-rt"
version = "0.39.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1"
dependencies = [
"bitflags",
]
[[package]]
name = "zerocopy"
version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1039dd0d3c310cf05de012d8a39ff557cb0d23087fd44cad61df08fc31907a2f"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ecf5b4cc5364572d7f4c329661bcc82724222973f2cab6f050a4e5c22f75181"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
+2
View File
@@ -4,3 +4,5 @@ version = "0.1.0"
edition = "2024"
[dependencies]
rand = "0.9.2" # 使用最新稳定版
rayon = "1.11.0"
+336
View File
@@ -0,0 +1,336 @@
use std::fs::File;
use std::io::{BufWriter};
use std::sync::{RwLock};
use std::time::Instant;
use crate::hittable::HittableList;
use crate::math_utils::{clamp, degrees_to_radians, random_in_unit_disk};
use crate::ppm_writer::PPMWriter;
use crate::types_defined::{Camera, Color, HitRecord, Point, Ray, Vec3};
use rand::rngs::ThreadRng;
use rand::{Rng, rng};
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use crate::material::{Material, MaterialKind};
impl<'a> Camera<'a> {
pub fn new(
fov: f32,
image_width: i32,
aspect_ratio: f32,
center: Point,
look_at: Point,
v_up: Vec3,
sample_times: i8,
reflect_depth: i8,
defocus_angle : f32,
focal_distance: f32,
ppm_file_writer: &'a mut PPMWriter<BufWriter<File>>
) -> Self {
// let focal_length = (look_at - center).length();
let theta = degrees_to_radians(fov);
let h = f32::tan(theta / 2.);
let viewport_height = 2. * h * focal_distance;
// let viewport_height = 2. * h * focal_length;
let image_height = ((image_width as f32 / aspect_ratio) as i32).max(1);
let viewport_width = viewport_height * (image_width as f32 / image_height as f32);
let w = (center - look_at).normalize();
let u = v_up.cross(w).normalize();
let v = w.cross(u);
let viewport_u = viewport_width * u;
// image x--> right
// |
// y
// space: y up , x right , z back, -z front
let viewport_v = viewport_height * (-1. * v);
// width per pix
let viewport_u_delta = viewport_u / (image_width as f32);
// height per pix
let viewport_v_delta = viewport_v / (image_height as f32);
let viewport_top_left_pixel =
center - focal_distance * w - viewport_u / 2.0 - viewport_v / 2.0;
// center - focal_length * w - viewport_u / 2.0 - viewport_v / 2.0;
// padding 0.5* delta u/v
let viewport_top_left_pixel_center =
viewport_top_left_pixel - viewport_u_delta / 2.0 - viewport_v_delta / 2.0;
// Calculate the camera defocus disk basis vectors.
let defocus_radius = focal_distance * degrees_to_radians(defocus_angle / 2.0).tan();
let defocus_disk_u = u * defocus_radius;
let defocus_disk_v = v * defocus_radius;
Camera {
image_width,
image_height,
// aspect_ratio,
// viewport_width,
// viewport_height,
center,
// look_at,
// v_up,
// focal_length,
// viewport_u,
// viewport_v,
defocus_angle,
defocus_disk_u,
defocus_disk_v,
viewport_u_delta,
viewport_v_delta,
viewport_top_left_pixel_center,
sample_times,
reflect_depth,
ppm_file_writer
}
}
pub fn render(&mut self, world: &HittableList) {
// let mut img_content = format!("P3\n{} {}\n255\n", self.image_width, self.image_height);
let start = Instant::now();
for j in 0..self.image_height {
// if j % 10 == 0 {
println!("scan line {}/{} ", j + 1, self.image_height);
// }
for i in 0..self.image_width {
let sample_color: Vec3 = (0..self.sample_times)
.into_par_iter()
.map(|_| {
let r = self.get_ray(i, j);
return self.ray_color_sync(&r, self.reflect_depth, RwLock::new(world));
}).sum();
let color: Vec3 = sample_color * (1.0 / self.sample_times as f32);
// // color
// let mut color = Color::new(0.0, 0.0, 0.0);
// for _ in 0..self.sample_times {
// let r = self.get_ray(i, j);
// let sample_color = self.ray_color(&r, self.reflect_depth, world);
// color = color + sample_color;
// }
// // color * each sample color
// color = color * (1.0 / self.sample_times as f32);
// clamp color rgb
let color_clamped = Color::new(
clamp(color.x, 0.0, 1.0),
clamp(color.y, 0.0, 1.0),
clamp(color.z, 0.0, 1.0),
);
let _ = self.ppm_file_writer.write(color_clamped.to_color());
}
}
let _ = self.ppm_file_writer.finish();
let end = Instant::now();
println!("================[{}mils]=====================", end.duration_since(start).as_millis())
// img_content
}
fn defocus_disk_sample(&self) -> Point {
// Returns a random point in the camera defocus disk.
let p = random_in_unit_disk();
return self.center + (p.x * self.defocus_disk_u) + (p.y * self.defocus_disk_v);
}
fn get_ray(&self, i: i32, j: i32) -> Ray {
let rng = &mut rng();
let bias = self.random_square(rng);
let pix_sample = self.viewport_top_left_pixel_center
+ (i as f32 + bias.x) * self.viewport_u_delta
+ (j as f32 + bias.y) * self.viewport_v_delta;
let center = if self.defocus_angle < 0. {
self.center
} else {
self.defocus_disk_sample()
};
Ray::new(center, pix_sample - self.center)
}
fn ray_color(&self, ray: &Ray, depth: i8, world: &HittableList) -> Vec3 {
// 反射次数
if depth <= 0 {
return Vec3::new(0.0, 0.0, 0.0);
}
// 限制出射光线的角度(0.001经验值)
let hit_record = &mut HitRecord{
t: 0.0,
p: Vec3::new(0.0, 0.0, 0.0),
normal: Vec3::new(0.0, 0.0, 0.0),
front_face: false,
material: None,
};
let hit = world.hit(&ray, 0.001, f32::MAX, hit_record);
if hit {
let hit_m = &hit_record.material;
let hc = &mut HitRecord{
t: hit_record.t,
p: hit_record.p,
normal: hit_record.normal,
front_face: hit_record.front_face,
material: None,
};
let hit_c = match hit_m {
Some(mk) => {
match mk {
// MaterialKind::Lambertian(l) => l.albedo,
MaterialKind::Lambertian(l) => self.scatter_color(ray, hc, l, depth, world),
MaterialKind::Metal(m) => self.scatter_color(ray, hc, m, depth, world),
MaterialKind::Dielectric(d) => self.scatter_color(ray, hc, d, depth, world)
}
},
None => {
if hit_record.t >= 0.0 {
let diffuse_vec = Vec3::random_unit();
let lambertian_vec = diffuse_vec + hit_record.normal;
0.5 * self.ray_color(&Ray::new(ray.point, lambertian_vec), depth - 1, world)
} else {
Color::new(0.0, 0.0, 0.0)
}
}
};
return hit_c;
}
// v / |v|
let unit_direction = ray.direction / ray.direction.length();
let a = 0.5 * (unit_direction.y + 1.0);
// return background color.
(1.0 - a) * Color::new(1.0, 1.0, 1.0) + a * Color::new(0.5, 0.7, 1.0)
}
fn ray_color_sync(&self, ray: &Ray, depth: i8, world: RwLock<&HittableList>) -> Vec3 {
// 反射次数
if depth <= 0 {
return Vec3::new(0.0, 0.0, 0.0);
}
// 限制出射光线的角度(0.001经验值)
let hit_record = &mut HitRecord{
t: 0.0,
p: Vec3::new(0.0, 0.0, 0.0),
normal: Vec3::new(0.0, 0.0, 0.0),
front_face: false,
material: None,
};
let hit = world.read().unwrap().hit(&ray, 0.001, f32::MAX, hit_record);
if hit {
let hit_m = &hit_record.material;
let hc = &mut HitRecord{
t: hit_record.t,
p: hit_record.p,
normal: hit_record.normal,
front_face: hit_record.front_face,
material: None,
};
let hit_c = match hit_m {
Some(mk) => {
match mk {
// MaterialKind::Lambertian(l) => l.albedo,
MaterialKind::Lambertian(l) => self.scatter_color_sync(ray, hc, l, depth, world),
MaterialKind::Metal(m) => self.scatter_color_sync(ray, hc, m, depth, world),
MaterialKind::Dielectric(d) => self.scatter_color_sync(ray, hc, d, depth, world)
}
},
None => {
if hit_record.t >= 0.0 {
let diffuse_vec = Vec3::random_unit();
let lambertian_vec = diffuse_vec + hit_record.normal;
0.5 * self.ray_color_sync(&Ray::new(ray.point, lambertian_vec), depth - 1, world)
} else {
Color::new(0.0, 0.0, 0.0)
}
}
};
return hit_c;
}
// v / |v|
let unit_direction = ray.direction / ray.direction.length();
let a = 0.5 * (unit_direction.y + 1.0);
// return background color.
(1.0 - a) * Color::new(1.0, 1.0, 1.0) + a * Color::new(0.5, 0.7, 1.0)
}
// -> [x, y, 0]
fn random_square(&self, rng: &mut ThreadRng) -> Vec3 {
Vec3::new(rng.random_range(-0.5..0.1), rng.random_range(-0.5..0.1), 0.0)
}
fn scatter_color(
&self,
ray: &Ray,
hc: &mut HitRecord,
material: &impl Material,
depth: i8,
world: &HittableList,
) -> Color {
let mut scatted = Ray::new(Point::new(0.0, 0.0, 0.0), Vec3::random());
let mut attenuation = Color::new(1.0, 1.0, 1.0);
if material.scatter(ray, hc, &mut attenuation, &mut scatted) {
let r_c = self.ray_color(&scatted, depth - 1, world);
let sc_color = Vec3::new(
attenuation.x * r_c.x,
attenuation.y * r_c.y,
attenuation.z * r_c.z,
);
let color = Color::new(sc_color.x, sc_color.y, sc_color.z);
// if near_zero(color) {
// println!("near zero: {:?}", scatted.direction);
// }
color
} else {
Color::new(0.0, 0.0, 0.0)
}
}
fn scatter_color_sync(
&self,
ray: &Ray,
hc: &mut HitRecord,
material: &impl Material,
depth: i8,
world: RwLock<&HittableList>,
) -> Color {
let mut scatted = Ray::new(Point::new(0.0, 0.0, 0.0), Vec3::random());
let mut attenuation = Color::new(1.0, 1.0, 1.0);
if material.scatter(ray, hc, &mut attenuation, &mut scatted) {
let r_c = self.ray_color_sync(&scatted, depth - 1, world);
let sc_color = Vec3::new(
attenuation.x * r_c.x,
attenuation.y * r_c.y,
attenuation.z * r_c.z,
);
let color = Color::new(sc_color.x, sc_color.y, sc_color.z);
// if near_zero(color) {
// println!("near zero: {:?}", scatted.direction);
// }
color
} else {
Color::new(0.0, 0.0, 0.0)
}
}
}
+4 -1
View File
@@ -1,7 +1,10 @@
use crate::types_defined::Color;
impl Color {
pub fn to_color(self) -> String {
pub fn to_color(self) -> Self {
return Color::new(self.x * 256.0 , self.y * 256.0, self.z * 256.0);
}
pub fn to_color_str(self) -> String {
return format!("{} {} {}\n", (self.x * 256.0) as u8, (self.y * 256.0) as u8, (self.z * 256.0) as u8);
}
}
+55 -3
View File
@@ -1,5 +1,57 @@
use crate::types_defined::{HitRecord, Ray};
use crate::material::MaterialKind;
use crate::math_utils::{front_face_normal, is_front_face};
use crate::types_defined::{HitRecord, Ray, Vec3};
pub trait Hittable {
fn hit(self, r: &Ray, t_min: f32, t_max: f32) -> HitRecord;
pub trait Hittable: Send + Sync {
fn hit(&self, r: &Ray, t_min: f32, t_max: f32, hit_record: &mut HitRecord) -> bool;
}
pub struct HittableList {
pub objects: Vec<Box<dyn Hittable>>,
}
impl HittableList {
pub fn new() -> Self {
HittableList { objects: vec![] }
}
pub fn put(&mut self, object: Box<dyn Hittable>) {
let _ = &self.objects.push(object);
}
pub fn hit(&self, r: &Ray, t_min: f32, t_max: f32, hit_record: &mut HitRecord) -> bool {
let mut hits = false;
let mut closest_so_far = t_max;
let temp_hit_record = &mut HitRecord {
t: 0.0,
normal: Vec3::new(0.0, 0.0, 0.0),
p: Vec3::new(0.0, 0.0, 0.0),
front_face: false,
material: None,
};
for object in &self.objects {
let hit = object.hit(r, t_min, closest_so_far, temp_hit_record);
if hit && temp_hit_record.t < closest_so_far {
hits = true;
closest_so_far = temp_hit_record.t;
hit_record.t = temp_hit_record.t;
hit_record.p = temp_hit_record.p;
hit_record.front_face = is_front_face(r, temp_hit_record.normal);
hit_record.normal = front_face_normal(r, temp_hit_record.normal);
hit_record.material = if let Some(ref m) = temp_hit_record.material {
match m {
MaterialKind::Lambertian(l) => Some(MaterialKind::Lambertian(l.clone())),
MaterialKind::Metal(m) => Some(MaterialKind::Metal(m.clone())),
MaterialKind::Dielectric(d) => Some(MaterialKind::Dielectric(d.clone()))
}
} else {
None
};
}
}
hits
}
}
-57
View File
@@ -1,57 +0,0 @@
use crate::hittable::Hittable;
use crate::types_defined::{Color, Point, Ray, Sphere, Vec3};
pub fn gen_ray_sphere_normal_ppm_p3(width: i32, height: i32, camera_center: Vec3, viewport_top_left_pixel_center: Vec3, viewport_u_delta: Vec3, viewport_v_delta: Vec3) -> String {
let mut img_content = format!("P3\n{} {}\n255\n", width, height);
for j in 0..height {
if j % 10 == 0 {
println!("scan line {}/{} ", j + 1, height);
}
for i in 0..width {
// every pixcel's position
let pixel_center = viewport_top_left_pixel_center + (i as f32 * viewport_u_delta) + (j as f32 * viewport_v_delta);
// Vector(camera, pixcel)
let ray_direction = pixel_center - camera_center;
// ray
let r = Ray::new(camera_center, ray_direction);
/*
*/
let _sphere_center = Point::new(0.0, 0.0, -1.0);
let sphere_radius = 0.5;
let sphere = Sphere::new(_sphere_center, sphere_radius);
// determind color
let color = ray_color(&r, sphere);
// content
img_content.push_str(color.to_color().as_str());
img_content.push('\n');
}
}
img_content
}
/*
ray color functions
*/
fn ray_color<H: Hittable>(r: &Ray, h: H) -> Color {
let hr = h.hit(&r, 0.0, f32::MAX);
if hr.t >= 0.0 {
let inter_point = hr.p;
// 单位向量
let n = inter_point / inter_point.dot(inter_point);
// 法向量也是 -1~1 +1再x0.5让他落到颜色的区间
// return 0.5 * (n - sphere_center + Point::new(1.0, 1.0, 1.0))
return 0.5 * (hr.normal + Point::new(1.0, 1.0, 1.0))
}
// v / |v|
let unit_direction = r.direction / r.direction.length();
let a = 0.5*(unit_direction.y + 1.0);
return (1.0-a)*Color::new(1.0, 1.0, 1.0) + a*Color::new(0.5, 0.7, 1.0);
}
+117 -37
View File
@@ -1,53 +1,133 @@
use write_file_util::{write_image};
use std::fs::File;
use std::io::BufWriter;
use crate::image::{gen_ray_sphere_normal_ppm_p3};
use crate::types_defined::{Point, Vec3};
mod image;
mod write_file_util;
mod vec3;
mod ray;
use rand::{rng, Rng};
use crate::hittable::HittableList;
use crate::material::{Dielectric, Lambertian, MaterialKind, Metal};
use crate::ppm_writer::PPMWriter;
use crate::triangle::Triangle;
use crate::types_defined::{Camera, Color, Point, Sphere, Vec3};
mod camera;
mod color;
mod hittable;
mod ray;
mod sphere;
mod types_defined;
mod color;
mod vec3;
mod math_utils;
mod ppm_writer;
mod material;
mod triangle;
fn main() {
ray_sphere_normal_scene_render();
camera_render();
}
fn camera_render() {
let fov: f32 = 90.;
let scale = 1;
let width: i32 = 1920/scale;
let height: i32 = 1080/scale;
let sample_times = 20;
let reflect_depth = 20;
let pw_r = PPMWriter::new(
BufWriter::new(File::create("./target/ray_sphere_normal_scene_render.ppm").unwrap()),
width, height);
if let Err(e) = pw_r {
println!("创建文件报错:{}", e);
return;
}
let pw = &mut pw_r.unwrap();
let mut camera: Camera = Camera::new(
fov,
width,
width as f32 / height as f32,
Point::new(-0.0, 5.0, 5.0),
Point::new(0.0, 0.0, -1.0),
Vec3::new(0., 1., 0.),
sample_times,
reflect_depth,
2.0,
1.0,
pw
);
// world
// world objects(spheres)
let mut world = HittableList::new();
let plane_m = Some(MaterialKind::Lambertian(Lambertian{albedo: Color::new(0.508, 0.508, 0.508)}));
// // let plane2_m = Some(MaterialKind::Metal(Metal{albedo: Color::new(0.784,0.784,0.784)}));
// let center_m = Some(MaterialKind::Lambertian(Lambertian{albedo: Color::new(0.2, 0.5, 0.5)}));
// let left_m = Some(MaterialKind::Metal(Metal{albedo: Color::new(0.799, 0.599, 0.799), fuzz: 0.0005}));
// let left_behind_m = Some(MaterialKind::Lambertian(Lambertian{albedo: Color::new(0.799, 0.599, 0.599)}));
// let right_m = Some(MaterialKind::Metal(Metal{albedo: Color::new(0.8, 0.6, 0.2), fuzz: 0.003}));
// // 折射率 1.33
// let left_dia_m = Some(MaterialKind::Dielectric(Dielectric{albedo: Color::new(0.8, 0.6, 0.2), refraction_index: 1.00 / 1.33}));
// let left_dia_small_m = Some(MaterialKind::Dielectric(Dielectric{albedo: Color::new(0.8, 0.6, 0.2), refraction_index: 1.00 / 2.5}));
// world.put(Box::new(Sphere::new(Point::new(0.0, 0.0, -1.0), 0.5, center_m)));
// world.put(Box::new(Sphere::new(Point::new(-1.0, 0.0, -1.0), 0.5, left_dia_m)));
// world.put(Box::new(Sphere::new(Point::new(-1.0, 0.0, -1.0), 0.2, left_dia_small_m)));
// // world.put(Box::new(Sphere::new(Point::new(-1.0, 0.0, -1.0), 0.5, left_m)));
// world.put(Box::new(Sphere::new(Point::new(-3.5, 1.5, -5.5), 1.5, left_behind_m)));
// world.put(Box::new(Sphere::new(Point::new(1.0, 0.0, -1.0), 0.5, right_m)));
world.put(Box::new(Sphere::new(Point::new(0.0, -500.5, -1.0), 500.0, plane_m)));
// world.put(Box::new(Triangle::new(
// Point::new(0.0, 0.5, 0.0),
// Point::new(5.5, 0.5, 0.0),
// Point::new(5.5, 5.5, 0.0),
// Some(MaterialKind::Metal(Metal { albedo: Color::new(0.8, 0.6, 0.2), fuzz: 0.00001 }))
// )));
world.put(Box::new(Triangle::new(
Point::new(0.0, 0.5, 5.0),
Point::new(0.0, 5.5, -5.0),
Point::new(5.5, 0.5, 0.0),
Some(MaterialKind::Dielectric(Dielectric { albedo: Color::new(1.0, 1.0, 1.0), refraction_index: 1./1.5 }))
)));
gen_spheres(&mut world);
camera.render(&world);
}
fn ray_sphere_normal_scene_render() {
let aspect_ratio = 16.0/9.0;
let image_width = 400;
// aleast 1px
let image_height = ((image_width as f32 / aspect_ratio) as i32).max(1);
fn gen_spheres(world: &mut HittableList) {
let rng = &mut rng();
let viewport_height = 2.0;
let viewport_width = viewport_height * (image_width as f32 / image_height as f32);
for _ in 0..500 {
let x = rng.random_range(-20..20);
let z = rng.random_range(-20..20);
println!("set image({},{}), viewport({},{})", image_width, image_height, viewport_width, viewport_height);
let viewport_u = Vec3::new(viewport_width, 0.0, 0.0);
// image x--> right
// |
// y
// space: y up , x right , z back, -z front
let viewport_v = Vec3::new(0.0, -viewport_height, 0.0);
let radius = rng.random_range(0.01..0.8);
// width per pix
let viewport_u_delta = viewport_u / (image_width as f32);
// height per pix
let viewport_v_delta = viewport_v / (image_height as f32);
// R = random(0.7, 0.95) # 红通道
// G = random(R - 0.2, R + 0.2) # 绿通道(与 R 相近)
// B = random(min(R, G) - 0.15, max(R, G) + 0.15) # 蓝通道(与 R/G 相近)
// 基础值在 0.7~0.95 之间
let base:f32 = rng.random_range(0.7..0.95);
// 每个通道在 base 附近小幅波动(±0.15)
let r: f32 = rng.random_range((base - 0.15).max(0.6)..(base + 0.15).min(1.0));
let g: f32 = rng.random_range((base - 0.15).max(0.6)..(base + 0.15).min(1.0));
let b: f32 = rng.random_range((base - 0.15).max(0.6)..(base + 0.15).min(1.0));
// camerea position
let camera_center = Point::new(0.0, 0.0, 0.0);
// -z 1.0 viewport to camera
let focal_length = Vec3::new(0.0, 0.0, -1.0);
// camera position --> viewport center ---> top center --> top left
let viewport_top_left_pixel = camera_center + focal_length - viewport_u / 2.0 - viewport_v / 2.0;
// padding 0.5* delta u/v
let viewport_top_left_pixel_center = viewport_top_left_pixel - viewport_u_delta / 2.0 - viewport_v_delta / 2.0;
let materail_color = Color::new(r,g, b);
let ppm_content = gen_ray_sphere_normal_ppm_p3(image_width, image_height, camera_center, viewport_top_left_pixel_center, viewport_u_delta, viewport_v_delta);
let materail_kind = rng.random_range(0.0..1.0);
let material = if materail_kind <= 0.33333 {
MaterialKind::Lambertian(Lambertian { albedo: materail_color })
} else if materail_kind > 0.3 && materail_kind <= 0.66666 {
MaterialKind::Metal(Metal { albedo: materail_color, fuzz: 0.00001 })
} else {
MaterialKind::Dielectric(Dielectric { albedo: materail_color, refraction_index: 1./1.5 })
};
write_image(ppm_content, "./target/ray_sphere_normal_scene_render.ppm".to_string())
world.put(Box::new(Sphere::new(Point::new(x as f32, 0., z as f32), radius, Some(material))));
}
}
+109
View File
@@ -0,0 +1,109 @@
use crate::math_utils::{near_zero, reflect, refract};
use crate::types_defined::{Color, HitRecord, Ray, Vec3};
#[derive(Debug)]
pub enum MaterialKind {
Lambertian(Lambertian),
Metal(Metal),
Dielectric (Dielectric),
}
pub trait Material {
fn scatter(&self, r_in: &Ray, hit_record: &mut HitRecord, attenuation: &mut Color, ray: &mut Ray) -> bool;
}
#[derive(Debug)]
pub struct Lambertian {
pub albedo: Color,
}
impl Clone for Lambertian {
fn clone(&self) -> Self {
Lambertian {
albedo: self.albedo,
}
}
}
impl Material for Lambertian {
fn scatter(&self, r_in: &Ray, hit_record: &mut HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool {
let mut scatter_direction = r_in.direction + Vec3::random_unit_on_hemisphere(hit_record.normal);
if near_zero(scatter_direction) {
scatter_direction = hit_record.normal;
}
*scattered = Ray::new(hit_record.p, scatter_direction);
*attenuation = self.albedo.clone();
true
}
}
#[derive(Debug)]
pub struct Metal {
pub albedo: Color,
pub fuzz: f32
}
impl Clone for Metal {
fn clone(&self) -> Self {
Metal {
albedo: self.albedo,
fuzz: self.fuzz
}
}
}
impl Material for Metal {
fn scatter(&self, r_in: &Ray, hit_record: &mut HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool {
let reflected = reflect(r_in.direction, hit_record.normal);
let fuzz_relected = (reflected / reflected.length_squared()) + (Vec3::random_unit() * self.fuzz);
*scattered = Ray::new(hit_record.p, fuzz_relected);
*attenuation = self.albedo.clone();
true
}
}
impl Clone for Dielectric {
fn clone(&self) -> Self {
Dielectric {
albedo: self.albedo,
refraction_index: self.refraction_index
}
}
}
#[derive(Debug)]
pub struct Dielectric {
pub albedo: Color,
pub refraction_index: f32
}
impl Material for Dielectric {
fn scatter(&self, r_in: &Ray, hit_record: &mut HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool {
*attenuation = Color::new(1.0, 1.0, 1.0);
let unit_direction = r_in.direction.normalize();
let cos = -unit_direction.dot(hit_record.normal).min(1.0);
let sin = (1.0 - cos.powi(2)).sqrt();
let direction = if self.refraction_index * sin > 1.0 {
reflect(unit_direction, hit_record.normal)
} else {
refract(unit_direction, hit_record.normal, self.refraction_index)
};
// let refracted = refract(unit_direction, hit_record.normal, ri);
*scattered = Ray::new(hit_record.p, direction);
true
}
}
+82
View File
@@ -0,0 +1,82 @@
use std::f32::consts::PI;
use rand::{rng, Rng};
use crate::types_defined::{Ray, Vec3};
pub fn random_in_unit_disk() -> Vec3 {
let rng = &mut rng();
loop {
let p = Vec3::new(rng.random_range(-1.0..1.0), rng.random_range(-1.0..1.0), 0.0);
if p.length_squared() < 1.0 {
return p;
}
}
}
pub fn degrees_to_radians(deg: f32) -> f32 {
deg * PI / 180.
}
pub fn clamp(value: f32, low: f32, high: f32) -> f32 {
if value < low {
return low;
}
if value > high {
return high;
}
return value;
}
pub fn near_zero(v: Vec3) -> bool {
const EPSILON: f32 = 1e-4;
v.x.abs() < EPSILON && v.y.abs() < EPSILON && v.z.abs() < EPSILON
}
pub fn reflect(v: Vec3, n: Vec3) -> Vec3 {
v - ((2.0 * v.dot(n)) * n)
}
// v, normal, 折射率
pub fn refract(uv: Vec3, n: Vec3, etai_over_etat: f32) -> Vec3 {
// auto cos_theta = std::fmin(dot(-uv, n), 1.0);
// vec3 r_out_perp = etai_over_etat * (uv + cos_theta*n);
// vec3 r_out_parallel = -std::sqrt(std::fabs(1.0 - r_out_perp.length_squared())) * n;
// return r_out_perp + r_out_parallel;
let cos_theta = (-uv.dot(n)).min(1.0);
let r_out_perp = etai_over_etat * (uv + cos_theta * n);
let r_out_parallel = -(f32::sqrt(f32::abs(1.0 - r_out_perp.length()))) * n;
r_out_perp + r_out_parallel
// let v_normalized = v;
// let n_normalized = n.normalize();
// let cos_theta = f32::min(-v_normalized.dot(n_normalized), 1.0);
// let sin_theta = (1.0 - cos_theta * cos_theta).sqrt();
// // 检查是否全内反射(无法折射)
// if etai_over_etat * sin_theta > 1.0 {
// return reflect(v_normalized, n)
// }
// let r_out_perp = etai_over_etat * (v_normalized + cos_theta * n_normalized);
// let discriminant = 1.0 - r_out_perp.length_squared();
// // if discriminant < 0.0 {
// // return -1.0 * v; // 全内反射,返回零向量(或改为反射)
// // }
// let r_out_parallel = -discriminant * n_normalized;
// r_out_perp + r_out_parallel
}
pub fn is_front_face(r: &Ray, outward_normal: Vec3) -> bool {
r.direction.dot(outward_normal) < 0.0
}
pub fn front_face_normal(r: &Ray, outward_normal: Vec3) -> Vec3 {
if is_front_face(r, outward_normal) {
outward_normal
} else {
-1. * outward_normal
}
}
+36
View File
@@ -0,0 +1,36 @@
use std::io::{BufWriter, Write, Result};
use crate::types_defined::Color;
pub struct PPMWriter<W: Write> {
writter: BufWriter<W>
}
impl<W: Write> PPMWriter<W> {
/// 创建新的 PPM 写入器
///
/// 参数:
/// writer: 实现 Write 的目标
/// width: 图像宽度
/// height: 图像高度
pub fn new(mut inner: W, width: i32, height: i32) -> Result<Self> {
// 写入 PPM 头 (P6 二进制格式)
let header = format!("P6\n{} {}\n255\n", width, height);
inner.write_all(header.as_bytes())?;
Ok(PPMWriter { writter: BufWriter::new(inner) })
}
/// 写入单个像素 (高性能实现)
#[inline]
pub fn write(&mut self, color: Color) -> Result<()> {
// 直接写入字节数组避免额外内存分配
let bytes = [color.x as u8, color.y as u8, color.z as u8];
self.writter.write_all(&bytes)
}
/// 完成写入并刷新缓冲区
pub fn finish(&mut self) -> Result<()> {
self.writter.flush()
}
}
+32 -10
View File
@@ -1,17 +1,20 @@
use crate::hittable;
use crate::material::{MaterialKind};
use crate::math_utils::{front_face_normal, is_front_face};
use crate::types_defined::{HitRecord, Point, Ray, Sphere};
impl Sphere {
pub fn new(c: Point, r: f32) -> Self {
pub fn new(c: Point, r: f32, m: Option<MaterialKind>) -> Self {
Self {
center: c,
radius: r,
material: m,
}
}
}
impl hittable::Hittable for Sphere {
fn hit(self, r: &Ray, t_min: f32, t_max: f32) -> HitRecord {
fn hit(&self, r: &Ray, t_min: f32, t_max: f32, hit_record: &mut HitRecord) -> bool {
let a: f32 = r.direction.length_squared();
let h = r.direction.dot(self.center - r.point);
// let b = -2.0 * r.direction.dot(sphere_center - r.point);
@@ -19,7 +22,11 @@ impl hittable::Hittable for Sphere {
let discriminant = h * h - a * c;
// // 两个焦点
if discriminant < 0.0 {
return false;
}
// // 两个交点
let disc_sqrt = discriminant.sqrt();
let near = (h - disc_sqrt) / a;
let far = (h + disc_sqrt) / a;
@@ -28,14 +35,29 @@ impl hittable::Hittable for Sphere {
if root <= t_min || root >= t_max {
root = far;
if root <= t_min || root >= t_max {
root = -1.0;
hit_record.t = -1.0;
return false
}
}
HitRecord {
t: root,
p: r.at(root),
normal: r.at(root) - self.center,
}
let p = r.at(root);
let normal = (p - self.center) / self.radius;
hit_record.t = root;
hit_record.p = p;
hit_record.normal = front_face_normal(r, normal);
hit_record.front_face = is_front_face(r, normal);
hit_record.material = if let Some(ref m) = self.material {
match m {
MaterialKind::Lambertian(l) => Some(MaterialKind::Lambertian(l.clone())),
MaterialKind::Metal(m) => Some(MaterialKind::Metal(m.clone())),
MaterialKind::Dielectric(d) => Some(MaterialKind::Dielectric(d.clone())),
}
} else {
None
};
true
}
}
+100
View File
@@ -0,0 +1,100 @@
use crate::{hittable::Hittable, material::MaterialKind, math_utils::is_front_face, types_defined::{HitRecord, Point, Ray, Vec3}};
pub struct Triangle {
pub points: Vec<Vec3>,
pub material: Option<MaterialKind>,
}
impl Triangle {
pub fn new(p1: Point, p2: Point, p3: Point, material: Option<MaterialKind>,) -> Self {
Triangle {
points: vec![
p1, p2, p3
],
material: material
}
}
}
impl Hittable for Triangle {
fn hit(&self, r: &Ray, t_min: f32, t_max: f32, hit_record: &mut HitRecord) -> bool {
let p0 = self.points[0];
let p1 = self.points[1];
let p2 = self.points[2];
let u = Vec3 {
x: p1.x - p0.x,
y: p1.y - p0.y,
z: p1.z - p0.z,
};
let v = Vec3 {
x: p2.x - p0.x,
y: p2.y - p0.y,
z: p2.z - p0.z,
};
let normal = Vec3 {
x: u.y * v.z - u.z * v.y,
y: u.z * v.x - u.x * v.z,
z: u.x * v.y - u.y * v.x,
};
let denominator = r.direction.x * normal.x + r.direction.y * normal.y + r.direction.z * normal.z;
if denominator.abs() < std::f32::EPSILON {
return false;
}
let w0 = Vec3 {
x: r.point.x - p0.x,
y: r.point.y - p0.y,
z: r.point.z - p0.z,
};
let t = -(w0.x * normal.x + w0.y * normal.y + w0.z * normal.z) / denominator;
if t < t_min || t > t_max {
return false;
}
let P = Vec3 {
x: r.point.x + t * r.direction.x,
y: r.point.y + t * r.direction.y,
z: r.point.z + t * r.direction.z,
};
let w = Vec3 {
x: P.x - p0.x,
y: P.y - p0.y,
z: P.z - p0.z,
};
let uv_cross = Vec3 {
x: u.y * v.z - u.z * v.y,
y: u.z * v.x - u.x * v.z,
z: u.x * v.y - u.y * v.x,
};
let wv_cross = Vec3 {
x: w.y * v.z - w.z * v.y,
y: w.z * v.x - w.x * v.z,
z: w.x * v.y - w.y * v.x,
};
let uw_cross = Vec3 {
x: u.y * w.z - u.z * w.y,
y: u.z * w.x - u.x * w.z,
z: u.x * w.y - u.y * w.x,
};
let alpha = (wv_cross.x * normal.x + wv_cross.y * normal.y + wv_cross.z * normal.z) / (uv_cross.x * normal.x + uv_cross.y * normal.y + uv_cross.z * normal.z);
let beta = (uw_cross.x * normal.x + uw_cross.y * normal.y + uw_cross.z * normal.z) / (uv_cross.x * normal.x + uv_cross.y * normal.y + uv_cross.z * normal.z);
if alpha >= 0.0 && beta >= 0.0 && alpha + beta <= 1.0 {
hit_record.p = P;
hit_record.t = t;
hit_record.normal = normal;
hit_record.front_face = is_front_face(r, normal);
hit_record.material = if let Some(ref m) = self.material {
match m {
MaterialKind::Lambertian(l) => Some(MaterialKind::Lambertian(l.clone())),
MaterialKind::Metal(m) => Some(MaterialKind::Metal(m.clone())),
MaterialKind::Dielectric(d) => Some(MaterialKind::Dielectric(d.clone())),
}
} else {
None
};
return true;
}
false
}
}
+36 -1
View File
@@ -1,3 +1,7 @@
use std::{fs::File, io::{BufWriter}};
use crate::{material::MaterialKind};
use crate::ppm_writer::PPMWriter;
/*
* [3]
*/
@@ -16,13 +20,43 @@ fn: P(t) = t*b
#[derive(Debug)]
pub struct Ray {
pub point: Point,
pub direction: Vec3
pub direction: Vec3,
}
pub struct HitRecord {
pub t: f32,
pub p: Vec3,
pub normal: Vec3,
pub front_face: bool,
pub material: Option<MaterialKind>,
}
pub struct Camera<'a> {
pub image_width: i32,
pub image_height: i32,
// pub aspect_ratio: f32,
// pub viewport_width: f32,
// pub viewport_height: f32,
pub center: Point,
// pub look_at: Point,
// pub v_up: Vec3,
// pub focal_length: Vec3,
// pub viewport_u: Vec3,
// pub viewport_v: Vec3,
pub defocus_angle: f32,
pub defocus_disk_u: Vec3,
pub defocus_disk_v: Vec3,
pub viewport_u_delta: Vec3,
pub viewport_v_delta: Vec3,
pub viewport_top_left_pixel_center: Vec3,
// sample times
pub sample_times: i8,
pub reflect_depth: i8,
// writer
pub ppm_file_writer: &'a mut PPMWriter<BufWriter<File>>
}
@@ -34,4 +68,5 @@ pub struct HitRecord {
pub struct Sphere {
pub center: Point,
pub radius: f32,
pub material: Option<MaterialKind>,
}
+64 -1
View File
@@ -1,6 +1,7 @@
use std::{fmt::Display, ops::{Add, AddAssign, Div, Mul, MulAssign, Sub}};
use std::{fmt::Display, iter::Sum, ops::{Add, AddAssign, Div, Mul, MulAssign, Sub}};
use std::fmt;
use rand::{rng, Rng};
use crate::types_defined::Vec3;
impl Display for Vec3 {
@@ -36,6 +37,45 @@ impl Vec3 {
return self.length_squared().sqrt();
}
pub fn normalize(self) -> Vec3 {
let length = self.length_squared().sqrt();
if length != 0. {
let new_x = self.x / length;
let new_y = self.y / length;
let new_z = self.z / length;
Vec3 { x: new_x, y: new_y, z: new_z }
} else {
Vec3 { x: 0., y: 0., z: 0. }
}
}
pub fn random_range(min: f32, max: f32) -> Self {
let rng = &mut rng();
Vec3::new(rng.random_range(min..max), rng.random_range(min..max), rng.random_range(min..max))
}
pub fn random() -> Self {
let rng = &mut rng();
Vec3::new(rng.random(), rng.random(), rng.random())
}
pub fn random_unit() -> Self {
loop {
let v = Vec3::random();
if 1e-160 < v.length_squared() && v.length_squared() <= 1.0 {
return v / v.length_squared();
}
}
}
pub fn random_unit_on_hemisphere(point_normal: Vec3) -> Self {
let on_unit_sphere = Vec3::random_unit();
if on_unit_sphere.dot(point_normal) > 0.0 {
on_unit_sphere
} else {
Vec3::new(0.0, 0.0, 0.0) - on_unit_sphere
}
}
}
impl Add for Vec3 {
@@ -97,6 +137,15 @@ impl Div<f32> for Vec3 {
}
}
// Vec * Vec
impl Mul<Vec3> for Vec3 {
type Output = Self;
fn mul(self, rhs: Vec3) -> Self::Output {
Vec3::new(self.x * rhs.x, self.y * rhs.y, self.z * rhs.z)
}
}
// 加法赋值: Vec3 += Vec3
impl AddAssign for Vec3 {
fn add_assign(&mut self, other: Self) {
@@ -109,4 +158,18 @@ impl MulAssign<f32> for Vec3 {
fn mul_assign(&mut self, scalar: f32) {
*self = *self * scalar;
}
}
impl Sum for Vec3 {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
let mut sum_x = 0.0;
let mut sum_y = 0.0;
let mut sum_z = 0.0;
for vec in iter {
sum_x += vec.x;
sum_y += vec.y;
sum_z += vec.z;
}
Vec3 { x: sum_x, y: sum_y, z: sum_z }
}
}
-12
View File
@@ -1,12 +0,0 @@
use std::{fs::File, io::{BufWriter, Write}};
pub fn write_image(content: String, file_path: String) {
if let Ok(file) = File::create(file_path) {
let mut writer = BufWriter::new(file);
if let Err(e) = writer.write(content.as_bytes()) {
println!("写出失败:{:#}", e)
}
} else {
println!("PPM保存失败")
}
}