webgl_lights_rectarealight.html 7.6 KB

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