bevyengine / bevy

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

Text rendered in 2d inside of a child bundle is extremely large #15272

Closed MatrixSenpai closed 1 month ago

MatrixSenpai commented 1 month ago

Bevy version: 0.14.2

The release number or commit hash of the version you're using.

Relevant system information

SystemInfo { os: "Windows 11 Pro", kernel: "22631", cpu: "12th Gen Intel(R) Core(TM) i7-12700K", core_count: "12", memory: "63.7 GiB" }
AdapterInfo { name: "AMD Radeon RX 7900 XTX", vendor: 4098, device: 29772, device_type: DiscreteGpu, driver: "AMD proprietary driver", driver_info: "24.8.1 (LLPC)", backend: Vulkan }

Attempting to use a 2d Text Bundle inside a nested 2d sprite without a 0.01 transform on x+y axes causes text to become so large i mistook it for a gradient.

The text bundle is nested inside multiple objects. Resizing for each entity is calculated based on window size so resolution should not affect this issue...i don't think?

With a transform included on the text bundle of value Vec3::new(0.01, 0.01, 1.0) the text is clear, as seen here image

When this transform is removed, however, i get this result: image

Relevant code snippet:

fn spawn_piece(
    commands: &mut Commands,
    style: TextStyle,
    board_entity: Entity,
    board_locations: &Query<(&LocationAnchor, &BoardSlot), With<GameBoardLocation>>,
    existing_pieces: &mut ResMut<GameState>,
) {
    // rough draft of where the piece should go
    let mut slot: usize = thread_rng().gen_range(0..16);
    while existing_pieces.0[slot].ne(&0) {
        slot = thread_rng().gen_range(0..16);
    }

    // pull a matching location out of stored board coordinates
    let location = match board_locations.iter().find(|(_, s)| s.0.eq(&slot)) {
        Some((a, _)) => a,
        None => return,
    };
    let starting_number = if thread_rng().gen_bool(0.7) {
        2
    } else { 4 };

    // actually spawning the piece. BOARD_SQUARE_SIZE = 0.23
    let piece = commands.spawn(SpriteBundle {
        sprite: Sprite {
            color: PIECE_COLOR,
            ..default()
        },
        transform: Transform {
            scale: Vec3::new(BOARD_SQUARE_SIZE, BOARD_SQUARE_SIZE, 0.0),
            translation: Vec3::new(location.x, location.y, 1.0),
            ..default()
        },
        ..default()
    })
        .with_children(|parent| {
            parent.spawn(Text2dBundle {
                text: Text {
                    sections: vec![TextSection::new(format!("{starting_number}"), style.clone())],
                    justify: JustifyText::Center,
                    linebreak_behavior: BreakLineOn::NoWrap,
                },
                text_2d_bounds: Text2dBounds {
                    size: Vec2::new(BOARD_SQUARE_SIZE, BOARD_SQUARE_SIZE),
                },
                // <-- This transform fixes the issue
                transform: Transform {
                    scale: Vec3::new(0.01, 0.01, 1.0),
                    ..default()
                },
                ..default()
            });
        })
        .insert(BoardSlot(slot))
        .insert(Piece { current_slot: slot })
        .id();

    // show as child of the board and store it in the internal game state
    existing_pieces.0[slot] = starting_number;
    commands.entity(board_entity).add_child(piece);
}
alice-i-cecile commented 1 month ago

Do your intermediate entities have a non-1 scale value? If so, this is expected transform propagation.

alice-i-cecile commented 1 month ago

Yeah looks like they do. You need to correct for that, or better yet set the image size of your sprite rather than scaling it.

MatrixSenpai commented 1 month ago

ah, you're absolutely right, thanks for the quick response!