11package mekanism .client .render .lib ;
22
3- import com .mojang .blaze3d .vertex .DefaultVertexFormat ;
4- import java .nio .ByteBuffer ;
5- import java .nio .IntBuffer ;
63import java .util .ArrayList ;
74import java .util .Arrays ;
85import java .util .List ;
96import mekanism .common .util .EnumUtils ;
107import net .minecraft .MethodsReturnNonnullByDefault ;
8+ import net .minecraft .client .renderer .RenderType ;
119import net .minecraft .client .renderer .block .model .BakedQuad ;
1210import net .minecraft .client .resources .model .BakedModel ;
1311import net .minecraft .core .Direction ;
1412import net .minecraft .util .Mth ;
1513import net .minecraft .util .RandomSource ;
1614import net .minecraft .world .level .block .state .BlockState ;
17- import net .minecraft . world . phys . Vec3 ;
15+ import net .neoforged . neoforge . client . model . IQuadTransformer ;
1816import net .neoforged .neoforge .client .model .data .ModelData ;
19- import org .lwjgl .system .MemoryStack ;
17+ import org .jetbrains .annotations .Nullable ;
18+ import org .joml .Vector3f ;
2019
2120public class Outlines {
2221
23- public static List <Line > extract (BakedModel model , BlockState state , RandomSource rand , ModelData modelData ) {
22+ public static List <Line > extract (BakedModel model , @ Nullable BlockState state , RandomSource rand , ModelData modelData , @ Nullable RenderType renderType ) {
2423 List <Line > lines = new ArrayList <>();
2524 VertexExtractor consumer = new VertexExtractor (lines );
2625 for (Direction direction : EnumUtils .DIRECTIONS ) {
27- //TODO: Eventually we may want to add support for Model data and maybe render type
28- for (BakedQuad quad : model .getQuads (state , direction , rand , modelData , null )) {
26+ for (BakedQuad quad : model .getQuads (state , direction , rand , modelData , renderType )) {
2927 consumer .unpack (quad );
3028 }
3129 }
3230
33- for (BakedQuad quad : model .getQuads (state , null , rand , modelData , null )) {
31+ for (BakedQuad quad : model .getQuads (state , null , rand , modelData , renderType )) {
3432 consumer .unpack (quad );
3533 }
3634 return lines ;
@@ -41,72 +39,61 @@ public static List<Line> extract(BakedModel model, BlockState state, RandomSourc
4139 private static class VertexExtractor {
4240
4341 final List <Line > lines ;
44- final Vec3 [] vertices = new Vec3 [4 ];
42+ final Vector3f [] vertices = new Vector3f [4 ];
4543 int vertexIndex = 0 ;
4644
4745 private VertexExtractor (List <Line > lines ) {
4846 this .lines = lines ;
4947 }
5048
51- public void vertex (double pX , double pY , double pZ ) {
52- vertices [vertexIndex ++] = new Vec3 (pX , pY , pZ );
49+ public void vertex (float pX , float pY , float pZ ) {
50+ vertices [vertexIndex ++] = new Vector3f (pX , pY , pZ );
5351 if (vertexIndex == 4 ) {
5452 vertexIndex = 0 ;
55- addLine (vertices [0 ]. x , vertices [0 ]. y , vertices [ 0 ]. z , vertices [ 1 ]. x , vertices [ 1 ]. y , vertices [ 1 ]. z );
56- addLine (vertices [1 ]. x , vertices [1 ]. y , vertices [ 1 ]. z , vertices [ 2 ]. x , vertices [ 2 ]. y , vertices [ 2 ]. z );
57- addLine (vertices [2 ]. x , vertices [2 ]. y , vertices [ 2 ]. z , vertices [ 3 ]. x , vertices [ 3 ]. y , vertices [ 3 ]. z );
58- addLine (vertices [3 ]. x , vertices [3 ]. y , vertices [ 3 ]. z , vertices [ 0 ]. x , vertices [ 0 ]. y , vertices [ 0 ]. z );
53+ addLine (vertices [0 ], vertices [1 ] );
54+ addLine (vertices [1 ], vertices [2 ] );
55+ addLine (vertices [2 ], vertices [3 ] );
56+ addLine (vertices [3 ], vertices [0 ] );
5957 Arrays .fill (vertices , null );
6058 }
6159 }
6260
63- //slimmed down version of putBulkData
64- public void unpack (BakedQuad pQuad ) {
65- int [] quadVertices = pQuad .getVertices ();
66- int elementCount = quadVertices .length / 8 ;
67-
68- try (MemoryStack memorystack = MemoryStack .stackPush ()) {
69- ByteBuffer bytebuffer = memorystack .malloc (DefaultVertexFormat .BLOCK .getVertexSize ());
70- IntBuffer intbuffer = bytebuffer .asIntBuffer ();
71-
72- for (int k = 0 ; k < elementCount ; ++k ) {
73- intbuffer .clear ();
74- intbuffer .put (quadVertices , k * 8 , 8 );
75- float f = bytebuffer .getFloat (0 );
76- float f1 = bytebuffer .getFloat (4 );
77- float f2 = bytebuffer .getFloat (8 );
78- this .vertex (f , f1 , f2 );
79- }
80- }
81- }
82-
83- private void addLine (double x1 , double y1 , double z1 , double x2 , double y2 , double z2 ) {
84- double nX = (x2 - x1 );
85- double nY = (y2 - y1 );
86- double nZ = (z2 - z1 );
87- double nLen = Math .sqrt (nX * nX + nY * nY + nZ * nZ );
88-
89- nX = nX / nLen ;
90- nY = nY / nLen ;
91- nZ = nZ / nLen ;
92-
93- Line line = new Line ((float ) x1 , (float ) y1 , (float ) z1 , (float ) x2 , (float ) y2 , (float ) z2 , (float ) nX , (float ) nY , (float ) nZ );
61+ private void addLine (Vector3f v1 , Vector3f v2 ) {
62+ Line line = new Line (v1 , v2 );
9463 if (!lines .contains (line )) {
9564 lines .add (line );
9665 }
9766 }
9867
68+ //Based on how QuadTransformers#applying extracts the vertex positions
69+ public void unpack (BakedQuad pQuad ) {
70+ int [] quadVertices = pQuad .getVertices ();
71+ for (int i = 0 ; i < 4 ; i ++) {
72+ int offset = i * IQuadTransformer .STRIDE + IQuadTransformer .POSITION ;
73+ float x = Float .intBitsToFloat (quadVertices [offset ]);
74+ float y = Float .intBitsToFloat (quadVertices [offset + 1 ]);
75+ float z = Float .intBitsToFloat (quadVertices [offset + 2 ]);
76+ this .vertex (x , y , z );
77+ }
78+ }
9979 }
10080
10181 public record Line (float x1 , float y1 , float z1 , float x2 , float y2 , float z2 , float nX , float nY , float nZ ) {
10282
83+ public Line (Vector3f v1 , Vector3f v2 , Vector3f normal ) {
84+ this (v1 .x , v1 .y , v1 .z , v2 .x , v2 .y , v2 .z , normal .x , normal .y , normal .z );
85+ }
86+
87+ public Line (Vector3f v1 , Vector3f v2 ) {
88+ this (v1 , v2 , v2 .sub (v1 , new Vector3f ()).normalize ());
89+ }
90+
10391 @ SuppressWarnings ("SuspiciousNameCombination" )
10492 @ Override
10593 public boolean equals (Object obj ) {
10694 if (obj == this ) {
10795 return true ;
108- }
109- if (obj == null || obj .getClass () != Line .class ) {
96+ } else if (obj == null || obj .getClass () != Line .class ) {
11097 return false ;
11198 }
11299 Line other = (Line ) obj ;
0 commit comments