aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/makamys/neodymium/mixin/MixinWorldRenderer.java
blob: 47450c37375d8d34ac7eba942ff833fbd3d628b3 (plain)
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
package makamys.neodymium.mixin;

import java.util.ArrayList;
import java.util.List;

import org.lwjgl.opengl.GL11;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import makamys.neodymium.LODMod;
import makamys.neodymium.ducks.IWorldRenderer;
import makamys.neodymium.renderer.ChunkMesh;
import makamys.neodymium.renderer.FarChunkCache;
import makamys.neodymium.renderer.FarWorldRenderer;
import makamys.neodymium.renderer.LODRenderer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.WorldRenderer;
import net.minecraft.client.renderer.entity.RenderItem;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.ChunkCache;
import net.minecraft.world.World;

@Mixin(WorldRenderer.class)
abstract class MixinWorldRenderer implements IWorldRenderer {
    
    @Shadow
    public int posX;
    @Shadow
    public int posY;
    @Shadow
    public int posZ;
    
    @Shadow
    private boolean isInFrustum;
    @Shadow
    public boolean[] skipRenderPass;
    @Shadow
    private int glRenderList;
    
    @Shadow
    public boolean needsUpdate;
    
    boolean savedDrawnStatus;
    
    public List<ChunkMesh> chunkMeshes;
    
    @Redirect(method = "setPosition", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/entity/RenderItem;renderAABB(Lnet/minecraft/util/AxisAlignedBB;)V"))
    private void redirectRenderAABB(AxisAlignedBB p1) {
        if(!FarWorldRenderer.class.isInstance(this.getClass())) {
            RenderItem.renderAABB(p1);
        }
    }
    
    @Redirect(method = "updateRenderer", at = @At(value = "NEW", target = "Lnet/minecraft/world/ChunkCache;"))
    private ChunkCache redirectConstructChunkCache(World p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8) {
        if(!FarWorldRenderer.class.isInstance(this.getClass())) {
            return new ChunkCache(p1, p2, p3, p4, p5, p6, p7, p8);
        } else {
            return new FarChunkCache(p1, p2, p3, p4, p5, p6, p7, p8);
        }
    }
    
    @Inject(method = "updateRenderer", at = @At(value = "HEAD"))
    private void preUpdateRenderer(CallbackInfo ci) {
        saveDrawnStatus();
        
        if(LODMod.isActive()) {
            if(needsUpdate) {
                if(chunkMeshes != null) {
                    chunkMeshes.clear();
                } else {
                    chunkMeshes = new ArrayList<>();
                }
            } else {
                chunkMeshes = null;
            }
        }
    }
    
    @Inject(method = "updateRenderer", at = @At(value = "RETURN"))
    private void postUpdateRenderer(CallbackInfo ci) {
        notifyIfDrawnStatusChanged();
        
        if(LODMod.isActive()) {
            if(chunkMeshes != null) {
                LODMod.renderer.onWorldRendererPost(WorldRenderer.class.cast(this));
                chunkMeshes.clear();
            }
        }
    }
    
    @Inject(method = "postRenderBlocks", at = @At(value = "HEAD"))
    private void prePostRenderBlocks(int pass, EntityLivingBase entity, CallbackInfo ci) {
        if(LODMod.isActive() && !LODMod.disableChunkMeshes) {
            if(chunkMeshes != null) {
                chunkMeshes.add(ChunkMesh.fromTessellator(pass, WorldRenderer.class.cast(this), Tessellator.instance));
            }
        }
    }
    
    @Redirect(method = "postRenderBlocks", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/Tessellator;draw()I"))
    private int redirectPostRenderBlocksDraw() {
        if(!FarWorldRenderer.class.isInstance(this.getClass())) {
            return Tessellator.instance.draw();
        } else {
            Tessellator.instance.reset();
            return 0;
        }
    }
    
    // There's probably a nicer way to do this
    
    @Redirect(method = "postRenderBlocks", at = @At(value = "INVOKE", target = "Lorg/lwjgl/opengl/GL11;glPopMatrix()V"))
    private void redirectPostRenderBlocksGL1() {
        if(!FarWorldRenderer.class.isInstance(this.getClass())) {
            GL11.glPopMatrix();
        }
    }
    
    @Redirect(method = "postRenderBlocks", at = @At(value = "INVOKE", target = "Lorg/lwjgl/opengl/GL11;glEndList()V"))
    private void redirectPostRenderBlocksGL2() {
        if(!FarWorldRenderer.class.isInstance(this.getClass())) {
            GL11.glEndList();
        }
    }
    
    // XXX this is inconsistent, Forge callbacks are preserved in postRenderBlocks but not preRenderBlocks
    
    @Inject(method = "preRenderBlocks", at = @At(value = "HEAD"))
    private void preRenderBlocksInjector(CallbackInfo ci) {
        if(FarWorldRenderer.class.isInstance(this.getClass())) {
            Tessellator.instance.setTranslation((double)(-this.posX), (double)(-this.posY), (double)(-this.posZ));
            ci.cancel();
        }
    }
    
    @Inject(method = "setDontDraw", at = @At(value = "HEAD"))
    private void preSetDontDraw(CallbackInfo ci) {
        if(LODMod.isActive()) {
            LODMod.renderer.onWorldRendererChanged(WorldRenderer.class.cast(this), LODRenderer.WorldRendererChange.DELETED);
        }
    }
    
    @Override
    public List<ChunkMesh> getChunkMeshes() {
        return chunkMeshes;
    }
    
    @Inject(method = "updateInFrustum", at = @At(value = "HEAD"))
    private void preUpdateInFrustum(CallbackInfo ci) {
        saveDrawnStatus();
    }
    
    @Inject(method = "updateInFrustum", at = @At(value = "RETURN"))
    private void postUpdateInFrustum(CallbackInfo ci) {
        notifyIfDrawnStatusChanged();
    }
    
    private void saveDrawnStatus() {
        savedDrawnStatus = isDrawn();
    }
    
    private void notifyIfDrawnStatusChanged() {
        boolean drawn = isDrawn();
        if(LODMod.isActive() && drawn != savedDrawnStatus) {
            LODMod.renderer.onWorldRendererChanged(WorldRenderer.class.cast(this), drawn ? LODRenderer.WorldRendererChange.VISIBLE : LODRenderer.WorldRendererChange.INVISIBLE);
        }
    }
    
    @Override
    public boolean isDrawn() {
        return isInFrustum && (!skipRenderPass[0] || !skipRenderPass[1]);
    }
}