webgl_lights_rectarealight.html 7.6 KB

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