Skip to content

Commit

Permalink
feat: UnitInVisualizer
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharlottes committed Oct 9, 2024
1 parent 7c1ea1b commit eaa67ce
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 6 deletions.
10 changes: 10 additions & 0 deletions assets/shaders/transparent.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
uniform sampler2D u_texture;
uniform float u_alpha;

varying vec2 v_texCoords;

void main(){
vec4 color = texture2D(u_texture, v_texCoords);
color.a *= u_alpha;
gl_FragColor = color;
}
10 changes: 7 additions & 3 deletions src/informatis/SVars.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
import static mindustry.Vars.content;

public class SVars {
public static final TextureRegion
clear = atlas.find("clear");
public static final TextureRegion clear = atlas.find("clear");
public static final TextureRegion error = atlas.find("error");
public static final RangeShader turretRange = new RangeShader();
public static informatis.core.Pathfinder pathfinder;

public static void init() {
Expand All @@ -33,3 +31,9 @@ public static void init() {
});
}
}

class Cl {
public Cl(string asdf = "how") {

}
}
6 changes: 6 additions & 0 deletions src/informatis/shaders/Shaders.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package informatis.shaders;

public class Shaders {
public static final RangeShader turretRange = new RangeShader();
public static final TransparentShader transparent = new TransparentShader();
}
18 changes: 18 additions & 0 deletions src/informatis/shaders/TransparentShader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package informatis.shaders;

import arc.Core;
import arc.graphics.gl.Shader;
import mindustry.Vars;

