webgl_shader_lava.html 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - shaders [lava]</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 lava shader by <a href="http://irrlicht.sourceforge.net/phpBB2/viewtopic.php?t=21057" target="_blank">TheGameMaker</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="fragmentShader" type="x-shader/x-fragment">
  36. uniform float time;
  37. uniform vec2 resolution;
  38. uniform float fogDensity;
  39. uniform vec3 fogColor;
  40. uniform sampler2D texture1;
  41. uniform sampler2D texture2;
  42. varying vec2 vUv;
  43. void main( void ) {
  44. vec2 position = -1.0 + 2.0 * vUv;
  45. vec4 noise = texture2D( texture1, vUv );
  46. vec2 T1 = vUv + vec2( 1.5, -1.5 ) * time *0.02;
  47. vec2 T2 = vUv + vec2( -0.5, 2.0 ) * time * 0.01;
  48. T1.x += noise.x * 2.0;
  49. T1.y += noise.y * 2.0;
  50. T2.x -= noise.y * 0.2;
  51. T2.y += noise.z * 0.2;
  52. float p = texture2D( texture1, T1 * 2.0 ).a;
  53. vec4 color = texture2D( texture2, T2 * 2.0 );
  54. vec4 temp = color * ( vec4( p, p, p, p ) * 2.0 ) + ( color * color - 0.1 );
  55. if( temp.r > 1.0 ){ temp.bg += clamp( temp.r - 2.0, 0.0, 100.0 ); }
  56. if( temp.g > 1.0 ){ temp.rb += temp.g - 1.0; }
  57. if( temp.b > 1.0 ){ temp.rg += temp.b - 1.0; }
  58. gl_FragColor = temp;
  59. float depth = gl_FragCoord.z / gl_FragCoord.w;
  60. const float LOG2 = 1.442695;
  61. float fogFactor = exp2( - fogDensity * fogDensity * depth * depth * LOG2 );
  62. fogFactor = 1.0 - clamp( fogFactor, 0.0, 1.0 );
  63. gl_FragColor = mix( gl_FragColor, vec4( fogColor, gl_FragColor.w ), fogFactor );
  64. }
  65. </script>
  66. <script id="vertexShader" type="x-shader/x-vertex">
  67. varying vec2 vUv;
  68. void main()
  69. {
  70. vUv = vec2( 3.0, 1.0 ) * uv;
  71. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  72. gl_Position = projectionMatrix * mvPosition;
  73. }
  74. </script>
  75. <script type="text/javascript">
  76. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  77. var container, stats;
  78. var start_time;
  79. var camera, scene, renderer;
  80. var uniforms, material, mesh;
  81. var mouseX = 0, mouseY = 0,
  82. lat = 0, lon = 0, phy = 0, theta = 0;
  83. var windowHalfX = window.innerWidth / 2;
  84. var windowHalfY = window.innerHeight / 2;
  85. var postprocessing = { enabled : true };
  86. init();
  87. animate();
  88. function init() {
  89. container = document.getElementById( 'container' );
  90. camera = new THREE.Camera( 35, windowHalfX / windowHalfY, 1, 3000 );
  91. camera.position.z = 4;
  92. scene = new THREE.Scene();
  93. start_time = new Date().getTime();
  94. uniforms = {
  95. fogDensity: { type: "f", value: 0.45 },
  96. fogColor: { type: "v3", value: new THREE.Vector3( 0, 0, 0 ) },
  97. time: { type: "f", value: 1.0 },
  98. resolution: { type: "v2", value: new THREE.Vector2() },
  99. texture1: { type: "t", value: 0, texture: ImageUtils.loadTexture( "textures/lava/cloud.png" ) },
  100. texture2: { type: "t", value: 1, texture: ImageUtils.loadTexture( "textures/lava/lavatile.jpg" ) }
  101. };
  102. uniforms.texture1.texture.wrapS = uniforms.texture1.texture.wrapT = THREE.Repeat;
  103. uniforms.texture2.texture.wrapS = uniforms.texture2.texture.wrapT = THREE.Repeat;
  104. var size = 0.65;
  105. material = new THREE.MeshShaderMaterial( {
  106. uniforms: uniforms,
  107. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  108. fragmentShader: document.getElementById( 'fragmentShader' ).textContent
  109. } );
  110. mesh = new THREE.Mesh( new Torus( size, 0.3, 30, 30 ), [ material /*, new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, wireframeLinewidth: 2 } ) */ ] );
  111. mesh.position.x = 0;
  112. mesh.position.y = 0;
  113. mesh.rotation.x = 0.3;
  114. scene.addObject( mesh );
  115. renderer = new THREE.WebGLRenderer( { antialias: true } );
  116. container.appendChild( renderer.domElement );
  117. initPostprocessing();
  118. renderer.autoClear = false;
  119. stats = new Stats();
  120. stats.domElement.style.position = 'absolute';
  121. stats.domElement.style.top = '0px';
  122. //container.appendChild( stats.domElement );
  123. onWindowResize();
  124. window.addEventListener( 'resize', onWindowResize, false );
  125. }
  126. function onWindowResize( event ) {
  127. uniforms.resolution.value.x = window.innerWidth;
  128. uniforms.resolution.value.y = window.innerHeight;
  129. renderer.setSize( window.innerWidth, window.innerHeight );
  130. camera.aspect = window.innerWidth / window.innerHeight;
  131. camera.updateProjectionMatrix();
  132. }
  133. function initPostprocessing() {
  134. postprocessing.scene = new THREE.Scene();
  135. postprocessing.camera = new THREE.Camera();
  136. postprocessing.camera.projectionMatrix = THREE.Matrix4.makeOrtho( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, -10000, 10000 );
  137. postprocessing.camera.position.z = 100;
  138. var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter };
  139. postprocessing.rtTexture1 = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, pars );
  140. postprocessing.rtTexture2 = new THREE.WebGLRenderTarget( 512, 512, pars );
  141. postprocessing.rtTexture3 = new THREE.WebGLRenderTarget( 512, 512, pars );
  142. var screen_shader = ShaderUtils.lib["screen"];
  143. var screen_uniforms = Uniforms.clone( screen_shader.uniforms );
  144. screen_uniforms["tDiffuse"].texture = postprocessing.rtTexture1;
  145. screen_uniforms["opacity"].value = 1.0;
  146. postprocessing.materialScreen = new THREE.MeshShaderMaterial( {
  147. uniforms: screen_uniforms,
  148. vertexShader: screen_shader.vertexShader,
  149. fragmentShader: screen_shader.fragmentShader,
  150. blending: THREE.AdditiveBlending,
  151. transparent: true
  152. } );
  153. var convolution_shader = ShaderUtils.lib["convolution"];
  154. var convolution_uniforms = Uniforms.clone( convolution_shader.uniforms );
  155. postprocessing.blurx = new THREE.Vector2( 0.001953125, 0.0 ),
  156. postprocessing.blury = new THREE.Vector2( 0.0, 0.001953125 );
  157. convolution_uniforms["tDiffuse"].texture = postprocessing.rtTexture1;
  158. convolution_uniforms["uImageIncrement"].value = postprocessing.blurx;
  159. convolution_uniforms["cKernel"].value = ShaderUtils.buildKernel( 4.0 );
  160. postprocessing.materialConvolution = new THREE.MeshShaderMaterial( {
  161. uniforms: convolution_uniforms,
  162. vertexShader: "#define KERNEL_SIZE 25.0\n" + convolution_shader.vertexShader,
  163. fragmentShader: "#define KERNEL_SIZE 25\n" + convolution_shader.fragmentShader
  164. } );
  165. var film_shader = ShaderUtils.lib["film"];
  166. var film_uniforms = Uniforms.clone( film_shader.uniforms );
  167. film_uniforms["tDiffuse"].texture = postprocessing.rtTexture1;
  168. postprocessing.materialFilm = new THREE.MeshShaderMaterial( { uniforms: film_uniforms, vertexShader: film_shader.vertexShader, fragmentShader: film_shader.fragmentShader } );
  169. postprocessing.materialFilm.uniforms.grayscale.value = 0;
  170. postprocessing.materialFilm.uniforms.nIntensity.value = 0.35;
  171. postprocessing.materialFilm.uniforms.sIntensity.value = 0.95;
  172. postprocessing.materialFilm.uniforms.sCount.value = 2048;
  173. postprocessing.quad = new THREE.Mesh( new Plane( window.innerWidth, window.innerHeight ), postprocessing.materialConvolution );
  174. postprocessing.quad.position.z = -500;
  175. postprocessing.scene.addObject( postprocessing.quad );
  176. }
  177. //
  178. function animate() {
  179. requestAnimationFrame( animate );
  180. render();
  181. stats.update();
  182. }
  183. function render() {
  184. uniforms.time.value += 0.02;
  185. mesh.rotation.y += 0.00125;
  186. mesh.rotation.x += 0.005;
  187. if ( postprocessing.enabled ) {
  188. renderer.clear();
  189. // Render scene into texture
  190. renderer.render( scene, camera, postprocessing.rtTexture1, true );
  191. // Render quad with blured scene into texture (convolution pass 1)
  192. postprocessing.quad.materials = [ postprocessing.materialConvolution ];
  193. postprocessing.materialConvolution.uniforms.tDiffuse.texture = postprocessing.rtTexture1;
  194. postprocessing.materialConvolution.uniforms.uImageIncrement.value = postprocessing.blurx;
  195. renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTexture2, true );
  196. // Render quad with blured scene into texture (convolution pass 2)
  197. postprocessing.materialConvolution.uniforms.tDiffuse.texture = postprocessing.rtTexture2;
  198. postprocessing.materialConvolution.uniforms.uImageIncrement.value = postprocessing.blury;
  199. renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTexture3, true );
  200. // Render original scene with superimposed blur to texture
  201. postprocessing.quad.materials = [ postprocessing.materialScreen ];
  202. postprocessing.materialScreen.uniforms.tDiffuse.texture = postprocessing.rtTexture3;
  203. postprocessing.materialScreen.uniforms.opacity.value = 1.25;
  204. renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTexture1, false );
  205. // Render to screen
  206. postprocessing.materialFilm.uniforms.time.value += 0.01;
  207. postprocessing.quad.materials = [ postprocessing.materialFilm ];
  208. postprocessing.materialScreen.uniforms.tDiffuse.texture = postprocessing.rtTexture1;
  209. renderer.render( postprocessing.scene, postprocessing.camera );
  210. } else {
  211. renderer.clear();
  212. renderer.render( scene, camera );
  213. }
  214. }
  215. </script>
  216. </body>
  217. </html>