asny / three-d

2D/3D renderer - makes it simple to draw stuff across platforms (including web)
MIT License
1.33k stars 110 forks source link

Strange behavior of update_shadow_maps() #311

Closed JPGygax68 closed 1 year ago

JPGygax68 commented 1 year ago

I adapted the "statues" example to my needs. It worked, but I found that the output was clipped to the lower left corner of my window, everything else was black: grafik The above ist the result of:

fn update_shadow_maps(data: &mut EngineData) {
    data.directional_light.generate_shadow_map(
        1024,
        data.scenes.iter().flat_map(|m| m.into_iter()),
    );
}

I had to increase the texture size passed to DirectionalLight.update_shadow_maps() until it was at least the width of my viewport (which is wider than it is high) to make the rest of the scene appear: grafik With:

fn update_shadow_maps(data: &mut EngineData) {
    let vp = data.camera.viewport();
    let size = vp.width.max(vp.height);
    data.directional_light.generate_shadow_map(
        size,
        data.scenes.iter().flat_map(|m| m.into_iter()),
    );
}

What could I be doing wrong?

asny commented 1 year ago

It looks like the state is not set correctly before drawing to the screen. So I think there is nothing wrong with the shadow map code, it's where you draw the objects to the screen that's the issue. Can you link or paste that part of the code?

So my initial thought is that the shadow map rendering sets the Scissor box correctly to 1024x1024, however, when rendering to the screen the scissor box is not set, hence it still uses the 1024x1024 Scissor box and don't draw outside of the 1024x1024 area. To test, can you add context.set_scissor(camera.viewport().into()); in between the shadow map call and rendering to the screen?

JPGygax68 commented 1 year ago

Thank you! I forgot to take my laptop with so I can't check right now, but I will this afternoon.

On Mon, Jan 23, 2023, 09:22 Asger Nyman Christiansen < @.***> wrote:

It looks like the state is not set correctly before drawing to the screen. So I think there is nothing wrong with the shadow map code, it's where you draw the objects to the screen that's the issue. Can you link or paste that part of the code?

So my initial thought is that the shadow map rendering sets the Scissor box correctly to 1024x1024, however, when rendering to the screen the scissor box is not set, hence it still uses the 1024x1024 Scissor box and don't draw outside of the 1024x1024 area. To test, can you add context.set_scissor(camera.viewport().into()); in between the shadow map call and rendering to the screen?

— Reply to this email directly, view it on GitHub https://github.com/asny/three-d/issues/311#issuecomment-1399959393, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAGCFCFQSTKXFWAKEOTT5S3WTY5VHANCNFSM6AAAAAAUCOTDZQ . You are receiving this because you authored the thread.Message ID: @.***>

JPGygax68 commented 1 year ago

Aaand after another delay I can now confirm that your suggestion worked 100%. (As a side effect, the shadows are now somewhat jagged, but that is now just a matter of balancing performance against visual quality.)