Skip to content

Commit 4078c2c

Browse files
committed
Fixed shader precision issue
1 parent 2dffccd commit 4078c2c

File tree

4 files changed

+47
-21
lines changed

4 files changed

+47
-21
lines changed

src/main/kotlin/com/lambda/graphics/renderer/esp/ShapeDsl.kt

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@ package com.lambda.graphics.renderer.esp
1919

2020
import com.lambda.graphics.pipeline.VertexBuilder
2121
import com.lambda.graphics.renderer.esp.DirectionMask.hasDirection
22+
import com.lambda.graphics.renderer.esp.Treed.Companion.cameraPos
2223
import com.lambda.threading.runSafe
2324
import com.lambda.util.BlockUtils.blockState
2425
import com.lambda.util.extension.max
2526
import com.lambda.util.extension.min
2627
import com.lambda.util.extension.outlineShape
28+
import com.lambda.util.math.minus
2729
import net.minecraft.block.BlockState
2830
import net.minecraft.block.entity.BlockEntity
2931
import net.minecraft.entity.Entity
30-
import net.minecraft.util.math.BlockBox
3132
import net.minecraft.util.math.BlockPos
3233
import net.minecraft.util.math.Box
3334
import net.minecraft.util.shape.VoxelShape
@@ -47,11 +48,12 @@ class ShapeBuilder(
4748
sides : Int = DirectionMask.ALL,
4849
) = faces.apply {
4950
val boxes = box.pair ?: return@apply
51+
val camera = cameraPos
5052

51-
val pos11 = boxes.first.min
52-
val pos12 = boxes.first.max
53-
val pos21 = boxes.second.min
54-
val pos22 = boxes.second.max
53+
val pos11 = boxes.first.min - camera
54+
val pos12 = boxes.first.max - camera
55+
val pos21 = boxes.second.min - camera
56+
val pos22 = boxes.second.max - camera
5557

5658
val blb by lazy { vertex { vec3(pos11.x, pos11.y, pos11.z).vec3(pos21.x, pos21.y, pos21.z).color(color) } }
5759
val blf by lazy { vertex { vec3(pos11.x, pos11.y, pos12.z).vec3(pos21.x, pos21.y, pos22.z).color(color) } }
@@ -77,8 +79,9 @@ class ShapeBuilder(
7779
topColor : Color = bottomColor,
7880
sides : Int = DirectionMask.ALL
7981
) = faces.apply {
80-
val pos1 = box.min
81-
val pos2 = box.max
82+
val camera = cameraPos
83+
val pos1 = box.min - camera
84+
val pos2 = box.max - camera
8285

8386
val blb by lazy { vertex { vec3(pos1.x, pos1.y, pos1.z).color(bottomColor) } }
8487
val blf by lazy { vertex { vec3(pos1.x, pos1.y, pos2.z).color(bottomColor) } }
@@ -153,11 +156,12 @@ class ShapeBuilder(
153156
mode : DirectionMask.OutlineMode = DirectionMask.OutlineMode.OR,
154157
) = edges.apply {
155158
val boxes = box.pair ?: return@apply
159+
val camera = cameraPos
156160

157-
val pos11 = boxes.first.min
158-
val pos12 = boxes.first.max
159-
val pos21 = boxes.second.min
160-
val pos22 = boxes.second.max
161+
val pos11 = boxes.first.min - camera
162+
val pos12 = boxes.first.max - camera
163+
val pos21 = boxes.second.min - camera
164+
val pos22 = boxes.second.max - camera
161165

162166
val blb by lazy { vertex { vec3(pos11.x, pos11.y, pos11.z).vec3(pos21.x, pos21.y, pos21.z).color(color) } }
163167
val blf by lazy { vertex { vec3(pos11.x, pos11.y, pos12.z).vec3(pos21.x, pos21.y, pos22.z).color(color) } }
@@ -199,8 +203,9 @@ class ShapeBuilder(
199203
sides : Int = DirectionMask.ALL,
200204
mode : DirectionMask.OutlineMode = DirectionMask.OutlineMode.OR,
201205
) = edges.apply {
202-
val pos1 = box.min
203-
val pos2 = box.max
206+
val camera = cameraPos
207+
val pos1 = box.min - camera
208+
val pos2 = box.max - camera
204209

205210
val blb by lazy { vertex { vec3(pos1.x, pos1.y, pos1.z).color(bottomColor) } }
206211
val blf by lazy { vertex { vec3(pos1.x, pos1.y, pos2.z).color(bottomColor) } }

src/main/kotlin/com/lambda/graphics/renderer/esp/Treed.kt

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
package com.lambda.graphics.renderer.esp
1919

20-
import com.lambda.Lambda
20+
import com.lambda.Lambda.mc
2121
import com.lambda.graphics.buffer.vertex.attributes.VertexAttrib
2222
import com.lambda.graphics.buffer.vertex.attributes.VertexMode
2323
import com.lambda.graphics.gl.GlStateUtils
@@ -26,7 +26,12 @@ import com.lambda.graphics.pipeline.VertexPipeline
2626
import com.lambda.graphics.shader.Shader.Companion.shader
2727
import com.lambda.module.modules.client.StyleEditor
2828
import com.lambda.util.extension.partialTicks
29+
import com.lambda.util.math.minus
30+
import net.minecraft.util.math.Vec3d
2931

32+
/**
33+
* Open class for 3d rendering. It contains two pipelines, one for edges and the other for faces.
34+
*/
3035
open class Treed(static: Boolean) {
3136
val shader = if (static) staticMode.first else dynamicMode.first
3237

@@ -43,8 +48,8 @@ open class Treed(static: Boolean) {
4348

4449
fun render() {
4550
shader.use()
46-
shader["u_TickDelta"] = Lambda.mc.partialTicks
47-
shader["u_CameraPosition"] = Lambda.mc.gameRenderer.camera.pos
51+
shader["u_TickDelta"] = mc.partialTicks
52+
shader["u_CameraLerp"] = cachedCameraPos - mc.gameRenderer.camera.pos
4853

4954
GlStateUtils.withFaceCulling(faces::render)
5055
GlStateUtils.withLineWidth(StyleEditor.outlineWidth, edges::render)
@@ -58,11 +63,27 @@ open class Treed(static: Boolean) {
5863
edgeBuilder = VertexBuilder()
5964
}
6065

66+
/**
67+
* Public object for static rendering. Shapes rendering by this are not interpolated.
68+
* That means that if the shape is frequently moving, its movement will saccade.
69+
*/
6170
object Static : Treed(true)
71+
72+
/**
73+
* Public object for dynamic rendering. Its position will be interpolated between ticks, allowing
74+
* for smooth movement at the cost of duplicate position and slightly higher memory consumption.
75+
*/
6276
object Dynamic : Treed(false)
6377

6478
companion object {
6579
private val staticMode = shader("renderer/box_static") to VertexAttrib.Group.STATIC_RENDERER
6680
private val dynamicMode = shader("renderer/box_dynamic") to VertexAttrib.Group.DYNAMIC_RENDERER
81+
82+
var cachedCameraPos: Vec3d = Vec3d.ZERO
83+
val cameraPos: Vec3d
84+
get() {
85+
cachedCameraPos = mc.gameRenderer.camera.pos
86+
return cachedCameraPos
87+
}
6788
}
6889
}

src/main/resources/assets/lambda/shaders/renderer/box_dynamic.glsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ attributes {
77
uniforms {
88
mat4 u_ProjModel; # vertex
99
float u_TickDelta; # vertex
10-
vec3 u_CameraPosition; # vertex
10+
vec3 u_CameraLerp; # vertex
1111
};
1212

1313
export {
14-
core gl_Position; # u_ProjModel * vec4(mix(pos1, pos2, u_TickDelta) - u_CameraPosition, 1.0)
14+
core gl_Position; # u_ProjModel * vec4(mix(pos1, pos2, u_TickDelta) + u_CameraLerp, 1.0)
1515
vec4 v_Color; # color
1616
};
1717

1818
void fragment() {
1919
color = v_Color;
20-
}#
20+
}#

src/main/resources/assets/lambda/shaders/renderer/box_static.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ attributes {
55

66
uniforms {
77
mat4 u_ProjModel; # vertex
8-
vec3 u_CameraPosition; # vertex
8+
vec3 u_CameraLerp; # vertex
99
};
1010

1111
export {
12-
core gl_Position; # u_ProjModel * vec4(pos - u_CameraPosition, 1.0)
12+
core gl_Position; # u_ProjModel * vec4(pos + u_CameraLerp, 1.0)
1313
vec4 v_Color; # color
1414
};
1515

0 commit comments

Comments
 (0)