diff --git a/src/drawables/shapes/circle.ts b/src/drawables/shapes/circle.ts index d3e945d..937bc65 100644 --- a/src/drawables/shapes/circle.ts +++ b/src/drawables/shapes/circle.ts @@ -9,17 +9,15 @@ export class Circle extends Drawable { uniform vec2 circleCenters[CIRCLE_COUNT]; uniform float circleRadii[CIRCLE_COUNT]; - void circleMinDistance(inout float minDistance, inout float color) { - float myMinDistance = maxMinDistance; - for (int i = 0; i < CIRCLE_COUNT; i++) { - float dist = distance(circleCenters[i], position) - circleRadii[i]; - myMinDistance = min(myMinDistance, dist); - } - minDistance = min(minDistance, myMinDistance); - color = mix(1.0 / {paletteSize}, color, step( - distanceNdcPixelSize + SURFACE_OFFSET, - myMinDistance - )); + float circleMinDistance(vec2 target, out float colorIndex) { + colorIndex = 2.0; + float minDistance = 1000.0; + for (int i = 0; i < CIRCLE_COUNT; i++) { + float dist = distance(circleCenters[i], target) - circleRadii[i]; + minDistance = min(minDistance, dist); + } + + return minDistance; } `, distanceFunctionName: 'circleMinDistance', diff --git a/src/drawables/shapes/inverted-tunnel.ts b/src/drawables/shapes/inverted-tunnel.ts index 8d3ccad..9f65925 100644 --- a/src/drawables/shapes/inverted-tunnel.ts +++ b/src/drawables/shapes/inverted-tunnel.ts @@ -12,12 +12,13 @@ export class InvertedTunnel extends Drawable { uniform vec2 toFromDeltas[INVERTED_TUNNEL_COUNT]; uniform float fromRadii[INVERTED_TUNNEL_COUNT]; uniform float toRadii[INVERTED_TUNNEL_COUNT]; - - void invertedTunnelMinDistance(inout float minDistance, inout float color) { - float myMinDistance = -minDistance; + + float invertedTunnelMinDistance(vec2 target, out float colorIndex) { + float minDistance = 1000.0; + for (int i = 0; i < INVERTED_TUNNEL_COUNT; i++) { - vec2 targetFromDelta = position - froms[i]; - + vec2 targetFromDelta = target - froms[i]; + float h = clamp( dot(targetFromDelta, toFromDeltas[i]) / dot(toFromDeltas[i], toFromDeltas[i]), @@ -30,14 +31,10 @@ export class InvertedTunnel extends Drawable { targetFromDelta, toFromDeltas[i] * h ); - myMinDistance = min(myMinDistance, currentDistance); + minDistance = min(minDistance, currentDistance); } - - color = mix(0.0, color, step( - distanceNdcPixelSize + SURFACE_OFFSET, - -myMinDistance - )); - minDistance = -myMinDistance; + + return -minDistance; } `, distanceFunctionName: 'invertedTunnelMinDistance', diff --git a/src/drawables/shapes/tunnel.ts b/src/drawables/shapes/tunnel.ts index ee2f435..3656581 100644 --- a/src/drawables/shapes/tunnel.ts +++ b/src/drawables/shapes/tunnel.ts @@ -12,11 +12,13 @@ export class Tunnel extends Drawable { uniform vec2 toFromDeltas[TUNNEL_COUNT]; uniform float fromRadii[TUNNEL_COUNT]; uniform float toRadii[TUNNEL_COUNT]; - - void tunnelMinDistance(inout float minDistance, inout float color) { - float myMinDistance = minDistance; + + float tunnelMinDistance(vec2 target, out float colorIndex) { + colorIndex = 1.0; + + float minDistance = 1000.0; for (int i = 0; i < TUNNEL_COUNT; i++) { - vec2 targetFromDelta = position - froms[i]; + vec2 targetFromDelta = target - froms[i]; float h = clamp( dot(targetFromDelta, toFromDeltas[i]) @@ -29,15 +31,11 @@ export class Tunnel extends Drawable { ) + distance( targetFromDelta, toFromDeltas[i] * h ); - - myMinDistance = min(myMinDistance, currentDistance); + + minDistance = min(minDistance, currentDistance); } - - color = mix(2.0 / {paletteSize}, color, step( - distanceNdcPixelSize + SURFACE_OFFSET, - myMinDistance - )); - minDistance = min(minDistance, myMinDistance); + + return minDistance; } `, distanceFunctionName: 'tunnelMinDistance', diff --git a/src/graphics/graphics-library/program/uniform-array-autoscaling-program.ts b/src/graphics/graphics-library/program/uniform-array-autoscaling-program.ts index 3e67f40..f7e6e93 100644 --- a/src/graphics/graphics-library/program/uniform-array-autoscaling-program.ts +++ b/src/graphics/graphics-library/program/uniform-array-autoscaling-program.ts @@ -135,7 +135,22 @@ export class UniformArrayAutoScalingProgram implements IProgram { return ''; } - return `${descriptors[i].sdf!.distanceFunctionName}(minDistance, color);`; + return ` + #ifndef NOT_EMPTY + #define NOT_EMPTY + #endif + + objectMinDistance = ${ + descriptors[i].sdf!.distanceFunctionName + }(position, objectColor); + + color = mix(objectColor / {paletteSize}, color, step( + distanceNdcPixelSize + SURFACE_OFFSET, + objectMinDistance + )); + + minDistance = min(minDistance, objectMinDistance); + `; }) .join('\n'); } diff --git a/src/graphics/rendering/shaders/distance-fs-100.glsl b/src/graphics/rendering/shaders/distance-fs-100.glsl index e1ed46e..ae51453 100644 --- a/src/graphics/rendering/shaders/distance-fs-100.glsl +++ b/src/graphics/rendering/shaders/distance-fs-100.glsl @@ -13,10 +13,16 @@ varying vec2 position; {declarations} void main() { - float minDistance = maxMinDistance; + float minDistance = abs(maxMinDistance); float color = 0.0; + float objectMinDistance, objectColor; + {functionCalls} + + #ifndef NOT_EMPTY + minDistance = maxMinDistance; + #endif // minDistance / 2.0: NDC to UV scale gl_FragColor = vec4(minDistance / 2.0, color, 0.0, 0.0); diff --git a/src/graphics/rendering/shaders/distance-fs.glsl b/src/graphics/rendering/shaders/distance-fs.glsl index 4e058d1..59234e7 100644 --- a/src/graphics/rendering/shaders/distance-fs.glsl +++ b/src/graphics/rendering/shaders/distance-fs.glsl @@ -15,11 +15,17 @@ in vec2 position; out vec2 fragmentColor; void main() { - float minDistance = maxMinDistance; + float minDistance = abs(maxMinDistance); float color = 0.0; + float objectMinDistance, objectColor; + {functionCalls} + #ifndef NOT_EMPTY + minDistance = maxMinDistance; + #endif + // minDistance / 2.0: NDC to UV scale fragmentColor = vec2(minDistance / 2.0, color); }