Change tilemap wgsl shader to support mipmaps#23929
Conversation
|
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 ✨ |
| var dy = dpdy(tile_uv); | ||
|
|
||
| if (tile.mirror_h) { | ||
| local_uv.x = 1 - local_uv.x; |
There was a problem hiding this comment.
Might be a stupid q, but why is there no change with dx here?
(similar question for when it is vertically mirrored)
There was a problem hiding this comment.
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)
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:After:
