This can be simplified further for most use cases: use the builtin function texture2DProj. The code becomes simpler, and it's faster too because there's no dependent texture fetch in the fragment shader, which can be slow especially on mobile GPUs.
OP's solution is the way to go when using ArrayImages though, since texture2DProj can't be used with sampler2DArray. There is a general textureProj function that may work for this, but I'm not sure; haven't used it.
OP's modified code:
Code: Select all
local perspective = love.graphics.newShader [[
uniform Image MainTex;
void effect() {
love_PixelColor = texture2DProj(MainTex, VaryingTexCoord.xyz);
}
]]
local width_top, width_bottom = 300, 600
local height = 300
local vertexFormat = {
{"VertexPosition", "float", 2},
{"VertexTexCoord", "float", 3}
}
local ratio = width_bottom / width_top
local uv = {
{ u = 0, v = 0, z = 1 },
{ u = 1, v = 0, z = 1 },
{ u = 1 * ratio, v = 1 * ratio, z = 1 * ratio },
{ u = 0 * ratio, v = 1 * ratio, z = 1 * ratio }
}
local mesh = love.graphics.newMesh( vertexFormat, {
-- X, Y, U, V, Z
{ -width_top/2, 0, uv[1].u, uv[1].v, uv[1].z }, -- Top Left Corner
{ width_top/2, 0, uv[2].u, uv[2].v, uv[2].z }, -- Top Right Corner
{ width_bottom/2, height, uv[3].u, uv[3].v, uv[3].z }, -- Bottom Right Corner
{ -width_bottom/2, height, uv[4].u, uv[4].v, uv[4].z } -- Bottom Left Corner
}, "fan", "static")
love.graphics.setDefaultFilter("nearest")
local texture = love.graphics.newImage("0.png")
mesh:setTexture(texture)
function love.draw()
love.graphics.setShader(perspective)
love.graphics.draw(mesh,400,150)
end