webgl_shader2.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - shaders [custom]</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 material 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="fragment_shader4" type="x-shader/x-fragment">
  36. uniform float time;
  37. uniform vec2 resolution;
  38. varying vec2 vUv;
  39. void main( void ) {
  40. vec2 position = -1.0 + 2.0 * vUv;
  41. float red = abs( sin( position.x * position.y + time / 5.0 ) );
  42. float green = abs( sin( position.x * position.y + time / 4.0 ) );
  43. float blue = abs( sin( position.x * position.y + time / 3.0 ) );
  44. gl_FragColor = vec4( red, green, blue, 1.0 );
  45. }
  46. </script>
  47. <script id="fragment_shader3" type="x-shader/x-fragment">
  48. uniform float time;
  49. uniform vec2 resolution;
  50. varying vec2 vUv;
  51. void main( void ) {
  52. vec2 position = vUv;
  53. float color = 0.0;
  54. color += sin( position.x * cos( time / 15.0 ) * 80.0 ) + cos( position.y * cos( time / 15.0 ) * 10.0 );
  55. color += sin( position.y * sin( time / 10.0 ) * 40.0 ) + cos( position.x * sin( time / 25.0 ) * 40.0 );
  56. color += sin( position.x * sin( time / 5.0 ) * 10.0 ) + sin( position.y * sin( time / 35.0 ) * 80.0 );
  57. color *= sin( time / 10.0 ) * 0.5;
  58. gl_FragColor = vec4( vec3( color, color * 0.5, sin( color + time / 3.0 ) * 0.75 ), 1.0 );
  59. }
  60. </script>
  61. <script id="fragment_shader2" type="x-shader/x-fragment">
  62. uniform float time;
  63. uniform vec2 resolution;
  64. uniform sampler2D texture;
  65. varying vec2 vUv;
  66. void main( void ) {
  67. vec2 position = -1.0 + 2.0 * vUv;
  68. float a = atan( position.y, position.x );
  69. float r = sqrt( dot( position, position ) );
  70. vec2 uv;
  71. uv.x = cos( a ) / r;
  72. uv.y = sin( a ) / r;
  73. uv /= 10.0;
  74. uv += time * 0.05;
  75. vec3 color = texture2D( texture, uv ).rgb;
  76. gl_FragColor = vec4( color * r * 1.5, 1.0 );
  77. }
  78. </script>
  79. <script id="fragment_shader1" type="x-shader/x-fragment">
  80. #ifdef GL_ES
  81. precision highp float;
  82. #endif
  83. uniform vec2 resolution;
  84. uniform float time;
  85. varying vec2 vUv;
  86. void main(void)
  87. {
  88. vec2 p = -1.0 + 2.0 * vUv;
  89. float a = time*40.0;
  90. float d,e,f,g=1.0/40.0,h,i,r,q;
  91. e=400.0*(p.x*0.5+0.5);
  92. f=400.0*(p.y*0.5+0.5);
  93. i=200.0+sin(e*g+a/150.0)*20.0;
  94. d=200.0+cos(f*g/2.0)*18.0+cos(e*g)*7.0;
  95. r=sqrt(pow(i-e,2.0)+pow(d-f,2.0));
  96. q=f/r;
  97. e=(r*cos(q))-a/2.0;f=(r*sin(q))-a/2.0;
  98. d=sin(e*g)*176.0+sin(e*g)*164.0+r;
  99. h=((f+d)+a/2.0)*g;
  100. i=cos(h+r*p.x/1.3)*(e+e+a)+cos(q*g*6.0)*(r+h/3.0);
  101. h=sin(f*g)*144.0-sin(e*g)*212.0*p.x;
  102. h=(h+(f-e)*q+sin(r-(a+h)/7.0)*10.0+i/4.0)*g;
  103. 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);
  104. i=mod(i/5.6,256.0)/64.0;
  105. if(i<0.0) i+=4.0;
  106. if(i>=2.0) i=4.0-i;
  107. d=r/350.0;
  108. d+=sin(d*d*8.0)*0.52;
  109. f=(sin(a*g)+1.0)/2.0;
  110. 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);
  111. }
  112. </script>
  113. <script id="vertex_shader" type="x-shader/x-vertex">
  114. varying vec2 vUv;
  115. void main()
  116. {
  117. vUv = uv;
  118. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  119. gl_Position = projectionMatrix * mvPosition;
  120. }
  121. </script>
  122. <script type="text/javascript">
  123. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  124. var container, stats;
  125. var start_time;
  126. var camera, scene, renderer;
  127. var uniforms1, uniforms2, material1, material2, mesh, meshes = [];
  128. var mouseX = 0, mouseY = 0,
  129. lat = 0, lon = 0, phy = 0, theta = 0;
  130. var windowHalfX = window.innerWidth / 2;
  131. var windowHalfY = window.innerHeight / 2;
  132. init();
  133. animate();
  134. function init() {
  135. container = document.getElementById( 'container' );
  136. camera = new THREE.Camera( 40, windowHalfX / windowHalfY, 1, 3000 );
  137. camera.position.z = 4;
  138. scene = new THREE.Scene();
  139. start_time = new Date().getTime();
  140. uniforms1 = {
  141. time: { type: "f", value: 1.0 },
  142. resolution: { type: "v2", value: new THREE.Vector2() }
  143. };
  144. uniforms2 = {
  145. time: { type: "f", value: 1.0 },
  146. resolution: { type: "v2", value: new THREE.Vector2() },
  147. texture: { type: "t", value: 0, texture: ImageUtils.loadTexture( "textures/disturb.jpg" ) }
  148. };
  149. uniforms2.texture.texture.wrap_s = uniforms2.texture.texture.wrap_t = THREE.Repeat;
  150. var size = 0.75, mlib = [],
  151. params = [ [ 'fragment_shader1', uniforms1 ], [ 'fragment_shader2', uniforms2 ], [ 'fragment_shader3', uniforms1 ], [ 'fragment_shader4', uniforms1 ] ];
  152. for( var i = 0; i < params.length; i++ ) {
  153. material = new THREE.MeshShaderMaterial( {
  154. uniforms: params[ i ][ 1 ],
  155. vertex_shader: document.getElementById( 'vertex_shader' ).textContent,
  156. fragment_shader: document.getElementById( params[ i ][ 0 ] ).textContent
  157. } );
  158. mlib[ i ] = material;
  159. mesh = new THREE.Mesh( new Cube( size, size, size, 1, 1, [ mlib[ i ], mlib[ i ], mlib[ i ], mlib[ i ], mlib[ i ], mlib[ i ] ], false ), new THREE.MeshFaceMaterial() );
  160. mesh.position.x = i - (params.length - 1)/2;
  161. mesh.position.y = i % 2 - 0.5;
  162. scene.addObject( mesh );
  163. meshes[ i ] = mesh;
  164. }
  165. renderer = new THREE.WebGLRenderer();
  166. container.appendChild( renderer.domElement );
  167. stats = new Stats();
  168. stats.domElement.style.position = 'absolute';
  169. stats.domElement.style.top = '0px';
  170. container.appendChild( stats.domElement );
  171. onWindowResize();
  172. window.addEventListener( 'resize', onWindowResize, false );
  173. }
  174. function onWindowResize( event ) {
  175. uniforms1.resolution.value.x = window.innerWidth;
  176. uniforms1.resolution.value.y = window.innerHeight;
  177. uniforms2.resolution.value.x = window.innerWidth;
  178. uniforms2.resolution.value.y = window.innerHeight;
  179. renderer.setSize( window.innerWidth, window.innerHeight );
  180. }
  181. //
  182. function animate() {
  183. requestAnimationFrame( animate );
  184. render();
  185. stats.update();
  186. }
  187. function render() {
  188. uniforms1.time.value += 0.05;
  189. uniforms2.time.value = ( new Date().getTime() - start_time ) / 1000;
  190. for( var i = 0; i < meshes.length; ++i ) {
  191. meshes[ i ].rotation.y += 0.01 * ( i % 2 ? 1 : -1 );
  192. meshes[ i ].rotation.x += 0.01 * ( i % 2 ? -1 : 1 );
  193. }
  194. renderer.render( scene, camera );
  195. }
  196. </script>
  197. </body>
  198. </html>