-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
RenderUtils.java
733 lines (672 loc) · 27.1 KB
/
RenderUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/*
* Copyright © Wynntils 2022.
* This file is released under AGPLv3. See LICENSE for full license details.
*/
package com.wynntils.mc.render;
import com.mojang.blaze3d.pipeline.RenderTarget;
import com.mojang.blaze3d.platform.NativeImage;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.BufferUploader;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.Tesselator;
import com.mojang.blaze3d.vertex.VertexFormat;
import com.mojang.math.Matrix4f;
import com.wynntils.utils.objects.CustomColor;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage;
import java.util.List;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
public class RenderUtils {
// tooltip colors for item screenshot creation. somewhat hacky solution to get around transparency issues -
// these colors were chosen to best match how tooltips are displayed in-game
private static final CustomColor BACKGROUND = CustomColor.fromInt(0xFF100010);
private static final CustomColor BORDER_START = CustomColor.fromInt(0xFF25005B);
private static final CustomColor BORDER_END = CustomColor.fromInt(0xFF180033);
// number of possible segments for arc drawing
private static final float MAX_CIRCLE_STEPS = 16f;
// See https://github.com/MinecraftForge/MinecraftForge/issues/8083 as to why this uses TRIANGLE_STRIPS.
// TLDR: New OpenGL only supports TRIANGLES and Minecraft patched QUADS to be usable ATM, but LINES patch is broken
// and you can't use it.
// (This also means that using QUADS is probably not the best idea)
public static void drawLine(
PoseStack poseStack, CustomColor color, float x1, float y1, float x2, float y2, float z, float width) {
Matrix4f matrix = poseStack.last().pose();
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
RenderSystem.setShader(GameRenderer::getPositionColorShader);
BufferBuilder bufferBuilder = Tesselator.getInstance().getBuilder();
float halfWidth = width / 2;
bufferBuilder.begin(VertexFormat.Mode.TRIANGLE_STRIP, DefaultVertexFormat.POSITION_COLOR);
if (x1 == x2) {
if (y2 < y1) {
float tmp = y1;
y1 = y2;
y2 = tmp;
}
bufferBuilder
.vertex(matrix, x1 - halfWidth, y1, z)
.color(color.r, color.g, color.b, color.a)
.endVertex();
bufferBuilder
.vertex(matrix, x2 - halfWidth, y2, z)
.color(color.r, color.g, color.b, color.a)
.endVertex();
bufferBuilder
.vertex(matrix, x1 + halfWidth, y1, z)
.color(color.r, color.g, color.b, color.a)
.endVertex();
bufferBuilder
.vertex(matrix, x2 + halfWidth, y2, z)
.color(color.r, color.g, color.b, color.a)
.endVertex();
} else if (y1 == y2) {
if (x2 < x1) {
float tmp = x1;
x1 = x2;
x2 = tmp;
}
bufferBuilder
.vertex(matrix, x1, y1 - halfWidth, z)
.color(color.r, color.g, color.b, color.a)
.endVertex();
bufferBuilder
.vertex(matrix, x1, y1 + halfWidth, z)
.color(color.r, color.g, color.b, color.a)
.endVertex();
bufferBuilder
.vertex(matrix, x2, y2 - halfWidth, z)
.color(color.r, color.g, color.b, color.a)
.endVertex();
bufferBuilder
.vertex(matrix, x2, y2 + halfWidth, z)
.color(color.r, color.g, color.b, color.a)
.endVertex();
} else if ((x1 < x2 && y1 < y2) || (x2 < x1 && y2 < y1)) { // Top Left to Bottom Right line
if (x2 < x1) {
float tmp = x1;
x1 = x2;
x2 = tmp;
tmp = y1;
y1 = y2;
y2 = tmp;
}
bufferBuilder
.vertex(matrix, x1 + halfWidth, y1 - halfWidth, z)
.color(color.r, color.g, color.b, color.a)
.endVertex();
bufferBuilder
.vertex(matrix, x1 - halfWidth, y1 + halfWidth, z)
.color(color.r, color.g, color.b, color.a)
.endVertex();
bufferBuilder
.vertex(matrix, x2 + halfWidth, y2 - halfWidth, z)
.color(color.r, color.g, color.b, color.a)
.endVertex();
bufferBuilder
.vertex(matrix, x2 - halfWidth, y2 + halfWidth, z)
.color(color.r, color.g, color.b, color.a)
.endVertex();
} else { // Top Right to Bottom Left Line
if (x1 < x2) {
float tmp = x1;
x1 = x2;
x2 = tmp;
tmp = y1;
y1 = y2;
y2 = tmp;
}
bufferBuilder
.vertex(matrix, x1 + halfWidth, y1 + halfWidth, z)
.color(color.r, color.g, color.b, color.a)
.endVertex();
bufferBuilder
.vertex(matrix, x1 - halfWidth, y1 - halfWidth, z)
.color(color.r, color.g, color.b, color.a)
.endVertex();
bufferBuilder
.vertex(matrix, x2 + halfWidth, y2 + halfWidth, z)
.color(color.r, color.g, color.b, color.a)
.endVertex();
bufferBuilder
.vertex(matrix, x2 - halfWidth, y2 - halfWidth, z)
.color(color.r, color.g, color.b, color.a)
.endVertex();
}
bufferBuilder.end();
BufferUploader.end(bufferBuilder);
RenderSystem.disableBlend();
}
public static void drawRectBorders(
PoseStack poseStack, CustomColor color, float x1, float y1, float x2, float y2, float z, float lineWidth) {
drawLine(poseStack, color, x1, y1, x2, y1, z, lineWidth);
drawLine(poseStack, color, x2, y1, x2, y2, z, lineWidth);
drawLine(poseStack, color, x2, y2, x1, y2, z, lineWidth);
drawLine(poseStack, color, x1, y2, x1, y1, z, lineWidth);
}
public static void drawRect(CustomColor color, float x, float y, float z, float width, float height) {
drawRect(new PoseStack(), color, x, y, z, width, height);
}
public static void drawRect(
PoseStack poseStack, CustomColor color, float x, float y, float z, float width, float height) {
Matrix4f matrix = poseStack.last().pose();
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
RenderSystem.setShader(GameRenderer::getPositionColorShader);
BufferBuilder bufferBuilder = Tesselator.getInstance().getBuilder();
bufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR);
bufferBuilder
.vertex(matrix, x, y + height, z)
.color(color.r, color.g, color.b, color.a)
.endVertex();
bufferBuilder
.vertex(matrix, x + width, y + height, z)
.color(color.r, color.g, color.b, color.a)
.endVertex();
bufferBuilder
.vertex(matrix, x + width, y, z)
.color(color.r, color.g, color.b, color.a)
.endVertex();
bufferBuilder
.vertex(matrix, x, y, z)
.color(color.r, color.g, color.b, color.a)
.endVertex();
bufferBuilder.end();
BufferUploader.end(bufferBuilder);
RenderSystem.disableBlend();
}
public static void drawTexturedRect(
PoseStack poseStack,
ResourceLocation tex,
int x,
int y,
int width,
int height,
int textureWidth,
int textureHeight) {
drawTexturedRect(poseStack, tex, x, y, 0, width, height, 0, 0, width, height, textureWidth, textureHeight);
}
public static void drawTexturedRect(
PoseStack poseStack,
ResourceLocation tex,
int x,
int y,
int z,
int width,
int height,
int textureWidth,
int textureHeight) {
drawTexturedRect(poseStack, tex, x, y, z, width, height, 0, 0, width, height, textureWidth, textureHeight);
}
public static void drawTexturedRect(
PoseStack poseStack,
ResourceLocation tex,
int x,
int y,
int z,
int width,
int height,
int uOffset,
int vOffset,
int u,
int v,
int textureWidth,
int textureHeight) {
float uScale = 1f / textureWidth;
float vScale = 1f / textureHeight;
Matrix4f matrix = poseStack.last().pose();
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderTexture(0, tex);
BufferBuilder bufferBuilder = Tesselator.getInstance().getBuilder();
bufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX);
bufferBuilder
.vertex(matrix, x, y + height, z)
.uv(uOffset * uScale, (vOffset + v) * vScale)
.endVertex();
bufferBuilder
.vertex(matrix, x + width, y + height, z)
.uv((uOffset + u) * uScale, (vOffset + v) * vScale)
.endVertex();
bufferBuilder
.vertex(matrix, x + width, y, z)
.uv((uOffset + u) * uScale, vOffset * vScale)
.endVertex();
bufferBuilder
.vertex(matrix, x, y, z)
.uv(uOffset * uScale, vOffset * vScale)
.endVertex();
bufferBuilder.end();
BufferUploader.end(bufferBuilder);
}
public static void drawTexturedRectWithColor(
ResourceLocation tex,
CustomColor color,
int x,
int y,
int z,
int width,
int height,
int textureWidth,
int textureHeight) {
drawTexturedRectWithColor(
new PoseStack(), tex, color, x, y, z, width, height, 0, 0, width, height, textureWidth, textureHeight);
}
public static void drawTexturedRectWithColor(
PoseStack poseStack,
ResourceLocation tex,
CustomColor color,
int x,
int y,
int z,
int width,
int height,
int uOffset,
int vOffset,
int u,
int v,
int textureWidth,
int textureHeight) {
float uScale = 1f / textureWidth;
float vScale = 1f / textureHeight;
Matrix4f matrix = poseStack.last().pose();
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
RenderSystem.setShader(GameRenderer::getPositionTexColorShader);
RenderSystem.setShaderTexture(0, tex);
BufferBuilder bufferBuilder = Tesselator.getInstance().getBuilder();
bufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX_COLOR);
bufferBuilder
.vertex(matrix, x, y + height, z)
.uv(uOffset * uScale, (vOffset + v) * vScale)
.color(color.r, color.g, color.b, color.a)
.endVertex();
bufferBuilder
.vertex(matrix, x + width, y + height, z)
.uv((uOffset + u) * uScale, (vOffset + v) * vScale)
.color(color.r, color.g, color.b, color.a)
.endVertex();
bufferBuilder
.vertex(matrix, x + width, y, z)
.uv((uOffset + u) * uScale, vOffset * vScale)
.color(color.r, color.g, color.b, color.a)
.endVertex();
bufferBuilder
.vertex(matrix, x, y, z)
.uv(uOffset * uScale, vOffset * vScale)
.color(color.r, color.g, color.b, color.a)
.endVertex();
bufferBuilder.end();
BufferUploader.end(bufferBuilder);
RenderSystem.disableBlend();
}
public static void drawArc(CustomColor color, int x, int y, int z, float fill, int radius) {
drawArc(new PoseStack(), color, x, y, z, fill, radius);
}
public static void drawArc(PoseStack poseStack, CustomColor color, int x, int y, int z, float fill, int radius) {
// keeps arc from overlapping itself
int segments = (int) Math.min(fill * MAX_CIRCLE_STEPS, MAX_CIRCLE_STEPS - 1);
// each section of arc texture is 64 wide, ordered left to right
int uOffset = 64 * segments;
// render arc texture
drawTexturedRectWithColor(
poseStack,
Texture.ARC.resource(),
color,
x,
y,
z,
radius * 2,
radius * 2,
uOffset,
0,
64,
64,
Texture.ARC.width(),
Texture.ARC.height());
}
public static void drawTooltip(
PoseStack poseStack, List<Component> componentLines, Font font, boolean firstLineHasPlusHeight) {
List<ClientTooltipComponent> lines = componentToClientTooltipComponent(componentLines);
int tooltipWidth = getToolTipWidth(lines, font);
int tooltipHeight = getToolTipHeight(lines);
// background box
poseStack.pushPose();
int tooltipX = 4;
int tooltipY = 4;
int zLevel = 400;
Tesselator tesselator = Tesselator.getInstance();
BufferBuilder bufferBuilder = tesselator.getBuilder();
RenderSystem.setShader(GameRenderer::getPositionColorShader);
bufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR);
Matrix4f matrix4f = poseStack.last().pose();
fillGradient(
matrix4f,
bufferBuilder,
tooltipX - 3,
tooltipY - 4,
tooltipX + tooltipWidth + 3,
tooltipY - 3,
zLevel,
BACKGROUND,
BACKGROUND);
fillGradient(
matrix4f,
bufferBuilder,
tooltipX - 3,
tooltipY + tooltipHeight + 3,
tooltipX + tooltipWidth + 3,
tooltipY + tooltipHeight + 4,
zLevel,
BACKGROUND,
BACKGROUND);
fillGradient(
matrix4f,
bufferBuilder,
tooltipX - 3,
tooltipY - 3,
tooltipX + tooltipWidth + 3,
tooltipY + tooltipHeight + 3,
zLevel,
BACKGROUND,
BACKGROUND);
fillGradient(
matrix4f,
bufferBuilder,
tooltipX - 4,
tooltipY - 3,
tooltipX - 3,
tooltipY + tooltipHeight + 3,
zLevel,
BACKGROUND,
BACKGROUND);
fillGradient(
matrix4f,
bufferBuilder,
tooltipX + tooltipWidth + 3,
tooltipY - 3,
tooltipX + tooltipWidth + 4,
tooltipY + tooltipHeight + 3,
zLevel,
BACKGROUND,
BACKGROUND);
fillGradient(
matrix4f,
bufferBuilder,
tooltipX - 3,
tooltipY - 3 + 1,
tooltipX - 3 + 1,
tooltipY + tooltipHeight + 3 - 1,
zLevel,
BORDER_START,
BORDER_END);
fillGradient(
matrix4f,
bufferBuilder,
tooltipX + tooltipWidth + 2,
tooltipY - 3 + 1,
tooltipX + tooltipWidth + 3,
tooltipY + tooltipHeight + 3 - 1,
zLevel,
BORDER_START,
BORDER_END);
fillGradient(
matrix4f,
bufferBuilder,
tooltipX - 3,
tooltipY - 3,
tooltipX + tooltipWidth + 3,
tooltipY - 3 + 1,
zLevel,
BORDER_START,
BORDER_START);
fillGradient(
matrix4f,
bufferBuilder,
tooltipX - 3,
tooltipY + tooltipHeight + 2,
tooltipX + tooltipWidth + 3,
tooltipY + tooltipHeight + 3,
zLevel,
BORDER_END,
BORDER_END);
RenderSystem.enableDepthTest();
RenderSystem.disableTexture();
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
bufferBuilder.end();
BufferUploader.end(bufferBuilder);
RenderSystem.disableBlend();
RenderSystem.enableTexture();
// text
MultiBufferSource.BufferSource bufferSource =
MultiBufferSource.immediate(Tesselator.getInstance().getBuilder());
poseStack.translate(0.0, 0.0, 400.0);
int s = tooltipY;
boolean first = firstLineHasPlusHeight;
for (ClientTooltipComponent line : lines) {
line.renderText(font, tooltipX, s, matrix4f, bufferSource);
s += line.getHeight() + (first ? 2 : 0);
first = false;
}
bufferSource.endBatch();
poseStack.popPose();
}
public static void drawTooltipAt(
PoseStack poseStack,
double renderX,
double renderY,
double renderZ,
List<Component> componentLines,
Font font,
boolean firstLineHasPlusHeight) {
poseStack.pushPose();
poseStack.translate(renderX, renderY, renderZ);
drawTooltip(poseStack, componentLines, font, firstLineHasPlusHeight);
poseStack.popPose();
}
public static int getToolTipWidth(List<ClientTooltipComponent> lines, Font font) {
return lines.stream()
.map(clientTooltipComponent -> clientTooltipComponent.getWidth(font))
.max(Integer::compareTo)
.orElse(0);
}
public static int getToolTipHeight(List<ClientTooltipComponent> lines) {
return (lines.size() == 1 ? -2 : 0)
+ lines.stream()
.map(ClientTooltipComponent::getHeight)
.mapToInt(Integer::intValue)
.sum();
}
public static List<ClientTooltipComponent> componentToClientTooltipComponent(List<Component> components) {
return components.stream()
.map(Component::getVisualOrderText)
.map(ClientTooltipComponent::create)
.toList();
}
/** drawProgressBar
* Draws a progress bar (textureY1 and textureY2 now specify both textures with background being on top of the bar)
*
* @param poseStack poseStack to use
* @param texture the texture to use
* @param x1 left x on screen
* @param y1 top y on screen
* @param x2 right x on screen
* @param y2 bottom right on screen
* @param textureX1 texture left x for the part
* @param textureY1 texture top y for the part (top of background)
* @param textureX2 texture right x for the part
* @param textureY2 texture bottom y for the part (bottom of bar)
* @param progress progress of the bar, 0.0f to 1.0f is left to right and 0.0f to -1.0f is right to left
*/
public static void drawProgressBar(
PoseStack poseStack,
Texture texture,
float x1,
float y1,
float x2,
float y2,
int textureX1,
int textureY1,
int textureX2,
int textureY2,
float progress) {
int half = (textureY1 + textureY2) / 2 + (textureY2 - textureY1) % 2;
drawProgressBarBackground(poseStack, texture, x1, y1, x2, y2, textureX1, textureY1, textureX2, half);
drawProgressBarForeground(
poseStack,
texture,
x1,
y1,
x2,
y2,
textureX1,
half,
textureX2,
textureY2 + (textureY2 - textureY1) % 2,
progress);
}
private static void drawProgressBarForeground(
PoseStack poseStack,
Texture texture,
float x1,
float y1,
float x2,
float y2,
int textureX1,
int textureY1,
int textureX2,
int textureY2,
float progress) {
if (progress == 0f) {
return;
}
Matrix4f matrix = poseStack.last().pose();
RenderSystem.enableTexture();
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderTexture(0, texture.resource());
BufferBuilder bufferBuilder = Tesselator.getInstance().getBuilder();
float xMin = Math.min(x1, x2),
xMax = Math.max(x1, x2),
yMin = Math.min(y1, y2),
yMax = Math.max(y1, y2),
txMin = (float) Math.min(textureX1, textureX2) / texture.width(),
txMax = (float) Math.max(textureX1, textureX2) / texture.width(),
tyMin = (float) Math.min(textureY1, textureY2) / texture.height(),
tyMax = (float) Math.max(textureY1, textureY2) / texture.height();
if (progress < 1.0f && progress > -1.0f) {
if (progress < 0.0f) {
xMin += (1.0f + progress) * (xMax - xMin);
txMin += (1.0f + progress) * (txMax - txMin);
} else {
xMax -= (1.0f - progress) * (xMax - xMin);
txMax -= (1.0f - progress) * (txMax - txMin);
}
}
bufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX);
bufferBuilder.vertex(matrix, xMin, yMin, 0).uv(txMin, tyMin).endVertex();
bufferBuilder.vertex(matrix, xMin, yMax, 0).uv(txMin, tyMax).endVertex();
bufferBuilder.vertex(matrix, xMax, yMax, 0).uv(txMax, tyMax).endVertex();
bufferBuilder.vertex(matrix, xMax, yMin, 0).uv(txMax, tyMin).endVertex();
bufferBuilder.end();
BufferUploader.end(bufferBuilder);
}
private static void drawProgressBarBackground(
PoseStack poseStack,
Texture texture,
float x1,
float y1,
float x2,
float y2,
int textureX1,
int textureY1,
int textureX2,
int textureY2) {
Matrix4f matrix = poseStack.last().pose();
RenderSystem.enableTexture();
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderTexture(0, texture.resource());
BufferBuilder bufferBuilder = Tesselator.getInstance().getBuilder();
float xMin = Math.min(x1, x2),
xMax = Math.max(x1, x2),
yMin = Math.min(y1, y2),
yMax = Math.max(y1, y2),
txMin = (float) Math.min(textureX1, textureX2) / texture.width(),
txMax = (float) Math.max(textureX1, textureX2) / texture.height(),
tyMin = (float) Math.min(textureY1, textureY2) / texture.height(),
tyMax = (float) Math.max(textureY1, textureY2) / texture.height();
bufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX);
bufferBuilder.vertex(matrix, xMin, yMin, 0).uv(txMin, tyMin).endVertex();
bufferBuilder.vertex(matrix, xMin, yMax, 0).uv(txMin, tyMax).endVertex();
bufferBuilder.vertex(matrix, xMax, yMax, 0).uv(txMax, tyMax).endVertex();
bufferBuilder.vertex(matrix, xMax, yMin, 0).uv(txMax, tyMin).endVertex();
bufferBuilder.end();
BufferUploader.end(bufferBuilder);
}
public static void fillGradient(
Matrix4f matrix,
BufferBuilder builder,
int x1,
int y1,
int x2,
int y2,
int blitOffset,
CustomColor colorA,
CustomColor colorB) {
builder.vertex(matrix, x2, y1, blitOffset)
.color(colorA.r, colorA.g, colorA.b, colorA.a)
.endVertex();
builder.vertex(matrix, x1, y1, blitOffset)
.color(colorA.r, colorA.g, colorA.b, colorA.a)
.endVertex();
builder.vertex(matrix, x1, y2, blitOffset)
.color(colorB.r, colorB.g, colorB.b, colorB.a)
.endVertex();
builder.vertex(matrix, x2, y2, blitOffset)
.color(colorB.r, colorB.g, colorB.b, colorB.a)
.endVertex();
}
public static void copyImageToClipboard(BufferedImage bi) {
class ClipboardImage implements Transferable {
private final Image image;
public ClipboardImage(Image image) {
this.image = image;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] {DataFlavor.imageFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return DataFlavor.imageFlavor.equals(flavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
if (!DataFlavor.imageFlavor.equals(flavor)) throw new UnsupportedFlavorException(flavor);
return this.image;
}
}
ClipboardImage ci = new ClipboardImage(bi);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ci, null);
}
public static BufferedImage createScreenshot(RenderTarget fb) {
BufferedImage bufferedimage = new BufferedImage(fb.width, fb.height, BufferedImage.TYPE_INT_ARGB);
try (NativeImage image = new NativeImage(fb.width, fb.height, false)) {
RenderSystem.bindTexture(fb.getColorTextureId());
image.downloadTexture(0, false);
image.flipY();
int[] pixelValues = image.makePixelArray();
bufferedimage.setRGB(0, 0, fb.width, fb.height, pixelValues, 0, fb.width);
}
return bufferedimage;
}
}