while DISPLAY.loop_running(): # count time as iterations instead t += 1* 0.01 #print(t) # set uniforms, for GL reasons, these are set on '48'? sprite.set_custom_data(48, [t, WIDTH, HEIGHT]) # draw the shader on the sprite (flat rectangle) sprite.draw()
| //#version 330 | |
| #ifdef GL_ES | |
| precision mediump float; | |
| #endif | |
| // Add this in so I can pass in uniforms | |
| uniform vec3 unif[20]; | |
| // is a vec3 so there is three spaces to place a uniform // I assume I can make more uniforms by making this a vec5? Or how? | |
| // for some reason they are in array place index 16 | |
| // 16,0 is uniform0 | |
| // 16,1 is uniform1 | |
| // 16,2 is uniform2 | |
| // ShaderToy variables boilerplate | |
| uniform vec3 iResolution; // viewport resolution (in pixels) | |
| uniform float iTime; // shader playback time (in seconds) | |
| uniform float iTimeDelta; // render time (in seconds) | |
| uniform int iFrame; // shader playback frame | |
| uniform float iChannelTime[4]; // channel playback time (in seconds) | |
| uniform vec3 iChannelResolution[4]; // channel resolution (in pixels) | |
| uniform vec4 iMouse; // mouse pixel coords. xy: current (if MLB down), zw: click | |
| uniform sampler2D iChannel0; // input channel | |
| uniform sampler2D iChannel1; | |
| uniform sampler2D iChannel2; | |
| uniform sampler2D iChannel3; | |
| uniform vec4 iDate; // (year, month, day, time in seconds) | |
| // Shader from shadertoy | |
| // http://www.pouet.net/prod.php?which=57245 | |
| // If you intend to reuse this shader, please add credits to 'Danilo Guanabara' | |
| // http://www.shadertoy.com/view/XsXXDnv | |
| // Why don't #define work? If I comment them in, the code breaks | |
| //#define t iTime | |
| //#define r iResolution.xy | |
| void main() { | |
| // The #defines don't work so instead I have been doing this. | |
| // I found that I HAVE TO assign the unif[...] in the main loop? | |
| float t = unif[16][0]; | |
| vec2 r = vec2(unif[16][1],unif[16][2]); | |
| // Additionally, actually using the uniforms from the | |
| // shadertoy boilerplate doesnt work, and I can't assign them here? | |
| // Commenting this in will break the code. | |
| //iResolution = vec3(unif[16][1],unif[16][2], 0.0); // dont work | |
| //iResolution.xy = vec2(unif[16][1],unif[16][2]); // dont work | |
| // From the original shader code | |
| vec3 c; | |
| float l,z =t; | |
| for(int i=0;i<3;i++) { | |
| vec2 uv,p=gl_FragCoord.xy/r; | |
| uv=p; | |
| p-= 0.5; | |
| p.x*=r.x/r.y; | |
| z+=.07; | |
| l=length(p); | |
| uv+=p/l*(sin(z)+1.)*abs(sin(l*9.-z*2.)); | |
| c[i]=.01/length(abs(mod(uv,1.)-.5)); | |
| } | |
| gl_FragColor=vec4(c/l,t); | |
| } |
sprite.unif[0:3] = [WITDH, HEIGHT, 0.0] # iResolution
sprite.unif[3:6] = [0.0, 0.0, 0.0] # iTime, iTimeDelta, iFrame#define iResolution unif[0]
#define r iResolution.xy //compiles but not checked if it actually works
#define iTime unif[1][0]
#define iTimeDelta unif[1][1]
#define iFrame unif[1][2]gl_FragColor.rgb *= 0.5;
or
gl_FragColor.rgb = vec3(dot(gl_FragColor.rgb, vec3(0.21, 0.72, 0.07)))...
float tmp = unif[18][0]; //but #define is fine, setting value in main()
...
void main(void) {
...sprite.M[2,0,:] = [mousex, mousey, mouseclickx, mouseclicky] # or whatever
...
#define iMouse modelviewmatrix[2][0]
#define iDate modelviewmatrix[2][1]As you're using frag coordinates you don't need any of the matrix or other unit values so they're all potentially available to use. The vertex shader doesn't need to do matrix multiplication to get to screen coordinates if these are set to 0,0 to 1,1 in pi3d.
Not sure about RPi4 don't have one here to check.
import time
import demo
import pi3d
display = pi3d.Display.create(w=800, h=600)
print(display.opengl.gl_id)
sprite = pi3d.Triangle(corners=((-1.0, -1.0),(-1.0, 3.0),(3.0, -1.0)))
shader = pi3d.Shader('shaders/shadertoy01')
sprite.set_shader(shader)
kbd = pi3d.Keyboard()
tm0 = time.time()
while display.loop_running():
sprite.draw()
sprite.unif[3] = time.time() - tm0
sprite.unif[0:2] = [800, 600]
if kbd.read() == 27:
kbd.close()
display.stop()
break
#include std_head_vs.inc
void main(void) {
gl_Position = vec4(vertex, 1.0);
}#include std_head_fs.inc
#define iTime unif[1][0]
#define iResolution unif[0]
void main(void) {
float t = iTime;
vec2 r = iResolution.xy;
vec3 c;
float len, z=t;
for(int i=0; i<3; i++) {
vec2 uv;
vec2 p = gl_FragCoord.xy / r;
uv = p;
p -= 0.5;
p.x *= r.x / r.y;
z += 0.07;
len = length(p);
uv += p / len * (sin(z) + 1.0) * abs(sin(len * 9.0 - z * 2.0));
c[i] = 0.01 / length(abs(mod(uv, 1.0) - 0.5));
}
gl_FragColor = vec4(c / len, t);
}


--
You received this message because you are subscribed to a topic in the Google Groups "pi3d" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pi3d/sQ8amvVqtO4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pi3d+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pi3d/7ab19e0b-d2b4-4c7a-9818-07bdebbce228o%40googlegroups.com.
Will look at the touchdesigner code of it's available and post an example of drawing to reduced scale off screen textures.
Louise, I'd be interested to see how touchdesigner works. As far as I'm aware the fragment shader code will run for every pixel on the screen. The only way I know too do what you are trying to achieve its to render to a small area using a stencil, say, then redraw that texture full screen.
Will look at the touchdesigner code of it's available and post an example of drawing to reduced scale off screen textures.
--
You received this message because you are subscribed to a topic in the Google Groups "pi3d" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pi3d/sQ8amvVqtO4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pi3d+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pi3d/7b3d34bd-01dc-4fad-b659-f9f24148c480o%40googlegroups.com.
#include std_head_fs.inc
#define iTime unif[1][0]
#define iResolution unif[0]
#define scale unif[1][1]
void main(void) {
float t = iTime;
vec2 r = iResolution.xy;
float invScale = 1.0 / scale; // obviously scale must not be zero!
float offset = vec2(invScale - 1.0) * 0.5;
vec3 c;
float len, z=t;
for(int i=0; i<3; i++) {
vec2 uv;
vec2 p = gl_FragCoord.xy / r * invScale - offset;
uv = p;
p -= 0.5;
p.x *= r.x / r.y;
z += 0.07;
len = length(p);
uv += p / len * (sin(z) + 1.0) * abs(sin(len * 9.0 - z * 2.0));
c[i] = 0.01 / length(abs(mod(uv, 1.0) - 0.5));
}
gl_FragColor = vec4(c / len, t);
}import time
import demo
import pi3d
(W, H) = (None, None) # None should fill the screen (there are edge issues)
SCALE = 0.25 #should have 16th the shadertoy workload
display = pi3d.Display.create(w=W, h=H, frames_per_second=60.0)
print(display.opengl.gl_id)
if W is None or H is None:
(W, H) = (display.width, display.height)
sprite = pi3d.Triangle(corners=((-1.0, -1.0),(-1.0, 3.0),(3.0, -1.0)))
shader = pi3d.Shader('shaders/shadertoy01')
sprite.set_shader(shader)
## offscreen texture stuff ##
cam = pi3d.Camera(is_3d=False)
flatsh = pi3d.Shader('shaders/post_vanilla')
post = pi3d.PostProcess(camera=cam, shader=flatsh, scale=SCALE)
kbd = pi3d.Keyboard()
sprite.unif[0:2] = [W, H]
sprite.unif[4] = SCALE
tm0 = time.time()
while display.loop_running():
post.start_capture()
sprite.draw()
post.end_capture()
post.draw()
sprite.unif[3] = time.time() - tm0
k = kbd.read()
if k == 27:
kbd.close()
display.stop()
break#include std_head_vs.inc
varying vec2 texcoordout;
void main(void) {
texcoordout = texcoord * unib[2].xy + unib[3].xy;
texcoordout.y = 1.0 - texcoordout.y;
gl_Position = modelviewmatrix[1] * vec4(vertex, 1.0);
}#include std_head_fs.inc
varying vec2 texcoordout;
void main(void) {
gl_FragColor = texture2D(tex0, texcoordout);
gl_FragColor.a = 1.0;
}Wot! The google groups interface is beyond crap. It's just truncated by post.This is what is missing (I will do all the python again)
--
You received this message because you are subscribed to a topic in the Google Groups "pi3d" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pi3d/sQ8amvVqtO4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pi3d+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pi3d/d0867e18-1605-42b8-a2cf-4700c4c7ef63o%40googlegroups.com.
--
You received this message because you are subscribed to a topic in the Google Groups "pi3d" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pi3d/sQ8amvVqtO4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pi3d+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pi3d/aff764cf-acab-4435-b631-ac8b1094b52fn%40googlegroups.com.
sudo python3 /home/pi/Desktop/Github/Shaders-on-raspberry-pi4/NewTest3shadertoy/run_shader.py
GL (NOTE: this is your printout from the python code, but then upon loading the shader I get this:)
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/pi3d/Shader.py", line 192, in _load_shader
st = resource_string('pi3d', 'shaders/' + sfile)
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 1157, in resource_string
self, resource_name
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 1401, in get_resource_string
return self._get(self._fn(self.module_path, resource_name))
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 1587, in _get
with open(path, 'rb') as stream:
FileNotFoundError: [Errno 2] No such file or directory: '/usr/local/lib/python3.7/dist-packages/pi3d/shaders/shadertoy01.vs'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/pi/Desktop/Github/Shaders-on-raspberry-pi4/NewTest3shadertoy/run_shader.py", line 20, in <module>
shader = pi3d.Shader('shadertoy01')
File "/usr/local/lib/python3.7/dist-packages/pi3d/Shader.py", line 149, in create
shader = Shader(shfile, vshader_source, fshader_source)
File "/usr/local/lib/python3.7/dist-packages/pi3d/Shader.py", line 110, in __init__
vshader_source, '.vs', GL_VERTEX_SHADER)
File "/usr/local/lib/python3.7/dist-packages/pi3d/Shader.py", line 97, in make_shader
src = src or self._load_shader(shfile + suffix, shader_type)
File "/usr/local/lib/python3.7/dist-packages/pi3d/Shader.py", line 195, in _load_shader
st = open(sfile, 'rb').read() #assume it's a non-standard shader in a file
FileNotFoundError: [Errno 2] No such file or directory: 'shadertoy01.vs'
mouse = pi3d.Mouse() # pi3d.Mouse(restrict = True) # changes input coordinates mouse.start() MX, MY = mouse.position()
#-------------------------------------------------
# PUT SHADER ON MATRIX
while display.loop_running():
post.start_capture()
sprite.draw()
post.end_capture()
post.draw()
sprite.unif[3] = time.time() - tm0
# draw the shader buffer into a PIL image here <------------ #
# set image to matrix
matrix.SetImage(image,0,0)Louise, I always run directly from the command line. It generally seems to turn out easier! I do all the development on Ubuntu laptop with MS code editor.The argument passed to Shader() is treated as an absolute path if it starts / otherwise it is treated as relative to the program location otherwise it is looked for in pi3d install directory shaders/ it uses normal os file functions but adds .vs and .fsSo tldr; shaders in same directory don't need shaders in name.Paddy
To view this discussion on the web visit https://groups.google.com/d/msgid/pi3d/CAFeYzYNe8uVkVGov1D5AGhXyPc28wVO0VS8cg0LTc%3DM03DtUhw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pi3d/6bb15c8e-fe26-46b0-9de0-158c1ff6b8cdn%40googlegroups.com.
import numpy as np
from PIL import Image
import ctypes
from pi3d.constants import (opengles, GLint, GLsizei, GLubyte, GL_DEPTH_COMPONENT,
GL_RGBA, GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT, PIL_OK)
...
sprite.unif[0:2] = [W, H]
sprite.unif[4] = SCALE
tm0 = time.time()
f = 0
(ws, hs) = (int(W * SCALE), int(H * SCALE))
(xos, yos) = (int((W - ws) * 0.5), int((H - hs) * 0.5))
np_img = np.zeros((hs, ws, 4), dtype=np.uint8)
while display.loop_running():
f += 1
post.start_capture()
sprite.draw()
if f == 50: # just do it once for testing
opengles.glReadPixels(GLint(xos), GLint(yos), GLsizei(ws), GLsizei(hs), GL_RGBA,
GL_UNSIGNED_BYTE, np_img.ctypes.data_as(ctypes.POINTER(GLubyte)))
np_img = np_img[::-1,:,:3].copy()
a = Image.frombuffer("RGB", (ws, hs), np_img,'raw', 'RGB', 0, 1)
post.end_capture()
...
dt = display.time - last_time
...
last_time = display.time--
You received this message because you are subscribed to a topic in the Google Groups "pi3d" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pi3d/sQ8amvVqtO4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pi3d+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pi3d/35b7e2ea-6878-4e46-af5a-cd2075022756o%40googlegroups.com.
import time
import demo
import pi3d
from PIL import Image
(W, H) = (None, None) # None should fill the screen (there are edge issues)
SCALE = 0.25 #should have 16th the shadertoy workload
display = pi3d.Display.create(w=W, h=H, frames_per_second=60.0)
print(display.opengl.gl_id)
if W is None or H is None:
(W, H) = (display.width, display.height)
sprite = pi3d.Triangle(corners=((-1.0, -1.0),(-1.0, 3.0),(3.0, -1.0)))
shader = pi3d.Shader('shaders/shadertoy01')
sprite.set_shader(shader)
## offscreen texture stuff ##
cam = pi3d.Camera(is_3d=False)
flatsh = pi3d.Shader('shaders/post_vanilla')
post = pi3d.PostProcess(camera=cam, shader=flatsh, scale=SCALE)
kbd = pi3d.Keyboard()
sprite.unif[0:2] = [W, H]
sprite.unif[4] = SCALE
tm0 = time.time()
f = 0
(ws, hs) = (int(W * SCALE), int(H * SCALE))
(xos, yos) = (int((W - ws) * 0.5), int((H - hs) * 0.5))
while display.loop_running():
f += 1
post.start_capture()
sprite.draw()
if f == 50 or f == 51:
a = Image.fromarray(pi3d.masked_screenshot(xos, yos, ws, hs))
post.end_capture()
post.draw()
sprite.unif[3] = time.time() - tm0
k = kbd.read()
if k == 27:
kbd.close()
display.stop()
breakdef masked_screenshot(x, y, w, h):
"""returns numpy array from part of screen so it can be used by applications
drawing low resolution offscreen textures using scaling.
"""
img = np.zeros((h, w, 4), dtype=np.uint8)
opengles.glReadPixels(GLint(x), GLint(y), GLsizei(w), GLsizei(h), GL_RGBA,
GL_UNSIGNED_BYTE, img.ctypes.data_as(ctypes.POINTER(GLubyte)))
return img[::-1,:,:3].copy()
--
You received this message because you are subscribed to a topic in the Google Groups "pi3d" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pi3d/sQ8amvVqtO4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pi3d+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pi3d/b888f3b2-68be-4555-a641-e8b9ae68b5d2o%40googlegroups.com.
mouse = pi3d.Mouse(use_x=True)
--
You received this message because you are subscribed to a topic in the Google Groups "pi3d" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pi3d/sQ8amvVqtO4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pi3d+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pi3d/53b2ba85-e262-437c-b89f-89426d2b5aa7o%40googlegroups.com.
--
You received this message because you are subscribed to a topic in the Google Groups "pi3d" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pi3d/sQ8amvVqtO4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pi3d+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pi3d/ea08c46f-eeee-4039-b6ff-1a655f888725o%40googlegroups.com.