-
Notifications
You must be signed in to change notification settings - Fork 79
/
ModelUtil.java
574 lines (487 loc) · 13.8 KB
/
ModelUtil.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
package mod.chiselsandbits.render.helpers;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import mod.chiselsandbits.chiseledblock.BlockBitInfo;
import mod.chiselsandbits.core.ChiselsAndBits;
import mod.chiselsandbits.core.ReflectionWrapper;
import mod.chiselsandbits.helpers.DeprecationHelper;
import mod.chiselsandbits.helpers.ModUtil;
import mod.chiselsandbits.interfaces.ICacheClearable;
import mod.chiselsandbits.render.helpers.ModelQuadLayer.ModelQuadLayerBuilder;
import net.minecraft.block.Block;
import net.minecraft.block.BlockLeaves;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.block.model.IBakedModel;
import net.minecraft.client.renderer.block.model.ItemOverrideList;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumFacing.Axis;
import net.minecraftforge.client.ForgeHooksClient;
import net.minecraftforge.fluids.Fluid;
@SuppressWarnings( "unchecked" )
public class ModelUtil implements ICacheClearable
{
private final static HashMap<Integer, String> blockToTexture[];
private static HashMap<Integer, ModelQuadLayer[]> cache = new HashMap<Integer, ModelQuadLayer[]>();
@SuppressWarnings( "unused" )
private static ModelUtil instance = new ModelUtil();
static
{
blockToTexture = new HashMap[EnumFacing.VALUES.length * BlockRenderLayer.values().length];
for ( int x = 0; x < blockToTexture.length; x++ )
{
blockToTexture[x] = new HashMap<Integer, String>();
}
}
@Override
public void clearCache()
{
for ( int x = 0; x < blockToTexture.length; x++ )
{
blockToTexture[x].clear();
}
cache.clear();
}
public static ModelQuadLayer[] getCachedFace(
final int stateID,
final long weight,
final EnumFacing face,
final BlockRenderLayer layer )
{
if ( layer == null )
{
return null;
}
final int cacheVal = stateID << 6 | layer.ordinal() << 4 | face.ordinal();
final ModelQuadLayer[] mpc = cache.get( cacheVal );
if ( mpc != null )
{
return mpc;
}
final BlockRenderLayer original = net.minecraftforge.client.MinecraftForgeClient.getRenderLayer();
try
{
ForgeHooksClient.setRenderLayer( layer );
return getInnerCachedFace( cacheVal, stateID, weight, face, layer );
}
finally
{
// restore previous layer.
ForgeHooksClient.setRenderLayer( original );
}
}
private static ModelQuadLayer[] getInnerCachedFace(
final int cacheVal,
final int stateID,
final long weight,
final EnumFacing face,
final BlockRenderLayer layer )
{
final IBlockState state = ModUtil.getStateById( stateID );
final IBakedModel model = ModelUtil.solveModel( state, weight, Minecraft.getMinecraft().getBlockRendererDispatcher().getBlockModelShapes().getModelForState( state ), layer );
final Fluid fluid = BlockBitInfo.getFluidFromBlock( state.getBlock() );
if ( fluid != null )
{
for ( final EnumFacing xf : EnumFacing.VALUES )
{
final ModelQuadLayer[] mp = new ModelQuadLayer[1];
mp[0] = new ModelQuadLayer();
mp[0].color = fluid.getColor();
mp[0].light = DeprecationHelper.getLightValue( state );
final float V = 0.5f;
final float Uf = 1.0f;
final float U = 0.5f;
final float Vf = 1.0f;
if ( xf.getAxis() == Axis.Y )
{
mp[0].sprite = Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite( fluid.getStill().toString() );
mp[0].uvs = new float[] { Uf, Vf, 0, Vf, Uf, 0, 0, 0 };
}
else if ( xf.getAxis() == Axis.X )
{
mp[0].sprite = Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite( fluid.getFlowing().toString() );
mp[0].uvs = new float[] { U, 0, U, V, 0, 0, 0, V };
}
else
{
mp[0].sprite = Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite( fluid.getFlowing().toString() );
mp[0].uvs = new float[] { U, 0, 0, 0, U, V, 0, V };
}
mp[0].tint = 0;
final int cacheV = stateID << 6 | layer.ordinal() << 4 | face.ordinal();
cache.put( cacheV, mp );
}
return cache.get( cacheVal );
}
final HashMap<EnumFacing, ArrayList<ModelQuadLayerBuilder>> tmp = new HashMap<EnumFacing, ArrayList<ModelQuadLayerBuilder>>();
final int color = BlockBitInfo.getColorFor( state, 0 );
for ( final EnumFacing f : EnumFacing.VALUES )
{
tmp.put( f, new ArrayList<ModelQuadLayer.ModelQuadLayerBuilder>() );
}
if ( model != null )
{
for ( final EnumFacing f : EnumFacing.VALUES )
{
final List<BakedQuad> quads = ModelUtil.getModelQuads( model, state, f, 0 );
processFaces( tmp, quads, state );
}
processFaces( tmp, ModelUtil.getModelQuads( model, state, null, 0 ), state );
}
for ( final EnumFacing f : EnumFacing.VALUES )
{
final int cacheV = stateID << 6 | layer.ordinal() << 4 | face.ordinal();
final ArrayList<ModelQuadLayerBuilder> x = tmp.get( f );
final ModelQuadLayer[] mp = new ModelQuadLayer[x.size()];
for ( int z = 0; z < x.size(); z++ )
{
mp[z] = x.get( z ).build( stateID, color, DeprecationHelper.getLightValue( state ), state.getBlock() == Blocks.GRASS || state.getBlock() instanceof BlockLeaves );
}
cache.put( cacheV, mp );
}
return cache.get( cacheVal );
}
private static List<BakedQuad> getModelQuads(
final IBakedModel model,
final IBlockState state,
final EnumFacing f,
final long rand )
{
try
{
// try to get block model...
return model.getQuads( state, f, rand );
}
catch ( final Throwable t )
{
}
try
{
// try to get item model?
return model.getQuads( null, f, rand );
}
catch ( final Throwable t )
{
}
final IBakedModel secondModel = getOverrides( model ).handleItemState( model, ModUtil.getItemFromBlock( state ), Minecraft.getMinecraft().theWorld, Minecraft.getMinecraft().thePlayer );
if ( secondModel != null )
{
try
{
return secondModel.getQuads( null, f, rand );
}
catch ( final Throwable t )
{
}
}
// try to not crash...
return Collections.emptyList();
}
private static ItemOverrideList getOverrides(
final IBakedModel model )
{
if ( model != null )
{
final ItemOverrideList modelOverrides = model.getOverrides();
return modelOverrides == null ? ItemOverrideList.NONE : modelOverrides;
}
return ItemOverrideList.NONE;
}
private static void processFaces(
final HashMap<EnumFacing, ArrayList<ModelQuadLayerBuilder>> tmp,
final List<BakedQuad> quads,
final IBlockState state )
{
for ( final BakedQuad q : quads )
{
final EnumFacing face = q.getFace();
if ( face == null )
{
continue;
}
try
{
final TextureAtlasSprite sprite = findQuadTexture( q, state );
final ArrayList<ModelQuadLayerBuilder> l = tmp.get( face );
ModelQuadLayerBuilder b = null;
for ( final ModelQuadLayerBuilder lx : l )
{
if ( lx.cache.sprite == sprite )
{
b = lx;
break;
}
}
if ( b == null )
{
// top/bottom
int uCoord = 0;
int vCoord = 2;
switch ( face )
{
case NORTH:
case SOUTH:
uCoord = 0;
vCoord = 1;
break;
case EAST:
case WEST:
uCoord = 1;
vCoord = 2;
break;
default:
}
b = new ModelQuadLayerBuilder( sprite, uCoord, vCoord );
b.cache.tint = q.getTintIndex();
l.add( b );
}
q.pipe( b.uvr );
q.pipe( b.lv );
}
catch ( final Exception e )
{
}
}
}
private ModelUtil()
{
ChiselsAndBits.getInstance().addClearable( this );
}
public static TextureAtlasSprite findQuadTexture(
final BakedQuad q,
final IBlockState state ) throws IllegalArgumentException, IllegalAccessException, NullPointerException
{
final TextureMap map = Minecraft.getMinecraft().getTextureMapBlocks();
final Map<String, TextureAtlasSprite> mapRegisteredSprites = ReflectionWrapper.instance.getRegSprite( map );
if ( mapRegisteredSprites == null )
{
throw new RuntimeException( "Unable to lookup textures." );
}
final ModelUVAverager av = new ModelUVAverager();
q.pipe( av );
final float U = av.getU();
final float V = av.getV();
final Iterator<?> iterator1 = mapRegisteredSprites.values().iterator();
while ( iterator1.hasNext() )
{
final TextureAtlasSprite sprite = (TextureAtlasSprite) iterator1.next();
if ( sprite.getMinU() <= U && U <= sprite.getMaxU() && sprite.getMinV() <= V && V <= sprite.getMaxV() )
{
return sprite;
}
}
TextureAtlasSprite texture = null;
try
{
if ( q.getSprite() != null )
{
texture = q.getSprite();
}
}
catch ( final Exception e )
{
}
if ( isMissing( texture ) && state != null )
{
try
{
texture = Minecraft.getMinecraft().getBlockRendererDispatcher().getBlockModelShapes().getTexture( state );
}
catch ( final Exception err )
{
}
}
if ( texture == null )
{
return Minecraft.getMinecraft().getTextureMapBlocks().getMissingSprite();
}
return texture;
}
public static IBakedModel solveModel(
final IBlockState state,
final long weight,
final IBakedModel originalModel,
final BlockRenderLayer layer )
{
boolean hasFaces = false;
try
{
hasFaces = hasFaces( originalModel, state, null, weight );
for ( final EnumFacing f : EnumFacing.VALUES )
{
hasFaces = hasFaces || hasFaces( originalModel, state, f, weight );
}
}
catch ( final Exception e )
{
// an exception was thrown.. use the item model and hope...
hasFaces = false;
}
if ( !hasFaces )
{
// if the model is empty then lets grab an item and try that...
final ItemStack is = ModUtil.getItemFromBlock( state );
if ( is != null )
{
final IBakedModel itemModel = Minecraft.getMinecraft().getRenderItem().getItemModelWithOverrides( is, Minecraft.getMinecraft().theWorld, Minecraft.getMinecraft().thePlayer );
try
{
hasFaces = hasFaces( originalModel, state, null, weight );
for ( final EnumFacing f : EnumFacing.VALUES )
{
hasFaces = hasFaces || hasFaces( originalModel, state, f, weight );
}
}
catch ( final Exception e )
{
// an exception was thrown.. use the item model and hope...
hasFaces = false;
}
if ( hasFaces )
{
return itemModel;
}
else
{
return new SimpleGeneratedModel( findTexture( Block.getStateId( state ), originalModel, EnumFacing.UP, layer ) );
}
}
}
return originalModel;
}
private static boolean hasFaces(
final IBakedModel model,
final IBlockState state,
final EnumFacing f,
final long weight )
{
final List<BakedQuad> l = getModelQuads( model, state, f, weight );
if ( l == null || l.isEmpty() )
{
return false;
}
TextureAtlasSprite texture = null;
try
{
texture = findTexture( null, l, f );
}
catch ( final Exception e )
{
}
final ModelVertexRange mvr = new ModelVertexRange();
for ( final BakedQuad q : l )
{
q.pipe( mvr );
}
return mvr.getLargestRange() > 0 && !isMissing( texture );
}
private static boolean isMissing(
final TextureAtlasSprite texture )
{
return texture == null || texture == Minecraft.getMinecraft().getTextureMapBlocks().getMissingSprite();
}
public static TextureAtlasSprite findTexture(
final int BlockRef,
final IBakedModel model,
final EnumFacing myFace,
final BlockRenderLayer layer )
{
final int blockToWork = layer.ordinal() * EnumFacing.VALUES.length + myFace.ordinal();
// didn't work? ok lets try scanning for the texture in the
if ( blockToTexture[blockToWork].containsKey( BlockRef ) )
{
final String textureName = blockToTexture[blockToWork].get( BlockRef );
return Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite( textureName );
}
TextureAtlasSprite texture = null;
final IBlockState state = ModUtil.getStateById( BlockRef );
if ( model != null )
{
try
{
texture = findTexture( texture, getModelQuads( model, state, myFace, 0 ), myFace );
if ( texture == null )
{
for ( final EnumFacing side : EnumFacing.VALUES )
{
texture = findTexture( texture, getModelQuads( model, state, side, 0 ), side );
}
texture = findTexture( texture, getModelQuads( model, state, null, 0 ), null );
}
}
catch ( final Exception errr )
{
}
}
// who knows if that worked.. now lets try to get a texture...
if ( isMissing( texture ) )
{
try
{
if ( model != null )
{
texture = model.getParticleTexture();
}
}
catch ( final Exception err )
{
}
}
if ( isMissing( texture ) )
{
try
{
texture = Minecraft.getMinecraft().getBlockRendererDispatcher().getBlockModelShapes().getTexture( state );
}
catch ( final Exception err )
{
}
}
if ( texture == null )
{
texture = Minecraft.getMinecraft().getTextureMapBlocks().getMissingSprite();
}
blockToTexture[blockToWork].put( BlockRef, texture.getIconName() );
return texture;
}
private static TextureAtlasSprite findTexture(
TextureAtlasSprite texture,
final List<BakedQuad> faceQuads,
final EnumFacing myFace ) throws IllegalArgumentException, IllegalAccessException, NullPointerException
{
for ( final BakedQuad q : faceQuads )
{
if ( q.getFace() == myFace )
{
texture = findQuadTexture( q, null );
}
}
return texture;
}
public static boolean isOne(
final float v )
{
return Math.abs( v ) < 0.01;
}
public static boolean isZero(
final float v )
{
return Math.abs( v - 1.0f ) < 0.01;
}
public static Integer getItemStackColor(
final ItemStack target,
final int tint )
{
return Minecraft.getMinecraft().getItemColors().getColorFromItemstack( target, tint );
}
}