|
1 | 1 | package me.juancarloscp52.bedrockify.mixin.client.features.bedrockShading.sunGlare; |
2 | 2 |
|
| 3 | +import com.llamalad7.mixinextras.injector.ModifyExpressionValue; |
3 | 4 | import com.llamalad7.mixinextras.injector.wrapoperation.Operation; |
4 | 5 | import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; |
5 | | -import com.mojang.blaze3d.buffers.GpuBuffer; |
6 | | -import com.mojang.blaze3d.buffers.GpuBufferSlice; |
7 | | -import com.mojang.blaze3d.systems.RenderPass; |
8 | | -import com.mojang.blaze3d.systems.RenderSystem; |
9 | | -import com.mojang.blaze3d.textures.GpuTextureView; |
10 | | -import com.mojang.blaze3d.vertex.VertexFormat; |
11 | 6 | import me.juancarloscp52.bedrockify.client.BedrockifyClient; |
12 | 7 | import me.juancarloscp52.bedrockify.client.features.bedrockShading.BedrockSunGlareShading; |
13 | | -import net.minecraft.client.MinecraftClient; |
14 | | -import net.minecraft.client.gl.RenderPipelines; |
15 | | -import net.minecraft.client.render.*; |
16 | | -import net.minecraft.client.render.state.SkyRenderState; |
17 | | -import net.minecraft.client.texture.AbstractTexture; |
18 | | -import net.minecraft.client.texture.TextureManager; |
19 | | -import net.minecraft.client.util.BufferAllocator; |
20 | | -import net.minecraft.client.util.math.MatrixStack; |
21 | | -import net.minecraft.client.world.ClientWorld; |
22 | | -import net.minecraft.util.Identifier; |
| 8 | +import net.minecraft.client.render.SkyRendering; |
23 | 9 | import net.minecraft.util.math.MathHelper; |
24 | | -import net.minecraft.util.math.Vec3d; |
25 | 10 | import org.joml.Matrix4f; |
26 | 11 | import org.joml.Matrix4fStack; |
27 | | -import org.joml.Vector3f; |
28 | 12 | import org.joml.Vector4f; |
29 | 13 | import org.spongepowered.asm.mixin.Mixin; |
30 | 14 | import org.spongepowered.asm.mixin.Unique; |
31 | | -import org.spongepowered.asm.mixin.gen.Accessor; |
32 | 15 | import org.spongepowered.asm.mixin.injection.At; |
33 | | -import org.spongepowered.asm.mixin.injection.Inject; |
34 | | -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
35 | | - |
36 | | -import java.util.Objects; |
37 | | -import java.util.OptionalDouble; |
38 | | -import java.util.OptionalInt; |
39 | 16 |
|
40 | 17 | @Mixin(SkyRendering.class) |
41 | 18 | public abstract class SkyRenderingMixin { |
42 | 19 | @Unique |
43 | 20 | private final BedrockSunGlareShading sunGlareShading = BedrockifyClient.getInstance().bedrockSunGlareShading; |
44 | | - @Unique |
45 | | - private GpuBuffer sunVertexBuffer; |
46 | | - @Unique |
47 | | - private int sunIndexCount; |
48 | | - |
49 | | - @Accessor("SUN_TEXTURE") |
50 | | - public static Identifier getSunTextureId() { |
51 | | - throw new AssertionError(); |
52 | | - } |
53 | | - |
54 | | - @Inject(method = "<init>", at = @At("RETURN")) |
55 | | - private void bedrockify$SkyRenderingCtor(CallbackInfo ci) { |
56 | | - // Allocate buffer. |
57 | | - try (BufferAllocator bufferAllocator = BufferAllocator.fixedSized(4 * VertexFormats.POSITION_TEXTURE_COLOR.getVertexSize())) { |
58 | | - BufferBuilder bufferBuilder = new BufferBuilder(bufferAllocator, VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_TEXTURE_COLOR); |
59 | | - |
60 | | - Matrix4f matrix4f = new Matrix4f(); |
61 | | - bufferBuilder.vertex(matrix4f, -30.0F, 100.0F, -30.0F).texture(0.0F, 0.0F).color(-1); |
62 | | - bufferBuilder.vertex(matrix4f, 30.0F, 100.0F, -30.0F).texture(1.0F, 0.0F).color(-1); |
63 | | - bufferBuilder.vertex(matrix4f, 30.0F, 100.0F, 30.0F).texture(1.0F, 1.0F).color(-1); |
64 | | - bufferBuilder.vertex(matrix4f, -30.0F, 100.0F, 30.0F).texture(0.0F, 1.0F).color(-1); |
65 | | - |
66 | | - try (BuiltBuffer builtBuffer = bufferBuilder.end()) { |
67 | | - this.sunIndexCount = builtBuffer.getDrawParameters().indexCount(); |
68 | | - this.sunVertexBuffer = RenderSystem.getDevice().createBuffer(() -> "Bedrockify Sun vertex buffer", 8, builtBuffer.getBuffer()); |
69 | | - } |
70 | | - } |
71 | | - } |
72 | 21 |
|
73 | | - @WrapOperation(method = "renderCelestialBodies", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/SkyRendering;renderSun(FLnet/minecraft/client/util/math/MatrixStack;)V")) |
74 | | - private void bedrockify$modifySunIntensity(SkyRendering instance, float alpha, MatrixStack matrices, Operation<Void> original) { |
75 | | - final float intensity = MathHelper.lerp(sunGlareShading.getSunBrightnessDelta(), 1.5f + sunGlareShading.getSunIntensityDelta() * 0.5f, 1.0f); |
| 22 | + @WrapOperation(method = "renderSun", at = @At(value = "INVOKE", target = "Lorg/joml/Matrix4fStack;scale(FFF)Lorg/joml/Matrix4f;")) |
| 23 | + private Matrix4f bedrockify$modifySunRadius(Matrix4fStack instance, float x, float y, float z, Operation<Matrix4f> original) { |
76 | 24 | final float scale = MathHelper.lerp(sunGlareShading.getSunBrightnessDelta(), 1.3f, 1f); |
77 | | - |
78 | | - // Get model view matrix. |
79 | | - Matrix4fStack modelView = RenderSystem.getModelViewStack(); |
80 | | - modelView.pushMatrix(); |
81 | | - |
82 | | - // Get position matrix. |
83 | | - Matrix4f position = matrices.peek().getPositionMatrix(); |
84 | | - // Apply scaling. |
85 | | - modelView.mul(position).scale(scale, 1f, scale); |
86 | | - |
87 | | - // Set up texture renderer. |
88 | | - TextureManager textureManager = MinecraftClient.getInstance().getTextureManager(); |
89 | | - AbstractTexture abstractTexture = textureManager.getTexture(getSunTextureId()); |
90 | | - abstractTexture.setUseMipmaps(false); |
91 | | - RenderSystem.ShapeIndexBuffer shapeIndexBuffer = RenderSystem.getSequentialBuffer(VertexFormat.DrawMode.QUADS); |
92 | | - GpuBuffer gpuBuffer = shapeIndexBuffer.getIndexBuffer(this.sunIndexCount); |
93 | | - GpuTextureView colorView = MinecraftClient.getInstance().getFramebuffer().getColorAttachmentView(); |
94 | | - GpuTextureView depthView = MinecraftClient.getInstance().getFramebuffer().getDepthAttachmentView(); |
95 | | - GpuBufferSlice uniform = RenderSystem.getDynamicUniforms().write(modelView, new Vector4f(intensity, intensity, intensity, alpha), new Vector3f(), new Matrix4f(), 0.0F); |
96 | | - |
97 | | - // Draw. |
98 | | - try (RenderPass renderPass = RenderSystem.getDevice().createCommandEncoder().createRenderPass(() -> "Bedrockify Sun", colorView, OptionalInt.empty(), depthView, OptionalDouble.empty())) { |
99 | | - renderPass.setPipeline(RenderPipelines.POSITION_TEX_COLOR_CELESTIAL); |
100 | | - RenderSystem.bindDefaultUniforms(renderPass); |
101 | | - renderPass.setUniform("DynamicTransforms", uniform); |
102 | | - renderPass.bindSampler("Sampler0", abstractTexture.getGlTextureView()); |
103 | | - renderPass.setVertexBuffer(0, Objects.requireNonNull(this.sunVertexBuffer)); |
104 | | - renderPass.setIndexBuffer(gpuBuffer, shapeIndexBuffer.getIndexType()); |
105 | | - renderPass.drawIndexed(0, 0, this.sunIndexCount, 1); |
106 | | - } |
107 | | - |
108 | | - modelView.popMatrix(); |
109 | | - } |
110 | | - |
111 | | - @Inject(method = "updateRenderState", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/world/ClientWorld;getSkyColor(Lnet/minecraft/util/math/Vec3d;F)I")) |
112 | | - private static void bedrockify$updateSunAngleDiff(ClientWorld world, float f, Vec3d pos, SkyRenderState state, CallbackInfo ci) { |
113 | | - final BedrockSunGlareShading sunGlareShading = BedrockifyClient.getInstance().bedrockSunGlareShading; |
114 | | - sunGlareShading.updateSunBrightnessDelta(f); |
| 25 | + return original.call(instance, x * scale, y, z * scale); |
115 | 26 | } |
116 | 27 |
|
117 | | - @Inject(method = "close", at = @At("HEAD")) |
118 | | - private void bedrockify$closeBuffer(CallbackInfo ci) { |
119 | | - if (this.sunVertexBuffer != null) { |
120 | | - this.sunVertexBuffer.close(); |
121 | | - } |
| 28 | + @ModifyExpressionValue(method = "renderSun", at = @At(value = "NEW", target = "org/joml/Vector4f")) |
| 29 | + private Vector4f bedrockify$modifySunBrightness(Vector4f original) { |
| 30 | + final float brightness = MathHelper.lerp(sunGlareShading.getSunBrightnessDelta(), 1.5f + sunGlareShading.getSunIntensityDelta() * 0.5f, 1.0f); |
| 31 | + return original.mul(brightness, brightness, brightness, 1); |
122 | 32 | } |
123 | 33 | } |
0 commit comments