Skip to content

fix: Lazily initialize Mesh in draw commands for webgl2 compat #1020

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

Merged
merged 1 commit into from
Mar 16, 2025
Merged
Changes from all commits
Commits
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
45 changes: 27 additions & 18 deletions bevy_nannou_draw/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ fn update_draw_mesh(
let mut stroke_tessellator = StrokeTessellator::new();

let mut last_shader_model = None;
let mut mesh = meshes.add(Mesh::init());
let mut current_mesh = None;
let mut curr_ctx: DrawContext = Default::default();

let draw_cmds = draw.drain_commands();
Expand All @@ -608,8 +608,30 @@ fn update_draw_mesh(
output_attachment_scale_factor: window.scale_factor(),
};

// If no mesh is currently set, initialise a new one.
let mesh = current_mesh.get_or_insert_with(|| {
let mesh = meshes.add(Mesh::init());
let model_id =
last_shader_model.expect("No shader model set for draw command");
commands.spawn((
UntypedShaderModelId(model_id),
Mesh3d(mesh.clone()),
Transform::default(),
GlobalTransform::default(),
Visibility::default(),
InheritedVisibility::default(),
ViewVisibility::default(),
ShaderModelMesh,
NannouTransient,
NoFrustumCulling,
DrawIndex(idx),
window_layers.clone(),
));
mesh
});

// Render the primitive.
let mut mesh = meshes.get_mut(&mesh).unwrap();
let mut mesh = meshes.get_mut(mesh).unwrap();
prim.render_primitive(ctxt, &mut mesh);
}
DrawCommand::Instanced(prim, range) => {
Expand Down Expand Up @@ -689,23 +711,10 @@ fn update_draw_mesh(
curr_ctx = ctx;
}
DrawCommand::ShaderModel(model_id) => {
// We switched models, so start rendering into a new mesh
// Drop the mesh, we'll initialise a new one if something is
// drawn with this shader model.
last_shader_model = Some(model_id.clone());
mesh = meshes.add(Mesh::init());
commands.spawn((
UntypedShaderModelId(model_id),
Mesh3d(mesh.clone()),
Transform::default(),
GlobalTransform::default(),
Visibility::default(),
InheritedVisibility::default(),
ViewVisibility::default(),
ShaderModelMesh,
NannouTransient,
NoFrustumCulling,
DrawIndex(idx),
window_layers.clone(),
));
current_mesh.take();
}
DrawCommand::BackgroundColor(color) => {
window_camera.clear_color = ClearColorConfig::Custom(color);
Expand Down