shader2_live.py 9.7 KB

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