gkjohnson / three-gpu-pathtracer

Path tracing renderer and utilities for three.js built on top of three-mesh-bvh.
https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/index.html
MIT License
1.28k stars 125 forks source link

spot / point lights do not work on Mac / iOS #471

Closed robertoranon closed 7 months ago

robertoranon commented 7 months ago

While I look into the "invisible" area light feature, I thought I'd give it also a try to fix the spot/point light issue on macOS / iOS. In short, in the spot light example, the spot light simply does not work, we get only the environment lighting. I also tried to construct an example using a standard point light, to the same effect. So, in short, point/spot lights do not seem to work at all (to be clear, no error is reported in the console).

I've taken a look at PhysicalPathTracingMaterial.js, now that I start to better understand how it works. I believe the only place where point lights are taken into consideration is at line 440:

// next event estimation
#if FEATURE_MIS

gl_FragColor.rgb += directLightContribution( - ray.direction, surf, state, hitPoint );

#endif

However, disabling MIS in Mac/iOS to avoid crashes obviously takes out this part. If I am right, is there a way to make direct light work without MIS?

gkjohnson commented 7 months ago

It's by-design that point, spot, and directional lights only work with multiple importance sampling. Point lights, directional lights, and spot lights are infinitely-small light surfaces so in practice random path scattering will never hit these points. The only way to sample them is to perform direct-light-sampling which (as far as I understand) requires multiple importance sampling to account for the random direct light position sample probability in conjunction with the surface scatter direction probability.

Generally getting the MIS path to work would likely require code simplification to avoid the types of compiler bugs that MacOS and iOS are running in to. In the past I've debugged these things by commenting code out and slowly adding it until it breaks - effectively doing a manual binary search. It's tedious but effective. It's particularly difficult on MacOS right now because of how hard the browser crashes, though

robertoranon commented 7 months ago

Making MIS work was exactly my thought after writing the first message :) so I set out to discover what in the MIS path is causing the crash, and it's exactly the direct lights computation. More specifically, in directLightContributions.glsls.js, if I comment out the body of the 'then' block in directLightContribution, which I believe covers direct lights (?), then ... no crashes. And much better environment lighting on Mac :) . I've to stop on this for today, I'll keep on digging into that code part to see where's the issue.

robertoranon commented 7 months ago

I've narrowed the crash to two lines of code, making a PR

gkjohnson commented 7 months ago

Lets close this for now since it's intended that we can't use them without MIS and we can see what we can do about #472.