-
Notifications
You must be signed in to change notification settings - Fork 497
/
PipeBlockModel.java
259 lines (214 loc) · 11.1 KB
/
PipeBlockModel.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
package buildcraft.transport.render;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import net.minecraft.block.state.IBlockState;
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.util.EnumFacing;
import net.minecraft.util.EnumFacing.Axis;
import net.minecraft.util.EnumFacing.AxisDirection;
import net.minecraft.util.EnumWorldBlockLayer;
import net.minecraft.util.Vec3;
import net.minecraftforge.client.MinecraftForgeClient;
import net.minecraftforge.client.model.ISmartBlockModel;
import net.minecraftforge.common.property.IExtendedBlockState;
import buildcraft.api.transport.pluggable.IPipePluggableStaticRenderer;
import buildcraft.api.transport.pluggable.IPipePluggableStaticRenderer.Translucent;
import buildcraft.api.transport.pluggable.PipePluggable;
import buildcraft.core.CoreConstants;
import buildcraft.core.lib.EntityResizableCuboid;
import buildcraft.core.lib.render.BuildCraftBakedModel;
import buildcraft.core.lib.render.RenderResizableCuboid;
import buildcraft.core.lib.utils.ColorUtils;
import buildcraft.core.lib.utils.Utils;
import buildcraft.transport.*;
import buildcraft.transport.TileGenericPipe.CoreState;
import buildcraft.transport.render.tile.PipeRendererWires;
public class PipeBlockModel extends BuildCraftBakedModel implements ISmartBlockModel {
public static final int INSIDE_COLOUR_MULT = 0xFF_AA_AA_AA;
public PipeBlockModel() {
super(ImmutableList.<BakedQuad> of(), null, null);
}
protected PipeBlockModel(ImmutableList<BakedQuad> quads, TextureAtlasSprite particle, VertexFormat format) {
super(quads, particle, format);
}
@Override
public ISmartBlockModel handleBlockState(IBlockState state) {
try {
return handle((IExtendedBlockState) state);
} catch (Throwable t) {
t.printStackTrace();
return defaultModel();
}
}
private static ISmartBlockModel defaultModel() {
return new PipeBlockModel();
}
public static ISmartBlockModel handle(IExtendedBlockState state) {
CoreState core = BlockGenericPipe.PIPE_CORE_STATE.getUnlistedValue(state);// Not required... :P
PipeRenderState render = BlockGenericPipe.PIPE_RENDER_STATE.getUnlistedValue(state);
PipePluggableState pluggable = BlockGenericPipe.PIPE_PLUGGABLE_STATE.getUnlistedValue(state);
Pipe<?> pipe = BlockGenericPipe.PIPE_PIPE.getUnlistedValue(state);
if (core == null || render == null || pluggable == null || pipe == null) {
return defaultModel();// Thats not good. Just return a cobblestone structure pipe centre model
}
List<BakedQuad> quads = Lists.newArrayList();
EnumWorldBlockLayer layer = MinecraftForgeClient.getRenderLayer();
if (layer == EnumWorldBlockLayer.CUTOUT) {
renderCutoutPass(render, pluggable, pipe, quads);
} else if (layer == EnumWorldBlockLayer.TRANSLUCENT) {
renderTranslucentPass(render, pluggable, pipe, quads);
}
TextureAtlasSprite particle = pipe.getIconProvider().getIcon(pipe.getIconIndex(null));
return new PipeBlockModel(ImmutableList.copyOf(quads), particle, DefaultVertexFormats.BLOCK);
}
private static void renderPipe(PipeRenderState render, List<BakedQuad> quads, Map<EnumFacing, TextureAtlasSprite> spriteMap, boolean smaller) {
float min = CoreConstants.PIPE_MIN_POS;
float max = CoreConstants.PIPE_MAX_POS;
float minUV = min * 16;
float maxUV = max * 16;
// Center bit
{
TextureAtlasSprite sprite = spriteMap.get(null);
float[] uvs = new float[4];
uvs[U_MIN] = sprite.getInterpolatedU(minUV);
uvs[U_MAX] = sprite.getInterpolatedU(maxUV);
uvs[V_MIN] = sprite.getInterpolatedV(minUV);
uvs[V_MAX] = sprite.getInterpolatedV(maxUV);
for (EnumFacing face : EnumFacing.VALUES) {
if (!render.pipeConnectionMatrix.isConnected(face) || !render.pipeConnectionBanned.isConnected(face)) {
Vec3 radius = Utils.vec3(0.25);
if (smaller) {
double smallerValue = Utils.getValue(radius, face.getAxis()) - 0.01f;
radius = Utils.withValue(radius, face.getAxis(), smallerValue);
}
List<BakedQuad> quadsIn = new ArrayList<>();
List<BakedQuad> quadsOut = new ArrayList<>();
bakeFace(quadsIn, face, Utils.vec3f(0.5f), Utils.convertFloat(radius), uvs);
bakeInverseFace(quadsOut, face, Utils.vec3f(0.5f), Utils.convertFloat(radius), uvs);
if (!shouldInvertForRender(face)) {
for (BakedQuad q : quadsIn) {
q = replaceShade(q, INSIDE_COLOUR_MULT);
quads.add(q);
}
quads.addAll(quadsOut);
} else {
quads.addAll(quadsIn);
for (BakedQuad q : quadsOut) {
q = replaceShade(q, INSIDE_COLOUR_MULT);
quads.add(q);
}
}
}
}
}
// All the connected bits
for (EnumFacing connect : EnumFacing.VALUES) {
if (render.pipeConnectionMatrix.isConnected(connect) && render.pipeConnectionBanned.isConnected(connect)) {
float extension = render.customConnections[connect.ordinal()];
TextureAtlasSprite sprite = spriteMap.get(connect);
Vec3 actualCenter = Utils.convert(connect, 0.375f + extension / 2).add(Utils.VEC_HALF);
Vec3 smallerFace = null;
if (connect.getAxisDirection() == AxisDirection.POSITIVE) {
smallerFace = Utils.convert(connect, 4 / 16d - extension);
} else {
smallerFace = Utils.convert(connect.getOpposite(), 4 / 16d - extension);
}
Vec3 actualSize = Utils.VEC_HALF.subtract(smallerFace);
if (smaller) {
// Decrease the entire size
Vec3 allSmaller = actualSize.subtract(Utils.vec3(0.02));
// Increase the size of axis the connection is in.
actualSize = allSmaller.add(Utils.convert(Utils.convertPositive(connect), 0.02));
}
Vec3 pos = actualCenter.subtract(Utils.multiply(actualSize, 1 / 2d));
EntityResizableCuboid cuboid = new EntityResizableCuboid(null);
cuboid.texture = sprite;
cuboid.makeClient();
double start = connect.getAxisDirection() == AxisDirection.POSITIVE ? 12 : 0;
cuboid.textureStartX = connect.getAxis() == Axis.X ? start : 4;
cuboid.textureStartY = connect.getAxis() == Axis.Y ? start : 4;
cuboid.textureStartZ = connect.getAxis() == Axis.Z ? start : 4;
cuboid.textureSizeX = connect.getAxis() == Axis.X ? 4 : 8;
cuboid.textureSizeY = connect.getAxis() == Axis.Y ? 4 : 8;
cuboid.textureSizeZ = connect.getAxis() == Axis.Z ? 4 : 8;
cuboid.textures[connect.ordinal()] = null;
cuboid.textures[connect.getOpposite().ordinal()] = null;
cuboid.setSize(actualSize);
cuboid.setPosition(pos.xCoord, pos.yCoord, pos.zCoord);
List<BakedQuad> quadsIn = new ArrayList<>();
RenderResizableCuboid.bakeCube(quadsIn, cuboid, false, true);
RenderResizableCuboid.bakeCube(quads, cuboid, true, false);
for (BakedQuad q : quadsIn) {
q = replaceShade(q, INSIDE_COLOUR_MULT);
quads.add(q);
}
}
}
}
// The main block model
private static void renderCutoutPass(PipeRenderState render, PipePluggableState pluggable, Pipe<?> pipe, List<BakedQuad> quads) {
Map<EnumFacing, TextureAtlasSprite> spriteMap = Maps.newHashMap();
for (EnumFacing face : EnumFacing.values()) {
spriteMap.put(face, pipe.getIconProvider().getIcon(render.textureMatrix.getTextureIndex(face)));
}
spriteMap.put(null, pipe.getIconProvider().getIcon(render.textureMatrix.getTextureIndex(null)));
renderPipe(render, quads, spriteMap, false);
// Pluggables
for (EnumFacing face : EnumFacing.VALUES) {
PipePluggable plug = pluggable.getPluggables()[face.ordinal()];
if (plug != null) {
IPipePluggableStaticRenderer plugRender = plug.getRenderer();
if (plugRender != null) {
List<BakedQuad> list = plugRender.bakeCutout(render, pluggable, pipe, plug, face);
if (list != null) {
quads.addAll(list);
}
}
}
}
// Wires
PipeRendererWires.renderPipeWires(quads, render);
}
// Used basically for pipe colour
private static void renderTranslucentPass(PipeRenderState render, PipePluggableState pluggable, Pipe<?> pipe, List<BakedQuad> quads) {
if (render.getGlassColor() >= 0 && render.getGlassColor() < 16) {
Map<EnumFacing, TextureAtlasSprite> spriteMap = Maps.newHashMap();
TextureAtlasSprite sprite = PipeIconProvider.TYPE.PipeStainedOverlay.getIcon();
for (EnumFacing face : EnumFacing.values()) {
spriteMap.put(face, sprite);
}
spriteMap.put(null, sprite);
// Grab the first index to apply shading to
int startIndex = quads.size();
renderPipe(render, quads, spriteMap, true);
int colour = ColorUtils.getRGBColor(render.getGlassColor());
for (int i = startIndex; i < quads.size(); i++) {
quads.get(i).getTintIndex();
BakedQuad shapeQuad = quads.get(i);
BakedQuad colouredQuad = new BakedQuad(shapeQuad.getVertexData(), colour, shapeQuad.getFace());
quads.set(i, colouredQuad);
}
}
// Pluggables
for (EnumFacing face : EnumFacing.VALUES) {
PipePluggable plug = pluggable.getPluggables()[face.ordinal()];
if (plug != null) {
IPipePluggableStaticRenderer plugRender = plug.getRenderer();
if (plugRender instanceof IPipePluggableStaticRenderer.Translucent) {
Translucent baker = (Translucent) plugRender;
List<BakedQuad> list = baker.bakeTranslucent(render, pluggable, pipe, plug, face);
if (list != null) {
quads.addAll(list);
}
}
}
}
}
}