webgl_lights_lightprobe.html 4.9 KB

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