-
Notifications
You must be signed in to change notification settings - Fork 497
/
RobotStationModel.java
147 lines (125 loc) · 6.28 KB
/
RobotStationModel.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
package buildcraft.robotics.render;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.vecmath.Matrix4f;
import javax.vecmath.Vector3f;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import net.minecraft.client.renderer.block.model.BakedQuad;
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.resources.model.ModelRotation;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.client.model.IFlexibleBakedModel;
import net.minecraftforge.client.model.IModel;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import buildcraft.BuildCraftCore;
import buildcraft.api.core.BCLog;
import buildcraft.api.transport.pluggable.IPluggableModelBaker;
import buildcraft.core.lib.client.model.*;
import buildcraft.core.lib.utils.MatrixUtils;
import buildcraft.robotics.RobotStationPluggable.EnumRobotStationState;
public class RobotStationModel extends BakedModelHolder implements IPluggableModelBaker<ModelKeyStation> {
public static final RobotStationModel INSTANCE = new RobotStationModel();
private static final ResourceLocation baseLoc = new ResourceLocation("buildcraftrobotics:models/pluggables/robot_station_base.obj");
private TextureAtlasSprite baseSprite;
private final Map<EnumRobotStationState, TextureAtlasSprite> stateSprites = Maps.newEnumMap(EnumRobotStationState.class);
private final Map<EnumRobotStationState, List<MutableQuad>> stateQuads = Maps.newEnumMap(EnumRobotStationState.class);
private final List<MutableQuad> modelBaseQuads = new ArrayList<>();
private IModel modelBase() {
return getModelOBJ(baseLoc);
}
public PerspAwareModelBase createItemModel() {
ImmutableList.Builder<BakedQuad> quads = ImmutableList.builder();
VertexFormat format = DefaultVertexFormats.ITEM;
quads.addAll(INSTANCE.bakeCutout(EnumRobotStationState.Available, EnumFacing.SOUTH, format));
return new PerspAwareModelBase(format, quads.build(), baseSprite, getPluggableTransforms());
}
@SubscribeEvent
public void textureStitchPre(TextureStitchEvent.Pre pre) {
TextureMap map = pre.map;
baseSprite = null;
baseSprite = map.getTextureExtry("buildcraftrobotics:station/base");
if (baseSprite == null) baseSprite = map.registerSprite(new ResourceLocation("buildcraftrobotics:station/base"));
for (final EnumRobotStationState state : EnumRobotStationState.values()) {
String suffix = state.getTextureSuffix() + (BuildCraftCore.colorBlindMode ? "_cb" : "");
ResourceLocation location = new ResourceLocation("buildcraftrobotics:station/state_" + suffix);
TextureAtlasSprite sprite = map.getTextureExtry(location.toString());
if (sprite == null) sprite = map.registerSprite(location);
stateSprites.put(state, sprite);
}
}
@SubscribeEvent
public void textureStitchPost(TextureStitchEvent.Post post) {
for (final EnumRobotStationState state : EnumRobotStationState.values()) {
Matrix4f translation = new Matrix4f();
translation.setIdentity();
translation.setTranslation(new Vector3f(2.8f / 16f, 0, 0));
List<MutableQuad> quads = Lists.newArrayList();
for (MutableQuad mutable : BuildCraftBakedModel.createQuadsItemLayer(stateSprites.get(state))) {
mutable.transform(translation);
mutable.colouri(0xFF_FF_FF_FF);
quads.add(mutable);
}
stateQuads.put(state, quads);
}
}
@Override
public VertexFormat getVertexFormat() {
return DefaultVertexFormats.BLOCK;
}
@Override
public ImmutableList<BakedQuad> bake(ModelKeyStation key) {
return ImmutableList.copyOf(bakeCutout(key.state, key.side, getVertexFormat()));
}
private List<MutableQuad> baseQuads() {
if (modelBaseQuads.isEmpty()) {
IModel base = modelBase();
if (base != null) {
Function<ResourceLocation, TextureAtlasSprite> singleTextureFunction = BuildCraftBakedModel.singleTextureFunction(baseSprite);
IFlexibleBakedModel baked = base.bake(ModelRotation.X0_Y0, DefaultVertexFormats.BLOCK, singleTextureFunction);
for (BakedQuad quad : baked.getGeneralQuads()) {
MutableQuad mutable = MutableQuad.create(quad);
modelBaseQuads.add(mutable);
}
}
}
return modelBaseQuads;
}
private List<BakedQuad> bakeCutout(EnumRobotStationState state, EnumFacing face, VertexFormat format) {
IModel base = modelBase();
List<MutableQuad> stateQuads = this.stateQuads.get(state);
List<BakedQuad> quads = Lists.newArrayList();
if (base != null) {
Matrix4f matrix = MatrixUtils.rotateTowardsFace(face);
for (MutableQuad mutable : baseQuads()) {
mutable = new MutableQuad(mutable);
mutable.transform(matrix);
Vector3f normal = mutable.getCalculatedNormal();
mutable.normalv(normal);
float diffuse = MutableQuad.diffuseLight(normal);
mutable.colourf(diffuse, diffuse, diffuse, 1);
BCModelHelper.appendBakeQuads(quads, format, mutable);
}
if (stateQuads != null) {
for (MutableQuad mutable : stateQuads) {
mutable = new MutableQuad(mutable);
mutable.transform(matrix);
Vector3f normal = mutable.getCalculatedNormal();
mutable.normalv(normal);
float diffuse = MutableQuad.diffuseLight(normal);
mutable.colourf(diffuse, diffuse, diffuse, 1);
BCModelHelper.appendBakeQuads(quads, format, mutable);
}
}
}
return quads;
}
}