demo_shaders.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 430
  12. in vec3 in_position;
  13. void main(){
  14. gl_Position = vec4(in_position, 0.9);
  15. }
  16. """
  17. fragment_shader = """
  18. #version 450
  19. #define PI 3.1415926538
  20. out vec4 fragColor;
  21. uniform vec2 resolution;
  22. uniform float time;
  23. vec4 ring(vec4 col,vec2 uv, int z,float ang, float speed,float scaleX, float scaleY){
  24. //z =1; //count of star's
  25. //ang = 270;
  26. float t = time;
  27. for( float i=0.0; i < z; i++){
  28. float a = 0;
  29. a = ang/z * i * (PI/180) ;
  30. //a += time*speed;
  31. a += time/100*speed;
  32. float dy = cos(a);
  33. float dx = sin(a);
  34. dy = dy * scaleY; // *.4; // scale y
  35. dx = dx * scaleX; //*.9; // scale x
  36. if( i== 0){
  37. //col = vec2(dx,dy);
  38. }else{
  39. col += .30 / length(uv+vec2(dx,dy) );
  40. }
  41. }
  42. col *= sin(vec4(0.4,0.8,0.9,1) * time) * .35 +0.25;
  43. fragColor = col ;//vec4(col, 1.0);
  44. return col;
  45. //return fragColor;
  46. }
  47. vec4 block(vec2 uv,vec4 col, vec2 a, vec2 b){
  48. if( uv.x > a.x && uv.x < a.x+b.x ){
  49. if( uv.y > a.y && uv.y < a.y+b.y ){
  50. col.r = 0 ;//time ;//vec4(0,time, 0,1);
  51. col.g = 0.5 ;//time ;//vec4(0,time, 0,1);
  52. col.b = 0.9 ;//time ;//vec4(0,time, 0,1);
  53. //col[3] += time*10 ;//vec4(0,time, 0,1);
  54. }
  55. }
  56. return col;
  57. }
  58. void main1(){;
  59. vec2 uv = gl_FragCoord.xy ; //vUV.st ;
  60. //vec2 uv = (gl_FragCoord.xy - resolution.xy/2) / resolution.y;
  61. uv -= resolution.xy/2;
  62. float r = 0.17;
  63. vec2 a = vec2(200,0);
  64. vec2 b = vec2(50,50);
  65. int z = 10;
  66. float ang = 360;
  67. float speed = .9;
  68. uv.x = uv.x * 2 -1;
  69. uv.y = uv.y * 2 -1;
  70. uv += vec2(0,1);
  71. uv.x += 1;
  72. vec4 col = vec4(0,0,0,1);
  73. col += 0.1 / length(uv+vec2(.6,.8) ); sun
  74. # background animation
  75. float mysin = sin(uv.x/3.14/20*time);
  76. float mysin2 = sin(uv.y/3.14/20*time*4);
  77. float mycos = cos(uv.x/3.14/10*time);
  78. col += vec4(mysin2,0, 0,1);
  79. //col += vec4(mysin-mysin2,0, 0,1);
  80. # BOX
  81. a.x += sin(time*2)*100*3;
  82. a.y += cos(time*2)*100*3;
  83. //b.y += 150+time*100;
  84. col = block(uv,col,a,b);
  85. a = vec2(0,0);
  86. b = vec2(100,200);
  87. a.x += cos(time*2)*100*3;
  88. a.y += sin(-1*time*2)*100*3;
  89. //b.y += 150+time*100;
  90. col = block(uv,col,a,b);
  91. //col.r -= 0.1;
  92. float dist = distance(uv , vec2(10,10));
  93. float circle = smoothstep((10-0.01),(10+0.01),1.0-dist );
  94. # Planet's
  95. z = 20; //count of star's
  96. ang = 360;
  97. speed = 50;
  98. col = ring(col,uv, z, ang, speed, 180, 180)*4;
  99. z = 80; //count of star's
  100. ang = 360;
  101. speed = 50;
  102. col = ring(col,uv, z, ang, -speed, 280, 280/2)*2;
  103. # blue background
  104. if( uv.x > -100 && uv.x < 100 ){
  105. col.b = 1 ;//length(uv+vec2(2.9,0.9) )/200;
  106. }
  107. fragColor = col ;//vec4(col,1);
  108. }
  109. void main2(){
  110. vec2 uv = gl_FragCoord.xy ; //vUV.st ;
  111. uv -= resolution.xy/2;
  112. vec4 col = vec4(0,0,0,1);
  113. if( uv.x > -100 && uv.x < 100 ){
  114. col.b = 1 ;//length(vec2(2.9,0.9) )/200;
  115. }
  116. fragColor = col ;//vec4(col,1);
  117. }
  118. void main(){
  119. main1();
  120. //main2();
  121. }
  122. """
  123. import math
  124. class Screen(mglw.WindowConfig):
  125. window_size = 600, 300
  126. window_size = 900, 506
  127. #window_size = 1200, 900
  128. #window_size = 1900, 1070
  129. #resource_dir = 'programms'
  130. def __init__(self,**args):
  131. super().__init__(**args)
  132. self.quad = mglw.geometry.quad_fs()
  133. # load shader's as string
  134. self.prog = self.ctx.program(vertex_shader=vertex_shader,
  135. fragment_shader=fragment_shader)
  136. # load shader's form file
  137. #self.prog = self.load_program(vertex_shader='vertex_shader.gls1',
  138. # #fragment_shader='fragment_shader.gls1')
  139. self.set_uniform('resolution',self.window_size)
  140. self.t = time.time()
  141. self.ANG = 0
  142. self.ANG_DIR = 1
  143. def set_uniform(self,u_name, u_value):
  144. try:
  145. self.prog[u_name] = u_value
  146. except KeyError:
  147. print(f'uniform: {u_name} - not used in shader')
  148. def render(self,_time,frame_time):
  149. self.ctx.clear()
  150. _t = _time % 600 # max cycle time 10minutes
  151. #self.set_uniform('time',_t)
  152. if self.ANG_DIR:
  153. self.ANG += 0.01
  154. else:
  155. self.ANG -= 0.01
  156. if self.ANG <= -2:
  157. self.ANG_DIR = 1
  158. self.ANG = -2
  159. elif self.ANG >= 3.1415:
  160. #self.ANG_DIR = 0
  161. self.ANG = 0
  162. a = self.ANG
  163. #a = math.sin(a ) #math.radians(a) )
  164. self.set_uniform('time',a)
  165. self.quad.render(self.prog)
  166. if self.t+1 < time.time():
  167. print(_time,_t,self.ANG)
  168. self.t = time.time()
  169. if __name__ == "__main__":
  170. mglw.run_window_config(Screen)