three-gpu-pathtracer icon indicating copy to clipboard operation
three-gpu-pathtracer copied to clipboard

Add support for rendering area light helper as area light surface

Open gkjohnson opened this issue 10 months ago • 4 comments

Would require special handling since the material is set to a mesh basic material. Perhaps this project can export one?

gkjohnson avatar Apr 05 '24 14:04 gkjohnson

Hi, I would like to take this one :)

Have you thought in something?

We can create a new folder just for "Helper" (this could work for the PhysicalCamera, and other components in the future), and create our own helper. If we do so, let me tried to add some "emissive" value or something to make them feel like a helper

--/src ----/helper

The other option might be to traverse the scene in searching for any basicMaterial to override them?

Which want you prefer? (any other alternative is accepted)

JaimeTorrealba avatar May 18 '24 18:05 JaimeTorrealba

Hey Jaime! This would be great!

Sorry for the slow response - I was thinking about the best way to implement this. Thinking about it more I'm not sure if adding geometry via something like RectAreaLightHelper is the best way to add support for rendering the light surface. They will doubly contribute to the lighting of the scene because the geometry will be considered emissive to get the right brightness.

I'm thinking adding an extra optional field like renderLightSurface to the light objects that defaults to false would be the best place to start. This would involve modifying the light info uniform texture to add a new field for rendering it and then finding the closest visible light on the first ray to check for collisions.

How does that sound?

gkjohnson avatar May 24 '24 03:05 gkjohnson

That's sound a good idea, but I have doubts where to start and what should be the final render "helper"

sorry if my questions are too basic, I'm new here, and I would like to contribute this project (who looks amazing)

Checking the code, I see that you have

src/uniforms/LightsInfoUniformStruct.js and src/shader/structs/lights_struct.glsl.js where changes have to be made in order to add this new field.

What I'm not sure if changes have to be made in something like src/shader/sampling/light_sampling_functions.glsl.js in order to check the collision?

I don't know if you're in discord, but maybe a more fluid communication could help BboyJT#2838

JaimeTorrealba avatar May 24 '24 17:05 JaimeTorrealba

Here's a breakdown of what I see as needing to change in order to add this feature. Lets start with the Area Lights and then we can consider adding the option for other light surface (like spot lights with a radius) if we want:

  • Add a field like ShapedAreaLight.visibleSurface to the ShapedAreaLight class.
  • The LightsInfoUniformStruct packs all light data into a texture so we'll need to add this new field to the class. Each light is allocated six pixels of data (6 * 4 float values) and you can see that the area light only uses up to 4 pixels. So if the surface is visible we can set the last value of pixel 6 (the alpha component) to "1". This value is unused by all light types so it's a good one to set.
  • The readLightInfo function in the lights_struct.glsl.js file reads the texture data and unpacks it for all lights and returns a struct with all the light information. We'll wan to add a new "surfaceVisible" field to the light struct so we can check this later.
  • Finally, if we're tracing the first ray then we'll want to find the first visible-surface light and return the color of the light as the result of the path trace. See this section here.

Let me know if that all makes sense. And feel free to make a preliminary PR if you'd like me to take a look. Appreciate you digging into this!

gkjohnson avatar May 29 '24 03:05 gkjohnson