webgl_shader.html 4.0 KB

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