123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - materials - dynamic cube reflection</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <style>
- body {
- background-color: #000000;
- margin: 0px;
- overflow: hidden;
- }
- #info {
- position: absolute;
- top: 0px; width: 100%;
- color: #ffffff;
- padding: 5px;
- font-family:Monospace;
- font-size:13px;
- font-weight: bold;
- text-align:center;
- }
- a {
- color: #ffffff;
- }
- </style>
- </head>
- <body>
- <div id="info"><a href="http://threejs.org" target="_blank">three.js webgl</a> - materials - dynamic cube reflection<br/>Photo by <a href="http://www.flickr.com/photos/jonragnarsson/2294472375/" target="_blank">Jón Ragnarsson</a>.</div>
- <script src="../build/three.js"></script>
- <script>
- var camera, scene, renderer;
- var cube, sphere, torus, material;
- var count = 0, cubeCamera1, cubeCamera2;
- var fov = 60,
- isUserInteracting = false,
- onMouseDownMouseX = 0, onMouseDownMouseY = 0,
- lon = 0, onMouseDownLon = 0,
- lat = 0, onMouseDownLat = 0,
- phi = 0, theta = 0;
- var textureLoader = new THREE.TextureLoader();
- textureLoader.load( 'textures/2294472375_24a3b8ef46_o.jpg', function ( texture ) {
- texture.mapping = THREE.UVMapping;
- init( texture );
- animate();
- } );
- function init( texture ) {
- camera = new THREE.PerspectiveCamera( fov, window.innerWidth / window.innerHeight, 1, 1000 );
- scene = new THREE.Scene();
- var mesh = new THREE.Mesh( new THREE.SphereGeometry( 500, 32, 16 ), new THREE.MeshBasicMaterial( { map: texture } ) );
- mesh.scale.x = -1;
- scene.add( mesh );
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- cubeCamera1 = new THREE.CubeCamera( 1, 1000, 256 );
- cubeCamera1.renderTarget.texture.minFilter = THREE.LinearMipMapLinearFilter;
- scene.add( cubeCamera1 );
- cubeCamera2 = new THREE.CubeCamera( 1, 1000, 256 );
- cubeCamera2.renderTarget.texture.minFilter = THREE.LinearMipMapLinearFilter;
- scene.add( cubeCamera2 );
- document.body.appendChild( renderer.domElement );
- //
- material = new THREE.MeshBasicMaterial( {
- envMap: cubeCamera2.renderTarget.texture
- } );
- sphere = new THREE.Mesh( new THREE.IcosahedronGeometry( 20, 3 ), material );
- scene.add( sphere );
- cube = new THREE.Mesh( new THREE.BoxGeometry( 20, 20, 20 ), material );
- scene.add( cube );
- torus = new THREE.Mesh( new THREE.TorusKnotGeometry( 10, 5, 100, 25 ), material );
- scene.add( torus );
- //
- document.addEventListener( 'mousedown', onDocumentMouseDown, false );
- document.addEventListener( 'wheel', onDocumentMouseWheel, false );
- window.addEventListener( 'resize', onWindowResized, false );
- onWindowResized( null );
- }
- function onWindowResized( event ) {
- renderer.setSize( window.innerWidth, window.innerHeight );
- camera.projectionMatrix.makePerspective( fov, window.innerWidth / window.innerHeight, 1, 1100 );
- }
- function onDocumentMouseDown( event ) {
- event.preventDefault();
- onPointerDownPointerX = event.clientX;
- onPointerDownPointerY = event.clientY;
- onPointerDownLon = lon;
- onPointerDownLat = lat;
- document.addEventListener( 'mousemove', onDocumentMouseMove, false );
- document.addEventListener( 'mouseup', onDocumentMouseUp, false );
- }
- function onDocumentMouseMove( event ) {
- lon = ( event.clientX - onPointerDownPointerX ) * 0.1 + onPointerDownLon;
- lat = ( event.clientY - onPointerDownPointerY ) * 0.1 + onPointerDownLat;
- }
- function onDocumentMouseUp( event ) {
- document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
- document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
- }
- function onDocumentMouseWheel( event ) {
- fov += event.deltaY * 0.05;
- camera.projectionMatrix.makePerspective( fov, window.innerWidth / window.innerHeight, 1, 1100 );
- }
- function animate() {
- requestAnimationFrame( animate );
- render();
- }
- function render() {
- var time = Date.now();
- lon += .15;
- lat = Math.max( - 85, Math.min( 85, lat ) );
- phi = THREE.Math.degToRad( 90 - lat );
- theta = THREE.Math.degToRad( lon );
- cube.position.x = Math.cos( time * 0.001 ) * 30;
- cube.position.y = Math.sin( time * 0.001 ) * 30;
- cube.position.z = Math.sin( time * 0.001 ) * 30;
- cube.rotation.x += 0.02;
- cube.rotation.y += 0.03;
- torus.position.x = Math.cos( time * 0.001 + 10 ) * 30;
- torus.position.y = Math.sin( time * 0.001 + 10 ) * 30;
- torus.position.z = Math.sin( time * 0.001 + 10 ) * 30;
- torus.rotation.x += 0.02;
- torus.rotation.y += 0.03;
- camera.position.x = 100 * Math.sin( phi ) * Math.cos( theta );
- camera.position.y = 100 * Math.cos( phi );
- camera.position.z = 100 * Math.sin( phi ) * Math.sin( theta );
- camera.lookAt( scene.position );
- sphere.visible = false; // *cough*
- // pingpong
- if ( count % 2 === 0 ) {
- material.envMap = cubeCamera1.renderTarget.texture;
- cubeCamera2.updateCubeMap( renderer, scene );
- } else {
- material.envMap = cubeCamera2.renderTarget.texture;
- cubeCamera1.updateCubeMap( renderer, scene );
- }
- count ++;
- sphere.visible = true; // *cough*
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|