-
Notifications
You must be signed in to change notification settings - Fork 497
/
PlugPluggableModel.java
91 lines (73 loc) · 3.64 KB
/
PlugPluggableModel.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
package buildcraft.transport.pluggable;
import java.util.List;
import javax.vecmath.Matrix4f;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
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.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.api.transport.IPipe;
import buildcraft.api.transport.pluggable.IPipePluggableState;
import buildcraft.api.transport.pluggable.IPipePluggableStaticRenderer;
import buildcraft.api.transport.pluggable.IPipeRenderState;
import buildcraft.api.transport.pluggable.PipePluggable;
import buildcraft.core.lib.render.BakedModelHolder;
import buildcraft.core.lib.render.PerspAwareModelBase;
import buildcraft.core.lib.utils.MatrixUtils;
public class PlugPluggableModel extends BakedModelHolder implements IPipePluggableStaticRenderer {
public static final PlugPluggableModel INSTANCE = new PlugPluggableModel();
private static final ResourceLocation plugLoc = new ResourceLocation("buildcrafttransport:models/blocks/pluggables/plug.obj");
private static final ResourceLocation plugSpriteLoc = new ResourceLocation("buildcrafttransport:pipes/plug");
private static TextureAtlasSprite spritePlug;
private PlugPluggableModel() {}
public static PerspAwareModelBase create() {
ImmutableList.Builder<BakedQuad> quads = ImmutableList.builder();
VertexFormat format = DefaultVertexFormats.ITEM;
quads.addAll(INSTANCE.bakeCutout(EnumFacing.SOUTH, format));
return new PerspAwareModelBase(format, quads.build(), spritePlug, getPluggableTransforms());
}
public IModel modelPlug() {
return getModelOBJ(plugLoc);
}
@SubscribeEvent
public void textureStitch(TextureStitchEvent.Pre event) {
spritePlug = null;
spritePlug = event.map.getTextureExtry(plugSpriteLoc.toString());
if (spritePlug == null) spritePlug = event.map.registerSprite(plugSpriteLoc);
}
@Override
public List<BakedQuad> bakeCutout(IPipeRenderState render, IPipePluggableState pluggableState, IPipe pipe, PipePluggable pluggable,
EnumFacing face) {
return bakeCutout(face, DefaultVertexFormats.BLOCK);
}
private List<BakedQuad> bakeCutout(EnumFacing face, VertexFormat format) {
IModel model = modelPlug();
TextureAtlasSprite sprite = spritePlug;
List<BakedQuad> quads = Lists.newArrayList();
List<BakedQuad> bakedQuads = renderPlug(model, sprite, format);
Matrix4f matrix = MatrixUtils.rotateTowardsFace(face);
for (BakedQuad quad : bakedQuads) {
quad = transform(quad, matrix);
// quad = applyDiffuse(quad);
quads.add(quad);
}
return quads;
}
public static List<BakedQuad> renderPlug(IModel model, TextureAtlasSprite sprite, VertexFormat format) {
List<BakedQuad> quads = Lists.newArrayList();
IFlexibleBakedModel baked = model.bake(ModelRotation.X0_Y0, format, singleTextureFunction(sprite));
for (BakedQuad quad : baked.getGeneralQuads()) {
quad = replaceShade(quad, 0xFFFFFFFF);
quads.add(quad);
}
return quads;
}
}