webgl_materials_cubemap_dynamic.html 5.4 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. <style>
  9. body {
  10. touch-action: none;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <div id="info"><a href="https://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>
  16. <script type="module">
  17. import * as THREE from '../build/three.module.js';
  18. let camera, scene, renderer;
  19. let cube, sphere, torus, material;
  20. let count = 0, cubeCamera1, cubeCamera2, cubeRenderTarget1, cubeRenderTarget2;
  21. let onPointerDownPointerX, onPointerDownPointerY, onPointerDownLon, onPointerDownLat;
  22. let lon = 0, lat = 0;
  23. let phi = 0, theta = 0;
  24. const textureLoader = new THREE.TextureLoader();
  25. textureLoader.load( 'textures/2294472375_24a3b8ef46_o.jpg', function ( texture ) {
  26. texture.encoding = THREE.sRGBEncoding;
  27. texture.mapping = THREE.EquirectangularReflectionMapping;
  28. init( texture );
  29. animate();
  30. } );
  31. function init( texture ) {
  32. renderer = new THREE.WebGLRenderer( { antialias: true } );
  33. renderer.setPixelRatio( window.devicePixelRatio );
  34. renderer.setSize( window.innerWidth, window.innerHeight );
  35. renderer.outputEncoding = THREE.sRGBEncoding;
  36. document.body.appendChild( renderer.domElement );
  37. scene = new THREE.Scene();
  38. scene.background = texture;
  39. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
  40. //
  41. cubeRenderTarget1 = new THREE.WebGLCubeRenderTarget( 256, {
  42. format: THREE.RGBFormat,
  43. generateMipmaps: true,
  44. minFilter: THREE.LinearMipmapLinearFilter,
  45. encoding: THREE.sRGBEncoding // temporary -- to prevent the material's shader from recompiling every frame
  46. } );
  47. cubeCamera1 = new THREE.CubeCamera( 1, 1000, cubeRenderTarget1 );
  48. cubeRenderTarget2 = new THREE.WebGLCubeRenderTarget( 256, {
  49. format: THREE.RGBFormat,
  50. generateMipmaps: true,
  51. minFilter: THREE.LinearMipmapLinearFilter,
  52. encoding: THREE.sRGBEncoding
  53. } );
  54. cubeCamera2 = new THREE.CubeCamera( 1, 1000, cubeRenderTarget2 );
  55. //
  56. material = new THREE.MeshBasicMaterial( {
  57. envMap: cubeRenderTarget2.texture,
  58. combine: THREE.MultiplyOperation,
  59. reflectivity: 1
  60. } );
  61. sphere = new THREE.Mesh( new THREE.IcosahedronGeometry( 20, 8 ), material );
  62. scene.add( sphere );
  63. cube = new THREE.Mesh( new THREE.BoxGeometry( 20, 20, 20 ), material );
  64. scene.add( cube );
  65. torus = new THREE.Mesh( new THREE.TorusKnotGeometry( 10, 5, 128, 16 ), material );
  66. scene.add( torus );
  67. //
  68. document.addEventListener( 'pointerdown', onPointerDown );
  69. document.addEventListener( 'wheel', onDocumentMouseWheel );
  70. window.addEventListener( 'resize', onWindowResized );
  71. }
  72. function onWindowResized() {
  73. renderer.setSize( window.innerWidth, window.innerHeight );
  74. camera.aspect = window.innerWidth / window.innerHeight;
  75. camera.updateProjectionMatrix();
  76. }
  77. function onPointerDown( event ) {
  78. event.preventDefault();
  79. onPointerDownPointerX = event.clientX;
  80. onPointerDownPointerY = event.clientY;
  81. onPointerDownLon = lon;
  82. onPointerDownLat = lat;
  83. document.addEventListener( 'pointermove', onPointerMove );
  84. document.addEventListener( 'pointerup', onPointerUp );
  85. }
  86. function onPointerMove( event ) {
  87. lon = ( event.clientX - onPointerDownPointerX ) * 0.1 + onPointerDownLon;
  88. lat = ( event.clientY - onPointerDownPointerY ) * 0.1 + onPointerDownLat;
  89. }
  90. function onPointerUp() {
  91. document.removeEventListener( 'pointermove', onPointerMove );
  92. document.removeEventListener( 'pointerup', onPointerUp );
  93. }
  94. function onDocumentMouseWheel( event ) {
  95. const fov = camera.fov + event.deltaY * 0.05;
  96. camera.fov = THREE.MathUtils.clamp( fov, 10, 75 );
  97. camera.updateProjectionMatrix();
  98. }
  99. function animate() {
  100. requestAnimationFrame( animate );
  101. render();
  102. }
  103. function render() {
  104. const time = Date.now();
  105. lon += .15;
  106. lat = Math.max( - 85, Math.min( 85, lat ) );
  107. phi = THREE.MathUtils.degToRad( 90 - lat );
  108. theta = THREE.MathUtils.degToRad( lon );
  109. cube.position.x = Math.cos( time * 0.001 ) * 30;
  110. cube.position.y = Math.sin( time * 0.001 ) * 30;
  111. cube.position.z = Math.sin( time * 0.001 ) * 30;
  112. cube.rotation.x += 0.02;
  113. cube.rotation.y += 0.03;
  114. torus.position.x = Math.cos( time * 0.001 + 10 ) * 30;
  115. torus.position.y = Math.sin( time * 0.001 + 10 ) * 30;
  116. torus.position.z = Math.sin( time * 0.001 + 10 ) * 30;
  117. torus.rotation.x += 0.02;
  118. torus.rotation.y += 0.03;
  119. camera.position.x = 100 * Math.sin( phi ) * Math.cos( theta );
  120. camera.position.y = 100 * Math.cos( phi );
  121. camera.position.z = 100 * Math.sin( phi ) * Math.sin( theta );
  122. camera.lookAt( scene.position );
  123. // pingpong
  124. if ( count % 2 === 0 ) {
  125. cubeCamera1.update( renderer, scene );
  126. material.envMap = cubeRenderTarget1.texture;
  127. } else {
  128. cubeCamera2.update( renderer, scene );
  129. material.envMap = cubeRenderTarget2.texture;
  130. }
  131. count ++;
  132. renderer.render( scene, camera );
  133. }
  134. </script>
  135. </body>
  136. </html>