-
Notifications
You must be signed in to change notification settings - Fork 497
/
PipeRendererWorld.java
334 lines (263 loc) · 11.4 KB
/
PipeRendererWorld.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
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.transport.render;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.init.Blocks;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.IBlockAccess;
import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.BuildCraftTransport;
import buildcraft.api.core.IIconProvider;
import buildcraft.core.CoreConstants;
import buildcraft.core.utils.MatrixTranformations;
import buildcraft.transport.BlockGenericPipe;
import buildcraft.transport.PipeIconProvider;
import buildcraft.transport.PipeRenderState;
import buildcraft.transport.TileGenericPipe;
import buildcraft.transport.TransportProxy;
public class PipeRendererWorld implements ISimpleBlockRenderingHandler {
public static int renderPass = -1;
public FakePluggablesStateBlock fakeBlock = new FakePluggablesStateBlock();
public void renderPipe(RenderBlocks renderblocks, IBlockAccess iblockaccess, BlockGenericPipe block, TileGenericPipe tile, int x, int y, int z) {
PipeRenderState state = tile.renderState;
IIconProvider icons = tile.getPipeIcons();
if (icons == null) {
return;
}
// Force pipe render into pass 0
if (renderPass == 0) {
int connectivity = state.pipeConnectionMatrix.getMask();
float[] dim = new float[6];
// render the unconnected pipe faces of the center block (if any)
if (connectivity != 0x3f) { // note: 0x3f = 0x111111 = all sides
resetToCenterDimensions(dim);
block.getTextureState().set(icons.getIcon(state.textureMatrix.getTextureIndex(ForgeDirection.UNKNOWN)));
renderTwoWayBlock(renderblocks, block, x, y, z, dim, connectivity ^ 0x3f);
}
// render the connecting pipe faces
for (int dir = 0; dir < 6; dir++) {
int mask = 1 << dir;
if ((connectivity & mask) == 0) {
continue; // no connection towards dir
}
// center piece offsets
resetToCenterDimensions(dim);
// extend block towards dir as it's connected to there
dim[dir / 2] = dir % 2 == 0 ? 0 : CoreConstants.PIPE_MAX_POS;
dim[dir / 2 + 3] = dir % 2 == 0 ? CoreConstants.PIPE_MIN_POS : 1;
// the mask points to all faces perpendicular to dir, i.e. dirs 0+1 -> mask 111100, 1+2 -> 110011, 3+5 -> 001111
int renderMask = (3 << (dir / 2 * 2)) ^ 0x3f;
// render sub block
block.getTextureState().set(icons.getIcon(state.textureMatrix.getTextureIndex(ForgeDirection.VALID_DIRECTIONS[dir])));
renderTwoWayBlock(renderblocks, block, x, y, z, dim, renderMask);
}
}
renderblocks.setRenderBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
// Facade renderer handles rendering in both passes
pipeFacadeRenderer(renderblocks, fakeBlock, state, x, y, z);
//block.setRenderAllSides();//Start fresh
// Force other opaque renders into pass 0
if (renderPass == 0) {
pipePlugRenderer(renderblocks, fakeBlock, state, x, y, z);
pipeRobotStationRenderer(renderblocks, fakeBlock, state, x, y, z);
}
}
private void resetToCenterDimensions(float[] dim) {
for (int i = 0; i < 3; i++) {
dim[i] = CoreConstants.PIPE_MIN_POS;
}
for (int i = 3; i < 6; i++) {
dim[i] = CoreConstants.PIPE_MAX_POS;
}
}
/**
* Render a block with normal and inverted vertex order so back face culling
* doesn't have any effect.
*/
private void renderTwoWayBlock(RenderBlocks renderblocks, BlockGenericPipe block, int x, int y, int z, float[] dim, int mask) {
assert mask != 0;
block.setRenderMask(mask);
renderblocks.setRenderBounds(dim[2], dim[0], dim[1], dim[5], dim[3], dim[4]);
renderblocks.renderStandardBlock(block, x, y, z);
block.setRenderMask((mask & 0x15) << 1 | (mask & 0x2a) >> 1); // pairwise swapped mask
renderblocks.setRenderBounds(dim[5], dim[3], dim[4], dim[2], dim[0], dim[1]);
renderblocks.renderStandardBlock(block, x, y, z);
}
private void pipeFacadeRenderer(RenderBlocks renderblocks, ITextureStates blockStateMachine, PipeRenderState state, int x, int y, int z) {
FacadeRenderHelper.pipeFacadeRenderer(renderblocks, blockStateMachine, state, x, y, z);
}
private void pipePlugRenderer(RenderBlocks renderblocks, ITextureStates blockStateMachine, PipeRenderState state, int x, int y, int z) {
float zFightOffset = 1F / 4096F;
float[][] zeroState = new float[3][2];
// X START - END
zeroState[0][0] = 0.25F + zFightOffset;
zeroState[0][1] = 0.75F - zFightOffset;
// Y START - END
zeroState[1][0] = 0.125F;
zeroState[1][1] = 0.251F;
// Z START - END
zeroState[2][0] = 0.25F + zFightOffset;
zeroState[2][1] = 0.75F - zFightOffset;
blockStateMachine.getTextureState().set(BuildCraftTransport.instance.pipeIconProvider.getIcon(PipeIconProvider.TYPE.PipeStructureCobblestone.ordinal())); // Structure Pipe
for (ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS) {
if (state.plugMatrix.isConnected(direction)) {
float[][] rotated = MatrixTranformations.deepClone(zeroState);
MatrixTranformations.transform(rotated, direction);
renderblocks.setRenderBounds(rotated[0][0], rotated[1][0], rotated[2][0], rotated[0][1], rotated[1][1], rotated[2][1]);
renderblocks.renderStandardBlock(blockStateMachine.getBlock(), x, y, z);
}
}
// X START - END
zeroState[0][0] = 0.25F + 0.125F / 2 + zFightOffset;
zeroState[0][1] = 0.75F - 0.125F / 2 + zFightOffset;
// Y START - END
zeroState[1][0] = 0.25F;
zeroState[1][1] = 0.25F + 0.125F;
// Z START - END
zeroState[2][0] = 0.25F + 0.125F / 2;
zeroState[2][1] = 0.75F - 0.125F / 2;
blockStateMachine.getTextureState().set(BuildCraftTransport.instance.pipeIconProvider.getIcon(PipeIconProvider.TYPE.PipeStructureCobblestone.ordinal())); // Structure Pipe
for (ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS) {
if (state.plugMatrix.isConnected(direction)) {
float[][] rotated = MatrixTranformations.deepClone(zeroState);
MatrixTranformations.transform(rotated, direction);
renderblocks.setRenderBounds(rotated[0][0], rotated[1][0], rotated[2][0], rotated[0][1], rotated[1][1], rotated[2][1]);
renderblocks.renderStandardBlock(blockStateMachine.getBlock(), x, y, z);
}
}
}
private void pipeRobotStationPartRender(RenderBlocks renderblocks,
ITextureStates blockStateMachine, PipeRenderState state, int x, int y, int z,
float xStart, float xEnd, float yStart, float yEnd, float zStart,
float zEnd) {
float zFightOffset = 1F / 4096F;
float[][] zeroState = new float[3][2];
// X START - END
zeroState[0][0] = xStart + zFightOffset;
zeroState[0][1] = xEnd - zFightOffset;
// Y START - END
zeroState[1][0] = yStart;
zeroState[1][1] = yEnd;
// Z START - END
zeroState[2][0] = zStart + zFightOffset;
zeroState[2][1] = zEnd - zFightOffset;
for (ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS) {
if (state.robotStationMatrix.isConnected(direction)) {
switch (state.robotStationMatrix.getState(direction)) {
case None:
case Available:
blockStateMachine.getTextureState().set(BuildCraftTransport.instance.pipeIconProvider
.getIcon(PipeIconProvider.TYPE.PipeRobotStation.ordinal()));
break;
case Reserved:
blockStateMachine.getTextureState().set(BuildCraftTransport.instance.pipeIconProvider
.getIcon(PipeIconProvider.TYPE.PipeRobotStationReserved.ordinal()));
break;
case Linked:
blockStateMachine.getTextureState().set(BuildCraftTransport.instance.pipeIconProvider
.getIcon(PipeIconProvider.TYPE.PipeRobotStationLinked.ordinal()));
break;
}
float[][] rotated = MatrixTranformations.deepClone(zeroState);
MatrixTranformations.transform(rotated, direction);
renderblocks.setRenderBounds(rotated[0][0], rotated[1][0],
rotated[2][0], rotated[0][1], rotated[1][1],
rotated[2][1]);
renderblocks.renderStandardBlock(blockStateMachine.getBlock(), x, y, z);
}
}
}
private void pipeRobotStationRenderer(RenderBlocks renderblocks, ITextureStates blockStateMachine, PipeRenderState state, int x, int y, int z) {
//float width = 0.075F;
pipeRobotStationPartRender (renderblocks, blockStateMachine, state, x, y, z,
0.45F, 0.55F,
0.0F, 0.224F,
0.45F, 0.55F);
/*pipeRobotStationPartRender (renderblocks, block, state, x, y, z,
0.25F, 0.75F,
0.025F, 0.224F,
0.25F, 0.25F + width);
pipeRobotStationPartRender (renderblocks, block, state, x, y, z,
0.25F, 0.75F,
0.025F, 0.224F,
0.75F - width, 0.75F);
pipeRobotStationPartRender (renderblocks, block, state, x, y, z,
0.25F, 0.25F + width,
0.025F, 0.224F,
0.25F + width, 0.75F - width);
pipeRobotStationPartRender (renderblocks, block, state, x, y, z,
0.75F - width, 0.75F,
0.025F, 0.224F,
0.25F + width, 0.75F - width);*/
float zFightOffset = 1F / 4096F;
float[][] zeroState = new float[3][2];
// X START - END
zeroState[0][0] = 0.25F + zFightOffset;
zeroState[0][1] = 0.75F - zFightOffset;
// Y START - END
zeroState[1][0] = 0.225F;
zeroState[1][1] = 0.251F;
// Z START - END
zeroState[2][0] = 0.25F + zFightOffset;
zeroState[2][1] = 0.75F - zFightOffset;
for (ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS) {
if (state.robotStationMatrix.isConnected(direction)) {
switch (state.robotStationMatrix.getState(direction)) {
case None:
case Available:
blockStateMachine.getTextureState().set(BuildCraftTransport.instance.pipeIconProvider
.getIcon(PipeIconProvider.TYPE.PipeRobotStation.ordinal()));
break;
case Reserved:
blockStateMachine.getTextureState().set(BuildCraftTransport.instance.pipeIconProvider
.getIcon(PipeIconProvider.TYPE.PipeRobotStationReserved.ordinal()));
break;
case Linked:
blockStateMachine.getTextureState().set(BuildCraftTransport.instance.pipeIconProvider
.getIcon(PipeIconProvider.TYPE.PipeRobotStationLinked.ordinal()));
break;
}
float[][] rotated = MatrixTranformations.deepClone(zeroState);
MatrixTranformations.transform(rotated, direction);
renderblocks.setRenderBounds(rotated[0][0], rotated[1][0],
rotated[2][0], rotated[0][1], rotated[1][1],
rotated[2][1]);
renderblocks.renderStandardBlock(blockStateMachine.getBlock(), x, y, z);
}
}
}
@Override
public void renderInventoryBlock(Block block, int metadata, int modelID, RenderBlocks renderer) {
// TODO Auto-generated method stub
}
@Override
public boolean renderWorldBlock(IBlockAccess world, int x, int y, int z, Block block, int modelId, RenderBlocks renderer) {
TileEntity tile = world.getTileEntity(x, y, z);
// Here to prevent Minecraft from crashing when nothing renders on render pass zero
// This is likely a bug, and has been submitted as an issue to the Forge team
renderer.setRenderBounds(0, 0, 0, 0, 0, 0);
renderer.renderStandardBlock(Blocks.stone, x, y, z);
renderer.setRenderBoundsFromBlock(block);
if (tile instanceof TileGenericPipe) {
TileGenericPipe pipeTile = (TileGenericPipe) tile;
renderPipe(renderer, world, (BlockGenericPipe) block, pipeTile, x, y, z);
}
return true;
}
@Override
public boolean shouldRender3DInInventory(int modelId) {
return false;
}
@Override
public int getRenderId() {
return TransportProxy.pipeModel;
}
}