123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - materials - video</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <div id="overlay">
- <div>
- <button id="startButton">Click to Play</button>
- <p>Video playback with audio requires user interaction.</p>
- </div>
- </div>
- <div id="container"></div>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl video demo<br/>
- playing <a href="http://durian.blender.org/" target="_blank" rel="noopener">sintel</a> trailer
- </div>
- <video id="video" loop crossOrigin="anonymous" playsinline style="display:none">
- <source src="textures/sintel.ogv" type='video/ogg; codecs="theora, vorbis"'>
- <source src="textures/sintel.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
- </video>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
- import { RenderPass } from './jsm/postprocessing/RenderPass.js';
- import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
- import { BloomPass } from './jsm/postprocessing/BloomPass.js';
- import { CopyShader } from './jsm/shaders/CopyShader.js';
- var container;
- var camera, scene, renderer;
- var video, texture, material, mesh;
- var composer;
- var mouseX = 0;
- var mouseY = 0;
- var windowHalfX = window.innerWidth / 2;
- var windowHalfY = window.innerHeight / 2;
- var cube_count,
- meshes = [],
- materials = [],
- xgrid = 20,
- ygrid = 10;
- var startButton = document.getElementById( 'startButton' );
- startButton.addEventListener( 'click', function () {
- init();
- animate();
- }, false );
- function init() {
- var overlay = document.getElementById( 'overlay' );
- overlay.remove();
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
- camera.position.z = 500;
- scene = new THREE.Scene();
- var light = new THREE.DirectionalLight( 0xffffff );
- light.position.set( 0.5, 1, 1 ).normalize();
- scene.add( light );
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- container.appendChild( renderer.domElement );
- video = document.getElementById( 'video' );
- video.play();
- texture = new THREE.VideoTexture( video );
- //
- var i, j, ux, uy, ox, oy,
- geometry,
- xsize, ysize;
- ux = 1 / xgrid;
- uy = 1 / ygrid;
- xsize = 480 / xgrid;
- ysize = 204 / ygrid;
- var parameters = { color: 0xffffff, map: texture };
- cube_count = 0;
- for ( i = 0; i < xgrid; i ++ )
- for ( j = 0; j < ygrid; j ++ ) {
- ox = i;
- oy = j;
- geometry = new THREE.BoxBufferGeometry( xsize, ysize, xsize );
- change_uvs( geometry, ux, uy, ox, oy );
- materials[ cube_count ] = new THREE.MeshLambertMaterial( parameters );
- material = materials[ cube_count ];
- material.hue = i / xgrid;
- material.saturation = 1 - j / ygrid;
- material.color.setHSL( material.hue, material.saturation, 0.5 );
- mesh = new THREE.Mesh( geometry, material );
- mesh.position.x = ( i - xgrid / 2 ) * xsize;
- mesh.position.y = ( j - ygrid / 2 ) * ysize;
- mesh.position.z = 0;
- mesh.scale.x = mesh.scale.y = mesh.scale.z = 1;
- scene.add( mesh );
- mesh.dx = 0.001 * ( 0.5 - Math.random() );
- mesh.dy = 0.001 * ( 0.5 - Math.random() );
- meshes[ cube_count ] = mesh;
- cube_count += 1;
- }
- renderer.autoClear = false;
- document.addEventListener( 'mousemove', onDocumentMouseMove, false );
- // postprocessing
- var renderModel = new RenderPass( scene, camera );
- var effectBloom = new BloomPass( 1.3 );
- var effectCopy = new ShaderPass( CopyShader );
- composer = new EffectComposer( renderer );
- composer.addPass( renderModel );
- composer.addPass( effectBloom );
- composer.addPass( effectCopy );
- //
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- windowHalfX = window.innerWidth / 2;
- windowHalfY = window.innerHeight / 2;
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- composer.setSize( window.innerWidth, window.innerHeight );
- }
- function change_uvs( geometry, unitx, unity, offsetx, offsety ) {
- var uvs = geometry.attributes.uv.array;
- for ( var i = 0; i < uvs.length; i += 2 ) {
- uvs[ i ] = ( uvs[ i ] + offsetx ) * unitx;
- uvs[ i + 1 ] = ( uvs[ i + 1 ] + offsety ) * unity;
- }
- }
- function onDocumentMouseMove( event ) {
- mouseX = ( event.clientX - windowHalfX );
- mouseY = ( event.clientY - windowHalfY ) * 0.3;
- }
- //
- function animate() {
- requestAnimationFrame( animate );
- render();
- }
- var h, counter = 1;
- function render() {
- var time = Date.now() * 0.00005;
- camera.position.x += ( mouseX - camera.position.x ) * 0.05;
- camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
- camera.lookAt( scene.position );
- for ( var i = 0; i < cube_count; i ++ ) {
- material = materials[ i ];
- h = ( 360 * ( material.hue + time ) % 360 ) / 360;
- material.color.setHSL( h, material.saturation, 0.5 );
- }
- if ( counter % 1000 > 200 ) {
- for ( var i = 0; i < cube_count; i ++ ) {
- mesh = meshes[ i ];
- mesh.rotation.x += 10 * mesh.dx;
- mesh.rotation.y += 10 * mesh.dy;
- mesh.position.x += 200 * mesh.dx;
- mesh.position.y += 200 * mesh.dy;
- mesh.position.z += 400 * mesh.dx;
- }
- }
- if ( counter % 1000 === 0 ) {
- for ( var i = 0; i < cube_count; i ++ ) {
- mesh = meshes[ i ];
- mesh.dx *= - 1;
- mesh.dy *= - 1;
- }
- }
- counter ++;
- renderer.clear();
- composer.render();
- }
- </script>
- </body>
- </html>
|