webgl_lights_rectarealight.html 7.3 KB

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