webgl_lights_rectarealight.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 { RectAreaLightHelper } from './jsm/helpers/RectAreaLightHelper.js';
  20. import { RectAreaLightUniformsLib } from './jsm/lights/RectAreaLightUniformsLib.js';
  21. let renderer, scene, camera;
  22. const origin = new THREE.Vector3();
  23. let rectLight, rectLightHelper;
  24. let param = {};
  25. let stats;
  26. init();
  27. animate();
  28. function init() {
  29. renderer = new THREE.WebGLRenderer( { antialias: true } );
  30. renderer.setPixelRatio( window.devicePixelRatio );
  31. renderer.setSize( window.innerWidth, window.innerHeight );
  32. renderer.shadowMap.enabled = true;
  33. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  34. renderer.outputEncoding = THREE.sRGBEncoding;
  35. document.body.appendChild( renderer.domElement );
  36. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  37. camera.position.set( 0, 20, 35 );
  38. scene = new THREE.Scene();
  39. const ambient = new THREE.AmbientLight( 0xffffff, 0.1 );
  40. scene.add( ambient );
  41. RectAreaLightUniformsLib.init();
  42. rectLight = new THREE.RectAreaLight( 0xffffff, 1, 10, 10 );
  43. rectLight.position.set( 5, 5, 0 );
  44. scene.add( rectLight );
  45. rectLightHelper = new RectAreaLightHelper( rectLight );
  46. rectLight.add( rectLightHelper );
  47. const geoFloor = new THREE.BoxBufferGeometry( 2000, 0.1, 2000 );
  48. const matStdFloor = new THREE.MeshStandardMaterial( { color: 0x808080, roughness: 0, metalness: 0 } );
  49. const mshStdFloor = new THREE.Mesh( geoFloor, matStdFloor );
  50. scene.add( mshStdFloor );
  51. const matStdObjects = new THREE.MeshStandardMaterial( { color: 0xA00000, roughness: 0, metalness: 0 } );
  52. const geoBox = new THREE.BoxBufferGeometry( Math.PI, Math.sqrt( 2 ), Math.E );
  53. const mshStdBox = new THREE.Mesh( geoBox, matStdObjects );
  54. mshStdBox.position.set( 0, 5, 0 );
  55. mshStdBox.rotation.set( 0, Math.PI / 2.0, 0 );
  56. mshStdBox.castShadow = true;
  57. mshStdBox.receiveShadow = true;
  58. scene.add( mshStdBox );
  59. const geoSphere = new THREE.SphereBufferGeometry( 1.5, 32, 32 );
  60. const mshStdSphere = new THREE.Mesh( geoSphere, matStdObjects );
  61. mshStdSphere.position.set( - 5, 5, 0 );
  62. mshStdSphere.castShadow = true;
  63. mshStdSphere.receiveShadow = true;
  64. scene.add( mshStdSphere );
  65. const geoKnot = new THREE.TorusKnotBufferGeometry( 1.5, 0.5, 100, 16 );
  66. const mshStdKnot = new THREE.Mesh( geoKnot, matStdObjects );
  67. mshStdKnot.position.set( 5, 5, 0 );
  68. mshStdKnot.castShadow = true;
  69. mshStdKnot.receiveShadow = true;
  70. scene.add( mshStdKnot );
  71. const controls = new OrbitControls( camera, renderer.domElement );
  72. controls.target.copy( mshStdBox.position );
  73. controls.update();
  74. // GUI
  75. const gui = new GUI( { width: 300 } );
  76. gui.open();
  77. param = {
  78. motion: true,
  79. width: rectLight.width,
  80. height: rectLight.height,
  81. color: rectLight.color.getHex(),
  82. intensity: rectLight.intensity,
  83. 'ambient': ambient.intensity,
  84. 'floor color': matStdFloor.color.getHex(),
  85. 'object color': matStdObjects.color.getHex(),
  86. 'roughness': matStdFloor.roughness,
  87. 'metalness': matStdFloor.metalness
  88. };
  89. gui.add( param, 'motion' );
  90. const lightFolder = gui.addFolder( 'Light' );
  91. lightFolder.add( param, 'width', 1, 20 ).step( 0.1 ).onChange( function ( val ) {
  92. rectLight.width = val;
  93. } );
  94. lightFolder.add( param, 'height', 1, 20 ).step( 0.1 ).onChange( function ( val ) {
  95. rectLight.height = val;
  96. } );
  97. lightFolder.addColor( param, 'color' ).onChange( function ( val ) {
  98. rectLight.color.setHex( val );
  99. } );
  100. lightFolder.add( param, 'intensity', 0.0, 4.0 ).step( 0.01 ).onChange( function ( val ) {
  101. rectLight.intensity = val;
  102. } );
  103. lightFolder.add( param, 'ambient', 0.0, 0.2 ).step( 0.01 ).onChange( function ( val ) {
  104. ambient.intensity = val;
  105. } );
  106. lightFolder.open();
  107. const standardFolder = gui.addFolder( 'Standard Material' );
  108. standardFolder.addColor( param, 'floor color' ).onChange( function ( val ) {
  109. matStdFloor.color.setHex( val );
  110. } );
  111. standardFolder.addColor( param, 'object color' ).onChange( function ( val ) {
  112. matStdObjects.color.setHex( val );
  113. } );
  114. standardFolder.add( param, 'roughness', 0.0, 1.0 ).step( 0.01 ).onChange( function ( val ) {
  115. matStdObjects.roughness = val;
  116. matStdFloor.roughness = val;
  117. } );
  118. // TODO (abelnation): use env map to reflect metal property
  119. standardFolder.add( param, 'metalness', 0.0, 1.0 ).step( 0.01 ).onChange( function ( val ) {
  120. matStdObjects.metalness = val;
  121. matStdFloor.metalness = val;
  122. } );
  123. standardFolder.open();
  124. // TODO: rect area light distance
  125. // TODO: rect area light decay
  126. //
  127. window.addEventListener( 'resize', onResize, false );
  128. stats = new Stats();
  129. document.body.appendChild( stats.dom );
  130. }
  131. function onResize() {
  132. renderer.setSize( window.innerWidth, window.innerHeight );
  133. camera.aspect = ( window.innerWidth / window.innerHeight );
  134. camera.updateProjectionMatrix();
  135. }
  136. function animate() {
  137. requestAnimationFrame( animate );
  138. if ( param.motion ) {
  139. const t = ( Date.now() / 2000 );
  140. // move light in circle around center
  141. // change light height with sine curve
  142. const r = 15.0;
  143. const lx = r * Math.cos( t );
  144. const lz = r * Math.sin( t );
  145. const ly = 5.0 + 5.0 * Math.sin( t / 3.0 );
  146. rectLight.position.set( lx, ly, lz );
  147. rectLight.lookAt( origin );
  148. }
  149. rectLightHelper.update();
  150. renderer.render( scene, camera );
  151. stats.update();
  152. }
  153. </script>
  154. </body>
  155. </html>