webgl_lights_rectarealight.html 7.2 KB

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