-
Notifications
You must be signed in to change notification settings - Fork 79
/
ChiselsAndBitsBakedQuad.java
225 lines (190 loc) · 4.97 KB
/
ChiselsAndBitsBakedQuad.java
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
package mod.chiselsandbits.render.chiseledblock;
import mod.chiselsandbits.render.cache.InMemoryQuadCompressor;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.client.renderer.vertex.VertexFormat;
import net.minecraft.client.renderer.vertex.VertexFormatElement;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.client.model.pipeline.IVertexConsumer;
import net.minecraftforge.client.model.pipeline.LightUtil;
public class ChiselsAndBitsBakedQuad extends BakedQuad
{
private static InMemoryQuadCompressor inMemoryCompressor = new InMemoryQuadCompressor();
public static final VertexFormat VERTEX_FORMAT = new VertexFormat();
static
{
for ( final VertexFormatElement element : DefaultVertexFormats.ITEM.getElements() )
{
VERTEX_FORMAT.addElement( element );
}
// add lightmap ;)
VERTEX_FORMAT.addElement( DefaultVertexFormats.TEX_2S );
}
protected final float[][][] rawVertData;
@Override
public void pipe(
final IVertexConsumer consumer )
{
final int[] eMap = LightUtil.mapFormats( consumer.getVertexFormat(), format );
consumer.setQuadTint( getTintIndex() );
consumer.setQuadOrientation( getFace() );
consumer.setApplyDiffuseLighting( true );
for ( int v = 0; v < 4; v++ )
{
for ( int e = 0; e < consumer.getVertexFormat().getElementCount(); e++ )
{
if ( eMap[e] != format.getElementCount() )
{
consumer.put( e, rawVertData[v][eMap[e]] );
}
else
{
consumer.put( e );
}
}
}
}
@Override
public int[] getVertexData() // anyone asking this will expect ITEM.
{
final int[] tmpData = new int[format.getNextOffset() /* / 4 * 4 */];
for ( int v = 0; v < 4; v++ )
{
for ( int e = 0; e < format.getElementCount(); e++ )
{
LightUtil.pack( rawVertData[v][e], tmpData, format, v, e );
}
}
return tmpData;
}
static int[] OPTIFINE_WORKAROUND = new int[1];
public ChiselsAndBitsBakedQuad(
final float[][][] unpackedData,
final int tint,
final EnumFacing orientation,
final TextureAtlasSprite sprite,
VertexFormat format,
boolean enableModelCompression )
{
super( OPTIFINE_WORKAROUND, tint, orientation, sprite, true, format );
rawVertData = enableModelCompression ? inMemoryCompressor.compress( unpackedData ) : unpackedData;
}
public static class Colored extends ChiselsAndBitsBakedQuad
{
public Colored(
final float[][][] unpackedData,
final int tint,
final EnumFacing orientation,
final TextureAtlasSprite sprite,
VertexFormat format,
boolean enableModelCompression )
{
super( unpackedData, tint, orientation, sprite, format, enableModelCompression );
}
}
public static class Builder implements IVertexConsumer, IFaceBuilder
{
private float[][][] unpackedData;
private int tint = -1;
private EnumFacing orientation;
private final boolean isColored = false;
private int vertices = 0;
private int elements = 0;
private final VertexFormat format;
private final boolean enableModelCompression;
public Builder(
VertexFormat format,
boolean enableModelCompression )
{
this.format = format;
this.enableModelCompression = enableModelCompression;
}
@Override
public VertexFormat getVertexFormat()
{
return format;
}
@Override
public void setQuadTint(
final int tint )
{
this.tint = tint;
}
@Override
public void setQuadOrientation(
final EnumFacing orientation )
{
this.orientation = orientation;
}
@Override
public void put(
final int element,
final float... data )
{
for ( int i = 0; i < 4; i++ )
{
if ( i < data.length )
{
unpackedData[vertices][element][i] = data[i];
}
else
{
unpackedData[vertices][element][i] = 0;
}
}
elements++;
if ( elements == getVertexFormat().getElementCount() )
{
vertices++;
elements = 0;
}
}
@Override
public void begin()
{
if ( format != getVertexFormat() )
{
throw new RuntimeException( "Bad format, can only be CNB." );
}
unpackedData = new float[4][getVertexFormat().getElementCount()][4];
tint = -1;
orientation = null;
vertices = 0;
elements = 0;
}
@Override
public BakedQuad create(
final TextureAtlasSprite sprite )
{
if ( isColored )
{
return new Colored( unpackedData, tint, orientation, sprite, getFormat(), enableModelCompression );
}
return new ChiselsAndBitsBakedQuad( unpackedData, tint, orientation, sprite, getFormat(), enableModelCompression );
}
@Override
public void setFace(
final EnumFacing myFace,
final int tintIndex )
{
setQuadOrientation( myFace );
setQuadTint( tintIndex );
}
@Override
public void setApplyDiffuseLighting(
final boolean diffuse )
{
}
@Override
public void setTexture(
final TextureAtlasSprite texture )
{
}
@Override
public VertexFormat getFormat()
{
return format;
}
}
}