-
Notifications
You must be signed in to change notification settings - Fork 497
/
RenderResizableCuboid.java
565 lines (472 loc) · 24.1 KB
/
RenderResizableCuboid.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
package buildcraft.core.lib.client.render;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.lwjgl.opengl.GL11;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.WorldRenderer;
import net.minecraft.client.renderer.culling.ICamera;
import net.minecraft.client.renderer.entity.Render;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.client.renderer.vertex.VertexFormat;
import net.minecraft.client.renderer.vertex.VertexFormatElement;
import net.minecraft.entity.Entity;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumFacing.Axis;
import net.minecraft.util.EnumFacing.AxisDirection;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.Vec3;
import net.minecraft.world.IBlockAccess;
import buildcraft.core.lib.EntityResizableCuboid;
import buildcraft.core.lib.client.model.BCModelHelper;
import buildcraft.core.lib.client.model.FacingRotationHelper;
import buildcraft.core.lib.client.model.MutableQuad;
import buildcraft.core.lib.client.model.MutableVertex;
import buildcraft.core.lib.utils.Utils;
public class RenderResizableCuboid extends Render<EntityResizableCuboid> {
public interface IBlockLocation {
Vec3 transformToWorld(Vec3 vec);
}
public interface IFacingLocation {
EnumFacing transformToWorld(EnumFacing face);
}
public enum DefaultFacingLocation implements IFacingLocation {
FACING_INSTANCE;
@Override
public EnumFacing transformToWorld(EnumFacing face) {
return face;
}
}
public static class RotatedFacingLocation implements IFacingLocation {
private final FacingRotationHelper helper;
private final EnumFacing to;
public RotatedFacingLocation(EnumFacing modelDirection, EnumFacing face) {
helper = FacingRotationHelper.helperForFace(modelDirection);
this.to = face;
}
@Override
public EnumFacing transformToWorld(EnumFacing face) {
return helper.rotateFace(to, face);
}
}
public enum EnumShadeType {
FACE(DefaultVertexFormats.COLOR_4UB),
LIGHT(DefaultVertexFormats.TEX_2S),
AMBIENT_OCCLUSION(DefaultVertexFormats.COLOR_4UB);
private final VertexFormatElement element;
private EnumShadeType(VertexFormatElement element) {
this.element = element;
}
}
public enum EnumShadeArgument {
NONE,
FACE(EnumShadeType.FACE),
FACE_LIGHT(EnumShadeType.FACE, EnumShadeType.LIGHT),
FACE_OCCLUDE(EnumShadeType.FACE, EnumShadeType.AMBIENT_OCCLUSION),
FACE_LIGHT_OCCLUDE(EnumShadeType.FACE, EnumShadeType.LIGHT, EnumShadeType.AMBIENT_OCCLUSION),
LIGHT(EnumShadeType.LIGHT),
LIGHT_OCCLUDE(EnumShadeType.LIGHT, EnumShadeType.AMBIENT_OCCLUSION),
OCCLUDE(EnumShadeType.AMBIENT_OCCLUSION);
public final ImmutableSet<EnumShadeType> types;
final VertexFormat vertexFormat;
EnumShadeArgument(EnumShadeType... types) {
this.vertexFormat = new VertexFormat();
vertexFormat.addElement(DefaultVertexFormats.POSITION_3F);
vertexFormat.addElement(DefaultVertexFormats.TEX_2F);
for (EnumShadeType type : types) {
if (!vertexFormat.getElements().contains(type.element)) vertexFormat.addElement(type.element);
}
this.types = ImmutableSet.copyOf(types);
}
public boolean isEnabled(EnumShadeType type) {
return types.contains(type);
}
}
public static final RenderResizableCuboid INSTANCE = new RenderResizableCuboid();
/** The AO map assumes that each direction in the world has a different amount of light going towards it. */
private static final Map<EnumFacing, Vec3> aoMap = Maps.newEnumMap(EnumFacing.class);
private static final int U_MIN = 0;
private static final int U_MAX = 1;
private static final int V_MIN = 2;
private static final int V_MAX = 3;
static {
// Static constants taken directly from minecraft's block renderer
// ( net.minecraft.client.renderer.BlockModelRenderer.EnumNeighborInfo )
aoMap.put(EnumFacing.UP, Utils.vec3(1));
aoMap.put(EnumFacing.DOWN, Utils.vec3(0.5));
aoMap.put(EnumFacing.NORTH, Utils.vec3(0.8));
aoMap.put(EnumFacing.SOUTH, Utils.vec3(0.8));
aoMap.put(EnumFacing.EAST, Utils.vec3(0.6));
aoMap.put(EnumFacing.WEST, Utils.vec3(0.6));
}
protected RenderResizableCuboid() {
super(Minecraft.getMinecraft().getRenderManager());
}
@Override
protected ResourceLocation getEntityTexture(EntityResizableCuboid entity) {
return null;
}
@Override
public boolean shouldRender(EntityResizableCuboid entity, ICamera camera, double camX, double camY, double camZ) {
return true;
}
@Override
public void doRender(EntityResizableCuboid entity, double x, double y, double z, float entityYaw, float partialTicks) {
GL11.glPushMatrix();
GL11.glTranslated(x, y, z);
final Vec3 entPos = Utils.getInterpolatedVec(entity, partialTicks);
IBlockLocation formula = new IBlockLocation() {
@Override
public Vec3 transformToWorld(Vec3 vec) {
return entPos.add(vec);
}
};
RenderHelper.disableStandardItemLighting();
renderCube(entity, EnumShadeArgument.FACE_LIGHT, formula, null);
RenderHelper.enableStandardItemLighting();
GL11.glPopMatrix();
}
/** This will render a cuboid from its middle. */
public void renderCubeFromCentre(EntityResizableCuboid cuboid) {
GL11.glPushMatrix();
GL11.glTranslated(-cuboid.xSize / 2d, -cuboid.ySize / 2d, -cuboid.zSize / 2d);
renderCube(cuboid, EnumShadeArgument.NONE, null, null);
GL11.glPopMatrix();
}
public void renderCube(EntityResizableCuboid cuboid) {
renderCube(cuboid, EnumShadeArgument.NONE, null, null);
}
public void renderCube(EntityResizableCuboid cube, EnumShadeArgument shadeTypes, IBlockLocation formula, IFacingLocation faceFormula) {
if (faceFormula == null) {
faceFormula = DefaultFacingLocation.FACING_INSTANCE;
}
TextureAtlasSprite[] sprites = cube.textures;
if (sprites == null) {
sprites = new TextureAtlasSprite[6];
for (int i = 0; i < 6; i++) {
sprites[i] = cube.texture;
}
}
int[] flips = cube.textureFlips;
if (flips == null) {
flips = new int[6];
}
Vec3 textureStart = new Vec3(cube.textureStartX / 16D, cube.textureStartY / 16D, cube.textureStartZ / 16D);
Vec3 textureSize = new Vec3(cube.textureSizeX / 16D, cube.textureSizeY / 16D, cube.textureSizeZ / 16D);
Vec3 textureOffset = new Vec3(cube.textureOffsetX / 16D, cube.textureOffsetY / 16D, cube.textureOffsetZ / 16D);
Vec3 size = new Vec3(cube.xSize, cube.ySize, cube.zSize);
bindTexture(cube.resource == null ? TextureMap.locationBlocksTexture : cube.resource);
Tessellator tess = Tessellator.getInstance();
WorldRenderer wr = tess.getWorldRenderer();
wr.begin(GL11.GL_QUADS, shadeTypes.vertexFormat);
for (EnumFacing face : EnumFacing.values()) {
renderCuboidFace(wr, face, sprites, flips, textureStart, textureSize, size, textureOffset, shadeTypes, formula, faceFormula,
cube.worldObj);
}
tess.draw();
}
private static void renderCuboidFace(WorldRenderer wr, EnumFacing face, TextureAtlasSprite[] sprites, int[] flips, Vec3 textureStart,
Vec3 textureSize, Vec3 size, Vec3 textureOffset, EnumShadeArgument shadeTypes, IBlockLocation locationFormula,
IFacingLocation faceFormula, IBlockAccess access) {
int ordinal = face.ordinal();
if (sprites[ordinal] == null) {
return;
}
Vec3 textureEnd = textureStart.add(textureSize);
float[] uv = getUVArray(sprites[ordinal], flips[ordinal], face, textureStart, textureEnd);
List<RenderInfo> renderInfoList = getRenderInfos(uv, face, size, textureSize, textureOffset);
Axis u = face.getAxis() == Axis.X ? Axis.Z : Axis.X;
Axis v = face.getAxis() == Axis.Y ? Axis.Z : Axis.Y;
double other = face.getAxisDirection() == AxisDirection.POSITIVE ? Utils.getValue(size, face.getAxis()) : 0;
/* Swap the face if this is positive: the renderer returns indexes that ALWAYS are for the negative face, so
* light it properly this way */
face = face.getAxisDirection() == AxisDirection.NEGATIVE ? face : face.getOpposite();
EnumFacing opposite = face.getOpposite();
for (RenderInfo ri : renderInfoList) {
renderPoint(wr, face, u, v, other, ri, true, false, locationFormula, faceFormula, access, shadeTypes);
renderPoint(wr, face, u, v, other, ri, true, true, locationFormula, faceFormula, access, shadeTypes);
renderPoint(wr, face, u, v, other, ri, false, true, locationFormula, faceFormula, access, shadeTypes);
renderPoint(wr, face, u, v, other, ri, false, false, locationFormula, faceFormula, access, shadeTypes);
renderPoint(wr, opposite, u, v, other, ri, false, false, locationFormula, faceFormula, access, shadeTypes);
renderPoint(wr, opposite, u, v, other, ri, false, true, locationFormula, faceFormula, access, shadeTypes);
renderPoint(wr, opposite, u, v, other, ri, true, true, locationFormula, faceFormula, access, shadeTypes);
renderPoint(wr, opposite, u, v, other, ri, true, false, locationFormula, faceFormula, access, shadeTypes);
}
}
private static void renderPoint(WorldRenderer wr, EnumFacing face, Axis u, Axis v, double other, RenderInfo ri, boolean minU, boolean minV,
IBlockLocation locationFormula, IFacingLocation faceFormula, IBlockAccess access, EnumShadeArgument shadeTypes) {
int U_ARRAY = minU ? U_MIN : U_MAX;
int V_ARRAY = minV ? V_MIN : V_MAX;
Vec3 vertex = Utils.withValue(Utils.VEC_ZERO, u, ri.xyz[U_ARRAY]);
vertex = Utils.withValue(vertex, v, ri.xyz[V_ARRAY]);
vertex = Utils.withValue(vertex, face.getAxis(), other);
wr.pos(vertex.xCoord, vertex.yCoord, vertex.zCoord);
wr.tex(ri.uv[U_ARRAY], ri.uv[V_ARRAY]);
if (shadeTypes.isEnabled(EnumShadeType.FACE)) {
RenderUtils.setWorldRendererRGB(wr, aoMap.get(faceFormula.transformToWorld(face)));
}
if (shadeTypes.isEnabled(EnumShadeType.AMBIENT_OCCLUSION)) {
applyLocalAO(wr, faceFormula.transformToWorld(face), locationFormula, access, shadeTypes, vertex);
} else if (shadeTypes.isEnabled(EnumShadeType.LIGHT)) {
Vec3 transVertex = locationFormula.transformToWorld(vertex);
BlockPos pos = Utils.convertFloor(transVertex);
Block block = access.getBlockState(pos).getBlock();
int combindedLight = block.getMixedBrightnessForBlock(access, pos);
wr.lightmap(combindedLight >> 16 & 65535, combindedLight & 65535);
}
wr.endVertex();
}
private static void applyLocalAO(WorldRenderer wr, EnumFacing face, IBlockLocation locationFormula, IBlockAccess access,
EnumShadeArgument shadeTypes, Vec3 vertex) {
// This doesn't work. At all.
boolean allAround = false;
int numPositions = allAround ? 7 : 5;
int[] skyLight = new int[numPositions];
int[] blockLight = new int[numPositions];
float[] colorMultiplier = new float[numPositions];
double[] distances = new double[numPositions];
double totalDist = 0;
Vec3 transVertex = locationFormula.transformToWorld(vertex);
BlockPos pos = Utils.convertFloor(transVertex);
Block block = access.getBlockState(pos).getBlock();
int combindedLight = block.getMixedBrightnessForBlock(access, pos);
skyLight[0] = combindedLight / 0x10000;
blockLight[0] = combindedLight % 0x10000;
colorMultiplier[0] = block.getAmbientOcclusionLightValue();
distances[0] = transVertex.distanceTo(Utils.convertMiddle(pos));
int index = 0;
EnumFacing[] testArray = allAround ? EnumFacing.values() : Utils.getNeighbours(face);
for (EnumFacing otherFace : testArray) {
Vec3 nearestOther = vertex.add(Utils.convert(otherFace));
pos = Utils.convertFloor(locationFormula.transformToWorld(nearestOther));
block = access.getBlockState(pos).getBlock();
combindedLight = block.getMixedBrightnessForBlock(access, pos);
index++;
skyLight[index] = combindedLight / 0x10000;
blockLight[index] = combindedLight % 0x10000;
colorMultiplier[index] = block.getAmbientOcclusionLightValue();
// The extra 0.1 is to stop any 1 divided by 0 errors
distances[index] = 1 / (transVertex.distanceTo(Utils.convertMiddle(pos)) + 0.1);
totalDist += distances[index];
}
double avgBlockLight = 0;
double avgSkyLight = 0;
float avgColorMultiplier = 0;
for (int i = 0; i < numPositions; i++) {
double part = distances[i] / totalDist;
avgBlockLight += blockLight[i] * part;
avgSkyLight += skyLight[i] * part;
avgColorMultiplier += colorMultiplier[i] * part;
}
if (shadeTypes.isEnabled(EnumShadeType.LIGHT)) {
int capBlockLight = (int) avgBlockLight;
int capSkyLight = (int) avgSkyLight;
wr.lightmap(capBlockLight, capSkyLight);
}
Vec3 color;
if (shadeTypes.isEnabled(EnumShadeType.FACE)) {
color = aoMap.get(face);
} else {
color = Utils.VEC_ONE;
}
color = Utils.multiply(color, avgColorMultiplier);
RenderUtils.setWorldRendererRGB(wr, color);
}
public static void bakeCube(List<MutableQuad> quads, EntityResizableCuboid cuboid, boolean outsideFace, boolean insideFace) {
TextureAtlasSprite[] sprites = cuboid.textures;
if (sprites == null) {
sprites = new TextureAtlasSprite[6];
for (int i = 0; i < 6; i++) {
sprites[i] = cuboid.texture;
}
}
int[] flips = cuboid.textureFlips;
if (flips == null) {
flips = new int[6];
}
Vec3 textureStart = new Vec3(cuboid.textureStartX / 16D, cuboid.textureStartY / 16D, cuboid.textureStartZ / 16D);
Vec3 textureSize = new Vec3(cuboid.textureSizeX / 16D, cuboid.textureSizeY / 16D, cuboid.textureSizeZ / 16D);
Vec3 textureOffset = new Vec3(cuboid.textureOffsetX / 16D, cuboid.textureOffsetY / 16D, cuboid.textureOffsetZ / 16D);
Vec3 size = new Vec3(cuboid.xSize, cuboid.ySize, cuboid.zSize);
for (EnumFacing face : EnumFacing.values()) {
bakeCuboidFace(quads, cuboid, face, sprites, flips, textureStart, textureSize, size, textureOffset, outsideFace, insideFace);
}
}
private static void bakeCuboidFace(List<MutableQuad> quads, EntityResizableCuboid cuboid, EnumFacing face, TextureAtlasSprite[] sprites,
int[] flips, Vec3 textureStart, Vec3 textureSize, Vec3 size, Vec3 textureOffset, boolean out, boolean in) {
int ordinal = face.ordinal();
if (sprites[ordinal] == null) {
return;
}
Vec3 textureEnd = textureStart.add(textureSize);
float[] uv = getUVArray(sprites[ordinal], flips[ordinal], face, textureStart, textureEnd);
List<RenderInfo> renderInfoList = getRenderInfos(uv, face, size, textureSize, textureOffset);
Axis u = face.getAxis() == Axis.X ? Axis.Z : Axis.X;
Axis v = face.getAxis() == Axis.Y ? Axis.Z : Axis.Y;
double other = face.getAxisDirection() == AxisDirection.POSITIVE ? Utils.getValue(size, face.getAxis()) : 0;
/* Swap the face if this is positive: the renderer returns indexes that ALWAYS are for the negative face, so
* light it properly this way */
// face = face.getAxisDirection() == AxisDirection.NEGATIVE ? face : face.getOpposite();
EnumFacing opposite = face.getOpposite();
boolean flip = BCModelHelper.shouldInvertForRender(face);
for (RenderInfo ri : renderInfoList) {
ri = ri.offset(cuboid, face.getAxis());
double otherMoved = other + Utils.getValue(cuboid.getPositionVector(), face.getAxis());
if (flip ? out : in) {
MutableQuad mutable = new MutableQuad(-1, face);
bakePoint(mutable.getVertex(0), face, u, v, otherMoved, ri, true, false);
bakePoint(mutable.getVertex(1), face, u, v, otherMoved, ri, true, true);
bakePoint(mutable.getVertex(2), face, u, v, otherMoved, ri, false, true);
bakePoint(mutable.getVertex(3), face, u, v, otherMoved, ri, false, false);
quads.add(mutable);
}
if (flip ? in : out) {
MutableQuad mutable = new MutableQuad(-1, face);
bakePoint(mutable.getVertex(0), opposite, u, v, otherMoved, ri, false, false);
bakePoint(mutable.getVertex(1), opposite, u, v, otherMoved, ri, false, true);
bakePoint(mutable.getVertex(2), opposite, u, v, otherMoved, ri, true, true);
bakePoint(mutable.getVertex(3), opposite, u, v, otherMoved, ri, true, false);
quads.add(mutable);
}
}
}
private static void bakePoint(MutableVertex mutable, EnumFacing face, Axis u, Axis v, double other, RenderInfo ri, boolean minU, boolean minV) {
int U_ARRAY = minU ? U_MIN : U_MAX;
int V_ARRAY = minV ? V_MIN : V_MAX;
Vec3 vertex = Utils.withValue(Utils.VEC_ZERO, u, ri.xyz[U_ARRAY]);
vertex = Utils.withValue(vertex, v, ri.xyz[V_ARRAY]);
vertex = Utils.withValue(vertex, face.getAxis(), other);
mutable.positionv(Utils.convertFloat(vertex));
mutable.colouri(0xFF_FF_FF_FF);
mutable.texf(ri.uv[U_ARRAY], ri.uv[V_ARRAY]);
}
private static float[] getUVArray(TextureAtlasSprite sprite, int flips, EnumFacing face, Vec3 start, Vec3 end) {
Axis u = face.getAxis() == Axis.X ? Axis.Z : Axis.X;
Axis v = face.getAxis() == Axis.Y ? Axis.Z : Axis.Y;
float minU = sprite.getInterpolatedU(Utils.getValue(start, u) * 16);
float maxU = sprite.getInterpolatedU(Utils.getValue(end, u) * 16);
float minV = sprite.getInterpolatedV(Utils.getValue(start, v) * 16);
float maxV = sprite.getInterpolatedV(Utils.getValue(end, v) * 16);
float[] uvarray = new float[] { minU, maxU, minV, maxV };
if (flips % 2 == 1) {
float holder = uvarray[0];
uvarray[0] = uvarray[1];
uvarray[1] = holder;
}
if (flips >> 1 % 2 == 1) {
float holder = uvarray[2];
uvarray[2] = uvarray[3];
uvarray[3] = holder;
}
return uvarray;
}
private static List<RenderInfo> getRenderInfos(float[] uv, EnumFacing face, Vec3 size, Vec3 texSize, Vec3 texOffset) {
Axis u = face.getAxis() == Axis.X ? Axis.Z : Axis.X;
Axis v = face.getAxis() == Axis.Y ? Axis.Z : Axis.Y;
double sizeU = Utils.getValue(size, u);
double sizeV = Utils.getValue(size, v);
double textureSizeU = Utils.getValue(texSize, u);
double textureSizeV = Utils.getValue(texSize, v);
double textureOffsetU = Utils.getValue(texOffset, u);
double textureOffsetV = Utils.getValue(texOffset, v);
return getRenderInfos(uv, sizeU, sizeV, textureSizeU, textureSizeV, textureOffsetU, textureOffsetV);
}
/** A way to automatically generate the different positions given the same arguments.
*
* @param rotation TODO */
private static List<RenderInfo> getRenderInfos(float[] uv, double sizeU, double sizeV, double textureSizeU, double textureSizeV,
double textureOffsetU, double textureOffsetV) {
List<RenderInfo> infos = Lists.newArrayList();
boolean firstU = true;
for (double u = 0; u < sizeU; u += textureSizeU) {
float[] uvCu = Arrays.copyOf(uv, 4);
double addU = textureSizeU;
boolean lowerU = false;
// If there is an offset then make sure the texture positions are changed properly
if (firstU && textureOffsetU != 0) {
uvCu[U_MIN] = uvCu[U_MIN] + (uvCu[U_MAX] - uvCu[U_MIN]) * (float) textureOffsetU;
addU -= textureOffsetU;
// addU = 1 - textureOffsetU;
lowerU = true;
}
// If the size of the texture is greater than the cuboid goes on for then make sure the texture
// positions are lowered
if (u + addU > sizeU) {
addU = sizeU - u;
if (firstU && textureOffsetU != 0) {
uvCu[U_MAX] = uvCu[U_MIN] + (uvCu[U_MAX] - uvCu[U_MIN]) * (float) (addU / (textureSizeU - textureOffsetU));
} else {
uvCu[U_MAX] = uvCu[U_MIN] + (uvCu[U_MAX] - uvCu[U_MIN]) * (float) (addU / textureSizeU);
}
}
firstU = false;
boolean firstV = true;
for (double v = 0; v < sizeV; v += textureSizeV) {
float[] uvCv = Arrays.copyOf(uvCu, 4);
double addV = textureSizeV;
boolean lowerV = false;
if (firstV && textureOffsetV != 0) {
uvCv[V_MIN] = uvCv[V_MIN] + (uvCv[V_MAX] - uvCv[V_MIN]) * (float) textureOffsetV;
addV -= textureOffsetV;
lowerV = true;
}
if (v + addV > sizeV) {
addV = sizeV - v;
if (firstV && textureOffsetV != 0) {
uvCv[V_MAX] = uvCv[V_MIN] + (uvCv[V_MAX] - uvCv[V_MIN]) * (float) (addV / (textureSizeV - textureOffsetV));
} else {
uvCv[V_MAX] = uvCv[V_MIN] + (uvCv[V_MAX] - uvCv[V_MIN]) * (float) (addV / textureSizeV);
}
}
double[] xyz = new double[4];
xyz[U_MIN] = u;
xyz[U_MAX] = u + addU;
xyz[V_MIN] = v;
xyz[V_MAX] = v + addV;
infos.add(new RenderInfo(uvCv, xyz));
if (lowerV) {
v -= textureOffsetV;
}
firstV = false;
}
// If we lowered the U because the cuboid started on an offset, reset it back to what was actually
// rendered, not what the for loop assumes
if (lowerU) {
u -= textureOffsetU;
}
}
return infos;
}
private static final class RenderInfo {
private final float[] uv;
private final double[] xyz;
public RenderInfo(float[] uv, double[] xyz) {
this.uv = uv;
this.xyz = xyz;
}
public RenderInfo offset(Entity ent, Axis axis) {
switch (axis) {
case X: {
return new RenderInfo(uv, new double[] { xyz[0] + ent.posZ, xyz[1] + ent.posZ, xyz[2] + ent.posY, xyz[3] + ent.posY });
}
case Y: {
return new RenderInfo(uv, new double[] { xyz[0] + ent.posX, xyz[1] + ent.posX, xyz[2] + ent.posZ, xyz[3] + ent.posZ });
}
case Z: {
return new RenderInfo(uv, new double[] { xyz[0] + ent.posX, xyz[1] + ent.posX, xyz[2] + ent.posY, xyz[3] + ent.posY });
}
}
return new RenderInfo(uv, xyz);
}
}
}