webgl_materials_cubemap_dynamic.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <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>
  11. <script src="../build/three.js"></script>
  12. <script src="js/loaders/EquirectangularToCubeGenerator.js"></script>
  13. <script>
  14. var camera, scene, renderer;
  15. var cube, sphere, torus, material, backgroundMesh;
  16. var count = 0, cubeCamera1, cubeCamera2;
  17. var onPointerDownPointerX, onPointerDownPointerY, onPointerDownLon, onPointerDownLat;
  18. var lon = 0, lat = 0;
  19. var phi = 0, theta = 0;
  20. var textureLoader = new THREE.TextureLoader();
  21. textureLoader.load( 'textures/2294472375_24a3b8ef46_o.jpg', function ( texture ) {
  22. texture.mapping = THREE.UVMapping;
  23. init( texture );
  24. animate();
  25. } );
  26. function init( texture ) {
  27. renderer = new THREE.WebGLRenderer( { antialias: true } );
  28. renderer.setPixelRatio( window.devicePixelRatio );
  29. renderer.setSize( window.innerWidth, window.innerHeight );
  30. scene = new THREE.Scene();
  31. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
  32. // background
  33. var options = {
  34. resolution: 1024,
  35. generateMipmaps: true,
  36. minFilter: THREE.LinearMipMapLinearFilter,
  37. magFilter: THREE.LinearFilter
  38. };
  39. scene.background = new THREE.CubemapGenerator( renderer ).fromEquirectangular( texture, options );
  40. //
  41. cubeCamera1 = new THREE.CubeCamera( 1, 1000, 256 );
  42. cubeCamera1.renderTarget.texture.generateMipmaps = true;
  43. cubeCamera1.renderTarget.texture.minFilter = THREE.LinearMipMapLinearFilter;
  44. scene.add( cubeCamera1 );
  45. cubeCamera2 = new THREE.CubeCamera( 1, 1000, 256 );
  46. cubeCamera2.renderTarget.texture.generateMipmaps = true;
  47. cubeCamera2.renderTarget.texture.minFilter = THREE.LinearMipMapLinearFilter;
  48. scene.add( cubeCamera2 );
  49. document.body.appendChild( renderer.domElement );
  50. //
  51. material = new THREE.MeshBasicMaterial( {
  52. envMap: cubeCamera2.renderTarget.texture
  53. } );
  54. sphere = new THREE.Mesh( new THREE.IcosahedronBufferGeometry( 20, 3 ), material );
  55. scene.add( sphere );
  56. cube = new THREE.Mesh( new THREE.BoxBufferGeometry( 20, 20, 20 ), material );
  57. scene.add( cube );
  58. torus = new THREE.Mesh( new THREE.TorusKnotBufferGeometry( 10, 5, 100, 25 ), material );
  59. scene.add( torus );
  60. //
  61. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  62. document.addEventListener( 'wheel', onDocumentMouseWheel, false );
  63. window.addEventListener( 'resize', onWindowResized, false );
  64. }
  65. function onWindowResized() {
  66. renderer.setSize( window.innerWidth, window.innerHeight );
  67. camera.aspect = window.innerWidth / window.innerHeight;
  68. camera.updateProjectionMatrix();
  69. }
  70. function onDocumentMouseDown( event ) {
  71. event.preventDefault();
  72. onPointerDownPointerX = event.clientX;
  73. onPointerDownPointerY = event.clientY;
  74. onPointerDownLon = lon;
  75. onPointerDownLat = lat;
  76. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  77. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  78. }
  79. function onDocumentMouseMove( event ) {
  80. lon = ( event.clientX - onPointerDownPointerX ) * 0.1 + onPointerDownLon;
  81. lat = ( event.clientY - onPointerDownPointerY ) * 0.1 + onPointerDownLat;
  82. }
  83. function onDocumentMouseUp() {
  84. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  85. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  86. }
  87. function onDocumentMouseWheel( event ) {
  88. var fov = camera.fov + event.deltaY * 0.05;
  89. camera.fov = THREE.Math.clamp( fov, 10, 75 );
  90. camera.updateProjectionMatrix();
  91. }
  92. function animate() {
  93. requestAnimationFrame( animate );
  94. render();
  95. }
  96. function render() {
  97. var time = Date.now();
  98. lon += .15;
  99. lat = Math.max( - 85, Math.min( 85, lat ) );
  100. phi = THREE.Math.degToRad( 90 - lat );
  101. theta = THREE.Math.degToRad( lon );
  102. cube.position.x = Math.cos( time * 0.001 ) * 30;
  103. cube.position.y = Math.sin( time * 0.001 ) * 30;
  104. cube.position.z = Math.sin( time * 0.001 ) * 30;
  105. cube.rotation.x += 0.02;
  106. cube.rotation.y += 0.03;
  107. torus.position.x = Math.cos( time * 0.001 + 10 ) * 30;
  108. torus.position.y = Math.sin( time * 0.001 + 10 ) * 30;
  109. torus.position.z = Math.sin( time * 0.001 + 10 ) * 30;
  110. torus.rotation.x += 0.02;
  111. torus.rotation.y += 0.03;
  112. camera.position.x = 100 * Math.sin( phi ) * Math.cos( theta );
  113. camera.position.y = 100 * Math.cos( phi );
  114. camera.position.z = 100 * Math.sin( phi ) * Math.sin( theta );
  115. camera.lookAt( scene.position );
  116. sphere.visible = false;
  117. // pingpong
  118. if ( count % 2 === 0 ) {
  119. material.envMap = cubeCamera1.renderTarget.texture;
  120. cubeCamera2.update( renderer, scene );
  121. } else {
  122. material.envMap = cubeCamera2.renderTarget.texture;
  123. cubeCamera1.update( renderer, scene );
  124. }
  125. count ++;
  126. sphere.visible = true;
  127. renderer.render( scene, camera );
  128. }
  129. </script>
  130. </body>
  131. </html>