demo_shaders.py 7.6 KB

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