From 50a3a42ab00cf303c453c805b7a369a25c7cae89 Mon Sep 17 00:00:00 2001 From: shylie Date: Tue, 15 Jul 2025 00:52:46 -0400 Subject: [PATCH] Update to love12; fix transparency --- main.lua | 2 +- zprite/init.lua | 86 +++++++++++++++++++++++++++++-------------------- 2 files changed, 52 insertions(+), 36 deletions(-) diff --git a/main.lua b/main.lua index b07c0fd..b940292 100644 --- a/main.lua +++ b/main.lua @@ -16,7 +16,7 @@ z._height_scale = 6 for i = 1, 32 do for j = 1, 32 do - local layers = love.math.noise(i * 0.1, j * 0.1) * 20 + 12 + local layers = love.math.simplexNoise(i * 0.06, j * 0.06) * 20 + 12 z:put(i * 24, j * 24, BIG_BLOCK, math.floor(layers) + 1, color) end end diff --git a/zprite/init.lua b/zprite/init.lua index ad5e7fa..084ea23 100644 --- a/zprite/init.lua +++ b/zprite/init.lua @@ -1,11 +1,13 @@ ---@class zprite ----@field _texture love.Image ----@field _instance_vertex_count integer ----@field _mesh love.Mesh ----@field _instance_mesh love.Mesh ----@field _instances {}[] ----@field _height_scale number ----@field _changed boolean +---@field private _texture love.Image +---@field private _instance_vertex_count integer +---@field private _mesh love.Mesh +---@field private _instance_buffer love.Buffer +---@field private _instances {integer: {}[]} +---@field private _top_layer integer +---@field private _height_scale number +---@field private _changed boolean +---@field private _count integer local zprite = {} zprite.__index = zprite @@ -13,11 +15,11 @@ local ZPRITE_SHADER = love.graphics.newShader([[ varying vec4 VsColorOut; #ifdef VERTEX -attribute vec2 InstancePosition; -attribute vec2 InstanceTextureOffset; -attribute vec2 InstanceScale; -attribute float InstanceLayer; -attribute vec4 InstanceColor; +layout (location = 3) attribute vec2 InstancePosition; +layout (location = 4) attribute vec2 InstanceTextureOffset; +layout (location = 5) attribute vec2 InstanceScale; +layout (location = 6) attribute float InstanceLayer; +layout (location = 7) attribute vec4 InstanceColor; uniform float HeightScale; @@ -68,29 +70,30 @@ function zprite.new(texture, max_instances) obj._texture = texture obj._instance_vertex_count = max_instances or 16384 - obj._mesh = love.graphics.newMesh( - { { "VertexPosition", "float", 2 }, { "VertexTexCoord", "float", 2 } }, - vertices, - "strip", - "static" - ) + obj._mesh = love.graphics.newMesh({ + { name = "VertexPosition", format = "floatvec2", location = 0 }, + { name = "VertexTexCoord", format = "floatvec2", location = 1 }, + }, vertices, "strip", "static") obj._mesh:setTexture(texture) - obj._instance_mesh = love.graphics.newMesh({ - { "InstancePosition", "float", 2 }, - { "InstanceTextureOffset", "float", 2 }, - { "InstanceScale", "float", 2 }, - { "InstanceLayer", "float", 1 }, - { "InstanceColor", "float", 4 }, - }, obj._instance_vertex_count, nil, "dynamic") + obj._instance_buffer = love.graphics.newBuffer({ + { name = "InstancePosition", format = "floatvec2", location = 3 }, + { name = "InstanceTextureOffset", format = "floatvec2", location = 4 }, + { name = "InstanceScale", format = "floatvec2", location = 5 }, + { name = "InstanceLayer", format = "float", location = 6 }, + { name = "InstanceColor", format = "floatvec4", location = 7 }, + }, obj._instance_vertex_count, { vertex = true }) obj._instances = {} obj._height_scale = 4 obj._changed = false - obj._mesh:attachAttribute("InstancePosition", obj._instance_mesh, "perinstance") - obj._mesh:attachAttribute("InstanceTextureOffset", obj._instance_mesh, "perinstance") - obj._mesh:attachAttribute("InstanceScale", obj._instance_mesh, "perinstance") - obj._mesh:attachAttribute("InstanceLayer", obj._instance_mesh, "perinstance") - obj._mesh:attachAttribute("InstanceColor", obj._instance_mesh, "perinstance") + obj._mesh:attachAttribute(3, obj._instance_buffer, "perinstance") + obj._mesh:attachAttribute(4, obj._instance_buffer, "perinstance") + obj._mesh:attachAttribute(5, obj._instance_buffer, "perinstance") + obj._mesh:attachAttribute(6, obj._instance_buffer, "perinstance") + obj._mesh:attachAttribute(7, obj._instance_buffer, "perinstance") + + obj._top_layer = 0 + obj._count = 0 return obj end @@ -99,6 +102,8 @@ end function zprite:clear() -- no need to set _changed to true, as #self._instances will be 0 self._instances = {} + self._top_layer = 0 + self._count = 0 end --- Add an 'entity' to be rendered @@ -126,7 +131,14 @@ function zprite:put(x, y, quad, layer_count, color_map, base_layer) if not a then a = 1 end - table.insert(self._instances, { x, y, qx / tw, qy / th, qw, qh, i + base_layer, r, g, b, a }) + if not self._instances[i + base_layer] then + self._instances[i + base_layer] = {} + if i > self._top_layer then + self._top_layer = i + end + end + table.insert(self._instances[i], { x, y, qx / tw, qy / th, qw, qh, i + base_layer, r, g, b, a }) + self._count = self._count + 1 end end @@ -134,17 +146,21 @@ end ---@overload fun(self, x: number, y: number, r: number?, sx: number?, sy: number?, ox: number?, oy: number?, kx: number?, ky: number?) ---@overload fun(self, transform: love.Transform) function zprite:draw(...) - love.graphics.setDepthMode("less", true) - love.graphics.setShader(ZPRITE_SHADER) ZPRITE_SHADER:send("HeightScale", self._height_scale) if self._changed then - self._instance_mesh:setVertices(self._instances) + local current = 1 + for i = 1, self._top_layer do + if self._instances[i] then + self._instance_buffer:setArrayData(self._instances[i], 1, current, #self._instances[i]) + current = current + #self._instances[i] + end + end self._changed = false end - love.graphics.drawInstanced(self._mesh, #self._instances, ...) + love.graphics.drawInstanced(self._mesh, self._count, ...) end return zprite