From a036c8d67277f9903b02a5ecaaa8fcd994fd3a91 Mon Sep 17 00:00:00 2001 From: MNAth_ Date: Sun, 14 Jan 2024 15:59:11 +0300 Subject: [PATCH] refactor: reorder shaders partials --- .../world/shaders/partials/clockDots.glsl | 18 ++++ .../world/shaders/partials/clockNumber.glsl | 20 +++++ .../world/shaders/partials/sevenSegment.glsl | 40 +++++++++ .../scene-3/phone-screen/fragment.glsl | 69 ++------------- .../scene-3/watch-screen/fragment.glsl | 83 +++---------------- 5 files changed, 97 insertions(+), 133 deletions(-) create mode 100644 src/experiences/home/world/shaders/partials/clockDots.glsl create mode 100644 src/experiences/home/world/shaders/partials/clockNumber.glsl create mode 100644 src/experiences/home/world/shaders/partials/sevenSegment.glsl diff --git a/src/experiences/home/world/shaders/partials/clockDots.glsl b/src/experiences/home/world/shaders/partials/clockDots.glsl new file mode 100644 index 0000000..9e2d715 --- /dev/null +++ b/src/experiences/home/world/shaders/partials/clockDots.glsl @@ -0,0 +1,18 @@ +/** + * clock Dots + * + * @original-author Andre VanKammen / https://github.com/AndreVanKammen + * + * @source https://www.shadertoy.com/view/3dtSRj + */ + +float clockDots(vec2 uv) { + float seg = .0; + uv.y -= .5; + seg += (1. - smoothstep(0.11, .13, length(uv))) * (1. - length(uv) * 2.0); + uv.y += 1.; + seg += (1. - smoothstep(0.11, .13, length(uv))) * (1. - length(uv) * 2.0); + return seg; +} + +#pragma glslify: export(clockDots) diff --git a/src/experiences/home/world/shaders/partials/clockNumber.glsl b/src/experiences/home/world/shaders/partials/clockNumber.glsl new file mode 100644 index 0000000..16f4362 --- /dev/null +++ b/src/experiences/home/world/shaders/partials/clockNumber.glsl @@ -0,0 +1,20 @@ +#pragma glslify: sevenSegment = require(./sevenSegment.glsl) + +float clockNumber(vec2 uv, int nr, bool zeroTrim, bool on) { + // Speed optimization, leave if pixel is not in segment/ + if(abs(uv.x) > 1.5 || abs(uv.y) > 1.2) + return .0; + + float seg = .0; + if(uv.x > .0) { + nr /= 10; + if(nr == 0 && zeroTrim) + nr = -1; + seg += sevenSegment(uv + vec2(-0.75, .0), nr, on); + } else + seg += sevenSegment(uv + vec2(0.75, .0), int(mod(float(nr), 10.0)), on); + + return seg; +} + +#pragma glslify: export(clockNumber) diff --git a/src/experiences/home/world/shaders/partials/sevenSegment.glsl b/src/experiences/home/world/shaders/partials/sevenSegment.glsl new file mode 100644 index 0000000..31027cc --- /dev/null +++ b/src/experiences/home/world/shaders/partials/sevenSegment.glsl @@ -0,0 +1,40 @@ +/** + * SevenSegment + * + * @original-author Andre VanKammen / https://github.com/AndreVanKammen + * + * @source https://www.shadertoy.com/view/3dtSRj + */ + +bool showOff = false; + +float segment(vec2 uv, bool On) { + if(!On && !showOff) + return .0; + + float seg = (1. - smoothstep(0.08, .09 + float(On) * .02, abs(uv.x))) * + (1. - smoothstep(0.46, .47 + float(On) * .02, abs(uv.y) + abs(uv.x))); + + // Led like brightness + if(On) + seg *= (1. - length(uv * vec2(3.8, .9))); + else + seg *= -(0.05 + length(uv * vec2(0.2, .1))); + + return seg; +} + +float sevenSegment(vec2 uv, int num, bool on) { + float seg = .0; + seg += segment(uv.yx + vec2(-1., .0), num != -1 && num != 1 && num != 4 && on); + seg += segment(uv.xy + vec2(-0.5, -0.5), num != -1 && num != 1 && num != 2 && num != 3 && num != 7 && on); + seg += segment(uv.xy + vec2(0.5, -0.5), num != -1 && num != 5 && num != 6 && on); + seg += segment(uv.yx + vec2(.0, .0), num != -1 && num != 0 && num != 1 && num != 7 && on); + seg += segment(uv.xy + vec2(-0.5, .5), (num == 0 || num == 2 || num == 6 || num == 8) && on); + seg += segment(uv.xy + vec2(0.5, .5), num != -1 && num != 2 && on); + seg += segment(uv.yx + vec2(1., .0), num != -1 && num != 1 && num != 4 && num != 7 && on); + + return seg; +} + +#pragma glslify: export(sevenSegment) diff --git a/src/experiences/home/world/shaders/scene-3/phone-screen/fragment.glsl b/src/experiences/home/world/shaders/scene-3/phone-screen/fragment.glsl index 6d6b057..47a94ed 100644 --- a/src/experiences/home/world/shaders/scene-3/phone-screen/fragment.glsl +++ b/src/experiences/home/world/shaders/scene-3/phone-screen/fragment.glsl @@ -12,63 +12,10 @@ varying vec2 vUv; uniform float uTime; uniform float uTimestamp; -bool showMatrix = false; -bool showOff = false; - -float segment(vec2 uv, bool On) { - if(!On && !showOff) - return .0; - - float seg = (1. - smoothstep(0.08, .09 + float(On) * .02, abs(uv.x))) * - (1. - smoothstep(0.46, .47 + float(On) * .02, abs(uv.y) + abs(uv.x))); - - // Led like brightness - if(On) - seg *= (1. - length(uv * vec2(3.8, .9))); - else - seg *= -(0.05 + length(uv * vec2(0.2, .1))); - - return seg; -} - -float sevenSegment(vec2 uv, int num) { - float seg = .0; - seg += segment(uv.yx + vec2(-1., .0), num != -1 && num != 1 && num != 4); - seg += segment(uv.xy + vec2(-0.5, -0.5), num != -1 && num != 1 && num != 2 && num != 3 && num != 7); - seg += segment(uv.xy + vec2(0.5, -0.5), num != -1 && num != 5 && num != 6); - seg += segment(uv.yx + vec2(.0, .0), num != -1 && num != 0 && num != 1 && num != 7); - seg += segment(uv.xy + vec2(-0.5, .5), (num == 0 || num == 2 || num == 6 || num == 8)); - seg += segment(uv.xy + vec2(0.5, .5), num != -1 && num != 2); - seg += segment(uv.yx + vec2(1., .0), num != -1 && num != 1 && num != 4 && num != 7); - - return seg; -} +#pragma glslify: clockNumber = require(../../partials/clockNumber.glsl) +#pragma glslify: clockDots = require(../../partials/clockDots.glsl) -float showNum(vec2 uv, int nr, bool zeroTrim) { - // Speed optimization, leave if pixel is not in segment/ - if(abs(uv.x) > 1.5 || abs(uv.y) > 1.2) - return .0; - - float seg = .0; - if(uv.x > .0) { - nr /= 10; - if(nr == 0 && zeroTrim) - nr = -1; - seg += sevenSegment(uv + vec2(-0.75, .0), nr); - } else - seg += sevenSegment(uv + vec2(0.75, .0), int(mod(float(nr), 10.0))); - - return seg; -} - -float dots(vec2 uv) { - float seg = .0; - uv.y -= .5; - seg += (1. - smoothstep(0.11, .13, length(uv))) * (1. - length(uv) * 2.0); - uv.y += 1.; - seg += (1. - smoothstep(0.11, .13, length(uv))) * (1. - length(uv) * 2.0); - return seg; -} +bool showMatrix = false; float sun(vec2 uv, float battery) { float val = smoothstep(0.3, 0.29, length(uv)); @@ -130,19 +77,19 @@ void mainImage(out vec4 fragColor, in vec2 fragCoord) { float seg = .0; float timeSecs = uTimestamp; - seg += showNum(uv, int(mod(timeSecs, 60.)), false); + seg += clockNumber(uv, int(mod(timeSecs, 60.)), false, true); timeSecs = floor(timeSecs / 60.); uv.x -= 1.75; - seg += dots(uv); + seg += clockDots(uv); uv.x -= 1.75; - seg += showNum(uv, int(mod(timeSecs, 60.)), false); + seg += clockNumber(uv, int(mod(timeSecs, 60.)), false, true); timeSecs = floor(timeSecs / 60.); uv.x -= 1.75; - seg += dots(uv); + seg += clockDots(uv); uv.x -= 1.75; - seg += showNum(uv, int(mod(timeSecs, 60.)), true); + seg += clockNumber(uv, int(mod(timeSecs, 60.)), true, true); // Matrix over segment if(showMatrix) { diff --git a/src/experiences/home/world/shaders/scene-3/watch-screen/fragment.glsl b/src/experiences/home/world/shaders/scene-3/watch-screen/fragment.glsl index 29c1fc8..f9bfc29 100644 --- a/src/experiences/home/world/shaders/scene-3/watch-screen/fragment.glsl +++ b/src/experiences/home/world/shaders/scene-3/watch-screen/fragment.glsl @@ -1,6 +1,10 @@ /** + * Mystic blob x Clock Shader + * + * @original-author laserdog / https://www.shadertoy.com/user/laserdog * @original-author Andre VanKammen / https://github.com/AndreVanKammen * + * @source https://www.shadertoy.com/view/XsKfDm * @source https://www.shadertoy.com/view/3dtSRj */ @@ -10,67 +14,13 @@ uniform float uTime; uniform float uSec; uniform float uTimestamp; -bool showMatrix = false; -bool showOff = true; - -float segment(vec2 uv, bool On) { - if(!On && !showOff) - return .0; - - float seg = (1. - smoothstep(0.08, .09 + float(On) * .02, abs(uv.x))) * - (1. - smoothstep(0.46, .47 + float(On) * .02, abs(uv.y) + abs(uv.x))); - - // Led like brightness - if(On) - seg *= (1. - length(uv * vec2(3.8, .9))); - else - seg *= -(0.05 + length(uv * vec2(0.2, .1))); - - return seg; -} - -float sevenSegment(vec2 uv, int num, bool on) { - float seg = .0; - seg += segment(uv.yx + vec2(-1., .0), num != -1 && num != 1 && num != 4 && on); - seg += segment(uv.xy + vec2(-0.5, -0.5), num != -1 && num != 1 && num != 2 && num != 3 && num != 7 && on); - seg += segment(uv.xy + vec2(0.5, -0.5), num != -1 && num != 5 && num != 6 && on); - seg += segment(uv.yx + vec2(.0, .0), num != -1 && num != 0 && num != 1 && num != 7 && on); - seg += segment(uv.xy + vec2(-0.5, .5), (num == 0 || num == 2 || num == 6 || num == 8) && on); - seg += segment(uv.xy + vec2(0.5, .5), num != -1 && num != 2 && on); - seg += segment(uv.yx + vec2(1., .0), num != -1 && num != 1 && num != 4 && num != 7 && on); - - return seg; -} - -float showNum(vec2 uv, int nr, bool zeroTrim, bool on) { - // Speed optimization, leave if pixel is not in segment/ - if(abs(uv.x) > 1.5 || abs(uv.y) > 1.2) - return .0; +#pragma glslify: clockNumber = require(../../partials/clockNumber.glsl) +#pragma glslify: clockDots = require(../../partials/clockDots.glsl) - float seg = .0; - if(uv.x > .0) { - nr /= 10; - if(nr == 0 && zeroTrim) - nr = -1; - seg += sevenSegment(uv + vec2(-0.75, .0), nr, on); - } else - seg += sevenSegment(uv + vec2(0.75, .0), int(mod(float(nr), 10.0)), on); - - return seg; -} - -float dots(vec2 uv) { - float seg = .0; - uv.y -= .5; - seg += (1. - smoothstep(0.11, .13, length(uv))) * (1. - length(uv) * 2.0); - uv.y += 1.; - seg += (1. - smoothstep(0.11, .13, length(uv))) * (1. - length(uv) * 2.0); - return seg; -} +bool showMatrix = false; void clock(out vec4 fragColor) { bool blink = mod(uSec, 2.) == 0.; - bool ampm = false; bool isGreen = true; vec2 uv = vUv * -8.; @@ -81,29 +31,19 @@ void clock(out vec4 fragColor) { float timeSecs = uTimestamp; // Display seconds - // seg += showNum(uv, int(mod(timeSecs, 60.0)), false, blink); timeSecs = floor(timeSecs / 60.0); - // uv.x -= 1.75; - // seg += dots(uv); uv.x -= 1.75; - seg += showNum(uv, int(mod(timeSecs, 60.0)), false, blink); + seg += clockNumber(uv, int(mod(timeSecs, 60.)), false, blink); timeSecs = floor(timeSecs / 60.0); - if(ampm) { - if(timeSecs > 12.0) - timeSecs = mod(timeSecs, 12.0); - } - uv.x -= 1.75; - seg += dots(uv); + seg += clockDots(uv); uv.x -= 1.75; - seg += showNum(uv, int(mod(timeSecs, 60.0)), true, blink); + seg += clockNumber(uv, int(mod(timeSecs, 60.0)), true, blink); // Matrix over segment - if(showMatrix) { + if(showMatrix) seg *= .8 + .2 * smoothstep(0.02, .04, mod(uv.y + uv.x, .06025)); - // seg *= .8+0.2*smoothstep(0.02,0.04,mod(uv.y-uv.x,0.06025)); - } if(seg < .0) { seg = -seg; @@ -154,5 +94,4 @@ void main() { clock(clock_fragement_color); gl_FragColor = mystic_blob_color + clock_fragement_color; - ; }