-
Notifications
You must be signed in to change notification settings - Fork 382
Expand file tree
/
Copy pathGTOverheatParticle.java
More file actions
307 lines (274 loc) · 10.5 KB
/
GTOverheatParticle.java
File metadata and controls
307 lines (274 loc) · 10.5 KB
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
package com.gregtechceu.gtceu.client.particle;
import com.gregtechceu.gtceu.client.bloom.EffectRenderContext;
import com.gregtechceu.gtceu.client.bloom.IRenderSetup;
import com.gregtechceu.gtceu.client.bloom.particle.GTBloomParticle;
import com.gregtechceu.gtceu.client.util.RenderBufferHelper;
import com.gregtechceu.gtceu.client.util.RenderUtil;
import com.gregtechceu.gtceu.common.blockentity.CableBlockEntity;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.*;
/**
* @author brachy84
*/
public class GTOverheatParticle extends GTBloomParticle {
/**
* <a href="http://www.vendian.org/mncharity/dir3/blackbody/">Source</a>
*/
private static final int[] blackBodyColors = {
0xFF3300, // 1000 K
0xFF5300, // 1200 K
0xFF6500, // 1400 K
0xFF7300, // 1600 K
0xFF7E00, // 1800 K
0xFF8912, // 2000 K
0xFF932C, // 2200 K
0xFF9D3F, // 2400 K
0xffa54f, // 2600 K
0xffad5e, // 2800 K
0xffb46b, // 3000 K
0xffbb78, // 3200 K
0xffc184, // 3400 K
0xffc78f, // 3600 K
0xffcc99, // 3800 K
0xffd1a3, // 4000 K
0xffd5ad, // 4200 K
0xffd9b6, // 4400 K
0xffddbe, // 4600 K
0xffe1c6, // 4800 K
0xffe4ce, // 5000 K
0xffe8d5, // 5200 K
0xffebdc, // 5400 K
0xffeee3, // 5600 K
0xfff0e9, // 5800 K
0xfff3ef, // 6000 K
0xfff5f5, // 6200 K
0xfff8fb, // 6400 K
0xfef9ff, // 6600 K
0xf9f6ff, // 6800 K
0xf5f3ff, // 7000 K
0xf0f1ff, // 7200 K
0xedefff, // 7400 K
0xe9edff, // 7600 K
0xe6ebff, // 7800 K
0xe3e9ff, // 8000 K
0xe0e7ff, // 8200 K
0xdde6ff, // 8400 K
0xdae4ff, // 8600 K
0xd8e3ff, // 8800 K
0xd6e1ff, // 9000 K
0xd3e0ff, // 9200 K
0xd1dfff, // 9400 K
0xcfddff, // 9600 K
0xcedcff, // 9800 K
0xccdbff, // 10000 K
0xcadaff, // 10200 K
0xc9d9ff, // 10400 K
0xc7d8ff, // 10600 K
0xc6d8ff, // 10800 K
0xc4d7ff, // 11000 K
0xc3d6ff, // 11200 K
0xc2d5ff, // 11400 K
0xc1d4ff, // 11600 K
0xc0d4ff, // 11800 K
0xbfd3ff, // 12000 K
0xbed2ff, // 12200 K
0xbdd2ff, // 12400 K
0xbcd1ff, // 12600 K
0xbbd1ff, // 12800 K
0xbad0ff, // 13000 K
0xb9d0ff, // 13200 K
0xb8cfff, // 13400 K
0xb7cfff, // 13600 K
0xb7ceff, // 13800 K
0xb6ceff, // 14000 K
0xb5cdff, // 14200 K
0xb5cdff, // 14400 K
0xb4ccff, // 14600 K
0xb3ccff, // 14800 K
0xb3ccff, // 15000 K
0xb2cbff, // 15200 K
0xb2cbff, // 15400 K
0xb1caff, // 15600 K
0xb1caff, // 15800 K
0xb0caff, // 16000 K
0xafc9ff, // 16200 K
0xafc9ff, // 16400 K
0xafc9ff, // 16600 K
0xaec9ff, // 16800 K
0xaec8ff, // 17000 K
0xadc8ff, // 17200 K
0xadc8ff, // 17400 K
0xacc7ff, // 17600 K
0xacc7ff, // 17800 K
0xacc7ff, // 18000 K
0xabc7ff, // 18200 K
0xabc6ff, // 18400 K
0xaac6ff, // 18600 K
0xaac6ff, // 18800 K
0xaac6ff, // 19000 K
0xa9c6ff, // 19200 K
0xa9c5ff, // 19400 K
0xa9c5ff, // 19600 K
0xa9c5ff, // 19800 K
0xa8c5ff, // 20000 K
// color doesn't really change onwards
};
public static int getBlackBodyColor(int temperature) {
if (temperature < 1000)
return blackBodyColors[0];
int index = (temperature - 1000) / 200;
if (index >= blackBodyColors.length - 1)
return blackBodyColors[blackBodyColors.length - 1];
int color = blackBodyColors[index];
return RenderUtil.interpolateColor(color, blackBodyColors[index + 1], temperature % 200 / 200f);
}
private final CableBlockEntity blockEntity;
private final int meltTemp;
private int temperature;
private final boolean insulated;
private VoxelShape pipeShape;
private AABB pipeBounds;
private float alpha = 0.0f;
private int color = blackBodyColors[0];
public GTOverheatParticle(CableBlockEntity blockEntity, int meltTemp, boolean insulated) {
super(blockEntity.getBlockPos().getX(), blockEntity.getBlockPos().getY(), blockEntity.getBlockPos().getZ());
this.blockEntity = blockEntity;
this.meltTemp = meltTemp;
this.setTemperature(blockEntity.getTemperature());
this.insulated = insulated;
this.pipeShape = blockEntity.getBlockState().getVisualShape(blockEntity.getLevel(), blockEntity.getBlockPos(),
CollisionContext.empty());
this.pipeBounds = pipeShape.bounds().inflate(0.001).move(posX, posY, posZ);
}
public void setTemperature(int temperature) {
this.temperature = temperature;
if (temperature <= blockEntity.getDefaultTemp() || temperature > meltTemp) {
setExpired();
return;
}
if (temperature < 300) {
alpha = 0.0f;
} else if (temperature < 600) {
alpha = 0.16f * (temperature - 300f) / 300f;
} else if (temperature < 1000) {
alpha = 0.8f * (temperature - 500f) / 500f;
} else {
alpha = 0.8f;
}
color = getBlackBodyColor(temperature);
}
@Override
public void onUpdate() {
// if this isn't the block entity's particle, remove both
if (blockEntity.isRemoved() || !blockEntity.isParticleAlive()) {
setExpired();
blockEntity.killParticle();
return;
}
// update pipeShape every tick so it doesn't desync if the pipe is disconnected
pipeShape = blockEntity.getBlockState().getVisualShape(blockEntity.getLevel(), blockEntity.getBlockPos(),
CollisionContext.empty());
pipeBounds = pipeShape.bounds().inflate(0.001).move(posX, posY, posZ);
if (temperature > 400 && blockEntity.getLevel().random.nextFloat() < 0.04f) {
spawnSmoke();
}
}
private void spawnSmoke() {
BlockPos pos = blockEntity.getBlockPos();
float xPos = pos.getX() + 0.5F;
float yPos = pos.getY() + 0.9F;
float zPos = pos.getZ() + 0.5F;
float ySpd = 0.3F + 0.1F * blockEntity.getLevel().random.nextFloat();
blockEntity.getLevel().addParticle(ParticleTypes.LARGE_SMOKE, xPos, yPos, zPos, 0, ySpd, 0);
}
@Override
public String toString() {
return "GTOverheatParticle{" +
"blockEntity=" + blockEntity +
", pipeShape=" + pipeShape +
", insulated=" + insulated +
", alpha=" + alpha +
", color=" + color +
'}';
}
@Override
public boolean shouldRender(EffectRenderContext context) {
return this.shouldRenderBloomEffect(context);
}
@Override
public boolean shouldRenderBloomEffect(EffectRenderContext context) {
if (this.insulated) return false;
return context.frustum().isVisible(pipeBounds);
}
@Override
public IRenderSetup getRenderSetup() {
return NO_BLOOM_SETUP;
}
@Override
protected IRenderSetup getBloomRenderSetup() {
return BLOOM_SETUP;
}
@Override
public void renderParticle(PoseStack poseStack, BufferBuilder buffer, EffectRenderContext context) {
renderBloomEffect(poseStack, buffer, context);
}
@Override
public void renderBloomEffect(PoseStack poseStack, BufferBuilder buffer, EffectRenderContext context) {
float red = ((color >> 16) & 0xFF) / 255f;
float green = ((color >> 8) & 0xFF) / 255f;
float blue = (color & 0xFF) / 255f;
poseStack.pushPose();
poseStack.translate(posX, posY, posZ);
pipeShape.forAllBoxes((x1, y1, z1, x2, y2, z2) -> {
RenderBufferHelper.renderColorCube(poseStack, buffer,
(float) x1 - 0.001f, (float) y1 - 0.001f, (float) z1 - 0.001f,
(float) x2 + 0.001f, (float) y2 + 0.001f, (float) z2 + 0.001f,
red, green, blue, alpha, true);
});
poseStack.popPose();
}
private static final IRenderSetup NO_BLOOM_SETUP = new IRenderSetup() {
@Override
@OnlyIn(Dist.CLIENT)
public void preDraw(BufferBuilder buffer) {
RenderSystem.enableBlend();
RenderSystem.blendFuncSeparate(
GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA,
GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA);
RenderSystem.setShader(GameRenderer::getPositionColorShader);
RenderSystem.setShaderColor(1, 1, 1, 1);
buffer.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR);
}
@Override
@OnlyIn(Dist.CLIENT)
public void postDraw(BufferBuilder buffer) {
BufferUploader.drawWithShader(buffer.end());
RenderSystem.disableBlend();
RenderSystem.defaultBlendFunc();
}
};
private static final IRenderSetup BLOOM_SETUP = new IRenderSetup() {
@Override
@OnlyIn(Dist.CLIENT)
public void preDraw(BufferBuilder buffer) {
RenderSystem.disableBlend();
RenderSystem.setShader(GameRenderer::getPositionColorShader);
RenderSystem.setShaderColor(1, 1, 1, 1);
buffer.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR);
}
@Override
@OnlyIn(Dist.CLIENT)
public void postDraw(BufferBuilder buffer) {
BufferUploader.drawWithShader(buffer.end());
}
};
}