6
6
# SDL2 + SkSL
7
7
8
8
# Multiple examples from https://shaders.skia.org/
9
- # Requires at least skia-python v132.b11
9
+ # Requires at least skia-python v138
10
10
11
11
from sdl2 import *
12
12
import skia
13
13
from skia import Paint , Rect , Font , Typeface , ColorWHITE
14
14
from OpenGL import GL
15
15
import ctypes
16
+ import time
16
17
17
18
width , height = 512 , 512
18
19
title = b"Skia + PySDL2 + SkSL Example"
25
26
SkSL_code = [ """
26
27
// Source: @notargs https://twitter.com/notargs/status/1250468645030858753
27
28
float f(vec3 p) {
28
- float iTime = float(iStep) / 1000.0;
29
29
p.z -= iTime * 10.;
30
30
float a = p.z * .1;
31
31
p.xy *= mat2(cos(a), sin(a), -sin(a), cos(a));
66
66
}
67
67
68
68
half4 main(float2 FC) {
69
- float iTime = float(iStep) / 1000.0;
70
69
vec4 o = vec4(0);
71
70
vec2 r = iResolution.xy;
72
71
vec3 v = vec3(1,3,7), p = vec3(0);
132
131
// -----------------------------------------------
133
132
134
133
half4 main(in vec2 fragCoord ) {
135
- float iTime = float(iStep) / 1000.0;
136
134
vec2 p = fragCoord.xy / iResolution.xy;
137
135
vec2 uv = p*vec2(iResolution.x/iResolution.y,1.0);
138
136
float time = iTime * speed;
204
202
"""
205
203
// Source: @XorDev https://twitter.com/XorDev/status/1475524322785640455
206
204
vec4 main(vec2 FC) {
207
- float iTime = float(iStep) / 1000.0;
208
205
vec4 o = vec4(0);
209
206
vec2 p = vec2(0), c=p, u=FC.xy*2.-iResolution.xy;
210
207
float a;
233
230
234
231
vec4 main( in vec2 fragCoord )
235
232
{
236
- float iTime = float(iStep) / 1000.0;
237
233
// Normalized pixel coordinates (from 0 to 1)
238
234
vec2 uv = -1.0 + 2.0 * fragCoord / iResolution.xy;
239
235
258
254
}
259
255
260
256
vec4 main(vec2 FC) {
261
- float iTime = float(iStep) / 1000.0;
262
257
float e=0,R=0,t=iTime,s;
263
258
vec2 r = iResolution.xy;
264
259
vec3 q=vec3(0,0,-1), p, d=vec3((FC.xy-.5*r)/r.y,.7);
@@ -287,15 +282,15 @@ def setBuilder():
287
282
input = current_index % len (SkSL_code )
288
283
from skia import RuntimeEffect , RuntimeShaderBuilder
289
284
header = """
290
- uniform int iStep ;
285
+ uniform float iTime ;
291
286
float3 iResolution = float3(512, 512, 512);
292
287
"""
293
288
litEffect = RuntimeEffect .MakeForShader (header + SkSL_code [input ])
294
289
builder = RuntimeShaderBuilder (litEffect )
295
290
296
- def draw (canvas , step ):
291
+ def draw (canvas , timenow ):
297
292
global builder
298
- builder .setUniform ("iStep " , step )
293
+ builder .setUniform ("iTime " , timenow )
299
294
paint = Paint ()
300
295
paint .setShader (builder .makeShader ())
301
296
canvas .drawRect (Rect (0 ,0 ,512 ,512 ), paint )
@@ -368,6 +363,7 @@ def main():
368
363
font = Font (Typeface ("Roman" ))
369
364
paintWHITE = Paint ()
370
365
paintWHITE .setColor (ColorWHITE )
366
+ initial_time = time .time ()
371
367
372
368
while running :
373
369
while SDL_PollEvent (event ):
@@ -388,7 +384,7 @@ def main():
388
384
GL .glClear (GL .GL_COLOR_BUFFER_BIT | GL .GL_STENCIL_BUFFER_BIT )
389
385
390
386
with surface as canvas :
391
- draw (canvas , step )
387
+ draw (canvas , time . time () - initial_time )
392
388
canvas .drawString ("Click to cycle through the examples" , 0 , font .getSize (), font , paintWHITE )
393
389
step += 10 # arbitrary to look dynamic
394
390
surface .flushAndSubmit ()
0 commit comments