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