Skip to content

Incorrect materials applied when over 65535 instances #23128

@DGriffin91

Description

@DGriffin91

Bevy 0.16 - 0.18, main fa379de

Bisected to: d82d323, #19041

Bevy 0.15 seems to work as expected.

When over 65535 unique material instances are created, instances are rendered using the incorrect material.

Scene with 65536 materials:
Image

Scene with 70k materials:
Image

use bevy::prelude::*;
fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, setup)
        .run();
}
fn setup(
    mut commands: Commands,
    mut standard_materials: ResMut<Assets<StandardMaterial>>,
    mut meshes: ResMut<Assets<Mesh>>,
) {
    let mesh = Mesh3d(meshes.add(Cuboid::default()));
    for i in 0..65536i64 {
        let x = i.rem_euclid(360) as f32;
        let y = (i / 360) as f32;
        commands
            .spawn((mesh.clone(), Transform::from_translation(vec3(x, -y, 0.0))))
            .insert(MeshMaterial3d(
                standard_materials.add(StandardMaterial {
                    emissive: Color::hsl(
                        (i as f32).rem_euclid(360.0),
                        (y * 0.05).sin().abs() * 0.8,
                        (x * 0.05).cos().abs() * 0.8,
                    )
                    .to_linear(),
                    ..Default::default()
                }),
            ));
    }
    commands.spawn((
        Camera3d::default(),
        bevy::core_pipeline::tonemapping::Tonemapping::None,
        Transform::from_xyz(200.0, -100.0, 500.0).looking_at(vec3(200.0, -100.0, 0.0), Vec3::Y),
    ));
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-RenderingDrawing game state to the screenC-BugAn unexpected or incorrect behaviorS-Needs-InvestigationThis issue requires detective work to figure out what's going wrong

    Type

    No type

    Projects

    Status

    Needs SME Triage

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions