webgl_lightprobe.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - lights - 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. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #000;
  11. color: #fff;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. a {
  16. color: #ffa;
  17. font-weight: bold;
  18. }
  19. #info {
  20. color: #fff;
  21. position: absolute;
  22. top: 10px;
  23. width: 100%;
  24. text-align: center;
  25. z-index: 0; /* to not conflict with dat.gui */
  26. display:block;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div id="info">
  32. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl lights - light probe
  33. </div>
  34. <script src="../build/three.js"></script>
  35. <script src="js/controls/OrbitControls.js"></script>
  36. <script src="js/libs/dat.gui.min.js"></script>
  37. <script src="js/lights/LightProbeGenerator.js"></script>
  38. <script src="js/WebGL.js"></script>
  39. <script>
  40. if ( WEBGL.isWebGLAvailable() === false ) {
  41. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  42. }
  43. var mesh, renderer, scene, camera;
  44. var gui;
  45. var lightProbe;
  46. var directionalLight;
  47. // linear color space
  48. var API = {
  49. lightProbeIntensity: 1.0,
  50. directionalLightIntensity: 0.2,
  51. envMapIntensity: 1
  52. };
  53. init();
  54. function init() {
  55. // renderer
  56. renderer = new THREE.WebGLRenderer( { antialias: true } );
  57. renderer.setPixelRatio( window.devicePixelRatio );
  58. renderer.setSize( window.innerWidth, window.innerHeight );
  59. document.body.appendChild( renderer.domElement );
  60. // tone mapping
  61. //renderer.toneMapping = THREE.LinearToneMapping;
  62. //renderer.toneMappingExposure = API.exposure;
  63. // gamma
  64. renderer.gammaOutput = true;
  65. renderer.gammaFactor = 2.2; // approximate sRGB
  66. // scene
  67. scene = new THREE.Scene();
  68. // camera
  69. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  70. camera.position.set( 0, 0, 30 );
  71. // controls
  72. var controls = new THREE.OrbitControls( camera, renderer.domElement );
  73. controls.addEventListener( 'change', render );
  74. controls.minDistance = 10;
  75. controls.maxDistance = 50;
  76. controls.enablePan = false;
  77. // probe
  78. lightProbe = new THREE.LightProbe();
  79. scene.add( lightProbe );
  80. // light
  81. directionalLight = new THREE.DirectionalLight( 0xffffff, API.directionalLightIntensity );
  82. directionalLight.position.set( 10, 10, 10 );
  83. scene.add( directionalLight );
  84. // envmap
  85. var genCubeUrls = function ( prefix, postfix ) {
  86. return [
  87. prefix + 'px' + postfix, prefix + 'nx' + postfix,
  88. prefix + 'py' + postfix, prefix + 'ny' + postfix,
  89. prefix + 'pz' + postfix, prefix + 'nz' + postfix
  90. ];
  91. };
  92. var urls = genCubeUrls( 'textures/cube/pisa/', '.png' );
  93. new THREE.CubeTextureLoader().load( urls, function ( cubeTexture ) {
  94. cubeTexture.encoding = THREE.sRGBEncoding;
  95. scene.background = cubeTexture;
  96. lightProbe.copy( THREE.LightProbeGenerator.fromCubeTexture( cubeTexture ) );
  97. console.log(lightProbe );
  98. var geometry = new THREE.SphereBufferGeometry( 5, 64, 32 );
  99. //var geometry = new THREE.TorusKnotBufferGeometry( 4, 1.5, 256, 32, 2, 3 );
  100. var material = new THREE.MeshStandardMaterial( {
  101. color: 0xffffff,
  102. metalness: 0,
  103. roughness: 0,
  104. envMap: cubeTexture,
  105. envMapIntensity: API.envMapIntensity,
  106. } );
  107. // mesh
  108. mesh = new THREE.Mesh( geometry, material );
  109. scene.add( mesh );
  110. render();
  111. } );
  112. // gui
  113. gui = new dat.GUI();
  114. gui.width = 300;
  115. gui.domElement.style.userSelect = 'none';
  116. var fl = gui.addFolder( 'Intensity' );
  117. fl.add( API, 'lightProbeIntensity', 0, 1, 0.02 )
  118. .name( 'light probe')
  119. .onChange( function() { lightProbe.intensity = API.lightProbeIntensity; render(); } );
  120. fl.add( API, 'directionalLightIntensity', 0, 1, 0.02 )
  121. .name( 'directional light')
  122. .onChange( function() { directionalLight.intensity = API.directionalLightIntensity; render(); } );
  123. fl.add( API, 'envMapIntensity', 0, 1, 0.02 )
  124. .name( 'envMap')
  125. .onChange( function() { mesh.material.envMapIntensity = API.envMapIntensity; render(); } );
  126. fl.open();
  127. // listener
  128. window.addEventListener( 'resize', onWindowResize, false );
  129. }
  130. function onWindowResize() {
  131. renderer.setSize( window.innerWidth, window.innerHeight );
  132. camera.aspect = window.innerWidth / window.innerHeight;
  133. camera.updateProjectionMatrix();
  134. render();
  135. }
  136. function render() {
  137. renderer.render( scene, camera );
  138. }
  139. </script>
  140. </body>
  141. </html>