webgl_lights_rectarealight.html 7.7 KB

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