11package pro .mikey .xray .core ;
22
3+ import com .mojang .blaze3d .PrimitiveTopology ;
34import com .mojang .blaze3d .buffers .GpuBuffer ;
45import com .mojang .blaze3d .buffers .GpuBufferSlice ;
56import com .mojang .blaze3d .pipeline .BlendFunction ;
7+ import com .mojang .blaze3d .pipeline .ColorTargetState ;
8+ import com .mojang .blaze3d .pipeline .DepthStencilState ;
69import com .mojang .blaze3d .pipeline .RenderPipeline ;
10+ import com .mojang .blaze3d .platform .CompareOp ;
711import com .mojang .blaze3d .systems .RenderPass ;
812import com .mojang .blaze3d .systems .RenderSystem ;
913import com .mojang .blaze3d .textures .GpuTextureView ;
1014import com .mojang .blaze3d .vertex .*;
1115import net .minecraft .client .Minecraft ;
1216import net .minecraft .client .renderer .DynamicUniforms ;
1317import net .minecraft .client .renderer .RenderPipelines ;
14- import net .minecraft .client .renderer .ShapeRenderer ;
15- import net .minecraft .resources .Identifier ;
1618import net .minecraft .world .level .ChunkPos ;
1719import net .minecraft .world .phys .Vec3 ;
1820import net .minecraft .world .phys .shapes .Shapes ;
1921import org .joml .Matrix4f ;
2022import org .joml .Matrix4fStack ;
2123import org .joml .Vector3f ;
2224import org .joml .Vector4f ;
23- import org . lwjgl . opengl . GL11 ;
25+ import pro . mikey . xray . XRay ;
2426
2527import java .io .Closeable ;
2628import java .util .*;
2729
2830public class OutlineRender {
29- private static final RenderSystem .AutoStorageIndexBuffer indices = RenderSystem .getSequentialBuffer (VertexFormat .Mode .LINES );
31+ public static final RenderPipeline NO_DEPTH_LINES_PIPELINE = RenderPipeline .builder (
32+ RenderPipelines .MATRICES_FOG_SNIPPET )
33+ .withVertexShader ("core/rendertype_lines" )
34+ .withFragmentShader ("core/rendertype_lines" )
35+ .withColorTargetState (new ColorTargetState (BlendFunction .TRANSLUCENT ))
36+ .withCull (false )
37+ .withVertexBinding (0 , DefaultVertexFormat .POSITION_COLOR_NORMAL_LINE_WIDTH )
38+ .withPrimitiveTopology (PrimitiveTopology .LINES )
39+ .withDepthStencilState (new DepthStencilState (CompareOp .ALWAYS_PASS , false )
40+ )
41+ .withLocation (XRay .id ("pipeline/lines_no_depth" ))
42+ .build ();
43+
44+ private static final RenderSystem .AutoStorageIndexBuffer indices = RenderSystem .getSequentialBuffer (PrimitiveTopology .LINES );
45+
3046 private static final Map <ChunkPos , VBOHolder > vertexBuffers = new HashMap <>();
3147
3248 private static final Set <ChunkPos > chunksToRefresh = Collections .synchronizedSet (new HashSet <>());
3349
34- public static void renderBlocks (PoseStack poseStack ) {
50+ public static void renderBlocks () {
3551 if (!ScanController .INSTANCE .isXRayActive () || Minecraft .getInstance ().player == null ) {
3652 return ;
3753 }
@@ -65,27 +81,34 @@ public static void renderBlocks(PoseStack poseStack) {
6581
6682 VBOHolder holder = vertexBuffers .get (chunkPos );
6783 if (holder == null ) {
68- BufferBuilder bufferBuilder = Tesselator .getInstance ().begin (RenderPipelines .LINES .getVertexFormatMode (), RenderPipelines .LINES .getVertexFormat ());
69-
70- // More concurrent modification exceptions can happen here, so we clone the list
71- var blockPropsClone = new ArrayList <>(blocksWithProps );
72-
73- for (var blockProps : blockPropsClone ) {
74- if (blockProps == null ) {
75- continue ;
84+ try (ByteBufferBuilder byteBufferBuilder = new ByteBufferBuilder (NO_DEPTH_LINES_PIPELINE .getVertexFormatBinding (0 ).getVertexSize () * 1024 )) {
85+ BufferBuilder bufferBuilder = new BufferBuilder (
86+ byteBufferBuilder ,
87+ NO_DEPTH_LINES_PIPELINE .getPrimitiveTopology (),
88+ NO_DEPTH_LINES_PIPELINE .getVertexFormatBinding (0 )
89+ );
90+
91+ var blockPropsClone = new ArrayList <>(blocksWithProps );
92+ for (var blockProps : blockPropsClone ) {
93+ Vector3f normal = new Vector3f ();
94+ if (blockProps == null ) continue ;
95+ final int x = blockProps .x (), y = blockProps .y (), z = blockProps .z ();
96+ int color = blockProps .color ();
97+ float width = 2.0f ;
98+
99+ Shapes .block ().forAllEdges ((x1 , y1 , z1 , x2 , y2 , z2 ) -> {
100+ normal .set ((float )(x2 - x1 ), (float )(y2 - y1 ), (float )(z2 - z1 )).normalize ();
101+ bufferBuilder .addVertex ((float )(x + x1 ), (float )(y + y1 ), (float )(z + z1 )).setColor (color ).setNormal (normal .x (), normal .y (), normal .z ()).setLineWidth (width );
102+ bufferBuilder .addVertex ((float )(x + x2 ), (float )(y + y2 ), (float )(z + z2 )).setColor (color ).setNormal (normal .x (), normal .y (), normal .z ()).setLineWidth (width );
103+ });
76104 }
77105
78- final int x = blockProps .x (), y = blockProps .y (), z = blockProps .z ();
79-
80- ShapeRenderer .renderShape (poseStack , bufferBuilder , Shapes .block (), x , y , z , blockProps .color (), 1f );
81- }
82-
83- try (MeshData meshData = bufferBuilder .buildOrThrow ()) {
84- int indexCount = meshData .drawState ().indexCount ();
85- GpuBuffer vertexBuffer = RenderSystem .getDevice ()
86- .createBuffer (() -> "Xray vertex buffer" , GpuBuffer .USAGE_VERTEX , meshData .vertexBuffer ());
87-
88- vertexBuffers .put (chunkPos , new VBOHolder (vertexBuffer , indexCount ));
106+ try (MeshData meshData = bufferBuilder .buildOrThrow ()) {
107+ int indexCount = meshData .drawState ().indexCount ();
108+ GpuBuffer vertexBuffer = RenderSystem .getDevice ()
109+ .createBuffer (() -> "Xray vertex buffer" , GpuBuffer .USAGE_VERTEX , meshData .vertexBuffer ());
110+ vertexBuffers .put (chunkPos , new VBOHolder (vertexBuffer , indexCount ));
111+ }
89112 }
90113 }
91114
@@ -94,33 +117,31 @@ public static void renderBlocks(PoseStack poseStack) {
94117 continue ;
95118 }
96119
97- Vec3 playerPos = Minecraft .getInstance ().gameRenderer .getMainCamera ().position ().reverse ();
120+ Vec3 playerPos = Minecraft .getInstance ().gameRenderer .mainCamera ().position ().reverse ();
98121
99122 Matrix4fStack matrix4fStack = RenderSystem .getModelViewStack ();
100- GpuTextureView colorTextureView = Minecraft .getInstance ().getMainRenderTarget ().getColorTextureView ();
101- GpuTextureView depthTextureView = Minecraft .getInstance ().getMainRenderTarget ().getDepthTextureView ();
123+ GpuTextureView colorTextureView = Minecraft .getInstance ().gameRenderer . mainRenderTarget ().getColorTextureView ();
124+ GpuTextureView depthTextureView = Minecraft .getInstance ().gameRenderer . mainRenderTarget ().getDepthTextureView ();
102125
103126 matrix4fStack .pushMatrix ();
104127 matrix4fStack .translate ((float ) playerPos .x (), (float ) playerPos .y (), (float ) playerPos .z ());
105128 GpuBufferSlice [] gpubufferslice = RenderSystem .getDynamicUniforms ().writeTransforms (new DynamicUniforms .Transform (new Matrix4f (matrix4fStack ), new Vector4f (1.0F , 1.0F , 1.0F , 1.0F ), new Vector3f (), new Matrix4f ()));
106129
107- GL11 .glDisable (GL11 .GL_DEPTH_TEST );
108130 RenderSystem .setShaderFog (gpubufferslice [0 ]);
109131
110132 GpuBuffer gpuBuffer = indices .getBuffer (holder .indexCount );
111133 try (RenderPass renderPass = RenderSystem .getDevice ()
112134 .createCommandEncoder ()
113- .createRenderPass (() -> "xray" , colorTextureView , OptionalInt .empty (), depthTextureView , OptionalDouble .empty ())) {
135+ .createRenderPass (() -> "xray" , colorTextureView , Optional .empty (), depthTextureView , OptionalDouble .empty ())) {
114136
115137 RenderSystem .bindDefaultUniforms (renderPass );
116- renderPass .setVertexBuffer (0 , holder .vertexBuffer );
138+ renderPass .setVertexBuffer (0 , holder .vertexBuffer . slice () );
117139 renderPass .setIndexBuffer (gpuBuffer , indices .type ());
118140 renderPass .setUniform ("DynamicTransforms" , gpubufferslice [0 ]);
119- renderPass .setPipeline (RenderPipelines . LINES );
120- renderPass .drawIndexed (0 , 0 , holder . indexCount , 1 );
141+ renderPass .setPipeline (NO_DEPTH_LINES_PIPELINE );
142+ renderPass .drawIndexed (holder . indexCount , 1 , 0 , 0 , 0 );
121143 }
122144
123- GL11 .glEnable (GL11 .GL_DEPTH_TEST );
124145 matrix4fStack .popMatrix ();
125146 }
126147 }
0 commit comments