Repository navigation
Draw the editor's dot grid with one shader on the web #253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
99b947f
Draw the editor's dot grid with one shader on the web
SunkenInTime f7ad2ea
Keep one dot-grid shader per grid, and the points path untouched
SunkenInTime 5421bbe
Start loading the dot-grid shader from build
SunkenInTime 4ab2816
Retry a failed grid shader load, and test the shader against the points
SunkenInTime File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| #version 460 core | ||
|
|
||
| // The background dot grid, as DotPainter lays it out: a lattice whose | ||
| // spacing is stretched so the outer dots sit on the edges. One shader over | ||
| // one rectangle instead of one point per dot, because the web redraws every | ||
| // point of the grid on every frame. | ||
|
|
||
| precision highp float; | ||
|
|
||
| #include <flutter/runtime_effect.glsl> | ||
|
|
||
| uniform vec2 uSpacing; // distance between dot centres on each axis | ||
| uniform vec2 uLast; // index of the last dot on each axis | ||
| uniform float uRadius; // dot radius | ||
| uniform float uPixel; // one device pixel, in the same units | ||
| uniform vec4 uColor; // dot colour (straight alpha) | ||
|
|
||
| out vec4 fragColor; | ||
|
|
||
| void main() { | ||
| vec2 p = FlutterFragCoord().xy; | ||
|
|
||
| // The nearest dot that exists: the lattice stops at the edges. | ||
| vec2 index = clamp(floor(p / uSpacing + 0.5), vec2(0.0), uLast); | ||
| float distToDot = length(p - index * uSpacing); | ||
|
|
||
| // About one device pixel of antialiasing at any zoom. | ||
| float mask = clamp((uRadius - distToDot) / uPixel + 0.5, 0.0, 1.0); | ||
|
|
||
| float a = uColor.a * mask; | ||
| fragColor = vec4(uColor.rgb * a, a); // premultiplied alpha | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import 'dart:typed_data'; | ||
| import 'dart:ui' as ui; | ||
|
|
||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:icarus/widgets/dot_painter.dart'; | ||
|
|
||
| // Its own file: the shader loads on the real event loop. | ||
| void main() { | ||
| Future<Uint8List> paint(WidgetTester tester, DotPainter painter, Size size) => | ||
| tester.runAsync(() async { | ||
| final recorder = ui.PictureRecorder(); | ||
| painter.paint(Canvas(recorder), size); | ||
| final image = await recorder | ||
| .endRecording() | ||
| .toImage(size.width.toInt(), size.height.toInt()); | ||
| final bytes = await image.toByteData(); | ||
| return bytes!.buffer.asUint8List(); | ||
| }).then((bytes) => bytes!); | ||
|
|
||
| testWidgets('the web grid shader draws the dots the points do', | ||
| (tester) async { | ||
| final program = await tester.runAsync( | ||
| () => ui.FragmentProgram.fromAsset('shaders/dot_lattice.frag')); | ||
| final shader = program!.fragmentShader(); | ||
| addTearDown(shader.dispose); | ||
| // Sizes whose spacing stretches by different amounts on each axis. | ||
| for (final size in const [Size(300, 200), Size(257, 133)]) { | ||
| final points = await paint(tester, DotPainter(opacity: 1), size); | ||
| final lattice = | ||
| await paint(tester, DotPainter(opacity: 1, shader: shader), size); | ||
| final width = size.width.toInt(), height = size.height.toInt(); | ||
| var worst = 0, ink = 0, latticeInk = 0; | ||
| // Away from the edges: rounding can drop the points' last row or | ||
| // column, which the shader always draws. | ||
| for (var y = 4; y < height - 4; y++) { | ||
| for (var x = 4; x < width - 4; x++) { | ||
| final i = (y * width + x) * 4; | ||
| for (var c = 0; c < 4; c++) { | ||
| final gap = (points[i + c] - lattice[i + c]).abs(); | ||
| if (gap > worst) worst = gap; | ||
| } | ||
| ink += points[i + 3]; | ||
| latticeInk += lattice[i + 3]; | ||
| } | ||
| } | ||
| // The same dots in the same places, antialiased a little differently. | ||
| expect(worst, lessThanOrEqualTo(48), reason: '$size'); | ||
| expect(latticeInk / ink, inInclusiveRange(0.92, 1.1), reason: '$size'); | ||
| } | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.