shader2_live.py 8.9 KB

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