bevyengine / bevy

A refreshingly simple data-driven game engine built in Rust
https://bevyengine.org
Apache License 2.0
35.02k stars 3.43k forks source link

Crash when loading custom gltf mesh #5683

Closed lattice0 closed 2 years ago

lattice0 commented 2 years ago

Bevy version

0.16.0

[Optional] Relevant system information

Ubuntu 20.04, GPU integrated i7 intel from 2017

What you did

I'm trying to use a mesh as a collision shape using rapier and I get a crash on a very generic line: https://github.com/lattice0/quad/blob/d73a44cbb3d638075a07f5c084a9ccf7e72bd2f0/src/main.rs#L64

please try to run the project from this commit, it should run without any additional efforts

What went wrong

crash

my X shaped drone to be collisionable

a crash

Additional information

backtrace:

thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', src/main.rs:58:31
stack backtrace:
   0: rust_begin_unwind
             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:584:5
   1: core::panicking::panic_fmt
             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/panicking.rs:142:14
   2: core::panicking::panic
             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/panicking.rs:48:5
   3: core::ops::function::impls::<impl core::ops::function::FnMut<A> for &mut F>::call_mut
   4: <bevy_ecs::system::function_system::FunctionSystem<In,Out,Param,Marker,F> as bevy_ecs::system::system::System>::run_unsafe
   5: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
   6: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
   7: async_task::raw::RawTask<F,T,S>::run
   8: async_executor::Executor::try_tick
   9: std::thread::local::LocalKey<T>::with
  10: <bevy_ecs::schedule::executor_parallel::ParallelExecutor as bevy_ecs::schedule::executor::ParallelSystemExecutor>::run_systems
  11: <bevy_ecs::schedule::stage::SystemStage as bevy_ecs::schedule::stage::Stage>::run
  12: bevy_ecs::schedule::Schedule::run_once
  13: bevy_ecs::schedule::Schedule::run_once
  14: bevy_app::app::App::update
  15: bevy_winit::winit_runner_with::{{closure}}
  16: winit::platform_impl::platform::x11::EventLoop<T>::run_return
  17: winit::platform_impl::platform::x11::EventLoop<T>::run
  18: winit::platform_impl::platform::EventLoop<T>::run
  19: winit::event_loop::EventLoop<T>::run
  20: bevy_winit::run
  21: bevy_winit::winit_runner_with
  22: core::ops::function::Fn::call
  23: bevy_app::app::App::run
  24: quad::main

Full backtrace:


