webgl_lightprobe_cubecamera.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - light probe from cubeCamera</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">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - light probe from cubeCamera
  12. </div>
  13. <script type="module">
  14. import * as THREE from '../build/three.module.js';
  15. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  16. import { LightProbeHelper } from './jsm/helpers/LightProbeHelper.js';
  17. import { LightProbeGenerator } from './jsm/lights/LightProbeGenerator.js';
  18. var renderer, scene, camera, cubeCamera;
  19. var lightProbe;
  20. init();
  21. function init() {
  22. // renderer
  23. renderer = new THREE.WebGLRenderer( { antialias: true } );
  24. renderer.setPixelRatio( window.devicePixelRatio );
  25. renderer.setSize( window.innerWidth, window.innerHeight );
  26. document.body.appendChild( renderer.domElement );
  27. renderer.outputEncoding = THREE.sRGBEncoding;
  28. // scene
  29. scene = new THREE.Scene();
  30. // camera
  31. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  32. camera.position.set( 0, 0, 30 );
  33. cubeCamera = new THREE.CubeCamera( 1, 1000, 256, { format: THREE.RGBAFormat, magFilter: THREE.LinearFilter, minFilter: THREE.LinearFilter } );
  34. //Since gamma is applied during rendering, the cubeCamera renderTarget texture encoding must be sRGBEncoding
  35. cubeCamera.renderTarget.texture.encoding = THREE.sRGBEncoding;
  36. // controls
  37. var controls = new OrbitControls( camera, renderer.domElement );
  38. controls.addEventListener( 'change', render );
  39. controls.minDistance = 10;
  40. controls.maxDistance = 50;
  41. controls.enablePan = false;
  42. // probe
  43. lightProbe = new THREE.LightProbe();
  44. scene.add( lightProbe );
  45. // envmap
  46. var genCubeUrls = function ( prefix, postfix ) {
  47. return [
  48. prefix + 'px' + postfix, prefix + 'nx' + postfix,
  49. prefix + 'py' + postfix, prefix + 'ny' + postfix,
  50. prefix + 'pz' + postfix, prefix + 'nz' + postfix
  51. ];
  52. };
  53. var urls = genCubeUrls( 'textures/cube/pisa/', '.png' );
  54. new THREE.CubeTextureLoader().load( urls, function ( cubeTexture ) {
  55. cubeTexture.encoding = THREE.sRGBEncoding;
  56. scene.background = cubeTexture;
  57. cubeCamera.update( renderer, scene );
  58. lightProbe.copy( LightProbeGenerator.fromCubeRenderTarget( renderer, cubeCamera.renderTarget ) );
  59. scene.add( new LightProbeHelper( lightProbe, 5 ) );
  60. render();
  61. } );
  62. // listener
  63. window.addEventListener( 'resize', onWindowResize, false );
  64. }
  65. function onWindowResize() {
  66. renderer.setSize( window.innerWidth, window.innerHeight );
  67. camera.aspect = window.innerWidth / window.innerHeight;
  68. camera.updateProjectionMatrix();
  69. render();
  70. }
  71. function render() {
  72. renderer.render( scene, camera );
  73. }
  74. </script>
  75. </body>
  76. </html>