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

Strange light behavior with different far plane values of orthographic camera #4473

Closed
kirusfg opened this issue Apr 14, 2022 · 5 comments
Closed
Labels
C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled

Comments

@kirusfg
Copy link
Contributor

kirusfg commented Apr 14, 2022

Bevy version

latest (8630b19)

Operating system & version

OS: Artix Linux x86_64
Linux Kernel: 5.17.2-zen2-1-zen
GPU: NVIDIA GeForce 920MX
Nvidia driver: 510.60.02-1

What you did

Loaded a glTF scene with an orthographic camera and a point light in it:

use bevy::prelude::*;


fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
    commands.spawn_scene(asset_server.load("ortho10.gltf#Scene0"));
}


fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_startup_system(setup)
        .run();
}

To test and experiment with this, edit the zfar field of the camera in the file provided:

ortho10.gtlf
{
    "asset" : {
        "generator" : "Khronos glTF Blender I/O v1.8.19",
        "version" : "2.0"
    },
    "extensionsUsed" : [
        "KHR_lights_punctual"
    ],
    "extensionsRequired" : [
        "KHR_lights_punctual"
    ],
    "extensions" : {
        "KHR_lights_punctual" : {
            "lights" : [
                {
                    "color" : [
                        1,
                        1,
                        1
                    ],
                    "intensity" : 1000,
                    "type" : "point",
                    "name" : "Light.001"
                }
            ]
        }
    },
    "scene" : 0,
    "scenes" : [
        {
            "name" : "Scene",
            "nodes" : [
                0,
                3,
                6,
                7
            ]
        }
    ],
    "nodes" : [
        {
            "mesh" : 0,
            "name" : "Cube",
            "translation" : [
                0,
                1,
                0
            ]
        },
        {
            "extensions" : {
                "KHR_lights_punctual" : {
                    "light" : 0
                }
            },
            "name" : "Light_Orientation_Orientation",
            "rotation" : [
                -0.7071067690849304,
                0,
                0,
                0.7071067690849304
            ]
        },
        {
            "children" : [
                1
            ],
            "name" : "Light_Orientation"
        },
        {
            "children" : [
                2
            ],
            "name" : "Light",
            "rotation" : [
                0.16907575726509094,
                0.7558803558349609,
                -0.27217137813568115,
                0.570947527885437
            ],
            "translation" : [
                4.076245307922363,
                5.903861999511719,
                -1.0054539442062378
            ]
        },
        {
            "camera" : 0,
            "name" : "Camera_Orientation_Orientation",
            "rotation" : [
                -0.7071067690849304,
                0,
                0,
                0.7071067690849304
            ]
        },
        {
            "children" : [
                4
            ],
            "name" : "Camera_Orientation"
        },
        {
            "children" : [
                5
            ],
            "name" : "Camera",
            "rotation" : [
                0.40501639246940613,
                0.4532789885997772,
                -0.24283890426158905,
                0.7559954524040222
            ],
            "translation" : [
                7.977607727050781,
                6.902405738830566,
                4.161380290985107
            ]
        },
        {
            "mesh" : 1,
            "name" : "Plane",
            "scale" : [
                10.624591827392578,
                10.624591827392578,
                10.624591827392578
            ]
        }
    ],
    "cameras" : [
        {
            "name" : "Camera.001",
            "orthographic" : {
                "xmag" : 10,
                "ymag" : 10,
                "zfar" : 100,
                "znear" : 0.10000000149011612
            },
            "type" : "orthographic"
        }
    ],
    "materials" : [
        {
            "doubleSided" : true,
            "name" : "Material.001",
            "pbrMetallicRoughness" : {
                "baseColorFactor" : [
                    0.057456985116004944,
                    0.0665164664387703,
                    0.8000000715255737,
                    1
                ],
                "metallicFactor" : 0,
                "roughnessFactor" : 0.4000000059604645
            }
        },
        {
            "doubleSided" : true,
            "name" : "Material.002",
            "pbrMetallicRoughness" : {
                "baseColorFactor" : [
                    0.8000000715255737,
                    0.2788206934928894,
                    0.2428315132856369,
                    1
                ],
                "metallicFactor" : 0,
                "roughnessFactor" : 0.5
            }
        }
    ],
    "meshes" : [
        {
            "name" : "Cube.001",
            "primitives" : [
                {
                    "attributes" : {
                        "POSITION" : 0,
                        "NORMAL" : 1,
                        "TEXCOORD_0" : 2
                    },
                    "indices" : 3,
                    "material" : 0
                }
            ]
        },
        {
            "name" : "Plane",
            "primitives" : [
                {
                    "attributes" : {
                        "POSITION" : 4,
                        "NORMAL" : 5,
                        "TEXCOORD_0" : 6
                    },
                    "indices" : 7,
                    "material" : 1
                }
            ]
        }
    ],
    "accessors" : [
        {
            "bufferView" : 0,
            "componentType" : 5126,
            "count" : 24,
            "max" : [
                1,
                1,
                1
            ],
            "min" : [
                -1,
                -1,
                -1
            ],
            "type" : "VEC3"
        },
        {
            "bufferView" : 1,
            "componentType" : 5126,
            "count" : 24,
            "type" : "VEC3"
        },
        {
            "bufferView" : 2,
            "componentType" : 5126,
            "count" : 24,
            "type" : "VEC2"
        },
        {
            "bufferView" : 3,
            "componentType" : 5123,
            "count" : 36,
            "type" : "SCALAR"
        },
        {
            "bufferView" : 4,
            "componentType" : 5126,
            "count" : 4,
            "max" : [
                1,
                0,
                1
            ],
            "min" : [
                -1,
                0,
                -1
            ],
            "type" : "VEC3"
        },
        {
            "bufferView" : 5,
            "componentType" : 5126,
            "count" : 4,
            "type" : "VEC3"
        },
        {
            "bufferView" : 6,
            "componentType" : 5126,
            "count" : 4,
            "type" : "VEC2"
        },
        {
            "bufferView" : 7,
            "componentType" : 5123,
            "count" : 6,
            "type" : "SCALAR"
        }
    ],
    "bufferViews" : [
        {
            "buffer" : 0,
            "byteLength" : 288,
            "byteOffset" : 0
        },
        {
            "buffer" : 0,
            "byteLength" : 288,
            "byteOffset" : 288
        },
        {
            "buffer" : 0,
            "byteLength" : 192,
            "byteOffset" : 576
        },
        {
            "buffer" : 0,
            "byteLength" : 72,
            "byteOffset" : 768
        },
        {
            "buffer" : 0,
            "byteLength" : 48,
            "byteOffset" : 840
        },
        {
            "buffer" : 0,
            "byteLength" : 48,
            "byteOffset" : 888
        },
        {
            "buffer" : 0,
            "byteLength" : 32,
            "byteOffset" : 936
        },
        {
            "buffer" : 0,
            "byteLength" : 12,
            "byteOffset" : 968
        }
    ],
    "buffers" : [
        {
            "byteLength" : 980,
            "uri" : "data:application/octet-stream;base64,AACAPwAAgD8AAIC/AACAPwAAgD8AAIC/AACAPwAAgD8A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AAAAAAAIA/AACAvwAAAAAAAIC/AACAPwAAAAAAAIC/AAAAAAAAgD8AAACAAAAAAAAAgD8AAACAAAAAAAAAgD8AAACAAAAAAAAAgD8AAACAAAAAAAAAgD8AAIA/AACAPwAAAAAAAAAAAACAPwAAAAAAAAEAAwAAAAMAAgA="
        }
    ]
}

