webgl_lights_arealight.html 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - lights - rect 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">three.js</a> - Just to show the rect light and it's edge - by <a href="http://github.com/abelnation" target="_blank">abelnation</a><br />
  35. Click and drag to move OrbitControls, center across the edge of the shadow.<br />
  36. Click to set random color CTRL-Click for White.<br />
  37. </div>
  38. <script src="../build/three.js"></script>
  39. <script src="js/lights/RectAreaLightUniformsLib.js"></script>
  40. <script src="../examples/js/libs/dat.gui.min.js"></script>
  41. <script src="../examples/js/controls/OrbitControls.js"></script>
  42. <script src="js/Detector.js"></script>
  43. <script>
  44. var container = document.getElementById( 'container' );
  45. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  46. var rnd = new THREE.WebGLRenderer();
  47. var cam = new THREE.PerspectiveCamera( 34, window.innerWidth / window.innerHeight, 0.1, 20000 );
  48. var orb = new THREE.OrbitControls( cam, rnd.domElement );
  49. var scn = new THREE.Scene();
  50. var matParams = {
  51. specular: 0xFFFFFF,
  52. shininess: 10000
  53. }
  54. var matFloor = new THREE.MeshPhongMaterial( matParams );
  55. var matBox = new THREE.MeshPhongMaterial( matParams );
  56. var geoFloor = new THREE.BoxGeometry( 2000, 0.1, 2000 );
  57. var geoBox = new THREE.BoxGeometry( Math.PI, Math.sqrt( 2 ), Math.E );
  58. var mshFloor = new THREE.Mesh( geoFloor, matFloor );
  59. var mshBox = new THREE.Mesh( geoBox, matBox );
  60. var amb = new THREE.AmbientLight( 0x080808 );
  61. // TODO (abelnation): temp point light for debugging
  62. var dir = new THREE.DirectionalLight( 0xFFFFFF );
  63. var dirHelper = new THREE.DirectionalLightHelper( dir );
  64. var shapes, shapeNames;
  65. var rectLight;
  66. var rectLightHelper;
  67. var ray = new THREE.Raycaster();
  68. var mouseDown = new THREE.Vector2();
  69. var mouse = new THREE.Vector2();
  70. var gui, guiElements;
  71. var param = {};
  72. function init() {
  73. var gl = rnd.context;
  74. // Check for float-RT support
  75. // TODO (abelnation): figure out fall-back for float textures
  76. if (!gl.getExtension("OES_texture_float")) {
  77. alert("OES_texture_float not supported");
  78. throw "missing webgl extension";
  79. }
  80. if (!gl.getExtension("OES_texture_float_linear")) {
  81. alert("OES_texture_float_linear not supported");
  82. throw "missing webgl extension";
  83. }
  84. rnd.shadowMap.enabled = true;
  85. rnd.shadowMap.type = THREE.PCFSoftShadowMap;
  86. rnd.gammaInput = true;
  87. rnd.gammaOutput = true;
  88. rnd.antialias = true;
  89. rnd.domElement.addEventListener( 'mousedown', onDocumentClick );
  90. rnd.domElement.addEventListener( 'mouseup', onDocumentClick );
  91. // cam.position.set( 0, 100, 0 );
  92. cam.position.set( 45, 20, 45 );
  93. // shapes = {
  94. // square: THREE.Polygon.makeSquare( 10.0 ),
  95. // rectangle: THREE.Polygon.makeRectangle( 10.0, 5.0 ),
  96. // circle: THREE.Polygon.makeCircle( 5.0, 20 ),
  97. // star: THREE.Polygon.makeStar( 5, 5.0, 2.5 ),
  98. // triangle: new THREE.Polygon( [
  99. // new THREE.Vector3( -5.0, -5.0, 0 ),
  100. // new THREE.Vector3( 0.0, 5.0, 0 ),
  101. // new THREE.Vector3( 5.0, -5.0, 0 ),
  102. // ] )
  103. // };
  104. // shapeNames = Object.keys( shapes );
  105. rectLight = new THREE.RectAreaLight( 0xFFFFFF, undefined, 2, 10 );
  106. rectLight.matrixAutoUpdate = true;
  107. rectLight.intensity = 70.0;
  108. rectLight.position.set( 5, 5, 0 );
  109. // rectLight.target = mshBox;
  110. rectLightHelper = new THREE.RectAreaLightHelper( rectLight );
  111. rectLight.add( rectLightHelper );
  112. // TODO (abelnation): rect light shadow
  113. matFloor.color.set( 0x808080 );
  114. randomColor( matBox );
  115. mshFloor.receiveShadow = true;
  116. mshFloor.position.set( 0, 0, 0 );
  117. mshBox.castShadow = true;
  118. mshBox.receiveShadow = true;
  119. mshBox.position.set( 0, 5, 0 );
  120. scn.add( cam );
  121. scn.add( mshFloor );
  122. scn.add( mshBox );
  123. // scn.add( amb );
  124. // scn.add( dir );
  125. // scn.add( dirHelper );
  126. scn.add( rectLight );
  127. // scn.add( rectLightHelper );
  128. // scn.add( new THREE.AxisHelper( 10 ) );
  129. document.body.appendChild( rnd.domElement );
  130. onResize();
  131. window.addEventListener( 'resize', onResize, false );
  132. orb.addEventListener( 'change', render );
  133. orb.update();
  134. }
  135. function onResize() {
  136. rnd.setSize( window.innerWidth, window.innerHeight );
  137. cam.aspect = ( window.innerWidth / window.innerHeight );
  138. cam.updateProjectionMatrix();
  139. orb.target = mshBox.position;
  140. }
  141. function tick() {
  142. update();
  143. render();
  144. requestAnimationFrame( tick );
  145. }
  146. function update() {
  147. var dt = 6000;
  148. var qdt = dt / 4.0;
  149. var dirSigns = [
  150. [ 1, 1 ],
  151. [ - 1, 1 ],
  152. [ - 1, 1 ],
  153. [ 1, 1 ]
  154. ];
  155. var t = ( Date.now() / 1000 );
  156. // move light in circle around center
  157. // change light height with sine curve
  158. var r = 10.0;
  159. var lx = r * Math.cos( t );
  160. var lz = r * Math.sin( t );
  161. var ly = 5.0 + 5.0 * Math.sin( t / 3.0 );
  162. // var ly = 7.0;
  163. rectLight.position.set( lx, ly, lz );
  164. rectLight.lookAt( mshBox.position );
  165. rectLight.updateMatrixWorld();
  166. // // move box in figure 8 path
  167. // // xz is a figure 8
  168. // // y is gently rising and falling
  169. // var r = t * ( 4.0 * Math.PI );
  170. // var s = Math.floor( t * dirSigns.length );
  171. //
  172. // var sign = dirSigns[ s ];
  173. // var x = 5.0 * ( Math.cos( r ) + 1.0 ) * sign[ 0 ];
  174. // var z = 5.0 * Math.sin( r ) * sign[ 1 ];
  175. // var y = 2.5 * Math.cos( 0.5 * r ) + 5.0;
  176. //
  177. // mshBox.position.set( x, y, z );
  178. // mshBox.updateMatrixWorld();
  179. }
  180. function render() {
  181. rectLightHelper.update(); // required
  182. rnd.render( scn, cam );
  183. }
  184. function clearGui() {
  185. if ( gui ) gui.destroy();
  186. gui = new dat.GUI();
  187. gui.width = 190;
  188. var gStyle = gui.domElement.style;
  189. gStyle.position = "absolute";
  190. gStyle.top = "48px";
  191. gStyle.height = "220px";
  192. gui.open();
  193. }
  194. function buildGui() {
  195. clearGui();
  196. param = {
  197. 'light color': rectLight.color.getHex(),
  198. intensity: rectLight.intensity,
  199. width: rectLight.width,
  200. height: rectLight.height,
  201. shininess: matFloor.shininess,
  202. // shape: shapeNames[0]
  203. };
  204. gui.add( param, 'width', 0.1, 20).onChange( function ( val ) {
  205. rectLight.width = val;
  206. } );
  207. gui.add( param, 'height', 0.1, 20).onChange( function ( val ) {
  208. rectLight.height = val;
  209. } );
  210. // gui.add( param, 'shape', shapeNames ).onChange( function ( val ) {
  211. //
  212. // rectLight.polygon = shapes[ val ].clone();
  213. //
  214. // } );
  215. gui.addColor( param, 'light color' ).onChange( function ( val ) {
  216. rectLight.color.setHex( val );
  217. } );
  218. gui.add( param, 'intensity', 0, 100 ).onChange( function ( val ) {
  219. rectLight.intensity = val;
  220. } );
  221. gui.add( param, 'shininess', 0, 100000 ).onChange( function ( val ) {
  222. matBox.shininess = val;
  223. matFloor.shininess = val;
  224. } );
  225. // TODO (abelnation): shadow controls
  226. // addGui( 'distance', rectLight.distance, function( val ) {
  227. // rectLight.distance = val;
  228. // render();
  229. // }, false, 0, 1000 );
  230. // addGui( 'penumbra', rectLight.penumbra, function( val ) {
  231. // rectLight.penumbra = val;
  232. // render();
  233. // }, false, 0, 1 );
  234. // addGui( 'decay', rectLight.decay, function( val ) {
  235. // rectLight.decay = val;
  236. // render();
  237. // }, false, 0, 100 );
  238. }
  239. function onDocumentClick( event ) {
  240. event.preventDefault();
  241. var rndDom = rnd.domElement;
  242. if ( event.type === 'mousedown' ) {
  243. mouseDown.x = ( event.clientX / rndDom.clientWidth ) * 2 - 1;
  244. mouseDown.y = - ( event.clientY / rndDom.clientHeight ) * 2 + 1;
  245. } else {
  246. mouse.x = ( event.clientX / rndDom.clientWidth ) * 2 - 1;
  247. mouse.y = - ( event.clientY / rndDom.clientHeight ) * 2 + 1;
  248. if ( mouseDown.distanceTo( mouse ) < 0.0075 ) {
  249. ray.setFromCamera( mouse, cam );
  250. var found = ray.intersectObjects( [ mshBox, mshFloor ] );
  251. if ( found.length > 0 ) {
  252. if ( event.ctrlKey === false ) randomColor( found[ 0 ].object );
  253. else found[ 0 ].object.material.color.set( 0xffffff );
  254. render();
  255. }
  256. }
  257. }
  258. }
  259. function randomColor( target ) {
  260. if ( target !== undefined ) {
  261. if ( target.material !== undefined ) target = target.material;
  262. if ( target.color !== undefined ) {
  263. target.color.setHex( 0xffffff * Math.random() );
  264. }
  265. }
  266. }
  267. init();
  268. buildGui();
  269. tick();
  270. </script>
  271. </body>
  272. </html>