2022-08-14T02:04:02.052952Z  INFO bevy_rapier3d::render::lines: Loaded 3d debug lines plugin.
thread 'Compute Task Pool (0)' panicked at 'called `Option::unwrap()` on a `None` value', src/main.rs:59:31
stack backtrace:
   0:     0x564573204d6d - std::backtrace_rs::backtrace::libunwind::trace::h8217d0a8f3fd2f41
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x564573204d6d - std::backtrace_rs::backtrace::trace_unsynchronized::h308103876b3af410
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x564573204d6d - std::sys_common::backtrace::_print_fmt::hc208018c6153605e
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/sys_common/backtrace.rs:66:5
   3:     0x564573204d6d - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hf89a7ed694dfb585
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/sys_common/backtrace.rs:45:22
   4:     0x5645732289ac - core::fmt::write::h21038c1382fe4264
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/fmt/mod.rs:1197:17
   5:     0x564573201f61 - std::io::Write::write_fmt::h7dbb1c9a3c254aef
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/io/mod.rs:1672:15
   6:     0x564573206515 - std::sys_common::backtrace::_print::h4e8889719c9ddeb8
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/sys_common/backtrace.rs:48:5
   7:     0x564573206515 - std::sys_common::backtrace::print::h1506fe2cb3022667
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/sys_common/backtrace.rs:35:9
   8:     0x564573206515 - std::panicking::default_hook::{{closure}}::hd9d7ce2a8a782440
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:295:22
   9:     0x564573206236 - std::panicking::default_hook::h5b16ec25444b1b5d
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:314:9
  10:     0x564573206aa6 - std::panicking::rust_panic_with_hook::hb0138cb6e6fea3e4
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:698:17
  11:     0x564573206959 - std::panicking::begin_panic_handler::{{closure}}::h4cb67095557cd1aa
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:586:13
  12:     0x564573205224 - std::sys_common::backtrace::__rust_end_short_backtrace::h2bfcac279dcdc911
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/sys_common/backtrace.rs:138:18
  13:     0x5645732066c9 - rust_begin_unwind
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:584:5
  14:     0x5645721e63e3 - core::panicking::panic_fmt::h1de71520faaa17d3
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/panicking.rs:142:14
  15:     0x5645721e62ad - core::panicking::panic::h467ee1bf554babeb
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/panicking.rs:48:5
  16:     0x564572234f02 - core::ops::function::impls::<impl core::ops::function::FnMut<A> for &mut F>::call_mut::h336b583cb3ce326f
  17:     0x5645722339ee - <bevy_ecs::system::function_system::FunctionSystem<In,Out,Param,Marker,F> as bevy_ecs::system::system::System>::run_unsafe::h4b509a80a3e29bc9
  18:     0x56457316929c - <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::hff888e7aa8904ce8
  19:     0x564573169127 - <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::hf65314fd6003d426
  20:     0x564573173593 - async_task::raw::RawTask<F,T,S>::run::h68a1de6c66f0e2f9
  21:     0x5645731793e0 - <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::h9e4dac87001a7790
  22:     0x564573179133 - <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::h0d91ffb14fbd5c1d
  23:     0x56457317beeb - std::thread::local::LocalKey<T>::with::h00e1d04e1164d31f
  24:     0x56457317efb1 - std::sys_common::backtrace::__rust_begin_short_backtrace::hfc5934f0a5d4489e
  25:     0x56457317ce9f - core::ops::function::FnOnce::call_once{{vtable.shim}}::h6bd71e2d5f701553
  26:     0x564573209e33 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h1680342795a2dc08
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/alloc/src/boxed.rs:1951:9
  27:     0x564573209e33 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h45204a69827b0e83
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/alloc/src/boxed.rs:1951:9
  28:     0x564573209e33 - std::sys::unix::thread::Thread::new::thread_start::h5d4e11bbda4161c8
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/sys/unix/thread.rs:108:17
  29:     0x7f9d4660b609 - start_thread
                               at /build/glibc-SzIz7B/glibc-2.31/nptl/pthread_create.c:477:8
  30:     0x7f9d463df133 - clone
                               at /build/glibc-SzIz7B/glibc-2.31/misc/../sysdeps/unix/sysv/linux/x86_64/clone.S:95
  31:                0x0 - <unknown>
