webgl_materials_cubemap_dynamic.html 5.6 KB

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