webgl_shader2.html 7.0 KB

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