thread 'main' panicked at 'task has failed', /home/lz/.cargo/registry/src/github.com-1ecc6299db9ec823/async-task-4.3.0/src/task.rs:426:45
stack backtrace:
   0:     0x564573204d6d - std::backtrace_rs::backtrace::libunwind::trace::h8217d0a8f3fd2f41
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x564573204d6d - std::backtrace_rs::backtrace::trace_unsynchronized::h308103876b3af410
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x564573204d6d - std::sys_common::backtrace::_print_fmt::hc208018c6153605e
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/sys_common/backtrace.rs:66:5
   3:     0x564573204d6d - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hf89a7ed694dfb585
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/sys_common/backtrace.rs:45:22
   4:     0x5645732289ac - core::fmt::write::h21038c1382fe4264
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/fmt/mod.rs:1197:17
   5:     0x564573201f61 - std::io::Write::write_fmt::h7dbb1c9a3c254aef
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/io/mod.rs:1672:15
   6:     0x564573206515 - std::sys_common::backtrace::_print::h4e8889719c9ddeb8
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/sys_common/backtrace.rs:48:5
   7:     0x564573206515 - std::sys_common::backtrace::print::h1506fe2cb3022667
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/sys_common/backtrace.rs:35:9
   8:     0x564573206515 - std::panicking::default_hook::{{closure}}::hd9d7ce2a8a782440
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:295:22
   9:     0x564573206236 - std::panicking::default_hook::h5b16ec25444b1b5d
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:314:9
  10:     0x564573206aa6 - std::panicking::rust_panic_with_hook::hb0138cb6e6fea3e4
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:698:17
  11:     0x564573206997 - std::panicking::begin_panic_handler::{{closure}}::h4cb67095557cd1aa
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:588:13
  12:     0x564573205224 - std::sys_common::backtrace::__rust_end_short_backtrace::h2bfcac279dcdc911
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/sys_common/backtrace.rs:138:18
  13:     0x5645732066c9 - rust_begin_unwind
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:584:5
  14:     0x5645721e63e3 - core::panicking::panic_fmt::h1de71520faaa17d3
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/panicking.rs:142:14
  15:     0x564573225ea1 - core::panicking::panic_display::h20fe9d7f27d4d64a
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/panicking.rs:72:5
  16:     0x564573225e4b - core::panicking::panic_str::h00c94914e747c9f5
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/panicking.rs:56:5
  17:     0x5645721e6256 - core::option::expect_failed::h0322fe3c07a466dd
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/option.rs:1874:5
  18:     0x564573164e44 - <async_task::task::Task<T> as core::future::future::Future>::poll::h02702633f3623b37
  19:     0x564573167ef7 - <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::h13c0327decb43d76
  20:     0x564573167ffe - <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::h594bd1e765b859f3
  21:     0x564573172939 - async_task::raw::RawTask<F,T,S>::run::h1600482c6d49dc2f
  22:     0x564573180a39 - async_executor::Executor::try_tick::he4e4f5dc5247f144
  23:     0x56457313c489 - std::thread::local::LocalKey<T>::with::ha59f2e18958255db
  24:     0x5645731655f5 - <bevy_ecs::schedule::executor_parallel::ParallelExecutor as bevy_ecs::schedule::executor::ParallelSystemExecutor>::run_systems::h938b9749a34d0b0f
  25:     0x564573160845 - <bevy_ecs::schedule::stage::SystemStage as bevy_ecs::schedule::stage::Stage>::run::h992f997331c3b257
  26:     0x56457316c010 - bevy_ecs::schedule::Schedule::run_once::hce086b39c5d8a35c
  27:     0x56457316c010 - bevy_ecs::schedule::Schedule::run_once::hce086b39c5d8a35c
  28:     0x56457313462b - bevy_app::app::App::update::he7370cd807fe8e1c
  29:     0x5645724972b1 - bevy_winit::winit_runner_with::{{closure}}::h02088fc50adf73ee
  30:     0x5645724a6991 - winit::platform_impl::platform::x11::EventLoop<T>::run_return::h794566684c505b7f
  31:     0x5645724a7119 - winit::platform_impl::platform::x11::EventLoop<T>::run::hf09b138d3c542ecc
  32:     0x564572495a6a - winit::platform_impl::platform::EventLoop<T>::run::ha9ca52d113cedf33
  33:     0x56457247cdfa - winit::event_loop::EventLoop<T>::run::hc58cc8fc3818b9ae
  34:     0x5645724804aa - bevy_winit::run::h86f4ee36b66c0023
  35:     0x56457248108c - bevy_winit::winit_runner_with::h36b95f2a075f2de9
  36:     0x5645724a3c74 - core::ops::function::Fn::call::h44d0b256fea4420d
  37:     0x5645731347ad - bevy_app::app::App::run::h79d64bad6bd68033
  38:     0x564572237e58 - quad::main::h9ad0bb9ea7e8e6b4
  39:     0x564572233f93 - std::sys_common::backtrace::__rust_begin_short_backtrace::h494dd9380ff4f3b8
  40:     0x56457223dfc9 - std::rt::lang_start::{{closure}}::hca76cf08b7972c85
  41:     0x5645731fda2e - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once::h4937aaa125c8d4b2
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/ops/function.rs:280:13
  42:     0x5645731fda2e - std::panicking::try::do_call::h6f5c70e8b0a34f92
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:492:40
  43:     0x5645731fda2e - std::panicking::try::h68766ba264ecf2e2
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:456:19
  44:     0x5645731fda2e - std::panic::catch_unwind::hc36033d2f9cc04af
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panic.rs:137:14
  45:     0x5645731fda2e - std::rt::lang_start_internal::{{closure}}::h78c037f4a1a28ded
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/rt.rs:128:48
  46:     0x5645731fda2e - std::panicking::try::do_call::he6e1fffda4c750ee
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:492:40
  47:     0x5645731fda2e - std::panicking::try::h48a77ddbb2f4c87a
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:456:19
  48:     0x5645731fda2e - std::panic::catch_unwind::hfa809b06a550a9e7
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panic.rs:137:14
  49:     0x5645731fda2e - std::rt::lang_start_internal::h4db69ed48eaca005
                               at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/rt.rs:128:20
  50:     0x564572237ec2 - main
  51:     0x7f9d462e4083 - __libc_start_main
                               at /build/glibc-SzIz7B/glibc-2.31/csu/../csu/libc-start.c:308:16
  52:     0x5645721e668e - _start
  53:                0x0 - <unknown>
