amberjs 1.16.1

JavaScript/TypeScript runtime built with Rust and V8
Documentation

use std::collections::{BTreeMap, HashMap};

// 实时光线追踪渲染器
/// 弹射次数限制
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BounceLimit {
    /// 无限制 (直到能量耗尽)
    Unlimited,
    /// 限制次数
    Limited(u32),
}
impl Default for BounceLimit {
    fn default() -> Self {
        Self::Limited(4)
    }
}
/// 光线追踪配置
#[derive(Debug, Clone)]
pub struct RayTracerConfig {
    /// 最大弹射次数
    pub max_bounces: BounceLimit,
    /// 每像素采样数
    pub samples_per_pixel: u32,
    /// 启用降噪
    pub enable_denoising: bool,
    /// 启用全局光照
    pub enable_global_illumination: bool,
}
impl Default for RayTracerConfig {
    fn default() -> Self {
        Self {
            max_bounces: BounceLimit::Limited(4),
            samples_per_pixel: 1,
            enable_denoising: true,
            enable_global_illumination: false,
        }
    }
}
/// 光线追踪渲染器
pub struct RayTracer {
    /// 配置
    config: RayTracerConfig,
    /// 累积帧数
    accumulated_frames: u64,
}
impl RayTracer {
    /// 创建光线追踪渲染器
    pub fn new(config: RayTracerConfig) -> Result<Self, RayTracerError> {
        Ok(Self {
            config,
            accumulated_frames: 0,
        })
    }
    /// 获取最大弹射次数
    pub fn max_bounces(&self) -> u32 {
        match self.config.max_bounces {
            BounceLimit::Unlimited => u32::MAX,
            BounceLimit::Limited(n) => n,
        }
    }
    /// 降噪是否启用
    pub fn denoising_enabled(&self) -> bool {
        self.config.enable_denoising
    }
    /// 全局光照是否启用
    pub fn global_illumination_enabled(&self) -> bool {
        self.config.enable_global_illumination
    }
    /// 获取每像素采样数
    pub fn samples_per_pixel(&self) -> u32 {
        self.config.samples_per_pixel
    }
    /// 追踪单条光线
    pub fn trace_ray(&self, origin: [f32; 3], direction: [f32; 3]) -> RayHit {
        // 简化实现
        RayHit {
            hit: true,
            distance: 1.0,
            position: [
                origin[0] + direction[0],
                origin[1] + direction[1],
                origin[2] + direction[2],
            ],
            normal: [0.0, 1.0, 0.0],
            color: [1.0, 1.0, 1.0, 1.0],
        }
    }
    /// 渲染场景
    pub fn render(
        &mut self,
        width: u32,
        height: u32,
    ) -> Result<Vec<[f32; 4]>, RayTracerError> {
        self.accumulated_frames += 1;
        let pixel_count: _ = (width * height) as usize;
        Ok(vec![[0.5, 0.5, 0.5, 1.0]; pixel_count])
    }
    /// 重置累积
    pub fn reset_accumulation(&mut self) {
        self.accumulated_frames = 0;
    }
}
/// 光线命中结果
#[derive(Debug, Clone)]
pub struct RayHit {
    /// 是否命中
    pub hit: bool,
    /// 命中距离
    pub distance: f32,
    /// 命中位置
    pub position: [f32; 3],
    /// 命中点法线
    pub normal: [f32; 3],
    /// 颜色
    pub color: [f32; 4],
}
/// 光线追踪错误
#[derive(Debug, Clone)]
pub enum RayTracerError {
    /// 初始化失败
    InitializationFailed(String),
    /// 渲染失败
    RenderFailed(String),
    /// GPU 不支持
    GpuNotSupported,
}
impl std::fmt::Display for RayTracerError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InitializationFailed(msg) => write!(f, "光线追踪初始化失败: {}", msg),
            Self::RenderFailed(msg) => write!(f, "渲染失败: {}", msg),
            Self::GpuNotSupported => write!(f, "GPU 不支持光线追踪"),
        }
    }
}
impl std::error::Error for RayTracerError {}