Packed uv and positions for floor and editor renderer

This commit is contained in:
Anuken
2025-07-21 20:32:44 -04:00
parent b412830062
commit 619887ec4b
6 changed files with 87 additions and 75 deletions
@@ -13,13 +13,15 @@ import mindustry.world.*;
import static mindustry.Vars.*;
public class MapRenderer implements Disposable{
public class EditorRenderer implements Disposable{
private static final float packPad = tilesize * 10f;
private static final int chunkSize = 60;
private static final Seq<Tile> tmpTiles = new Seq<>();
private EditorSpriteCache[][] chunks;
private IntSet recacheChunks = new IntSet();
private int width, height;
private float packWidth, packHeight;
private Shader shader;
@@ -31,6 +33,8 @@ public class MapRenderer implements Disposable{
this.width = width;
this.height = height;
packWidth = width * tilesize + packPad * 2;
packHeight = height * tilesize + packPad * 2;
recache();
}
@@ -111,7 +115,7 @@ public class MapRenderer implements Disposable{
}
shader.bind();
shader.setUniformMatrix4("u_projTrans", Core.camera.mat);
shader.setUniformMatrix4("u_projTrans", Tmp.m1.set(Core.camera.mat).translate(-packPad, -packPad).scale(packWidth, packHeight));
for(int x = 0; x < chunks.length; x++){
for(int y = 0; y < chunks[0].length; y++){
@@ -160,7 +164,7 @@ public class MapRenderer implements Disposable{
chunks[cx][cy] = null;
}
EditorSpriteCache cache = new EditorSpriteCache(renderer.blocks.floor.getVertexBuffer());
EditorSpriteCache cache = new EditorSpriteCache(renderer.blocks.floor.getVertexBuffer(), packPad, packPad, packWidth, packHeight);
TextureRegion teamRegion = Core.atlas.find("block-border");
@@ -8,12 +8,13 @@ import arc.struct.*;
import arc.util.*;
public class EditorSpriteCache implements Disposable{
//xy + color + uv
static final int vertexSize = 2 + 1 + 2;
//packed xy + color + packed uv
static final int vertexSize = 1 + 1 + 1;
private @Nullable Mesh mesh;
private final Seq<Texture> textures = new Seq<>(8);
private final IntSeq counts = new IntSeq(8);
private final float packX, packY, packW, packH;
private float[] tmpVertices;
@@ -21,8 +22,12 @@ public class EditorSpriteCache implements Disposable{
private int index;
/** @param tmpVertices Temporary buffer to hold vertices while building up sprites. Should be large enough to hold all sprite data this cache will contain. */
public EditorSpriteCache(float[] tmpVertices){
public EditorSpriteCache(float[] tmpVertices, float packX, float packY, float packW, float packH){
this.tmpVertices = tmpVertices;
this.packX = packX;
this.packY = packY;
this.packW = packW;
this.packH = packH;
}
/** @return whether anything was added to the cache. */
@@ -41,9 +46,9 @@ public class EditorSpriteCache implements Disposable{
if(mesh != null) mesh.dispose();
mesh = new Mesh(true, index / vertexSize, 0,
VertexAttribute.position,
VertexAttribute.packedPosition,
VertexAttribute.color,
VertexAttribute.texCoords
VertexAttribute.packedTexCoords
);
mesh.indices = indices;
mesh.setVertices(tmpVertices, 0, index);
@@ -111,29 +116,21 @@ public class EditorSpriteCache implements Disposable{
float[] verts = tmpVertices;
Texture texture = region.texture;
verts[idx + 0] = x1;
verts[idx + 1] = y1;
verts[idx + 2] = colorPacked;
verts[idx + 3] = u;
verts[idx + 4] = v;
verts[idx + 0] = pack(x1, y1);
verts[idx + 1] = colorPacked;
verts[idx + 2] = Pack.packUv(u, v);
verts[idx + 5] = x2;
verts[idx + 6] = y2;
verts[idx + 3] = pack(x2, y2);
verts[idx + 4] = colorPacked;
verts[idx + 5] = Pack.packUv(u, v2);
verts[idx + 6] = pack(x3, y3);
verts[idx + 7] = colorPacked;
verts[idx + 8] = u;
verts[idx + 9] = v2;
verts[idx + 8] = Pack.packUv(u2, v2);
verts[idx + 10] = x3;
verts[idx + 11] = y3;
verts[idx + 12] = colorPacked;
verts[idx + 13] = u2;
verts[idx + 14] = v2;
verts[idx + 15] = x4;
verts[idx + 16] = y4;
verts[idx + 17] = colorPacked;
verts[idx + 18] = u2;
verts[idx + 19] = v;
verts[idx + 9] = pack(x4, y4);
verts[idx + 10] = colorPacked;
verts[idx + 11] = Pack.packUv(u2, v);
int lastIndex = textures.size - 1;
if(lastIndex < 0 || textures.get(lastIndex) != texture){
@@ -146,6 +143,10 @@ public class EditorSpriteCache implements Disposable{
index += vertexSize * 4;
}
private float pack(float x, float y){
return Pack.packUv((x + packX) / packW, (y + packY) / packH);
}
/** Renders the cached mesh. The shader must already have the correct view matrix set as a uniform. */
public void render(Shader shader){
if(mesh == null) throw new IllegalStateException("Cache is empty, call build() first.");
+1 -1
View File
@@ -20,7 +20,7 @@ public class MapEditor{
public static final float[] brushSizes = {1, 1.5f, 2, 3, 4, 5, 9, 15, 20};
public StringMap tags = new StringMap();
public MapRenderer renderer = new MapRenderer();
public EditorRenderer renderer = new EditorRenderer();
private final Context context = new Context();
private OperationStack stack = new OperationStack();