From 6c75a5b44781c2aaec543237312df1f14274feac Mon Sep 17 00:00:00 2001 From: Hendrik Borchardt Date: Thu, 16 May 2024 19:13:54 +0200 Subject: [PATCH] Improve precision of surface3d worldPosition calculation When the objectOffset is very large compared to the values in the localCoordinate, adding it to the localCoordinate loses precision in the localCoordinate. As the addition of the objectOffset was immediately undone by application of the model matrix, it is better to combine the model matrix and the translation by the objectOffset into one matrix and apply it directly to the localCoordinate. --- shaders/contour-vertex.glsl | 3 ++- shaders/vertex.glsl | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/shaders/contour-vertex.glsl b/shaders/contour-vertex.glsl index 4560404..c23e7ad 100644 --- a/shaders/contour-vertex.glsl +++ b/shaders/contour-vertex.glsl @@ -18,7 +18,8 @@ varying vec4 vColor; void main() { vec3 dataCoordinate = permutation * vec3(uv.xy, height); worldCoordinate = objectOffset + dataCoordinate; - vec4 worldPosition = model * vec4(worldCoordinate, 1.0); + mat4 objectOffsetTranslation = mat4(1.0) + mat4(vec4(0), vec4(0), vec4(0), vec4(objectOffset, 0)); + vec4 worldPosition = (model * objectOffsetTranslation) * vec4(dataCoordinate, 1.0); vec4 clipPosition = projection * (view * worldPosition); clipPosition.z += zOffset; diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl index 2dde81c..4587c0c 100644 --- a/shaders/vertex.glsl +++ b/shaders/vertex.glsl @@ -18,7 +18,8 @@ varying vec4 vColor; void main() { vec3 localCoordinate = vec3(uv.zw, f.x); worldCoordinate = objectOffset + localCoordinate; - vec4 worldPosition = model * vec4(worldCoordinate, 1.0); + mat4 objectOffsetTranslation = mat4(1.0) + mat4(vec4(0), vec4(0), vec4(0), vec4(objectOffset, 0)); + vec4 worldPosition = (model * objectOffsetTranslation) * vec4(localCoordinate, 1.0); vec4 clipPosition = projection * (view * worldPosition); gl_Position = clipPosition; kill = f.y;