webgl_lights_rectarealight.html 7.7 KB

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