webgl_lightprobe.html 4.3 KB

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