shader2.html 6.9 KB

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