webgl_lightprobe.html 4.2 KB

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