Expand description
§屏幕→世界射线投射
提供鼠标拾取和射线测试所需的数学工具函数。
§功能
screen_to_ray— 将屏幕坐标转换为世界空间射线ray_plane_intersection— 射线与水平平面相交测试ray_sphere_intersection— 射线与球体相交测试
§使用示例
use anvilkit_render::renderer::raycast::*;
use glam::{Vec2, Vec3, Mat4};
// 从屏幕中心发射射线
let view_proj = Mat4::perspective_lh(60f32.to_radians(), 16.0 / 9.0, 0.1, 100.0)
* Mat4::look_at_lh(Vec3::new(0.0, 5.0, -10.0), Vec3::ZERO, Vec3::Y);
let (origin, dir) = screen_to_ray(Vec2::new(640.0, 360.0), Vec2::new(1280.0, 720.0), &view_proj);
// 测试射线是否命中 y=0 平面
if let Some(hit) = ray_plane_intersection(origin, dir, 0.0) {
println!("Hit at {:?}", hit);
}Functions§
- ray_
plane_ intersection - 射线与水平平面相交测试
- ray_
sphere_ intersection - 射线与球体相交测试
- screen_
to_ ray - 将屏幕坐标转换为世界空间射线