Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement lightmaps. #10231

Merged
merged 45 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
5e593aa
Implement lightmaps.
pcwalton Oct 17, 2023
2dd3d2c
Merge remote-tracking branch 'origin/main' into lightmaps
pcwalton Oct 23, 2023
ff21b6c
Remove `MeshLayoutKey` as it conflicts with other work
pcwalton Oct 23, 2023
44a64a9
Rename `uv_a` back to `uv` per consensus
pcwalton Oct 24, 2023
2b6344d
Merge remote-tracking branch 'origin/main' into lightmaps
pcwalton Nov 22, 2023
c21aa58
Merge fallout
pcwalton Nov 23, 2023
130e110
Add exposure
pcwalton Nov 24, 2023
403760b
Fix error logged in console
pcwalton Nov 24, 2023
616c004
Update examples/README.md
pcwalton Nov 24, 2023
964caaf
Fix formatting
pcwalton Nov 24, 2023
00364d3
Clippy police
pcwalton Nov 24, 2023
dc04e83
Write more comments
pcwalton Nov 24, 2023
3120d53
Undo `generate_custom_mesh` changes
pcwalton Nov 24, 2023
ea200cc
Stop using texture arrays for lightmaps
pcwalton Nov 24, 2023
acd848f
Merge remote-tracking branch 'origin/main' into lightmaps
pcwalton Nov 25, 2023
97c6501
Break batches as appropriate
pcwalton Nov 25, 2023
4a800f4
Add more docs
pcwalton Nov 25, 2023
7539abf
Get rid of the lightmap fallback
pcwalton Nov 25, 2023
9e9e2dc
Pack the lightmap UVs into uvec2
pcwalton Nov 25, 2023
66bd398
Clippy police
pcwalton Nov 25, 2023
d713203
Don't use `bevy_internal`
pcwalton Nov 25, 2023
8358ea5
Fix comment
pcwalton Nov 25, 2023
9a53e16
Remove some old stuff from previous versions of the patch
pcwalton Nov 25, 2023
7cc37e6
Move the lightmap exposure value into the standard material
pcwalton Nov 27, 2023
db1bbfe
Merge remote-tracking branch 'origin/main' into lightmaps
pcwalton Dec 3, 2023
6137e62
Get rid of `COPY_SRC`
pcwalton Dec 3, 2023
7829f57
Rename `lightmaps` to singular
pcwalton Dec 3, 2023
cd39c04
Address review comments
pcwalton Dec 4, 2023
e3ec650
Rework the lightmap extraction to be simpler and easier to understand
pcwalton Dec 4, 2023
98dc107
Update lightmap example description in Cargo.toml
pcwalton Dec 4, 2023
6fedfb2
Clippy police
pcwalton Dec 4, 2023
6d0ed71
Doc police
pcwalton Dec 4, 2023
762a8ed
Merge remote-tracking branch 'origin/main' into lightmaps
pcwalton Dec 17, 2023
e2812e8
Move RenderApp stuff to `finish`
pcwalton Dec 17, 2023
bc57847
Fix typo in comment and use default()
cart Dec 28, 2023
36be135
Merge remote-tracking branch 'origin/main' into lightmaps
pcwalton Dec 28, 2023
655fd91
Fix silly typo
pcwalton Dec 28, 2023
4057915
Merge remote-tracking branch 'pcwalton/lightmaps' into lightmaps
pcwalton Dec 28, 2023
b7281e8
Bake the lightmaps in the example again, adding more bleed between the
pcwalton Dec 29, 2023
d561631
Fix prepass
pcwalton Dec 29, 2023
a47e589
Remove unneeded `#else` branch in `pbr_fragment.wgsl`
pcwalton Dec 29, 2023
ac22e82
Fix Clippy failure
pcwalton Dec 29, 2023
a75098e
Revert "Fix Clippy failure"
pcwalton Dec 29, 2023
faec5c3
Expose lightmap UVs to the prepass
pcwalton Dec 29, 2023
df3c1d6
Merge remote-tracking branch 'origin/main' into lightmaps
pcwalton Jan 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,17 @@ description = "Showcases wireframe rendering"
category = "3D Rendering"
wasm = false

[[example]]
name = "lightmaps"
path = "examples/3d/lightmaps.rs"
doc-scrape-examples = true

[package.metadata.example.lightmaps]
name = "Lightmaps"
description = "Displays the Cornell Box with lightmaps"
pcwalton marked this conversation as resolved.
Show resolved Hide resolved
category = "3D Rendering"
wasm = false

