webgl_lightprobe.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - light probe</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
  12. </div>
  13. <script type="module">
  14. import * as THREE from '../build/three.module.js';
  15. import { GUI } from './jsm/libs/dat.gui.module.js';
  16. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  17. import { LightProbeGenerator } from './jsm/lights/LightProbeGenerator.js';
  18. let mesh, renderer, scene, camera;
  19. let gui;
  20. let lightProbe;
  21. let directionalLight;
  22. // linear color space
  23. const API = {
  24. lightProbeIntensity: 1.0,
  25. directionalLightIntensity: 0.2,
  26. envMapIntensity: 1
  27. };
  28. init();
  29. function init() {
  30. // renderer
  31. renderer = new THREE.WebGLRenderer( { antialias: true } );
  32. renderer.setPixelRatio( window.devicePixelRatio );
  33. renderer.setSize( window.innerWidth, window.innerHeight );
  34. document.body.appendChild( renderer.domElement );
  35. // tone mapping
  36. renderer.toneMapping = THREE.NoToneMapping;
  37. renderer.outputEncoding = THREE.sRGBEncoding;
  38. // scene
  39. scene = new THREE.Scene();
  40. // camera
  41. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  42. camera.position.set( 0, 0, 30 );
  43. // controls
  44. const controls = new OrbitControls( camera, renderer.domElement );
  45. controls.addEventListener( 'change', render );
  46. controls.minDistance = 10;
  47. controls.maxDistance = 50;
  48. controls.enablePan = false;
  49. // probe
  50. lightProbe = new THREE.LightProbe();
  51. scene.add( lightProbe );
  52. // light
  53. directionalLight = new THREE.DirectionalLight( 0xffffff, API.directionalLightIntensity );
  54. directionalLight.position.set( 10, 10, 10 );
  55. scene.add( directionalLight );
  56. // envmap
  57. const genCubeUrls = function ( prefix, postfix ) {
  58. return [
  59. prefix + 'px' + postfix, prefix + 'nx' + postfix,
  60. prefix + 'py' + postfix, prefix + 'ny' + postfix,
  61. prefix + 'pz' + postfix, prefix + 'nz' + postfix
  62. ];
  63. };
  64. const urls = genCubeUrls( 'textures/cube/pisa/', '.png' );
  65. new THREE.CubeTextureLoader().load( urls, function ( cubeTexture ) {
  66. cubeTexture.encoding = THREE.sRGBEncoding;
  67. scene.background = cubeTexture;
  68. lightProbe.copy( LightProbeGenerator.fromCubeTexture( cubeTexture ) );
  69. const geometry = new THREE.SphereGeometry( 5, 64, 32 );
  70. //const geometry = new THREE.TorusKnotGeometry( 4, 1.5, 256, 32, 2, 3 );
  71. const material = new THREE.MeshStandardMaterial( {
  72. color: 0xffffff,
  73. metalness: 0,
  74. roughness: 0,
  75. envMap: cubeTexture,
  76. envMapIntensity: API.envMapIntensity,
  77. } );
  78. // mesh
  79. mesh = new THREE.Mesh( geometry, material );
  80. scene.add( mesh );
  81. render();
  82. } );
  83. // gui
  84. gui = new GUI();
  85. gui.width = 300;
  86. gui.domElement.style.userSelect = 'none';
  87. const fl = gui.addFolder( 'Intensity' );
  88. fl.add( API, 'lightProbeIntensity', 0, 1, 0.02 )
  89. .name( 'light probe' )
  90. .onChange( function () {
  91. lightProbe.intensity = API.lightProbeIntensity; render();
  92. } );
  93. fl.add( API, 'directionalLightIntensity', 0, 1, 0.02 )
  94. .name( 'directional light' )
  95. .onChange( function () {
  96. directionalLight.intensity = API.directionalLightIntensity; render();
  97. } );
  98. fl.add( API, 'envMapIntensity', 0, 1, 0.02 )
  99. .name( 'envMap' )
  100. .onChange( function () {
  101. mesh.material.envMapIntensity = API.envMapIntensity; render();
  102. } );
  103. fl.open();
  104. // listener
  105. window.addEventListener( 'resize', onWindowResize );
  106. }
  107. function onWindowResize() {
  108. renderer.setSize( window.innerWidth, window.innerHeight );
  109. camera.aspect = window.innerWidth / window.innerHeight;
  110. camera.updateProjectionMatrix();
  111. render();
  112. }
  113. function render() {
  114. renderer.render( scene, camera );
  115. }
  116. </script>
  117. </body>
  118. </html>