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://github.com/mrdoob/three.js" 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, cubeCamera, scene, renderer;
  33. var cube, sphere, torus;
  34. var fov = 70,
  35. isUserInteracting = false,
  36. onMouseDownMouseX = 0, onMouseDownMouseY = 0,
  37. lon = 0, onMouseDownLon = 0,
  38. lat = 0, onMouseDownLat = 0,
  39. phi = 0, theta = 0;
  40. var texture = THREE.ImageUtils.loadTexture( 'textures/2294472375_24a3b8ef46_o.jpg', new THREE.UVMapping(), function () {
  41. init();
  42. animate();
  43. } );
  44. function init() {
  45. scene = new THREE.Scene();
  46. camera = new THREE.PerspectiveCamera( fov, window.innerWidth / window.innerHeight, 1, 1000 );
  47. camera.target = new THREE.Vector3( 0, 0, 0 );
  48. scene.add( camera );
  49. var mesh = new THREE.Mesh( new THREE.SphereGeometry( 500, 60, 40 ), new THREE.MeshBasicMaterial( { map: texture } ) );
  50. mesh.scale.x = -1;
  51. scene.add( mesh );
  52. renderer = new THREE.WebGLRenderer( { antialias: true } );
  53. renderer.setSize( window.innerWidth, window.innerHeight );
  54. cubeCamera = new THREE.CubeCamera( 1, 1000, 256 );
  55. cubeCamera.renderTarget.minFilter = THREE.LinearMipMapLinearFilter;
  56. scene.add( cubeCamera );
  57. document.body.appendChild( renderer.domElement );
  58. //
  59. var material = new THREE.MeshBasicMaterial( { envMap: cubeCamera.renderTarget } );
  60. sphere = new THREE.Mesh( new THREE.SphereGeometry( 20, 60, 40 ), material );
  61. scene.add( sphere );
  62. cube = new THREE.Mesh( new THREE.CubeGeometry( 20, 20, 20 ), material );
  63. scene.add( cube );
  64. torus = new THREE.Mesh( new THREE.TorusKnotGeometry( 20, 5, 100, 100 ), material );
  65. scene.add( torus );
  66. //
  67. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  68. document.addEventListener( 'mousewheel', onDocumentMouseWheel, false );
  69. document.addEventListener( 'DOMMouseScroll', onDocumentMouseWheel, false);
  70. window.addEventListener( 'resize', onWindowResized, false );
  71. onWindowResized( null );
  72. }
  73. function onWindowResized( event ) {
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. camera.projectionMatrix.makePerspective( fov, window.innerWidth / window.innerHeight, 1, 1100 );
  76. }
  77. function onDocumentMouseDown( event ) {
  78. event.preventDefault();
  79. onPointerDownPointerX = event.clientX;
  80. onPointerDownPointerY = event.clientY;
  81. onPointerDownLon = lon;
  82. onPointerDownLat = lat;
  83. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  84. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  85. }
  86. function onDocumentMouseMove( event ) {
  87. lon = ( event.clientX - onPointerDownPointerX ) * 0.1 + onPointerDownLon;
  88. lat = ( event.clientY - onPointerDownPointerY ) * 0.1 + onPointerDownLat;
  89. }
  90. function onDocumentMouseUp( event ) {
  91. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  92. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  93. }
  94. function onDocumentMouseWheel( event ) {
  95. // WebKit
  96. if ( event.wheelDeltaY ) {
  97. fov -= event.wheelDeltaY * 0.05;
  98. // Opera / Explorer 9
  99. } else if ( event.wheelDelta ) {
  100. fov -= event.wheelDelta * 0.05;
  101. // Firefox
  102. } else if ( event.detail ) {
  103. fov += event.detail * 1.0;
  104. }
  105. camera.projectionMatrix.makePerspective( fov, window.innerWidth / window.innerHeight, 1, 1100 );
  106. }
  107. function animate() {
  108. requestAnimationFrame( animate );
  109. render();
  110. }
  111. function render() {
  112. var time = Date.now();
  113. lon += .15;
  114. lat = Math.max( - 85, Math.min( 85, lat ) );
  115. phi = ( 90 - lat ) * Math.PI / 180;
  116. theta = lon * Math.PI / 180;
  117. sphere.position.x = Math.sin( time * 0.001 ) * 30;
  118. sphere.position.y = Math.sin( time * 0.0011 ) * 30;
  119. sphere.position.z = Math.sin( time * 0.0012 ) * 30;
  120. sphere.rotation.x += 0.02;
  121. sphere.rotation.y += 0.03;
  122. cube.position.x = Math.sin( time * 0.001 + 2 ) * 30;
  123. cube.position.y = Math.sin( time * 0.0011 + 2 ) * 30;
  124. cube.position.z = Math.sin( time * 0.0012 + 2 ) * 30;
  125. cube.rotation.x += 0.02;
  126. cube.rotation.y += 0.03;
  127. torus.position.x = Math.sin( time * 0.001 + 4 ) * 30;
  128. torus.position.y = Math.sin( time * 0.0011 + 4 ) * 30;
  129. torus.position.z = Math.sin( time * 0.0012 + 4 ) * 30;
  130. torus.rotation.x += 0.02;
  131. torus.rotation.y += 0.03;
  132. camera.position.x = 100 * Math.sin( phi ) * Math.cos( theta );
  133. camera.position.y = 100 * Math.cos( phi );
  134. camera.position.z = 100 * Math.sin( phi ) * Math.sin( theta );
  135. camera.lookAt( camera.target );
  136. sphere.visible = false; // *cough*
  137. cubeCamera.updateCubeMap( renderer, scene );
  138. sphere.visible = true; // *cough*
  139. renderer.render( scene, camera );
  140. }
  141. </script>
  142. </body>
  143. </html>