Inspired by 2020's April Fools' 20w14infinite Snapshot, this mod brings endless randomly generated dimensions into Minecraft.
at master 886 lines 38 kB view raw
1package net.lerariemann.infinity.options; 2 3//FIXME 1.21.8 - rendering broke 4//? if <1.21.2 { 5/*import com.mojang.blaze3d.platform.GlStateManager; 6import com.mojang.blaze3d.systems.RenderSystem; 7import com.mojang.blaze3d.vertex.*; 8import com.mojang.math.Axis; 9import net.lerariemann.infinity.util.VersionMethods; 10import net.minecraft.client.Camera; 11import net.minecraft.client.Minecraft; 12import net.minecraft.client.multiplayer.ClientLevel; 13import net.minecraft.client.renderer.DimensionSpecialEffects; 14import net.minecraft.client.renderer.FogRenderer; 15import net.minecraft.client.renderer.GameRenderer; 16import net.minecraft.resources.ResourceLocation; 17import net.minecraft.util.Mth; 18import net.minecraft.world.level.material.FogType; 19import net.minecraft.world.phys.Vec3; 20import org.joml.Matrix4f; 21import org.joml.Vector3f; 22 23import java.awt.Color; 24 25public record SkyRenderer(InfinityOptions options, Minecraft client, ClientLevel world, 26 PoseStack matrices, Tesselator tesselator, float tickDelta, Matrix4f projectionMatrix, 27 VertexBuffer lightSkyBuffer, VertexBuffer starsBuffer) { 28 29 public SkyRenderer(InfinityOptions options, Minecraft client, ClientLevel world, 30 PoseStack matrices, float tickDelta, Matrix4f projectionMatrix, 31 VertexBuffer lightSkyBuffer, VertexBuffer starsBuffer) { 32 this(options, client, world, matrices, Tesselator.getInstance(), tickDelta, projectionMatrix, lightSkyBuffer, starsBuffer); 33 } 34 35 public static boolean testCameraCancels(Camera camera) { 36 FogType cameraSubmersionType = camera.getFluidInCamera(); 37 return (cameraSubmersionType == FogType.POWDER_SNOW 38 || cameraSubmersionType == FogType.LAVA); 39 } 40 41 public void render(Runnable fogCallback) { 42 if (testAndRenderNonOverworldySkies()) return; 43 setupOverworldySky(); 44 renderAllCelestialBodies(fogCallback); 45 finish(); 46 } 47 48 public boolean testAndRenderNonOverworldySkies() { 49 if (client.level!=null && client.level.effects().skyType() == DimensionSpecialEffects.SkyType.END) { 50 renderSkybox(VersionMethods.id("textures/environment/end_sky.png"), 16.0f, 40, 255); 51 return true; 52 } 53 if (options.endSkyLike()) { 54 handleSkyBackground(); 55 return true; 56 } 57 return client.level != null && client.level.effects().skyType() != DimensionSpecialEffects.SkyType.NORMAL; 58 } 59 60 public void setupOverworldySky() { 61 FogRenderer.levelFogColor(); 62 RenderSystem.depthMask(false); 63 handleSkyBackground(); 64 handleFog(); 65 matrices.pushPose(); 66 } 67 public void handleSkyBackground() { 68 String skyType = options.getSkyType(); 69 if (skyType.equals("rainbow")) { 70 renderRainbowBackground(); 71 } 72 else { 73 Vec3 vec3d = this.world.getSkyColor(client.gameRenderer.getMainCamera().getPosition(), tickDelta); 74 renderSingleColorBackground((float)vec3d.x, (float)vec3d.y, (float)vec3d.z, 1.0f); 75 } 76 } 77 public void renderRainbowBackground() { 78 float main = world.getTimeOfDay(tickDelta) * 2; 79 int color = Color.getHSBColor(main - (int)main, 1.0f, 1.0f).getRGB(); 80 float f = Mth.clamp(Mth.cos(world.getTimeOfDay(tickDelta) * ((float)Math.PI * 2)) * 2.0f + 0.5f, 0.0f, 1.0f); 81 renderSingleColorBackground(f * (float)(color >> 16 & 0xFF) / 255.0f, f * (float)(color >> 8 & 0xFF) / 255.0f, f * (float)(color & 0xFF) / 255.0f, 1.0f); 82 } 83 public void renderSingleColorBackground(float f, float g, float h, float a) { 84 RenderSystem.setShaderColor(f, g, h, a); 85 lightSkyBuffer.bind(); 86 lightSkyBuffer.drawWithShader(matrices.last().pose(), projectionMatrix, RenderSystem.getShader()); 87 VertexBuffer.unbind(); 88 } 89 90 public void handleFog() { 91 RenderSystem.enableBlend(); 92 float[] fs = this.world.effects().getSunriseColor(this.world.getTimeOfDay(tickDelta), tickDelta); 93 if (fs != null && options().hasDawn()) handleSunriseFog(fs); 94 RenderSystem.blendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO); 95 } 96 public void handleSunriseFog(float[] fs) { 97 RenderSystem.setShader(GameRenderer::getPositionColorShader); 98 RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f); 99 100 matrices.pushPose(); 101 matrices.mulPose(Axis.XP.rotationDegrees(90.0f)); 102 float i = Mth.sin(this.world.getSunAngle(tickDelta)) < 0.0f ? 180.0f : 0.0f; 103 matrices.mulPose(Axis.ZP.rotationDegrees(i - options.getSolarTilt())); 104 105 Matrix4f matrix4f = matrices.last().pose(); 106 FriendlyBuilder bufferBuilder = new FriendlyBuilder(tesselator, VertexFormat.Mode.TRIANGLE_FAN, DefaultVertexFormat.POSITION_COLOR); 107 bufferBuilder.addVertex(matrix4f, 0.0f, 100.0f, 0.0f).setColor(fs[0], fs[1], fs[2], fs[3]).endVertex(); 108 for (int n = 0; n <= 16; ++n) { 109 float o = (float)n * ((float)Math.PI * 2) / 16.0f; 110 float p = Mth.sin(o); 111 float q = Mth.cos(o); 112 bufferBuilder.addVertex(matrix4f, p * 120.0f, q * 120.0f, -q * 40.0f * fs[3]).setColor(fs[0], fs[1], fs[2], 0.0f).endVertex(); 113 } 114 BufferUploader.drawWithShader(bufferBuilder.buildOrThrow()); 115 matrices.popPose(); 116 } 117 118 public record FriendlyVertexConsumer(VertexConsumer vc) { 119 FriendlyVertexConsumer setColor(float r, float g, float b, float a) { 120 //? if >1.21 { 121 vc.setColor(r, g, b, a); 122 //?} else { 123 /^vc.color(r, g, b, a); 124 ^///?} 125 return this; 126 } 127 128 FriendlyVertexConsumer setUv(float u, float v) { 129 //? if >1.21 { 130 vc.setUv(u, v); 131 //?} else { 132 /^vc.uv(u, v); 133 ^///?} 134 return this; 135 } 136 137 void endVertex() { 138 //? if <1.21 { 139 /^vc.endVertex(); 140 ^///?} 141 } 142 } 143 144 public static class FriendlyBuilder { 145 BufferBuilder bufferBuilder; 146 147 FriendlyBuilder(Tesselator tesselator, VertexFormat.Mode mode, VertexFormat format) { 148 //? if >1.21 { 149 bufferBuilder = tesselator.begin(mode, format); 150 //?} else { 151 /^bufferBuilder = tesselator.getBuilder(); 152 bufferBuilder.begin(mode, format); 153 ^///?} 154 } 155 156 FriendlyVertexConsumer addVertex(Matrix4f m4f, float x, float y, float z) { 157 return new FriendlyVertexConsumer( 158 //? if >1.21 { 159 bufferBuilder.addVertex(m4f, x, y, z) 160 //?} else { 161 /^bufferBuilder.vertex(m4f, x, y, z) 162 ^///?} 163 ); 164 } 165 166 //? if >1.21 { 167 MeshData buildOrThrow() { 168 return bufferBuilder.buildOrThrow(); 169 } 170 //?} else { 171 /^BufferBuilder.RenderedBuffer buildOrThrow() { 172 return bufferBuilder.end(); 173 } 174 ^///?} 175 } 176 177 public void renderAllCelestialBodies(Runnable fogCallback) { 178 float rain_alpha = 1.0f - this.world.getRainLevel(tickDelta); 179 RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, rain_alpha); 180 181 renderSun(); 182 for (int i = 0; i < options.getNumMoons(); i++) { 183 renderMoon(i); 184 } 185 renderStars(fogCallback, rain_alpha); 186 } 187 188 public void finish() { 189 RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f); 190 RenderSystem.disableBlend(); 191 RenderSystem.defaultBlendFunc(); 192 matrices.popPose(); 193 RenderSystem.depthMask(true); 194 } 195 196 public void rotate_with_velocity(float v, float offset) { 197 matrices.mulPose(Axis.XP.rotationDegrees((world.getTimeOfDay(tickDelta) + offset) * 360.0f * v)); 198 } 199 200 public void renderSun() { 201 renderSingleBody( 202 options.getSolarSize(), 203 options.getSolarTilt(), 204 0, 205 1, 206 0, 207 options.getSolarTint(), 208 options.getSolarTexture(), 209 true); 210 } 211 public void renderMoon(int i) { 212 renderSingleBody( 213 options.getLunarSize(i), 214 options.getLunarTiltY(i), 215 options.getLunarTiltZ(i), 216 options.getLunarVelocity(i), 217 options.getLunarOffset(i), 218 options.getLunarTint(i), 219 options.getLunarTexture(i), 220 false); 221 } 222 223 public void renderSingleBody(float size, float tilt_y, float tilt_z, float velocity, float offset, Vector3f tint, ResourceLocation texture, 224 boolean sun) { 225 matrices.mulPose(Axis.YP.rotationDegrees(tilt_y)); 226 matrices.mulPose(Axis.ZP.rotationDegrees(tilt_z)); 227 rotate_with_velocity(velocity, offset); 228 if (sun) renderSun(matrices.last().pose(), texture, size, 100.0f, tint); 229 else renderMoon(matrices.last().pose(), texture, size, -100.0f, tint); 230 rotate_with_velocity(-1 * velocity, offset); 231 matrices.mulPose(Axis.ZP.rotationDegrees(-tilt_z)); 232 matrices.mulPose(Axis.YP.rotationDegrees(-tilt_y)); 233 } 234 public void renderSun(Matrix4f matrix4f2, ResourceLocation texture, float k, float y, Vector3f tint) { 235 RenderSystem.setShader(GameRenderer::getPositionTexShader); 236 RenderSystem.setShaderTexture(0, texture); 237 RenderSystem.setShaderColor(tint.x, tint.y, tint.z, 1.0f); 238 FriendlyBuilder bufferBuilder = new FriendlyBuilder(tesselator, VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX); 239 bufferBuilder.addVertex(matrix4f2, -k, y, -k).setUv(0.0f, 0.0f).endVertex(); 240 bufferBuilder.addVertex(matrix4f2, k, y, -k).setUv(1.0f, 0.0f).endVertex(); 241 bufferBuilder.addVertex(matrix4f2, k, y, k).setUv(1.0f, 1.0f).endVertex(); 242 bufferBuilder.addVertex(matrix4f2, -k, y, k).setUv(0.0f, 1.0f).endVertex(); 243 BufferUploader.drawWithShader(bufferBuilder.buildOrThrow()); 244 } 245 public void renderMoon(Matrix4f matrix4f2, ResourceLocation texture, float k, float y, Vector3f tint) { 246 float t, q, p, o; 247 if (isMoonNormal(texture)) { 248 int moon_phase = world.getMoonPhase(); 249 int s = moon_phase % 4; 250 int m = moon_phase / 4 % 2; 251 t = (float)(s) / 4.0f; 252 o = (float)(m) / 2.0f; 253 p = (float)(s + 1) / 4.0f; 254 q = (float)(m + 1) / 2.0f; 255 } 256 else { 257 t = q = 1.0f; 258 p = o = 0.0f; 259 } 260 renderMoon(matrix4f2, texture, k, y, tint, t, q, p, o); 261 } 262 boolean isMoonNormal(ResourceLocation texture) { 263 return texture.getPath().equals("textures/environment/moon_phases.png"); 264 } 265 public void renderMoon(Matrix4f matrix4f2, ResourceLocation texture, float k, float y, Vector3f tint, float t, float q, float p, float o) { 266 RenderSystem.setShader(GameRenderer::getPositionTexShader); 267 RenderSystem.setShaderTexture(0, texture); 268 RenderSystem.setShaderColor(tint.x, tint.y, tint.z, 1.0f); 269 FriendlyBuilder bufferBuilder = new FriendlyBuilder(tesselator, VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX); 270 bufferBuilder.addVertex(matrix4f2, -k, y, k).setUv(p, q).endVertex(); 271 bufferBuilder.addVertex(matrix4f2, k, y, k).setUv(t, q).endVertex(); 272 bufferBuilder.addVertex(matrix4f2, k, y, -k).setUv(t, o).endVertex(); 273 bufferBuilder.addVertex(matrix4f2, -k, y, -k).setUv(p, o).endVertex(); 274 BufferUploader.drawWithShader(bufferBuilder.buildOrThrow()); 275 } 276 277 public void renderStars(Runnable fogCallback, float rain_alpha) { 278 renderStars(options.getStellarTiltY(), 279 options.getStellarTiltZ(), 280 options.getStellarVelocity(), 281 0, 282 fogCallback, rain_alpha); 283 } 284 public void renderStars(float tilt_y, float tilt_z, float velocity, float offset, 285 Runnable fogCallback, float rain_alpha) { 286 matrices.mulPose(Axis.YP.rotationDegrees(tilt_y)); 287 matrices.mulPose(Axis.ZP.rotationDegrees(tilt_z)); 288 rotate_with_velocity(velocity, offset); 289 renderStars(matrices.last().pose(), projectionMatrix, fogCallback, rain_alpha); 290 rotate_with_velocity(-1 * velocity, offset); 291 matrices.mulPose(Axis.ZP.rotationDegrees(-tilt_z)); 292 matrices.mulPose(Axis.YP.rotationDegrees(-tilt_y)); 293 } 294 public void renderStars(Matrix4f matrix4f2, Matrix4f projectionMatrix, Runnable fogCallback, float rain_alpha) { 295 float u = getStarBrightness(tickDelta) * rain_alpha; 296 Vector3f color = options.getStellarColor(); 297 if (u > 0.0f) { 298 RenderSystem.setShaderColor(u*color.x, u*color.y, u*color.z, u); 299 FogRenderer.setupNoFog(); 300 starsBuffer.bind(); 301 starsBuffer.drawWithShader(matrix4f2, projectionMatrix, GameRenderer.getPositionShader()); 302 VertexBuffer.unbind(); 303 fogCallback.run(); 304 } 305 } 306 public float getStarBrightness(float tickDelta) { 307 float f = world.getTimeOfDay(tickDelta); 308 float g = 1.0F - (Mth.cos(f * (float) (Math.PI * 2)) * 2.0F + 0.25F); 309 g = Mth.clamp(g, 0.0f, 1.0f); 310 float day = options.getDayStarBrightness(); 311 float night = options.getNightStarBrightness(); 312 return day + g*g*(night-day); 313 } 314 315 public void renderSkybox(ResourceLocation texture, float copies, int brightness, int alpha) { 316 renderSkybox(texture, copies, brightness, brightness, brightness, alpha); 317 } 318 public void renderSkybox(ResourceLocation texture, float copies, int r, int g, int b, int alpha) { 319 RenderSystem.enableBlend(); 320 RenderSystem.depthMask(false); 321 RenderSystem.setShader(GameRenderer::getPositionTexColorShader); 322 RenderSystem.setShaderTexture(0, texture); 323 Tesselator tesselator = Tesselator.getInstance(); 324 325 for (int i = 0; i < 6; ++i) { 326 matrices.pushPose(); 327 if (i == 1) { 328 matrices.mulPose(Axis.XP.rotationDegrees(90.0f)); 329 } 330 if (i == 2) { 331 matrices.mulPose(Axis.XP.rotationDegrees(-90.0f)); 332 } 333 if (i == 3) { 334 matrices.mulPose(Axis.XP.rotationDegrees(180.0f)); 335 } 336 if (i == 4) { 337 matrices.mulPose(Axis.ZP.rotationDegrees(90.0f)); 338 } 339 if (i == 5) { 340 matrices.mulPose(Axis.ZP.rotationDegrees(-90.0f)); 341 } 342 343 Matrix4f matrix4f = matrices.last().pose(); 344 FriendlyBuilder bufferBuilder = new FriendlyBuilder(tesselator, VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX_COLOR); 345 bufferBuilder.addVertex(matrix4f, -100.0f, -100.0f, -100.0f).setUv(0.0f, 0.0f).setColor(r, g, b, alpha).endVertex(); 346 bufferBuilder.addVertex(matrix4f, -100.0f, -100.0f, 100.0f).setUv(0.0f, copies).setColor(r, g, b, alpha).endVertex(); 347 bufferBuilder.addVertex(matrix4f, 100.0f, -100.0f, 100.0f).setUv(copies, copies).setColor(r, g, b, alpha).endVertex(); 348 bufferBuilder.addVertex(matrix4f, 100.0f, -100.0f, -100.0f).setUv(copies, 0.0f).setColor(r, g, b, alpha).endVertex(); 349 BufferUploader.drawWithShader(bufferBuilder.buildOrThrow()); 350 matrices.popPose(); 351 } 352 RenderSystem.depthMask(true); 353 RenderSystem.disableBlend(); 354 } 355} 356 357*///?} else { 358import com.mojang.blaze3d.buffers.GpuBuffer; 359import com.mojang.blaze3d.buffers.GpuBufferSlice; 360import com.mojang.blaze3d.pipeline.RenderPipeline; 361import com.mojang.blaze3d.systems.RenderPass; 362import com.mojang.blaze3d.systems.RenderSystem; 363import com.mojang.blaze3d.textures.GpuTextureView; 364import com.mojang.blaze3d.vertex.*; 365import com.mojang.math.Axis; 366import net.minecraft.client.Minecraft; 367import net.minecraft.client.multiplayer.ClientLevel; 368import net.minecraft.client.renderer.*; 369import net.minecraft.client.renderer.texture.AbstractTexture; 370import net.minecraft.client.renderer.texture.TextureManager; 371import net.minecraft.resources.ResourceLocation; 372import net.minecraft.util.ARGB; 373import net.minecraft.util.Mth; 374import net.minecraft.util.RandomSource; 375import org.joml.*; 376 377import java.awt.*; 378import java.lang.Math; 379import java.util.OptionalDouble; 380import java.util.OptionalInt; 381 382public class SkyRenderer implements AutoCloseable { 383 public static final ResourceLocation END_SKY_LOCATION = ResourceLocation.withDefaultNamespace("textures/environment/end_sky.png"); 384 private final GpuBuffer starBuffer; 385 private final RenderSystem.AutoStorageIndexBuffer starIndices; 386 private final GpuBuffer topSkyBuffer; 387 private final GpuBuffer bottomSkyBuffer; 388 private final GpuBuffer endSkyBuffer; 389 private int starIndexCount; 390 InfinityOptions options; 391 ClientLevel level; 392 MultiBufferSource.BufferSource bufferSource; 393 PoseStack poseStack; 394 float timeOfDay; 395 396 397 public SkyRenderer(InfinityOptions options, ClientLevel level) { 398 this.options = options; 399 this.level = level; 400 this.starIndices = RenderSystem.getSequentialBuffer(VertexFormat.Mode.QUADS); 401 this.starBuffer = (new SafeBufferBuilder.Stars(this, options)).build(); 402 this.endSkyBuffer = (new SafeBufferBuilder.End()).build(); 403 this.topSkyBuffer = (new SafeBufferBuilder.Top()).build(); 404 this.bottomSkyBuffer = (new SafeBufferBuilder.Bottom()).build(); 405 } 406 407 public void renderAll(RenderBuffers renderBuffers, float partialTick) { 408 if (level.effects().skyType() == DimensionSpecialEffects.SkyType.END || options.endSkyLike()) { 409 renderEndSky(); 410 } else { 411 poseStack = new PoseStack(); 412 float sunAngle = this.level.getSunAngle(partialTick); 413 timeOfDay = this.level.getTimeOfDay(partialTick); 414 415 float rainLevel = 1.0F - this.level.getRainLevel(partialTick); 416 float starBrightness = getStarBrightness(partialTick) * rainLevel; 417 418 int dawnColor = level.effects().getSunriseOrSunsetColor(timeOfDay); 419 420 int skyColor = (options.getSkyType().equals("rainbow")) 421 ? Color.getHSBColor((2*timeOfDay) - (int)(2*timeOfDay), 1.0f, 1.0f).getRGB() 422 : this.level.getSkyColor(Minecraft.getInstance().gameRenderer.getMainCamera().getPosition(), partialTick); 423 renderSkyDisc(skyColor); 424 425 bufferSource = renderBuffers.bufferSource(); 426 if (level.effects().isSunriseOrSunset(timeOfDay)) { 427 renderSunriseAndSunset(sunAngle, dawnColor); 428 } 429 430 renderSunMoonAndStars(rainLevel, starBrightness); 431 bufferSource.endBatch(); 432 } 433 } 434 435 public float getStarBrightness(float partialTick) { 436 float f = level.getTimeOfDay(partialTick); 437 float g = 1.0F - (Mth.cos(f * (float) (Math.PI * 2)) * 2.0F + 0.25F); 438 g = Mth.clamp(g, 0.0f, 1.0f); 439 float day = options.getDayStarBrightness(); 440 float night = options.getNightStarBrightness(); 441 return day + g*g*(night-day); 442 } 443 444 public void renderSunMoonAndStars(float rainLevel, float starBrightness) { 445 poseStack.pushPose(); 446 this.renderSun(rainLevel); 447 for (int i = 0; i < options.getNumMoons(); i++) { 448 this.renderMoon(i, rainLevel); 449 } 450 bufferSource.endBatch(); 451 if (starBrightness > 0.0F) { 452 this.renderStars(starBrightness); 453 } 454 poseStack.popPose(); 455 } 456 457 public void renderSun(float alpha) { 458 renderSingleBody( 459 options.getSolarSize(), 460 options.getSolarTilt(), 461 0, 462 1, 463 0, 464 ofTintAlpha(options.getSolarTint(), alpha), 465 options.getSolarTexture(), 466 true); 467 } 468 public void renderMoon(int i, float alpha) { 469 renderSingleBody( 470 options.getLunarSize(i), 471 options.getLunarTiltY(i), 472 options.getLunarTiltZ(i), 473 options.getLunarVelocity(i), 474 options.getLunarOffset(i), 475 ofTintAlpha(options.getLunarTint(i), alpha), 476 options.getLunarTexture(i), 477 false); 478 } 479 480 public void renderSingleBody(float size, float tilt_y, float tilt_z, float velocity, float offset, 481 int tint, ResourceLocation texture, boolean sun) { 482 poseStack.mulPose(Axis.YP.rotationDegrees(tilt_y)); 483 poseStack.mulPose(Axis.ZP.rotationDegrees(tilt_z)); 484 rotateWithVelocity(velocity, offset); 485 if (sun) renderSun(texture, size, 100.0f, tint); 486 else renderMoon(texture, size, -100.0f, tint); 487 rotateWithVelocity(-1 * velocity, offset); 488 poseStack.mulPose(Axis.ZP.rotationDegrees(-tilt_z)); 489 poseStack.mulPose(Axis.YP.rotationDegrees(-tilt_y)); 490 } 491 public void rotateWithVelocity(float velocity, float offset) { 492 poseStack.mulPose(Axis.XP.rotationDegrees((timeOfDay + offset) * 360.0f * velocity)); 493 } 494 int ofTintAlpha(Vector3f tint, float alpha) { 495 return ARGB.colorFromFloat(alpha, tint.x, tint.y, tint.z); 496 } 497 498 public void renderSun(ResourceLocation texture, float size, float y, int tint) { 499 VertexConsumer vertexconsumer = bufferSource.getBuffer(RenderType.celestial(texture)); 500 Matrix4f matrix4f = poseStack.last().pose(); 501 vertexconsumer.addVertex(matrix4f, -size, y, -size).setUv(0.0F, 0.0F).setColor(tint); 502 vertexconsumer.addVertex(matrix4f, size, y, -size).setUv(1.0F, 0.0F).setColor(tint); 503 vertexconsumer.addVertex(matrix4f, size, y, size).setUv(1.0F, 1.0F).setColor(tint); 504 vertexconsumer.addVertex(matrix4f, -size, y, size).setUv(0.0F, 1.0F).setColor(tint); 505 } 506 507 public void renderMoon(ResourceLocation texture, float size, float y, int tint) { 508 float t, q, p, o; 509 if (isMoonNormal(texture)) { 510 int phase = this.level.getMoonPhase(); 511 int s = phase % 4; 512 int m = phase / 4 % 2; 513 t = (float)(s) / 4.0f; 514 o = (float)(m) / 2.0f; 515 p = (float)(s + 1) / 4.0f; 516 q = (float)(m + 1) / 2.0f; 517 } 518 else { 519 t = q = 1.0f; 520 p = o = 0.0f; 521 } 522 523 VertexConsumer vertexconsumer = bufferSource.getBuffer(RenderType.celestial(texture)); 524 Matrix4f matrix4f = poseStack.last().pose(); 525 vertexconsumer.addVertex(matrix4f, -size, y, size).setUv(p, q).setColor(tint); 526 vertexconsumer.addVertex(matrix4f, size, y, size).setUv(t, q).setColor(tint); 527 vertexconsumer.addVertex(matrix4f, size, y, -size).setUv(t, o).setColor(tint); 528 vertexconsumer.addVertex(matrix4f, -size, y, -size).setUv(p, o).setColor(tint); 529 } 530 boolean isMoonNormal(ResourceLocation texture) { 531 return texture.getPath().equals("textures/environment/moon_phases.png"); 532 } 533 534 535 public void renderStars(float starBrightness) { 536 renderStars(options.getStellarTiltY(), 537 options.getStellarTiltZ(), 538 options.getStellarVelocity(), 539 0, 540 starBrightness); 541 } 542 public void renderStars(float tilt_y, float tilt_z, float velocity, float offset, 543 float starBrightness) { 544 poseStack.mulPose(Axis.YP.rotationDegrees(tilt_y)); 545 poseStack.mulPose(Axis.ZP.rotationDegrees(tilt_z)); 546 rotateWithVelocity(velocity, offset); 547 renderStarsInner(starBrightness); 548 rotateWithVelocity(-1 * velocity, offset); 549 poseStack.mulPose(Axis.ZP.rotationDegrees(-tilt_z)); 550 poseStack.mulPose(Axis.YP.rotationDegrees(-tilt_y)); 551 } 552 553 private void renderStarsInner(float starBrightness) { 554 Vector3f color = options.getStellarColor(); 555 Matrix4fStack matrix4fstack = RenderSystem.getModelViewStack(); 556 matrix4fstack.pushMatrix(); 557 matrix4fstack.mul(poseStack.last().pose()); 558 RenderPipeline renderpipeline = RenderPipelines.STARS; 559 GpuTextureView gputextureview = Minecraft.getInstance().getMainRenderTarget().getColorTextureView(); 560 GpuTextureView gputextureview1 = Minecraft.getInstance().getMainRenderTarget().getDepthTextureView(); 561 GpuBuffer gpubuffer = this.starIndices.getBuffer(this.starIndexCount); 562 GpuBufferSlice gpubufferslice = RenderSystem.getDynamicUniforms().writeTransform(matrix4fstack, 563 new Vector4f(starBrightness*color.x, starBrightness*color.y, starBrightness*color.z, starBrightness), 564 new Vector3f(), new Matrix4f(), 0.0F); 565 RenderPass renderpass = RenderSystem.getDevice().createCommandEncoder().createRenderPass(() -> "Stars", 566 gputextureview, OptionalInt.empty(), gputextureview1, OptionalDouble.empty()); 567 568 try { 569 renderpass.setPipeline(renderpipeline); 570 RenderSystem.bindDefaultUniforms(renderpass); 571 renderpass.setUniform("DynamicTransforms", gpubufferslice); 572 renderpass.setVertexBuffer(0, this.starBuffer); 573 renderpass.setIndexBuffer(gpubuffer, this.starIndices.type()); 574 renderpass.drawIndexed(0, 0, this.starIndexCount, 1); 575 } catch (Throwable var13) { 576 if (renderpass != null) { 577 try { 578 renderpass.close(); 579 } catch (Throwable var12) { 580 var13.addSuppressed(var12); 581 } 582 } 583 584 throw var13; 585 } 586 587 if (renderpass != null) { 588 renderpass.close(); 589 } 590 591 matrix4fstack.popMatrix(); 592 } 593 594 public void renderSunriseAndSunset(float sunAngle, int color) { 595 poseStack.pushPose(); 596 float tilt = -1.0F * options.getSolarTilt(); 597 poseStack.mulPose(Axis.XP.rotationDegrees(90.0F)); 598 float f = Mth.sin(sunAngle) < 0.0F ? 180.0F : 0.0F; 599 poseStack.mulPose(Axis.ZP.rotationDegrees(f)); 600 poseStack.mulPose(Axis.ZP.rotationDegrees(tilt)); 601 602 Matrix4f matrix4f = poseStack.last().pose(); 603 VertexConsumer vertexconsumer = bufferSource.getBuffer(RenderType.sunriseSunset()); 604 float f1 = ARGB.alphaFloat(color); 605 vertexconsumer.addVertex(matrix4f, 0.0F, 100.0F, 0.0F).setColor(color); 606 int i = ARGB.transparent(color); 607 608 for(int k = 0; k <= 16; ++k) { 609 float f2 = (float)k * 6.2831855F / 16.0F; 610 float f3 = Mth.sin(f2); 611 float f4 = Mth.cos(f2); 612 vertexconsumer.addVertex(matrix4f, f3 * 120.0F, f4 * 120.0F, -f4 * 40.0F * f1).setColor(i); 613 } 614 615 poseStack.popPose(); 616 } 617 618 public void renderSkyDisc(int skyColor) { 619 GpuBufferSlice gpubufferslice = RenderSystem.getDynamicUniforms() 620 .writeTransform(RenderSystem.getModelViewMatrix(), 621 new Vector4f(ARGB.redFloat(skyColor), ARGB.greenFloat(skyColor), ARGB.blueFloat(skyColor), 1.0F), 622 new Vector3f(), new Matrix4f(), 0.0F); 623 GpuTextureView gputextureview = Minecraft.getInstance().getMainRenderTarget().getColorTextureView(); 624 GpuTextureView gputextureview1 = Minecraft.getInstance().getMainRenderTarget().getDepthTextureView(); 625 RenderPass renderpass = RenderSystem.getDevice().createCommandEncoder().createRenderPass(() -> "Sky disc", 626 gputextureview, OptionalInt.empty(), gputextureview1, OptionalDouble.empty()); 627 try { 628 renderpass.setPipeline(RenderPipelines.SKY); 629 RenderSystem.bindDefaultUniforms(renderpass); 630 renderpass.setUniform("DynamicTransforms", gpubufferslice); 631 renderpass.setVertexBuffer(0, this.topSkyBuffer); 632 renderpass.draw(0, 10); 633 } catch (Throwable var11) { 634 if (renderpass != null) { 635 try { 636 renderpass.close(); 637 } catch (Throwable var10) { 638 var11.addSuppressed(var10); 639 } 640 } 641 throw var11; 642 } 643 if (renderpass != null) { 644 renderpass.close(); 645 } 646 } 647 648 public void renderEndSky() { 649 TextureManager texturemanager = Minecraft.getInstance().getTextureManager(); 650 AbstractTexture abstracttexture = texturemanager.getTexture(END_SKY_LOCATION); 651 abstracttexture.setUseMipmaps(false); 652 RenderSystem.AutoStorageIndexBuffer rendersystem$autostorageindexbuffer = RenderSystem.getSequentialBuffer(VertexFormat.Mode.QUADS); 653 GpuBuffer gpubuffer = rendersystem$autostorageindexbuffer.getBuffer(36); 654 GpuTextureView gputextureview = Minecraft.getInstance().getMainRenderTarget().getColorTextureView(); 655 GpuTextureView gputextureview1 = Minecraft.getInstance().getMainRenderTarget().getDepthTextureView(); 656 GpuBufferSlice gpubufferslice = RenderSystem.getDynamicUniforms().writeTransform(RenderSystem.getModelViewMatrix(), new Vector4f(1.0F, 1.0F, 1.0F, 1.0F), new Vector3f(), new Matrix4f(), 0.0F); 657 RenderPass renderpass = RenderSystem.getDevice().createCommandEncoder().createRenderPass(() -> "End sky", 658 gputextureview, OptionalInt.empty(), gputextureview1, OptionalDouble.empty()); 659 660 try { 661 renderpass.setPipeline(RenderPipelines.END_SKY); 662 RenderSystem.bindDefaultUniforms(renderpass); 663 renderpass.setUniform("DynamicTransforms", gpubufferslice); 664 renderpass.bindSampler("Sampler0", abstracttexture.getTextureView()); 665 renderpass.setVertexBuffer(0, this.endSkyBuffer); 666 renderpass.setIndexBuffer(gpubuffer, rendersystem$autostorageindexbuffer.type()); 667 renderpass.drawIndexed(0, 0, 36, 1); 668 } catch (Throwable var12) { 669 if (renderpass != null) { 670 try { 671 renderpass.close(); 672 } catch (Throwable var11) { 673 var12.addSuppressed(var11); 674 } 675 } 676 677 throw var12; 678 } 679 680 if (renderpass != null) { 681 renderpass.close(); 682 } 683 684 } 685 686 @Override 687 public void close() { 688 this.starBuffer.close(); 689 this.topSkyBuffer.close(); 690 this.bottomSkyBuffer.close(); 691 this.endSkyBuffer.close(); 692 } 693 694 public static abstract class SafeBufferBuilder { 695 ByteBufferBuilder bytebufferbuilder; 696 BufferBuilder bufferbuilder; 697 MeshData meshdata; 698 GpuBuffer gpubuffer; 699 700 abstract ByteBufferBuilder createByteBuffer(); 701 abstract BufferBuilder createBuffer(); 702 abstract void populate(); 703 abstract String manifest(); 704 abstract int maniint(); 705 void runSpecial() { 706 } 707 708 GpuBuffer build() { 709 bytebufferbuilder = createByteBuffer(); 710 try { 711 bufferbuilder = createBuffer(); 712 populate(); 713 meshdata = bufferbuilder.buildOrThrow(); 714 715 try { 716 runSpecial(); 717 gpubuffer = RenderSystem.getDevice().createBuffer(this::manifest, maniint(), meshdata.vertexBuffer()); 718 } catch (Throwable var8) { 719 if (meshdata != null) { 720 try { 721 meshdata.close(); 722 } catch (Throwable var7) { 723 var8.addSuppressed(var7); 724 } 725 } 726 throw var8; 727 } 728 if (meshdata != null) { 729 meshdata.close(); 730 } 731 } catch (Throwable var9) { 732 if (bytebufferbuilder != null) { 733 try { 734 bytebufferbuilder.close(); 735 } catch (Throwable var6) { 736 var9.addSuppressed(var6); 737 } 738 } 739 throw var9; 740 } 741 if (bytebufferbuilder != null) { 742 bytebufferbuilder.close(); 743 } 744 return gpubuffer; 745 } 746 747 public static class Stars extends SafeBufferBuilder { 748 int numStars; 749 float sizeBase; 750 float sizeMod; 751 SkyRenderer parent; 752 public Stars(SkyRenderer parent, InfinityOptions options) { 753 this.numStars = options.getNumStars(); 754 this.sizeBase = options.getStarSizeBase(); 755 this.sizeMod = options.getStarSizeModifier(); 756 this.parent = parent; 757 } 758 759 @Override 760 void runSpecial() { 761 parent.starIndexCount = meshdata.drawState().indexCount(); 762 } 763 @Override 764 ByteBufferBuilder createByteBuffer() { 765 return ByteBufferBuilder.exactlySized(DefaultVertexFormat.POSITION.getVertexSize() * numStars * 4); 766 } 767 @Override 768 BufferBuilder createBuffer() { 769 return new BufferBuilder(bytebufferbuilder, VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION); 770 } 771 @Override 772 void populate() { 773 RandomSource randomsource = RandomSource.create(10842L); 774 for(int i = 0; i < numStars; ++i) { 775 float f1 = randomsource.nextFloat() * 2.0F - 1.0F; 776 float f2 = randomsource.nextFloat() * 2.0F - 1.0F; 777 float f3 = randomsource.nextFloat() * 2.0F - 1.0F; 778 float f4 = sizeBase + randomsource.nextFloat() * sizeMod; 779 float f5 = Mth.lengthSquared(f1, f2, f3); 780 if (!(f5 <= 0.010000001F) && !(f5 >= 1.0F)) { 781 Vector3f vector3f = (new Vector3f(f1, f2, f3)).normalize(100.0F); 782 float f6 = (float)(randomsource.nextDouble() * 3.1415927410125732 * 2.0); 783 Matrix3f matrix3f = (new Matrix3f()).rotateTowards((new Vector3f(vector3f)).negate(), new Vector3f(0.0F, 1.0F, 0.0F)).rotateZ(-f6); 784 bufferbuilder.addVertex((new Vector3f(f4, -f4, 0.0F)).mul(matrix3f).add(vector3f)); 785 bufferbuilder.addVertex((new Vector3f(f4, f4, 0.0F)).mul(matrix3f).add(vector3f)); 786 bufferbuilder.addVertex((new Vector3f(-f4, f4, 0.0F)).mul(matrix3f).add(vector3f)); 787 bufferbuilder.addVertex((new Vector3f(-f4, -f4, 0.0F)).mul(matrix3f).add(vector3f)); 788 } 789 } 790 } 791 @Override 792 String manifest() { 793 return "Stars vertex buffer"; 794 } 795 796 @Override 797 int maniint() { 798 return 40; 799 } 800 } 801 802 public static class End extends SafeBufferBuilder { 803 @Override 804 ByteBufferBuilder createByteBuffer() { 805 return ByteBufferBuilder.exactlySized(24 * DefaultVertexFormat.POSITION_TEX_COLOR.getVertexSize()); 806 } 807 @Override 808 BufferBuilder createBuffer() { 809 return new BufferBuilder(bytebufferbuilder, VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX_COLOR); 810 } 811 @Override 812 void populate() { 813 for(int i = 0; i < 6; ++i) { 814 Matrix4f matrix4f = new Matrix4f(); 815 switch (i) { 816 case 1 -> matrix4f.rotationX(1.5707964F); 817 case 2 -> matrix4f.rotationX(-1.5707964F); 818 case 3 -> matrix4f.rotationX(3.1415927F); 819 case 4 -> matrix4f.rotationZ(1.5707964F); 820 case 5 -> matrix4f.rotationZ(-1.5707964F); 821 } 822 823 bufferbuilder.addVertex(matrix4f, -100.0F, -100.0F, -100.0F).setUv(0.0F, 0.0F).setColor(-14145496); 824 bufferbuilder.addVertex(matrix4f, -100.0F, -100.0F, 100.0F).setUv(0.0F, 16.0F).setColor(-14145496); 825 bufferbuilder.addVertex(matrix4f, 100.0F, -100.0F, 100.0F).setUv(16.0F, 16.0F).setColor(-14145496); 826 bufferbuilder.addVertex(matrix4f, 100.0F, -100.0F, -100.0F).setUv(16.0F, 0.0F).setColor(-14145496); 827 } 828 } 829 @Override 830 String manifest() { 831 return "End sky vertex buffer"; 832 } 833 834 @Override 835 int maniint() { 836 return 40; 837 } 838 } 839 840 public abstract static class TopBottom extends SafeBufferBuilder { 841 @Override 842 ByteBufferBuilder createByteBuffer() { 843 return ByteBufferBuilder.exactlySized(10 * DefaultVertexFormat.POSITION.getVertexSize()); 844 } 845 @Override 846 BufferBuilder createBuffer() { 847 return new BufferBuilder(bytebufferbuilder, VertexFormat.Mode.TRIANGLE_FAN, DefaultVertexFormat.POSITION); 848 } 849 abstract float y(); 850 @Override 851 void populate() { 852 float y = y(); 853 float f = Math.signum(y) * 512.0F; 854 bufferbuilder.addVertex(0.0F, y, 0.0F); 855 for(int i = -180; i <= 180; i += 45) { 856 bufferbuilder.addVertex(f * Mth.cos((float)i * 0.017453292F), y, 512.0F * Mth.sin((float)i * 0.017453292F)); 857 } 858 } 859 @Override 860 int maniint() { 861 return 32; 862 } 863 } 864 public static class Top extends TopBottom { 865 @Override 866 float y() { 867 return 16.0F; 868 } 869 @Override 870 String manifest() { 871 return "Top sky vertex buffer"; 872 } 873 } 874 public static class Bottom extends TopBottom { 875 @Override 876 float y() { 877 return -16.0F; 878 } 879 @Override 880 String manifest() { 881 return "Bottom sky vertex buffer"; 882 } 883 } 884 } 885} 886//?}