What you expected to happen

Proper lighting

What actually happened

Lighting behaving strangely on different far settings of the orthographic camera set in Blender:

30

30

50

50

100

100

1000

1000

Additional Information

Note that at a 1000, the lighted parts of the scene are not visible in a 16:9 window and I had to stretch it. In #4005, the light was being "cutoff" because the far value was not loaded from glTF, and by default, it was being set to 0. Same thing is happening here.

@kirusfg kirusfg added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Apr 14, 2022
@robtfm
Copy link
Contributor

robtfm commented Apr 14, 2022

I think this will be fixed by #4345 - specifically the small pbr.wgsl change from that pr. could you please check?
If that works and if the whole pr is too big for 0.7 perhaps we can separate out the pbr.wgsl change into a separate pr and merge it more quickly.

@kirusfg
Copy link
Contributor Author

kirusfg commented Apr 14, 2022

Yes, the issue is fixed there, great work!

Do not forget to mark this issue as being resolved by #4345.

@superdump
Copy link
Contributor

To flesh out the test results a bit with what @kirusfg found and reported on Discord:

@superdump
Copy link
Contributor

One more note, the following has been tested as an effective workaround:

ClusterConfig::FixedZ {
    total: 4096,
    z_slices: 24,
    z_config: ClusterZConfig {
        first_slice_depth: 5.0,
        far_z_mode: ClusterFarZMode::CameraFarPlane,
    },
    dynamic_resizing: false,
}

