webgl_materials_cubemap_dynamic.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - dynamic cube reflection</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. background-color: #000000;
  10. margin: 0px;
  11. overflow: hidden;
  12. }
  13. #info {
  14. position: absolute;
  15. top: 0px; width: 100%;
  16. color: #ffffff;
  17. padding: 5px;
  18. font-family:Monospace;
  19. font-size:13px;
  20. font-weight: bold;
  21. text-align:center;
  22. }
  23. a {
  24. color: #ffffff;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js webgl</a> - materials - dynamic cube reflection<br/>Photo by <a href="http://www.flickr.com/photos/jonragnarsson/2294472375/" target="_blank" rel="noopener">J&oacute;n Ragnarsson</a>.</div>
  30. <script src="../build/three.js"></script>
  31. <script>
  32. var camera, scene, renderer;
  33. var cube, sphere, torus, material, backgroundMesh;
  34. var count = 0, cubeCamera1, cubeCamera2;
  35. var onPointerDownPointerX, onPointerDownPointerY, onPointerDownLon, onPointerDownLat;
  36. var lon = 0, lat = 0;
  37. var phi = 0, theta = 0;
  38. var textureLoader = new THREE.TextureLoader();
  39. textureLoader.load( 'textures/2294472375_24a3b8ef46_o.jpg', function ( texture ) {
  40. texture.mapping = THREE.UVMapping;
  41. init( texture );
  42. animate();
  43. } );
  44. function init( texture ) {
  45. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
  46. scene = new THREE.Scene();
  47. backgroundMesh = new THREE.Mesh( new THREE.SphereBufferGeometry( 500, 32, 16 ), new THREE.MeshBasicMaterial( { map: texture } ) );
  48. backgroundMesh.geometry.scale( - 1, 1, 1 );
  49. scene.add( backgroundMesh );
  50. renderer = new THREE.WebGLRenderer( { antialias: true } );
  51. renderer.setPixelRatio( window.devicePixelRatio );
  52. renderer.setSize( window.innerWidth, window.innerHeight );
  53. cubeCamera1 = new THREE.CubeCamera( 1, 1000, 256 );
  54. cubeCamera1.renderTarget.texture.generateMipmaps = true;
  55. cubeCamera1.renderTarget.texture.minFilter = THREE.LinearMipMapLinearFilter;
  56. scene.add( cubeCamera1 );
  57. cubeCamera2 = new THREE.CubeCamera( 1, 1000, 256 );
  58. cubeCamera2.renderTarget.texture.generateMipmaps = true;
  59. cubeCamera2.renderTarget.texture.minFilter = THREE.LinearMipMapLinearFilter;
  60. scene.add( cubeCamera2 );
  61. document.body.appendChild( renderer.domElement );
  62. //
  63. material = new THREE.MeshBasicMaterial( {
  64. envMap: cubeCamera2.renderTarget.texture
  65. } );
  66. sphere = new THREE.Mesh( new THREE.IcosahedronBufferGeometry( 20, 3 ), material );
  67. scene.add( sphere );
  68. cube = new THREE.Mesh( new THREE.BoxBufferGeometry( 20, 20, 20 ), material );
  69. scene.add( cube );
  70. torus = new THREE.Mesh( new THREE.TorusKnotBufferGeometry( 10, 5, 100, 25 ), material );
  71. scene.add( torus );
  72. //
  73. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  74. document.addEventListener( 'wheel', onDocumentMouseWheel, false );
  75. window.addEventListener( 'resize', onWindowResized, false );
  76. }
  77. function onWindowResized() {
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. camera.aspect = window.innerWidth / window.innerHeight;
  80. camera.updateProjectionMatrix();
  81. }
  82. function onDocumentMouseDown( event ) {
  83. event.preventDefault();
  84. onPointerDownPointerX = event.clientX;
  85. onPointerDownPointerY = event.clientY;
  86. onPointerDownLon = lon;
  87. onPointerDownLat = lat;
  88. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  89. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  90. }
  91. function onDocumentMouseMove( event ) {
  92. lon = ( event.clientX - onPointerDownPointerX ) * 0.1 + onPointerDownLon;
  93. lat = ( event.clientY - onPointerDownPointerY ) * 0.1 + onPointerDownLat;
  94. }
  95. function onDocumentMouseUp() {
  96. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  97. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  98. }
  99. function onDocumentMouseWheel( event ) {
  100. var fov = camera.fov + event.deltaY * 0.05;
  101. camera.fov = THREE.Math.clamp( fov, 10, 75 );
  102. camera.updateProjectionMatrix();
  103. }
  104. function animate() {
  105. requestAnimationFrame( animate );
  106. render();
  107. }
  108. function render() {
  109. var time = Date.now();
  110. lon += .15;
  111. lat = Math.max( - 85, Math.min( 85, lat ) );
  112. phi = THREE.Math.degToRad( 90 - lat );
  113. theta = THREE.Math.degToRad( lon );
  114. cube.position.x = Math.cos( time * 0.001 ) * 30;
  115. cube.position.y = Math.sin( time * 0.001 ) * 30;
  116. cube.position.z = Math.sin( time * 0.001 ) * 30;
  117. cube.rotation.x += 0.02;
  118. cube.rotation.y += 0.03;
  119. torus.position.x = Math.cos( time * 0.001 + 10 ) * 30;
  120. torus.position.y = Math.sin( time * 0.001 + 10 ) * 30;
  121. torus.position.z = Math.sin( time * 0.001 + 10 ) * 30;
  122. torus.rotation.x += 0.02;
  123. torus.rotation.y += 0.03;
  124. camera.position.x = 100 * Math.sin( phi ) * Math.cos( theta );
  125. camera.position.y = 100 * Math.cos( phi );
  126. camera.position.z = 100 * Math.sin( phi ) * Math.sin( theta );
  127. camera.lookAt( scene.position );
  128. sphere.visible = false;
  129. // pingpong
  130. if ( count % 2 === 0 ) {
  131. material.envMap = cubeCamera1.renderTarget.texture;
  132. backgroundMesh.position.copy( cubeCamera2.position );
  133. cubeCamera2.update( renderer, scene );
  134. } else {
  135. material.envMap = cubeCamera2.renderTarget.texture;
  136. backgroundMesh.position.copy( cubeCamera1.position );
  137. cubeCamera1.update( renderer, scene );
  138. }
  139. count ++;
  140. sphere.visible = true;
  141. backgroundMesh.position.copy( camera.position );
  142. renderer.render( scene, camera );
  143. }
  144. </script>
  145. </body>
  146. </html>