mrdoob / three.js

JavaScript 3D Library.
https://threejs.org/
MIT License
102.78k stars 35.38k forks source link

Raytracing material & lighting #27645

Closed Hudsxn closed 9 months ago

Hudsxn commented 9 months ago

Description

A good addition to threejs would be a raytracing material and raytracing light, For devices that don't have raytracing capabilities, a property like fallbackMaterial could be used to take all the heavy lifting out for developers. I know different vendors often have their own version of raytracing too, so this could be a polymorphic object with multiple implementations for different GPU vendors.

Solution

Create an abstract raytracing material, in which there's a child descendant class based on GPU vendor, This could even have automatic selection based on the hardware:

let gl = canvas.getContext("webgl");
let raytracingExtension = gl.getExtension("EXT_rasterization_raytracing");
if (raytracingExtension) {
    // Ray tracing is supported
    console.log("Ray tracing is supported!");
} else {
    // Ray tracing is not supported
    console.log("Ray tracing is not supported.");
}

The same for lighting too, create a base Raytracing light and have child descendants also based off of vendors.

Alternatives

N/A

Additional context

This could then be imported & used via:

import { RayTracingMaterial, RayTracingLight } from 'three';
CodyJasonBennett commented 9 months ago

This is complex enough to warrant a library or team of its own to maintain like https://github.com/gkjohnson/three-gpu-pathtracer. I will add that there are no hardware extensions for raytracing on web, nor will there be with WebGPU https://github.com/gpuweb/gpuweb/issues/535. I do urge you to implement acceleration structures yourself in software since device support will never be good enough for general use. Best case-scenario, hypothetically this is implemented as an extension in WebGPU and is actually implemented everywhere, you will always need a fallback for device support. If you don't know what this means, educate yourself on raytracing; you do not need RTX or whatever hardware support to perform raytracing and can implement everything yourself.

mrdoob commented 9 months ago

The hardware is definitely not there... Maybe in 10 years?

CodyJasonBennett commented 9 months ago

FWIW, the newest high-end iPhones have hardware raytracing support. I haven't looked into how this is exposed in Metal, but the timeline for that to diffuse into the market is definitely closer to a decade for iOS alone. You can implement a BVH in a weekend.