webgl_lightprobe.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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/WebGL.js"></script>
  38. <script>
  39. if ( WEBGL.isWebGLAvailable() === false ) {
  40. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  41. }
  42. var mesh, renderer, scene, camera;
  43. var gui;
  44. var lightProbe;
  45. var directionalLight;
  46. // linear color space
  47. var API = {
  48. lightProbeIntensity: 1.0,
  49. directionalLightIntensity: 0.2,
  50. envMapIntensity: 1
  51. };
  52. init();
  53. function init() {
  54. // renderer
  55. renderer = new THREE.WebGLRenderer( { antialias: true } );
  56. renderer.setPixelRatio( window.devicePixelRatio );
  57. renderer.setSize( window.innerWidth, window.innerHeight );
  58. document.body.appendChild( renderer.domElement );
  59. // tone mapping
  60. //renderer.toneMapping = THREE.LinearToneMapping;
  61. //renderer.toneMappingExposure = API.exposure;
  62. // gamma
  63. renderer.gammaOutput = true;
  64. renderer.gammaFactor = 2.2; // approximate sRGB
  65. // scene
  66. scene = new THREE.Scene();
  67. // camera
  68. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  69. camera.position.set( 0, 0, 30 );
  70. // controls
  71. var controls = new THREE.OrbitControls( camera, renderer.domElement );
  72. controls.addEventListener( 'change', render );
  73. controls.minDistance = 10;
  74. controls.maxDistance = 50;
  75. controls.enablePan = false;
  76. // probe
  77. lightProbe = new THREE.LightProbe( 0xffffff, API.lightProbeIntensity );
  78. scene.add( lightProbe );
  79. // light
  80. directionalLight = new THREE.DirectionalLight( 0xffffff, API.directionalLightIntensity );
  81. directionalLight.position.set( 10, 10, 10 );
  82. scene.add( directionalLight );
  83. // envmap
  84. var genCubeUrls = function ( prefix, postfix ) {
  85. return [
  86. prefix + 'px' + postfix, prefix + 'nx' + postfix,
  87. prefix + 'py' + postfix, prefix + 'ny' + postfix,
  88. prefix + 'pz' + postfix, prefix + 'nz' + postfix
  89. ];
  90. };
  91. var urls = genCubeUrls( 'textures/cube/pisa/', '.png' );
  92. new THREE.CubeTextureLoader().load( urls, function ( cubeTexture ) {
  93. cubeTexture.encoding = THREE.sRGBEncoding;
  94. scene.background = cubeTexture;
  95. lightProbe.setFromCubeTexture( cubeTexture );
  96. var geometry = new THREE.SphereBufferGeometry( 5, 64, 32 );
  97. //var geometry = new THREE.TorusKnotBufferGeometry( 4, 1.5, 256, 32, 2, 3 );
  98. var material = new THREE.MeshStandardMaterial( {
  99. color: 0xffffff,
  100. metalness: 0,
  101. roughness: 0,
  102. envMap: cubeTexture,
  103. envMapIntensity: API.envMapIntensity,
  104. } );
  105. // mesh
  106. mesh = new THREE.Mesh( geometry, material );
  107. scene.add( mesh );
  108. render();
  109. } );
  110. // gui
  111. gui = new dat.GUI();
  112. gui.width = 300;
  113. gui.domElement.style.userSelect = 'none';
  114. var fl = gui.addFolder( 'Intensity' );
  115. fl.add( API, 'lightProbeIntensity', 0, 1, 0.02 )
  116. .name( 'light probe')
  117. .onChange( function() { lightProbe.intensity = API.lightProbeIntensity; render(); } );
  118. fl.add( API, 'directionalLightIntensity', 0, 1, 0.02 )
  119. .name( 'directional light')
  120. .onChange( function() { directionalLight.intensity = API.directionalLightIntensity; render(); } );
  121. fl.add( API, 'envMapIntensity', 0, 1, 0.02 )
  122. .name( 'envMap')
  123. .onChange( function() { mesh.material.envMapIntensity = API.envMapIntensity; render(); } );
  124. fl.open();
  125. // listener
  126. window.addEventListener( 'resize', onWindowResize, false );
  127. }
  128. function onWindowResize() {
  129. renderer.setSize( window.innerWidth, window.innerHeight );
  130. camera.aspect = window.innerWidth / window.innerHeight;
  131. camera.updateProjectionMatrix();
  132. render();
  133. }
  134. function render() {
  135. renderer.render( scene, camera );
  136. }
  137. </script>
  138. </body>
  139. </html>