glow 0.0.1
glow: ^0.0.1 copied to clipboard
Flutter plugin providing GPU shader rendering via a Texture widget. Uses a Rust engine with glow for cross-platform GL (OpenGL, GL ES, WebGL2). ShaderToy compatible.
import 'package:flutter/material.dart';
import 'package:glow/glow.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'game_menu/all_games.dart';
import 'main_in_deep.dart';
void main() {
OpenGLController().initializeGL();
runApp(const ProviderScope(
child: MaterialApp(
home: MyApp(),
),
));
}
class _GamesPage extends StatelessWidget {
const _GamesPage();
@override
Widget build(BuildContext context) {
final games = allGames();
return Scaffold(
appBar: AppBar(title: const Text('Mini Games')),
body: Padding(
padding: const EdgeInsets.only(top: 10),
child: Column(
children: games.map((entry) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: ElevatedButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (ctx) => Scaffold(
appBar: AppBar(title: Text(entry.name)),
body: entry.buildWidget(ctx),
),
),
);
},
child: Text(entry.name),
),
);
}).toList(),
),
),
);
}
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
final String fShader = '''
// https://www.shadertoy.com/view/XlfGRj
// Star Nest by Pablo Roman Andrioli
#define iterations 17
#define formuparam 0.53
#define volsteps 20
#define stepsize 0.1
#define zoom 0.800
#define tile 0.850
#define speed 0.010
#define brightness 0.0015
#define darkmatter 0.300
#define distfading 0.730
#define saturation 0.850
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
//get coords and direction
vec2 uv=fragCoord.xy/iResolution.xy-.5;
uv.y*=iResolution.y/iResolution.x;
vec3 dir=vec3(uv*zoom,1.);
float time=iTime*speed+.25;
//mouse rotation
float a1=.5+iMouse.x/iResolution.x*2.;
float a2=.8+iMouse.y/iResolution.y*2.;
mat2 rot1=mat2(cos(a1),sin(a1),-sin(a1),cos(a1));
mat2 rot2=mat2(cos(a2),sin(a2),-sin(a2),cos(a2));
dir.xz = dir.xz * rot1;
dir.xy = dir.xy * rot2;
vec3 from=vec3(1.,.5,0.5);
from+=vec3(time*2.,time,-2.);
from.xz = from.xz * rot1;
from.xy = from.xy * rot2;
//volumetric rendering
float s=0.1,fade=1.;
vec3 v=vec3(0.);
for (int r=0; r<volsteps; r++) {
vec3 p=from+s*dir*.5;
p = abs(vec3(tile)-mod(p,vec3(tile*2.))); // tiling fold
float pa=0.,a=0.;
for (int i=0; i<iterations; i++) {
p=abs(p)/dot(p,p)-formuparam; // the magic formula
a+=abs(length(p)-pa); // absolute sum of average change
pa=length(p);
}
float dm=max(0.,darkmatter-a*a*.001); //dark matter
a*=a*a; // add contrast
if (r>6) fade*=1.-dm; // dark matter, don't render near
//v+=vec3(dm,dm*.5,0.);
v+=fade;
v+=vec3(s,s*s,s*s*s*s)*a*brightness*fade; // coloring based on distance
fade*=distfading; // distance fading
s+=stepsize;
}
v=mix(vec3(length(v)),v,saturation); //color adjust
fragColor = vec4(v*.01,1.);
}
''';
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: SizedBox(
width: 400,
height: 300,
child: FutureBuilder(
/// The surface size identifies the real texture size and
/// it is not related to the above SizedBox size
future: OpenGLController().openglFFI.createSurface(300, 200),
builder: (_, snapshot) {
if (snapshot.hasError || !snapshot.hasData) {
return const SizedBox.shrink();
}
/// Start renderer thread
OpenGLController().openglFFI.startThread();
/// Set the fragment shader
OpenGLController().openglFFI.setShaderToy(fShader);
/// build the texture widget
return OpenGLTexture(id: snapshot.data!);
},
),
),
),
floatingActionButton: Column(
mainAxisSize: MainAxisSize.min,
children: [
FloatingActionButton(
heroTag: 'explore',
onPressed: () {
OpenGLController().openglFFI.stopThread();
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (_) => const TextureAndTabs()),
);
},
child: const Icon(Icons.explore),
),
const SizedBox(height: 16),
FloatingActionButton(
heroTag: 'games',
onPressed: () {
OpenGLController().openglFFI.stopThread();
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (_) => const _GamesPage()),
);
},
child: const Icon(Icons.games),
),
],
),
);
}
}