webgl_materials_video.html 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - video</title>
  5. <meta charset="utf-8">
  6. <style type="text/css">
  7. body {
  8. background-color: #000;
  9. color: #fff;
  10. margin: 0px;
  11. overflow: hidden;
  12. font-family:Monospace;
  13. font-size:13px;
  14. text-align:center;
  15. font-weight: bold;
  16. text-align:center;
  17. }
  18. a {
  19. color:#0078ff;
  20. }
  21. #info {
  22. color:#fff;
  23. position: absolute;
  24. top: 0px; width: 100%;
  25. padding: 5px;
  26. z-index:100;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div id="info">
  32. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - webgl video demo. playing <a href="http://durian.blender.org/" target="_blank">sintel</a> trailer
  33. </div>
  34. <script type="text/javascript" src="../build/Three.js"></script>
  35. <script type="text/javascript" src="js/Detector.js"></script>
  36. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  37. <script type="text/javascript" src="js/Stats.js"></script>
  38. <video id="video" autoplay loop style="display:none">
  39. <source src="textures/sintel.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
  40. <source src="textures/sintel.ogv" type='video/ogg; codecs="theora, vorbis"'>
  41. </video>
  42. <script type="text/javascript">
  43. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  44. var container, stats;
  45. var camera, scene, renderer;
  46. var video, texture, material, mesh;
  47. var mouseX = 0;
  48. var mouseY = 0;
  49. var windowHalfX = window.innerWidth / 2;
  50. var windowHalfY = window.innerHeight / 2;
  51. var postprocessing = { enabled: true };
  52. var cube_count,
  53. meshes = [],
  54. materials = [],
  55. xgrid = 20,
  56. ygrid = 10;
  57. init();
  58. animate();
  59. function init() {
  60. container = document.createElement('div');
  61. document.body.appendChild( container );
  62. camera = new THREE.Camera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
  63. camera.position.z = 500;
  64. scene = new THREE.Scene();
  65. var light = new THREE.DirectionalLight( 0xffffff );
  66. light.position.set( 0.5, 1, 1 );
  67. light.position.normalize();
  68. scene.addLight( light );
  69. renderer = new THREE.WebGLRenderer( { antialias: false } );
  70. renderer.setSize( window.innerWidth, window.innerHeight );
  71. container.appendChild( renderer.domElement );
  72. video = document.getElementById( 'video' );
  73. //video.volume = 0;
  74. //video.muted = true;
  75. texture = new THREE.Texture( video );
  76. texture.minFilter = THREE.LinearFilter;
  77. texture.magFilter = THREE.LinearFilter;
  78. //
  79. var i, j, ux, uy, ox, oy,
  80. geometry,
  81. xsize, ysize;
  82. ux = 1 / xgrid;
  83. uy = 1 / ygrid;
  84. xsize = 480 / xgrid;
  85. ysize = 204 / ygrid;
  86. var parameters = { color: 0xffffff, map: texture },
  87. material_base = new THREE.MeshLambertMaterial( parameters );
  88. renderer.initMaterial( material_base, scene.lights, scene.fog );
  89. cube_count = 0;
  90. for ( i = 0; i < xgrid; i ++ )
  91. for ( j = 0; j < ygrid; j ++ ) {
  92. ox = i;
  93. oy = j;
  94. geometry = new THREE.Cube( xsize, ysize, xsize );
  95. change_uvs( geometry, ux, uy, ox, oy );
  96. materials[ cube_count ] = new THREE.MeshLambertMaterial( parameters );
  97. material = materials[ cube_count ];
  98. //material.program = material_base.program;
  99. material.hue = i/xgrid;
  100. material.saturation = j/ygrid;
  101. material.color.setHSV( material.hue, material.saturation, 1 );
  102. mesh = new THREE.Mesh( geometry, material );
  103. mesh.position.x = ( i - xgrid/2 ) * xsize;
  104. mesh.position.y = - ( j - ygrid/2 ) * ysize;
  105. mesh.position.z = 0;
  106. mesh.scale.x = mesh.scale.y = mesh.scale.z = 1;
  107. scene.addObject(mesh);
  108. mesh.dx = 0.001 * ( 0.5 - Math.random() );
  109. mesh.dy = 0.001 * ( 0.5 - Math.random() );
  110. meshes[ cube_count ] = mesh;
  111. cube_count += 1;
  112. }
  113. initPostprocessing();
  114. renderer.autoClear = false;
  115. stats = new Stats();
  116. stats.domElement.style.position = 'absolute';
  117. stats.domElement.style.top = '0px';
  118. //container.appendChild( stats.domElement );
  119. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  120. }
  121. function change_uvs( geometry, unitx, unity, offsetx, offsety ) {
  122. var i, j, uv;
  123. for ( i = 0; i < geometry.faceVertexUvs[ 0 ].length; i++ ) {
  124. uv = geometry.faceVertexUvs[ 0 ][ i ];
  125. for ( j = 0; j < uv.length; j++ ) {
  126. uv[j].u = ( uv[j].u + offsetx ) * unitx;
  127. uv[j].v = ( uv[j].v + offsety ) * unity;
  128. }
  129. }
  130. }
  131. function initPostprocessing() {
  132. postprocessing.scene = new THREE.Scene();
  133. postprocessing.camera = new THREE.Camera();
  134. postprocessing.camera.projectionMatrix = THREE.Matrix4.makeOrtho( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, -10000, 10000 );
  135. postprocessing.camera.position.z = 100;
  136. var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat };
  137. postprocessing.rtTexture1 = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, pars );
  138. postprocessing.rtTexture2 = new THREE.WebGLRenderTarget( 512, 512, pars );
  139. postprocessing.rtTexture3 = new THREE.WebGLRenderTarget( 512, 512, pars );
  140. var screen_shader = THREE.ShaderUtils.lib["screen"];
  141. var screen_uniforms = THREE.UniformsUtils.clone( screen_shader.uniforms );
  142. screen_uniforms["tDiffuse"].texture = postprocessing.rtTexture1;
  143. screen_uniforms["opacity"].value = 1.0;
  144. postprocessing.materialScreen = new THREE.MeshShaderMaterial( {
  145. uniforms: screen_uniforms,
  146. vertexShader: screen_shader.vertexShader,
  147. fragmentShader: screen_shader.fragmentShader,
  148. blending: THREE.AdditiveBlending,
  149. transparent: true
  150. } );
  151. var convolution_shader = THREE.ShaderUtils.lib["convolution"];
  152. var convolution_uniforms = THREE.UniformsUtils.clone( convolution_shader.uniforms );
  153. postprocessing.blurx = new THREE.Vector2( 0.001953125, 0.0 ),
  154. postprocessing.blury = new THREE.Vector2( 0.0, 0.001953125 );
  155. convolution_uniforms["tDiffuse"].texture = postprocessing.rtTexture1;
  156. convolution_uniforms["uImageIncrement"].value = postprocessing.blurx;
  157. convolution_uniforms["cKernel"].value = THREE.ShaderUtils.buildKernel( 4.0 );
  158. postprocessing.materialConvolution = new THREE.MeshShaderMaterial( {
  159. uniforms: convolution_uniforms,
  160. vertexShader: "#define KERNEL_SIZE 25.0\n" + convolution_shader.vertexShader,
  161. fragmentShader: "#define KERNEL_SIZE 25\n" + convolution_shader.fragmentShader
  162. } );
  163. postprocessing.quad = new THREE.Mesh( new THREE.Plane( window.innerWidth, window.innerHeight ), postprocessing.materialConvolution );
  164. postprocessing.quad.position.z = -500;
  165. postprocessing.scene.addObject( postprocessing.quad );
  166. }
  167. function onDocumentMouseMove(event) {
  168. mouseX = ( event.clientX - windowHalfX );
  169. mouseY = ( event.clientY - windowHalfY ) * 0.3;
  170. }
  171. //
  172. function animate() {
  173. requestAnimationFrame( animate );
  174. render();
  175. stats.update();
  176. }
  177. var h, counter = 1;
  178. function render() {
  179. var time = new Date().getTime() * 0.00005;
  180. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  181. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  182. if ( video.readyState === video.HAVE_ENOUGH_DATA ) {
  183. if ( texture ) texture.needsUpdate = true;
  184. }
  185. for( i = 0; i < cube_count; i++ ) {
  186. material = materials[i];
  187. h = ( 360 * ( material.hue + time ) % 360 ) / 360;
  188. material.color.setHSV( h, material.saturation, 1 );
  189. }
  190. if( counter % 1000 > 200 ) {
  191. for( i = 0; i < cube_count; i++ ) {
  192. mesh = meshes[i];
  193. mesh.rotation.x += 10 * mesh.dx;
  194. mesh.rotation.y += 10 * mesh.dy;
  195. mesh.position.x += 200 * mesh.dx;
  196. mesh.position.y += 200 * mesh.dy;
  197. mesh.position.z += 400 * mesh.dx;
  198. }
  199. }
  200. if( counter % 1000 == 0 ) {
  201. for( i = 0; i < cube_count; i++ ) {
  202. mesh = meshes[i];
  203. mesh.dx *= -1;
  204. mesh.dy *= -1;
  205. }
  206. }
  207. counter++;
  208. renderer.clear();
  209. if ( postprocessing.enabled ) {
  210. // Render scene into texture
  211. renderer.render( scene, camera, postprocessing.rtTexture1, true );
  212. // Render quad with blured scene into texture (convolution pass 1)
  213. postprocessing.quad.materials = [ postprocessing.materialConvolution ];
  214. postprocessing.materialConvolution.uniforms.tDiffuse.texture = postprocessing.rtTexture1;
  215. postprocessing.materialConvolution.uniforms.uImageIncrement.value = postprocessing.blurx;
  216. renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTexture2, true );
  217. // Render quad with blured scene into texture (convolution pass 2)
  218. postprocessing.materialConvolution.uniforms.tDiffuse.texture = postprocessing.rtTexture2;
  219. postprocessing.materialConvolution.uniforms.uImageIncrement.value = postprocessing.blury;
  220. renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTexture3, true );
  221. // Render original scene with superimposed blur to texture
  222. postprocessing.quad.materials = [ postprocessing.materialScreen ];
  223. postprocessing.materialScreen.uniforms.tDiffuse.texture = postprocessing.rtTexture3;
  224. postprocessing.materialScreen.uniforms.opacity.value = 1.3;
  225. renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTexture1, false );
  226. // Render to screen
  227. postprocessing.materialScreen.uniforms.tDiffuse.texture = postprocessing.rtTexture1;
  228. renderer.render( postprocessing.scene, postprocessing.camera );
  229. } else {
  230. renderer.render( scene, camera );
  231. }
  232. }
  233. </script>
  234. </body>
  235. </html>