Skip to content

Skybox in WASM #16424

Open
Open
@tomlikesnakes

Description

@tomlikesnakes

Hello, I've been trying to have a skybox working in wasm but whatever the tool I used or flags or so it is not visible yet I dont have any wrong log, here is my code :

use bevy::prelude::*;
use bevy::core_pipeline::Skybox;

pub struct SkyboxPlugin;

impl Plugin for SkyboxPlugin {
    fn build(&self, app: &mut App) {
        app
            .insert_resource(SkyboxState::default())
            .add_systems(Startup, setup_skybox)
            .add_systems(
                Update,
                (
                    skybox_control_system,
                    log_skybox_status,
                ),
            );
    }
}

#[derive(Resource)]
struct SkyboxTexture {
    image: Handle<Image>,
}

#[derive(Resource)]
struct SkyboxState {
    visible: bool,
}

impl Default for SkyboxState {
    fn default() -> Self {
        Self {
            visible: true,
        }
    }
}

fn setup_skybox(
    mut commands: Commands,
    asset_server: Res<AssetServer>,
    mut query: Query<Entity, With<Camera3d>>,
) {
    warn!("Setting up the skybox...");

    // Load the cubemap texture
    let cubemap_texture: Handle<Image> = asset_server.load("textures/skybox/cubemap.ktx2");

    // Store the texture in a resource
    commands.insert_resource(SkyboxTexture {
        image: cubemap_texture.clone(),
    });

    // Add the skybox to the camera
    for camera_entity in query.iter_mut() {
        commands.entity(camera_entity).insert(Skybox {
            image: cubemap_texture.clone(),
            brightness: 1.0,
            rotation: Quat::IDENTITY,
        });
        warn!("Skybox added to camera.");
    }

    warn!("Skybox setup complete.");
}

fn skybox_control_system(
    keyboard_input: Res<ButtonInput<KeyCode>>,
    mut skybox_state: ResMut<SkyboxState>,
    time: Res<Time>,
    mut query: Query<&mut Skybox, With<Camera3d>>,
) {
    // Toggle visibility
    if keyboard_input.just_pressed(KeyCode::KeyV) {
        skybox_state.visible = !skybox_state.visible;
        warn!("Skybox visibility toggled: {}", skybox_state.visible);

        // Update brightness to toggle visibility
        for mut skybox in query.iter_mut() {
            skybox.brightness = if skybox_state.visible { 1.0 } else { 0.0 };
        }
    }

    // Rotate skybox
    let rotation_speed: f32 = 30.0; // degrees per second
    let delta_time = time.delta_secs();
    let mut rotated = false;

    let mut delta_rotation = Quat::IDENTITY;

    if keyboard_input.pressed(KeyCode::ArrowLeft) {
        delta_rotation *= Quat::from_rotation_y(rotation_speed.to_radians() * delta_time);
        rotated = true;
    }
    if keyboard_input.pressed(KeyCode::ArrowRight) {
        delta_rotation *= Quat::from_rotation_y(-rotation_speed.to_radians() * delta_time);
        rotated = true;
    }
    if keyboard_input.pressed(KeyCode::ArrowUp) {
        delta_rotation *= Quat::from_rotation_x(rotation_speed.to_radians() * delta_time);
        rotated = true;
    }
    if keyboard_input.pressed(KeyCode::ArrowDown) {
        delta_rotation *= Quat::from_rotation_x(-rotation_speed.to_radians() * delta_time);
        rotated = true;
    }

    if rotated {
        // Update the skybox rotation
        for mut skybox in query.iter_mut() {
            skybox.rotation *= delta_rotation;
        }
        warn!("Skybox rotated.");
    }
}

fn log_skybox_status(
    skybox_state: Res<SkyboxState>,
    time: Res<Time>,
    mut timer: Local<Timer>,
) {
    // Initialize the timer if needed
    if timer.finished() {
        *timer = Timer::from_seconds(1.0, TimerMode::Repeating);
    }
    // Update the timer
    timer.tick(time.delta());
    if timer.just_finished() {
        warn!(
            "Skybox status - Visible: {}",
            skybox_state.visible,
        );
    }
}

how I created the skybox:

#!/bin/bash

# Set path to KTX tools
KTX_BIN_PATH="$HOME/ktx_software/KTX-Software-4.3.2-Linux-x86_64/bin"

# Check dependencies
check_dependencies() {
    if [ ! -d "$KTX_BIN_PATH" ]; then
        echo "Error: KTX tools not found in $KTX_BIN_PATH"
        exit 1
    fi
}

create_ktx2() {
    local skybox_dir="$1"
    local output_file="$2"
    
    echo "Creating KTX2 file from skybox PNGs..."
    
    # Create uncompressed KTX2 cubemap
    if ! "$KTX_BIN_PATH/toktx" \
        --t2 \
        --cubemap \
        --genmipmap \
        --assign_oetf linear \
        "$output_file" \
        "${skybox_dir}/right.png" \
        "${skybox_dir}/left.png" \
        "${skybox_dir}/top.png" \
        "${skybox_dir}/bottom.png" \
        "${skybox_dir}/front.png" \
        "${skybox_dir}/back.png"; then
        echo "Error creating KTX2 file"
        exit 1
    fi
    
    echo "Successfully created $output_file"
    echo "File info:"
    "$KTX_BIN_PATH/ktxinfo" "$output_file"
}

# Main script
main() {
    local SKYBOX_DIR="bevy_webtransport/assets/textures/skybox"
    local OUTPUT_FILE="$SKYBOX_DIR/cubemap.ktx2"
    
    check_dependencies
    create_ktx2 "$SKYBOX_DIR" "$OUTPUT_FILE"
}

main "$@"

I would like to know what are all the good possibilities and so, meaning on linux for target wasm we have toktx but also ktx and other + differents flags so would like to know you recommendations for wasm target

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-RenderingDrawing game state to the screenC-BugAn unexpected or incorrect behaviorO-WebSpecific to web (WASM) buildsO-WebGL2Specific to the WebGL2 render APIS-Needs-InvestigationThis issue requires detective work to figure out what's going wrong

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions