bevy/examples/3d
robtfm 67d92e9b85
light renderlayers (#10742)
# Objective

add `RenderLayers` awareness to lights. lights default to
`RenderLayers::layer(0)`, and must intersect the camera entity's
`RenderLayers` in order to affect the camera's output.

note that lights already use renderlayers to filter meshes for shadow
casting. this adds filtering lights per view based on intersection of
camera layers and light layers.

fixes #3462 

## Solution

PointLights and SpotLights are assigned to individual views in
`assign_lights_to_clusters`, so we simply cull the lights which don't
match the view layers in that function.

DirectionalLights are global, so we 
- add the light layers to the `DirectionalLight` struct
- add the view layers to the `ViewUniform` struct
- check for intersection before processing the light in
`apply_pbr_lighting`

potential issue: when mesh/light layers are smaller than the view layers
weird results can occur. e.g:
camera = layers 1+2
light = layers 1
mesh = layers 2

the mesh does not cast shadows wrt the light as (1 & 2) == 0.
the light affects the view as (1+2 & 1) != 0. 
the view renders the mesh as (1+2 & 2) != 0.

so the mesh is rendered and lit, but does not cast a shadow. 

this could be fixed (so that the light would not affect the mesh in that
view) by adding the light layers to the point and spot light structs,
but i think the setup is pretty unusual, and space is at a premium in
those structs (adding 4 bytes more would reduce the webgl point+spot
light max count to 240 from 256).

I think typical usage is for cameras to have a single layer, and
meshes/lights to maybe have multiple layers to render to e.g. minimaps
as well as primary views.

if there is a good use case for the above setup and we should support
it, please let me know.

---

## Migration Guide

Lights no longer affect all `RenderLayers` by default, now like cameras
and meshes they default to `RenderLayers::layer(0)`. To recover the
previous behaviour and have all lights affect all views, add a
`RenderLayers::all()` component to the light entity.
2023-12-12 19:45:37 +00:00
..
3d_gizmos.rs Rename Input to ButtonInput (#10859) 2023-12-06 20:32:34 +00:00
3d_scene.rs Update default ClearColor to better match Bevy's branding (#10339) 2023-11-03 12:57:38 +00:00
3d_shapes.rs Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00
3d_viewport_to_world.rs Split Ray into Ray2d and Ray3d and simplify plane construction (#10856) 2023-12-06 14:09:04 +00:00
anti_aliasing.rs Rename Input to ButtonInput (#10859) 2023-12-06 20:32:34 +00:00
atmospheric_fog.rs Rename Input to ButtonInput (#10859) 2023-12-06 20:32:34 +00:00
blend_modes.rs Rename Input to ButtonInput (#10859) 2023-12-06 20:32:34 +00:00
bloom_3d.rs Rename Input to ButtonInput (#10859) 2023-12-06 20:32:34 +00:00
deferred_rendering.rs Rename Input to ButtonInput (#10859) 2023-12-06 20:32:34 +00:00
fog.rs Rename Input to ButtonInput (#10859) 2023-12-06 20:32:34 +00:00
generate_custom_mesh.rs Rename Input to ButtonInput (#10859) 2023-12-06 20:32:34 +00:00
lighting.rs Rename Input to ButtonInput (#10859) 2023-12-06 20:32:34 +00:00
lines.rs Add consuming builder methods for more ergonomic Mesh creation (#10056) 2023-10-09 19:47:41 +00:00
load_gltf.rs Update default ClearColor to better match Bevy's branding (#10339) 2023-11-03 12:57:38 +00:00
orthographic.rs Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00
parallax_mapping.rs Rename Input to ButtonInput (#10859) 2023-12-06 20:32:34 +00:00
parenting.rs Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00
pbr.rs Update default ClearColor to better match Bevy's branding (#10339) 2023-11-03 12:57:38 +00:00
render_to_texture.rs light renderlayers (#10742) 2023-12-12 19:45:37 +00:00
shadow_biases.rs Rename Input to ButtonInput (#10859) 2023-12-06 20:32:34 +00:00
shadow_caster_receiver.rs Rename Input to ButtonInput (#10859) 2023-12-06 20:32:34 +00:00
skybox.rs Rename Input to ButtonInput (#10859) 2023-12-06 20:32:34 +00:00
spherical_area_lights.rs Update default ClearColor to better match Bevy's branding (#10339) 2023-11-03 12:57:38 +00:00
split_screen.rs Refactor EventReader::iter to read (#9631) 2023-08-30 14:20:03 +00:00
spotlight.rs Rename Input to ButtonInput (#10859) 2023-12-06 20:32:34 +00:00
ssao.rs Rename Input to ButtonInput (#10859) 2023-12-06 20:32:34 +00:00
texture.rs Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00
tonemapping.rs Rename Input to ButtonInput (#10859) 2023-12-06 20:32:34 +00:00
transmission.rs Rename Input to ButtonInput (#10859) 2023-12-06 20:32:34 +00:00
transparency_3d.rs Fixed several missing links in docs. (#8117) 2023-04-23 17:28:36 +00:00
two_passes.rs Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00
update_gltf_scene.rs Update default ClearColor to better match Bevy's branding (#10339) 2023-11-03 12:57:38 +00:00
vertex_colors.rs Schedule-First: the new and improved add_systems (#8079) 2023-03-18 01:45:34 +00:00
wireframe.rs Rename Input to ButtonInput (#10859) 2023-12-06 20:32:34 +00:00