Strange behavior of texture on mesh in Love2D, help needed to achieve the result I expected
The following code has the following output:
local mesh = nil
local img = love.graphics.newImage("test_blue.png")
function love.load()
mesh = love.graphics.newMesh(4, img, "fan")
mesh:setVertices({
{125, 100, 0, 0, 255, 255, 255, 255}, --Top Left
{150, 100, 1, 0, 255, 255, 255, 255}, --Top Right
{200, 400, 1, 1, 255, 255, 255, 255}, --Bottom Right
{100, 400, 0, 1, 255, 255, 255, 255} --Bottom Left
})
end
function love.draw()
love.graphics.draw(mesh, 200, 0)
end
I would like to know how to get a result like this:
source to share
Without using the 3D library, you cannot get the true depth effect without realizing perspective. The problem is that the polygon is made of 2D triangles, and only 2D effects can be applied like shearing or scaling (in general). Parallel lines in the texture will always be parallel, which doesn't match your bottom image as they converge towards the vanishing point.
Read more in Section Perspective Texture Adjustments
Changing the texture map coordinate can minimize some of the artifacts visually by clipping to the vanishing point rather than scaling.
The lines in the texture do not have to be parallel if they are part of separate triangles, and therefore adding more triangles allows them to move towards each other at the cost of more clipping.
Both changing the texture coordinates and using more triangles can be problematic for different texture styles, so you may need to tweak it on a case-by-case basis.
local mesh = nil
local img = love.graphics.newImage("test_blue.png")
function love.load()
mesh = love.graphics.newMesh(5, img, "strip")
local top_left = {125, 100, .125, 0, 255, 255, 255, 255}
local top_right = {150, 100, .875, 0, 255, 255, 255, 255}
local bot_right = {200, 400, 1, 1, 255, 255, 255, 255}
local bot_left = {100, 400, 0, 1, 255, 255, 255, 255}
local bot_mid = {150, 400, .5,1, 255, 255, 255, 255}
mesh:setVertices{
bot_left, top_left, bot_mid, top_right, bot_right,
}
end
function love.draw()
love.graphics.draw(mesh, 200, 0)
end
source to share
The math for making shaders capable of fixing this issue is usually explained on google in many threads and there are several approaches (tag: perspective correct texture mapping).
If you want to create your own shader, or use a shader from a source other than Love2D, please remember that Love2D is currently using GLSL v.1.20 with some minor changes.
There is a forum where you can get the completed shader file, currently for Love2D v.0.10.2. Easy to use, code commented correctly.
https://www.love2d.org/forums/viewtopic.php?f=5&t=12483&start=120 Posted by drunken_munki "Wed Apr 26, 2017 11:03 am