-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add new sample simple_sdf #2835
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 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
330dafc
Add sample simple_sdf by copy+rename of simple_shader
andywolff b32b8a7
Update some names to say SDF
andywolff 70cbb49
Replace shader with heart SDF and update screenshot
andywolff 7e7330d
Add license for sdHeart
andywolff 7149969
Add reference to FragmentShader API in the description to help folks …
andywolff 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,44 @@ | ||
| # Miscellaneous | ||
| *.class | ||
| *.log | ||
| *.pyc | ||
| *.swp | ||
| .DS_Store | ||
| .atom/ | ||
| .buildlog/ | ||
| .history | ||
| .svn/ | ||
| migrate_working_dir/ | ||
|
|
||
| # IntelliJ related | ||
| *.iml | ||
| *.ipr | ||
| *.iws | ||
| .idea/ | ||
|
|
||
| # The .vscode folder contains launch configuration and tasks you configure in | ||
| # VS Code which you may wish to be included in version control, so this line | ||
| # is commented out by default. | ||
| #.vscode/ | ||
|
|
||
| # Flutter/Dart/Pub related | ||
| **/doc/api/ | ||
| **/ios/Flutter/.last_build_id | ||
| .dart_tool/ | ||
| .flutter-plugins | ||
| .flutter-plugins-dependencies | ||
| .packages | ||
| .pub-cache/ | ||
| .pub/ | ||
| /build/ | ||
|
|
||
| # Symbolication related | ||
| app.*.symbols | ||
|
|
||
| # Obfuscation related | ||
| app.*.map.json | ||
|
|
||
| # Android Studio will place build artifacts here | ||
| /android/app/debug | ||
| /android/app/profile | ||
| /android/app/release |
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,9 @@ | ||
| # `simple_sdf` | ||
|
|
||
| A simple [Flutter fragment shaders][] sample project showing how to use Signed Distance Functions. | ||
|
andywolff marked this conversation as resolved.
Outdated
|
||
|
|
||
| Use `flutter create --no-overwrite .` to initialize the project. | ||
|
|
||
| [Flutter fragment shaders]: https://docs.flutter.dev/development/ui/advanced/shaders | ||
|
|
||
|  | ||
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 @@ | ||
| include: package:analysis_defaults/flutter.yaml |
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,59 @@ | ||
| import 'dart:ui' as ui; | ||
|
|
||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter_shaders/flutter_shaders.dart'; | ||
|
|
||
| void main() { | ||
| runApp(const MyApp()); | ||
| } | ||
|
|
||
| class MyApp extends StatelessWidget { | ||
| const MyApp({super.key}); | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return MaterialApp( | ||
| title: 'Simple SDF Demo', | ||
| theme: ThemeData(colorSchemeSeed: Colors.blue), | ||
| home: const MyHomePage(), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| class MyHomePage extends StatelessWidget { | ||
| const MyHomePage({super.key}); | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return Scaffold( | ||
| appBar: AppBar(title: const Text('Simple SDF Demo')), | ||
| body: ShaderBuilder( | ||
| assetKey: 'shaders/SDF.frag', | ||
| (context, shader, child) => CustomPaint( | ||
| size: MediaQuery.of(context).size, | ||
| painter: ShaderPainter(shader: shader), | ||
| ), | ||
| child: const Center(child: CircularProgressIndicator()), | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| class ShaderPainter extends CustomPainter { | ||
| ShaderPainter({required this.shader}); | ||
| ui.FragmentShader shader; | ||
|
|
||
| @override | ||
| void paint(Canvas canvas, Size size) { | ||
| shader.setFloat(0, size.width); | ||
| shader.setFloat(1, size.height); | ||
|
|
||
| final paint = Paint()..shader = shader; | ||
| canvas.drawRect(Rect.fromLTWH(0, 0, size.width, size.height), paint); | ||
| } | ||
|
|
||
| @override | ||
| bool shouldRepaint(covariant CustomPainter oldDelegate) { | ||
| return false; | ||
| } | ||
| } |
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,23 @@ | ||
| name: simple_sdf | ||
| description: Using a shader, simply. | ||
| publish_to: 'none' | ||
| version: 1.0.0+1 | ||
| resolution: workspace | ||
|
|
||
| environment: | ||
| sdk: ^3.9.0-0 | ||
|
|
||
| dependencies: | ||
| flutter: | ||
| sdk: flutter | ||
| flutter_shaders: ^0.1.0 | ||
|
|
||
| dev_dependencies: | ||
| analysis_defaults: | ||
| path: ../analysis_defaults | ||
| flutter_test: | ||
| sdk: flutter | ||
| flutter: | ||
| uses-material-design: true | ||
| shaders: | ||
| - shaders/SDF.frag |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,62 @@ | ||
| #version 460 core | ||
|
|
||
| #include <flutter/runtime_effect.glsl> | ||
|
|
||
| precision mediump float; | ||
|
|
||
| uniform vec2 resolution; | ||
| out vec4 fragColor; | ||
|
|
||
| vec3 pink = vec3(255, 105, 180) / 255; | ||
|
|
||
| // dot2 and sdHeart from https://iquilezles.org/articles/distfunctions2d/ | ||
| // | ||
| // The MIT License | ||
| // Copyright © 2015 Inigo Quilez | ||
| // 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. | ||
| // https://www.youtube.com/c/InigoQuilez | ||
| // https://iquilezles.org | ||
|
|
||
| float dot2(vec2 v) { return dot(v, v); } | ||
| float sdHeart(in vec2 p) { | ||
| p.x = abs(p.x); | ||
|
|
||
| if (p.y + p.x > 1.0) | ||
| return sqrt(dot2(p - vec2(0.25, 0.75))) - sqrt(2.0) / 4.0; | ||
| return sqrt(min(dot2(p - vec2(0.00, 1.00)), | ||
| dot2(p - 0.5 * max(p.x + p.y, 0.0)))) * | ||
| sign(p.x - p.y); | ||
| } | ||
|
|
||
| void main() { | ||
| vec2 st = FlutterFragCoord().xy / resolution.xy; | ||
| // Remap coordinates. | ||
| // Flutter normalized coordinates have range [0,1] but sdHeart expects [-1,1]. | ||
| st = (st - vec2(0.5)) * vec2(2.0); | ||
| // Center the heart. | ||
| // sdHeart is written such that the bottom point is at (0,0) and it's about 1 | ||
| // unit tall. | ||
| st.y -= 0.5; | ||
| // Invert Y coordinate. | ||
| st.y *= -1; | ||
|
|
||
| // Calculate the color of this pixel according to the heart SDF, using | ||
| // smoothstep to anti-alias the edges. | ||
| vec3 color = vec3(0.0); | ||
| color = mix(pink, color, smoothstep(0.01, 0.02, sdHeart(st))); | ||
|
|
||
| fragColor = vec4(color, 1); | ||
| } |
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,9 @@ | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:simple_sdf/main.dart'; | ||
|
|
||
| void main() { | ||
| testWidgets('Smoke test', (tester) async { | ||
| // Build our app and trigger a frame. | ||
| await tester.pumpWidget(const MyApp()); | ||
| }); | ||
| } |
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.