Exposing world position in the vertex shader #618
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background
I was inspired by this video that showcases applying a single texture across an entire tilemap while preserving the edges. They achieve this effect with a shader. I wanted to try recreating that effect in bevy, and I'm currently using bevy_ecs_tilemap as part of the bevy_ecs_ldtk crate.
Problem
One trick this shader relies on is using the world position of each vertex to apply the texture. The world texture is not part of the
MeshVertexOutputthat the default vertex shader returns. I tried to work around this by using the "local" position and thefractfunction but the result was the texture following the camera around.I'm also not sure how to create my own vertex shader without the existing logic that runs today. I took a stab at it in my project but I couldn't get it to work and had to recreate a lot of code already present in the crate.
Proposed Solution
The world_position is already available in the
mesh_dataobject and is being used to calculate the local position. This change exposesmesh_data.world_positionso it can be used in the fragment shader without having to write a new vertex shader.