public class TransparentShader extends Shader {
public float alpha = 1;

public TransparentShader() {
super(Core.files.internal("shaders/screenspace.vert"), Vars.tree.get("shaders/transparent.frag"));
}

@Override
public void apply(){
setUniformf("u_alpha", alpha);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import arc.graphics.gl.FrameBuffer;
import arc.struct.*;
import arc.util.Tmp;
import informatis.shaders.Shaders;
import mindustry.Vars;
import mindustry.game.EventType;
import mindustry.game.Team;
Expand All @@ -16,7 +17,6 @@
import mindustry.world.Tile;

import static arc.Core.graphics;
import static informatis.SVars.turretRange;

public class OverDrawManager {
public static final ObjectMap<OverDrawCategory, OverDraw[]> draws = ObjectMap.of(
Expand Down Expand Up @@ -45,7 +45,7 @@ public static void init() {
for(float zIndex : zIndexTeamCache) {
Draw.drawRange(zIndex, () -> effectBuffer.begin(Color.clear), () -> {
effectBuffer.end();
effectBuffer.blit(turretRange);
effectBuffer.blit(Shaders.turretRange);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import mindustry.game.EventType;

public class ToolManager {
public static final Tool[] tools = new Tool[] { new FogRemover(), new CameraScaler(), new AutoShooter() };
public static final Tool[] tools = new Tool[] { new FogRemover(), new CameraScaler(), new UnitVisualizer(), new AutoShooter() };

public static void init() {
Events.run(EventType.Trigger.update, () -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package informatis.ui.fragments.sidebar.windows.tools.tools;

import arc.Core;
import arc.Events;
import arc.graphics.Color;
import arc.graphics.g2d.Draw;
import arc.graphics.gl.FrameBuffer;
import arc.math.Mathf;
import arc.struct.Seq;
import arc.util.Log;
import informatis.shaders.Shaders;
import mindustry.game.EventType;
import mindustry.graphics.Layer;

public class UnitVisualizer extends Tool {
Seq<Object>[] cachedTextures;
final float[] layers = { Layer.flyingUnit, Layer.flyingUnitLow, Layer.legUnit, Layer.groundUnit};
final FrameBuffer buffer = new FrameBuffer();
float alpha = 1;

public UnitVisualizer() {
super("unitVisualizer");

Events.run(EventType.Trigger.draw, () -> {
float alphaTo = isEnabled() ? 0.5f : 1f;
alpha = Mathf.lerpDelta(alpha, alphaTo, 0.06f);
Shaders.transparent.alpha = alpha;
if (Mathf.equal(alpha, 1f)) return;
Log.info(alpha);
buffer.resize(Core.graphics.getWidth(), Core.graphics.getHeight());
for (float layer : layers) {
Draw.drawRange(layer, () -> buffer.begin(Color.clear), () -> {
buffer.end();
buffer.blit(Shaders.transparent);
});
}
});
}
}
/*
Core.app.post(() -> {
cachedTextures = new Seq[Vars.content.units().size];
Seq<UnitType> unitTypes = Vars.content.units();
for(int i = 0; i < cachedTextures.length; i++) {
UnitType unitType = unitTypes.get(i);
Seq<Object> regions = new Seq<>();
for (var part : unitType.parts) {
regions.add(part);
}
regions.add((Object) null);
for (var engine : unitType.engines) {
regions.add(engine);
}
regions.add((Object) null);
regions.add(unitType.baseRegion);
regions.add(unitType.legRegion);
regions.add(unitType.region);
regions.add(unitType.previewRegion);
regions.add(unitType.shadowRegion);
regions.add(unitType.cellRegion);
regions.add(unitType.itemCircleRegion);
regions.add(unitType.softShadowRegion);
regions.add(unitType.jointRegion);
regions.add(unitType.footRegion);
regions.add(unitType.legBaseRegion);
regions.add(unitType.baseJointRegion);
regions.add(unitType.outlineRegion);
regions.add(unitType.treadRegion);
for(Weapon weapon : unitType.weapons) {
for (var part : weapon.parts) {
regions.add(part);
}
regions.add((Object) null);
regions.add(weapon.region);
regions.add(weapon.cellRegion);
regions.add(weapon.heatRegion);
regions.add(weapon.outlineRegion);
}
cachedTextures[i] = regions;
}
});
}
@Override
public void setEnabled(boolean value) {
super.setEnabled(value);
if(this.isEnabled()) clearRegions();
else restoreRegions();
}
private void restoreRegions() {
Seq<UnitType> unitTypes = Vars.content.units();
for(int i = 0; i < cachedTextures.length; i++) {
Iterator<?> regions = cachedTextures[i].iterator();
UnitType unitType = unitTypes.get(i);
while(true) {
DrawPart region = (DrawPart) regions.next();
if(region == null) break;
unitType.parts.add(region);
}
while(true) {
UnitType.UnitEngine region = (UnitType.UnitEngine) regions.next();
if(region == null) break;
unitType.engines.add(region);
}
unitType.baseRegion = (TextureRegion) regions.next();
unitType.legRegion = (TextureRegion) regions.next();
unitType.region = (TextureRegion) regions.next();
unitType.previewRegion = (TextureRegion) regions.next();
unitType.shadowRegion = (TextureRegion) regions.next();
unitType.cellRegion = (TextureRegion) regions.next();
unitType.itemCircleRegion = (TextureRegion) regions.next();
unitType.softShadowRegion = (TextureRegion) regions.next();
unitType.jointRegion = (TextureRegion) regions.next();
unitType.footRegion = (TextureRegion) regions.next();
unitType.legBaseRegion = (TextureRegion) regions.next();
unitType.baseJointRegion = (TextureRegion) regions.next();
unitType.outlineRegion = (TextureRegion) regions.next();
unitType.treadRegion = (TextureRegion) regions.next();
for(Weapon weapon : unitType.weapons) {
while(true) {
DrawPart part = (DrawPart) regions.next();
if(part == null) break;
weapon.parts.add(part);
}
weapon.region = (TextureRegion) regions.next();
weapon.region = (TextureRegion) regions.next();
weapon.cellRegion = (TextureRegion) regions.next();
weapon.heatRegion = (TextureRegion) regions.next();
weapon.outlineRegion = (TextureRegion) regions.next();
}
}
}
private void clearRegions() {
Seq<UnitType> unitTypes = Vars.content.units();
for(int i = 0; i < cachedTextures.length; i++) {
UnitType unitType = unitTypes.get(i);
unitType.parts.clear();
unitType.engines.clear();
unitType.baseRegion =
unitType.legRegion =
unitType.region =
unitType.previewRegion =
unitType.shadowRegion =
unitType.cellRegion =
unitType.itemCircleRegion =
unitType.softShadowRegion =
unitType.jointRegion =
unitType.footRegion =
unitType.legBaseRegion =
unitType.baseJointRegion =
unitType.outlineRegion =
unitType.treadRegion = SVars.clear;
unitType.wreckRegions = unitType.segmentRegions = unitType.segmentOutlineRegions = new TextureRegion[]{};
unitType.treadRegions = new TextureRegion[][] {};
for(Weapon weapon : unitType.weapons) {
weapon.parts.clear();
weapon.region = weapon.cellRegion = weapon.heatRegion = weapon.outlineRegion = SVars.clear;
}
}
}
}
*/

0 comments on commit eaa67ce

Please sign in to comment.