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
|
package de.hysky.skyblocker.utils.render;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.logging.LogUtils;
import de.hysky.skyblocker.SkyblockerMod;
import de.hysky.skyblocker.mixin.accessor.BeaconBlockEntityRendererInvoker;
import de.hysky.skyblocker.utils.render.culling.OcclusionCulling;
import de.hysky.skyblocker.utils.render.title.Title;
import de.hysky.skyblocker.utils.render.title.TitleContainer;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents;
import net.fabricmc.fabric.api.event.Event;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.render.*;
import net.minecraft.client.render.VertexFormat.DrawMode;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.OrderedText;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import org.joml.Matrix3f;
import org.joml.Matrix4f;
import org.lwjgl.opengl.GL11;
import org.slf4j.Logger;
public class RenderHelper {
private static final Logger LOGGER = LogUtils.getLogger();
private static final Identifier TRANSLUCENT_DRAW = new Identifier(SkyblockerMod.NAMESPACE, "translucent_draw");
private static final MethodHandle SCHEDULE_DEFERRED_RENDER_TASK = getDeferredRenderTaskHandle();
private static final Vec3d ONE = new Vec3d(1, 1, 1);
private static final int MAX_OVERWORLD_BUILD_HEIGHT = 319;
private static final MinecraftClient client = MinecraftClient.getInstance();
public static void init() {
WorldRenderEvents.AFTER_TRANSLUCENT.addPhaseOrdering(Event.DEFAULT_PHASE, TRANSLUCENT_DRAW);
WorldRenderEvents.AFTER_TRANSLUCENT.register(TRANSLUCENT_DRAW, RenderHelper::drawTranslucents);
}
public static void renderFilledThroughWallsWithBeaconBeam(WorldRenderContext context, BlockPos pos, float[] colorComponents, float alpha) {
renderFilledThroughWalls(context, pos, colorComponents, alpha);
renderBeaconBeam(context, pos, colorComponents);
}
public static void renderFilledThroughWalls(WorldRenderContext context, BlockPos pos, float[] colorComponents, float alpha) {
if (FrustumUtils.isVisible(pos.getX(), pos.getY(), pos.getZ(), pos.getX() + 1, pos.getY() + 1, pos.getZ() + 1)) {
renderFilled(context, Vec3d.of(pos), ONE, colorComponents, alpha, true);
}
}
public static void renderFilledIfVisible(WorldRenderContext context, BlockPos pos, float[] colorComponents, float alpha) {
if (OcclusionCulling.isVisible(pos.getX(), pos.getY(), pos.getZ(), pos.getX() + 1, pos.getY() + 1, pos.getZ() + 1)) {
renderFilled(context, Vec3d.of(pos), ONE, colorComponents, alpha, false);
}
}
private static void renderFilled(WorldRenderContext context, Vec3d pos, Vec3d dimensions, float[] colorComponents, float alpha, boolean throughWalls) {
MatrixStack matrices = context.matrixStack();
Vec3d camera = context.camera().getPos();
matrices.push();
matrices.translate(-camera.x, -camera.y, -camera.z);
VertexConsumerProvider consumers = context.consumers();
VertexConsumer buffer = consumers.getBuffer(throughWalls ? SkyblockerRenderLayers.FILLED_THROUGH_WALLS : SkyblockerRenderLayers.FILLED);
WorldRenderer.renderFilledBox(matrices, buffer, pos.x, pos.y, pos.z, pos.x + dimensions.x, pos.y + dimensions.y, pos.z + dimensions.z, colorComponents[0], colorComponents[1], colorComponents[2], alpha);
matrices.pop();
}
private static void renderBeaconBeam(WorldRenderContext context, BlockPos pos, float[] colorComponents) {
if (FrustumUtils.isVisible(pos.getX(), pos.getY(), pos.getZ(), pos.getX() + 1, MAX_OVERWORLD_BUILD_HEIGHT, pos.getZ() + 1)) {
MatrixStack matrices = context.matrixStack();
Vec3d camera = context.camera().getPos();
matrices.push();
matrices.translate(pos.getX() - camera.getX(), pos.getY() - camera.getY(), pos.getZ() - camera.getZ());
BeaconBlockEntityRendererInvoker.renderBeam(matrices, context.consumers(), context.tickDelta(), context.world().getTime(), 0, MAX_OVERWORLD_BUILD_HEIGHT, colorComponents);
matrices.pop();
}
}
/**
* Renders the outline of a box with the specified color components and line width.
* This does not use renderer since renderer draws outline using debug lines with a fixed width.
*/
public static void renderOutline(WorldRenderContext context, Box box, float[] colorComponents, float lineWidth, boolean throughWalls) {
if (FrustumUtils.isVisible(box)) {
MatrixStack matrices = context.matrixStack();
Vec3d camera = context.camera().getPos();
Tessellator tessellator = RenderSystem.renderThreadTesselator();
BufferBuilder buffer = tessellator.getBuffer();
RenderSystem.setShader(GameRenderer::getRenderTypeLinesProgram);
RenderSystem.setShaderColor(1f, 1f, 1f, 1f);
RenderSystem.lineWidth(lineWidth);
RenderSystem.disableCull();
RenderSystem.enableDepthTest();
RenderSystem.depthFunc(throughWalls ? GL11.GL_ALWAYS : GL11.GL_LEQUAL);
matrices.push();
matrices.translate(-camera.getX(), -camera.getY(), -camera.getZ());
buffer.begin(DrawMode.LINES, VertexFormats.LINES);
WorldRenderer.drawBox(matrices, buffer, box, colorComponents[0], colorComponents[1], colorComponents[2], 1f);
tessellator.draw();
matrices.pop();
RenderSystem.lineWidth(1f);
RenderSystem.enableCull();
RenderSystem.disableDepthTest();
RenderSystem.depthFunc(GL11.GL_LEQUAL);
}
}
/**
* Draws lines from point to point.<br><br>
* <p>
* Tip: To draw lines from the center of a block, offset the X, Y and Z each by 0.5
* <p>
* Note: This is super messed up when drawing long lines. Tried different normals and {@link DrawMode#LINES} but nothing worked.
*
* @param context The WorldRenderContext which supplies the matrices and tick delta
* @param points The points from which to draw lines between
* @param colorComponents An array of R, G and B color components
* @param alpha The alpha of the lines
* @param lineWidth The width of the lines
*/
public static void renderLinesFromPoints(WorldRenderContext context, Vec3d[] points, float[] colorComponents, float alpha, float lineWidth) {
Vec3d camera = context.camera().getPos();
MatrixStack matrices = context.matrixStack();
matrices.push();
matrices.translate(-camera.x, -camera.y, -camera.z);
Tessellator tessellator = RenderSystem.renderThreadTesselator();
BufferBuilder buffer = tessellator.getBuffer();
Matrix4f positionMatrix = matrices.peek().getPositionMatrix();
Matrix3f normalMatrix = matrices.peek().getNormalMatrix();
GL11.glEnable(GL11.GL_LINE_SMOOTH);
GL11.glHint(GL11.GL_LINE_SMOOTH_HINT, GL11.GL_NICEST);
RenderSystem.setShader(GameRenderer::getRenderTypeLinesProgram);
RenderSystem.setShaderColor(1f, 1f, 1f, 1f);
RenderSystem.lineWidth(lineWidth);
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
RenderSystem.disableCull();
RenderSystem.enableDepthTest();
buffer.begin(DrawMode.LINE_STRIP, VertexFormats.LINES);
for (int i = 0; i < points.length; i++) {
Vec3d normalVec = points[(i + 1) % points.length].subtract(points[i]).normalize();
buffer
.vertex(positionMatrix, (float) points[i].getX(), (float) points[i].getY(), (float) points[i].getZ())
.color(colorComponents[0], colorComponents[1], colorComponents[2], alpha)
.normal(normalMatrix, (float) normalVec.x, (float) normalVec.y, (float) normalVec.z)
.next();
}
tessellator.draw();
matrices.pop();
GL11.glDisable(GL11.GL_LINE_SMOOTH);
RenderSystem.lineWidth(1f);
RenderSystem.enableCull();
}
public static void renderQuad(WorldRenderContext context, Vec3d[] points, float[] colorComponents, float alpha, boolean throughWalls) {
Vec3d camera = context.camera().getPos();
MatrixStack matrices = context.matrixStack();
matrices.push();
matrices.translate(-camera.x, -camera.y, -camera.z);
Tessellator tessellator = RenderSystem.renderThreadTesselator();
BufferBuilder buffer = tessellator.getBuffer();
Matrix4f positionMatrix = matrices.peek().getPositionMatrix();
RenderSystem.setShader(GameRenderer::getPositionColorProgram);
RenderSystem.setShaderColor(1f, 1f, 1f, 1f);
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
RenderSystem.disableCull();
RenderSystem.depthFunc(throughWalls ? GL11.GL_ALWAYS : GL11.GL_LEQUAL);
buffer.begin(DrawMode.QUADS, VertexFormats.POSITION_COLOR);
for (int i = 0; i < 4; i++) {
buffer.vertex(positionMatrix, (float) points[i].getX(), (float) points[i].getY(), (float) points[i].getZ()).color(colorComponents[0], colorComponents[1], colorComponents[2], alpha).next();
}
tessellator.draw();
RenderSystem.enableCull();
RenderSystem.depthFunc(GL11.GL_LEQUAL);
matrices.pop();
}
public static void renderText(WorldRenderContext context, Text text, Vec3d pos, boolean throughWalls) {
renderText(context, text, pos, 1, throughWalls);
}
public static void renderText(WorldRenderContext context, Text text, Vec3d pos, float scale, boolean throughWalls) {
renderText(context, text, pos, scale, 0, throughWalls);
}
public static void renderText(WorldRenderContext context, Text text, Vec3d pos, float scale, float yOffset, boolean throughWalls) {
renderText(context, text.asOrderedText(), pos, scale, yOffset, throughWalls);
}
/**
* Renders text in the world space.
*
* @param throughWalls whether the text should be able to be seen through walls or not.
*/
public static void renderText(WorldRenderContext context, OrderedText text, Vec3d pos, float scale, float yOffset, boolean throughWalls) {
MatrixStack matrices = context.matrixStack();
Vec3d camera = context.camera().getPos();
TextRenderer textRenderer = client.textRenderer;
scale *= 0.025f;
matrices.push();
matrices.translate(pos.getX() - camera.getX(), pos.getY() - camera.getY(), pos.getZ() - camera.getZ());
matrices.peek().getPositionMatrix().mul(RenderSystem.getModelViewMatrix());
matrices.multiply(context.camera().getRotation());
matrices.scale(-scale, -scale, scale);
Matrix4f positionMatrix = matrices.peek().getPositionMatrix();
float xOffset = -textRenderer.getWidth(text) / 2f;
Tessellator tessellator = RenderSystem.renderThreadTesselator();
BufferBuilder buffer = tessellator.getBuffer();
VertexConsumerProvider.Immediate consumers = VertexConsumerProvider.immediate(buffer);
RenderSystem.depthFunc(throughWalls ? GL11.GL_ALWAYS : GL11.GL_LEQUAL);
textRenderer.draw(text, xOffset, yOffset, 0xFFFFFFFF, false, positionMatrix, consumers, TextRenderer.TextLayerType.SEE_THROUGH, 0, LightmapTextureManager.MAX_LIGHT_COORDINATE);
consumers.draw();
RenderSystem.depthFunc(GL11.GL_LEQUAL);
matrices.pop();
}
/**
* This is called after all {@link WorldRenderEvents#AFTER_TRANSLUCENT} listeners have been called so that we can draw all remaining render layers.
*/
private static void drawTranslucents(WorldRenderContext context) {
//Draw all render layers that haven't been drawn yet - drawing a specific layer does nothing and idk why
((VertexConsumerProvider.Immediate) context.consumers()).draw();
}
public static void runOnRenderThread(Runnable runnable) {
if (RenderSystem.isOnRenderThread()) {
runnable.run();
} else if (SCHEDULE_DEFERRED_RENDER_TASK != null) { //Sodium
try {
SCHEDULE_DEFERRED_RENDER_TASK.invokeExact(runnable);
} catch (Throwable t) {
LOGGER.error("[Skyblocker] Failed to schedule a render task!", t);
}
} else { //Vanilla
RenderSystem.recordRenderCall(runnable::run);
}
}
/**
* Adds the title to {@link TitleContainer} and {@link #playNotificationSound() plays the notification sound} if the title is not in the {@link TitleContainer} already.
* No checking needs to be done on whether the title is in the {@link TitleContainer} already by the caller.
*
* @param title the title
*/
public static void displayInTitleContainerAndPlaySound(Title title) {
if (TitleContainer.addTitle(title)) {
playNotificationSound();
}
}
/**
* Adds the title to {@link TitleContainer} for a set number of ticks and {@link #playNotificationSound() plays the notification sound} if the title is not in the {@link TitleContainer} already.
* No checking needs to be done on whether the title is in the {@link TitleContainer} already by the caller.
*
* @param title the title
* @param ticks the number of ticks the title will remain
*/
public static void displayInTitleContainerAndPlaySound(Title title, int ticks) {
if (TitleContainer.addTitle(title, ticks)) {
playNotificationSound();
}
}
private static void playNotificationSound() {
if (client.player != null) {
client.player.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 100f, 0.1f);
}
}
public static boolean pointIsInArea(double x, double y, double x1, double y1, double x2, double y2) {
return x >= x1 && x <= x2 && y >= y1 && y <= y2;
}
// TODO Get rid of reflection once the new Sodium is released
private static MethodHandle getDeferredRenderTaskHandle() {
try {
Class<?> deferredTaskClass = Class.forName("me.jellysquid.mods.sodium.client.render.util.DeferredRenderTask");
MethodHandles.Lookup lookup = MethodHandles.publicLookup();
MethodType mt = MethodType.methodType(void.class, Runnable.class);
return lookup.findStatic(deferredTaskClass, "schedule", mt);
} catch (Throwable ignored) {}
return null;
}
}
|