Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d25ad19
commit 0b0e103
Showing
1 changed file
with
103 additions
and
0 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
src/main/java/mod/chiselsandbits/render/cache/FormatInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package mod.chiselsandbits.render.cache; | ||
|
||
import net.minecraft.client.renderer.vertex.VertexFormat; | ||
|
||
public class FormatInfo | ||
{ | ||
|
||
final int totalSize; | ||
final int faceSize; | ||
|
||
final int[] offsets; | ||
final int[] indexLengths; | ||
final int[] finalLengths; | ||
|
||
public FormatInfo( | ||
final VertexFormat format ) | ||
{ | ||
int total = 0; | ||
indexLengths = new int[format.getElementCount()]; | ||
finalLengths = new int[format.getElementCount()]; | ||
offsets = new int[format.getElementCount()]; | ||
|
||
for ( int x = 0; x < indexLengths.length; ++x ) | ||
{ | ||
finalLengths[x] = format.getElement( x ).getElementCount(); | ||
indexLengths[x] = finalLengths[x]; | ||
|
||
switch ( format.getElement( x ).getUsage() ) | ||
{ | ||
default: | ||
case GENERIC: | ||
case PADDING: | ||
indexLengths[x] = 0; | ||
break; | ||
|
||
case COLOR: | ||
indexLengths[x] = 4; | ||
break; | ||
|
||
case NORMAL: | ||
indexLengths[x] = 3; | ||
break; | ||
|
||
case POSITION: | ||
indexLengths[x] = 3; | ||
break; | ||
|
||
case UV: | ||
indexLengths[x] = 2; | ||
break; | ||
} | ||
|
||
offsets[x] = total; | ||
total += indexLengths[x]; | ||
} | ||
|
||
this.totalSize = total; | ||
this.faceSize = total * 4; | ||
} | ||
|
||
public int[] pack( | ||
float[][][] unpackedData ) | ||
{ | ||
int[] out = new int[this.faceSize]; | ||
|
||
int offset = 0; | ||
for ( int f = 0; f < 4; ++f ) | ||
{ | ||
float[][] run2 = unpackedData[f]; | ||
for ( int x = 0; x < indexLengths.length; ++x ) | ||
{ | ||
float[] run = run2[x]; | ||
for ( int z = 0; z < indexLengths[x]; z++ ) | ||
{ | ||
if ( run.length > z ) | ||
out[offset++] = Float.floatToRawIntBits( run[z] ); | ||
else | ||
out[offset++] = 0; | ||
} | ||
} | ||
} | ||
|
||
return out; | ||
} | ||
|
||
public float[] unpack( | ||
int[] raw, | ||
int vertex, | ||
int index ) | ||
{ | ||
int size = indexLengths[index]; | ||
float[] out = new float[size]; | ||
int start = vertex * this.totalSize + offsets[index]; | ||
|
||
for ( int x = 0; x < size; x++ ) | ||
{ | ||
out[x] = Float.intBitsToFloat( raw[start + x] ); | ||
} | ||
|
||
return out; | ||
} | ||
|
||
} |