webgl_lightprobe.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. // 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. cubeTexture.colorSpace = THREE.SRGBColorSpace;
  77. scene.background = cubeTexture;
  78. lightProbe.copy( LightProbeGenerator.fromCubeTexture( cubeTexture ) );
  79. const geometry = new THREE.SphereGeometry( 5, 64, 32 );
  80. //const geometry = new THREE.TorusKnotGeometry( 4, 1.5, 256, 32, 2, 3 );
  81. const material = new THREE.MeshStandardMaterial( {
  82. color: 0xffffff,
  83. metalness: 0,
  84. roughness: 0,
  85. envMap: cubeTexture,
  86. envMapIntensity: API.envMapIntensity,
  87. } );
  88. // mesh
  89. mesh = new THREE.Mesh( geometry, material );
  90. scene.add( mesh );
  91. render();
  92. } );
  93. // gui
  94. gui = new GUI( { title: 'Intensity' } );
  95. gui.add( API, 'lightProbeIntensity', 0, 1, 0.02 )
  96. .name( 'light probe' )
  97. .onChange( function () {
  98. lightProbe.intensity = API.lightProbeIntensity; render();
  99. } );
  100. gui.add( API, 'directionalLightIntensity', 0, 1, 0.02 )
  101. .name( 'directional light' )
  102. .onChange( function () {
  103. directionalLight.intensity = API.directionalLightIntensity; render();
  104. } );
  105. gui.add( API, 'envMapIntensity', 0, 1, 0.02 )
  106. .name( 'envMap' )
  107. .onChange( function () {
  108. mesh.material.envMapIntensity = API.envMapIntensity; render();
  109. } );
  110. // listener
  111. window.addEventListener( 'resize', onWindowResize );
  112. }
  113. function onWindowResize() {
  114. renderer.setSize( window.innerWidth, window.innerHeight );
  115. camera.aspect = window.innerWidth / window.innerHeight;
  116. camera.updateProjectionMatrix();
  117. render();
  118. }
  119. function render() {
  120. renderer.render( scene, camera );
  121. }
  122. </script>
  123. </body>
  124. </html>