shader1_live.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. //# background animation
  132. float mysin = sin(uv.x/3.14/20*time);
  133. float mysin2 = sin(uv.y/3.14/20*time*4);
  134. float mycos = cos(uv.x/3.14/10*time);
  135. //col += vec4(mysin2,0, 0,1);
  136. //col += vec4(mysin-mysin2 ,0 ,0, 1);
  137. //col += vec4((mysin-mysin2)*col44.r ,col44.g , col44.b, 1);
  138. col += vec4((mysin-mysin2)*col44.r ,(mysin-mysin2)*col44.g , (mysin-mysin2)*col44.b, 1);
  139. a = vec2(0,0);
  140. col = pulse(uv,col,a,col4);
  141. col4 = vec4(1,0,0,1);
  142. a = vec2(600,0);
  143. col = pulse(uv,col,a,col4);
  144. if( uv.x > -100 && uv.x < 100 ){
  145. col.r = 0;
  146. col.b = 1 ;//length(uv+vec2(2.9,0.9) )/200;
  147. col.g = 0;
  148. }
  149. col += 0.1 / length(uv+vec2(.6,.8) ); //sun
  150. //# BOX
  151. a.x += sin(time*2)*100*3;
  152. a.y += cos(time*2)*100*3;
  153. //b.y += 150+time*100;
  154. b = vec2(100,200);
  155. col = block(uv,col,a,b,col2);
  156. a = vec2(0,0);
  157. b = vec2(100,200);
  158. a.x += cos(time*2)*100*3;
  159. a.y += sin(-1*time*2)*100*3;
  160. //b.y += 150+time*100;
  161. //col = block(uv,col,a,b,col2);
  162. col = circle(uv,col,a,100,col3);
  163. a.x += cos(time*2+.1)*100*3;
  164. a.y += sin(-1*time*2+.1)*100*3;
  165. col4 = vec4(1,0,1,0);
  166. col4 = vec4(1,0,1,0);
  167. col = circle(uv,col,a,100,col4);
  168. //col.r -= 0.1;
  169. a.x = cos(time*2-.3)*100*4;
  170. a.y = sin(-1*time*2-.3)*100*2;
  171. a.y *=-1;
  172. col4 = vec4(1,1,0,0);
  173. col = circle(uv,col,a,100,col4);
  174. float dist = distance(uv , vec2(10,10));
  175. float circle = smoothstep((10-0.01),(10+0.01),1.0-dist );
  176. //# Planet's
  177. a.x = -1000;
  178. z = 20; //count of star's
  179. ang = 360;
  180. speed = 50;
  181. a = vec2(500,100);
  182. col = ring(col,uv,a, z, ang, speed, 180, 180)*4;
  183. z = 80; //count of star's
  184. ang = 360;
  185. speed = 50;
  186. col = ring(col,uv,a, z, ang, -speed, 280, 280/2)*2;
  187. //# blue background
  188. fragColor = col ;//vec4(col,1);
  189. }
  190. void main2(){
  191. vec2 uv = gl_FragCoord.xy ; //vUV.st ;
  192. uv -= resolution.xy/2;
  193. vec4 col = vec4(0,0,0,1);
  194. if( uv.x > -100 && uv.x < 100 ){
  195. col.b = 1 ;//length(vec2(2.9,0.9) )/200;
  196. }
  197. fragColor = col ;//vec4(col,1);
  198. }
  199. void main(){
  200. main1();
  201. //main2();
  202. }
  203. """
  204. import memcache
  205. mc = memcache.Client(['127.0.0.1:11211'], debug=0)
  206. mc.set("some_key", "Some value")
  207. value = mc.get("some_key")
  208. mc.set("another_key", 3)
  209. mc.delete("another_key")
  210. import time
  211. import json
  212. data = {}
  213. start = time.time()
  214. delta = start
  215. def mc_loop():
  216. ch = 141
  217. send = 0
  218. #cmd="stats items"
  219. x=mc.get("index")#cmd)
  220. if x:
  221. #print(x)
  222. print()
  223. for k,v in x.items():
  224. #print(k,v)
  225. x=mc.get(k)
  226. print(k,v,ch,"=",x[ch-1])
  227. time.sleep(.13)
  228. import math
  229. class Screen(mglw.WindowConfig):
  230. window_size = 600, 300
  231. window_size = 900, 506
  232. window_size = 1024, 768
  233. #window_size = 1900, 1070
  234. #resource_dir = 'programms'
  235. def __init__(self,**args):
  236. super().__init__(**args)
  237. self.quad = mglw.geometry.quad_fs()
  238. print(dir(self.quad))
  239. # load shader's as string
  240. self.prog = self.ctx.program(vertex_shader=vertex_shader,
  241. fragment_shader=fragment_shader)
  242. # load shader's form file
  243. print(dir(self.prog))
  244. #self.prog = self.load_program(vertex_shader='vertex_shader.gls1',
  245. # #fragment_shader='fragment_shader.gls1')
  246. self.set_uniform('resolution',self.window_size)
  247. self.t = time.time()
  248. self.ANG = 0
  249. self.ANG_DIR = 1
  250. def set_uniform(self,u_name, u_value):
  251. try:
  252. self.prog[u_name] = u_value
  253. except KeyError:
  254. print(f'uniform: {u_name} - not used in shader')
  255. def render(self,_time,frame_time):
  256. self.set_uniform('resolution',self.window_size)
  257. self.ctx.clear()
  258. _t = _time % 600 # max cycle time 10minutes
  259. #self.set_uniform('time',_t)
  260. if self.ANG_DIR:
  261. self.ANG += 0.01
  262. else:
  263. self.ANG -= 0.01
  264. if self.ANG <= -2:
  265. self.ANG_DIR = 1
  266. self.ANG = -2
  267. elif self.ANG >= 3.1415:
  268. #self.ANG_DIR = 0
  269. self.ANG = 0
  270. a = self.ANG
  271. #a = math.sin(a ) #math.radians(a) )
  272. x = mc.get("2.0.0.13:2")
  273. r = 1#2155
  274. g = 1
  275. b = 1#255
  276. if x:
  277. #a = 1+x[141-1]/255.*10
  278. r = x[141-1]/255
  279. g = x[142-1]/255
  280. b = x[143-1]/255
  281. self.set_uniform('time',a)
  282. x = time.time()/10%1
  283. #self.set_uniform('col2',[r,g,b,1])
  284. self.set_uniform('col44',[r,g,b,1])
  285. self.set_uniform('col2',[1,x,0,1])
  286. self.quad.render(self.prog)
  287. if self.t+1 < time.time():
  288. print(_time,_t,self.ANG)
  289. self.t = time.time()
  290. if __name__ == "__main__":
  291. mglw.run_window_config(Screen)