From 41a7a40ecf71613c6cb3f7823491c45d9c9c63e0 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Thu, 23 Feb 2023 11:53:10 +0000 Subject: implement new rendering system using gpu instancing this has extremely better performance on my machine also, player is circle now --- client/chunk.lua | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 client/chunk.lua (limited to 'client/chunk.lua') diff --git a/client/chunk.lua b/client/chunk.lua new file mode 100644 index 0000000..73fe949 --- /dev/null +++ b/client/chunk.lua @@ -0,0 +1,42 @@ +local class = require"common.class" +local chunk = require"common.chunk" +local Chunk = require"common.chunk".Chunk +local CHUNK_SIZE = require"common.constants".CHUNK_SIZE + +local ChunkC = class.extend(chunk.Chunk) +function ChunkC.make(cls,...) + local self = chunk.Chunk.make(cls,...) + self.imesh = cls.imesh_from_chunk_data(self) + return self +end +local function tile_to_vattr(tile) + -- for now tiles are always numbers + -- so this just returns the input + -- this function might move to drawing.lua once it does something less trivial + assert(type(tile) == "number","can't send non-numerical tile to gpu") + return tile +end +function ChunkC.imesh_from_chunk_data(the_chunk) + local mesh_data = {} + for q=0,CHUNK_SIZE-1 do + for r =0,CHUNK_SIZE-1 do + local t = the_chunk:_atqr(q,r) + table.insert(mesh_data,{tile_to_vattr(t)}) + end + end + + local imesh = love.graphics.newMesh({ + {"tile_type","float",1} + }, mesh_data, nil, "dynamic") + + return imesh +end + +function ChunkC.set_at(self,hoffs,tile) + Chunk.set_at(self,hoffs,tile) + + local idx = chunk.index(hoffs.q,hoffs.r) + self.imesh:setVertexAttribute(idx, 1, tile_to_vattr(tile)) +end + +return {ChunkC=ChunkC} -- cgit v1.2.3