diff --git a/core/assets/shaders/blockbuild.frag b/core/assets/shaders/blockbuild.frag index b227d899df..10132b0610 100644 --- a/core/assets/shaders/blockbuild.frag +++ b/core/assets/shaders/blockbuild.frag @@ -1,6 +1,6 @@ #define HIGHP -uniform sampler2D u_texture; +uniform highp sampler2DArray u_texture; uniform vec2 u_texsize; uniform vec2 u_uv; @@ -10,22 +10,23 @@ uniform float u_time; uniform float u_alpha; varying vec4 v_color; -varying vec2 v_texCoords; +varying vec3 v_texCoords; -bool id(vec2 coords, vec4 base){ - vec4 target = texture2D(u_texture, coords); +bool id(vec2 coords, vec4 base, float depth){ + vec4 target = texture2D(u_texture, vec3(coords, depth)); return target.a < 0.1 || (coords.x < u_uv.x || coords.y < u_uv.y || coords.x > u_uv2.x || coords.y > u_uv2.y); } -bool cont(vec2 T, vec2 v){ +bool cont(vec3 coords, vec2 v){ const float step = 3.5; - vec4 base = texture2D(u_texture, T); + vec4 base = texture(u_texture, coords); + vec2 T = coords.xy; return base.a > 0.1 && - (id(T + vec2(0, step) * v, base) || id(T + vec2(0, -step) * v, base) || - id(T + vec2(step, 0) * v, base) || id(T + vec2(-step, 0) * v, base) || - id(T + vec2(step, step) * v, base) || id(T + vec2(step, -step) * v, base) || - id(T + vec2(-step, -step) * v, base) || id(T + vec2(-step, step) * v, base)); + (id(T + vec2(0, step) * v, base, coords.z) || id(T + vec2(0, -step) * v, base, coords.z) || + id(T + vec2(step, 0) * v, base, coords.z) || id(T + vec2(-step, 0) * v, base, coords.z) || + id(T + vec2(step, step) * v, base, coords.z) || id(T + vec2(step, -step) * v, base, coords.z) || + id(T + vec2(-step, -step) * v, base, coords.z) || id(T + vec2(-step, step) * v, base, coords.z)); } vec4 blend(vec4 dst, vec4 src){ @@ -34,18 +35,16 @@ vec4 blend(vec4 dst, vec4 src){ void main(){ - vec2 t = v_texCoords.xy; - vec2 v = vec2(1.0/u_texsize.x, 1.0/u_texsize.y); - vec2 coords = (v_texCoords-u_uv) / v; + vec2 coords = (v_texCoords.xy-u_uv) / v; float value = coords.x + coords.y; - vec4 color = texture2D(u_texture, t); + vec4 color = texture(u_texture, v_texCoords); vec2 center = ((u_uv + u_uv2)/2.0 - u_uv) /v; float dst = (abs(center.x - coords.x) + abs(center.y - coords.y))/2.0; - if((mod(u_time / 1.5 + value, 20.0) < 15.0 && cont(t, v))){ + if((mod(u_time / 1.5 + value, 20.0) < 15.0 && cont(v_texCoords, v))){ gl_FragColor = blend(color, v_color) * vec4(vec3(1.0), u_alpha); }else if(dst > (1.0-u_progress) * (center.x)){ gl_FragColor = color * vec4(vec3(1.0), u_alpha); diff --git a/core/assets/shaders/default_batch.vert b/core/assets/shaders/default_batch.vert new file mode 100644 index 0000000000..b7b9ee09cb --- /dev/null +++ b/core/assets/shaders/default_batch.vert @@ -0,0 +1,16 @@ +uniform mat4 u_projTrans; + +attribute vec4 a_position; +attribute vec3 a_texCoord0; +attribute vec4 a_color; + +varying vec4 v_color; +varying vec3 v_texCoords; + +uniform vec2 u_viewportInverse; + +void main(){ + gl_Position = u_projTrans * a_position; + v_texCoords = a_texCoord0; + v_color = a_color; +} diff --git a/core/assets/shaders/unitbuild.frag b/core/assets/shaders/unitbuild.frag index 9f4386aba7..e57ba54b5e 100644 --- a/core/assets/shaders/unitbuild.frag +++ b/core/assets/shaders/unitbuild.frag @@ -1,4 +1,4 @@ -uniform sampler2D u_texture; +uniform highp sampler2DArray u_texture; uniform float u_time; uniform float u_progress; @@ -8,13 +8,13 @@ uniform vec2 u_uv2; uniform vec2 u_texsize; varying vec4 v_color; -varying vec2 v_texCoords; +varying vec3 v_texCoords; void main(){ - vec2 coords = (v_texCoords - u_uv) / (u_uv2 - u_uv); + vec2 coords = (v_texCoords.xy - u_uv) / (u_uv2 - u_uv); vec2 v = vec2(1.0/u_texsize.x, 1.0/u_texsize.y); - vec4 c = texture2D(u_texture, v_texCoords); + vec4 c = texture(u_texture, v_texCoords); float alpha = c.a; c.a *= u_progress; diff --git a/core/src/mindustry/editor/EditorRenderer.java b/core/src/mindustry/editor/EditorRenderer.java index 833336cc05..5cb4f81f4c 100644 --- a/core/src/mindustry/editor/EditorRenderer.java +++ b/core/src/mindustry/editor/EditorRenderer.java @@ -57,13 +57,18 @@ public class EditorRenderer implements Disposable{ attribute vec4 a_position; attribute vec4 a_color; attribute vec2 a_texCoord0; + attribute float a_depth; + uniform mat4 u_projTrans; varying vec4 v_color; varying vec2 v_texCoords; + varying float v_depth; + void main(){ v_color = a_color; v_color.a = v_color.a * (255.0/254.0); v_texCoords = a_texCoord0; + v_depth = a_depth; gl_Position = u_projTrans * a_position; } """, @@ -71,10 +76,11 @@ public class EditorRenderer implements Disposable{ """ varying lowp vec4 v_color; varying vec2 v_texCoords; - uniform sampler2D u_texture; + varying float v_depth; + uniform highp sampler2DArray u_texture; void main(){ - gl_FragColor = v_color * texture2D(u_texture, v_texCoords); + gl_FragColor = v_color * texture2D(u_texture, vec3(v_texCoords, v_depth)); } """ ); diff --git a/core/src/mindustry/editor/EditorSpriteCache.java b/core/src/mindustry/editor/EditorSpriteCache.java index f7f7478611..3160a9cbe2 100644 --- a/core/src/mindustry/editor/EditorSpriteCache.java +++ b/core/src/mindustry/editor/EditorSpriteCache.java @@ -4,22 +4,21 @@ import arc.graphics.*; import arc.graphics.g2d.*; import arc.graphics.gl.*; import arc.math.*; -import arc.struct.*; import arc.util.*; public class EditorSpriteCache implements Disposable{ - //packed xy + color + packed uv - static final int vertexSize = 1 + 1 + 1; + //packed xy + color + packed uv + depth + static final int vertexSize = 1 + 1 + 1 + 1; private @Nullable Mesh mesh; - private final Seq textures = new Seq<>(8); - private final IntSeq counts = new IntSeq(8); + private int count; private final float packX, packY, packW, packH; private float[] tmpVertices; /** Index in tmpVertices of current vertex data. */ private int index; + private Texture texture; /** @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, float packX, float packY, float packW, float packH){ @@ -48,7 +47,8 @@ public class EditorSpriteCache implements Disposable{ mesh = new Mesh(true, index / vertexSize, 0, VertexAttribute.packedPosition, VertexAttribute.color, - VertexAttribute.packedTexCoords + VertexAttribute.packedTexCoords, + VertexAttribute.depthCoords ); mesh.indices = indices; mesh.setVertices(tmpVertices, 0, index); @@ -115,32 +115,31 @@ public class EditorSpriteCache implements Disposable{ int idx = index; float[] verts = tmpVertices; Texture texture = region.texture; + float depth = texture.getDepth(); verts[idx + 0] = pack(x1, y1); verts[idx + 1] = colorPacked; verts[idx + 2] = Pack.packUv(u, v); + verts[idx + 3] = depth; - verts[idx + 3] = pack(x2, y2); - verts[idx + 4] = colorPacked; - verts[idx + 5] = Pack.packUv(u, v2); + verts[idx + 4] = pack(x2, y2); + verts[idx + 5] = colorPacked; + verts[idx + 6] = Pack.packUv(u, v2); + verts[idx + 7] = depth; - verts[idx + 6] = pack(x3, y3); - verts[idx + 7] = colorPacked; - verts[idx + 8] = Pack.packUv(u2, v2); + verts[idx + 8] = pack(x3, y3); + verts[idx + 9] = colorPacked; + verts[idx + 10] = Pack.packUv(u2, v2); + verts[idx + 11] = depth; - 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){ - textures.add(texture); - counts.add(6); - }else{ - counts.incr(lastIndex, 6); - } + verts[idx + 12] = pack(x4, y4); + verts[idx + 13] = colorPacked; + verts[idx + 14] = Pack.packUv(u2, v); + verts[idx + 15] = depth; + count += 6; index += vertexSize * 4; + this.texture = texture; } private float pack(float x, float y){ @@ -151,15 +150,8 @@ public class EditorSpriteCache implements Disposable{ public void render(Shader shader){ if(mesh == null) throw new IllegalStateException("Cache is empty, call build() first."); - int offset = 0; - - for(int i = 0; i < textures.size; i++){ - int count = counts.items[i]; - textures.get(i).bind(); - - mesh.render(shader, Gl.triangles, offset, count); - offset += count; - } + texture.bind(); + mesh.render(shader, Gl.triangles, 0, count); } @Override diff --git a/core/src/mindustry/graphics/FloorRenderer.java b/core/src/mindustry/graphics/FloorRenderer.java index 9ddf740e6f..48219db2d3 100644 --- a/core/src/mindustry/graphics/FloorRenderer.java +++ b/core/src/mindustry/graphics/FloorRenderer.java @@ -525,20 +525,22 @@ public class FloorRenderer{ @Override protected void draw(Texture texture, float[] spriteVertices, int offset, int count){ //TODO broken - if(spriteVertices.length != 20){ - throw new IllegalArgumentException("cached vertices must be in non-mixcolor format (20 per sprite, 5 per vertex)"); + if(spriteVertices.length != 24){ + throw new IllegalArgumentException("cached vertices must be in non-mixcolor format (24 per sprite, 6 per vertex: x y color u v depth)"); } float[] verts = vertices; float[] src = spriteVertices; int idx = vidx; int sidx = offset; + float depth = texture.getDepth(); //convert 5-float format to internal packed 3-float format for(int i = 0; i < 4; i++){ verts[idx++] = pack(src[sidx++], src[sidx++]); verts[idx++] = src[sidx++]; verts[idx++] = Pack.packUv(src[sidx++], src[sidx++]); + verts[idx++] = depth; } vidx += spriteSize; diff --git a/core/src/mindustry/graphics/Shaders.java b/core/src/mindustry/graphics/Shaders.java index 9e6e111bd6..7f2b432eef 100644 --- a/core/src/mindustry/graphics/Shaders.java +++ b/core/src/mindustry/graphics/Shaders.java @@ -203,7 +203,7 @@ public class Shaders{ public TextureRegion region; public UnitBuildShader(){ - super("unitbuild", "default"); + super("unitbuild", "default_batch"); } @Override @@ -243,7 +243,7 @@ public class Shaders{ public float time; public BlockBuildShader(){ - super("blockbuild", "default"); + super("blockbuild", "default_batch"); } @Override @@ -477,6 +477,7 @@ public class Shaders{ } public static class LoadShader extends Shader{ + public LoadShader(String frag, String vert){ super(getShaderFi(vert + ".vert"), getShaderFi(frag + ".frag")); } diff --git a/core/src/mindustry/world/blocks/environment/ColoredFloor.java b/core/src/mindustry/world/blocks/environment/ColoredFloor.java index 719fa37178..03246d6151 100644 --- a/core/src/mindustry/world/blocks/environment/ColoredFloor.java +++ b/core/src/mindustry/world/blocks/environment/ColoredFloor.java @@ -20,8 +20,8 @@ public class ColoredFloor extends Floor{ /** If the alpha value of the color is set to this value, colors are interpolated across corners. This is essentially linear filtering for the whole "image". */ public static final int flagSmoothBlend = 2; - //4x (pos2 color uv2) - private static final float[] verts = new float[4 * 5]; + //4x (pos2 color uv2 depth) + private static final float[] verts = new float[4 * 6]; public Color defaultColor = Color.white; protected int defaultColorRgba; @@ -85,30 +85,35 @@ public class ColoredFloor extends Floor{ var region = autotileRegions[TileBitmask.values[bits]]; float s = Vars.tilesize/2f; float x = tile.worldx(), y = tile.worldy(); + float depth = region.getDepth(); verts[0] = x - s; verts[1] = y - s; verts[2] = sample(this, tile.x, tile.y, tile.x - 1, tile.y - 1, tile.x, tile.y - 1, tile.x - 1, tile.y); verts[3] = region.u; verts[4] = region.v2; + verts[5] = depth; - verts[5] = x + s; - verts[6] = y - s; - verts[7] = sample(this, tile.x, tile.y, tile.x, tile.y - 1, tile.x + 1, tile.y - 1, tile.x + 1, tile.y); - verts[8] = region.u2; - verts[9] = region.v2; + verts[6] = x + s; + verts[7] = y - s; + verts[8] = sample(this, tile.x, tile.y, tile.x, tile.y - 1, tile.x + 1, tile.y - 1, tile.x + 1, tile.y); + verts[9] = region.u2; + verts[10] = region.v2; + verts[11] = depth; - verts[10] = x + s; - verts[11] = y + s; - verts[12] = sample(this, tile.x, tile.y, tile.x + 1, tile.y + 1, tile.x, tile.y + 1, tile.x + 1, tile.y); - verts[13] = region.u2; - verts[14] = region.v; + verts[12] = x + s; + verts[13] = y + s; + verts[14] = sample(this, tile.x, tile.y, tile.x + 1, tile.y + 1, tile.x, tile.y + 1, tile.x + 1, tile.y); + verts[15] = region.u2; + verts[16] = region.v; + verts[17] = depth; - verts[15] = x - s; - verts[16] = y + s; - verts[17] = sample(this, tile.x, tile.y, tile.x - 1, tile.y + 1, tile.x, tile.y + 1, tile.x - 1, tile.y); - verts[18] = region.u; - verts[19] = region.v; + verts[18] = x - s; + verts[19] = y + s; + verts[20] = sample(this, tile.x, tile.y, tile.x - 1, tile.y + 1, tile.x, tile.y + 1, tile.x - 1, tile.y); + verts[21] = region.u; + verts[22] = region.v; + verts[23] = depth; Draw.vert(region.texture, verts, 0, verts.length); }else{ diff --git a/gradle.properties b/gradle.properties index 30dbfd1956..ecf513c900 100644 --- a/gradle.properties +++ b/gradle.properties @@ -26,4 +26,4 @@ org.gradle.caching=true org.gradle.internal.http.socketTimeout=100000 org.gradle.internal.http.connectionTimeout=100000 android.enableR8.fullMode=false -archash=1d92c52fcc +archash=fcc3251f16