logo

TOjeIT 28. 8. 2020

Programování grafické karty


Odkaz na ShaderToy: https://www.shadertoy.com/new
  1. Začínáme

  2. void mainImage(out vec4 fragColor, in vec2 fragCoord){
    	fragColor = vec4(1.0,1.0,0.0,1.0);  //za dvema lomitky je komentar, tady se nastavuje zluta barva pixelu
    }
    
    
  3. Obarvení podle pozice

  4. void mainImage( out vec4 fragColor, in vec2 fragCoord){
      fragColor= vec4(1.0,1.0,0.0,1.0);
    	
      float fx = fragCoord.x; //souradnice x fragmentu
      float fy = fragCoord.y; //souradnice y fragmentu
      if (fy < 100.0)
        	fragColor= vec4(0.0,0.0,0.0,1.0);
    }
    
    
  5. Odstín podle pozice

  6. void mainImage( out vec4 fragColor, in vec2 fragCoord){
     vec2 texc = fragCoord.xy / iResolution.xy;
     fragColor = vec4(texc.x,texc.y,0.0,1.0);
    }
    
    
  7. Barva z textury, nutno nastavit iChannel0

  8. void mainImage( out vec4 fragColor, in vec2 fragCoord){
      vec2 texc = fragCoord.xy / iResolution.xy;
      vec3 color = texture(iChannel0,texc).rgb; 	//barva z textury
      fragColor = vec4(color,1.0);                  //barva pixelu RGBA
    }
    
    
  9. Kruh

  10. void mainImage( out vec4 fragColor, in vec2 fragCoord){
      vec2 texc = fragCoord.xy / iResolution.xy;
      vec3 color = texture(iChannel0, texc).rgb; 
      fragColor = vec4(color,1.0); 
      
      vec2 f = fragCoord.xy;    //souradnice xy pixelu
      vec2 s = vec2(300.0,150); //souradnice xy stredu
      float r = 50.0;           //polomer
      if (length(f-s) < r)
        fragColor = vec4(1.0,1.0,0.0,1.0);	
    }
    
    
  11. Kruh na pozici myši

  12. void mainImage( out vec4 fragColor, in vec2 fragCoord){
      vec2 texc = fragCoord.xy / iResolution.xy;
      vec3 color = texture(iChannel0, texc).rgb; 
      fragColor = vec4(color,1.0); 
      
      vec2 f = fragCoord.xy;    //souradnice xy pixelu
      vec2 s = iMouse.xy;       //souradnice xy stredu podle mysi  
      float r = 50.0;           //polomer
      if (length(f - s) < r)
        fragColor= vec4(1.0,1.0,0.0,1.0);
    }
    
    
  13. Čtverec

  14. void mainImage(out vec4 fragColor, in vec2 fragCoord){
      vec2 texc = fragCoord.xy / iResolution.xy;
      vec3 color = texture(iChannel0, texc).rgb; 
      fragColor = vec4(color,1.0); 
      
      vec2 f = fragCoord.xy;    //souradnice xy pixelu
      vec2 s = iMouse.xy;       //souradnice xy stredu podle mysi  
      float r = 50.0;           //polomer
      if (max(abs(f.x-s.x),abs(f.y-s.y)) < r)  
        fragColor= vec4(1.0,1.0,0.0,1.0);	
    }
    
    
  15. Pohyb obrazu na pozadí

  16. void mainImage(out vec4 fragColor, in vec2 fragCoord){
      vec2 texc = fragCoord.xy / iResolution.xy;
      texc.x = texc.x + 0.1*iTime; //prepocet souradnice x
      vec3 color = texture(iChannel0, texc).rgb; 
      fragColor = vec4(color,1.0); 
      
      vec2 f = fragCoord.xy;    //souradnice xy pixelu
      vec2 s = iMouse.xy;       //souradnice xy stredu podle mysi  
      float r = 50.0;           //polomer
    
      if (length(f-s) < r)  
         fragColor = vec4(1.0,1.0,0.0,1.0);	
    }
    
    
  17. Zašumění obrazu, nutno nastavit šumovou texturu na iChannel1

  18. void mainImage(out vec4 fragColor, in vec2 fragCoord){
      vec2 texc = fragCoord.xy / iResolution.xy;
      texc = texc + 0.03*texture(iChannel1,texc + 0.1*iTime).xy;
    	
      //prepocet souradnice na zaklade sumove textury, polohy a casu
      vec3 color = texture(iChannel0, texc).rgb; 
      fragColor = vec4(color,1.0); 
      
      vec2 f = fragCoord.xy;    //souradnice xy pixelu
      vec2 s = iMouse.xy;       //souradnice xy stredu podle mysi  
      float r = 50.0;           //polomer
    	if (length(f-s) < r)
      		fragColor = vec4(1.0,1.0,0.0,1.0);			
    }
    
    
  19. Kombinace efektů

  20. void mainImage(out vec4 fragColor, in vec2 fragCoord){
      vec2 texc = fragCoord.xy / iResolution.xy;
      vec2 texc2 = texc + 0.03*texture(iChannel1,texc + 0.1*iTime).xy;
      vec3 color = texture(iChannel0, texc2).rgb;
      fragColor = vec4(color,1.0); 
      vec2 f = fragCoord.xy;             //souradnice xy pixelu
      vec2 s = iMouse.xy;                //souradnice xy stredu podle mysi  
    
      float r = iResolution.x / 5.0;     //polomer kruhu
      if (abs(length(f-s) - r) < 2.0)
         fragColor = vec4(0.0,0.0,0.0,1.0); //hranice kruznice
      if (length(f-s) < r)                  //vypln kruhu
         fragColor = vec4(texture(iChannel0, texc).rgb,1.0);
    }
    
    
  21. Efekt zakřiveni lupy

  22. void mainImage(out vec4 fragColor, in vec2 fragCoord){
      vec2 texc = fragCoord.xy / iResolution.xy;
      vec2 texc2 = texc + 0.03*texture(iChannel1,texc + 0.1*iTime).xy;
      vec3 color = texture(iChannel0, texc2).rgb;
      fragColor = vec4(color,1.0); 
      vec2 f = fragCoord.xy;                      //souradnice xy pixelu
      vec2 s = iMouse.xy;                         //souradnice xy stredu podle mysi  
      float r = iResolution.x / 5.0;              //polomer kruhu
      float l = length(f - s);                    //vzdalenost od stredu
        
      if (abs(l-r) < 2.0)
         fragColor = vec4(.0,.0,.0,1.0);          //hranice kruznice
      if (l < r){                                 //vypln kruhu
         vec2 d = (f - s) * pow(l/r,1.5) * r/l;   //efekt zakriveni cocky
         vec2 a = (s+d/3.0) / iResolution.xy;     //zvetseni
       	 fragColor = vec4(texture(iChannel0, a).rgb,1.0);
      }
    }