Skip to content

Change tilemap wgsl shader to support mipmaps#23929

Open
christopherverch wants to merge 2 commits into
bevyengine:mainfrom
christopherverch:fix/tilemap-shader-mipmaps
Open

Change tilemap wgsl shader to support mipmaps#23929
christopherverch wants to merge 2 commits into
bevyengine:mainfrom
christopherverch:fix/tilemap-shader-mipmaps

Conversation

@christopherverch
Copy link
Copy Markdown

Objective

If a tilemap texture uses mipmaps, texturesample in the shader leads to strange lines at the edges, I believe because the uv reset between tiles makes it jump from 0.99 to 0.0 at the edges, causing the shader to always sample the lowest res mipmap at the edges.

Solution

textureSampleGrad can use the uv change between the original uv coords, i.e. 0.9 vs 1.1 instead of 0.9 vs 0.1, resulting in the correct mipmap to be chosen

Testing

Tested with a project that uses mipmaps, and this has no visible changes on the example tilemap program, though I think this change is simple enough that it can be verified mathematically.

Showcase

Click to view showcase Before: image

After:
image

@github-actions
Copy link
Copy Markdown
Contributor

Welcome, new contributor!

Please make sure you've read our contributing guide, as well as our policy regarding AI usage, and we look forward to reviewing your pull request shortly ✨

@kfc35 kfc35 added C-Bug An unexpected or incorrect behavior A-Rendering Drawing game state to the screen D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Apr 22, 2026
@github-project-automation github-project-automation Bot moved this to Needs SME Triage in Rendering Apr 22, 2026
var dy = dpdy(tile_uv);

if (tile.mirror_h) {
local_uv.x = 1 - local_uv.x;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be a stupid q, but why is there no change with dx here?
(similar question for when it is vertically mirrored)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm no expert in shader programming, but as far as I understand:
Say the sampled uv was (0.9, 0.3) and now it's (0.8, 0.1). If we mirror the x, the difference (dx) is the same (0.1) regardless of if we're going from 0.9 to 0.8, or 0.8 to 0.9, so the mirroring is irrelevant. Same deal for the y. If we mirror the dimensions however, then instead of dx = 0.9 - 0.8, it's 0.3 - 0.1 (i.e. dy)

@kfc35 kfc35 added the D-Shaders This code uses GPU shader languages label Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior D-Shaders This code uses GPU shader languages D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward

Projects

Status: Needs SME Triage

Development

Successfully merging this pull request may close these issues.

3 participants