webgl_lightprobe.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.2,
  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. renderer.outputEncoding = THREE.sRGBEncoding;
  49. // scene
  50. scene = new THREE.Scene();
  51. // camera
  52. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  53. camera.position.set( 0, 0, 30 );
  54. // controls
  55. const controls = new OrbitControls( camera, renderer.domElement );
  56. controls.addEventListener( 'change', render );
  57. controls.minDistance = 10;
  58. controls.maxDistance = 50;
  59. controls.enablePan = false;
  60. // probe
  61. lightProbe = new THREE.LightProbe();
  62. scene.add( lightProbe );
  63. // light
  64. directionalLight = new THREE.DirectionalLight( 0xffffff, API.directionalLightIntensity );
  65. directionalLight.position.set( 10, 10, 10 );
  66. scene.add( directionalLight );
  67. // envmap
  68. const genCubeUrls = function ( prefix, postfix ) {
  69. return [
  70. prefix + 'px' + postfix, prefix + 'nx' + postfix,
  71. prefix + 'py' + postfix, prefix + 'ny' + postfix,
  72. prefix + 'pz' + postfix, prefix + 'nz' + postfix
  73. ];
  74. };
  75. const urls = genCubeUrls( 'textures/cube/pisa/', '.png' );
  76. new THREE.CubeTextureLoader().load( urls, function ( cubeTexture ) {
  77. cubeTexture.encoding = THREE.sRGBEncoding;
  78. scene.background = cubeTexture;
  79. lightProbe.copy( LightProbeGenerator.fromCubeTexture( cubeTexture ) );
  80. const geometry = new THREE.SphereGeometry( 5, 64, 32 );
  81. //const geometry = new THREE.TorusKnotGeometry( 4, 1.5, 256, 32, 2, 3 );
  82. const material = new THREE.MeshStandardMaterial( {
  83. color: 0xffffff,
  84. metalness: 0,
  85. roughness: 0,
  86. envMap: cubeTexture,
  87. envMapIntensity: API.envMapIntensity,
  88. } );
  89. // mesh
  90. mesh = new THREE.Mesh( geometry, material );
  91. scene.add( mesh );
  92. render();
  93. } );
  94. // gui
  95. gui = new GUI( { title: 'Intensity' } );
  96. gui.add( API, 'lightProbeIntensity', 0, 1, 0.02 )
  97. .name( 'light probe' )
  98. .onChange( function () {
  99. lightProbe.intensity = API.lightProbeIntensity; render();
  100. } );
  101. gui.add( API, 'directionalLightIntensity', 0, 1, 0.02 )
  102. .name( 'directional light' )
  103. .onChange( function () {
  104. directionalLight.intensity = API.directionalLightIntensity; render();
  105. } );
  106. gui.add( API, 'envMapIntensity', 0, 1, 0.02 )
  107. .name( 'envMap' )
  108. .onChange( function () {
  109. mesh.material.envMapIntensity = API.envMapIntensity; render();
  110. } );
  111. // listener
  112. window.addEventListener( 'resize', onWindowResize );
  113. }
  114. function onWindowResize() {
  115. renderer.setSize( window.innerWidth, window.innerHeight );
  116. camera.aspect = window.innerWidth / window.innerHeight;
  117. camera.updateProjectionMatrix();
  118. render();
  119. }
  120. function render() {
  121. renderer.render( scene, camera );
  122. }
  123. </script>
  124. </body>
  125. </html>