shader2_live.py 8.7 KB

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