|
4 | 4 | import net.minecraft.client.Minecraft; |
5 | 5 | import net.minecraft.client.renderer.texture.DynamicTexture; |
6 | 6 | import net.minecraft.resources.ResourceLocation; |
| 7 | +import net.minecraft.util.FastColor; |
| 8 | +import net.minecraft.util.Mth; |
7 | 9 | import net.minecraft.util.Tuple; |
8 | 10 | import net.minecraft.world.phys.Vec2; |
9 | 11 |
|
10 | | -import java.awt.*; |
11 | | -import java.awt.image.BufferedImage; |
| 12 | +import java.awt.geom.Line2D; |
12 | 13 | import java.util.List; |
13 | 14 | import java.util.*; |
14 | 15 | import java.util.concurrent.*; |
@@ -86,46 +87,38 @@ private static Map<String, ResourceLocation> registerTextures(String patTextureK |
86 | 87 | } |
87 | 88 |
|
88 | 89 | private static NativeImage drawLines(List<Vec2> points, HexPatternPoints staticPoints, float unscaledLineWidth, int resPerUnit) { |
89 | | - BufferedImage img = new BufferedImage((int)(staticPoints.fullWidth*resPerUnit), (int)(staticPoints.fullHeight*resPerUnit), BufferedImage.TYPE_INT_ARGB); |
90 | | - Graphics2D g2d = img.createGraphics(); |
91 | | - g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
92 | | - |
93 | | - g2d.setColor(new Color(0xFF_FFFFFF)); // set it to white so we can reuse the texture with different colors |
94 | | - g2d.setStroke(new BasicStroke(unscaledLineWidth * resPerUnit, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); |
| 90 | + NativeImage nativeImage = new NativeImage((int)(staticPoints.fullWidth*resPerUnit), (int)(staticPoints.fullHeight*resPerUnit), true); |
95 | 91 | for (int i = 0; i < points.size() - 1; i++) { |
96 | 92 | Tuple<Integer, Integer> pointFrom = getTextureCoordinates(points.get(i), staticPoints, resPerUnit); |
97 | 93 | Tuple<Integer, Integer> pointTo = getTextureCoordinates(points.get(i+1), staticPoints, resPerUnit); |
98 | | - g2d.drawLine(pointFrom.getA(), pointFrom.getB(), pointTo.getA(), pointTo.getB()); |
| 94 | + drawLine(nativeImage, pointFrom.getA(), pointFrom.getB(), pointTo.getA(), pointTo.getB(), unscaledLineWidth * resPerUnit); |
99 | 95 | } |
100 | | - g2d.dispose(); |
101 | | - NativeImage nativeImage = new NativeImage(img.getWidth(), img.getHeight(), true); |
102 | | - for (int y = 0; y < img.getHeight(); y++) |
103 | | - for (int x = 0; x < img.getWidth(); x++) |
104 | | - nativeImage.setPixelRGBA(x, y, img.getRGB(x, y)); |
105 | 96 | return nativeImage; |
106 | 97 | } |
107 | 98 |
|
| 99 | + private static void drawLine(NativeImage image, int x0, int y0, int x1, int y1, float width) { |
| 100 | + var line = new Line2D.Float(x0, y0, x1, y1); |
| 101 | + var bounds = line.getBounds(); |
| 102 | + double halfWidth = width / 2; |
| 103 | + for (int x = (int) (bounds.x - width - 1); x < (int) (bounds.x + bounds.width + width + 1); x++) { |
| 104 | + for (int y = (int) (bounds.y - width - 1); y < (int) (bounds.y + bounds.height + width + 1); y++) { |
| 105 | + double dist = line.ptSegDist(x, y); |
| 106 | + int alpha = (int) (Mth.clamp(halfWidth - dist + 0.5, 0, 1) * 255); |
| 107 | + if (alpha > 0) { |
| 108 | + int oldAlpha = FastColor.ARGB32.alpha(image.getPixelRGBA(x, y)); |
| 109 | + int newAlpha = Math.max(oldAlpha, alpha); |
| 110 | + image.setPixelRGBA(x, y, 0xFFFFFF | (newAlpha << 24)); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
108 | 116 | private static Tuple<Integer, Integer> getTextureCoordinates(Vec2 point, HexPatternPoints staticPoints, int resPerUnit) { |
109 | 117 | int x = (int) ( point.x * resPerUnit); |
110 | 118 | int y = (int) ( point.y * resPerUnit); |
111 | 119 | return new Tuple<>(x, y); |
112 | 120 | } |
113 | 121 |
|
114 | | - // keeping this around just in case we ever decide to put the dots in the textures instead of dynamic |
115 | | - private static void drawHexagon(Graphics2D g2d, int x, int y, int radius) { |
116 | | - int fracOfCircle = 6; |
117 | | - Polygon hexagon = new Polygon(); |
118 | | - |
119 | | - for (int i = 0; i < fracOfCircle; i++) { |
120 | | - double theta = (i / (double) fracOfCircle) * Math.PI * 2; |
121 | | - int hx = (int) (x + Math.cos(theta) * radius); |
122 | | - int hy = (int) (y + Math.sin(theta) * radius); |
123 | | - hexagon.addPoint(hx, hy); |
124 | | - } |
125 | | - |
126 | | - g2d.fill(hexagon); |
127 | | - } |
128 | | - |
129 | 122 | public static void repaint() { |
130 | 123 | repaintIndex++; |
131 | 124 | patternTexturesToAdd.clear(); |
|
0 commit comments