webgl_shader.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - shader [Monjori]</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. color: #ffffff;
  10. font-family:Monospace;
  11. font-size:13px;
  12. text-align:center;
  13. font-weight: bold;
  14. background-color: #000000;
  15. margin: 0px;
  16. overflow: hidden;
  17. }
  18. #info {
  19. position: absolute;
  20. top: 0px; width: 100%;
  21. padding: 5px;
  22. }
  23. a {
  24. color: #ffffff;
  25. }
  26. #oldie a { color:#da0 }
  27. </style>
  28. </head>
  29. <body>
  30. <div id="container"></div>
  31. <div id="info"><a href="http://threejs.org" target="_blank">three.js</a> - shader demo. featuring <a href="http://www.pouet.net/prod.php?which=52761" target="_blank">Monjori by Mic</a></div>
  32. <script src="../build/three.min.js"></script>
  33. <script src="js/Detector.js"></script>
  34. <script src="js/libs/stats.min.js"></script>
  35. <script id="vertexShader" type="x-shader/x-vertex">
  36. void main() {
  37. gl_Position = vec4( position, 1.0 );
  38. }
  39. </script>
  40. <script id="fragmentShader" type="x-shader/x-fragment">
  41. uniform vec2 resolution;
  42. uniform float time;
  43. void main() {
  44. vec2 p = -1.0 + 2.0 * gl_FragCoord.xy / resolution.xy;
  45. float a = time*40.0;
  46. float d,e,f,g=1.0/40.0,h,i,r,q;
  47. e=400.0*(p.x*0.5+0.5);
  48. f=400.0*(p.y*0.5+0.5);
  49. i=200.0+sin(e*g+a/150.0)*20.0;
  50. d=200.0+cos(f*g/2.0)*18.0+cos(e*g)*7.0;
  51. r=sqrt(pow(i-e,2.0)+pow(d-f,2.0));
  52. q=f/r;
  53. e=(r*cos(q))-a/2.0;f=(r*sin(q))-a/2.0;
  54. d=sin(e*g)*176.0+sin(e*g)*164.0+r;
  55. h=((f+d)+a/2.0)*g;
  56. i=cos(h+r*p.x/1.3)*(e+e+a)+cos(q*g*6.0)*(r+h/3.0);
  57. h=sin(f*g)*144.0-sin(e*g)*212.0*p.x;
  58. h=(h+(f-e)*q+sin(r-(a+h)/7.0)*10.0+i/4.0)*g;
  59. i+=cos(h*2.3*sin(a/350.0-q))*184.0*sin(q-(r*4.3+a/12.0)*g)+tan(r*g+h)*184.0*cos(r*g+h);
  60. i=mod(i/5.6,256.0)/64.0;
  61. if(i<0.0) i+=4.0;
  62. if(i>=2.0) i=4.0-i;
  63. d=r/350.0;
  64. d+=sin(d*d*8.0)*0.52;
  65. f=(sin(a*g)+1.0)/2.0;
  66. gl_FragColor=vec4(vec3(f*i/1.6,i/2.0+d/13.0,i)*d*p.x+vec3(i/1.3+d/8.0,i/2.0+d/18.0,i)*d*(1.0-p.x),1.0);
  67. }
  68. </script>
  69. <script>
  70. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  71. var container, stats;
  72. var camera, scene, renderer;
  73. var uniforms, material, mesh;
  74. var mouseX = 0, mouseY = 0,
  75. lat = 0, lon = 0, phy = 0, theta = 0;
  76. var windowHalfX = window.innerWidth / 2;
  77. var windowHalfY = window.innerHeight / 2;
  78. init();
  79. animate();
  80. function init() {
  81. container = document.getElementById( 'container' );
  82. camera = new THREE.Camera();
  83. camera.position.z = 1;
  84. scene = new THREE.Scene();
  85. uniforms = {
  86. time: { type: "f", value: 1.0 },
  87. resolution: { type: "v2", value: new THREE.Vector2() }
  88. };
  89. material = new THREE.ShaderMaterial( {
  90. uniforms: uniforms,
  91. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  92. fragmentShader: document.getElementById( 'fragmentShader' ).textContent
  93. } );
  94. mesh = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2 ), material );
  95. scene.add( mesh );
  96. renderer = new THREE.WebGLRenderer();
  97. container.appendChild( renderer.domElement );
  98. stats = new Stats();
  99. stats.domElement.style.position = 'absolute';
  100. stats.domElement.style.top = '0px';
  101. container.appendChild( stats.domElement );
  102. onWindowResize();
  103. window.addEventListener( 'resize', onWindowResize, false );
  104. }
  105. function onWindowResize( event ) {
  106. uniforms.resolution.value.x = window.innerWidth;
  107. uniforms.resolution.value.y = window.innerHeight;
  108. renderer.setSize( window.innerWidth, window.innerHeight );
  109. }
  110. //
  111. function animate() {
  112. requestAnimationFrame( animate );
  113. render();
  114. stats.update();
  115. }
  116. function render() {
  117. uniforms.time.value += 0.05;
  118. renderer.render( scene, camera );
  119. }
  120. </script>
  121. </body>
  122. </html>