fenomas / noa

Experimental voxel game engine.
MIT License
611 stars 85 forks source link

UV offsets wrapping at edges of blocks #182

Closed MCArth closed 1 year ago

MCArth commented 1 year ago

Hi, I encountered an issue with non symmetrical block textures where the UVs wrapped (i assume due to floating point precision)

see https://playground.babylonjs.com/#6XIT28#2974 and https://forum.babylonjs.com/t/artefact-at-cube-edge-with-low-resolution-texture/33132

I fixed it in the current main by adding small offsets to the corner of quads, bring textures in a small amount:

    function addUVs(uvArr, offset, d, w, h, dir) {
        const delta = 0.0015
        for (var i = 0; i < 8; i++) uvArr[offset + i] = delta
        if (d === 2) {
            uvArr[offset + 1] = uvArr[offset + 3] = h-delta
            uvArr[offset + 2] = uvArr[offset + 4] = -dir * (w-delta)
            uvArr[offset + 0] = uvArr[offset + 6] = -dir*delta
        } else {
            uvArr[offset + 1] = uvArr[offset + 7] = w-delta
            uvArr[offset + 4] = uvArr[offset + 6] = dir * (h-delta)
            uvArr[offset + 0] = uvArr[offset + 2] = dir*delta
        }
    }

which seems to be a tradeoff between removing the artefacts and not having visible texture shifts when remeshing a chunk

The fix will be better with texture arrays on the current develop though, I imagine it's possible to bring the UVs in on each block face instead of only each greedy quad

fenomas commented 1 year ago

Hi, this is a nice catch and I see what you're talking about in the current master branch.

However I've got a pretty heavy rewrite going for meshing at the moment, which is on top of the texture atlas feature. And this issue doesn't seem to reproduce there - at least not in a straightforward way (colors bleeding through from other textures).

For now, let me table this and I'll ping this thread when I push the new to #develop. The fix you have here may be the right thing to do, but I'd like to figure outa clearer reproducing case so we can be sure it's fixing the root problem.

Thanks for trying the new features by the way!

fenomas commented 1 year ago

I haven't seen this since updating the mesher and (and also on updated Babylon), so closing this. Let me know if you see it again!