diff --git a/.gitmodules b/.gitmodules index 8c476a7..e69de29 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +0,0 @@ -[submodule "msdfgen"] - path = msdfgen - url = https://github.com/Chlumsky/msdfgen diff --git a/README.md b/README.md deleted file mode 100644 index cee5084..0000000 --- a/README.md +++ /dev/null @@ -1,177 +0,0 @@ -![Matrix screenshot](/screenshot.png?raw=true "Matrix's default appearance.") - -# matrix (web-based green code rain, made with love) - -**_News Update September 2022:_** this project was [featured in Vice Motherboard](https://www.vice.com/en/article/88qvn3/coder-makes-matrix-green-rain-simulator-that-lilly-wachowski-says-is-better-than-the-original), along with insight into the effect from Lilly Wachowski. - -## Quick Links - -- [Classic Matrix code](https://rezmason.github.io/matrix) -- [Starting from a blank screen (`skipIntro=false`)](https://rezmason.github.io/matrix/?skipIntro=false) (which some people really like, but isn't the default mode) -- [3D mode](https://rezmason.github.io/matrix?version=3d) -- Mirror mode, [with camera](https://rezmason.github.io/matrix/?version=updated&effect=mirror&camera=true) and [without](https://rezmason.github.io/matrix/?version=updated&effect=mirror). (Click to make ripples.) -- [Matrix Resurrections updated code](https://rezmason.github.io/matrix?version=resurrections) -- [Trinity mode](https://rezmason.github.io/matrix?version=trinity) -- [Operator Matrix code (with ripple effects)](https://rezmason.github.io/matrix?version=operator) -- [Megacity Mode, as seen in Revolutions](https://rezmason.github.io/matrix?version=megacity) - -*Variants* - -- [Code of the "Nightmare Matrix"](https://rezmason.github.io/matrix?version=nightmare) - - [(you know, this stuff).](http://matrix.wikia.com/wiki/Nightmare_Matrix) -- [Code of the "Paradise Matrix"](https://rezmason.github.io/matrix?version=paradise) - - [(AKA this stuff).](http://matrix.wikia.com/wiki/Paradise_Matrix) -- [A custom variant I call "Palimpsest"](https://rezmason.github.io/matrix?version=palimpsest) -- [A custom variant I call "Twilight"](https://rezmason.github.io/matrix?version=twilight) -- [Morpheus mode](https://rezmason.github.io/matrix?version=morpheus) -- [Bugs mode](https://rezmason.github.io/matrix?version=bugs) -- [Pride flag colors](https://rezmason.github.io/matrix/?effect=pride) -- [Trans flag colors](https://rezmason.github.io/matrix/?effect=trans) -- [Custom stripes (`effect=stripes&stripeColors=R,G,B,R,G,B,R,G,B, etc`)](https://rezmason.github.io/matrix/?effect=stripes&stripeColors=1,0,0,1,1,0,0,1,0) -- [Custom palette (`palette=R,G,B,%,R,G,B,%,R,G,B,%, etc`)](https://rezmason.github.io/matrix/?palette=0.1,0,0.2,0,0.2,0.5,0,0.5,1,0.7,0,1) -- [Custom image (`url=www.website.com/picture.jpg`)](https://rezmason.github.io/matrix/?effect=image&url=https://upload.wikimedia.org/wikipedia/commons/f/f5/EagleRock.jpg) -- [Debug view (`effect=none`)](https://rezmason.github.io/matrix/?effect=none) (*epilepsy warning*: this once had lots of flickering) -- [Holographic version](https://rezmason.github.io/matrix?version=holoplay) (requires a Looking Glass display; see it in action [here](https://www.youtube.com/watch?v=gwA9hfq1Ing)) - -*Typography* - -- [The free classic font (TrueType).](https://github.com/Rezmason/matrix/raw/master/assets/Matrix-Code.ttf) -- [The free *Resurrections* font (TrueType).](https://github.com/Rezmason/matrix/raw/master/assets/Matrix-Resurrected.ttf) -- [The unofficial glyph database.](https://docs.google.com/spreadsheets/d/1NRJP88EzQlj_ghBbtjkGi-NbluZzlWpAqVIAq1MDGJc) - - -## Contents -- [About](#about) -- [Goals](#goals) -- [Sidenote: other people's Matrix effects](#sidenote-other-peoples-matrix-effects) -- [Customization](#customization) -- [Troubleshooting](#troubleshooting) -- [Future directions](#future-directions) -- [Friends of the project](#friends-of-the-project) -- [Colophon](#colophon) -- [Other details](#other-details) - - -## About - -This project is a web implementation of the raining green code seen in the *Matrix* franchise. It's built right on top of the functional WebGL wrapper, [REGL](https://regl.party), with beta support for the upcoming graphics API [WebGPU](https://github.com/gpuweb/gpuweb); its previous Three.js version is maintained in a separate branch. - -This project runs right in the web browser; you can serve it with any HTTP/HTTPS server, with no additional setup. For example, on a Mac you can point a Terminal shell at a local copy of the project and run a simple HTTP server with Python: `cd /path/to/the/project ; python3 -m http.server` - - -## Goals - -There are four kinds of Matrix effects people call ["digital rain"](http://matrix.wikia.com/wiki/Matrix_code): -1. The green symbols that "rain down" operators' screens endlessly -2. Scenes from within the simulation that depict green symbols streaking across everything -3. The films' opening title graphics, which dazzle viewers and then draw them into the world of the franchise -4. The "dialing" visualization at the opening of *The Matrix* and *Resurrections* - -A motivated fan can attempt to portray any of these. However, this project focuses specifically on #1 and #3— an endless effect, visually stunning and mystifying, that feels right at home on any screen. - -The following criteria guided the development process: - -- **Get the right glyphs**. Like the actual ones. By now everyone's heard how the Matrix glyphs are some treatment of [katakana](https://en.wikipedia.org/wiki/Katakana), but they also include a few characters from [Susan Kare's Chicago typeface](https://en.wikipedia.org/wiki/Chicago_(typeface)). The Matrix glyphs in *this* project come from the source: cleaned up vectors [from an old SWF](https://web.archive.org/web/20070914173039/http://www.atari.com:80/thematrixpathofneo/) from the promotional site for an official Matrix product, archived back in 2007. That's how deep this rabbit hole goes, friends. -(Please support the [Internet Archive!](https://archive.org/about/)) -- **Get the new glyphs**. When *Resurrections* hit theaters in December 2021, it debuted an expanded glyph set with a daunting *135 symbols*. Virtually all of them were recovered from the movie trailers for this project and uploaded before the film's release! ...But they were of relatively poor quality. Fortunately, in this age of 720p reference material and tie-in marketing, a decent sized sample of new glyphs were eventually reverse-engineered from [a sparkly watch ad](https://www.hamiltonwatch.com/en-int/thematrixresurrections), and the rest were lovingly synthesized from frames of a [behind-the-scenes VFX video](https://buf.com/films/the-matrix-resurrections). -- **Make it look sweet in 2D.** The most versatile, recognizable and mesmerizing manifestation of the code rain is when it seems to pour right down your screen like rain on a windowpane. While depth effects are cool, they can obscure the details that make the difference between a goodtrix and a *greatrix*. -- **Make it look sweet in 3D, too.** To facilitate future support of stereoscopic and holographic displays, it made sense to nail down a 3D variation, but it looks pretty on any kind of display. -- **The 2D glyphs are in a *fixed grid* and *don't move*.** The "raindrops" we see in the effect are simply waves of illumination of stationary symbols that occupy a column. To get a better look at this, try setting the `fallSpeed` to a number close to 0. -- **Get the glow and color right.** Matrix symbols aren't just some shade of phosphorous green; they're first given a bloom effect, and then get tone-mapped to the green color palette. -- **Capture the proper rhythm of raindrops falling.** Multiple raindrops often occupy a column at the same time, and they may have different speeds, but they can never collide. This project achieves this with a [sawtooth wave](http://mathworld.wolfram.com/SawtoothWave.html), modulating the width of the teeth to keep things interesting. The tips of those teeth— the cells in the grid where the sawtooth dips— are where we put the "cursors" (or "tracers") at the bottom of each raindrop. -- **Capture the glyph cycling sequence.** The symbols in *Reloaded* and *Revolutions*' opening titles, which were at one point the highest fidelity versions of the 2D effect, change according to a repeating sequence (see the [unofficial glyph database](https://docs.google.com/spreadsheets/d/1NRJP88EzQlj_ghBbtjkGi-NbluZzlWpAqVIAq1MDGJc)). This is only a technical detail, and no longer drives the glyph cycle in this project, but it can be used to analyze [promotional material](https://wwws.warnerbros.co.jp/matrix-movie/news/?id=5). -- **Whip up some artistic license and imagine the "previous" Matrix versions.** The sequels describe [a paradisiacal predecessor](https://rezmason.github.io/matrix?version=paradise) to the Matrix that was too idyllic, [and another earlier, nightmarish Hobbesian version](https://rezmason.github.io/matrix?version=nightmare) that proved too campy. They depict some programs running older, differently colored code. So, this project dares to speculate how these old Matrix versions looked and acted. -- **Support a broad range of customization options, and use them to produce other noncanonical variants.** See the list of links above for the full set of available versions, and see the list below to see all the ways you can personalize the effect for yourself. -- **Make it free, open source and web based.** The [MIT License](https://github.com/Rezmason/matrix/blob/master/LICENSE) permits distribution and modification of this project. Both are highly encouraged! -- **Support as many browsers and devices as possible.** For all the flack it receives, the web is the most ubiquitous and accessible platform for sharing graphics, or anything really. This project is built on the web stack so it can reach wherever the web goes. -- **Promote a progressive interpretation of the film franchise.** *The Matrix* is an action film you can enjoy without critical analysis, but if you do read into it, you'll be rewarded. And let's be clear: **The Matrix is a story about transitioning, directed by two siblings who transitioned**. This is undeniable. Its franchise has plenty more themes, and plenty of room for interpretation, but the communities of misogynists and bigots who claim this imagery for their movements cannot be tolerated in any form. This is a chance to open minds, not shut them. - - -## Sidenote: other people's Matrix effects - -The number of implementations out there of this effect is a testament to the size of the film's impact on popular culture. For decades, I've enjoyed searching for and comparing them from time to time. That's probably how you arrived here— it's *fun* to see what kinds of solutions different people come up with to a problem, when the process is purely recreational and its success is subjective. I myself tried and failed to make the effect many times over. - -Some of the [earliest](https://github.com/ppetr/xlockmore/blob/master/modes/matrix.c), [roughest](https://github.com/Zygo/xscreensaver/blob/d1f484cfa47f4a0862140421480bb536ad66ede9/hacks/xmatrix.c) versions were made after the film hit theaters in March, but before it was released on home media in October— people were recreating the effect purely from memory. Others probably used the official screensaver as a reference, which was made by the time-strappped developers of [the (excellent, defunct) official site](https://web.archive.org/web/*/http://whatisthematrix.com) from the images and multimedia tools they had available. - - -## Customization - -You can customize the digital rain quite a bit by stapling "URL variables" to its URL— by putting a '?' at the end of the link above, and then chaining together words, like this: - -[https://rezmason.github.io/matrix/?width=100&fallSpeed=-0.1&effect=none](https://rezmason.github.io/matrix/?width=100&fallSpeed=-0.1&effect=none) - -Now you know link fu. Here's a list of customization options: - -- `version` - the version of the Matrix to simulate. Default is "classic". - - "classic" is the Matrix code everyone knows and loves, mostly based on the sequels' opening title graphics. - - "3d" is the classic code in 3D mode. - - "megacity" is a variation of the classic code that includes the Megacity as a glyph, as is seen in the opening titles of *Revolutions*. - - "operator" is more reminiscent of the matrix code as it appears in the first movie's opening titles, and on operators' screens: flatter, crowded, without a gradient, and with occasional effects (such as a square ripple). - - "nightmare" is how the Matrix may have appeared in the Merovingian's heyday: flashy, foreboding, relentless. - - "paradise" is how the Matrix's idyllic predecessor may have appeared: warm, simplistic, encompassing. - - "resurrections" is the updated Matrix code - - "palimpsest" is a custom version inspired by the art and sound of [Rob Dougan](https://en.wikipedia.org/wiki/Rob_Dougan)'s [Furious Angels](https://en.wikipedia.org/wiki/Furious_Angels). -- `skipIntro` - whether or not to start from a blank screen. Can be "true" or "false", default is *true*. -- `font` - the set of glyphs to draw. Current options are "matrixcode", "resurrections", "gothic", "coptic", "huberfishA", and "huberfishD". -- `width` - the number of columns (and rows) to draw. Default is 80. -- `volumetric` - when set to "true", this renders the glyphs with depth, slowly approaching the eye. Default is "false". -- `density` - the number of 3D raindrops to draw, proportional to the default. Default is 1.0. -- `forwardSpeed` - the rate that the 3D raindrops approach. Default is 1.0. -- `slant` - the angle that the 2D raindrops fall, in degrees. Default is 0. -- `bloomSize` - the glow quality, from 0 to 1. Default is 0.4. Lowering this value may help the digital rain run smoother on your device. -- `bloomStrength` - the glow intensity, from 0 to 1. Default is 0.7. -- `ditherMagnitude` - the amount to randomly darken pixels, to conceal [banding](https://en.wikipedia.org/wiki/Colour_banding). Default is 0.05. -- `resolution` - the image size, relative to the window size. Default is 1. Lowering this value may improve your performance, especially on high pixel density displays. -- `raindropLength` - the vertical scale of "raindrops" in the columns. Can be any number. -- `animationSpeed` - the overall speed of the animation. Can be any number. -- `fallSpeed` - the speed of the rain's descent. Can be any number. -- `cycleSpeed` - the speed that the glyphs change their symbol. Can be any number. -- `effect` - alternatives to the default post-processing effect. Can be "plain", "pride", "stripes", "none", "image" or "mirror". - - ("none" displays the 'debug view', a behind-the-scenes look at the anatomy of the effect.) -- `camera` - some effects, ie. the mirror effect, optionally support webcam input. Can be "true" or "false". Default is false. -- `stripeColors` - if you set the effect to "stripes", you can specify the colors of vertical stripes as alternating *R,G,B* numeric values, like so: [https://rezmason.github.io/matrix/?effect=stripes&stripeColors=1,0,0,1,1,0,0,1,0](https://rezmason.github.io/matrix/?effect=stripes&stripeColors=1,0,0,1,1,0,0,1,0) -- `palette` — with the normal "palette" effect, you can specify the colors and placement of the colors along the color grade as alternating *R,G,B,%* numeric values, like so: [https://rezmason.github.io/matrix/?palette=0.1,0,0.2,0,0.2,0.5,0,0.5,1,0.7,0,1](https://rezmason.github.io/matrix/?palette=0.1,0,0.2,0,0.2,0.5,0,0.5,1,0.7,0,1) -- `backgroundColor`, `cursorColor`, `glintColor` — other *R,G,B* values that apply to the corresponding parts of the effect. -- `paletteHSL`, `stripeHSL`, `backgroundHSL`, `cursorHSL`, and `glintHSL` — the same as the above, except they use *H,S,L* (hue, saturation, lightness) instead of *R,G,B*. -- `cursorIntensity`, — the brightness of cursors' glow. Can be any number greater than zero. Default is 2.0. -- `glintIntensity` — the brightness of glint glow, if there is any glint. Can be any number greater than zero. Default is 1.0. -- `url` - if you set the effect to "image", this is how you specify which image to load. It doesn't work with any URL; I suggest grabbing them from Wikipedia: [https://rezmason.github.io/matrix/?effect=image&url=https://upload.wikimedia.org/wikipedia/commons/f/f5/EagleRock.jpg](https://rezmason.github.io/matrix/?effect=image&url=https://upload.wikimedia.org/wikipedia/commons/f/f5/EagleRock.jpg) -- `loops` - (WIP) if set to "true", this causes the effect to loop, so that it can be converted into a looping video. -- `fps` — the framerate of the effect. Can be any number between 0 and 60. Default is 60. -- `suppressWarnings` - if set to "true", this suppresses any warnings that would otherwise appear— when viewing the project on a device with no GPU, for example. - -## Troubleshooting - -There haven't been many reported issues yet that weren't quick fixes, but one has stood out: many visitors have previously *disabled hardware acceleration* in their Chrome browsers, at the advice of well-meaning Internet websites. - -What this does is cause Chrome to fall back to **SwiftShader**, a software renderer that runs projects like this one at a much slower rate. Because of this, if you are seeing serious performance issues on Chrome, it's recommended that you ensure hardware acceleration is enabled in your browser settings. - -## Future directions - -This project is still in active development, but some upcoming features are worth mentioning. - -- **An audio element.** Things make sounds, don't they? Yes, they do, especially in movies. And while silence is precious, there are plans to provide a setting that introduces some kind of pleasant audio treatment to the effect. -- **A user interface that isn't a URL.** This project supports a lot of configurable options under the hood, and it would be wise to add a fun looking UI that exposes them all to visitors in an intuitive way. - - -## Friends of the project - -- Vesuveus was gracious to [spend time discussing this project](https://anchor.fm/vesuveusmxo/episodes/Podcast-Episode-5---Rezmason--Matrix-Code-e1i3iia) and the effect that inspires it on his long-running podcast, [The Matrix Online Revisited with Vesuveus](https://anchor.fm/vesuveusmxo). Fandom is the interwoven story of people interacting with a piece of media, and Vesuveus keeps ours alive and gives them perspective. -- Alexi García's [stunning in-depth comparison](https://bit.ly/MatrixVersions) of the many home video releases of *The Matrix* is a must-see for any fan of the franchise, and those curious about how a movie can subtly change over time. Alexi's diligence and familiarity with the material are to thank for the high-fidelity references and high-fidelity feedback that have helped shape this project. Visit [his main site](https://alxcia.wordpress.com/) for more information. -- GitHub user 57r31 produced a proof of concept that led to the [interactive mirror effect](https://rezmason.github.io/matrix/?version=updated&effect=mirror). - - -## Colophon - -The Coptic glyphs in the "Paradise Matrix" version are derived from [George Douros's font "Symbola"](http://users.teilar.gr/~g1951d), due to their similarity to the script in [CG II of Nag Hammadi](https://en.wikipedia.org/wiki/Nag_Hammadi_Codex_II). If a 4th century Gnostic scribe trolled Athanasius over IRC, it might look like this. - -The Gothic glyphs in the "Nightmare Matrix" version are derived from [Dr. jur. Robert Pfeffer's font "Silubur"](http://www.robert-pfeffer.net/gotica/englisch/index.html), which are inspired by the uncial script found in the [Codex Argenteus](https://en.wikipedia.org/wiki/Codex_Argenteus). If a werewolf emailed a vampire in the 6th century, it might look like this. - -The glyphs used in the "Palimpsest" and "Twilight" versions are derived from [Teague Chrystie's font "Huberfish"](http://robotsoup.com/huberfish.html), a fictitious alphabet that comes in several styles. If a spacedock technician bought a soda from a vending machine in an cool utopian future that will never happen, it might look like this. - - -## Other details - -The glyphs are formatted as a multi-channel distance field (or MSDF) via Victor Chlumsky's [msdfgen](https://github.com/Chlumsky/msdfgen). This format preserves the crisp edges and corners of vector graphics when rendered as textures. Chlumsky's thesis paper, which is in English and is also easy to read, is [available to download here](https://dspace.cvut.cz/handle/10467/62770). - -The raindrops themselves are particles [computed on the GPU and stored in textures](https://threejs.org/examples/webgl_gpgpu_water.html), much smaller than the final render. The data sent from the CPU to the GPU every frame is negligible. diff --git a/TODO.txt b/TODO.txt index 1371c67..2db24f7 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,79 +1,10 @@ TODO: -Live config update roadmap - Modify regl pass - async build(config, inputs) - loads all required stuff - async resize - adjusts the sizes - run — performs the GPU operation - Rewrite main code to accept changes to config - Build a Tweakpane for config - https://cocopon.github.io/tweakpane - Show a gear emoji in the top right on mouse move - Hide after three seconds - If tapped, show the tweakpane - -Seems like bloom size and resolution impact the REGL and WebGPU bloom implementations differently - Move high pass into WebGPU bloom - -Dithering bug on mobile? - Looks like a subtle fade occurring at a per-second interval - -Write an explanation of the rain pass (and include images) - Compute - Volumetric quads - Fullscreen quad and spacial mapping - MSDFs - -Audio system - Toggle (or number representing frequency) - Load the sound effect - Play it multiple times with delay - Random pan - Random volume? - Randomize pitch a little? - -Playdate version - Audio system - Falling sound - Launch sound - Undocking sound - Docking sound - Maybe crank sounds? Not sure yet - -Support Resurrections SDF bevel and "lights" - https://shaderfun.com/2018/07/23/signed-distance-fields-part-8-gradients-bevels-and-noise/ - Get the "normals" and color right - Note: even completely dark glyphs can have glint on their edges - "Golden hour" - Stuff MSDF-derived normal vectors from rain pass frag into two of its output channels - Anomaly mode toggles between this and anomaly streaks - -WebGPU - Try https://github.com/brendan-duncan/wgsl_reflect - Get rid of end pass once it's possible to copy a bgra8unorm to a canvas texture - Switch to rgba32float somehow? - Why isn't this straightforward? - Try shorthand - Share a bind group and layout just for time? - Try using a buffer for the stripe pass 1D texture - -Improve loop support - -Idea: Build a UI - Replace versions with presets - Simple changes update the values - Complex changes replace the pipeline - Make it a form, so it's accessible - Then, make it look cool like the UI from the old site - -Maybe pay someone to make Mac/Windows screensavers - -Zion Control's matrix variant - From Reloaded - -gpu-buffer, working title - Support type aliasing (type Q = array) - Support shorthand (vec4f) - Build mocha tests, example project - Give it its own repo, microsite +Simplify! + Get rid of everything inessential + Remove WebGPU + Remove config options + Remove features + Remove subsystems + Get as much into one file as you possibly can + Remove dependencies diff --git a/assets/Matrix-Code.ttf b/assets/Matrix-Code.ttf deleted file mode 100644 index 95a3a5e..0000000 Binary files a/assets/Matrix-Code.ttf and /dev/null differ diff --git a/assets/Matrix-Resurrected.ttf b/assets/Matrix-Resurrected.ttf deleted file mode 100644 index 58dad20..0000000 Binary files a/assets/Matrix-Resurrected.ttf and /dev/null differ diff --git a/assets/coptic_msdf.png b/assets/coptic_msdf.png deleted file mode 100644 index ec64e51..0000000 Binary files a/assets/coptic_msdf.png and /dev/null differ diff --git a/assets/gothic_msdf.png b/assets/gothic_msdf.png deleted file mode 100644 index b551247..0000000 Binary files a/assets/gothic_msdf.png and /dev/null differ diff --git a/assets/gtarg_alientext_msdf.png b/assets/gtarg_alientext_msdf.png deleted file mode 100644 index de0ce09..0000000 Binary files a/assets/gtarg_alientext_msdf.png and /dev/null differ diff --git a/assets/gtarg_tenretniolleh_msdf.png b/assets/gtarg_tenretniolleh_msdf.png deleted file mode 100644 index 9975f93..0000000 Binary files a/assets/gtarg_tenretniolleh_msdf.png and /dev/null differ diff --git a/assets/huberfish_a_msdf.png b/assets/huberfish_a_msdf.png deleted file mode 100644 index 51001c3..0000000 Binary files a/assets/huberfish_a_msdf.png and /dev/null differ diff --git a/assets/huberfish_d_msdf.png b/assets/huberfish_d_msdf.png deleted file mode 100644 index d2c9baa..0000000 Binary files a/assets/huberfish_d_msdf.png and /dev/null differ diff --git a/assets/megacity_msdf.png b/assets/megacity_msdf.png deleted file mode 100644 index f43be39..0000000 Binary files a/assets/megacity_msdf.png and /dev/null differ diff --git a/assets/mesh.png b/assets/mesh.png deleted file mode 100644 index c126591..0000000 Binary files a/assets/mesh.png and /dev/null differ diff --git a/assets/metal.png b/assets/metal.png deleted file mode 100644 index c70b3ef..0000000 Binary files a/assets/metal.png and /dev/null differ diff --git a/assets/msdf_command.txt b/assets/msdf_command.txt deleted file mode 100644 index a2e5ef5..0000000 --- a/assets/msdf_command.txt +++ /dev/null @@ -1,3 +0,0 @@ -./msdfgen/out/msdfgen msdf -svg ./svg\ sources/texture_simplified.svg -size 512 512 -pxrange 4 -o ./matrixcode_msdf.png -./msdfgen/out/msdfgen msdf -svg ./svg\ sources/gothic_texture_simplified.svg -size 512 512 -pxrange 4 -o ./gothic_msdf.png -./msdfgen/out/msdfgen msdf -svg ./svg\ sources/coptic_texture_simplified.svg -size 512 512 -pxrange 4 -o ./coptic_msdf.png diff --git a/assets/neomatrixology_msdf.png b/assets/neomatrixology_msdf.png deleted file mode 100644 index 861bcc6..0000000 Binary files a/assets/neomatrixology_msdf.png and /dev/null differ diff --git a/assets/pixel_grid.png b/assets/pixel_grid.png deleted file mode 100644 index 076aa9a..0000000 Binary files a/assets/pixel_grid.png and /dev/null differ diff --git a/assets/resurrections_glint_msdf.png b/assets/resurrections_glint_msdf.png deleted file mode 100644 index ff10297..0000000 Binary files a/assets/resurrections_glint_msdf.png and /dev/null differ diff --git a/assets/sand.png b/assets/sand.png deleted file mode 100644 index 0bc9696..0000000 Binary files a/assets/sand.png and /dev/null differ diff --git a/glyph order.txt b/glyph order.txt deleted file mode 100644 index c55a9de..0000000 --- a/glyph order.txt +++ /dev/null @@ -1,5 +0,0 @@ -Reloaded/Revolutions: -モエヤキオカ7ケサスz152ヨタワ4ネヌナ98ヒ0ホア3ウ セ¦:"꞊ミラリ╌ツテニハソ▪—<>0|+*コシマムメ - -Resurrections: -モエヤキオカ7ケサスz152ヨタワ4ネヌナ98ヒ0ホア3ウ セ¦:"꞊ミラリ╌ツテニハソコ—<ム0|*▪メシマ>+ diff --git a/js/camera.js b/js/camera.js deleted file mode 100644 index 3aeeaa1..0000000 --- a/js/camera.js +++ /dev/null @@ -1,45 +0,0 @@ -// TODO: switch to video-based texture -// TODO: mipmap? -const video = document.createElement("video"); -const cameraCanvas = document.createElement("canvas"); -cameraCanvas.width = 1; -cameraCanvas.height = 1; -const context = cameraCanvas.getContext("2d"); -let cameraAspectRatio = 1.0; -const cameraSize = [1, 1]; - -const drawToCanvas = () => { - requestAnimationFrame(drawToCanvas); - context.drawImage(video, 0, 0); -}; - -const setupCamera = async () => { - try { - const stream = await navigator.mediaDevices.getUserMedia({ - video: { - width: { min: 800, ideal: 1280 }, - frameRate: { ideal: 60 }, - }, - audio: false, - }); - const videoTrack = stream.getVideoTracks()[0]; - const { width, height } = videoTrack.getSettings(); - - video.width = width; - video.height = height; - cameraCanvas.width = width; - cameraCanvas.height = height; - cameraAspectRatio = width / height; - cameraSize[0] = width; - cameraSize[1] = height; - - video.srcObject = stream; - video.play(); - - drawToCanvas(); - } catch (e) { - console.warn(`Camera not initialized: ${e}`); - } -}; - -export { cameraCanvas, cameraAspectRatio, cameraSize, setupCamera }; diff --git a/js/regl/imagePass.js b/js/regl/imagePass.js deleted file mode 100644 index f78106f..0000000 --- a/js/regl/imagePass.js +++ /dev/null @@ -1,33 +0,0 @@ -import { loadImage, loadText, makePassFBO, makePass } from "./utils.js"; - -// Multiplies the rendered rain and bloom by a loaded in image - -const defaultBGURL = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/0a/Flammarion_Colored.jpg/917px-Flammarion_Colored.jpg"; - -export default ({ regl, config }, inputs) => { - const output = makePassFBO(regl, config.useHalfFloat); - const bgURL = "bgURL" in config ? config.bgURL : defaultBGURL; - const background = loadImage(regl, bgURL); - const imagePassFrag = loadText("shaders/glsl/imagePass.frag.glsl"); - const render = regl({ - frag: regl.prop("frag"), - uniforms: { - backgroundTex: background.texture, - tex: inputs.primary, - bloomTex: inputs.bloom, - }, - framebuffer: output, - }); - return makePass( - { - primary: output, - }, - Promise.all([background.loaded, imagePassFrag.loaded]), - (w, h) => output.resize(w, h), - (shouldRender) => { - if (shouldRender) { - render({ frag: imagePassFrag.text() }); - } - } - ); -}; diff --git a/js/regl/lkgHelper.js b/js/regl/lkgHelper.js deleted file mode 100644 index c8636c2..0000000 --- a/js/regl/lkgHelper.js +++ /dev/null @@ -1,89 +0,0 @@ -const recordedDevice = { - buttons: [0, 0, 0, 0], - calibration: { - DPI: { value: 324 }, - center: { value: 0.15018756687641144 }, - configVersion: "3.0", - flipImageX: { value: 0 }, - flipImageY: { value: 0 }, - flipSubp: { value: 0 }, - fringe: { value: 0 }, - invView: { value: 1 }, - pitch: { value: 52.58013153076172 }, - screenH: { value: 2048 }, - screenW: { value: 1536 }, - slope: { value: -7.145165920257568 }, - verticalAngle: { value: 0 }, - viewCone: { value: 40 }, - }, - defaultQuilt: { - quiltAspect: 0.75, - quiltX: 3840, - quiltY: 3840, - tileX: 8, - tileY: 6, - }, - hardwareVersion: "portrait", - hwid: "LKG-P11063", - index: 0, - joystickIndex: -1, - state: "ok", - unityIndex: 1, - windowCoords: [1440, 900], -}; - -const interpretDevice = (device) => { - if (device == null) { - return { enabled: false, tileX: 1, tileY: 1 }; - } - - const fov = 15; - - const calibration = Object.fromEntries( - Object.entries(device.calibration) - .map(([key, value]) => [key, value.value]) - .filter(([key, value]) => value != null) - ); - - const screenInches = calibration.screenW / calibration.DPI; - const pitch = calibration.pitch * screenInches * Math.cos(Math.atan(1.0 / calibration.slope)); - const tilt = (calibration.screenH / (calibration.screenW * calibration.slope)) * -(calibration.flipImageX * 2 - 1); - const subp = 1 / (calibration.screenW * 3); - - const defaultQuilt = device.defaultQuilt; - - const quiltViewPortion = [ - (Math.floor(defaultQuilt.quiltX / defaultQuilt.tileX) * defaultQuilt.tileX) / defaultQuilt.quiltX, - (Math.floor(defaultQuilt.quiltY / defaultQuilt.tileY) * defaultQuilt.tileY) / defaultQuilt.quiltY, - ]; - - return { - ...defaultQuilt, - ...calibration, - pitch, - tilt, - subp, - - quiltViewPortion, - fov, - enabled: true, - }; -}; - -export default async (useHoloplay = false, useRecordedDevice = false) => { - if (!useHoloplay) { - return interpretDevice(null); - } - const HoloPlayCore = await import("../../lib/holoplaycore.module.js"); - const device = await new Promise( - (resolve, reject) => - new HoloPlayCore.Client( - (data) => resolve(data.devices?.[0]), - (error) => resolve(null) - ) - ); - if (device == null && useRecordedDevice) { - return interpretDevice(recordedDevice); - } - return interpretDevice(device); -}; diff --git a/js/regl/main.js b/js/regl/main.js index 9caec17..424a688 100644 --- a/js/regl/main.js +++ b/js/regl/main.js @@ -3,25 +3,6 @@ import { makeFullScreenQuad, makePipeline } from "./utils.js"; import makeRain from "./rainPass.js"; import makeBloomPass from "./bloomPass.js"; import makePalettePass from "./palettePass.js"; -import makeStripePass from "./stripePass.js"; -import makeImagePass from "./imagePass.js"; -import makeQuiltPass from "./quiltPass.js"; -import makeMirrorPass from "./mirrorPass.js"; -import { setupCamera, cameraCanvas, cameraAspectRatio } from "../camera.js"; -import getLKG from "./lkgHelper.js"; - -const effects = { - none: null, - plain: makePalettePass, - palette: makePalettePass, - customStripes: makeStripePass, - stripes: makeStripePass, - pride: makeStripePass, - transPride: makeStripePass, - trans: makeStripePass, - image: makeImagePass, - mirror: makeMirrorPass, -}; const dimensions = { width: 1, height: 1 }; @@ -35,7 +16,7 @@ const loadJS = (src) => }); export default async (canvas, config) => { - await Promise.all([loadJS("lib/regl.min.js"), loadJS("lib/gl-matrix.js")]); + await Promise.all([loadJS("lib/regl.js"), loadJS("lib/gl-matrix.js")]); const resize = () => { const devicePixelRatio = window.devicePixelRatio ?? 1; @@ -78,14 +59,10 @@ export default async (canvas, config) => { const regl = createREGL({ canvas, pixelRatio: 1, extensions, optionalExtensions }); - const cameraTex = regl.texture(cameraCanvas); - const lkg = await getLKG(config.useHoloplay, true); - // All this takes place in a full screen quad. const fullScreenQuad = makeFullScreenQuad(regl); - const effectName = config.effect in effects ? config.effect : "palette"; - const context = { regl, config, lkg, cameraTex, cameraAspectRatio }; - const pipeline = makePipeline(context, [makeRain, makeBloomPass, effects[effectName], makeQuiltPass]); + const context = { regl, config }; + const pipeline = makePipeline(context, [makeRain, makeBloomPass, makePalettePass]); const screenUniforms = { tex: pipeline[pipeline.length - 1].outputs.primary }; const drawToScreen = regl({ uniforms: screenUniforms }); await Promise.all(pipeline.map((step) => step.ready)); diff --git a/js/regl/mirrorPass.js b/js/regl/mirrorPass.js deleted file mode 100644 index 16af2e1..0000000 --- a/js/regl/mirrorPass.js +++ /dev/null @@ -1,50 +0,0 @@ -import { loadText, makePassFBO, makePass } from "./utils.js"; - -let start; -const numClicks = 5; -const clicks = Array(numClicks).fill([0, 0, -Infinity]).flat(); -let aspectRatio = 1; - -let index = 0; -window.onclick = (e) => { - clicks[index * 3 + 0] = 0 + e.clientX / e.srcElement.clientWidth; - clicks[index * 3 + 1] = 1 - e.clientY / e.srcElement.clientHeight; - clicks[index * 3 + 2] = (Date.now() - start) / 1000; - index = (index + 1) % numClicks; -}; - -export default ({ regl, config, cameraTex, cameraAspectRatio }, inputs) => { - const output = makePassFBO(regl, config.useHalfFloat); - const mirrorPassFrag = loadText("shaders/glsl/mirrorPass.frag.glsl"); - const render = regl({ - frag: regl.prop("frag"), - uniforms: { - time: regl.context("time"), - tex: inputs.primary, - bloomTex: inputs.bloom, - cameraTex, - clicks: () => clicks, - aspectRatio: () => aspectRatio, - cameraAspectRatio, - }, - framebuffer: output, - }); - - start = Date.now(); - - return makePass( - { - primary: output, - }, - Promise.all([mirrorPassFrag.loaded]), - (w, h) => { - output.resize(w, h); - aspectRatio = w / h; - }, - (shouldRender) => { - if (shouldRender) { - render({ frag: mirrorPassFrag.text() }); - } - } - ); -}; diff --git a/js/regl/quiltPass.js b/js/regl/quiltPass.js deleted file mode 100644 index fcef01c..0000000 --- a/js/regl/quiltPass.js +++ /dev/null @@ -1,34 +0,0 @@ -import { loadText, makePassFBO, makePass } from "./utils.js"; - -// Multiplies the rendered rain and bloom by a loaded in image - -export default ({ regl, config, lkg }, inputs) => { - if (!lkg.enabled) { - return makePass({ - primary: inputs.primary, - }); - } - - const output = makePassFBO(regl, config.useHalfFloat); - const quiltPassFrag = loadText("shaders/glsl/quiltPass.frag.glsl"); - const render = regl({ - frag: regl.prop("frag"), - uniforms: { - quiltTexture: inputs.primary, - ...lkg, - }, - framebuffer: output, - }); - return makePass( - { - primary: output, - }, - Promise.all([quiltPassFrag.loaded]), - (w, h) => output.resize(w, h), - (shouldRender) => { - if (shouldRender) { - render({ frag: quiltPassFrag.text() }); - } - } - ); -}; diff --git a/js/regl/rainPass.js b/js/regl/rainPass.js index 0a49363..e5c9b03 100644 --- a/js/regl/rainPass.js +++ b/js/regl/rainPass.js @@ -29,7 +29,7 @@ const blVert = [1, 0]; const brVert = [1, 1]; const quadVertices = [tlVert, trVert, brVert, tlVert, brVert, blVert]; -export default ({ regl, config, lkg }) => { +export default ({ regl, config }) => { // The volumetric mode multiplies the number of columns // to reach the desired density, and then overlaps them const volumetric = config.volumetric; @@ -198,8 +198,6 @@ export default ({ regl, config, lkg }) => { screenSize: regl.prop("screenSize"), }, - viewport: regl.prop("viewport"), - attributes: { aPosition: quadPositions, aCorner: Array(numQuads).fill(quadVertices), @@ -218,17 +216,11 @@ export default ({ regl, config, lkg }) => { mat4.rotateY(transform, transform, (Math.PI * 1) / 4); mat4.translate(transform, transform, vec3.fromValues(0, 0, -1)); mat4.scale(transform, transform, vec3.fromValues(1, 1, 2)); - } else if (lkg.enabled) { - mat4.translate(transform, transform, vec3.fromValues(0, 0, -1.1)); - mat4.scale(transform, transform, vec3.fromValues(1, 1, 1)); - mat4.scale(transform, transform, vec3.fromValues(0.15, 0.15, 0.15)); } else { mat4.translate(transform, transform, vec3.fromValues(0, 0, -1)); } const camera = mat4.create(); - const vantagePoints = []; - return makePass( { primary: output, @@ -248,48 +240,16 @@ export default ({ regl, config, lkg }) => { output.resize(w, h); const aspectRatio = w / h; - const [numTileColumns, numTileRows] = [lkg.tileX, lkg.tileY]; - const numVantagePoints = numTileRows * numTileColumns; - const tileWidth = Math.floor(w / numTileColumns); - const tileHeight = Math.floor(h / numTileRows); - vantagePoints.length = 0; - for (let row = 0; row < numTileRows; row++) { - for (let column = 0; column < numTileColumns; column++) { - const index = column + row * numTileColumns; - const camera = mat4.create(); - - if (volumetric && config.isometric) { - if (aspectRatio > 1) { - mat4.ortho(camera, -1.5 * aspectRatio, 1.5 * aspectRatio, -1.5, 1.5, -1000, 1000); - } else { - mat4.ortho(camera, -1.5, 1.5, -1.5 / aspectRatio, 1.5 / aspectRatio, -1000, 1000); - } - } else if (lkg.enabled) { - mat4.perspective(camera, (Math.PI / 180) * lkg.fov, lkg.quiltAspect, 0.0001, 1000); - - const distanceToTarget = -1; // TODO: Get from somewhere else - let vantagePointAngle = (Math.PI / 180) * lkg.viewCone * (index / (numVantagePoints - 1) - 0.5); - if (isNaN(vantagePointAngle)) { - vantagePointAngle = 0; - } - const xOffset = distanceToTarget * Math.tan(vantagePointAngle); - - mat4.translate(camera, camera, vec3.fromValues(xOffset, 0, 0)); - - camera[8] = -xOffset / (distanceToTarget * Math.tan((Math.PI / 180) * 0.5 * lkg.fov) * lkg.quiltAspect); // Is this right?? - } else { - mat4.perspective(camera, (Math.PI / 180) * 90, aspectRatio, 0.0001, 1000); - } - - const viewport = { - x: column * tileWidth, - y: row * tileHeight, - width: tileWidth, - height: tileHeight, - }; - vantagePoints.push({ camera, viewport }); + if (volumetric && config.isometric) { + if (aspectRatio > 1) { + mat4.ortho(camera, -1.5 * aspectRatio, 1.5 * aspectRatio, -1.5, 1.5, -1000, 1000); + } else { + mat4.ortho(camera, -1.5, 1.5, -1.5 / aspectRatio, 1.5 / aspectRatio, -1000, 1000); } + } else { + mat4.perspective(camera, (Math.PI / 180) * 90, aspectRatio, 0.0001, 1000); } + [screenSize[0], screenSize[1]] = aspectRatio > 1 ? [1, aspectRatio] : [1 / aspectRatio, 1]; }, (shouldRender) => { @@ -305,9 +265,7 @@ export default ({ regl, config, lkg }) => { framebuffer: output, }); - for (const vantagePoint of vantagePoints) { - render({ ...vantagePoint, transform, screenSize, vert: rainPassVert.text(), frag: rainPassFrag.text() }); - } + render({ camera, transform, screenSize, vert: rainPassVert.text(), frag: rainPassFrag.text() }); } } ); diff --git a/js/regl/stripePass.js b/js/regl/stripePass.js deleted file mode 100644 index a45dd61..0000000 --- a/js/regl/stripePass.js +++ /dev/null @@ -1,73 +0,0 @@ -import colorToRGB from "../colorToRGB.js"; -import { loadText, make1DTexture, makePassFBO, makePass } from "./utils.js"; - -// Multiplies the rendered rain and bloom by a 1D gradient texture -// generated from the passed-in color sequence - -// This shader introduces noise into the renders, to avoid banding - -const transPrideStripeColors = [ - { space: "rgb", values: [0.36, 0.81, 0.98] }, - { space: "rgb", values: [0.96, 0.66, 0.72] }, - { space: "rgb", values: [1.0, 1.0, 1.0] }, - { space: "rgb", values: [0.96, 0.66, 0.72] }, - { space: "rgb", values: [0.36, 0.81, 0.98] }, -] - .map((color) => Array(3).fill(color)) - .flat(); - -const prideStripeColors = [ - { space: "rgb", values: [0.89, 0.01, 0.01] }, - { space: "rgb", values: [1.0, 0.55, 0.0] }, - { space: "rgb", values: [1.0, 0.93, 0.0] }, - { space: "rgb", values: [0.0, 0.5, 0.15] }, - { space: "rgb", values: [0.0, 0.3, 1.0] }, - { space: "rgb", values: [0.46, 0.03, 0.53] }, -] - .map((color) => Array(2).fill(color)) - .flat(); - -export default ({ regl, config }, inputs) => { - const output = makePassFBO(regl, config.useHalfFloat); - - const { backgroundColor, cursorColor, glintColor, cursorIntensity, glintIntensity, ditherMagnitude } = config; - - // Expand and convert stripe colors into 1D texture data - const stripeColors = "stripeColors" in config ? config.stripeColors : config.effect === "pride" ? prideStripeColors : transPrideStripeColors; - const stripeTex = make1DTexture( - regl, - stripeColors.map((color) => [...colorToRGB(color), 1]) - ); - - const stripePassFrag = loadText("shaders/glsl/stripePass.frag.glsl"); - - const render = regl({ - frag: regl.prop("frag"), - - uniforms: { - backgroundColor: colorToRGB(backgroundColor), - cursorColor: colorToRGB(cursorColor), - glintColor: colorToRGB(glintColor), - cursorIntensity, - glintIntensity, - ditherMagnitude, - tex: inputs.primary, - bloomTex: inputs.bloom, - stripeTex, - }, - framebuffer: output, - }); - - return makePass( - { - primary: output, - }, - stripePassFrag.loaded, - (w, h) => output.resize(w, h), - (shouldRender) => { - if (shouldRender) { - render({ frag: stripePassFrag.text() }); - } - } - ); -}; diff --git a/js/webgpu/bloomPass.js b/js/webgpu/bloomPass.js deleted file mode 100644 index 78c3a4e..0000000 --- a/js/webgpu/bloomPass.js +++ /dev/null @@ -1,162 +0,0 @@ -import { structs } from "../../lib/gpu-buffer.js"; -import { makeComputeTarget, loadShader, makeUniformBuffer, makeBindGroup, makePass } from "./utils.js"; - -// const makePyramid = makeComputeTarget; - -// const destroyPyramid = (pyramid) => pyramid?.destroy(); - -// const makePyramidLevelView = (pyramid, level) => -// pyramid.createView({ -// baseMipLevel: level, -// mipLevelCount: 1, -// dimension: "2d", -// }); - -// const makePyramidViews = (pyramid) => [pyramid.createView()]; - -const makePyramid = (device, size, pyramidHeight) => - Array(pyramidHeight) - .fill() - .map((_, index) => - makeComputeTarget( - device, - size.map((x) => Math.floor(x * 2 ** -index)) - ) - ); - -const destroyPyramid = (pyramid) => pyramid?.forEach((texture) => texture.destroy()); - -const makePyramidLevelView = (pyramid, level) => pyramid[level].createView(); - -const makePyramidViews = (pyramid) => pyramid.map((tex) => tex.createView()); - -// The bloom pass is basically an added blur of the rain pass's high-pass output. -// The blur approximation is the sum of a pyramid of downscaled, blurred textures. - -export default ({ config, device }) => { - const pyramidHeight = 4; - const bloomSize = config.bloomSize; - const bloomStrength = config.bloomStrength; - const bloomRadius = 2; // Looks better with more, but is more costly - - const enabled = bloomSize > 0 && bloomStrength > 0; - - // If there's no bloom to apply, return a no-op pass with an empty bloom texture - if (!enabled) { - const emptyTexture = makeComputeTarget(device, [1, 1]); - return makePass("No Bloom", null, (size, inputs) => ({ ...inputs, bloom: emptyTexture })); - } - - const assets = [loadShader(device, "shaders/wgsl/bloomBlur.wgsl"), loadShader(device, "shaders/wgsl/bloomCombine.wgsl")]; - - const linearSampler = device.createSampler({ - magFilter: "linear", - minFilter: "linear", - }); - - // The blur pipeline applies a blur in one direction; it's applied horizontally - // to the first image pyramid, and then vertically to the second image pyramid. - let blurPipeline; - let hBlurPyramid; - let vBlurPyramid; - let hBlurBuffer; - let vBlurBuffer; - let hBlurBindGroups; - let vBlurBindGroups; - - // The combine pipeline blends the last image pyramid's layers into the output. - let combinePipeline; - let combineBuffer; - let combineBindGroup; - let output; - let scaledScreenSize; - - const loaded = (async () => { - const [blurShader, combineShader] = await Promise.all(assets); - - [blurPipeline, combinePipeline] = await Promise.all([ - device.createComputePipeline({ - layout: "auto", - compute: { - module: blurShader.module, - entryPoint: "computeMain", - }, - }), - - device.createComputePipeline({ - layout: "auto", - compute: { - module: combineShader.module, - entryPoint: "computeMain", - }, - }), - ]); - - const blurUniforms = structs.from(blurShader.code).Config; - hBlurBuffer = makeUniformBuffer(device, blurUniforms, { bloomRadius, direction: [1, 0] }); - vBlurBuffer = makeUniformBuffer(device, blurUniforms, { bloomRadius, direction: [0, 1] }); - - const combineUniforms = structs.from(combineShader.code).Config; - combineBuffer = makeUniformBuffer(device, combineUniforms, { pyramidHeight, bloomStrength }); - })(); - - const build = (screenSize, inputs) => { - // Since the bloom is blurry, we downscale everything - scaledScreenSize = screenSize.map((x) => Math.floor(x * bloomSize)); - - destroyPyramid(hBlurPyramid); - hBlurPyramid = makePyramid(device, scaledScreenSize, pyramidHeight); - - destroyPyramid(vBlurPyramid); - vBlurPyramid = makePyramid(device, scaledScreenSize, pyramidHeight); - - output?.destroy(); - output = makeComputeTarget(device, scaledScreenSize); - - hBlurBindGroups = []; - vBlurBindGroups = []; - - // The first pyramid's level 1 texture is the input texture blurred. - // The subsequent levels of the pyramid are the preceding level blurred. - let srcView = inputs.highPass.createView(); - for (let i = 0; i < pyramidHeight; i++) { - const hBlurPyramidView = makePyramidLevelView(hBlurPyramid, i); - const vBlurPyramidView = makePyramidLevelView(vBlurPyramid, i); - hBlurBindGroups[i] = makeBindGroup(device, blurPipeline, 0, [hBlurBuffer, linearSampler, srcView, hBlurPyramidView]); - vBlurBindGroups[i] = makeBindGroup(device, blurPipeline, 0, [vBlurBuffer, linearSampler, hBlurPyramidView, vBlurPyramidView]); - srcView = hBlurPyramidView; - } - - combineBindGroup = makeBindGroup(device, combinePipeline, 0, [combineBuffer, linearSampler, ...makePyramidViews(vBlurPyramid), output.createView()]); - - return { - ...inputs, - bloom: output, - }; - }; - - const run = (encoder, shouldRender) => { - if (!shouldRender) { - return; - } - - const computePass = encoder.beginComputePass(); - - computePass.setPipeline(blurPipeline); - for (let i = 0; i < pyramidHeight; i++) { - const dispatchSize = [Math.ceil(Math.floor(scaledScreenSize[0] * 2 ** -i) / 32), Math.floor(Math.floor(scaledScreenSize[1] * 2 ** -i)), 1]; - computePass.setBindGroup(0, hBlurBindGroups[i]); - computePass.dispatchWorkgroups(...dispatchSize); - computePass.setBindGroup(0, vBlurBindGroups[i]); - computePass.dispatchWorkgroups(...dispatchSize); - } - - computePass.setPipeline(combinePipeline); - computePass.setBindGroup(0, combineBindGroup); - computePass.dispatchWorkgroups(Math.ceil(scaledScreenSize[0] / 32), scaledScreenSize[1], 1); - - computePass.end(); - }; - - return makePass("Bloom", loaded, build, run); -}; diff --git a/js/webgpu/endPass.js b/js/webgpu/endPass.js deleted file mode 100644 index 5030aad..0000000 --- a/js/webgpu/endPass.js +++ /dev/null @@ -1,66 +0,0 @@ -import { loadShader, makeBindGroup, makePass } from "./utils.js"; - -// Eventually, WebGPU will allow the output of the final pass in the pipeline to be copied to the canvas texture. -// Until then, this render pass does the job. - -const numVerticesPerQuad = 2 * 3; - -export default ({ device, canvasFormat, canvasContext }) => { - const nearestSampler = device.createSampler(); - - const renderPassConfig = { - colorAttachments: [ - { - // view: null, - loadOp: "clear", - storeOp: "store", - }, - ], - }; - - let renderPipeline; - let renderBindGroup; - - const assets = [loadShader(device, "shaders/wgsl/endPass.wgsl")]; - - const loaded = (async () => { - const [imageShader] = await Promise.all(assets); - - renderPipeline = await device.createRenderPipelineAsync({ - layout: "auto", - vertex: { - module: imageShader.module, - entryPoint: "vertMain", - }, - fragment: { - module: imageShader.module, - entryPoint: "fragMain", - targets: [ - { - format: canvasFormat, - }, - ], - }, - }); - })(); - - const build = (size, inputs) => { - renderBindGroup = makeBindGroup(device, renderPipeline, 0, [nearestSampler, inputs.primary.createView()]); - return null; - }; - - const run = (encoder, shouldRender) => { - if (!shouldRender) { - return; - } - - renderPassConfig.colorAttachments[0].view = canvasContext.getCurrentTexture().createView(); - const renderPass = encoder.beginRenderPass(renderPassConfig); - renderPass.setPipeline(renderPipeline); - renderPass.setBindGroup(0, renderBindGroup); - renderPass.draw(numVerticesPerQuad, 1, 0, 0); - renderPass.end(); - }; - - return makePass("End", loaded, build, run); -}; diff --git a/js/webgpu/imagePass.js b/js/webgpu/imagePass.js deleted file mode 100644 index ee859de..0000000 --- a/js/webgpu/imagePass.js +++ /dev/null @@ -1,69 +0,0 @@ -import { structs } from "../../lib/gpu-buffer.js"; -import { makeComputeTarget, makeUniformBuffer, loadTexture, loadShader, makeBindGroup, makePass } from "./utils.js"; - -// Multiplies the rendered rain and bloom by a loaded in image - -const defaultBGURL = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/0a/Flammarion_Colored.jpg/917px-Flammarion_Colored.jpg"; - -export default ({ config, device }) => { - const bgURL = "bgURL" in config ? config.bgURL : defaultBGURL; - const assets = [loadTexture(device, bgURL), loadShader(device, "shaders/wgsl/imagePass.wgsl")]; - - const linearSampler = device.createSampler({ - magFilter: "linear", - minFilter: "linear", - }); - - let computePipeline; - let configBuffer; - let output; - let screenSize; - let backgroundTex; - let computeBindGroup; - - const loaded = (async () => { - const [bgTex, imageShader] = await Promise.all(assets); - - backgroundTex = bgTex; - - computePipeline = await device.createComputePipelineAsync({ - layout: "auto", - compute: { - module: imageShader.module, - entryPoint: "computeMain", - }, - }); - - const configUniforms = structs.from(imageShader.code).Config; - configBuffer = makeUniformBuffer(device, configUniforms, { unused: 0 }); - })(); - - const build = (size, inputs) => { - output?.destroy(); - output = makeComputeTarget(device, size); - screenSize = size; - computeBindGroup = makeBindGroup(device, computePipeline, 0, [ - configBuffer, - linearSampler, - inputs.primary.createView(), - inputs.bloom.createView(), - backgroundTex.createView(), - output.createView(), - ]); - return { primary: output }; - }; - - const run = (encoder, shouldRender) => { - if (!shouldRender) { - return; - } - - const computePass = encoder.beginComputePass(); - computePass.setPipeline(computePipeline); - computePass.setBindGroup(0, computeBindGroup); - computePass.dispatchWorkgroups(Math.ceil(screenSize[0] / 32), screenSize[1], 1); - computePass.end(); - }; - - return makePass("Image", loaded, build, run); -}; diff --git a/js/webgpu/main.js b/js/webgpu/main.js deleted file mode 100644 index aa5ce42..0000000 --- a/js/webgpu/main.js +++ /dev/null @@ -1,146 +0,0 @@ -import { structs } from "../../lib/gpu-buffer.js"; -import { makeUniformBuffer, makePipeline } from "./utils.js"; - -import makeRain from "./rainPass.js"; -import makeBloomPass from "./bloomPass.js"; -import makePalettePass from "./palettePass.js"; -import makeStripePass from "./stripePass.js"; -import makeImagePass from "./imagePass.js"; -import makeMirrorPass from "./mirrorPass.js"; -import makeEndPass from "./endPass.js"; -import { setupCamera, cameraCanvas, cameraAspectRatio, cameraSize } from "../camera.js"; - -const loadJS = (src) => - new Promise((resolve, reject) => { - const tag = document.createElement("script"); - tag.onload = resolve; - tag.onerror = reject; - tag.src = src; - document.body.appendChild(tag); - }); - -const effects = { - none: null, - plain: makePalettePass, - palette: makePalettePass, - customStripes: makeStripePass, - stripes: makeStripePass, - pride: makeStripePass, - transPride: makeStripePass, - trans: makeStripePass, - image: makeImagePass, - mirror: makeMirrorPass, -}; - -export default async (canvas, config) => { - await loadJS("lib/gl-matrix.js"); - - if (document.fullscreenEnabled || document.webkitFullscreenEnabled) { - window.ondblclick = () => { - if (document.fullscreenElement == null) { - if (canvas.webkitRequestFullscreen != null) { - canvas.webkitRequestFullscreen(); - } else { - canvas.requestFullscreen(); - } - } else { - document.exitFullscreen(); - } - }; - } - - if (config.useCamera) { - await setupCamera(); - } - - const canvasFormat = navigator.gpu.getPreferredCanvasFormat(); - const adapter = await navigator.gpu.requestAdapter(); - const device = await adapter.requestDevice(); - const canvasContext = canvas.getContext("webgpu"); - - // console.table(device.limits); - - canvasContext.configure({ - device, - format: canvasFormat, - alphaMode: "opaque", - usage: - // GPUTextureUsage.STORAGE_BINDING | - GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_DST, - }); - - const timeUniforms = structs.from(`struct Time { seconds : f32, frames : i32, };`).Time; - const timeBuffer = makeUniformBuffer(device, timeUniforms); - const cameraTex = device.createTexture({ - size: cameraSize, - format: "rgba8unorm", - usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT, - }); - - const context = { - config, - adapter, - device, - canvasContext, - timeBuffer, - canvasFormat, - cameraTex, - cameraAspectRatio, - cameraSize, - }; - - const effectName = config.effect in effects ? config.effect : "palette"; - const pipeline = await makePipeline(context, [makeRain, makeBloomPass, effects[effectName], makeEndPass]); - - const targetFrameTimeMilliseconds = 1000 / config.fps; - let frames = 0; - let start = NaN; - let last = NaN; - let outputs; - - const renderLoop = (now) => { - if (isNaN(start)) { - start = now; - } - - if (isNaN(last)) { - last = start; - } - - const shouldRender = config.fps >= 60 || now - last >= targetFrameTimeMilliseconds || config.once; - if (shouldRender) { - while (now - targetFrameTimeMilliseconds > last) { - last += targetFrameTimeMilliseconds; - } - } - - const devicePixelRatio = window.devicePixelRatio ?? 1; - const canvasWidth = Math.ceil(canvas.clientWidth * devicePixelRatio * config.resolution); - const canvasHeight = Math.ceil(canvas.clientHeight * devicePixelRatio * config.resolution); - const canvasSize = [canvasWidth, canvasHeight]; - if (canvas.width !== canvasWidth || canvas.height !== canvasHeight) { - canvas.width = canvasWidth; - canvas.height = canvasHeight; - outputs = pipeline.build(canvasSize); - } - - if (config.useCamera) { - device.queue.copyExternalImageToTexture({ source: cameraCanvas }, { texture: cameraTex }, cameraSize); - } - - device.queue.writeBuffer(timeBuffer, 0, timeUniforms.toBuffer({ seconds: (now - start) / 1000, frames })); - frames++; - - const encoder = device.createCommandEncoder(); - pipeline.run(encoder, shouldRender); - // Eventually, when WebGPU allows it, we'll remove the endPass and just copy from our pipeline's output to the canvas texture. - // encoder.copyTextureToTexture({ texture: outputs?.primary }, { texture: canvasContext.getCurrentTexture() }, canvasSize); - device.queue.submit([encoder.finish()]); - - if (!config.once) { - requestAnimationFrame(renderLoop); - } - }; - - requestAnimationFrame(renderLoop); -}; diff --git a/js/webgpu/mirrorPass.js b/js/webgpu/mirrorPass.js deleted file mode 100644 index 9de12af..0000000 --- a/js/webgpu/mirrorPass.js +++ /dev/null @@ -1,105 +0,0 @@ -import { structs } from "../../lib/gpu-buffer.js"; -import { makeComputeTarget, makeUniformBuffer, loadShader, makeBindGroup, makePass } from "./utils.js"; - -let start; -const numTouches = 5; -const touches = Array(numTouches) - .fill() - .map((_) => [0, 0, -Infinity, 0]); -let aspectRatio = 1; - -let index = 0; -let touchesChanged = true; -window.onclick = (e) => { - touches[index][0] = 0 + e.clientX / e.srcElement.clientWidth; - touches[index][1] = 1 - e.clientY / e.srcElement.clientHeight; - touches[index][2] = (Date.now() - start) / 1000; - index = (index + 1) % numTouches; - touchesChanged = true; -}; - -export default ({ config, device, cameraTex, cameraAspectRatio, timeBuffer }) => { - const assets = [loadShader(device, "shaders/wgsl/mirrorPass.wgsl")]; - - const linearSampler = device.createSampler({ - magFilter: "linear", - minFilter: "linear", - }); - - let computePipeline; - let configBuffer; - let sceneUniforms; - let sceneBuffer; - let touchUniforms; - let touchBuffer; - let output; - let screenSize; - let computeBindGroup; - - const loaded = (async () => { - const [mirrorShader] = await Promise.all(assets); - - computePipeline = await device.createComputePipelineAsync({ - layout: "auto", - compute: { - module: mirrorShader.module, - entryPoint: "computeMain", - }, - }); - - const mirrorShaderUniforms = structs.from(mirrorShader.code); - - const configUniforms = mirrorShaderUniforms.Config; - configBuffer = makeUniformBuffer(device, configUniforms, { unused: 0 }); - - sceneUniforms = mirrorShaderUniforms.Scene; - sceneBuffer = makeUniformBuffer(device, sceneUniforms); - - touchUniforms = mirrorShaderUniforms.Touches; - touchBuffer = makeUniformBuffer(device, touchUniforms); - })(); - - const build = (size, inputs) => { - output?.destroy(); - output = makeComputeTarget(device, size); - screenSize = size; - aspectRatio = size[0] / size[1]; - computeBindGroup = makeBindGroup(device, computePipeline, 0, [ - configBuffer, - timeBuffer, - sceneBuffer, - touchBuffer, - linearSampler, - inputs.primary.createView(), - inputs.bloom.createView(), - cameraTex.createView(), - output.createView(), - ]); - - const screenAspectRatio = size[0] / size[1]; - device.queue.writeBuffer(sceneBuffer, 0, sceneUniforms.toBuffer({ screenAspectRatio, cameraAspectRatio })); - - return { primary: output }; - }; - - const run = (encoder, shouldRender) => { - if (!shouldRender) { - return; - } - - if (touchesChanged) { - touchesChanged = false; - device.queue.writeBuffer(touchBuffer, 0, touchUniforms.toBuffer({ touches })); - } - - const computePass = encoder.beginComputePass(); - computePass.setPipeline(computePipeline); - computePass.setBindGroup(0, computeBindGroup); - computePass.dispatchWorkgroups(Math.ceil(screenSize[0] / 32), screenSize[1], 1); - computePass.end(); - }; - - start = Date.now(); - - return makePass("Mirror", loaded, build, run); -}; diff --git a/js/webgpu/palettePass.js b/js/webgpu/palettePass.js deleted file mode 100644 index 7e2b479..0000000 --- a/js/webgpu/palettePass.js +++ /dev/null @@ -1,140 +0,0 @@ -import colorToRGB from "../colorToRGB.js"; -import { structs } from "../../lib/gpu-buffer.js"; -import { loadShader, makeUniformBuffer, makeBindGroup, makeComputeTarget, makePass } from "./utils.js"; - -// Maps the brightness of the rendered rain and bloom to colors -// in a linear gradient buffer generated from the passed-in color sequence - -// This shader introduces noise into the renders, to avoid banding - -const makePalette = (device, paletteUniforms, entries) => { - const PALETTE_SIZE = 512; - const paletteColors = Array(PALETTE_SIZE); - - // Convert HSL gradient into sorted RGB gradient, capping the ends - const sortedEntries = entries - .slice() - .sort((e1, e2) => e1.at - e2.at) - .map((entry) => ({ - rgb: colorToRGB(entry.color), - arrayIndex: Math.floor(Math.max(Math.min(1, entry.at), 0) * (PALETTE_SIZE - 1)), - })); - sortedEntries.unshift({ rgb: sortedEntries[0].rgb, arrayIndex: 0 }); - sortedEntries.push({ - rgb: sortedEntries[sortedEntries.length - 1].rgb, - arrayIndex: PALETTE_SIZE - 1, - }); - - // Interpolate between the sorted RGB entries to generate - // the palette texture data - sortedEntries.forEach((entry, index) => { - paletteColors[entry.arrayIndex] = entry.rgb.slice(); - if (index + 1 < sortedEntries.length) { - const nextEntry = sortedEntries[index + 1]; - const diff = nextEntry.arrayIndex - entry.arrayIndex; - for (let i = 0; i < diff; i++) { - const ratio = i / diff; - paletteColors[entry.arrayIndex + i] = [ - entry.rgb[0] * (1 - ratio) + nextEntry.rgb[0] * ratio, - entry.rgb[1] * (1 - ratio) + nextEntry.rgb[1] * ratio, - entry.rgb[2] * (1 - ratio) + nextEntry.rgb[2] * ratio, - ]; - } - } - }); - - // TODO: try using gpu-uniforms - - const paletteBuffer = device.createBuffer({ - size: (3 + 1) * PALETTE_SIZE * Float32Array.BYTES_PER_ELEMENT, - usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST, - mappedAtCreation: true, - }); - - const view = new Float32Array(paletteBuffer.getMappedRange()); - for (let i = 0; i < paletteColors.length; i++) { - view.set(paletteColors[i], (3 + 1) * i); - } - - paletteBuffer.unmap(); - - return paletteBuffer; -}; - -// The rendered texture's values are mapped to colors in a palette texture. -// A little noise is introduced, to hide the banding that appears -// in subtle gradients. The noise is also time-driven, so its grain -// won't persist across subsequent frames. This is a safe trick -// in screen space. - -export default ({ config, device, timeBuffer }) => { - const linearSampler = device.createSampler({ - magFilter: "linear", - minFilter: "linear", - }); - - let computePipeline; - let configBuffer; - let paletteBuffer; - let computeBindGroup; - let output; - let screenSize; - - const assets = [loadShader(device, "shaders/wgsl/palettePass.wgsl")]; - - const loaded = (async () => { - const [paletteShader] = await Promise.all(assets); - - computePipeline = await device.createComputePipelineAsync({ - layout: "auto", - compute: { - module: paletteShader.module, - entryPoint: "computeMain", - }, - }); - - const paletteShaderUniforms = structs.from(paletteShader.code); - const configUniforms = paletteShaderUniforms.Config; - configBuffer = makeUniformBuffer(device, configUniforms, { - ditherMagnitude: config.ditherMagnitude, - backgroundColor: colorToRGB(config.backgroundColor), - cursorColor: colorToRGB(config.cursorColor), - glintColor: colorToRGB(config.glintColor), - cursorIntensity: config.cursorIntensity, - glintIntensity: config.glintIntensity, - }); - - const paletteUniforms = paletteShaderUniforms.Palette; - paletteBuffer = makePalette(device, paletteUniforms, config.palette); - })(); - - const build = (size, inputs) => { - output?.destroy(); - output = makeComputeTarget(device, size); - screenSize = size; - computeBindGroup = makeBindGroup(device, computePipeline, 0, [ - configBuffer, - paletteBuffer, - timeBuffer, - linearSampler, - inputs.primary.createView(), - inputs.bloom.createView(), - output.createView(), - ]); - return { primary: output }; - }; - - const run = (encoder, shouldRender) => { - if (!shouldRender) { - return; - } - - const computePass = encoder.beginComputePass(); - computePass.setPipeline(computePipeline); - computePass.setBindGroup(0, computeBindGroup); - computePass.dispatchWorkgroups(Math.ceil(screenSize[0] / 32), screenSize[1], 1); - computePass.end(); - }; - - return makePass("Palette", loaded, build, run); -}; diff --git a/js/webgpu/rainPass.js b/js/webgpu/rainPass.js deleted file mode 100644 index bfeb473..0000000 --- a/js/webgpu/rainPass.js +++ /dev/null @@ -1,237 +0,0 @@ -import { structs } from "../../lib/gpu-buffer.js"; -import { makeRenderTarget, loadTexture, loadShader, makeUniformBuffer, makeBindGroup, makePass } from "./utils.js"; - -const rippleTypes = { - box: 0, - circle: 1, -}; - -const numVerticesPerQuad = 2 * 3; - -const makeConfigBuffer = (device, configUniforms, config, density, gridSize) => { - const configData = { - ...config, - gridSize, - density, - showDebugView: config.effect === "none", - rippleType: config.rippleTypeName in rippleTypes ? rippleTypes[config.rippleTypeName] : -1, - slantScale: 1 / (Math.abs(Math.sin(2 * config.slant)) * (Math.sqrt(2) - 1) + 1), - slantVec: [Math.cos(config.slant), Math.sin(config.slant)], - msdfPxRange: 4, - }; - // console.table(configData); - - return makeUniformBuffer(device, configUniforms, configData); -}; - -export default ({ config, device, timeBuffer }) => { - const { mat4, vec3 } = glMatrix; - - const assets = [ - loadTexture(device, config.glyphMSDFURL), - loadTexture(device, config.glintMSDFURL), - loadTexture(device, config.baseTextureURL, false, true), - loadTexture(device, config.glintTextureURL, false, true), - loadShader(device, "shaders/wgsl/rainPass.wgsl"), - ]; - - // The volumetric mode multiplies the number of columns - // to reach the desired density, and then overlaps them - const density = config.volumetric && config.effect !== "none" ? config.density : 1; - const gridSize = [Math.floor(config.numColumns * density), config.numColumns]; - const numCells = gridSize[0] * gridSize[1]; - - // The volumetric mode requires us to create a grid of quads, - // rather than a single quad for our geometry - const numQuads = config.volumetric ? numCells : 1; - - const transform = mat4.create(); - if (config.volumetric && config.isometric) { - mat4.rotateX(transform, transform, (Math.PI * 1) / 8); - mat4.rotateY(transform, transform, (Math.PI * 1) / 4); - mat4.translate(transform, transform, vec3.fromValues(0, 0, -1)); - mat4.scale(transform, transform, vec3.fromValues(1, 1, 2)); - } else { - mat4.translate(transform, transform, vec3.fromValues(0, 0, -1)); - } - const camera = mat4.create(); - - // TODO: vantage points, multiple renders - - // We use the different channels for different parts of the raindrop - const renderFormat = "rgba8unorm"; - - const linearSampler = device.createSampler({ - magFilter: "linear", - minFilter: "linear", - }); - - const renderPassConfig = { - colorAttachments: [ - { - // view: null, - loadOp: "clear", - storeOp: "store", - }, - { - // view: null, - loadOp: "clear", - storeOp: "store", - }, - ], - }; - - let configBuffer; - let sceneUniforms; - let sceneBuffer; - let introPipeline; - let computePipeline; - let renderPipeline; - let introBindGroup; - let computeBindGroup; - let renderBindGroup; - let output; - let highPassOutput; - - const loaded = (async () => { - const [glyphMSDFTexture, glintMSDFTexture, baseTexture, glintTexture, rainShader] = await Promise.all(assets); - - const rainShaderUniforms = structs.from(rainShader.code); - configBuffer = makeConfigBuffer(device, rainShaderUniforms.Config, config, density, gridSize); - - const introCellsBuffer = device.createBuffer({ - size: gridSize[0] * rainShaderUniforms.IntroCell.minSize, - usage: GPUBufferUsage.STORAGE, - }); - - const cellsBuffer = device.createBuffer({ - size: numCells * rainShaderUniforms.Cell.minSize, - usage: GPUBufferUsage.STORAGE, - }); - - sceneUniforms = rainShaderUniforms.Scene; - sceneBuffer = makeUniformBuffer(device, sceneUniforms); - - const additiveBlendComponent = { - operation: "add", - srcFactor: "one", - dstFactor: "one", - }; - - [introPipeline, computePipeline, renderPipeline] = await Promise.all([ - device.createComputePipelineAsync({ - layout: "auto", - compute: { - module: rainShader.module, - entryPoint: "computeIntro", - }, - }), - - device.createComputePipelineAsync({ - layout: "auto", - compute: { - module: rainShader.module, - entryPoint: "computeMain", - }, - }), - - device.createRenderPipelineAsync({ - layout: "auto", - vertex: { - module: rainShader.module, - entryPoint: "vertMain", - }, - fragment: { - module: rainShader.module, - entryPoint: "fragMain", - targets: [ - { - format: renderFormat, - blend: { - color: additiveBlendComponent, - alpha: additiveBlendComponent, - }, - }, - { - format: renderFormat, - blend: { - color: additiveBlendComponent, - alpha: additiveBlendComponent, - }, - }, - ], - }, - }), - ]); - - introBindGroup = makeBindGroup(device, introPipeline, 0, [configBuffer, timeBuffer, introCellsBuffer]); - computeBindGroup = makeBindGroup(device, computePipeline, 0, [configBuffer, timeBuffer, cellsBuffer, introCellsBuffer]); - renderBindGroup = makeBindGroup(device, renderPipeline, 0, [ - configBuffer, - timeBuffer, - sceneBuffer, - linearSampler, - glyphMSDFTexture.createView(), - glintMSDFTexture.createView(), - baseTexture.createView(), - glintTexture.createView(), - cellsBuffer, - ]); - })(); - - const build = (size) => { - // Update scene buffer: camera and transform math for the volumetric mode - const aspectRatio = size[0] / size[1]; - if (config.volumetric && config.isometric) { - if (aspectRatio > 1) { - mat4.orthoZO(camera, -1.5 * aspectRatio, 1.5 * aspectRatio, -1.5, 1.5, -1000, 1000); - } else { - mat4.orthoZO(camera, -1.5, 1.5, -1.5 / aspectRatio, 1.5 / aspectRatio, -1000, 1000); - } - } else { - mat4.perspectiveZO(camera, (Math.PI / 180) * 90, aspectRatio, 0.0001, 1000); - } - const screenSize = aspectRatio > 1 ? [1, aspectRatio] : [1 / aspectRatio, 1]; - device.queue.writeBuffer(sceneBuffer, 0, sceneUniforms.toBuffer({ screenSize, camera, transform })); - - // Update - output?.destroy(); - output = makeRenderTarget(device, size, renderFormat); - - highPassOutput?.destroy(); - highPassOutput = makeRenderTarget(device, size, renderFormat); - - return { - primary: output, - highPass: highPassOutput, - }; - }; - - const run = (encoder, shouldRender) => { - // We render the code into an Target using MSDFs: https://github.com/Chlumsky/msdfgen - - const introPass = encoder.beginComputePass(); - introPass.setPipeline(introPipeline); - introPass.setBindGroup(0, introBindGroup); - introPass.dispatchWorkgroups(Math.ceil(gridSize[0] / 32), 1, 1); - introPass.end(); - - const computePass = encoder.beginComputePass(); - computePass.setPipeline(computePipeline); - computePass.setBindGroup(0, computeBindGroup); - computePass.dispatchWorkgroups(Math.ceil(gridSize[0] / 32), gridSize[1], 1); - computePass.end(); - - if (shouldRender) { - renderPassConfig.colorAttachments[0].view = output.createView(); - renderPassConfig.colorAttachments[1].view = highPassOutput.createView(); - const renderPass = encoder.beginRenderPass(renderPassConfig); - renderPass.setPipeline(renderPipeline); - renderPass.setBindGroup(0, renderBindGroup); - renderPass.draw(numVerticesPerQuad * numQuads, 1, 0, 0); - renderPass.end(); - } - }; - - return makePass("Rain", loaded, build, run); -}; diff --git a/js/webgpu/stripePass.js b/js/webgpu/stripePass.js deleted file mode 100644 index 3f709ce..0000000 --- a/js/webgpu/stripePass.js +++ /dev/null @@ -1,118 +0,0 @@ -import colorToRGB from "../colorToRGB.js"; -import { structs } from "../../lib/gpu-buffer.js"; -import { loadShader, make1DTexture, makeUniformBuffer, makeBindGroup, makeComputeTarget, makePass } from "./utils.js"; - -// Multiplies the rendered rain and bloom by a 1D gradient texture -// generated from the passed-in color sequence - -// This shader introduces noise into the renders, to avoid banding - -const transPrideStripeColors = [ - { space: "rgb", values: [0.36, 0.81, 0.98] }, - { space: "rgb", values: [0.96, 0.66, 0.72] }, - { space: "rgb", values: [1.0, 1.0, 1.0] }, - { space: "rgb", values: [0.96, 0.66, 0.72] }, - { space: "rgb", values: [0.36, 0.81, 0.98] }, -] - .map((color) => Array(3).fill(color)) - .flat(1); - -const prideStripeColors = [ - { space: "rgb", values: [0.89, 0.01, 0.01] }, - { space: "rgb", values: [1.0, 0.55, 0.0] }, - { space: "rgb", values: [1.0, 0.93, 0.0] }, - { space: "rgb", values: [0.0, 0.5, 0.15] }, - { space: "rgb", values: [0.0, 0.3, 1.0] }, - { space: "rgb", values: [0.46, 0.03, 0.53] }, -] - .map((color) => Array(2).fill(color)) - .flat(1); - -const numVerticesPerQuad = 2 * 3; - -// The rendered texture's values are mapped to colors in a palette texture. -// A little noise is introduced, to hide the banding that appears -// in subtle gradients. The noise is also time-driven, so its grain -// won't persist across subsequent frames. This is a safe trick -// in screen space. - -export default ({ config, device, timeBuffer }) => { - // Expand and convert stripe colors into 1D texture data - const stripeColors = "stripeColors" in config ? config.stripeColors : config.effect === "pride" ? prideStripeColors : transPrideStripeColors; - const stripeTex = make1DTexture( - device, - stripeColors.map((color) => [...colorToRGB(color), 1]) - ); - - const linearSampler = device.createSampler({ - magFilter: "linear", - minFilter: "linear", - }); - - let computePipeline; - let configBuffer; - let tex; - let bloomTex; - let output; - let screenSize; - - const assets = [loadShader(device, "shaders/wgsl/stripePass.wgsl")]; - - const loaded = (async () => { - const [stripeShader] = await Promise.all(assets); - - computePipeline = await device.createComputePipelineAsync({ - layout: "auto", - compute: { - module: stripeShader.module, - entryPoint: "computeMain", - }, - }); - - const configUniforms = structs.from(stripeShader.code).Config; - configBuffer = makeUniformBuffer(device, configUniforms, { - ditherMagnitude: config.ditherMagnitude, - backgroundColor: colorToRGB(config.backgroundColor), - cursorColor: colorToRGB(config.cursorColor), - glintColor: colorToRGB(config.glintColor), - cursorIntensity: config.cursorIntensity, - glintIntensity: config.glintIntensity, - }); - })(); - - const build = (size, inputs) => { - output?.destroy(); - output = makeComputeTarget(device, size); - screenSize = size; - - tex = inputs.primary; - bloomTex = inputs.bloom; - - return { - primary: output, - }; - }; - - const run = (encoder, shouldRender) => { - if (!shouldRender) { - return; - } - - const computePass = encoder.beginComputePass(); - computePass.setPipeline(computePipeline); - const computeBindGroup = makeBindGroup(device, computePipeline, 0, [ - configBuffer, - timeBuffer, - linearSampler, - tex.createView(), - bloomTex.createView(), - stripeTex.createView(), - output.createView(), - ]); - computePass.setBindGroup(0, computeBindGroup); - computePass.dispatchWorkgroups(Math.ceil(screenSize[0] / 32), screenSize[1], 1); - computePass.end(); - }; - - return makePass("Stripe", loaded, build, run); -}; diff --git a/js/webgpu/utils.js b/js/webgpu/utils.js deleted file mode 100644 index f0b63d7..0000000 --- a/js/webgpu/utils.js +++ /dev/null @@ -1,108 +0,0 @@ -const loadTexture = async (device, url) => { - if (url == null) { - return device.createTexture({ - size: [1, 1, 1], - format: "rgba8unorm", - usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT, - }); - } - - const response = await fetch(url); - const data = await response.blob(); - const source = await createImageBitmap(data); - const size = [source.width, source.height, 1]; - - const texture = device.createTexture({ - size, - format: "rgba8unorm", - usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT, - }); - - device.queue.copyExternalImageToTexture({ source, flipY: true }, { texture }, size); - - return texture; -}; - -const makeRenderTarget = (device, size, format, mipLevelCount = 1) => - device.createTexture({ - size: [...size, 1], - mipLevelCount, - format, - usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_SRC | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT, - }); - -const makeComputeTarget = (device, size, mipLevelCount = 1) => - device.createTexture({ - size: [...size, 1], - mipLevelCount, - format: "rgba8unorm", - usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_SRC | GPUTextureUsage.COPY_DST | GPUTextureUsage.STORAGE_BINDING, - }); - -const loadShader = async (device, url) => { - const response = await fetch(url); - const code = await response.text(); - return { - code, - module: device.createShaderModule({ code }), - }; -}; - -const makeUniformBuffer = (device, uniforms, data = null) => { - const buffer = device.createBuffer({ - size: uniforms.minSize, - usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST, - mappedAtCreation: data != null, - }); - if (data != null) { - uniforms.toBuffer(data, buffer.getMappedRange()); - buffer.unmap(); - } - return buffer; -}; - -const make1DTexture = (device, rgbas) => { - const size = [rgbas.length]; - const texture = device.createTexture({ - size, - // dimension: "1d", - format: "rgba8unorm", - usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST, - }); - const data = new Uint8ClampedArray(rgbas.map((color) => color.map((f) => f * 0xff)).flat()); - device.queue.writeTexture({ texture }, data, {}, size); - return texture; -}; - -const makeBindGroup = (device, pipeline, index, entries) => - device.createBindGroup({ - layout: pipeline.getBindGroupLayout(index), - entries: entries - .map((resource) => (resource instanceof GPUBuffer ? { buffer: resource } : resource)) - .map((resource, binding) => ({ - binding, - resource, - })), - }); - -const makePass = (name, loaded, build, run) => ({ - loaded: loaded ?? Promise.resolve(), - build: build ?? ((size, inputs) => inputs), - run: (encoder, shouldRender) => { - encoder.pushDebugGroup(`Pass "${name}"`); - run?.(encoder, shouldRender); - encoder.popDebugGroup(); - }, -}); - -const makePipeline = async (context, steps) => { - steps = steps.filter((f) => f != null).map((f) => f(context)); - await Promise.all(steps.map((step) => step.loaded)); - return { - steps, - build: (canvasSize) => steps.reduce((outputs, step) => step.build(canvasSize, outputs), null), - run: (encoder, shouldRender) => steps.forEach((step) => step.run(encoder, shouldRender)), - }; -}; - -export { makeRenderTarget, makeComputeTarget, make1DTexture, loadTexture, loadShader, makeUniformBuffer, makePass, makePipeline, makeBindGroup }; diff --git a/lib/gpu-buffer.js b/lib/gpu-buffer.js deleted file mode 100644 index 33264e5..0000000 --- a/lib/gpu-buffer.js +++ /dev/null @@ -1,264 +0,0 @@ -/** - * - * Meant to conform to the WGSL spec: - * - * https://gpuweb.github.io/gpuweb/wgsl/#alignment-and-size - * https://gpuweb.github.io/gpuweb/wgsl/#structure-layout-rules - * - * TODO: - * - Put in own repo - * - create mocha tests - * - Document. Provide examples of the simple constructor and from WGSL - * - **/ - -const zero = () => 0; -const array = (n) => () => Array(n).fill(0); - -const simpleTypes = { - ["i32"]: [4, 4, "i32", zero], - ["u32"]: [4, 4, "u32", zero], - ["f32"]: [4, 4, "f32", zero], - - ["atomic"]: [4, 4, "i32", zero], - ["atomic"]: [4, 4, "u32", zero], - ["atomic"]: [4, 4, "f32", zero], - - ["vec2"]: [8, 8, "i32", array(2)], - ["vec2"]: [8, 8, "u32", array(2)], - ["vec2"]: [8, 8, "f32", array(2)], - - ["vec3"]: [16, 12, "i32", array(3)], - ["vec3"]: [16, 12, "u32", array(3)], - ["vec3"]: [16, 12, "f32", array(3)], - - ["vec4"]: [16, 16, "i32", array(4)], - ["vec4"]: [16, 16, "u32", array(4)], - ["vec4"]: [16, 16, "f32", array(4)], - - ["mat2x2"]: [8, 16, "f32", array(2 * 2)], - ["mat3x2"]: [8, 24, "f32", array(3 * 2)], - ["mat4x2"]: [8, 32, "f32", array(4 * 2)], - ["mat2x3"]: [16, 32, "f32", array(2 * 3)], - ["mat3x3"]: [16, 48, "f32", array(3 * 3)], - ["mat4x3"]: [16, 64, "f32", array(4 * 3)], - ["mat2x4"]: [16, 32, "f32", array(2 * 4)], - ["mat3x4"]: [16, 48, "f32", array(3 * 4)], - ["mat4x4"]: [16, 64, "f32", array(4 * 4)], -}; - -const getTypeData = (type, attributes, otherStructLayouts) => { - type = type.replaceAll(/\s/g, ""); - if (simpleTypes[type] != null) { - let [align, size, baseType, defaultValue] = simpleTypes[type]; - if (attributes.align != null) { - align = parseInt(attributes.align); - } - if (attributes.size != null) { - size = parseInt(attributes.size); - } - return { - baseType, - align, - size, - defaultValue, - }; - } else if (type in otherStructLayouts) { - const innerLayout = otherStructLayouts[type]; - let { align, size } = innerLayout; - if (attributes.align != null) { - align = parseInt(attributes.align); - } - if (attributes.size != null) { - size = parseInt(attributes.size); - } - return { - isStruct: true, - innerLayout, - size, - align, - defaultValue: () => makeDataForLayout(otherStructLayouts, innerLayout), - }; - } else if (type.startsWith("array<")) { - const arrayMatch = type.match(/array<(.*?),?(\d+)?>$/); - const [_, innerType, fixedSize] = arrayMatch; - if (innerType == null) { - return null; - } - const elementTypeData = getTypeData(innerType, [], otherStructLayouts); - - const mult = parseInt(fixedSize ?? "0"); - const align = elementTypeData.align; - let stride = elementTypeData.size; - if (attributes.stride != null) { - stride = parseInt(attributes.stride); - } - const size = stride * mult; - - return { - isArray: true, - isFixedSize: mult > 0, - elementTypeData, - mult, - size, - align, - stride, - defaultValue: () => - Array(mult) - .fill() - .map((_) => elementTypeData.defaultValue()), - }; - } else { - console.warn(`Unrecognized type ${type}.`); - return null; - } -}; - -const parseAttributes = (str) => { - const attributes = {}; - for (const attr of str.split(",").filter((attr) => attr.length > 0)) { - const match = attr.match(/(\w+)(\((.*)\))?/); // foo(bar) - const [_, identifier, __, value] = match; - attributes[identifier] = value; - } - return attributes; -}; - -const parseStructLayout = (identifier, body, structLayouts) => { - const fields = []; - let byteOffset = 0; - const lines = body - .trim() - .split(",") // WGSL struct fields are currently delimited by commas... - .filter((s) => s.length > 0) - // ...but some commas separate elements between angle brackets, rather than between lines: - .reduce((existingLines, line, index) => { - if (index === 0) { - return [line]; - } - - const lastLine = existingLines[index - 1]; - const angleBracketBalance = lastLine.split("<").length - lastLine.split(">").length; - if (angleBracketBalance !== 0) { - existingLines[index - 1] = `${lastLine},${line}`; - } else { - existingLines.push(line); - } - - return existingLines; - }, []); - - for (const line of lines) { - const fieldMatch = line.match(/(@(.*?))? ?(\w+) ?: ?(@(.*?))? ?(.*)/); // @a(...) @b(...) foo : @c(...) @d(...) bar; - const [_, __, leftAttributes, identifier, ___, rightAttributes, type] = fieldMatch; - - const typeData = getTypeData(type, parseAttributes(rightAttributes ?? ""), structLayouts); - if (typeData == null) { - console.warn(`Skipping struct ${identifier}.`); - return null; - } - - byteOffset = Math.ceil(byteOffset / typeData.align) * typeData.align; - fields.push({ - attributes: parseAttributes(leftAttributes ?? ""), - identifier, - type, - ...typeData, - byteOffset, - }); - byteOffset += typeData.size; - } - - const minSizeInBytes = byteOffset; - const align = Math.max(...fields.map((field) => field.align)); - const size = Math.ceil(minSizeInBytes / align) * align; - return { identifier, fields, size, align }; -}; - -const parseStructLayoutsFromShader = (wgsl) => { - wgsl = wgsl - .replace(/\s*\/\/.*$/gm, "") // remove end-of-line comments - .replace(/\/\*.*?\*\//gms, "") // remove multi-line comments - .replace(/\s+/gm, " "); // convert all contiguous whitespace to single space characters - - const structLayouts = {}; - const structMatches = Array.from(wgsl.matchAll(/struct (\w+) ?\{(.*?)\};/g)); // struct Foo {...} - - for (const structMatch of structMatches) { - const [_, identifier, body] = structMatch; - const layout = parseStructLayout(identifier, body, structLayouts); - if (layout != null) { - structLayouts[layout.identifier] = layout; - } - } - return structLayouts; -}; - -const makeDataForLayout = (structLayouts, layout) => Object.fromEntries(layout.fields.map((field) => [field.identifier, field.defaultValue()])); - -const writeField = (allLayouts, field, value, views, byteOffset, warnMissingFields) => { - if (value == null) { - if (warnMissingFields) { - console.warn(`Property missing from data: ${field.identifier}`); - } - return; - } - if (field.isArray) { - const count = field.isFixedSize ? field.mult : value.length; - for (let i = 0; i < count; i++) { - writeField(allLayouts, field.elementTypeData, value[i], views, byteOffset + field.stride * i, warnMissingFields); - } - } else if (field.isStruct) { - for (const innerField of field.innerLayout.fields) { - writeField(allLayouts, innerField, value[innerField.identifier], views, byteOffset + field.stride * i, warnMissingFields); - } - } else { - const view = views[field.baseType]; - const array = value[Symbol.iterator] == null ? [Number(value)] : value; - view.set(array, (byteOffset + (field.byteOffset ?? 0)) / 4); - } -}; - -const makeGenerator = (layout, structLayouts) => { - const minSize = layout.size; - return Object.freeze({ - minSize, - create: () => makeDataForLayout(structLayouts, layout), - toBuffer: (object, destination, warnMissingFields = false) => { - if (destination == null) { - let size = layout.size; - const lastField = layout.fields[layout.fields.length - 1]; - if (lastField.isArray && lastField.identifier in object && !lastField.isFixedSize) { - size += lastField.stride * object[lastField.identifier].length; - } - destination = new ArrayBuffer(size); - } - - const views = { - i32: new Int32Array(destination), - u32: new Uint32Array(destination), - f32: new Float32Array(destination), - }; - - for (const field of layout.fields) { - writeField(structLayouts, field, object[field.identifier], views, 0, warnMissingFields); - } - - return destination; - }, - }); -}; - -const byteSizeOf = (simpleType) => simpleTypes[simpleType]?.[1]; - -const structs = Object.freeze({ - from: (wgsl) => { - if (typeof wgsl !== "string") { - throw new Error("Input is not a string."); - } - const structLayouts = parseStructLayoutsFromShader(wgsl); - return Object.fromEntries(Object.entries(structLayouts).map(([identifier, layout]) => [identifier, makeGenerator(layout, structLayouts)])); - }, -}); - -export { structs, byteSizeOf }; diff --git a/lib/holoplaycore.module.js b/lib/holoplaycore.module.js deleted file mode 100644 index 05adc9e..0000000 --- a/lib/holoplaycore.module.js +++ /dev/null @@ -1,787 +0,0 @@ -var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; - -function createCommonjsModule(fn, module) { - return module = { exports: {} }, fn(module, module.exports), module.exports; -} - -var cbor = createCommonjsModule(function (module) { -/* - * The MIT License (MIT) - * - * Copyright (c) 2014 Patrick Gansterer - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -(function(global, undefined$1) {var POW_2_24 = Math.pow(2, -24), - POW_2_32 = Math.pow(2, 32), - POW_2_53 = Math.pow(2, 53); - -function encode(value) { - var data = new ArrayBuffer(256); - var dataView = new DataView(data); - var lastLength; - var offset = 0; - - function ensureSpace(length) { - var newByteLength = data.byteLength; - var requiredLength = offset + length; - while (newByteLength < requiredLength) - newByteLength *= 2; - if (newByteLength !== data.byteLength) { - var oldDataView = dataView; - data = new ArrayBuffer(newByteLength); - dataView = new DataView(data); - var uint32count = (offset + 3) >> 2; - for (var i = 0; i < uint32count; ++i) - dataView.setUint32(i * 4, oldDataView.getUint32(i * 4)); - } - - lastLength = length; - return dataView; - } - function write() { - offset += lastLength; - } - function writeFloat64(value) { - write(ensureSpace(8).setFloat64(offset, value)); - } - function writeUint8(value) { - write(ensureSpace(1).setUint8(offset, value)); - } - function writeUint8Array(value) { - var dataView = ensureSpace(value.length); - for (var i = 0; i < value.length; ++i) - dataView.setUint8(offset + i, value[i]); - write(); - } - function writeUint16(value) { - write(ensureSpace(2).setUint16(offset, value)); - } - function writeUint32(value) { - write(ensureSpace(4).setUint32(offset, value)); - } - function writeUint64(value) { - var low = value % POW_2_32; - var high = (value - low) / POW_2_32; - var dataView = ensureSpace(8); - dataView.setUint32(offset, high); - dataView.setUint32(offset + 4, low); - write(); - } - function writeTypeAndLength(type, length) { - if (length < 24) { - writeUint8(type << 5 | length); - } else if (length < 0x100) { - writeUint8(type << 5 | 24); - writeUint8(length); - } else if (length < 0x10000) { - writeUint8(type << 5 | 25); - writeUint16(length); - } else if (length < 0x100000000) { - writeUint8(type << 5 | 26); - writeUint32(length); - } else { - writeUint8(type << 5 | 27); - writeUint64(length); - } - } - - function encodeItem(value) { - var i; - - if (value === false) - return writeUint8(0xf4); - if (value === true) - return writeUint8(0xf5); - if (value === null) - return writeUint8(0xf6); - if (value === undefined$1) - return writeUint8(0xf7); - - switch (typeof value) { - case "number": - if (Math.floor(value) === value) { - if (0 <= value && value <= POW_2_53) - return writeTypeAndLength(0, value); - if (-POW_2_53 <= value && value < 0) - return writeTypeAndLength(1, -(value + 1)); - } - writeUint8(0xfb); - return writeFloat64(value); - - case "string": - var utf8data = []; - for (i = 0; i < value.length; ++i) { - var charCode = value.charCodeAt(i); - if (charCode < 0x80) { - utf8data.push(charCode); - } else if (charCode < 0x800) { - utf8data.push(0xc0 | charCode >> 6); - utf8data.push(0x80 | charCode & 0x3f); - } else if (charCode < 0xd800) { - utf8data.push(0xe0 | charCode >> 12); - utf8data.push(0x80 | (charCode >> 6) & 0x3f); - utf8data.push(0x80 | charCode & 0x3f); - } else { - charCode = (charCode & 0x3ff) << 10; - charCode |= value.charCodeAt(++i) & 0x3ff; - charCode += 0x10000; - - utf8data.push(0xf0 | charCode >> 18); - utf8data.push(0x80 | (charCode >> 12) & 0x3f); - utf8data.push(0x80 | (charCode >> 6) & 0x3f); - utf8data.push(0x80 | charCode & 0x3f); - } - } - - writeTypeAndLength(3, utf8data.length); - return writeUint8Array(utf8data); - - default: - var length; - if (Array.isArray(value)) { - length = value.length; - writeTypeAndLength(4, length); - for (i = 0; i < length; ++i) - encodeItem(value[i]); - } else if (value instanceof Uint8Array) { - writeTypeAndLength(2, value.length); - writeUint8Array(value); - } else { - var keys = Object.keys(value); - length = keys.length; - writeTypeAndLength(5, length); - for (i = 0; i < length; ++i) { - var key = keys[i]; - encodeItem(key); - encodeItem(value[key]); - } - } - } - } - - encodeItem(value); - - if ("slice" in data) - return data.slice(0, offset); - - var ret = new ArrayBuffer(offset); - var retView = new DataView(ret); - for (var i = 0; i < offset; ++i) - retView.setUint8(i, dataView.getUint8(i)); - return ret; -} - -function decode(data, tagger, simpleValue) { - var dataView = new DataView(data); - var offset = 0; - - if (typeof tagger !== "function") - tagger = function(value) { return value; }; - if (typeof simpleValue !== "function") - simpleValue = function() { return undefined$1; }; - - function read(value, length) { - offset += length; - return value; - } - function readArrayBuffer(length) { - return read(new Uint8Array(data, offset, length), length); - } - function readFloat16() { - var tempArrayBuffer = new ArrayBuffer(4); - var tempDataView = new DataView(tempArrayBuffer); - var value = readUint16(); - - var sign = value & 0x8000; - var exponent = value & 0x7c00; - var fraction = value & 0x03ff; - - if (exponent === 0x7c00) - exponent = 0xff << 10; - else if (exponent !== 0) - exponent += (127 - 15) << 10; - else if (fraction !== 0) - return fraction * POW_2_24; - - tempDataView.setUint32(0, sign << 16 | exponent << 13 | fraction << 13); - return tempDataView.getFloat32(0); - } - function readFloat32() { - return read(dataView.getFloat32(offset), 4); - } - function readFloat64() { - return read(dataView.getFloat64(offset), 8); - } - function readUint8() { - return read(dataView.getUint8(offset), 1); - } - function readUint16() { - return read(dataView.getUint16(offset), 2); - } - function readUint32() { - return read(dataView.getUint32(offset), 4); - } - function readUint64() { - return readUint32() * POW_2_32 + readUint32(); - } - function readBreak() { - if (dataView.getUint8(offset) !== 0xff) - return false; - offset += 1; - return true; - } - function readLength(additionalInformation) { - if (additionalInformation < 24) - return additionalInformation; - if (additionalInformation === 24) - return readUint8(); - if (additionalInformation === 25) - return readUint16(); - if (additionalInformation === 26) - return readUint32(); - if (additionalInformation === 27) - return readUint64(); - if (additionalInformation === 31) - return -1; - throw "Invalid length encoding"; - } - function readIndefiniteStringLength(majorType) { - var initialByte = readUint8(); - if (initialByte === 0xff) - return -1; - var length = readLength(initialByte & 0x1f); - if (length < 0 || (initialByte >> 5) !== majorType) - throw "Invalid indefinite length element"; - return length; - } - - function appendUtf16data(utf16data, length) { - for (var i = 0; i < length; ++i) { - var value = readUint8(); - if (value & 0x80) { - if (value < 0xe0) { - value = (value & 0x1f) << 6 - | (readUint8() & 0x3f); - length -= 1; - } else if (value < 0xf0) { - value = (value & 0x0f) << 12 - | (readUint8() & 0x3f) << 6 - | (readUint8() & 0x3f); - length -= 2; - } else { - value = (value & 0x0f) << 18 - | (readUint8() & 0x3f) << 12 - | (readUint8() & 0x3f) << 6 - | (readUint8() & 0x3f); - length -= 3; - } - } - - if (value < 0x10000) { - utf16data.push(value); - } else { - value -= 0x10000; - utf16data.push(0xd800 | (value >> 10)); - utf16data.push(0xdc00 | (value & 0x3ff)); - } - } - } - - function decodeItem() { - var initialByte = readUint8(); - var majorType = initialByte >> 5; - var additionalInformation = initialByte & 0x1f; - var i; - var length; - - if (majorType === 7) { - switch (additionalInformation) { - case 25: - return readFloat16(); - case 26: - return readFloat32(); - case 27: - return readFloat64(); - } - } - - length = readLength(additionalInformation); - if (length < 0 && (majorType < 2 || 6 < majorType)) - throw "Invalid length"; - - switch (majorType) { - case 0: - return length; - case 1: - return -1 - length; - case 2: - if (length < 0) { - var elements = []; - var fullArrayLength = 0; - while ((length = readIndefiniteStringLength(majorType)) >= 0) { - fullArrayLength += length; - elements.push(readArrayBuffer(length)); - } - var fullArray = new Uint8Array(fullArrayLength); - var fullArrayOffset = 0; - for (i = 0; i < elements.length; ++i) { - fullArray.set(elements[i], fullArrayOffset); - fullArrayOffset += elements[i].length; - } - return fullArray; - } - return readArrayBuffer(length); - case 3: - var utf16data = []; - if (length < 0) { - while ((length = readIndefiniteStringLength(majorType)) >= 0) - appendUtf16data(utf16data, length); - } else - appendUtf16data(utf16data, length); - return String.fromCharCode.apply(null, utf16data); - case 4: - var retArray; - if (length < 0) { - retArray = []; - while (!readBreak()) - retArray.push(decodeItem()); - } else { - retArray = new Array(length); - for (i = 0; i < length; ++i) - retArray[i] = decodeItem(); - } - return retArray; - case 5: - var retObject = {}; - for (i = 0; i < length || length < 0 && !readBreak(); ++i) { - var key = decodeItem(); - retObject[key] = decodeItem(); - } - return retObject; - case 6: - return tagger(decodeItem(), length); - case 7: - switch (length) { - case 20: - return false; - case 21: - return true; - case 22: - return null; - case 23: - return undefined$1; - default: - return simpleValue(length); - } - } - } - - var ret = decodeItem(); - if (offset !== data.byteLength) - throw "Remaining bytes"; - return ret; -} - -var obj = { encode: encode, decode: decode }; - -if (typeof undefined$1 === "function" && undefined$1.amd) - undefined$1("cbor/cbor", obj); -else if ( module.exports) - module.exports = obj; -else if (!global.CBOR) - global.CBOR = obj; - -})(commonjsGlobal); -}); - -/** - * This files defines the HoloPlayClient class and Message class. - * - * Copyright (c) [2019] [Looking Glass Factory] - * - * @link https://lookingglassfactory.com/ - * @file This files defines the HoloPlayClient class and Message class. - * @author Looking Glass Factory. - * @version 0.0.8 - * @license SEE LICENSE IN LICENSE.md - */ - -// Polyfill WebSocket for nodejs applications. -const WebSocket = - typeof window === 'undefined' ? require('ws') : window.WebSocket; - -/** Class representing a client to communicates with the HoloPlayService. */ -class Client { - /** - * Establish a client to talk to HoloPlayService. - * @constructor - * @param {function} initCallback - optional; a function to trigger when - * response is received - * @param {function} errCallback - optional; a function to trigger when there - * is a connection error - * @param {function} closeCallback - optional; a function to trigger when the - * socket is closed - * @param {boolean} debug - optional; default is false - * @param {string} appId - optional - * @param {boolean} isGreedy - optional - * @param {string} oncloseBehavior - optional, can be 'wipe', 'hide', 'none' - */ - constructor( - initCallback, errCallback, closeCallback, debug = false, appId, isGreedy, - oncloseBehavior) { - this.reqs = []; - this.reps = []; - this.requestId = this.getRequestId(); - this.debug = debug; - this.isGreedy = isGreedy; - this.errCallback = errCallback; - this.closeCallback = closeCallback; - this.alwaysdebug = false; - this.isConnected = false; - let initCmd = null; - if (appId || isGreedy || oncloseBehavior) { - initCmd = new InitMessage(appId, isGreedy, oncloseBehavior, this.debug); - } else { - if (debug) this.alwaysdebug = true; - if (typeof initCallback == 'function') initCmd = new InfoMessage(); - } - this.openWebsocket(initCmd, initCallback); - } - /** - * Send a message over the websocket to HoloPlayService. - * @public - * @param {Message} msg - message object - * @param {integer} timeoutSecs - optional, default is 60 seconds - */ - sendMessage(msg, timeoutSecs = 60) { - if (this.alwaysdebug) msg.cmd.debug = true; - let cborData = msg.toCbor(); - return this.sendRequestObj(cborData, timeoutSecs); - } - /** - * Disconnects from the web socket. - * @public - */ - disconnect() { - this.ws.close(); - } - /** - * Open a websocket and set handlers - * @private - */ - openWebsocket(firstCmd = null, initCallback = null) { - this.ws = - new WebSocket('ws://localhost:11222/driver', ['rep.sp.nanomsg.org']); - this.ws.parent = this; - this.ws.binaryType = 'arraybuffer'; - this.ws.onmessage = this.messageHandler; - this.ws.onopen = (() => { - this.isConnected = true; - if (this.debug) { - console.log('socket open'); - } - if (firstCmd != null) { - this.sendMessage(firstCmd).then(initCallback); - } - }); - this.ws.onerror = this.onSocketError; - this.ws.onclose = this.onClose; - } - /** - * Send a request object over websocket - * @private - */ - sendRequestObj(data, timeoutSecs) { - return new Promise((resolve, reject) => { - let reqObj = { - id: this.requestId++, - parent: this, - payload: data, - success: resolve, - error: reject, - send: function() { - if (this.debug) - console.log('attemtping to send request with ID ' + this.id); - this.timeout = setTimeout(reqObj.send.bind(this), timeoutSecs * 1000); - let tmp = new Uint8Array(data.byteLength + 4); - let view = new DataView(tmp.buffer); - view.setUint32(0, this.id); - tmp.set(new Uint8Array(this.payload), 4); - this.parent.ws.send(tmp.buffer); - } - }; - this.reqs.push(reqObj); - reqObj.send(); - }); - } - /** - * Handles a message when received - * @private - */ - messageHandler(event) { - console.log('message'); - let data = event.data; - if (data.byteLength < 4) return; - let view = new DataView(data); - let replyId = view.getUint32(0); - if (replyId < 0x80000000) { - this.parent.err('bad nng header'); - return; - } - let i = this.parent.findReqIndex(replyId); - if (i == -1) { - this.parent.err('got reply that doesn\'t match known request!'); - return; - } - let rep = {id: replyId, payload: cbor.decode(data.slice(4))}; - if (rep.payload.error == 0) { - this.parent.reqs[i].success(rep.payload); - } else { - this.parent.reqs[i].error(rep.payload); - } - clearTimeout(this.parent.reqs[i].timeout); - this.parent.reqs.splice(i, 1); - this.parent.reps.push(rep); - if (this.debug) { - console.log(rep.payload); - } - } - getRequestId() { - return Math.floor(this.prng() * (0x7fffffff)) + 0x80000000; - } - onClose(event) { - this.parent.isConnected = false; - if (this.parent.debug) { - console.log('socket closed'); - } - if (typeof this.parent.closeCallback == 'function') - this.parent.closeCallback(event); - } - onSocketError(error) { - if (this.parent.debug) { - console.log(error); - } - if (typeof this.parent.errCallback == 'function') { - this.parent.errCallback(error); - } - } - err(errorMsg) { - if (this.debug) { - console.log('[DRIVER ERROR]' + errorMsg); - } - // TODO : make this return an event obj rather than a string - // if (typeof this.errCallback == 'function') - // this.errCallback(errorMsg); - } - findReqIndex(replyId) { - let i = 0; - for (; i < this.reqs.length; i++) { - if (this.reqs[i].id == replyId) { - return i; - } - } - return -1; - } - prng() { - if (this.rng == undefined) { - this.rng = generateRng(); - } - return this.rng(); - } -} - -/** A class to represent messages being sent over to HoloPlay Service */ -class Message { - /** - * Construct a barebone message. - * @constructor - */ - constructor(cmd, bin) { - this.cmd = cmd; - this.bin = bin; - } - /** - * Convert the class instance to the CBOR format - * @public - * @returns {CBOR} - cbor object of the message - */ - toCbor() { - return cbor.encode(this); - } -} -/** Message to init. Extends the base Message class. */ -class InitMessage extends Message { - /** - * @constructor - * @param {string} appId - a unique id for app - * @param {boolean} isGreedy - will it take over screen - * @param {string} oncloseBehavior - can be 'wipe', 'hide', 'none' - */ - constructor(appId = '', isGreedy = false, onclose = '', debug = false) { - let cmd = {'init': {}}; - if (appId != '') cmd['init'].appid = appId; - if (onclose != '') cmd['init'].onclose = onclose; - if (isGreedy) cmd['init'].greedy = true; - if (debug) cmd['init'].debug = true; - super(cmd, null); - } -} -/** Delete a quilt from HoloPlayService. Extends the base Message class. */ -class DeleteMessage extends Message { - /** - * @constructor - * @param {string} name - name of the quilt - */ - constructor(name = '') { - let cmd = {'delete': {'name': name}}; - super(cmd, null); - } -} -/** Check if a quilt exist in cache. Extends the base Message class. */ -class CheckMessage extends Message { - /** - * @constructor - * @param {string} name - name of the quilt - */ - constructor(name = '') { - let cmd = {'check': {'name': name}}; - super(cmd, null); - } -} -/** Wipes the image in Looking Glass and displays the background image */ -class WipeMessage extends Message { - /** - * @constructor - * @param {number} targetDisplay - optional, if not provided, default is 0 - */ - constructor(targetDisplay = null) { - let cmd = {'wipe': {}}; - if (targetDisplay != null) cmd['wipe'].targetDisplay = targetDisplay; - super(cmd, null); - } -} -/** Get info from the HoloPlayService */ -class InfoMessage extends Message { - /** - * @constructor - */ - constructor() { - let cmd = {'info': {}}; - super(cmd, null); - } -} -/** Get shader uniforms from HoloPlayService */ -class UniformsMessage extends Message { - /** - * @constructor - * @param {object} - */ - constructor() { - let cmd = {'uniforms': {}}; - super(cmd, bindata); - } -} -/** Get GLSL shader code from HoloPlayService */ -class ShaderMessage extends Message { - /** - * @constructor - * @param {object} - */ - constructor() { - let cmd = {'shader': {}}; - super(cmd, bindata); - } -} -/** Show a quilt in the Looking Glass with the binary data of quilt provided */ -class ShowMessage extends Message { - /** - * @constructor - * @param {object} - */ - constructor( - settings = {vx: 5, vy: 9, aspect: 1.6}, bindata = '', - targetDisplay = null) { - let cmd = { - 'show': { - 'source': 'bindata', - 'quilt': {'type': 'image', 'settings': settings} - } - }; - if (targetDisplay != null) cmd['show']['targetDisplay'] = targetDisplay; - super(cmd, bindata); - } -} -/** extends the base Message class */ -class CacheMessage extends Message { - constructor( - name, settings = {vx: 5, vy: 9, aspect: 1.6}, bindata = '', - show = false) { - let cmd = { - 'cache': { - 'show': show, - 'quilt': { - 'name': name, - 'type': 'image', - 'settings': settings, - } - } - }; - super(cmd, bindata); - } -} - -class ShowCachedMessage extends Message { - constructor(name, targetDisplay = null, settings = null) { - let cmd = {'show': {'source': 'cache', 'quilt': {'name': name}}}; - if (targetDisplay != null) cmd['show']['targetDisplay'] = targetDisplay; - if (settings != null) cmd['show']['quilt'].settings = settings; - super(cmd, null); - } -} -/* helper function */ -function generateRng() { - function xmur3(str) { - for (var i = 0, h = 1779033703 ^ str.length; i < str.length; i++) - h = Math.imul(h ^ str.charCodeAt(i), 3432918353), h = h << 13 | h >>> 19; - return function() { - h = Math.imul(h ^ h >>> 16, 2246822507); - h = Math.imul(h ^ h >>> 13, 3266489909); - return (h ^= h >>> 16) >>> 0; - } - } - function xoshiro128ss(a, b, c, d) { - return (() => { - var t = b << 9, r = a * 5; - r = (r << 7 | r >>> 25) * 9; - c ^= a; - d ^= b; - b ^= c; - a ^= d; - c ^= t; - d = d << 11 | d >>> 21; - return (r >>> 0) / 4294967296; - }) - } var state = Date.now(); - var seed = xmur3(state.toString()); - return xoshiro128ss(seed(), seed(), seed(), seed()); -} - -export { CacheMessage, CheckMessage, Client, DeleteMessage, InfoMessage, InitMessage, Message, ShaderMessage, ShowCachedMessage, ShowMessage, UniformsMessage, WipeMessage }; diff --git a/lib/regl.min.js b/lib/regl.min.js deleted file mode 100644 index 8b68260..0000000 --- a/lib/regl.min.js +++ /dev/null @@ -1,171 +0,0 @@ -(function(Z,ka){"object"===typeof exports&&"undefined"!==typeof module?module.exports=ka():"function"===typeof define&&define.amd?define(ka):Z.createREGL=ka()})(this,function(){function Z(a,b){this.id=Db++;this.type=a;this.data=b}function ka(a){if(0===a.length)return[];var b=a.charAt(0),c=a.charAt(a.length-1);if(1>>=b;c=(255>>=c;b|=c;c=(15>>=c;b|=c;c=(3>>c>>1}function hb(){function a(a){a:{for(var b=16;268435456>=b;b*=16)if(a<=b){a=b;break a}a=0}b=c[gb(a)>>2];return 0>2].push(a)}var c=R(8,function(){return[]});return{alloc:a,free:b,allocType:function(b,c){var d=null;switch(b){case 5120:d=new Int8Array(a(c),0,c);break;case 5121:d=new Uint8Array(a(c),0,c);break;case 5122:d=new Int16Array(a(2*c),0,c);break;case 5123:d=new Uint16Array(a(2*c),0,c);break;case 5124:d=new Int32Array(a(4*c),0,c);break;case 5125:d=new Uint32Array(a(4*c),0,c);break;case 5126:d=new Float32Array(a(4*c),0,c);break;default:return null}return d.length!== -c?d.subarray(0,c):d},freeType:function(a){b(a.buffer)}}}function la(a){return!!a&&"object"===typeof a&&Array.isArray(a.shape)&&Array.isArray(a.stride)&&"number"===typeof a.offset&&a.shape.length===a.stride.length&&(Array.isArray(a.data)||O(a.data))}function ib(a,b,c,e,f,d){for(var q=0;qe&&(e=d.buffer.byteLength,5123===k?e>>=1:5125===k&&(e>>=2));d.vertCount=e;e=g;0>g&&(e=4,g=d.buffer.dimension,1===g&&(e=0),2===g&&(e=1),3===g&&(e=4));d.primType=e}function q(a){e.elementsCount--;delete n[a.id];a.buffer.destroy();a.buffer=null}var n={},v=0,k={uint8:5121,uint16:5123};b.oes_element_index_uint&&(k.uint32=5125);f.prototype.bind=function(){this.buffer.bind()};var u=[];return{create:function(a, -b){function l(a){if(a)if("number"===typeof a)g(a),h.primType=4,h.vertCount=a|0,h.type=5121;else{var b=null,c=35044,e=-1,f=-1,m=0,n=0;if(Array.isArray(a)||O(a)||la(a))b=a;else if("data"in a&&(b=a.data),"usage"in a&&(c=nb[a.usage]),"primitive"in a&&(e=Ka[a.primitive]),"count"in a&&(f=a.count|0),"type"in a&&(n=k[a.type]),"length"in a)m=a.length|0;else if(m=f,5123===n||5122===n)m*=2;else if(5125===n||5124===n)m*=4;d(h,b,c,e,f,m,n)}else g(),h.primType=4,h.vertCount=0,h.type=5121;return l}var g=c.create(null, -34963,!0),h=new f(g._buffer);e.elementsCount++;l(a);l._reglType="elements";l._elements=h;l.subdata=function(a,b){g.subdata(a,b);return l};l.destroy=function(){q(h)};return l},createStream:function(a){var b=u.pop();b||(b=new f(c.create(null,34963,!0,!1)._buffer));d(b,a,35040,-1,-1,0,0);return b},destroyStream:function(a){u.push(a)},getElements:function(a){return"function"===typeof a&&a._elements instanceof f?a._elements:null},clear:function(){I(n).forEach(q)}}}function ob(a){for(var b=G.allocType(5123, -a.length),c=0;c>>31<<15,d=(e<<1>>>24)-127,e=e>>13&1023;b[c]=-24>d?f:-14>d?f+(e+1024>>-14-d):15>=e,c.height>>=e,x(c,d[e]),a.mipmask|=1<b;++b)a.images[b]=null;return a}function ya(a){for(var b=a.images,c=0;cb){for(var c=0;c=--this.refCount&&F(this)}});q.profile&&(d.getTotalTextureSize=function(){var a=0;Object.keys(ea).forEach(function(b){a+=ea[b].stats.size});return a});return{create2D:function(b,c){function e(a,b){var c=f.texInfo;w.call(c);var d=ma();"number"===typeof a?"number"===typeof b?p(d,a|0,b|0):p(d,a|0,a|0):a?(H(c,a),P(d,a)):p(d,1,1);c.genMipmaps&&(d.mipmask=(d.width<<1)-1);f.mipmask=d.mipmask;v(f, -d);f.internalformat=d.internalformat;e.width=d.width;e.height=d.height;T(f);t(d,3553);M(c,3553);wa();ya(d);q.profile&&(f.stats.size=La(f.internalformat,f.type,d.width,d.height,c.genMipmaps,!1));e.format=ca[f.internalformat];e.type=K[f.type];e.mag=Fa[c.magFilter];e.min=pa[c.minFilter];e.wrapS=qa[c.wrapS];e.wrapT=qa[c.wrapT];return e}var f=new y(3553);ea[f.id]=f;d.textureCount++;e(b,c);e.subimage=function(a,b,c,d){b|=0;c|=0;d|=0;var y=g();v(y,f);y.width=0;y.height=0;x(y,a);y.width=y.width||(f.width>> -d)-b;y.height=y.height||(f.height>>d)-c;T(f);l(y,3553,b,c,d);wa();h(y);return e};e.resize=function(b,c){var d=b|0,g=c|0||d;if(d===f.width&&g===f.height)return e;e.width=f.width=d;e.height=f.height=g;T(f);for(var y=0;f.mipmask>>y;++y){var h=d>>y,z=g>>y;if(!h||!z)break;a.texImage2D(3553,y,f.format,h,z,0,f.format,f.type,null)}wa();q.profile&&(f.stats.size=La(f.internalformat,f.type,d,g,!1,!1));return e};e._reglType="texture2d";e._texture=f;q.profile&&(e.stats=f.stats);e.destroy=function(){f.decRef()}; -return e},createCube:function(b,c,e,f,n,r){function m(a,b,c,d,e,f){var g,da=A.texInfo;w.call(da);for(g=0;6>g;++g)F[g]=ma();if("number"===typeof a||!a)for(a=a|0||1,g=0;6>g;++g)p(F[g],a,a);else if("object"===typeof a)if(b)P(F[0],a),P(F[1],b),P(F[2],c),P(F[3],d),P(F[4],e),P(F[5],f);else if(H(da,a),k(A,a),"faces"in a)for(a=a.faces,g=0;6>g;++g)v(F[g],A),P(F[g],a[g]);else for(g=0;6>g;++g)P(F[g],a);v(A,F[0]);A.mipmask=da.genMipmaps?(F[0].width<<1)-1:F[0].mipmask;A.internalformat=F[0].internalformat;m.width= -F[0].width;m.height=F[0].height;T(A);for(g=0;6>g;++g)t(F[g],34069+g);M(da,34067);wa();q.profile&&(A.stats.size=La(A.internalformat,A.type,m.width,m.height,da.genMipmaps,!0));m.format=ca[A.internalformat];m.type=K[A.type];m.mag=Fa[da.magFilter];m.min=pa[da.minFilter];m.wrapS=qa[da.wrapS];m.wrapT=qa[da.wrapT];for(g=0;6>g;++g)ya(F[g]);return m}var A=new y(34067);ea[A.id]=A;d.cubeCount++;var F=Array(6);m(b,c,e,f,n,r);m.subimage=function(a,b,c,d,e){c|=0;d|=0;e|=0;var f=g();v(f,A);f.width=0;f.height=0; -x(f,b);f.width=f.width||(A.width>>e)-c;f.height=f.height||(A.height>>e)-d;T(A);l(f,34069+a,c,d,e);wa();h(f);return m};m.resize=function(b){b|=0;if(b!==A.width){m.width=A.width=b;m.height=A.height=b;T(A);for(var c=0;6>c;++c)for(var d=0;A.mipmask>>d;++d)a.texImage2D(34069+c,d,A.format,b>>d,b>>d,0,A.format,A.type,null);wa();q.profile&&(A.stats.size=La(A.internalformat,A.type,m.width,m.height,!1,!0));return m}};m._reglType="textureCube";m._texture=A;q.profile&&(m.stats=A.stats);m.destroy=function(){A.decRef()}; -return m},clear:function(){for(var b=0;bc;++c)if(0!==(b.mipmask&1<>c,b.height>>c,0,b.internalformat, -b.type,null);else for(var d=0;6>d;++d)a.texImage2D(34069+d,c,b.internalformat,b.width>>c,b.height>>c,0,b.internalformat,b.type,null);M(b.texInfo,b.target)})},refresh:function(){for(var b=0;bd;++d){for(p= -0;pa;++a)c[a].resize(d);b.width=b.height=d;return b},_reglType:"framebufferCube",destroy:function(){c.forEach(function(a){a.destroy()})}})},clear:function(){I(M).forEach(r)}, -restore:function(){t.cur=null;t.next=null;t.dirty=!0;I(M).forEach(function(b){b.framebuffer=a.createFramebuffer();p(b)})}})}function $a(){this.w=this.z=this.y=this.x=this.state=0;this.buffer=null;this.size=0;this.normalized=!1;this.type=5126;this.divisor=this.stride=this.offset=0}function Sb(a,b,c,e,f,d,q){function n(a){if(a!==r.currentVAO){var c=b.oes_vertex_array_object;a?c.bindVertexArrayOES(a.vao):c.bindVertexArrayOES(null);r.currentVAO=a}}function v(c){if(c!==r.currentVAO){if(c)c.bindAttrs(); -else{for(var d=b.angle_instanced_arrays,e=0;e=m.byteLength?l.subdata(m): -(l.destroy(),c.buffers[h]=null));c.buffers[h]||(l=c.buffers[h]=f.create(p,34962,!1,!0));k.buffer=f.getBuffer(l);k.size=k.buffer.dimension|0;k.normalized=!1;k.type=k.buffer.dtype;k.offset=0;k.stride=0;k.divisor=0;k.state=1;a[h]=1}else f.getBuffer(p)?(k.buffer=f.getBuffer(p),k.size=k.buffer.dimension|0,k.normalized=!1,k.type=k.buffer.dtype,k.offset=0,k.stride=0,k.divisor=0,k.state=1):f.getBuffer(p.buffer)?(k.buffer=f.getBuffer(p.buffer),k.size=(+p.size||k.buffer.dimension)|0,k.normalized=!!p.normalized|| -!1,k.type="type"in p?Ja[p.type]:k.buffer.dtype,k.offset=(p.offset||0)|0,k.stride=(p.stride||0)|0,k.divisor=(p.divisor||0)|0,k.state=1):"x"in p&&(k.x=+p.x||0,k.y=+p.y||0,k.z=+p.z||0,k.w=+p.w||0,k.state=2)}for(l=0;la&&(a=b.stats.uniformsCount)});return a},c.getMaxAttributesCount=function(){var a=0;x.forEach(function(b){b.stats.attributesCount>a&&(a=b.stats.attributesCount)});return a});return{clear:function(){var b=a.deleteShader.bind(a);I(k).forEach(b);k={};I(u).forEach(b); -u={};x.forEach(function(b){a.deleteProgram(b.program)});x.length=0;m={};c.shaderCount=0},program:function(b,d,e,f){var l=m[d];l||(l=m[d]={});var q=l[b];if(q&&(q.refCount++,!f))return q;var w=new n(d,b);c.shaderCount++;v(w,e,f);q||(l[b]=w);x.push(w);return L(w,{destroy:function(){w.refCount--;if(0>=w.refCount){a.deleteProgram(w.program);var b=x.indexOf(w);x.splice(b,1);c.shaderCount--}0>=l[w.vertId].refCount&&(a.deleteShader(u[w.vertId]),delete u[w.vertId],delete m[w.fragId][w.vertId]);Object.keys(m[w.fragId]).length|| -(a.deleteShader(k[w.fragId]),delete k[w.fragId],delete m[w.fragId])}})},restore:function(){k={};u={};for(var a=0;a"+b+"?"+e+".constant["+b+"]:0;"}).join(""),"}}else{","if(",g,"(",e,".buffer)){",k,"=",f,".createStream(",34962,",",e,".buffer);","}else{",k,"=",f,".getBuffer(",e,".buffer);","}",m,'="type" in ',e,"?",z.glTypes,"[",e,".type]:",k,".dtype;",B.normalized,"=!!", -e,".normalized;");d("size");d("offset");d("stride");d("divisor");c("}}");c.exit("if(",B.isStream,"){",f,".destroyStream(",k,");","}");return B})});return g}function F(a){var b=a["static"],c=a.dynamic,d={};Object.keys(b).forEach(function(a){var c=b[a];d[a]=w(function(a,b){return"number"===typeof c||"boolean"===typeof c?""+c:a.link(c)})});Object.keys(c).forEach(function(a){var b=c[a];d[a]=K(b,function(a,c){return a.invoke(c,b)})});return d}function A(a,b,d,e,f){function g(a){var b=p[a];b&&(ja[a]=b)} -var m=O(a,b),l=G(a,f),p=C(a,l,f),X=M(a,f),ja=y(a,f),q=H(a,f,m);g("viewport");g(h("scissor.box"));var n=0>1)",u],");")}function b(){c(t,".drawArraysInstancedANGLE(",[n,q,r,u],");")}p&&"null"!==p?v?a():(c("if(",p,"){"),a(),c("}else{"),b(),c("}")):b()}function g(){function a(){c(l+".drawElements("+[n,r,x,q+"<<(("+x+"-5121)>>1)"]+");")}function b(){c(l+".drawArrays("+[n,q,r]+");")}p&&"null"!==p?v?a():(c("if(",p,"){"),a(),c("}else{"),b(),c("}")):b()}var h=a.shared,l=h.gl,k=h.draw,m=d.draw, -p=function(){var e=m.elements,f=b;if(e){if(e.contextDep&&d.contextDynamic||e.propDep)f=c;e=e.append(a,f);m.elementsActive&&f("if("+e+")"+l+".bindBuffer(34963,"+e+".buffer.buffer);")}else e=f.def(),f(e,"=",k,".","elements",";","if(",e,"){",l,".bindBuffer(",34963,",",e,".buffer.buffer);}","else if(",h.vao,".currentVAO){",e,"=",a.shared.elements+".getElements("+h.vao,".currentVAO.elements);",na?"":"if("+e+")"+l+".bindBuffer(34963,"+e+".buffer.buffer);","}");return e}(),n=e("primitive"),q=e("offset"), -r=function(){var e=m.count,f=b;if(e){if(e.contextDep&&d.contextDynamic||e.propDep)f=c;e=e.append(a,f)}else e=f.def(k,".","count");return e}();if("number"===typeof r){if(0===r)return}else c("if(",r,"){"),c.exit("}");var u,t;W&&(u=e("instances"),t=a.instancing);var x=p+".type",v=m.elements&&xa(m.elements)&&!m.vaoActive;W&&("number"!==typeof u||0<=u)?"string"===typeof u?(c("if(",u,">0){"),f(),c("}else if(",u,"<0){"),g(),c("}")):f():g()}function ca(a,b,c,d,e){b=P();e=b.proc("body",e);W&&(b.instancing= -e.def(b.shared.extensions,".angle_instanced_arrays"));a(b,e,c,d);return b.compile().body}function Z(a,b,c,d){N(a,b);c.useVAO?c.drawVAO?b(a.shared.vao,".setVAO(",c.drawVAO.append(a,b),");"):b(a.shared.vao,".setVAO(",a.shared.vao,".targetVAO);"):(b(a.shared.vao,".setVAO(null);"),ga(a,b,c,d.attributes,function(){return!0}));Q(a,b,c,d.uniforms,function(){return!0},!1);U(a,b,b,c)}function Fa(a,b){var c=a.proc("draw",1);N(a,c);ia(a,c,b.context);S(a,c,b.framebuffer);Aa(a,c,b);I(a,c,b.state);E(a,c,b,!1,!0); -var d=b.shader.progVar.append(a,c);c(a.shared.gl,".useProgram(",d,".program);");if(b.shader.program)Z(a,c,b,b.shader.program);else{c(a.shared.vao,".setVAO(null);");var e=a.global.def("{}"),f=c.def(d,".id"),g=c.def(e,"[",f,"]");c(a.cond(g).then(g,".call(this,a0);")["else"](g,"=",e,"[",f,"]=",a.link(function(c){return ca(Z,a,b,c,1)}),"(",d,");",g,".call(this,a0);"))}0=--this.refCount&&q(this)};f.profile&&(e.getTotalRenderbufferSize=function(){var a=0;Object.keys(u).forEach(function(b){a+=u[b].stats.size});return a});return{create:function(b, -c){function l(b,c){var d=0,e=0,k=32854;"object"===typeof b&&b?("shape"in b?(e=b.shape,d=e[0]|0,e=e[1]|0):("radius"in b&&(d=e=b.radius|0),"width"in b&&(d=b.width|0),"height"in b&&(e=b.height|0)),"format"in b&&(k=n[b.format])):"number"===typeof b?(d=b|0,e="number"===typeof c?c|0:d):b||(d=e=1);if(d!==g.width||e!==g.height||k!==g.format)return l.width=g.width=d,l.height=g.height=e,g.format=k,a.bindRenderbuffer(36161,g.renderbuffer),a.renderbufferStorage(36161,k,d,e),f.profile&&(g.stats.size=Q[g.format]* -g.width*g.height),l.format=v[g.format],l}var g=new d(a.createRenderbuffer());u[g.id]=g;e.renderbufferCount++;l(b,c);l.resize=function(b,c){var d=b|0,e=c|0||d;if(d===g.width&&e===g.height)return l;l.width=g.width=d;l.height=g.height=e;a.bindRenderbuffer(36161,g.renderbuffer);a.renderbufferStorage(36161,g.format,d,e);f.profile&&(g.stats.size=Q[g.format]*g.width*g.height);return l};l._reglType="renderbuffer";l._renderbuffer=g;f.profile&&(l.stats=g.stats);l.destroy=function(){g.decRef()};return l},clear:function(){I(u).forEach(q)}, -restore:function(){I(u).forEach(function(b){b.renderbuffer=a.createRenderbuffer();a.bindRenderbuffer(36161,b.renderbuffer);a.renderbufferStorage(36161,b.format,b.width,b.height)});a.bindRenderbuffer(36161,null)}}},Za=[];Za[6408]=4;Za[6407]=3;var Ra=[];Ra[5121]=1;Ra[5126]=4;Ra[36193]=2;var Da=["x","y","z","w"],Xb="blend.func blend.equation stencil.func stencil.opFront stencil.opBack sample.coverage viewport scissor.box polygonOffset.offset".split(" "),Ga={0:0,1:1,zero:0,one:1,"src color":768,"one minus src color":769, -"src alpha":770,"one minus src alpha":771,"dst color":774,"one minus dst color":775,"dst alpha":772,"one minus dst alpha":773,"constant color":32769,"one minus constant color":32770,"constant alpha":32771,"one minus constant alpha":32772,"src alpha saturate":776},ab={never:512,less:513,"<":513,equal:514,"=":514,"==":514,"===":514,lequal:515,"<=":515,greater:516,">":516,notequal:517,"!=":517,"!==":517,gequal:518,">=":518,always:519},Ta={0:0,zero:0,keep:7680,replace:7681,increment:7682,decrement:7683, -"increment wrap":34055,"decrement wrap":34056,invert:5386},zb={cw:2304,ccw:2305},Ab=new J(!1,!1,!1,function(){}),$b=function(a,b){function c(){this.endQueryIndex=this.startQueryIndex=-1;this.sum=0;this.stats=null}function e(a,b,d){var e=q.pop()||new c;e.startQueryIndex=a;e.endQueryIndex=b;e.sum=0;e.stats=d;n.push(e)}if(!b.ext_disjoint_timer_query)return null;var f=[],d=[],q=[],n=[],v=[],k=[];return{beginQuery:function(a){var c=f.pop()||b.ext_disjoint_timer_query.createQueryEXT();b.ext_disjoint_timer_query.beginQueryEXT(35007, -c);d.push(c);e(d.length-1,d.length,a)},endQuery:function(){b.ext_disjoint_timer_query.endQueryEXT(35007)},pushScopeStats:e,update:function(){var a,c;a=d.length;if(0!==a){k.length=Math.max(k.length,a+1);v.length=Math.max(v.length,a+1);v[0]=0;var e=k[0]=0;for(c=a=0;c=E.length&&e()}var c=Bb(E,a);E[c]=b}}}function k(){var a=Q.viewport,b=Q.scissor_box;a[0]=a[1]=b[0]=b[1]=0;H.viewportWidth=H.framebufferWidth=H.drawingBufferWidth=a[2]=b[2]=l.drawingBufferWidth;H.viewportHeight=H.framebufferHeight=H.drawingBufferHeight=a[3]=b[3]=l.drawingBufferHeight}function u(){H.tick+=1;H.time=x();k();I.procs.poll()}function m(){A.refresh();k();I.procs.refresh();t&&t.update()}function x(){return(Cb()- -G)/1E3}a=Hb(a);if(!a)return null;var l=a.gl,g=l.getContextAttributes();l.isContextLost();var h=Ib(l,a);if(!h)return null;var r=Eb(),p={vaoCount:0,bufferCount:0,elementsCount:0,framebufferCount:0,shaderCount:0,textureCount:0,cubeCount:0,renderbufferCount:0,maxTextureUnits:0},w=h.extensions,t=$b(l,w),G=Cb(),C=l.drawingBufferWidth,J=l.drawingBufferHeight,H={tick:0,time:0,viewportWidth:C,viewportHeight:J,framebufferWidth:C,framebufferHeight:J,drawingBufferWidth:C,drawingBufferHeight:J,pixelRatio:a.pixelRatio}, -C={elements:null,primitive:4,count:-1,offset:0,instances:-1},M=Yb(l,w),y=Jb(l,p,a,function(a){return K.destroyBuffer(a)}),T=Kb(l,w,y,p),K=Sb(l,w,M,p,y,T,C),F=Tb(l,r,p,a),A=Nb(l,w,M,function(){I.procs.poll()},H,p,a),O=Zb(l,w,M,p,a),S=Rb(l,w,M,A,O,p),I=Wb(l,r,w,M,y,T,A,S,{},K,F,C,H,t,a),r=Ub(l,S,I.procs.poll,H,g,w,M),Q=I.next,N=l.canvas,E=[],R=[],U=[],Z=[a.onDestroy],ca=null;N&&(N.addEventListener("webglcontextlost",f,!1),N.addEventListener("webglcontextrestored",d,!1));var aa=S.setFBO=q({framebuffer:Y.define.call(null, -1,"framebuffer")});m();g=L(q,{clear:function(a){if("framebuffer"in a)if(a.framebuffer&&"framebufferCube"===a.framebuffer_reglType)for(var b=0;6>b;++b)aa(L({framebuffer:a.framebuffer.faces[b]},a),n);else aa(a,n);else n(null,a)},prop:Y.define.bind(null,1),context:Y.define.bind(null,2),"this":Y.define.bind(null,3),draw:q({}),buffer:function(a){return y.create(a,34962,!1,!1)},elements:function(a){return T.create(a,!1)},texture:A.create2D,cube:A.createCube,renderbuffer:O.create,framebuffer:S.create,framebufferCube:S.createCube, -vao:K.createVAO,attributes:g,frame:v,on:function(a,b){var c;switch(a){case "frame":return v(b);case "lost":c=R;break;case "restore":c=U;break;case "destroy":c=Z}c.push(b);return{cancel:function(){for(var a=0;a - -#include "pd_api.h" - -typedef struct { - int x; - int y; - int fadeIndex; - int glyphIndex; - float glyphCycle; - float columnSpeedOffset; - float columnTimeOffset; -} Cell; - -const struct playdate_sound* snd = NULL; -const struct playdate_display* disp = NULL; -const struct playdate_graphics* gfx = NULL; -const struct playdate_sys* sys = NULL; - -static const int glyphWidth = 20; -static int numColumns; -static int numRows; -static int numCells; - -static const int numStandardGlyphs = 135; -static const int numPDGlyphs = 10; -static const int numTotalGlyphs = numStandardGlyphs + numPDGlyphs; -static const int numFades = 32; - -static const float minSpeed = 0.15; -static const float maxSpeed = 1; -static float time = 0; -static float speed = maxSpeed; - -static float sineTable[360]; -static float wobbleA; -static float wobbleB; - -static LCDBitmap **glyphs = NULL; -static Cell *cells = NULL; - -static float randf(void) { - return (float)rand() / (float)(RAND_MAX); -} - -static void init(void) -{ - gfx->clear(kColorBlack); - - srand(sys->getSecondsSinceEpoch(NULL)); - - wobbleA = sqrt(2) / 50; - wobbleB = sqrt(5) / 50; - - int screenWidth = disp->getWidth(); - int screenHeight = disp->getHeight(); - numColumns = screenWidth / glyphWidth; - numRows = screenHeight / glyphWidth; - numCells = numColumns * numRows; - - for (int i = 0; i < 360; i++) - { - sineTable[i] = sin(M_PI / 180 * i); - } - - LCDBitmap *glyphSpritesheet = gfx->loadBitmap("images/matrix-glyphs", NULL); - int glyphSpritesheetWidth; - gfx->getBitmapData(glyphSpritesheet, &glyphSpritesheetWidth, NULL, NULL, NULL, NULL); - int spritesheetColumns = floor(glyphSpritesheetWidth / glyphWidth); - - LCDBitmap *fadeGradient = gfx->loadBitmap("images/fade-gradient", NULL); - int fadeGradientWidth; - gfx->getBitmapData(fadeGradient, &fadeGradientWidth, NULL, NULL, NULL, NULL); - - glyphs = sys->realloc(NULL, sizeof(LCDBitmap *) * numTotalGlyphs * numFades); - - LCDBitmap *glyph = gfx->newBitmap(glyphWidth, glyphWidth, kColorBlack); - - gfx->pushContext(glyph); - for (int i = 0; i < numTotalGlyphs; i++) { - int column = i % spritesheetColumns; - int row = i / spritesheetColumns; - gfx->drawBitmap(glyphSpritesheet, -column * glyphWidth, -row * glyphWidth, kBitmapUnflipped); - for (int j = 0; j < numFades; j++) { - float fade = j / (numFades - 1.0); - LCDBitmap *variant = gfx->copyBitmap(glyph); - glyphs[i * numFades + j] = variant; - gfx->pushContext(variant); - gfx->drawBitmap(fadeGradient, fade * (glyphWidth - fadeGradientWidth), 0, kBitmapUnflipped); - gfx->popContext(); - } - } - gfx->popContext(); - - gfx->freeBitmap(glyphSpritesheet); - gfx->freeBitmap(fadeGradient); - - cells = sys->realloc(NULL, sizeof(Cell) * numCells); - - int i = 0; - for (int x = 0; x < numColumns; x++) { - float columnTimeOffset = randf() * 1000; - float columnSpeedOffset = randf() * 0.5f + 0.5f; - for (int y = 0; y < numRows; y++) { - Cell *cell = &cells[i]; - i++; - - cell->x = x; - cell->y = y; - cell->glyphCycle = randf(); - cell->columnTimeOffset = columnTimeOffset; - cell->columnSpeedOffset = columnSpeedOffset; - cell->glyphIndex = rand() % numStandardGlyphs; - cell->fadeIndex = -1; - } - } -} - -static int update(void* ud) -{ - float delta; - int currentFadeSkip = 1; - - if (sys->isCrankDocked()) { - speed += 0.07f; - if (speed > maxSpeed) { - speed = maxSpeed; - } - delta = sys->getElapsedTime() * speed; - currentFadeSkip = 6; - } else { - speed -= 0.07f; - if (speed < minSpeed) { - speed = minSpeed; - } - delta = sys->getElapsedTime() * speed + sys->getCrankChange() * 5 / 360; - } - sys->resetElapsedTime(); - time += delta; - - int currentNumFades = numFades / currentFadeSkip; - - PDButtons currentButtons; - sys->getButtonState(¤tButtons, NULL, NULL); - int addPDGlyphs = currentButtons & kButtonA && currentButtons & kButtonB; - - for (int i = 0; i < numCells; i++) { - int mustDraw = 0; - Cell *cell = &cells[i]; - - float cellTime = cell->y * -0.03f + cell->columnTimeOffset + time * cell->columnSpeedOffset; - float brightness = 4 * fmodf( - cellTime - + 0.3f * sineTable[(int)(fmodf(wobbleA * cellTime, 360))] - + 0.2f * sineTable[(int)(fmodf(wobbleB * cellTime, 360))], - 1 - ); - - int fadeIndex = brightness * currentNumFades; - fadeIndex *= currentFadeSkip; - if (fadeIndex < 0) fadeIndex = 0; - if (fadeIndex >= numFades - 1) fadeIndex = numFades - 1; - if (cell->fadeIndex != fadeIndex) { - cell->fadeIndex = fadeIndex; - mustDraw = 1; - } - - cell->glyphCycle = cell->glyphCycle + delta * 2; - if (cell->glyphCycle > 1) { - cell->glyphCycle = fmodf(cell->glyphCycle, 1); - int lastGlyphIndex = cell->glyphIndex; - while (cell->glyphIndex == lastGlyphIndex) { - cell->glyphIndex = rand(); - if (addPDGlyphs && rand() % 4 == 0) { - cell->glyphIndex = numStandardGlyphs + cell->glyphIndex % numPDGlyphs; - } else { - cell->glyphIndex = cell->glyphIndex % numStandardGlyphs; - } - } - if (fadeIndex < numFades - 1) { - mustDraw = 1; - } - } - - if (mustDraw) { - LCDBitmap *glyph = glyphs[cell->glyphIndex * numFades + cell->fadeIndex]; - gfx->drawBitmap(glyph, cell->x * glyphWidth, cell->y * glyphWidth, kBitmapUnflipped); - } - } - - return 1; -} - -#ifdef _WINDLL -__declspec(dllexport) -#endif -int eventHandler(PlaydateAPI* playdate, PDSystemEvent event, uint32_t arg) -{ - if ( event == kEventInit ) - { - snd = playdate->sound; - gfx = playdate->graphics; - sys = playdate->system; - disp = playdate->display; - - disp->setRefreshRate(30); - sys->resetElapsedTime(); - sys->setUpdateCallback(update, NULL); - init(); - } - - return 0; -} diff --git a/playdate/matrix_lua/Source/images/fade-gradient.png b/playdate/matrix_lua/Source/images/fade-gradient.png deleted file mode 100644 index 7c8f97f..0000000 Binary files a/playdate/matrix_lua/Source/images/fade-gradient.png and /dev/null differ diff --git a/playdate/matrix_lua/Source/images/matrix-glyphs.png b/playdate/matrix_lua/Source/images/matrix-glyphs.png deleted file mode 100644 index e7a8ebf..0000000 Binary files a/playdate/matrix_lua/Source/images/matrix-glyphs.png and /dev/null differ diff --git a/playdate/matrix_lua/Source/main.lua b/playdate/matrix_lua/Source/main.lua deleted file mode 100644 index 3b37fed..0000000 --- a/playdate/matrix_lua/Source/main.lua +++ /dev/null @@ -1,138 +0,0 @@ --- --- main.lua --- Matrix Effect (Lua build) --- --- Created by Rezmason on 5/30/22. --- Licensed under MIT. (See the LICENSE file.) --- - -local gfx = playdate.graphics -local floor = math.floor -local random = math.random - -local screenWidth = playdate.display.getWidth() -local screenHeight = playdate.display.getHeight() -local glyphWidth = 20 -local numColumns = floor(screenWidth / glyphWidth) -local numRows = floor(screenHeight / glyphWidth) -local numCells = numColumns * numRows - -local numStandardGlyphs = 135 -local numPDGlyphs = 10 -local numTotalGlyphs = numStandardGlyphs + numPDGlyphs -local numFades = 32 -local glyphs = {} - -do - local glyphSpritesheet = gfx.image.new('images/matrix-glyphs') - local spritesheetColumns = floor(glyphSpritesheet.width / glyphWidth) - local fadeGradient = gfx.image.new('images/fade-gradient') - local glyph = gfx.image.new(glyphWidth, glyphWidth, gfx.kColorBlack) - - gfx.pushContext(glyph) - for i = 1, numTotalGlyphs do - local column = (i - 1) % spritesheetColumns - local row = floor((i - 1) / spritesheetColumns) - glyphSpritesheet:draw(-column * glyphWidth, -row * glyphWidth) - glyphs[i] = {} - for j = 1, numFades do - local fade = (j - 1) / (numFades - 1) - local variant = glyph:copy() - glyphs[i][j] = variant - gfx.pushContext(variant) - fadeGradient:draw(fade * (glyphWidth - fadeGradient.width), 0) - gfx.popContext() - end - end - gfx.popContext() -end - -local minSpeed = 0.15 -local maxSpeed = 1 -local time = 0 -local speed = maxSpeed - -local sineTable = {} -for i = 1,360 do - sineTable[i] = math.sin(math.pi / 180 * i) -end - -local wobbleA = math.sqrt(2) / 50 -local wobbleB = math.sqrt(5) / 50 - -local cells = {} -for x = 1, numColumns do - local columnTimeOffset = random() * 1000 - local columnSpeedOffset = random() * 0.5 + 0.5 - for y = 1, numRows do - local cell = {} - cell.x = x - cell.y = y - cell.glyphCycle = random() - cell.columnTimeOffset = columnTimeOffset - cell.columnSpeedOffset = columnSpeedOffset - cell.glyphIndex = random(numStandardGlyphs) - cell.fadeIndex = -1 - - cells[#cells + 1] = cell - end -end - -playdate.display.setRefreshRate(30) -playdate.resetElapsedTime() - -function playdate.update() - local delta - if playdate.isCrankDocked() then - speed = math.min(maxSpeed, speed + 0.07) - delta = playdate.getElapsedTime() * speed - else - speed = math.max(minSpeed, speed - 0.07) - delta = playdate.getElapsedTime() * speed + playdate.getCrankChange() * 2 / 360 -- TODO: tune - end - playdate.resetElapsedTime() - time += delta - - local addPDGlyphs = playdate.buttonIsPressed(playdate.kButtonA) and playdate.buttonIsPressed(playdate.kButtonB) - - for i = 1, numCells do - local mustDraw = false - local cell = cells[i] - - local cellTime = cell.y * -0.03 + cell.columnTimeOffset + time * cell.columnSpeedOffset - local brightness = 4 * ( - ( - cellTime - + 0.3 * sineTable[floor((wobbleA * cellTime) % 360) + 1] - + 0.2 * sineTable[floor((wobbleB * cellTime) % 360) + 1] - ) % 1 - ) - local fadeIndex = floor(brightness * numFades) - if fadeIndex < 1 then fadeIndex = 1 end - if fadeIndex > numFades then fadeIndex = numFades end - if cell.fadeIndex ~= fadeIndex then - cell.fadeIndex = fadeIndex - mustDraw = true - end - - cell.glyphCycle = cell.glyphCycle + delta * 5 - if cell.glyphCycle > 1 then - cell.glyphCycle = cell.glyphCycle % 1 - local lastGlyphIndex = cell.glyphIndex - while cell.glyphIndex == lastGlyphIndex do - if addPDGlyphs and random(4) == 1 then - cell.glyphIndex = random(numPDGlyphs) + numStandardGlyphs - else - cell.glyphIndex = random(numStandardGlyphs) - end - end - if fadeIndex < numFades then - mustDraw = true - end - end - - if mustDraw then - glyphs[cell.glyphIndex][cell.fadeIndex]:draw((cell.x - 1) * glyphWidth, (cell.y - 1) * glyphWidth) - end - end -end diff --git a/playdate/matrix_lua/Source/pdxinfo b/playdate/matrix_lua/Source/pdxinfo deleted file mode 100644 index fe07014..0000000 --- a/playdate/matrix_lua/Source/pdxinfo +++ /dev/null @@ -1,8 +0,0 @@ -name=The Playtrix (Lua build) -author=Rezmason -description=A familiar animation of mysterious raining symbols. -bundleID=net.rezmason.theplaytrix_lua -version=1.0 -buildNumber=1 -imagePath=path/to/launcher/assets -launchSoundPath=path/to/launch/sound/file diff --git a/screenshot.png b/screenshot.png deleted file mode 100644 index 0391a8e..0000000 Binary files a/screenshot.png and /dev/null differ diff --git a/shaders/glsl/imagePass.frag.glsl b/shaders/glsl/imagePass.frag.glsl deleted file mode 100644 index 1de22d6..0000000 --- a/shaders/glsl/imagePass.frag.glsl +++ /dev/null @@ -1,20 +0,0 @@ -precision mediump float; -uniform sampler2D tex; -uniform sampler2D bloomTex; -uniform sampler2D backgroundTex; -varying vec2 vUV; - -vec4 getBrightness(vec2 uv) { - vec4 primary = texture2D(tex, uv); - vec4 bloom = texture2D(bloomTex, uv); - return primary + bloom; -} - -void main() { - vec3 bgColor = texture2D(backgroundTex, vUV).rgb; - - // Combine the texture and bloom, then blow it out to reveal more of the image - vec4 brightness = getBrightness(vUV); - - gl_FragColor = vec4(bgColor * (brightness.r + brightness.g * 2.0), 1.0); -} diff --git a/shaders/glsl/mirrorPass.frag.glsl b/shaders/glsl/mirrorPass.frag.glsl deleted file mode 100644 index 5683fc3..0000000 --- a/shaders/glsl/mirrorPass.frag.glsl +++ /dev/null @@ -1,38 +0,0 @@ -precision mediump float; -varying vec2 vUV; -uniform float aspectRatio, cameraAspectRatio; -uniform float time; -uniform vec3 clicks[5]; - -uniform sampler2D tex; -uniform sampler2D bloomTex; -uniform sampler2D cameraTex; - -void main() { - - float intensity = 0.0; - for (int i = 0; i < 5; i++) { - vec3 click = clicks[i]; - float distanceToClick = length((click.xy - vUV) * vec2(aspectRatio, 1.0)); - float elapsedTime = clamp(time - click.z, -100.0, 100.0); - float t = distanceToClick - elapsedTime * 0.5; - intensity += sin(t * 40.0) / t; - } - intensity *= 0.2; - - vec2 uv = vUV + intensity * 0.001; - - float webcamAspectAdjust = cameraAspectRatio / aspectRatio; - vec2 webcamTransform = vec2(1.0, webcamAspectAdjust); - if (webcamAspectAdjust > 1.0) { - webcamTransform = vec2(1.0 / webcamAspectAdjust, 1.0); - } - vec2 webcamUV = ((uv - 0.5) * webcamTransform) + 0.5; - - vec3 webcam = texture2D(cameraTex, 1.0 - webcamUV).rgb; - webcam *= mix(vec3(0.1, 0.3, 0.0), vec3(0.9, 1.0, 0.7), 1.0 - length(vUV - 0.5) * 1.5); - - vec3 code = mix(webcam, vec3(0.7, 1.0, 0.4), texture2D(tex, uv).r * (1.0 + intensity * 0.3) + texture2D(bloomTex, uv).r * 0.5); - - gl_FragColor = vec4(code, 1.0); -} diff --git a/shaders/glsl/quiltPass.frag.glsl b/shaders/glsl/quiltPass.frag.glsl deleted file mode 100644 index 3aeb889..0000000 --- a/shaders/glsl/quiltPass.frag.glsl +++ /dev/null @@ -1,42 +0,0 @@ -precision mediump float; -uniform sampler2D quiltTexture; -uniform float pitch; -uniform float tilt; -uniform float center; -uniform float invView; -uniform float flipImageX; -uniform float flipImageY; -uniform float subp; -uniform float tileX; -uniform float tileY; -uniform vec2 quiltViewPortion; -varying vec2 vUV; - -vec2 texArr(vec3 uvz) { - float z = floor(uvz.z * tileX * tileY); - float x = (mod(z, tileX) + uvz.x) / tileX; - float y = (floor(z / tileX) + uvz.y) / tileY; - return vec2(x, y) * quiltViewPortion; -} - -float remap(float value, float from1, float to1, float from2, float to2) { - return (value - from1) / (to1 - from1) * (to2 - from2) + from2; -} - -void main() { - vec4 rgb[3]; - vec3 nuv = vec3(vUV.xy, 0.0); - - // Flip UVs if necessary - nuv.x = (1.0 - flipImageX) * nuv.x + flipImageX * (1.0 - nuv.x); - nuv.y = (1.0 - flipImageY) * nuv.y + flipImageY * (1.0 - nuv.y); - - for (int i = 0; i < 3; i++) { - nuv.z = (vUV.x + float(i) * subp + vUV.y * tilt) * pitch - center; - nuv.z = mod(nuv.z + ceil(abs(nuv.z)), 1.0); - nuv.z = (1.0 - invView) * nuv.z + invView * (1.0 - nuv.z); - rgb[i] = texture2D(quiltTexture, texArr(vec3(vUV.x, vUV.y, nuv.z))); - } - - gl_FragColor = vec4(rgb[0].r, rgb[1].g, rgb[2].b, 1); -} diff --git a/shaders/glsl/stripePass.frag.glsl b/shaders/glsl/stripePass.frag.glsl deleted file mode 100644 index dbd5280..0000000 --- a/shaders/glsl/stripePass.frag.glsl +++ /dev/null @@ -1,40 +0,0 @@ -precision mediump float; -#define PI 3.14159265359 - -uniform sampler2D tex; -uniform sampler2D bloomTex; -uniform sampler2D stripeTex; -uniform float ditherMagnitude; -uniform float time; -uniform vec3 backgroundColor, cursorColor, glintColor; -uniform float cursorIntensity, glintIntensity; -varying vec2 vUV; - -highp float rand( const in vec2 uv, const in float t ) { - const highp float a = 12.9898, b = 78.233, c = 43758.5453; - highp float dt = dot( uv.xy, vec2( a,b ) ), sn = mod( dt, PI ); - return fract(sin(sn) * c + t); -} - -vec4 getBrightness(vec2 uv) { - vec4 primary = texture2D(tex, uv); - vec4 bloom = texture2D(bloomTex, uv); - return primary + bloom; -} - -void main() { - vec3 color = texture2D(stripeTex, vUV).rgb; - - vec4 brightness = getBrightness(vUV); - - // Dither: subtract a random value from the brightness - brightness -= rand( gl_FragCoord.xy, time ) * ditherMagnitude / 3.0; - - gl_FragColor = vec4( - color * brightness.r - + min(cursorColor * cursorIntensity * brightness.g, vec3(1.0)) - + min(glintColor * glintIntensity * brightness.b, vec3(1.0)) - + backgroundColor, - 1.0 - ); -} diff --git a/shaders/wgsl/bloomBlur.wgsl b/shaders/wgsl/bloomBlur.wgsl deleted file mode 100644 index 838cbfb..0000000 --- a/shaders/wgsl/bloomBlur.wgsl +++ /dev/null @@ -1,45 +0,0 @@ -const ONE_OVER_SQRT_2PI = 0.39894; - -struct Config { - bloomRadius : f32, - direction : vec2, -}; - -@group(0) @binding(0) var config : Config; -@group(0) @binding(1) var linearSampler : sampler; -@group(0) @binding(2) var tex : texture_2d; -@group(0) @binding(3) var outputTex : texture_storage_2d; - -struct ComputeInput { - @builtin(global_invocation_id) id : vec3, -}; - -fn gaussianPDF(x : f32) -> f32 { - return ONE_OVER_SQRT_2PI * exp( -0.5 * - ( x * x ) / ( config.bloomRadius * config.bloomRadius ) - ) / config.bloomRadius; -} - -@compute @workgroup_size(32, 1, 1) fn computeMain(input : ComputeInput) { - - var coord = vec2(input.id.xy); - var outputSize = textureDimensions(outputTex); - - if (coord.x >= outputSize.x) { - return; - } - - var uv = (vec2(coord) + 0.5) / vec2(outputSize); - var uvOffset = config.direction / vec2(outputSize); - - var weightSum = gaussianPDF(0.0); - var sum = textureSampleLevel( tex, linearSampler, uv, 0.0) * weightSum; - for (var x : f32 = 1.0; x < config.bloomRadius; x += 1.0) { - var weight = gaussianPDF(x); - sum += textureSampleLevel( tex, linearSampler, uv + uvOffset * x, 0.0) * weight; - sum += textureSampleLevel( tex, linearSampler, uv - uvOffset * x, 0.0) * weight; - weightSum += weight * 2.0; - } - - textureStore(outputTex, coord, sum / weightSum); -} diff --git a/shaders/wgsl/bloomCombine.wgsl b/shaders/wgsl/bloomCombine.wgsl deleted file mode 100644 index aacb2f1..0000000 --- a/shaders/wgsl/bloomCombine.wgsl +++ /dev/null @@ -1,63 +0,0 @@ -struct Config { - pyramidHeight : f32, - bloomStrength : f32 -}; - -@group(0) @binding(0) var config : Config; -@group(0) @binding(1) var linearSampler : sampler; - -@group(0) @binding(2) var tex1 : texture_2d; -@group(0) @binding(3) var tex2 : texture_2d; -@group(0) @binding(4) var tex3 : texture_2d; -@group(0) @binding(5) var tex4 : texture_2d; -@group(0) @binding(6) var outputTex : texture_storage_2d; - -struct ComputeInput { - @builtin(global_invocation_id) id : vec3, -}; - -@compute @workgroup_size(32, 1, 1) fn computeMain(input : ComputeInput) { - - var coord = vec2(input.id.xy); - var outputSize = textureDimensions(outputTex); - - if (coord.x >= outputSize.x) { - return; - } - - var uv = (vec2(coord) + 0.5) / vec2(outputSize); - var sum = vec4(0.0); - - // for (var i = 0.0; i < config.pyramidHeight; i += 1.0) { - // var weight = (1.0 - i / config.pyramidHeight); - // weight = pow(weight + 0.5, 1.0 / 3.0); - // sum += textureSampleLevel( tex, linearSampler, uv, i + 1.0 ) * weight; - // } - - { - var i = 0.0; - var weight = (1.0 - i / config.pyramidHeight); - weight = pow(weight + 0.5, 1.0 / 3.0); - sum += textureSampleLevel( tex1, linearSampler, uv, i + 1.0 ) * weight; - } - { - var i = 1.0; - var weight = (1.0 - i / config.pyramidHeight); - weight = pow(weight + 0.5, 1.0 / 3.0); - sum += textureSampleLevel( tex2, linearSampler, uv, i + 1.0 ) * weight; - } - { - var i = 2.0; - var weight = (1.0 - i / config.pyramidHeight); - weight = pow(weight + 0.5, 1.0 / 3.0); - sum += textureSampleLevel( tex3, linearSampler, uv, i + 1.0 ) * weight; - } - { - var i = 3.0; - var weight = (1.0 - i / config.pyramidHeight); - weight = pow(weight + 0.5, 1.0 / 3.0); - sum += textureSampleLevel( tex4, linearSampler, uv, i + 1.0 ) * weight; - } - - textureStore(outputTex, coord, sum * config.bloomStrength); -} diff --git a/shaders/wgsl/endPass.wgsl b/shaders/wgsl/endPass.wgsl deleted file mode 100644 index e149798..0000000 --- a/shaders/wgsl/endPass.wgsl +++ /dev/null @@ -1,19 +0,0 @@ -@group(0) @binding(0) var nearestSampler : sampler; -@group(0) @binding(1) var tex : texture_2d; - -struct VertOutput { - @builtin(position) Position : vec4, - @location(0) uv : vec2, -}; - -@vertex fn vertMain(@builtin(vertex_index) index : u32) -> VertOutput { - var uv = vec2(f32(index % 2u), f32((index + 1u) % 6u / 3u)); - var position = vec4(uv * 2.0 - 1.0, 1.0, 1.0); - return VertOutput(position, uv); -} - -@fragment fn fragMain(input : VertOutput) -> @location(0) vec4 { - var uv = input.uv; - uv.y = 1.0 - uv.y; - return textureSample( tex, nearestSampler, uv ); -} diff --git a/shaders/wgsl/imagePass.wgsl b/shaders/wgsl/imagePass.wgsl deleted file mode 100644 index 419e85e..0000000 --- a/shaders/wgsl/imagePass.wgsl +++ /dev/null @@ -1,42 +0,0 @@ -struct Config { - unused : f32, -}; - -@group(0) @binding(0) var config : Config; -@group(0) @binding(1) var linearSampler : sampler; -@group(0) @binding(2) var tex : texture_2d; -@group(0) @binding(3) var bloomTex : texture_2d; -@group(0) @binding(4) var backgroundTex : texture_2d; -@group(0) @binding(5) var outputTex : texture_storage_2d; - -struct ComputeInput { - @builtin(global_invocation_id) id : vec3, -}; - -fn getBrightness(uv : vec2) -> vec4 { - var primary = textureSampleLevel(tex, linearSampler, uv, 0.0); - var bloom = textureSampleLevel(bloomTex, linearSampler, uv, 0.0); - return primary + bloom; -} - -@compute @workgroup_size(32, 1, 1) fn computeMain(input : ComputeInput) { - - var unused = config.unused; - - // Resolve the invocation ID to a texel coordinate - var coord = vec2(input.id.xy); - var screenSize = textureDimensions(tex); - - if (coord.x >= screenSize.x) { - return; - } - - var uv = vec2(coord) / vec2(screenSize); - - var bgColor = textureSampleLevel( backgroundTex, linearSampler, vec2(uv.x, 1.0 - uv.y), 0.0 ).rgb; - - // Combine the texture and bloom, then blow it out to reveal more of the image - var brightness = getBrightness(uv); - - textureStore(outputTex, coord, vec4(bgColor * (brightness.r + brightness.g * 2.0), 1.0)); -} diff --git a/shaders/wgsl/mirrorPass.wgsl b/shaders/wgsl/mirrorPass.wgsl deleted file mode 100644 index 0372c1e..0000000 --- a/shaders/wgsl/mirrorPass.wgsl +++ /dev/null @@ -1,80 +0,0 @@ -struct Config { - unused : f32, -}; - -struct Time { - seconds : f32, - frames : i32, -}; - -struct Touches { - touches : array, 5>, -}; - -struct Scene { - screenAspectRatio : f32, - cameraAspectRatio : f32, -}; - -@group(0) @binding(0) var config : Config; -@group(0) @binding(1) var time : Time; -@group(0) @binding(2) var scene : Scene; -@group(0) @binding(3) var touches : Touches; -@group(0) @binding(4) var linearSampler : sampler; -@group(0) @binding(5) var tex : texture_2d; -@group(0) @binding(6) var bloomTex : texture_2d; -@group(0) @binding(7) var cameraTex : texture_2d; -@group(0) @binding(8) var outputTex : texture_storage_2d; - -struct ComputeInput { - @builtin(global_invocation_id) id : vec3, -}; - -fn getBrightness(uv : vec2, intensity : f32) -> vec4 { - - var primary = textureSampleLevel(tex, linearSampler, uv, 0.0); - var bloom = textureSampleLevel(bloomTex, linearSampler, uv, 0.0); - - return primary * (1.0 + intensity * 0.3) + bloom * 0.5; -} - -@compute @workgroup_size(32, 1, 1) fn computeMain(input : ComputeInput) { - - var unused = config.unused; - - // Resolve the invocation ID to a texel coordinate - var coord = vec2(input.id.xy); - var screenSize = textureDimensions(tex); - - if (coord.x >= screenSize.x) { - return; - } - - var uv = vec2(coord) / vec2(screenSize); - - var intensity = 0.0; - for (var i = 0; i < 5; i++) { - var touch = touches.touches[i]; - touch.y = 1.0 - touch.y; - var distanceToClick = length((touch.xy - uv) * vec2(scene.screenAspectRatio, 1.0)); - var elapsedTime = clamp(time.seconds - touch.z, -100.0, 100.0); - var t = distanceToClick - elapsedTime * 0.5; - intensity += sin(t * 40.0) / t; - } - intensity *= 0.2; - - var rippledUV = uv + intensity * 0.001; - - var webcamAspectAdjust = scene.cameraAspectRatio / scene.screenAspectRatio; - var webcamTransform = vec2(1.0, webcamAspectAdjust); - if (webcamAspectAdjust > 1.0) { - webcamTransform = vec2(1.0 / webcamAspectAdjust, 1.0); - } - var webcamUV = ((rippledUV - 0.5) * webcamTransform) + 0.5; - - var webcam = textureSampleLevel(cameraTex, linearSampler, webcamUV, 0.0).rgb; - webcam *= mix(vec3(0.1, 0.3, 0.0), vec3(0.9, 1.0, 0.7), 1.0 - length(uv - 0.5) * 1.5); - - var code = mix(webcam, vec3(0.7, 1.0, 0.4), getBrightness(rippledUV, intensity).r); - textureStore(outputTex, coord, vec4(code, 1.0)); -} diff --git a/shaders/wgsl/palettePass.wgsl b/shaders/wgsl/palettePass.wgsl deleted file mode 100644 index 7c087a5..0000000 --- a/shaders/wgsl/palettePass.wgsl +++ /dev/null @@ -1,76 +0,0 @@ -struct Config { - ditherMagnitude : f32, - backgroundColor : vec3, - cursorColor : vec3, - glintColor : vec3, - cursorIntensity : f32, - glintIntensity : f32, -}; - -struct Palette { - colors : array, 512>, -}; - -struct Time { - seconds : f32, - frames : i32, -}; - -@group(0) @binding(0) var config : Config; -@group(0) @binding(1) var palette : Palette; -@group(0) @binding(2) var time : Time; -@group(0) @binding(3) var linearSampler : sampler; -@group(0) @binding(4) var tex : texture_2d; -@group(0) @binding(5) var bloomTex : texture_2d; -@group(0) @binding(6) var outputTex : texture_storage_2d; - -struct ComputeInput { - @builtin(global_invocation_id) id : vec3, -}; - -const PI : f32 = 3.14159265359; - -fn randomFloat( uv : vec2 ) -> f32 { - let a = 12.9898; - let b = 78.233; - let c = 43758.5453; - let dt = dot( uv, vec2( a, b ) ); - let sn = dt % PI; - return fract(sin(sn) * c); -} - -fn getBrightness(uv : vec2) -> vec4 { - var primary = textureSampleLevel(tex, linearSampler, uv, 0.0); - var bloom = textureSampleLevel(bloomTex, linearSampler, uv, 0.0); - return primary + bloom; -} - -@compute @workgroup_size(32, 1, 1) fn computeMain(input : ComputeInput) { - - // Resolve the invocation ID to a texel coordinate - var coord = vec2(input.id.xy); - var screenSize = textureDimensions(tex); - - if (coord.x >= screenSize.x) { - return; - } - - var uv = vec2(coord) / vec2(screenSize); - - var brightness = getBrightness(uv); - - // Dither: subtract a random value from the brightness - brightness -= randomFloat( uv + vec2(time.seconds) ) * config.ditherMagnitude / 3.0; - - // Map the brightness to a position in the palette texture - var paletteIndex = clamp(i32(brightness.r * 512.0), 0, 511); - - textureStore(outputTex, coord, vec4( - palette.colors[paletteIndex] - + min(config.cursorColor * config.cursorIntensity * brightness.g, vec3(1.0)) - + min(config.glintColor * config.glintIntensity * brightness.b, vec3(1.0)) - + config.backgroundColor, - 1.0 - )); -} - diff --git a/shaders/wgsl/rainPass.wgsl b/shaders/wgsl/rainPass.wgsl deleted file mode 100644 index 43c8c6a..0000000 --- a/shaders/wgsl/rainPass.wgsl +++ /dev/null @@ -1,582 +0,0 @@ -// This shader module is the star of the show. -// It is where the cell states update and the symbols get drawn to the screen. - -struct Config { - // common properties used for compute and rendering - animationSpeed : f32, - glyphSequenceLength : f32, - glyphTextureGridSize : vec2, - glyphHeightToWidth : f32, - gridSize : vec2, - showDebugView : i32, - - // compute-specific properties - brightnessThreshold : f32, - brightnessOverride : f32, - brightnessDecay : f32, - cursorBrightness : f32, - cycleSpeed : f32, - cycleFrameSkip : i32, - fallSpeed : f32, - hasThunder : i32, - raindropLength : f32, - rippleScale : f32, - rippleSpeed : f32, - rippleThickness : f32, - rippleType : i32, - - // render-specific properties - msdfPxRange : f32, - forwardSpeed : f32, - baseBrightness : f32, - baseContrast : f32, - glintBrightness : f32, - glintContrast : f32, - hasBaseTexture: i32, - hasGlintTexture: i32, - glyphVerticalSpacing : f32, - glyphEdgeCrop : f32, - isPolar : i32, - density : f32, - slantScale : f32, - slantVec : vec2, - volumetric : i32, - isolateCursor : i32, - isolateGlint : i32, - loops : i32, - skipIntro : i32, - highPassThreshold : f32, -}; - -// The properties that change over time get their own buffer. -struct Time { - seconds : f32, - frames : i32, -}; - -// The properties related to the size of the canvas get their own buffer. -struct Scene { - screenSize : vec2, - camera : mat4x4, - transform : mat4x4, -}; - -struct Cell { - raindrop : vec4, - symbol : vec4, - effect : vec4, -}; - -// The array of cells that the compute shader updates, and the fragment shader draws. -struct CellData { - cells: array, -}; - -struct IntroCell { - progress : vec4, -}; - -// The array of cells that the compute shader updates, and the fragment shader draws. -struct IntroCellData { - cells: array, -}; - -// Shared bindings -@group(0) @binding(0) var config : Config; -@group(0) @binding(1) var time : Time; - -// Intro-specific bindings -@group(0) @binding(2) var introCells_RW : IntroCellData; - -// Compute-specific bindings -@group(0) @binding(2) var cells_RW : CellData; -@group(0) @binding(3) var introCells_RO : IntroCellData; - -// Render-specific bindings -@group(0) @binding(2) var scene : Scene; -@group(0) @binding(3) var linearSampler : sampler; -@group(0) @binding(4) var glyphMSDFTexture : texture_2d; -@group(0) @binding(5) var glintMSDFTexture : texture_2d; -@group(0) @binding(6) var baseTexture : texture_2d; -@group(0) @binding(7) var glintTexture : texture_2d; -@group(0) @binding(8) var cells_RO : CellData; - -// Shader params - -struct ComputeInput { - @builtin(global_invocation_id) id : vec3, -}; - -struct VertInput { - @builtin(vertex_index) index : u32, -}; - -struct VertOutput { - @builtin(position) Position : vec4, - @location(0) uv : vec2, - @location(1) quadDepth : f32, -}; - -struct FragOutput { - @location(0) color : vec4, - @location(1) highPassColor : vec4, -}; - -// Constants - -const NUM_VERTICES_PER_QUAD : i32 = 6; // 2 * 3 -const PI : f32 = 3.14159265359; -const TWO_PI : f32 = 6.28318530718; -const SQRT_2 : f32 = 1.4142135623730951; -const SQRT_5 : f32 = 2.23606797749979; - -// Helper functions for generating randomness, borrowed from elsewhere - -fn randomFloat( uv : vec2 ) -> f32 { - let a = 12.9898; - let b = 78.233; - let c = 43758.5453; - let dt = dot( uv, vec2( a, b ) ); - let sn = dt % PI; - return fract(sin(sn) * c); -} - -fn randomVec2( uv : vec2 ) -> vec2 { - return fract(vec2(sin(uv.x * 591.32 + uv.y * 154.077), cos(uv.x * 391.32 + uv.y * 49.077))); -} - -fn wobble(x : f32) -> f32 { - return x + 0.3 * sin(SQRT_2 * x) + 0.2 * sin(SQRT_5 * x); -} - -// Compute shader core functions - -// This is the code rain's key underlying concept. -// It's why glyphs that share a column are lit simultaneously, and are brighter toward the bottom. -// It's also why those bright areas are truncated into raindrops. -fn getRainBrightness(simTime : f32, glyphPos : vec2) -> f32 { - var columnTimeOffset = randomFloat(vec2(glyphPos.x, 0.0)) * 1000.0; - var columnSpeedOffset = randomFloat(vec2(glyphPos.x + 0.1, 0.0)) * 0.5 + 0.5; - if (bool(config.loops)) { - columnSpeedOffset = 0.5; - } - var columnTime = columnTimeOffset + simTime * config.fallSpeed * columnSpeedOffset; - var rainTime = (glyphPos.y * 0.01 + columnTime) / config.raindropLength; - if (!bool(config.loops)) { - rainTime = wobble(rainTime); - } - return 1.0 - fract(rainTime); -} - -// Compute shader additional effects - -fn getThunder(simTime : f32, screenPos : vec2) -> f32 { - if (!bool(config.hasThunder)) { - return 0.0; - } - - var thunderTime = simTime * 0.5; - var thunder = 1.0 - fract(wobble(thunderTime)); - if (bool(config.loops)) { - thunder = 1.0 - fract(thunderTime + 0.3); - } - - thunder = log(thunder * 1.5) * 4.0; - thunder = clamp(thunder, 0.0, 1.0) * 10.0 * pow(screenPos.y, 2.0); - return thunder; -} - -fn getRipple(simTime : f32, screenPos : vec2) -> f32 { - if (config.rippleType == -1) { - return 0.0; - } - - var rippleTime = (simTime * 0.5 + sin(simTime) * 0.2) * config.rippleSpeed + 1.0; // TODO: clarify - if (bool(config.loops)) { - rippleTime = (simTime * 0.5) * config.rippleSpeed + 1.0; - } - - var offset = randomVec2(vec2(floor(rippleTime), 0.0)) - 0.5; - if (bool(config.loops)) { - offset = vec2(0.0); - } - var ripplePos = screenPos * 2.0 - 1.0 + offset; - var rippleDistance : f32; - if (config.rippleType == 0) { - var boxDistance = abs(ripplePos) * vec2(1.0, config.glyphHeightToWidth); - rippleDistance = max(boxDistance.x, boxDistance.y); - } else if (config.rippleType == 1) { - rippleDistance = length(ripplePos); - } - - var rippleValue = fract(rippleTime) * config.rippleScale - rippleDistance; - - if (rippleValue > 0.0 && rippleValue < config.rippleThickness) { - return 0.75; - } - - return 0.0; -} - -// Compute shader main functions - -fn computeIntroProgress (simTime : f32, isFirstFrame : bool, glyphPos : vec2, screenPos : vec2, previous : vec4) -> vec4 { - if (bool(config.skipIntro)) { - return vec4(2.0, 0.0, 0.0, 0.0); - } - - var columnTimeOffset = 0.0; - var column = i32(glyphPos.x); - if (column == i32(config.gridSize.y / 2.0)) { - columnTimeOffset = -1.0; - } else if (column == i32(config.gridSize.y * 0.75)) { - columnTimeOffset = -2.0; - } else { - columnTimeOffset = randomFloat(vec2(glyphPos.x, 0.)) * -4.; - columnTimeOffset += (sin(glyphPos.x / config.gridSize.y * PI) - 1.) * 2. - 2.5; - } - var introTime = (simTime + columnTimeOffset) * config.fallSpeed / config.gridSize.y * 100.0; - - var result = vec4(introTime, 0.0, 0.0, 0.0); - return result; -} - -fn computeRaindrop (simTime : f32, isFirstFrame : bool, glyphPos : vec2, screenPos : vec2, previous : vec4, progress : vec4) -> vec4 { - - var brightness = getRainBrightness(simTime, glyphPos); - var brightnessBelow = getRainBrightness(simTime, glyphPos + vec2(0.0, -1.0)); - - var introProgress = progress.r - (1.0 - glyphPos.y / config.gridSize.y); - var introProgressBelow = progress.r - (1.0 - (glyphPos.y - 1.0) / config.gridSize.y); - - var skipIntro = bool(config.skipIntro); - var activated = bool(previous.b) || skipIntro || introProgress > 0.0; - var activatedBelow = skipIntro || introProgressBelow > 0.0; - - var cursor = brightness > brightnessBelow || (activated && !activatedBelow); - - // Blend the glyph's brightness with its previous brightness, so it winks on and off organically - if (!isFirstFrame) { - var previousBrightness = previous.r; - brightness = mix(previousBrightness, brightness, config.brightnessDecay); - } - - var result = vec4(brightness, f32(cursor), f32(activated), introProgress); - return result; -} - -fn computeSymbol (simTime : f32, isFirstFrame : bool, glyphPos : vec2, screenPos : vec2, previous : vec4, raindrop : vec4) -> vec4 { - - var previousSymbol = previous.r; - var previousAge = previous.g; - var resetGlyph = isFirstFrame; - if (bool(config.loops)) { - resetGlyph = resetGlyph || raindrop.r < 0.0; - } - if (resetGlyph) { - previousAge = randomFloat(screenPos + 0.5); - previousSymbol = floor(config.glyphSequenceLength * randomFloat(screenPos)); - } - var cycleSpeed = config.animationSpeed * config.cycleSpeed; - var age = previousAge; - var symbol = previousSymbol; - if (time.frames % config.cycleFrameSkip == 0) { - age += cycleSpeed * f32(config.cycleFrameSkip); - var advance = floor(age); - if (age > 1.0) { - symbol = floor(config.glyphSequenceLength * randomFloat(screenPos + simTime)); - age = fract(age); - } - } - - var result = vec4(symbol, age, 0.0, 0.0); - return result; -} - -fn computeEffect (simTime : f32, isFirstFrame : bool, glyphPos : vec2, screenPos : vec2, previous : vec4, raindrop : vec4) -> vec4 { - - var multipliedEffects = 1.0 + getThunder(simTime, screenPos); - var addedEffects = getRipple(simTime, screenPos); // Round or square ripples across the grid - - var result = vec4(multipliedEffects, addedEffects, 0.0, 0.0); - return result; -} - -@compute @workgroup_size(32, 1, 1) fn computeIntro(input : ComputeInput) { - - // Resolve the invocation ID to an intro cell coordinate - var column = i32(input.id.x); - - if (column >= i32(config.gridSize.x)) { - return; - } - - var simTime = time.seconds * config.animationSpeed; - var isFirstFrame = time.frames == 0; - - // Update the cell - var glyphPos = vec2(f32(column), 0.0); - var screenPos = glyphPos / config.gridSize; - - var introCell = introCells_RW.cells[column]; - introCell.progress = computeIntroProgress(simTime, isFirstFrame, glyphPos, screenPos, introCell.progress); - introCells_RW.cells[column] = introCell; -} - -@compute @workgroup_size(32, 1, 1) fn computeMain(input : ComputeInput) { - - // Resolve the invocation ID to a cell coordinate - var row = i32(input.id.y); - var column = i32(input.id.x); - - if (column >= i32(config.gridSize.x)) { - return; - } - - var i = row * i32(config.gridSize.x) + column; - - var simTime = time.seconds * config.animationSpeed; - var isFirstFrame = time.frames == 0; - - // Update the cell - var glyphPos = vec2(f32(column), f32(row)); - var screenPos = glyphPos / config.gridSize; - - var cell = cells_RW.cells[i]; - var introCell = introCells_RO.cells[column]; - cell.raindrop = computeRaindrop(simTime, isFirstFrame, glyphPos, screenPos, cell.raindrop, introCell.progress); - cell.symbol = computeSymbol(simTime, isFirstFrame, glyphPos, screenPos, cell.symbol, cell.raindrop); - cell.effect = computeEffect(simTime, isFirstFrame, glyphPos, screenPos, cell.effect, cell.raindrop); - cells_RW.cells[i] = cell; -} - -// Vertex shader - -var quadCorners : array, NUM_VERTICES_PER_QUAD> = array, NUM_VERTICES_PER_QUAD>( - vec2(0.0, 0.0), vec2(0.0, 1.0), vec2(1.0, 1.0), - vec2(0.0, 0.0), vec2(1.0, 1.0), vec2(1.0, 0.0) -); - -@vertex fn vertMain(input : VertInput) -> VertOutput { - - var volumetric = bool(config.volumetric); - - var quadGridSize = select(vec2(1.0), config.gridSize, volumetric); - - // Convert the vertex index into its quad's position and its corner in its quad - var i = i32(input.index); - var quadIndex = i / NUM_VERTICES_PER_QUAD; - - var quadCorner = quadCorners[i % NUM_VERTICES_PER_QUAD]; - - var quadPosition = vec2( - f32(quadIndex % i32(quadGridSize.x)), - f32(quadIndex / i32(quadGridSize.x)) - ); - - // Calculate the vertex's uv - var uv = (quadPosition + quadCorner) / quadGridSize; - - // Determine the quad's depth. This is a static value for each column of quads. - var quadDepth = 0.0; - if (volumetric) { - var startDepth = randomFloat(vec2(quadPosition.x, 0.0)); - quadDepth = fract(startDepth + time.seconds * config.animationSpeed * config.forwardSpeed); - } - - // Calculate the vertex's world space position - var worldPosition = quadPosition * vec2(1.0, config.glyphVerticalSpacing); - worldPosition += quadCorner * vec2(config.density, 1.0); - if (volumetric) { - worldPosition.y += randomFloat(vec2(quadPosition.x, 1.0)); - } - worldPosition /= quadGridSize; - worldPosition = (worldPosition - 0.5) * 2.0; - - // Convert the vertex's world space position to screen space - var screenPosition = vec4(worldPosition, quadDepth, 1.0); - if (volumetric) { - screenPosition.x /= config.glyphHeightToWidth; - screenPosition = scene.camera * scene.transform * screenPosition; - } else { - screenPosition = vec4(screenPosition.xy * scene.screenSize, screenPosition.zw); - } - - return VertOutput( - screenPosition, - uv, - quadDepth - ); -} - -// Fragment shader core functions - -fn median3(i : vec3) -> f32 { - return max(min(i.r, i.g), min(max(i.r, i.g), i.b)); -} - -fn getUV(inputUV : vec2) -> vec2 { - - var uv = inputUV; - - if (bool(config.volumetric)) { - return uv; - } - - if (bool(config.isPolar)) { - // Curved space to make the letters appear to radiate from up above - uv -= 0.5; - uv *= 0.5; - uv.y -= 0.5; - var radius = length(uv); - var angle = atan2(uv.y, uv.x) / (2.0 * PI) + 0.5; - uv = vec2(fract(angle * 4.0 - 0.5), 1.5 * (1.0 - sqrt(radius))); - } else { - // Apply the slant and a scale to space so the viewport is still fully covered by the geometry - uv = vec2( - (uv.x - 0.5) * config.slantVec.x + (uv.y - 0.5) * config.slantVec.y, - (uv.y - 0.5) * config.slantVec.x - (uv.x - 0.5) * config.slantVec.y - ) * config.slantScale + 0.5; - } - - uv.y /= config.glyphHeightToWidth; - - return uv; -} - -fn getBrightness(raindrop : vec4, effect : vec4, uv : vec2, quadDepth : f32) -> vec3 { - - var base = raindrop.r + max(0.0, 1.0 - raindrop.a * 5.0); - var isCursor = bool(raindrop.g) && bool(config.isolateCursor); - var glint = base; - var multipliedEffects = effect.r; - var addedEffects = effect.g; - - var textureUV = fract(uv * config.gridSize); - base = base * config.baseContrast + config.baseBrightness; - if (bool(config.hasBaseTexture)) { - base *= textureSample(baseTexture, linearSampler, textureUV).r; - } - glint = glint * config.glintContrast + config.glintBrightness; - if (bool(config.hasGlintTexture)) { - glint *= textureSample(glintTexture, linearSampler, textureUV).r; - } - - // Modes that don't fade glyphs set their actual brightness here - if (config.brightnessOverride > 0. && base > config.brightnessThreshold && !isCursor) { - base = config.brightnessOverride; - } - - base = base * multipliedEffects + addedEffects; - glint = glint * multipliedEffects + addedEffects; - - // In volumetric mode, distant glyphs are dimmer - if (bool(config.volumetric) && !bool(config.showDebugView)) { - base = base * min(1.0, quadDepth); - glint = glint * min(1.0, quadDepth); - } - - return vec3( - select(vec2(1.0, 0.0), vec2(0.0, 1.0), isCursor) * base, - glint - ) * raindrop.b; -} - -fn getSymbolUV(symbol : i32) -> vec2 { - var symbolX = symbol % config.glyphTextureGridSize.x; - var symbolY = symbol / config.glyphTextureGridSize.x; - symbolY = config.glyphTextureGridSize.y - symbolY - 1; - return vec2(f32(symbolX), f32(symbolY)); -} - -fn getSymbol(cellUV : vec2, index : i32) -> vec2 { - // resolve UV to cropped position of glyph in MSDF texture - var uv = fract(cellUV * config.gridSize); - uv -= 0.5; - uv *= clamp(1.0 - config.glyphEdgeCrop, 0.0, 1.0); - uv += 0.5; - uv = (uv + getSymbolUV(index)) / vec2(config.glyphTextureGridSize); - - // MSDF: calculate brightness of fragment based on distance to shape - var symbol = vec2(); - { - // var dist = textureSample(glyphMSDFTexture, linearSampler, uv).rgb; - // var sigDist = median3(dist) - 0.5; - // symbol.r = clamp(sigDist / fwidth(sigDist) + 0.5, 0.0, 1.0); - - var unitRange = vec2(config.msdfPxRange) / vec2(textureDimensions(glyphMSDFTexture)); - var screenTexSize = vec2(1.0) / fwidth(uv); - var screenPxRange = max(0.5 * dot(unitRange, screenTexSize), 1.0); - - var signedDistance = median3(textureSample(glyphMSDFTexture, linearSampler, uv).rgb); - var screenPxDistance = screenPxRange * (signedDistance - 0.5); - symbol.r = clamp(screenPxDistance + 0.5, 0.0, 1.0); - } - - if (bool(config.isolateGlint)) { - // var dist = textureSample(glintMSDFTexture, linearSampler, uv).rgb; - // var sigDist = median3(dist) - 0.5; - // symbol.g = clamp(sigDist / fwidth(sigDist) + 0.5, 0.0, 1.0); - - var unitRange = vec2(config.msdfPxRange) / vec2(textureDimensions(glintMSDFTexture)); - var screenTexSize = vec2(1.0) / fwidth(uv); - var screenPxRange = max(0.5 * dot(unitRange, screenTexSize), 1.0); - - var signedDistance = median3(textureSample(glintMSDFTexture, linearSampler, uv).rgb); - var screenPxDistance = screenPxRange * (signedDistance - 0.5); - symbol.g = clamp(screenPxDistance + 0.5, 0.0, 1.0); - } - - return symbol; -} - -// Fragment shader - -@fragment fn fragMain(input : VertOutput) -> FragOutput { - - var uv = getUV(input.uv); - - // Retrieve cell - var gridCoord : vec2 = vec2(uv * config.gridSize); - var gridIndex = gridCoord.y * i32(config.gridSize.x) + gridCoord.x; - var cell = cells_RO.cells[gridIndex]; - - var brightness = getBrightness( - cell.raindrop, - cell.effect, - uv, - input.quadDepth - ); - var symbol = getSymbol(uv, i32(cell.symbol.r)); - - var output : FragOutput; - - if (bool(config.showDebugView)) { - output.color = vec4( - vec3( - cell.raindrop.g, - vec2( - 1.0 - ((1.0 - cell.raindrop.r) * 3.0), - 1.0 - ((1.0 - cell.raindrop.r) * 8.0) - ) * (1.0 - cell.raindrop.g) - ) * symbol.r, - 1.0 - ); - } else { - output.color = vec4(brightness.rg * symbol.r, brightness.b * symbol.g, 0.0); - } - - var highPassColor = output.color; - if (highPassColor.r < config.highPassThreshold) { - highPassColor.r = 0.0; - } - if (highPassColor.g < config.highPassThreshold) { - highPassColor.g = 0.0; - } - if (highPassColor.b < config.highPassThreshold) { - highPassColor.b = 0.0; - } - output.highPassColor = highPassColor; - - return output; -} diff --git a/shaders/wgsl/stripePass.wgsl b/shaders/wgsl/stripePass.wgsl deleted file mode 100644 index c8646bb..0000000 --- a/shaders/wgsl/stripePass.wgsl +++ /dev/null @@ -1,70 +0,0 @@ -struct Config { - ditherMagnitude : f32, - backgroundColor : vec3, - cursorColor : vec3, - glintColor : vec3, - cursorIntensity : f32, - glintIntensity : f32, -}; - -struct Time { - seconds : f32, - frames : i32, -}; - -@group(0) @binding(0) var config : Config; -@group(0) @binding(1) var time : Time; -@group(0) @binding(2) var linearSampler : sampler; -@group(0) @binding(3) var tex : texture_2d; -@group(0) @binding(4) var bloomTex : texture_2d; -@group(0) @binding(5) var stripeTex : texture_2d; -@group(0) @binding(6) var outputTex : texture_storage_2d; - -struct ComputeInput { - @builtin(global_invocation_id) id : vec3, -}; - -const PI : f32 = 3.14159265359; - -fn randomFloat( uv : vec2 ) -> f32 { - let a = 12.9898; - let b = 78.233; - let c = 43758.5453; - let dt = dot( uv, vec2( a, b ) ); - let sn = dt % PI; - return fract(sin(sn) * c); -} - -fn getBrightness(uv : vec2) -> vec4 { - var primary = textureSampleLevel(tex, linearSampler, uv, 0.0); - var bloom = textureSampleLevel(bloomTex, linearSampler, uv, 0.0); - return primary + bloom; -} - -@compute @workgroup_size(32, 1, 1) fn computeMain(input : ComputeInput) { - - // Resolve the invocation ID to a texel coordinate - var coord = vec2(input.id.xy); - var screenSize = textureDimensions(tex); - - if (coord.x >= screenSize.x) { - return; - } - - var uv = vec2(coord) / vec2(screenSize); - - var color = textureSampleLevel( stripeTex, linearSampler, vec2(uv.x, 1.0 - uv.y), 0.0 ).rgb; - - var brightness = getBrightness(uv); - - // Dither: subtract a random value from the brightness - brightness -= randomFloat( uv + vec2(time.seconds) ) * config.ditherMagnitude / 3.0; - - textureStore(outputTex, coord, vec4( - color * brightness.r - + min(config.cursorColor * config.cursorIntensity * brightness.g, vec3(1.0)) - + min(config.glintColor * config.glintIntensity * brightness.b, vec3(1.0)) - + config.backgroundColor, - 1.0 - )); -} diff --git a/svg sources/coptic_texture_simplified.svg b/svg sources/coptic_texture_simplified.svg deleted file mode 100644 index 20e3838..0000000 --- a/svg sources/coptic_texture_simplified.svg +++ /dev/null @@ -1,150 +0,0 @@ - - - - - diff --git a/svg sources/gothic_texture_simplified.svg b/svg sources/gothic_texture_simplified.svg deleted file mode 100644 index 2149f9c..0000000 --- a/svg sources/gothic_texture_simplified.svg +++ /dev/null @@ -1,136 +0,0 @@ - - - - - - - diff --git a/svg sources/huberfish_a.svg b/svg sources/huberfish_a.svg deleted file mode 100644 index 35cb567..0000000 --- a/svg sources/huberfish_a.svg +++ /dev/null @@ -1,326 +0,0 @@ - - - - - diff --git a/svg sources/huberfish_d.svg b/svg sources/huberfish_d.svg deleted file mode 100644 index 85c18a0..0000000 --- a/svg sources/huberfish_d.svg +++ /dev/null @@ -1,231 +0,0 @@ - - - - - diff --git a/svg sources/texture_simplified.svg b/svg sources/texture_simplified.svg deleted file mode 100644 index fbe6d93..0000000 --- a/svg sources/texture_simplified.svg +++ /dev/null @@ -1,119 +0,0 @@ - - - - - diff --git a/webgpu_notes.txt b/webgpu_notes.txt deleted file mode 100644 index bcf8751..0000000 --- a/webgpu_notes.txt +++ /dev/null @@ -1,50 +0,0 @@ -Learning WebGPU - Request an adaptor from navigator.gpu - Request a device from the adapter - Devices have features and (numeric) limits - So do adapters, but the device ones are the important ones - Devices have a destroy method - Create objects from the device - Pipelines (optionally with async) and pipeline layouts - Shader modules (programs) - Textures and samplers - You need a sampler to sample a texture in a shader - Texture has a createView() method, these views get bound to bind groups and passes - Has destroy() method - Buffers - Has destroy() method - Bind groups and bind group layouts - Bind groups are the interfaces defined between the CPU and a pipeline - Command encoders - Used to create passes, like a render pass - begin, set, set, set, end, finish, submit to device queue - It should be possible to reuse command buffers - Render bundle encoders [What are these?] - Query sets - Useful for measuring stats that come from the GPU - [Is this needed to detect errors?] - - -You do not need a canvas to webgpu. -You DO need a canvas to display what you're rendering - Get canvas's "webgpu" context - Configure context to point at the device - Adjust it when you resize - detect in the RAF when the presentation size isn't the canvas size - Create a view of the context's current texture, and reference it in the render pass's color attachments - [When does that currentTexture change?] - -Textures were never resizable, you simply forgot - Screen-size textures have to be destroyed and recreated - Transfer the data over if you need to - -Bind groups let you bind a bunch of resources at once -Render bundles let you reissue commands - -You can only use up to FOUR bind groups on your laptop's device! -Here's an annoying gotcha: uniform buffers have layout requirements listed in the WGSL spec - https://gpuweb.github.io/gpuweb/wgsl/#structure-layout-rules - -Texture lookup in vertex shader - You can only sample textures in fragment stages - But you can use textureLoad anywhere to get a specific texel, no sampler needed