webgl_materials_cubemap_dynamic2.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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">three.js webgl</a> - materials - dynamic cube reflection<br/>Photo by <a href="http://www.flickr.com/photos/jonragnarsson/2294472375/" target="_blank">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;
  34. var count = 0, cubeCamera1, cubeCamera2;
  35. var fov = 60,
  36. isUserInteracting = false,
  37. onMouseDownMouseX = 0, onMouseDownMouseY = 0,
  38. lon = 0, onMouseDownLon = 0,
  39. lat = 0, onMouseDownLat = 0,
  40. phi = 0, theta = 0;
  41. var textureLoader = new THREE.TextureLoader();
  42. textureLoader.load( 'textures/2294472375_24a3b8ef46_o.jpg', function ( texture ) {
  43. texture.mapping = THREE.UVMapping;
  44. init( texture );
  45. animate();
  46. } );
  47. function init( texture ) {
  48. camera = new THREE.PerspectiveCamera( fov, window.innerWidth / window.innerHeight, 1, 1000 );
  49. scene = new THREE.Scene();
  50. var mesh = new THREE.Mesh( new THREE.SphereGeometry( 500, 32, 16 ), new THREE.MeshBasicMaterial( { map: texture } ) );
  51. mesh.scale.x = -1;
  52. scene.add( mesh );
  53. renderer = new THREE.WebGLRenderer( { antialias: true } );
  54. renderer.setPixelRatio( window.devicePixelRatio );
  55. renderer.setSize( window.innerWidth, window.innerHeight );
  56. cubeCamera1 = new THREE.CubeCamera( 1, 1000, 256 );
  57. cubeCamera1.renderTarget.texture.minFilter = THREE.LinearMipMapLinearFilter;
  58. scene.add( cubeCamera1 );
  59. cubeCamera2 = new THREE.CubeCamera( 1, 1000, 256 );
  60. cubeCamera2.renderTarget.texture.minFilter = THREE.LinearMipMapLinearFilter;
  61. scene.add( cubeCamera2 );
  62. document.body.appendChild( renderer.domElement );
  63. //
  64. material = new THREE.MeshBasicMaterial( {
  65. envMap: cubeCamera2.renderTarget.texture
  66. } );
  67. sphere = new THREE.Mesh( new THREE.IcosahedronGeometry( 20, 3 ), material );
  68. scene.add( sphere );
  69. cube = new THREE.Mesh( new THREE.BoxGeometry( 20, 20, 20 ), material );
  70. scene.add( cube );
  71. torus = new THREE.Mesh( new THREE.TorusKnotGeometry( 10, 5, 100, 25 ), material );
  72. scene.add( torus );
  73. //
  74. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  75. document.addEventListener( 'wheel', onDocumentMouseWheel, false );
  76. window.addEventListener( 'resize', onWindowResized, false );
  77. onWindowResized( null );
  78. }
  79. function onWindowResized( event ) {
  80. renderer.setSize( window.innerWidth, window.innerHeight );
  81. camera.projectionMatrix.makePerspective( fov, window.innerWidth / window.innerHeight, 1, 1100 );
  82. }
  83. function onDocumentMouseDown( event ) {
  84. event.preventDefault();
  85. onPointerDownPointerX = event.clientX;
  86. onPointerDownPointerY = event.clientY;
  87. onPointerDownLon = lon;
  88. onPointerDownLat = lat;
  89. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  90. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  91. }
  92. function onDocumentMouseMove( event ) {
  93. lon = ( event.clientX - onPointerDownPointerX ) * 0.1 + onPointerDownLon;
  94. lat = ( event.clientY - onPointerDownPointerY ) * 0.1 + onPointerDownLat;
  95. }
  96. function onDocumentMouseUp( event ) {
  97. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  98. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  99. }
  100. function onDocumentMouseWheel( event ) {
  101. fov += event.deltaY * 0.05;
  102. camera.projectionMatrix.makePerspective( fov, window.innerWidth / window.innerHeight, 1, 1100 );
  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; // *cough*
  129. // pingpong
  130. if ( count % 2 === 0 ) {
  131. material.envMap = cubeCamera1.renderTarget.texture;
  132. cubeCamera2.updateCubeMap( renderer, scene );
  133. } else {
  134. material.envMap = cubeCamera2.renderTarget.texture;
  135. cubeCamera1.updateCubeMap( renderer, scene );
  136. }
  137. count ++;
  138. sphere.visible = true; // *cough*
  139. renderer.render( scene, camera );
  140. }
  141. </script>
  142. </body>
  143. </html>