Basically ensuring that the far z plane used for clustering matches the far z plane of the camera.

bors bot pushed a commit that referenced this issue Apr 15, 2022
# Objective

- Fixes #4234
- Fixes #4473 
- Built on top of #3989
- Improve performance of `assign_lights_to_clusters`

## Solution

- Remove the OBB-based cluster light assignment algorithm and calculation of view space AABBs
- Implement the 'iterative sphere refinement' algorithm used in Just Cause 3 by Emil Persson as documented in the Siggraph 2015 Practical Clustered Shading talk by Persson, on pages 42-44 http://newq.net/dl/pub/s2015_practical.pdf
- Adapt to also support orthographic projections
- Add `many_lights -- orthographic` for testing many lights using an orthographic projection

## Results

- `assign_lights_to_clusters` in `many_lights` before this PR on an M1 Max over 1500 frames had a median execution time of 1.71ms. With this PR it is 1.51ms, a reduction of 0.2ms or 11.7% for this system.

---

## Changelog

- Changed: Improved cluster light assignment performance

Co-authored-by: robtfm <50659922+robtfm@users.noreply.github.com>
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
@kirusfg
Copy link
Contributor Author

kirusfg commented Apr 15, 2022

Fixed by #4345.

@kirusfg kirusfg closed this as completed Apr 15, 2022
aevyrie pushed a commit to aevyrie/bevy that referenced this issue Jun 7, 2022
# Objective

- Fixes bevyengine#4234
- Fixes bevyengine#4473 
- Built on top of bevyengine#3989
- Improve performance of `assign_lights_to_clusters`

## Solution

- Remove the OBB-based cluster light assignment algorithm and calculation of view space AABBs
- Implement the 'iterative sphere refinement' algorithm used in Just Cause 3 by Emil Persson as documented in the Siggraph 2015 Practical Clustered Shading talk by Persson, on pages 42-44 http://newq.net/dl/pub/s2015_practical.pdf
- Adapt to also support orthographic projections
- Add `many_lights -- orthographic` for testing many lights using an orthographic projection

## Results

- `assign_lights_to_clusters` in `many_lights` before this PR on an M1 Max over 1500 frames had a median execution time of 1.71ms. With this PR it is 1.51ms, a reduction of 0.2ms or 11.7% for this system.

---

## Changelog

- Changed: Improved cluster light assignment performance

Co-authored-by: robtfm <50659922+robtfm@users.noreply.github.com>
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
ItsDoot pushed a commit to ItsDoot/bevy that referenced this issue Feb 1, 2023
# Objective

- Fixes bevyengine#4234
- Fixes bevyengine#4473 
- Built on top of bevyengine#3989
- Improve performance of `assign_lights_to_clusters`

## Solution

- Remove the OBB-based cluster light assignment algorithm and calculation of view space AABBs
- Implement the 'iterative sphere refinement' algorithm used in Just Cause 3 by Emil Persson as documented in the Siggraph 2015 Practical Clustered Shading talk by Persson, on pages 42-44 http://newq.net/dl/pub/s2015_practical.pdf
- Adapt to also support orthographic projections
- Add `many_lights -- orthographic` for testing many lights using an orthographic projection

## Results

- `assign_lights_to_clusters` in `many_lights` before this PR on an M1 Max over 1500 frames had a median execution time of 1.71ms. With this PR it is 1.51ms, a reduction of 0.2ms or 11.7% for this system.

---

## Changelog

- Changed: Improved cluster light assignment performance

Co-authored-by: robtfm <50659922+robtfm@users.noreply.github.com>
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants