aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/makamys/lodmod/mixin/MixinTessellator.java
blob: 36248321da679bee196383779e54f23a8f0c4597 (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
package makamys.lodmod.mixin;

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

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

import makamys.lodmod.ducks.ITessellator;
import makamys.lodmod.renderer.ChunkMesh;
import makamys.lodmod.renderer.MeshQuad;
import makamys.lodmod.renderer.MeshQuad.QuadPlaneComparator;
import net.minecraft.client.renderer.Tessellator;

@Mixin(Tessellator.class)
abstract class MixinTessellator implements ITessellator {
    
    @Shadow
    private int vertexCount;
    
    @Shadow
    private int[] rawBuffer;
    
    @Shadow
    private double xOffset;
    @Shadow
    private double yOffset;
    @Shadow
    private double zOffset;
    
    @Shadow
    private boolean hasTexture;
    @Shadow
    private boolean hasBrightness;
    @Shadow
    private boolean hasColor;
    @Shadow
    private boolean hasNormals;
    
    private static int totalOriginalQuadCount = 0;
    private static int totalSimplifiedQuadCount = 0;
    /*
    public static void endSave() {
        if(out == null) {
            return;
        }
        try {
            out.close();
            String nameList = String.join("\n", ((TextureMap)Minecraft.getMinecraft().getTextureManager().getTexture(TextureMap.locationBlocksTexture)).mapUploadedSprites.keySet());
            Files.write(nameList, new File("tessellator_strings.txt"), Charset.forName("UTF-8"));
            out = null;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }*/
    
    public ChunkMesh toChunkMesh() {
        if(this.vertexCount % 4 != 0) {
            System.out.println("Error: Vertex count is not a multiple of 4");
            return null;
        }
        
        List<MeshQuad> quads = new ArrayList<>();
        
        List<Integer> spriteIndexes = new ArrayList<>();
        List<Byte> xs = new ArrayList<>();
        List<Byte> ys = new ArrayList<>();
        List<Byte> zs = new ArrayList<>();
        List<Byte> relUs = new ArrayList<>();
        List<Byte> relVs = new ArrayList<>();
        List<Byte> bUs = new ArrayList<>();
        List<Byte> bVs = new ArrayList<>();
        List<Integer> cs = new ArrayList<>();
        
        for(int quadI = 0; quadI < this.vertexCount / 4; quadI++) {
            MeshQuad quad = new MeshQuad(rawBuffer, quadI * 32, new ChunkMesh.Flags(hasTexture, hasBrightness, hasColor, hasNormals));
            /*if(quad.bUs[0] == quad.bUs[1] && quad.bUs[1] == quad.bUs[2] && quad.bUs[2] == quad.bUs[3] && quad.bUs[3] == quad.bVs[0] && quad.bVs[0] == quad.bVs[1] && quad.bVs[1] == quad.bVs[2] && quad.bVs[2] == quad.bVs[3] && quad.bVs[3] == 0) {
                quad.deleted = true;
            }*/
            if(quad.plane == quad.PLANE_XZ && !quad.isClockwiseXZ()) {
                // water hack
                quad.deleted = true;
            }
            quads.add(quad);
        }
        boolean optimize = true;
        if(optimize) {
            ArrayList<ArrayList<MeshQuad>> quadsByPlaneDir = new ArrayList<>(); // XY, XZ, YZ
            for(int i = 0; i < 3; i++) {
                quadsByPlaneDir.add(new ArrayList<MeshQuad>());
            }
            for(MeshQuad quad : quads) {
                if(quad.plane != MeshQuad.PLANE_NONE) {
                    quadsByPlaneDir.get(quad.plane).add(quad);
                }
            }
            for(int plane = 0; plane < 3; plane++) {
                quadsByPlaneDir.get(plane).sort(MeshQuad.QuadPlaneComparator.quadPlaneComparators[plane]);
            }
            
            for(int plane = 0; plane < 3; plane++) {
                List<MeshQuad> planeDirQuads = quadsByPlaneDir.get(plane);
                int planeStart = 0;
                for(int quadI = 0; quadI < planeDirQuads.size(); quadI++) {
                    MeshQuad quad = planeDirQuads.get(quadI);
                    MeshQuad nextQuad = quadI == planeDirQuads.size() - 1 ? null : planeDirQuads.get(quadI + 1);
                    if(!quad.onSamePlaneAs(nextQuad)) {
                        simplifyPlane(planeDirQuads.subList(planeStart, quadI));
                        planeStart = quadI + 1;
                    }
                }
            }
        }
        
        int quadCount = countValidQuads(quads);
        
        totalOriginalQuadCount += quads.size();
        totalSimplifiedQuadCount += quadCount;
        //System.out.println("simplified quads " + totalOriginalQuadCount + " -> " + totalSimplifiedQuadCount + " (ratio: " + ((float)totalSimplifiedQuadCount / (float)totalOriginalQuadCount) + ") totalMergeCountByPlane: " + Arrays.toString(totalMergeCountByPlane));
        
        if(quadCount > 0) {
            return new ChunkMesh(
                    (int)(-xOffset / 16), (int)(-yOffset / 16), (int)(-zOffset / 16),
                    new ChunkMesh.Flags(hasTexture, hasBrightness, hasColor, hasNormals),
                    quadCount, quads);
        } else {
            return null;
        }
    }
    
    private void simplifyPlane(List<MeshQuad> planeQuads) {
        MeshQuad lastQuad = null;
        // Pass 1: merge quads to create rows
        for(MeshQuad quad : planeQuads) {
            if(lastQuad != null) {
                lastQuad.tryToMerge(quad);
            }
            if(quad.isValid(quad)) {
                lastQuad = quad;
            }
        }
        
        // Pass 2: merge rows to create rectangles
        // TODO optimize?
        for(int i = 0; i < planeQuads.size(); i++) {
            for(int j = i + 1; j < planeQuads.size(); j++) {
                planeQuads.get(i).tryToMerge(planeQuads.get(j));
            }
        }
    }
    
    private int countValidQuads(List<MeshQuad> quads) {
        int quadCount = 0;
        for(MeshQuad quad : quads) {
            if(!quad.deleted) {
                quadCount++;
            }
        }
        return quadCount;
    }
}