shader2_live.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. import moderngl_window as mglw
  2. import moderngl
  3. #import moderngl.window
  4. import time
  5. #print(dir(moderngl))
  6. #export DISPLAY=:99.0
  7. #Xvfb :99 -screen 0 640x480x24 &
  8. #redirects screen output to invisibel framebuffer
  9. # python3 main.py --window glfw --fullscreen --samples 16 --cursor false --size 800x600
  10. vertex_shader = """
  11. #version 400
  12. in vec3 in_position;
  13. void main(){
  14. gl_Position = vec4(in_position, 0.9);
  15. }
  16. """
  17. fragment_shader = """
  18. #version 400
  19. #define PI 3.1415926538
  20. out vec4 fragColor;
  21. uniform vec4 col2 = vec4(1);
  22. uniform vec4 col3 = vec4(1,0,0,0);
  23. uniform vec4 fix_bg = vec4(0,1,0,0);
  24. uniform float master_dim = 1;
  25. uniform vec4 fix_block_pos = vec4(0,1,0,0);
  26. uniform vec4 fix_circle_pos = vec4(0,1,0,0);
  27. uniform vec2 resolution;
  28. uniform float time;
  29. float x_grid = 1;
  30. vec4 ring(vec4 col,vec2 uv,vec2 aa, int z,float ang, float speed,float scaleX, float scaleY){
  31. //z =1; //count of star's
  32. //ang = 270;
  33. float t = time;
  34. for( float i=0.0; i < z; i++){
  35. float a = 0;
  36. a = ang/z * i * (PI/180) ;
  37. //a += time*speed;
  38. a += time/100*speed;
  39. float dy = cos(a) ;
  40. float dx = sin(a) ;
  41. dy = dy * scaleY; // *.4; // scale y
  42. dx = dx * scaleX; //*.9; // scale x
  43. dy += aa.y;
  44. dx += aa.x;
  45. if( i== 0){
  46. //col = vec2(dx,dy);
  47. }else{
  48. col += .30 / length(uv+vec2(dx,dy) );
  49. }
  50. }
  51. col *= sin(vec4(0.4,0.8,0.9,1) * time) * .35 +0.25;
  52. fragColor = col ;//vec4(col, 1.0);
  53. return col;
  54. //return fragColor;
  55. }
  56. vec4 circle(vec2 uv,vec4 col,vec2 a, float r, vec4 col2){
  57. if( distance( uv-a, vec2(0.5,0.5)) < r/2.0){
  58. col = col2;
  59. }
  60. return col;
  61. }
  62. vec4 block(vec2 uv,vec4 col, vec2 a, vec2 b, vec4 col2){
  63. float r1 = 0;
  64. float boarder = 5;
  65. float x1 = uv.x-a.x;
  66. float x2 = uv.x-a.x+b.x;
  67. if( uv.x > a.x-b.x/2 && uv.x < a.x+b.x/2 ){
  68. float y1 = uv.y-a.y;
  69. float y2 = uv.y-a.y+b.y;
  70. float r2 = 0;
  71. if( uv.y > a.y-b.y/2 && uv.y < a.y+b.y/2 ){
  72. col = col2;
  73. //col.r = 1 ; //time ;//vec4(0,time, 0,1);
  74. //col.g = 0.0 ;//time ;//vec4(0,time, 0,1);
  75. //col.b = 1.1 ;//time ;//vec4(0,time, 0,1);
  76. //col.a = 1.0;
  77. //col *= 0.2+(r1*r2) ;//(r1 * r2);
  78. //col[3] += time*10 ;//vec4(0,time, 0,1);
  79. if( r2< 1){
  80. r2 += 0.2;
  81. }
  82. }
  83. if( r1 < 1){
  84. r1 += 0.2;
  85. }
  86. }
  87. if( uv.y == a.y ){ //|| uv.x == a.x ){
  88. col.r = 1 ; //time ;//vec4(0,time, 0,1);
  89. col.g = 1 ;//time ;//vec4(0,time, 0,1);
  90. col.b = 0 ;//time ;//vec4(0,time, 0,1);
  91. col.a = 1.0;
  92. }
  93. return col;
  94. }
  95. vec4 block1(vec2 uv,vec4 col, vec2 a, vec2 b){
  96. if( uv.x > a.x && uv.x < a.x+b.x ){
  97. if( uv.y > a.y && uv.y < a.y+b.y ){
  98. col.r = 0 ;//time ;//vec4(0,time, 0,1);
  99. col.g = 0.5 ;//time ;//vec4(0,time, 0,1);
  100. col.b = 0.9 ;//time ;//vec4(0,time, 0,1);
  101. col.a = 1.0;
  102. //col[3] += time*10 ;//vec4(0,time, 0,1);
  103. }
  104. }
  105. return col;
  106. }
  107. vec4 pulse(vec2 uv,vec4 col, vec2 a,vec4 col4){
  108. float tt = sin(time*10)*1000;
  109. col = circle(uv,col,a,tt,col4);
  110. col4 = vec4(0,0,0,1);
  111. col = circle(uv,col,a,tt-100,col4);
  112. tt = cos(time)*1000;
  113. col4 = vec4(1,0,0,1);
  114. col = circle(uv,col,a,tt-300,col4);
  115. col4 = vec4(0,0,0,1);
  116. col = circle(uv,col,a,tt-310,col4);
  117. return col;
  118. }
  119. float grid(vec2 st, float res)
  120. {
  121. vec2 grid = fract(st*res);
  122. return (step(res,grid.x) * step(res,grid.y));
  123. }
  124. void main1(){;
  125. vec2 uv = gl_FragCoord.xy ; //vUV.st ;
  126. //vec2 uv = (gl_FragCoord.xy - resolution.xy/2) / resolution.y;
  127. uv -= resolution.xy/2;
  128. float r = 0.17;
  129. vec2 a = vec2(200,0);
  130. vec2 b = vec2(50,50);
  131. int z = 10;
  132. float ang = 360;
  133. float speed = .9;
  134. uv.x = uv.x * 2 -1;
  135. uv.y = uv.y * 2 -1;
  136. uv += vec2(0,1);
  137. uv.x += 1;
  138. vec4 col = vec4(0,0,0,1);
  139. vec4 col4 = vec4(0,1,0,1);
  140. col = fix_bg ; //col44;
  141. vec2 grid_uv = vec2(uv.xy*0.5);
  142. float x = grid( grid_uv, 1/15.) ;
  143. col.rgb += vec3(x,x,x) ;//*x;
  144. //# background animation
  145. //float mysin = sin(uv.x/3.14/20*time);
  146. float mysin = sin(uv.y/3.14/10+time*4);
  147. float mysin2 = cos(uv.y/3.14/20+time*4);
  148. //float mysin2 = sin(uv.y/3.14/20*time*4);
  149. float mycos = cos(uv.x/3.14/10*time);
  150. //col += vec4(mysin2,0, 0,1);
  151. //col += vec4(mysin-mysin2 ,0 ,0, 1);
  152. //col += vec4((mysin-mysin2)*col44.r ,col44.g , col44.b, 1);
  153. //col += vec4((mysin-mysin2)*fix_bg.r ,(mysin-mysin2)*fix_bg.g , (mysin-mysin2)*fix_bg.b, 1);
  154. col -= vec4(mysin*fix_bg.r ,mysin*fix_bg.g ,mysin*fix_bg.b , 1 ) ;
  155. //col += vec4(mysin2*fix_bg.r ,mysin2*fix_bg.g ,mysin2*fix_bg.b , 1 ) ;
  156. a = vec2(0,0);
  157. //col = pulse(uv,col,a,col4);
  158. col4 = vec4(1,0,0,1);
  159. a = vec2(600,0);
  160. //col = pulse(uv,col,a,col4);
  161. //if( uv.x > -100 && uv.x < 100 ){
  162. // col.r = 0;
  163. // col.b = 1 ;//length(uv+vec2(2.9,0.9) )/200;
  164. // col.g = 0;
  165. //}
  166. col += 0.1 / length(uv+vec2(.6,.8) ); //sun
  167. //# BOX
  168. a.x += sin(time*2)*100*3;
  169. a.y += cos(time*2)*100*3;
  170. //b.y += 150+time*100;
  171. b = vec2(100,200);
  172. //col = block(uv,col,a,b,col2);
  173. vec2 aa = vec2(0,0);
  174. aa.x = fix_block_pos.x -255*2;
  175. aa.y = fix_block_pos.y -255*2;
  176. col = block(uv,col,aa,b,col2);
  177. a = vec2(0,0);
  178. b = vec2(100,200);
  179. a.x += cos(time*2)*100*3;
  180. a.y += sin(-1*time*2)*100*3;
  181. //b.y += 150+time*100;
  182. //col = block(uv,col,a,b,col2);
  183. //col = circle(uv,col,a,100,col3);
  184. aa.x = fix_circle_pos.x -255*2;
  185. aa.y = fix_circle_pos.y -255*2;
  186. col = circle(uv,col,aa,100,col3);
  187. a.x += cos(time*2+.1)*100*3;
  188. a.y += sin(-1*time*2+.1)*100*3;
  189. col4 = vec4(1,0,1,0);
  190. col4 = vec4(1,0,1,0);
  191. //col = circle(uv,col,a,100,col4);
  192. //col.r -= 0.1;
  193. a.x = cos(time*2-.3)*100*4;
  194. a.y = sin(-1*time*2-.3)*100*2;
  195. a.y *=-1;
  196. col4 = vec4(1,1,0,0);
  197. //col = circle(uv,col,a,100,col4);
  198. //float dist = distance(uv , vec2(10,10));
  199. //float circle = smoothstep((10-0.01),(10+0.01),1.0-dist );
  200. //# Planet's
  201. a.x = -1000;
  202. z = 20; //count of star's
  203. ang = 360;
  204. speed = 50;
  205. a = vec2(500,100);
  206. //col = ring(col,uv,a, z, ang, speed, 180, 180)*4;
  207. z = 80; //count of star's
  208. ang = 360;
  209. speed = 50;
  210. //col = ring(col,uv,a, z, ang, -speed, 280, 280/2)*2;
  211. //col *= length(master_dim);
  212. //col *= 0.5;
  213. col *= master_dim;
  214. //# blue background
  215. fragColor = col ;//vec4(col,1);
  216. }
  217. void main2(){
  218. vec2 uv = gl_FragCoord.xy ; //vUV.st ;
  219. uv -= resolution.xy/2;
  220. vec4 col = vec4(0,0,0,1);
  221. if( uv.x > -100 && uv.x < 100 ){
  222. col.b = 1 ;//length(vec2(2.9,0.9) )/200;
  223. }
  224. fragColor = col ;//vec4(col,1);
  225. }
  226. void main(){
  227. main1();
  228. //main2();
  229. }
  230. """
  231. import memcache
  232. mc = memcache.Client(['127.0.0.1:11211'], debug=0)
  233. mc.set("some_key", "Some value")
  234. value = mc.get("some_key")
  235. mc.set("another_key", 3)
  236. mc.delete("another_key")
  237. import time
  238. import json
  239. data = {}
  240. start = time.time()
  241. delta = start
  242. def mc_loop():
  243. ch = 141
  244. send = 0
  245. #cmd="stats items"
  246. x=mc.get("index")#cmd)
  247. if x:
  248. #print(x)
  249. print()
  250. for k,v in x.items():
  251. #print(k,v)
  252. x=mc.get(k)
  253. print(k,v,ch,"=",x[ch-1])
  254. time.sleep(.13)
  255. import math
  256. class Screen(mglw.WindowConfig):
  257. window_size = 600, 300
  258. window_size = 900, 506
  259. window_size = 1024, 768
  260. #window_size = 1900, 1070
  261. #resource_dir = 'programms'
  262. def __init__(self,**args):
  263. super().__init__(**args)
  264. self.quad = mglw.geometry.quad_fs()
  265. print(dir(self.quad))
  266. # load shader's as string
  267. self.prog = self.ctx.program(vertex_shader=vertex_shader,
  268. fragment_shader=fragment_shader)
  269. # load shader's form file
  270. print(dir(self.prog))
  271. #self.prog = self.load_program(vertex_shader='vertex_shader.gls1',
  272. # #fragment_shader='fragment_shader.gls1')
  273. self.set_uniform('resolution',self.window_size)
  274. self.t = time.time()
  275. self.ANG = 0
  276. self.ANG_DIR = 1
  277. self.time = 0
  278. def set_uniform(self,u_name, u_value):
  279. try:
  280. self.prog[u_name] = u_value
  281. except KeyError:
  282. print(f'uniform: {u_name} - not used in shader')
  283. def render(self,_time,frame_time):
  284. self.set_uniform('resolution',self.window_size)
  285. self.ctx.clear()
  286. _t = _time % 600 # max cycle time 10minutes
  287. #self.set_uniform('time',_t)
  288. if self.ANG_DIR:
  289. self.ANG += 0.01
  290. else:
  291. self.ANG -= 0.01
  292. if self.ANG <= -2:
  293. self.ANG_DIR = 1
  294. self.ANG = -2
  295. elif self.ANG >= 3.1415:
  296. #self.ANG_DIR = 0
  297. self.ANG = 0
  298. a = self.ANG
  299. #a = math.sin(a ) #math.radians(a) )
  300. x = mc.get("2.0.0.13:2")
  301. if not x:
  302. x = mc.get("10.10.10.13:2")
  303. if not x:
  304. x=[1]*512
  305. r = 1 #255
  306. g = 0
  307. b = 1 #255
  308. aa=0
  309. bb=0
  310. if x:
  311. #a = 1+x[141-1]/255.*10
  312. r = x[141-1]/255
  313. g = x[142-1]/255
  314. b = x[143-1]/255
  315. aa = x[144-1] *4 #* a
  316. bb = x[145-1] *4 #* a
  317. self.time += (x[147-1]/255.-0.5) # *4 #* a
  318. self.set_uniform('time',self.time)
  319. self.set_uniform('fix_block_pos',[aa,bb,0,0])
  320. self.set_uniform('fix_circle_pos',[bb,aa,0,0])
  321. self.set_uniform('master_dim',x[146-1]/255.)
  322. x = time.time()/10%1
  323. #self.set_uniform('col2',[r,g,b,1])
  324. self.set_uniform('fix_bg',[r,g,b,1])
  325. self.set_uniform('col2',[1,x,0,1])
  326. self.quad.render(self.prog)
  327. if self.t+1 < time.time():
  328. print("_time",round(_time,2),"_t",round(_t,2),"self.ANG",round(self.ANG,2))
  329. self.t = time.time()
  330. if __name__ == "__main__":
  331. mglw.run_window_config(Screen)