shader2.html 7.1 KB

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