[[example]]
name = "no_prepass"
path = "tests/3d/no_prepass.rs"
Expand Down
Binary file added assets/lightmaps/CornellBox-Box.zstd.ktx2
Binary file not shown.
Binary file added assets/lightmaps/CornellBox-Large.zstd.ktx2
Binary file not shown.
Binary file added assets/lightmaps/CornellBox-Small.zstd.ktx2
Binary file not shown.
Binary file added assets/models/CornellBox/CornellBox.glb
Binary file not shown.
2 changes: 1 addition & 1 deletion assets/shaders/animate_shader.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {
let t_1 = sin(globals.time * speed) * 0.5 + 0.5;
let t_2 = cos(globals.time * speed);

let distance_to_center = distance(in.uv, vec2<f32>(0.5)) * 1.4;
let distance_to_center = distance(in.uv_a, vec2<f32>(0.5)) * 1.4;

// blending is done in a perceptual color space: https://bottosson.github.io/posts/oklab/
let red = vec3<f32>(0.627955, 0.224863, 0.125846);
Expand Down
4 changes: 2 additions & 2 deletions assets/shaders/array_texture.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn fragment(
// the material members
var pbr_input: PbrInput = pbr_input_new();

pbr_input.material.base_color = textureSample(my_array_texture, my_array_texture_sampler, mesh.uv, layer);
pbr_input.material.base_color = textureSample(my_array_texture, my_array_texture_sampler, mesh.uv_a, layer);
#ifdef VERTEX_COLORS
pbr_input.material.base_color = pbr_input.material.base_color * mesh.color;
#endif
Expand All @@ -43,7 +43,7 @@ fn fragment(
mesh.world_tangent,
#endif
#endif
mesh.uv,
mesh.uv_a,
view.mip_bias,
);
pbr_input.V = fns::calculate_view(mesh.world_position, pbr_input.is_orthographic);
Expand Down
2 changes: 1 addition & 1 deletion assets/shaders/custom_material.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ struct CustomMaterial {
fn fragment(
mesh: VertexOutput,
) -> @location(0) vec4<f32> {
return material.color * textureSample(base_color_texture, base_color_sampler, mesh.uv) * COLOR_MULTIPLIER;
return material.color * textureSample(base_color_texture, base_color_sampler, mesh.uv_a) * COLOR_MULTIPLIER;
}
4 changes: 2 additions & 2 deletions assets/shaders/texture_binding_array.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ fn fragment(
mesh: VertexOutput,
) -> @location(0) vec4<f32> {
// Select the texture to sample from using non-uniform uv coordinates
let coords = clamp(vec2<u32>(mesh.uv * 4.0), vec2<u32>(0u), vec2<u32>(3u));
let coords = clamp(vec2<u32>(mesh.uv_a * 4.0), vec2<u32>(0u), vec2<u32>(3u));
let index = coords.y * 4u + coords.x;
let inner_uv = fract(mesh.uv * 4.0);
let inner_uv = fract(mesh.uv_a * 4.0);
return textureSample(textures[index], nearest_sampler, inner_uv);
}
2 changes: 1 addition & 1 deletion assets/shaders/tonemapping_test_patterns.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn continuous_hue(uv: vec2<f32>) -> vec3<f32> {
fn fragment(
in: VertexOutput,
) -> @location(0) vec4<f32> {
var uv = in.uv;
var uv = in.uv_a;
var out = vec3(0.0);
if uv.y > 0.5 {
uv.y = 1.0 - uv.y;
Expand Down
9 changes: 9 additions & 0 deletions crates/bevy_pbr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ pub const PBR_PREPASS_FUNCTIONS_SHADER_HANDLE: Handle<Shader> =
pub const PBR_DEFERRED_TYPES_HANDLE: Handle<Shader> = Handle::weak_from_u128(3221241127431430599);
pub const PBR_DEFERRED_FUNCTIONS_HANDLE: Handle<Shader> = Handle::weak_from_u128(72019026415438599);
pub const RGB9E5_FUNCTIONS_HANDLE: Handle<Shader> = Handle::weak_from_u128(2659010996143919192);
pub const LIGHTMAPS_SHADER_HANDLE: Handle<Shader> =
Handle::weak_from_u128(285484768317531991932943596447919767152);

/// Sets up the entire PBR infrastructure of bevy.
pub struct PbrPlugin {
Expand Down Expand Up @@ -200,6 +202,12 @@ impl Plugin for PbrPlugin {
"render/parallax_mapping.wgsl",
Shader::from_wgsl
);
load_internal_asset!(
app,
LIGHTMAPS_SHADER_HANDLE,
"render/lightmaps.wgsl",
Shader::from_wgsl
);

app.register_asset_reflect::<StandardMaterial>()
.register_type::<AlphaMode>()
Expand Down Expand Up @@ -238,6 +246,7 @@ impl Plugin for PbrPlugin {
FogPlugin,
ExtractResourcePlugin::<DefaultOpaqueRendererMethod>::default(),
ExtractComponentPlugin::<ShadowFilteringMethod>::default(),
LightmapPlugin,
))
.configure_sets(
PostUpdate,
Expand Down
12 changes: 9 additions & 3 deletions crates/bevy_pbr/src/material.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{
render, AlphaMode, DrawMesh, DrawPrepass, EnvironmentMapLight, MeshPipeline, MeshPipelineKey,
PrepassPipelinePlugin, PrepassPlugin, RenderMeshInstances, ScreenSpaceAmbientOcclusionSettings,
SetMeshBindGroup, SetMeshViewBindGroup, Shadow, ShadowFilteringMethod,
render, AlphaMode, DrawMesh, DrawPrepass, EnvironmentMapLight, Lightmap, MeshPipeline,
MeshPipelineKey, PrepassPipelinePlugin, PrepassPlugin, RenderMeshInstances,
ScreenSpaceAmbientOcclusionSettings, SetMeshBindGroup, SetMeshViewBindGroup, Shadow,
ShadowFilteringMethod,
};
use bevy_app::{App, Plugin};
use bevy_asset::{Asset, AssetApp, AssetEvent, AssetId, AssetServer, Assets, Handle};
Expand Down Expand Up @@ -442,6 +443,7 @@ pub fn queue_material_meshes<M: Material>(
mut render_mesh_instances: ResMut<RenderMeshInstances>,
render_material_instances: Res<RenderMaterialInstances<M>>,
images: Res<RenderAssets<Image>>,
lightmaps: Query<&Lightmap>,
mut views: Query<(
&ExtractedView,
&VisibleEntities,
Expand Down Expand Up @@ -565,6 +567,10 @@ pub fn queue_material_meshes<M: Material>(
}
mesh_key |= alpha_mode_pipeline_key(material.properties.alpha_mode);

if lightmaps.contains(*visible_entity) {
mesh_key |= MeshPipelineKey::LIGHTMAPPED;
}

let pipeline_id = pipelines.specialize(
&pipeline_cache,
&material_pipeline,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/prepass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ where
}

if layout.contains(Mesh::ATTRIBUTE_UV_0) {
shader_defs.push("VERTEX_UVS".into());
shader_defs.push("VERTEX_UVS_A".into());
vertex_attributes.push(Mesh::ATTRIBUTE_UV_0.at_shader_location(1));
}

Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_pbr/src/prepass/prepass.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ fn vertex(vertex_no_morph: Vertex) -> VertexOutput {
out.position.z = min(out.position.z, 1.0);
#endif // DEPTH_CLAMP_ORTHO

#ifdef VERTEX_UVS
out.uv = vertex.uv;
#endif // VERTEX_UVS
#ifdef VERTEX_UVS_A
out.uv_a = vertex.uv_a;
#endif // VERTEX_UVS_A

#ifdef NORMAL_PREPASS_OR_DEFERRED_PREPASS
#ifdef SKINNED
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_pbr/src/prepass/prepass_io.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ struct Vertex {
@builtin(instance_index) instance_index: u32,
@location(0) position: vec3<f32>,

#ifdef VERTEX_UVS
#ifdef VERTEX_UVS_A
@location(1) uv: vec2<f32>,
#endif

Expand Down Expand Up @@ -36,7 +36,7 @@ struct VertexOutput {
// and `frag coord` when used as a fragment stage input
@builtin(position) position: vec4<f32>,

#ifdef VERTEX_UVS
#ifdef VERTEX_UVS_A
@location(0) uv: vec2<f32>,
#endif

Expand Down
21 changes: 13 additions & 8 deletions crates/bevy_pbr/src/render/forward_io.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ struct Vertex {
#ifdef VERTEX_NORMALS
@location(1) normal: vec3<f32>,
#endif
#ifdef VERTEX_UVS
@location(2) uv: vec2<f32>,
#ifdef VERTEX_UVS_A
@location(2) uv_a: vec2<f32>,
#endif
#ifdef VERTEX_UVS_B
@location(3) uv_b: vec2<f32>,
#endif
// (Alternate UVs are at location 3, but they're currently unused here.)
#ifdef VERTEX_TANGENTS
@location(4) tangent: vec4<f32>,
#endif
Expand All @@ -33,17 +35,20 @@ struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) world_position: vec4<f32>,
@location(1) world_normal: vec3<f32>,
#ifdef VERTEX_UVS
@location(2) uv: vec2<f32>,
#ifdef VERTEX_UVS_A
@location(2) uv_a: vec2<f32>,
#endif
#ifdef VERTEX_UVS_B
@location(3) uv_b: vec2<f32>,
#endif
#ifdef VERTEX_TANGENTS
@location(3) world_tangent: vec4<f32>,
@location(4) world_tangent: vec4<f32>,
#endif
#ifdef VERTEX_COLORS
@location(4) color: vec4<f32>,
@location(5) color: vec4<f32>,
#endif
#ifdef VERTEX_OUTPUT_INSTANCE_INDEX
@location(5) @interpolate(flat) instance_index: u32,
@location(6) @interpolate(flat) instance_index: u32,
#endif
}

Expand Down
Loading
Loading