webgl_lights_rectarealight.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - lights - rect area light</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> - THREE.RectAreaLight<br/>
  12. by <a href="http://github.com/abelnation" target="_blank" rel="noopener">abelnation</a>
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import Stats from './jsm/libs/stats.module.js';
  17. import { GUI } from './jsm/libs/dat.gui.module.js';
  18. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  19. import { RectAreaLightUniformsLib } from './jsm/lights/RectAreaLightUniformsLib.js';
  20. var renderer, scene, camera;
  21. var origin = new THREE.Vector3();
  22. var rectLight;
  23. var param = {};
  24. var stats;
  25. init();
  26. animate();
  27. function init() {
  28. renderer = new THREE.WebGLRenderer( { antialias: true } );
  29. renderer.setPixelRatio( window.devicePixelRatio );
  30. renderer.setSize( window.innerWidth, window.innerHeight );
  31. renderer.shadowMap.enabled = true;
  32. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  33. renderer.outputEncoding = THREE.sRGBEncoding;
  34. document.body.appendChild( renderer.domElement );
  35. // Check for float-RT support
  36. // TODO (abelnation): figure out fall-back for float textures
  37. if ( ! renderer.extensions.get( 'OES_texture_float' ) ) {
  38. alert( 'OES_texture_float not supported' );
  39. throw 'missing webgl extension';
  40. }
  41. if ( ! renderer.extensions.get( 'OES_texture_float_linear' ) ) {
  42. alert( 'OES_texture_float_linear not supported' );
  43. throw 'missing webgl extension';
  44. }
  45. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  46. camera.position.set( 0, 20, 35 );
  47. scene = new THREE.Scene();
  48. var ambient = new THREE.AmbientLight( 0xffffff, 0.1 );
  49. scene.add( ambient );
  50. RectAreaLightUniformsLib.init();
  51. rectLight = new THREE.RectAreaLight( 0xffffff, 1, 10, 10 );
  52. rectLight.position.set( 5, 5, 0 );
  53. scene.add( rectLight );
  54. var rectLightMesh = new THREE.Mesh( new THREE.PlaneBufferGeometry(), new THREE.MeshBasicMaterial( { side: THREE.BackSide } ) );
  55. rectLightMesh.scale.x = rectLight.width;
  56. rectLightMesh.scale.y = rectLight.height;
  57. rectLight.add( rectLightMesh );
  58. var rectLightMeshBack = new THREE.Mesh( new THREE.PlaneBufferGeometry(), new THREE.MeshBasicMaterial( { color: 0x080808 } ) );
  59. rectLightMesh.add( rectLightMeshBack );
  60. var geoFloor = new THREE.BoxBufferGeometry( 2000, 0.1, 2000 );
  61. var matStdFloor = new THREE.MeshStandardMaterial( { color: 0x808080, roughness: 0, metalness: 0 } );
  62. var mshStdFloor = new THREE.Mesh( geoFloor, matStdFloor );
  63. scene.add( mshStdFloor );
  64. var matStdObjects = new THREE.MeshStandardMaterial( { color: 0xA00000, roughness: 0, metalness: 0 } );
  65. var geoBox = new THREE.BoxBufferGeometry( Math.PI, Math.sqrt( 2 ), Math.E );
  66. var mshStdBox = new THREE.Mesh( geoBox, matStdObjects );
  67. mshStdBox.position.set( 0, 5, 0 );
  68. mshStdBox.rotation.set( 0, Math.PI / 2.0, 0 );
  69. mshStdBox.castShadow = true;
  70. mshStdBox.receiveShadow = true;
  71. scene.add( mshStdBox );
  72. var geoSphere = new THREE.SphereBufferGeometry( 1.5, 32, 32 );
  73. var mshStdSphere = new THREE.Mesh( geoSphere, matStdObjects );
  74. mshStdSphere.position.set( - 5, 5, 0 );
  75. mshStdSphere.castShadow = true;
  76. mshStdSphere.receiveShadow = true;
  77. scene.add( mshStdSphere );
  78. var geoKnot = new THREE.TorusKnotBufferGeometry( 1.5, 0.5, 100, 16 );
  79. var mshStdKnot = new THREE.Mesh( geoKnot, matStdObjects );
  80. mshStdKnot.position.set( 5, 5, 0 );
  81. mshStdKnot.castShadow = true;
  82. mshStdKnot.receiveShadow = true;
  83. scene.add( mshStdKnot );
  84. var controls = new OrbitControls( camera, renderer.domElement );
  85. controls.target.copy( mshStdBox.position );
  86. controls.update();
  87. // GUI
  88. var gui = new GUI( { width: 300 } );
  89. gui.open();
  90. param = {
  91. motion: true,
  92. width: rectLight.width,
  93. height: rectLight.height,
  94. color: rectLight.color.getHex(),
  95. intensity: rectLight.intensity,
  96. 'ambient': ambient.intensity,
  97. 'floor color': matStdFloor.color.getHex(),
  98. 'object color': matStdObjects.color.getHex(),
  99. 'roughness': matStdFloor.roughness,
  100. 'metalness': matStdFloor.metalness
  101. };
  102. gui.add( param, 'motion' );
  103. var lightFolder = gui.addFolder( 'Light' );
  104. lightFolder.add( param, 'width', 1, 20 ).step( 0.1 ).onChange( function ( val ) {
  105. rectLight.width = val;
  106. rectLightMesh.scale.x = val;
  107. } );
  108. lightFolder.add( param, 'height', 1, 20 ).step( 0.1 ).onChange( function ( val ) {
  109. rectLight.height = val;
  110. rectLightMesh.scale.y = val;
  111. } );
  112. lightFolder.addColor( param, 'color' ).onChange( function ( val ) {
  113. rectLight.color.setHex( val );
  114. rectLightMesh.material.color.copy( rectLight.color ).multiplyScalar( rectLight.intensity );
  115. } );
  116. lightFolder.add( param, 'intensity', 0.0, 4.0 ).step( 0.01 ).onChange( function ( val ) {
  117. rectLight.intensity = val;
  118. rectLightMesh.material.color.copy( rectLight.color ).multiplyScalar( rectLight.intensity );
  119. } );
  120. lightFolder.add( param, 'ambient', 0.0, 0.2 ).step( 0.01 ).onChange( function ( val ) {
  121. ambient.intensity = val;
  122. } );
  123. lightFolder.open();
  124. var standardFolder = gui.addFolder( 'Standard Material' );
  125. standardFolder.addColor( param, 'floor color' ).onChange( function ( val ) {
  126. matStdFloor.color.setHex( val );
  127. } );
  128. standardFolder.addColor( param, 'object color' ).onChange( function ( val ) {
  129. matStdObjects.color.setHex( val );
  130. } );
  131. standardFolder.add( param, 'roughness', 0.0, 1.0 ).step( 0.01 ).onChange( function ( val ) {
  132. matStdObjects.roughness = val;
  133. matStdFloor.roughness = val;
  134. } );
  135. // TODO (abelnation): use env map to reflect metal property
  136. standardFolder.add( param, 'metalness', 0.0, 1.0 ).step( 0.01 ).onChange( function ( val ) {
  137. matStdObjects.metalness = val;
  138. matStdFloor.metalness = val;
  139. } );
  140. standardFolder.open();
  141. // TODO: rect area light distance
  142. // TODO: rect area light decay
  143. //
  144. window.addEventListener( 'resize', onResize, false );
  145. stats = new Stats();
  146. document.body.appendChild( stats.dom );
  147. }
  148. function onResize() {
  149. renderer.setSize( window.innerWidth, window.innerHeight );
  150. camera.aspect = ( window.innerWidth / window.innerHeight );
  151. camera.updateProjectionMatrix();
  152. }
  153. function animate() {
  154. requestAnimationFrame( animate );
  155. if ( param.motion ) {
  156. var t = ( Date.now() / 2000 );
  157. // move light in circle around center
  158. // change light height with sine curve
  159. var r = 15.0;
  160. var lx = r * Math.cos( t );
  161. var lz = r * Math.sin( t );
  162. var ly = 5.0 + 5.0 * Math.sin( t / 3.0 );
  163. rectLight.position.set( lx, ly, lz );
  164. rectLight.lookAt( origin );
  165. }
  166. renderer.render( scene, camera );
  167. stats.update();
  168. }
  169. </script>
  170. </body>
  171. </html>