rparrett commented 2 years ago

On my end, I am seeing the panic happen on the line containing &meshes.get(&x_shape).unwrap().

At this point in the code, the mesh has been queued to load but not actually been loaded yet, so the panic on unwrap is expected.

lattice0 commented 2 years ago

I think I added/rremoved some lines after getting the panic then clicked and got a wrong panic line. How should I wait for the mesh to be loaded then?

rparrett commented 2 years ago

Also, to get a handle to a specific mesh in a gltf file, you need to do something like

asset_server.load("quad.gltf#Mesh0/Primitive0")

(see asset_loading.rs which coincidentally loads some meshes)

You can check if an asset's loaded with get_load_state. Maybe this cheatbook page will be helpful.

lattice0 commented 2 years ago

Yes I was looking at asset_loading.rs and they don't ever wait for asset loading, also it seems that the asset never loads, as

   let x_shape: Handle<Mesh> = asset_server.load("quad.gltf#Mesh0/Primitive0");
    while asset_server.get_load_state(&x_shape)!=LoadState::Loaded {

    }
    let m = &meshes.get(&x_shape);

runs forever.

Maybe #Mesh0/Primitive0 is wrong, it should have another name, but I don't know which one to put, I just made this simple file on Blender and exported

rparrett commented 2 years ago

You need to check the load state in separate non-startup system.

lattice0 commented 2 years ago

But https://github.com/bevyengine/bevy/blob/main/examples/asset/asset_loading.rs does all in one system

rparrett commented 2 years ago

asset_loading.rs never waits for anything to load. It only ever deals with handles to assets that have been queued to load. Bevy's renderer is cool with letting those pop in whenever they end up actually loading.

Your situation is different, because Collider::from_bevy_mesh needs the actual loaded Mesh, not a handle.

It might take many frames to load a gltf, so you can't just block on it.

Checkout the cheatbook link above. It's important to store the handle in a Resource or elsewhere so that it's not garbage collected. The example in the cheatbook uses a Vec of handles and get_group_load_state, but you can do something similar with a single handle and get_load_state.

rparrett commented 2 years ago

I'm going to close this because I don't think there's a bug here. But please feel free to continue replying, ask another question in the discussions tab, or come and chat in Discord.