Skip to content

Voxels changing position when other entities are present #2

Closed

Description

@arodgers-dg

Hello, I'm trying to integrate the voxel plotting into an existing application. I'm seeing the transformation of the voxels change with that of seemingly unrelated cuboid entities and with changing camera position. It's a bit of a long shot but I was wondering if you had any ideas what might cause such behaviour @hacknus ?

e.g. without other cuboid entities:
Image
with an added cuboid entity, with some rotation:

Image

I'm pretty sure that my system that handles the updating of the cuboid transform should not alter the spawned voxel entity. Any insight is hugely appreciated, the integration feels close except for this odd behaviour!

Activity

  1. hacknus commented on Nov 6, 2025

    @hacknus
    Owner

    Hello @arodgers-dg
    Thanks for bringing this up! This looks odd indeed. Can you share your code?

  2. arodgers-dg commented on Nov 6, 2025

    @arodgers-dg
    Author

    I can do when I have some time tomorrow, for now I am wondering if it is a consequence of the shader using get_world_from_local(0u)? I have no experience with .wgsl files, so just a hunch.

    #import bevy_pbr::mesh_functions::{get_world_from_local, mesh_position_local_to_clip}
    
    struct Vertex {
        @location(0) position: vec3<f32>,
        @location(1) normal: vec3<f32>,
        @location(2) uv: vec2<f32>,
    
        @location(3) i_pos_scale: vec4<f32>,
        @location(4) i_color: vec4<f32>,
    };
    
    struct VertexOutput {
        @builtin(position) clip_position: vec4<f32>,
        @location(0) color: vec4<f32>,
    };
    
    @vertex
    fn vertex(vertex: Vertex) -> VertexOutput {
        let position = vertex.position * vertex.i_pos_scale.w + vertex.i_pos_scale.xyz;
        var out: VertexOutput;
        // NOTE: Passing 0 as the instance_index to get_world_from_local() is a hack
        // for this example as the instance_index builtin would map to the wrong
        // index in the Mesh array. This index could be passed in via another
        // uniform instead but it's unnecessary for the example.
        out.clip_position = mesh_position_local_to_clip(
            get_world_from_local(0u),
            vec4<f32>(position, 1.0)
        );
        out.color = vertex.i_color;
        return out;
    }
    
    @fragment
    fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {
        return in.color;
    }
    
  3. hacknus commented on Nov 7, 2025

    @hacknus
    Owner

    Yes, that might indeed be the cause. I'm also not too familiar with wgsl, unfortunately. I based this code on the official example from bevy (it has been moved to here in bevy 0.17: https://github.com/bevyengine/bevy/blob/main/examples/shader_advanced/custom_shader_instancing.rs)

  4. arodgers-dg commented on Nov 7, 2025

    @arodgers-dg
    Author

    F.Y.I I managed to fix the behaviour with the following:

    #import bevy_pbr::mesh_functions::{mesh_position_local_to_world}
    #import bevy_pbr::view_transformations::position_world_to_clip
    
    struct Vertex {
        @location(0) position: vec3<f32>,
        @location(1) normal: vec3<f32>,
        @location(2) uv: vec2<f32>,
    
        @location(3) i_pos_scale: vec4<f32>,
        @location(4) i_color: vec4<f32>,
    };
    
    struct VertexOutput {
        @builtin(position) clip_position: vec4<f32>,
        @location(0) color: vec4<f32>,
    };
    
    @vertex
    fn vertex(vertex: Vertex) -> VertexOutput {
        let world_position = vertex.position * vertex.i_pos_scale.w + vertex.i_pos_scale.xyz;
    
        var out: VertexOutput;
        out.clip_position = position_world_to_clip(world_position);
        out.color = vertex.i_color;
        return out;
    }
    
    @fragment
    fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {
        return in.color;
    }
    

    👍

  5. hacknus commented on Nov 7, 2025

    @hacknus
    Owner

    oh cool! can you create a PR?

  6. arodgers-dg commented on Dec 1, 2025

    @arodgers-dg
    Author

    Closed by #3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions