playcanvas / engine

JavaScript game engine built on WebGL, WebGPU, WebXR and glTF
https://playcanvas.com
MIT License
9.55k stars 1.34k forks source link

Feature Request: Subsurface Scattering #5344

Closed defektu closed 12 months ago

defektu commented 1 year ago

I was working on subsurface scattering implementation over Lit Shader. I did not wanted to open a PR because I'm not sure about implementation is mathematically right.

Here is the project that I've cleaned up for you guys. https://playcanvas.com/editor/scene/1751652 (?debug=true flag opens up gui.) https://playcanv.as/p/KW8u9ebM/?debug=true

Implementation

  1. falloffLinearPS
    • Added LIGHTS_AVAILABLE for cross-checking.
    • pointLightCount - total point count
    • lightPointsPos[] - Array for light positions
    • lightPointsRad[] - Array for light rads;
  2. lightDirPointPS - sets light positions
  3. refractionDynamicPS
    • addSSS() - adds up sss effect on top of refraction shader.

Screenshots

SSS on image SSS off image

This is an old build but you can change some variables with dat.gui (debug=true flag opens up gui.): https://playcanv.as/index/ca8102ee image

defektu commented 1 year ago

Made a ViolentMonkey script for quick test in editor. https://github.com/defektu/Playcanvas-Editor-Scripts Thanks to @yaustar for good example

defektu commented 12 months ago

Hi @mvaligursky , Do you think it would be a good feature implementation? I might create a PR about it.

mvaligursky commented 12 months ago

To be honest, perhaps not at the moment. I have few comments at the moment:

defektu commented 12 months ago

Thanks for the feedback Martin. Newly introduced arrays can be eliminated by using light" + i + "_position & light" + i + "_radius in lit-shader.js I assume. On physicaly correct part, I need to cross-check it cause of quick implementation. If you consider adding it as a render feature after glTF material model milestones just ping me up so I might work over it.

mvaligursky commented 12 months ago

Newly introduced arrays can be eliminated by using light" + i + "_position & light" + i + "_radius in lit-shader.js I assume.

But those are only available in non-clustered mode. Clustered mode encodes lights in textures and so this is harder to do this way.

mvaligursky commented 12 months ago

If you consider adding it as a render feature after glTF material model milestones just ping me up so I might work over it.

Absolutely. Please feel free to create an issue to implement SSS and link this PR there as WIP, so that we won't forget, thanks a lot!