UNPKG

@threlte/extras

Version:

Utilities, abstractions and plugins for your Threlte apps

3 lines (2 loc) 4.55 kB
export declare const vertexShader = "\nvarying vec2 vUv;\nvarying vec2 vPos;\nvoid main () {\n gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1.0);\n vUv = uv;\n vPos = position.xy;\n}\n"; export declare const fragmentShader = "\n// Majority from https://gist.github.com/statico/df64c5d167362ecf7b34fca0b1459a44\nvarying vec2 vUv;\nvarying vec2 vPos;\nuniform vec2 scale;\nuniform vec2 imageBounds;\nuniform float resolution;\nuniform vec3 color;\nuniform sampler2D map;\nuniform sampler2D colorProccessingTexture;\nuniform float radius;\nuniform float zoom;\nuniform float alphaThreshold;\nuniform float alphaSmoothing;\nuniform float brightness;\nuniform float contrast;\nuniform float monochromeStrength;\nuniform vec3 monochromeColor;\nuniform float negative;\nuniform vec3 hsl;\nuniform float grayscale;\nuniform float opacity;\nuniform int colorProcessingEnabled;\nuniform int colorProcessingTextureOverride;\n\n#define PI 3.14159265;\n\nvec2 aspect(vec2 size) {\n return size / min(size.x, size.y);\n}\n\n// from https://iquilezles.org/articles/distfunctions\nfloat udRoundBox(vec2 p, vec2 b, float r) {\n return length(max(abs(p) - b + r, 0.0)) - r;\n}\n\nfloat hueToRgb(float p, float q, float t) {\n if (t < 0.0f)\n t += 1.0f;\n if (t > 1.0f)\n t -= 1.0f;\n if (t < 1.0f / 6.0f)\n return p + (q - p) * 6.0f * t;\n if (t < 1.0f / 2.0f)\n return q;\n if (t < 2.0f / 3.0f)\n return p + (q - p) * (2.0f / 3.0f - t) * 6.0f;\n return p;\n}\n\nvec3 rgbToHsl(vec3 color) {\n float max = max(max(color.r, color.g), color.b);\n float min = min(min(color.r, color.g), color.b);\n float h, s, l = (max + min) / 2.0f;\n\n if (max == min) {\n h = s = 0.0f;\n } else {\n float d = max - min;\n s = l > 0.5f ? d / (2.0f - max - min) : d / (max + min);\n if (max == color.r) {\n h = (color.g - color.b) / d + (color.g < color.b ? 6.0f : 0.0f);\n } else if (max == color.g) {\n h = (color.b - color.r) / d + 2.0f;\n } else if (max == color.b) {\n h = (color.r - color.g) / d + 4.0f;\n }\n h /= 6.0f;\n }\n\n return vec3(h, s, l);\n}\n\nvec3 hslToRgb(vec3 hsl) {\n float h = hsl.x;\n float s = hsl.y;\n float l = hsl.z;\n\n float r, g, b;\n\n if (s == 0.0f) {\n r = g = b = l;\n } else {\n float q = l < 0.5f ? l * (1.0f + s) : l + s - l * s;\n float p = 2.0f * l - q;\n r = hueToRgb(p, q, h + 1.0f / 3.0f);\n g = hueToRgb(p, q, h);\n b = hueToRgb(p, q, h - 1.0f / 3.0f);\n }\n\n return vec3(r, g, b);\n}\n\nvec3 monochrome(float x, vec3 col) {\n return col * exp(4.0 * x - 1.0);\n}\n\nvoid processColors (inout vec4 colors) {\n\tvec4 strength = vec4(1.0);\n\n\tif (colorProcessingTextureOverride == 1) {\n\t\tstrength = texture2D(colorProccessingTexture, vUv);\n\n\t\tfloat smoothedAlpha = smoothstep(1.0 - alphaThreshold - alphaSmoothing, 1.0 - alphaThreshold, strength.a + 0.0001);\n\t\tcolors.a *= smoothedAlpha;\n\n\t\tif (gl_FragColor.a == 0.0) {\n\t\t\tdiscard;\n\t\t\treturn;\n\t\t}\n\t}\n\n\t// BRIGHTNESS\n\tcolors.rgb = max(colors.rgb + brightness, 0.0);\n\n\t// CONTRAST\n colors.rgb = max(((colors.rgb - 0.5) * max(contrast + 1.0, 0.0)) + 0.5, 0.0);\n\n\t// HSL\n\tvec3 hslColor = rgbToHsl(colors.rgb);\n\thslColor.x = mod(hslColor.x + hsl.x * strength.r ,1.0);\n\thslColor.y *= (1.0 + hsl.y * strength.g);\n\thslColor.z += hsl.z * strength.b;\n\tcolors.rgb = max(hslToRgb(hslColor), vec3(0.0));\n\n\t// MONOCHROME\n\tcolors.rgb = mix(colors.rgb, monochrome(hslColor.z, monochromeColor), monochromeStrength);\n}\n\nvoid main() {\n vec2 s = aspect(scale);\n vec2 i = aspect(imageBounds);\n float rs = s.x / s.y;\n float ri = i.x / i.y;\n vec2 new = rs < ri ? vec2(i.x * s.y / i.y, s.y) : vec2(s.x, i.y * s.x / i.x);\n vec2 offset = (rs < ri ? vec2((new.x - s.x) / 2.0, 0.0) : vec2(0.0, (new.y - s.y) / 2.0)) / new;\n vec2 uv = vUv * s / new + offset;\n vec2 zUv = (uv - vec2(0.5, 0.5)) / zoom + vec2(0.5, 0.5);\n\n vec2 res = vec2(scale * resolution);\n vec2 halfRes = 0.5 * res;\n float b = udRoundBox(vUv.xy * res - halfRes, halfRes, resolution * radius);\n vec3 a = mix(vec3(1.0, 0.0, 0.0), vec3(0.0, 0.0, 0.0), smoothstep(0.0, 1.0, b));\n\n\tgl_FragColor = texture2D(map, zUv) * vec4(color, opacity * a);\n\n\tif (colorProcessingEnabled == 1) {\n\t processColors(gl_FragColor);\n\t}\n\n\tif (gl_FragColor.a == 0.0) {\n\t discard;\n\t}\n\n #include <tonemapping_fragment>\n #include <colorspace_fragment>\n\tgl_FragColor.rgb = mix(gl_FragColor.rgb, vec3(1.0) - gl_FragColor.rgb, negative);\n}\n";