Skip to content

Commit 17bc496

Browse files
committed
Adjust example for skia-python v138 for iTime properly
1 parent da01629 commit 17bc496

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

SkSL_ShaderMulti+SDL2.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
# SDL2 + SkSL
77

88
# Multiple examples from https://shaders.skia.org/
9-
# Requires at least skia-python v132.b11
9+
# Requires at least skia-python v138
1010

1111
from sdl2 import *
1212
import skia
1313
from skia import Paint, Rect, Font, Typeface, ColorWHITE
1414
from OpenGL import GL
1515
import ctypes
16+
import time
1617

1718
width, height = 512, 512
1819
title = b"Skia + PySDL2 + SkSL Example"
@@ -25,7 +26,6 @@
2526
SkSL_code = [ """
2627
// Source: @notargs https://twitter.com/notargs/status/1250468645030858753
2728
float f(vec3 p) {
28-
float iTime = float(iStep) / 1000.0;
2929
p.z -= iTime * 10.;
3030
float a = p.z * .1;
3131
p.xy *= mat2(cos(a), sin(a), -sin(a), cos(a));
@@ -66,7 +66,6 @@
6666
}
6767
6868
half4 main(float2 FC) {
69-
float iTime = float(iStep) / 1000.0;
7069
vec4 o = vec4(0);
7170
vec2 r = iResolution.xy;
7271
vec3 v = vec3(1,3,7), p = vec3(0);
@@ -132,7 +131,6 @@
132131
// -----------------------------------------------
133132
134133
half4 main(in vec2 fragCoord ) {
135-
float iTime = float(iStep) / 1000.0;
136134
vec2 p = fragCoord.xy / iResolution.xy;
137135
vec2 uv = p*vec2(iResolution.x/iResolution.y,1.0);
138136
float time = iTime * speed;
@@ -204,7 +202,6 @@
204202
"""
205203
// Source: @XorDev https://twitter.com/XorDev/status/1475524322785640455
206204
vec4 main(vec2 FC) {
207-
float iTime = float(iStep) / 1000.0;
208205
vec4 o = vec4(0);
209206
vec2 p = vec2(0), c=p, u=FC.xy*2.-iResolution.xy;
210207
float a;
@@ -233,7 +230,6 @@
233230
234231
vec4 main( in vec2 fragCoord )
235232
{
236-
float iTime = float(iStep) / 1000.0;
237233
// Normalized pixel coordinates (from 0 to 1)
238234
vec2 uv = -1.0 + 2.0 * fragCoord / iResolution.xy;
239235
@@ -258,7 +254,6 @@
258254
}
259255
260256
vec4 main(vec2 FC) {
261-
float iTime = float(iStep) / 1000.0;
262257
float e=0,R=0,t=iTime,s;
263258
vec2 r = iResolution.xy;
264259
vec3 q=vec3(0,0,-1), p, d=vec3((FC.xy-.5*r)/r.y,.7);
@@ -287,15 +282,15 @@ def setBuilder():
287282
input = current_index % len(SkSL_code)
288283
from skia import RuntimeEffect, RuntimeShaderBuilder
289284
header = """
290-
uniform int iStep;
285+
uniform float iTime;
291286
float3 iResolution = float3(512, 512, 512);
292287
"""
293288
litEffect = RuntimeEffect.MakeForShader(header + SkSL_code[input])
294289
builder = RuntimeShaderBuilder(litEffect)
295290

296-
def draw(canvas, step):
291+
def draw(canvas, timenow):
297292
global builder
298-
builder.setUniform("iStep", step)
293+
builder.setUniform("iTime", timenow)
299294
paint = Paint()
300295
paint.setShader(builder.makeShader())
301296
canvas.drawRect(Rect(0,0,512,512), paint)
@@ -368,6 +363,7 @@ def main():
368363
font = Font(Typeface("Roman"))
369364
paintWHITE = Paint()
370365
paintWHITE.setColor(ColorWHITE)
366+
initial_time = time.time()
371367

372368
while running:
373369
while SDL_PollEvent(event):
@@ -388,7 +384,7 @@ def main():
388384
GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_STENCIL_BUFFER_BIT)
389385

390386
with surface as canvas:
391-
draw(canvas, step)
387+
draw(canvas, time.time() - initial_time)
392388
canvas.drawString("Click to cycle through the examples", 0, font.getSize(), font, paintWHITE)
393389
step += 10 # arbitrary to look dynamic
394390
surface.flushAndSubmit()

0 commit comments

Comments
 (0)