splashdust / bevy_voxel_world

Easy to use voxel world for Bevy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Alpha channel supported?

bbarker opened this issue · comments

Hi - I loaded up some textures with alpha channels, but the alpha is rendered as black. Just checking if there's currently a way to support alpha, or if this should be converted to a feature request, assuming that is ok.

Hi, no it is not supported yet, but it should be! I'll mark this is a feature request.

Thanks! btw - I love this stuff - worked on a related voxel world project in Scala a long time ago. Would you be interested in a discussion forum on github or discord to discuss these things more informally? I need to decompress a bit after the game jam but would love to come back to it.

@bbarker Oh, cool! Yeah for sure, I enabled the discussions tab on this repo, so that's an option now. I'm kinda noob when it comes to Discord, but I've seen that the bevy discord has a channel for crates, maybe that would be an option too.

Any updates on this?

@insberr Yes, to some extent at least.

It is now possible to set up your own Bevy material. That means you can configure alpha blending however you like. It is now also possible to set up multiple world instances with different materials, so you could for example use a separate world with alpha blending and put transparent voxels there.

Here's an example of adding a second world with a custom material and shader: multiple_worlds.rs

Assigning different Bevy materials to different voxels within the same world is not yet supported at this point.

Thank you, I will take a look. I have a basic understanding of graphics and shaders, but not enough to know what I am doing in shaders. How hard would it be to make a material that adds alpha?

@insberr In the example I linked to, you need to add AlphaMode::Blend to the material (check this bevy example: https://bevyengine.org/examples/3D%20Rendering/blend-modes/).

The easiest way to set it up for bevy_voxel_world and combine with textures would be to use Bevy's ExtendedMaterial and extend StandardMaterial. Like this:

ExtendedMaterial {
    base: StandardMaterial {
        alpha_mode: AlphaMode::Blend, // Now you can set AlphaMode here
        ..default()
    },
    extension: MyCustomMaterial { // Extend your custom material
        voxels_texture: Handle::<Image>::default(),
    },
}

Check the bevy examples on ExtendedMaterial for a more complete example on how to use that.

For the shader part, you can take a look at the fragment shader that bevy_voxel_world use internally. There you can see how the array texture is sampled: https://github.com/splashdust/bevy_voxel_world/blob/main/src/shaders/voxel_texture.wgsl#L118

Interesting, thank you. Do I have to use ExtendedMaterial or can I use StandardMaterial directly? At least at the moment I do not need textures, just colored cubes.

@insberr No, you can use any material. But to get different colors for different voxels you need a shader that can pick up the custom ATTRIBUTE_TEX_INDEX vertex attribute (to map to colors in your case), and afaik you can't use a custom shader with StandardMaterial alone, so ExtendedMaterial is an easy way to get all the properties of StandardMaterial, while adding your own shader.

Oh, I see, thank you. I will try this out and let you know how it goes. I am making a falling sand simulation/game in 3D (not an easy task) so if there are any suggestions on how else to help performance, that would be helpful. I did try the voxels, but it seems to make things start to lag, and that mostly comes from having to for loop over the entire world and set each position to air (because everything changes position every update). So I am thinking I may have to make my own branch of this where I can just give it the data I already have each update and it uses that to render voxels, rather than having to convert my data to voxels and create each voxel again every update. If that makes any sense haha.

@insberr I see. This plugin is not really optimized for that use-case.

Every time a voxel changes, the containing chunk needs to be re-meshed. You may be able to improve it a little bit by only clearing the positions you set on the last frame, but will probably still be too slow.

If you need fluid voxel based sand simulation, you should probably look into a ray tracing based approach instead.

I think instancing will work for my use, voxels are not fast enough. I will have to create a chunk system of sorts in order to not render things that are out of sight, but that's something for later when things become laggy. I was able to get shadows almost working with instancing, but still have no idea how to get alpha working with instancing. I did manage to get alpha working with voxels, but there was some weird stuff going on with two worlds and their z-index. And its not performant enough.
By any chance do you have any idea how to make alpha work with instancing?

@insberr instancing won't work for your use-case with this plugin. Instancing happens per mesh, and here every 32^3 chunk of voxels are a single mesh.

Oh haha, I wasnt very clear. I am talking of using instancing or voxels, not both.

Oh right, I see that now :D

I would recommend asking in the Bevy discord. Lot's of friendly, helpful and very knowledgable folks there!

Will do. Thank you for the help!