webgl_lights_spotlight.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - lights - spot 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 spot light and it's edge - by <a href="http://master-domain.com" target="_blank">Master James</a><br />
  35. Right click and drag to move OrbitControls,<br /> center across the edge of the shadow.<br />
  36. </div>
  37. <script src="../build/three.js"></script>
  38. <script src="../examples/js/libs/dat.gui.min.js"></script>
  39. <script src="../examples/js/controls/OrbitControls.js"></script>
  40. <script src="js/Detector.js"></script>
  41. <script>
  42. var container = document.getElementById( 'container' );
  43. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  44. var rnd = new THREE.WebGLRenderer();
  45. var cam = new THREE.PerspectiveCamera(34, window.innerWidth / window.innerHeight, 0.1, 20000);
  46. var orb = new THREE.OrbitControls(cam, rnd.domElement);
  47. var scn = new THREE.Scene();
  48. var matFloor = new THREE.MeshPhongMaterial();
  49. var matBox = new THREE.MeshPhongMaterial();
  50. var geoFloor = new THREE.BoxGeometry(2000, 0.1, 2000);
  51. var geoBox = new THREE.BoxGeometry(Math.PI, Math.sqrt(2), Math.E);
  52. var mshFloor = new THREE.Mesh(geoFloor, matFloor);
  53. var mshBox = new THREE.Mesh(geoBox, matBox);
  54. var amb = new THREE.AmbientLight(0x121422);
  55. var spt = new THREE.SpotLight(0xFFFFFF);
  56. var lightHelper;
  57. var ray = new THREE.Raycaster();
  58. var mouseDown = new THREE.Vector2();
  59. var mouse = new THREE.Vector2();
  60. var gui, guiElements, param = { color: '0xffffff' };
  61. function init() {
  62. rnd.shadowMap.enabled = true;
  63. rnd.shadowMap.type = THREE.PCFSoftShadowMap;
  64. rnd.gammaInput = true;
  65. rnd.gammaOutput = true;
  66. rnd.antialias = true;
  67. rnd.domElement.addEventListener('mousedown', onDocumentClick);
  68. rnd.domElement.addEventListener('mouseup', onDocumentClick);
  69. cam.position.set(30, 13, -24);
  70. spt.position.set(15, 40, 35);
  71. spt.castShadow = true;
  72. spt.angle = 0.75;
  73. spt.exponent = 2.0;
  74. spt.penumbra = 0.05;
  75. spt.decay = 2;
  76. spt.distance = 500;
  77. spt.shadow.mapSize.width = 8192;
  78. spt.shadow.mapSize.height = 8192;
  79. // shadow camera helper
  80. spt.shadowCameraHelper = new THREE.CameraHelper( spt.shadow.camera ); // colored lines
  81. spt.shadow.camera.near = 0.1;
  82. spt.shadow.camera.far = 20000;
  83. //spt.shadow.camera.fov = (spt.angle * (360 / Math.PI));
  84. lightHelper = new THREE.SpotLightHelper( spt );
  85. matFloor.color.set( 0x808080 );
  86. randomColor( matBox );
  87. mshFloor.receiveShadow = true;
  88. mshFloor.position.set(0, -0.05, 0);
  89. mshBox.castShadow = true;
  90. mshBox.receiveShadow = true;
  91. mshBox.position.set(40, 1.8, 0);
  92. scn.add(cam);
  93. scn.add(mshFloor);
  94. scn.add(mshBox);
  95. scn.add(amb);
  96. scn.add(spt);
  97. scn.add( spt.shadowCameraHelper );
  98. scn.add( new THREE.AxisHelper( 10 ) );
  99. scn.add( lightHelper );
  100. document.body.appendChild(rnd.domElement);
  101. onResize();
  102. window.addEventListener('resize', onResize, false);
  103. orb.addEventListener('change', render);
  104. //orb.maxPolarAngle = (Math.PI / 2);
  105. orb.update();
  106. }
  107. function onResize() {
  108. rnd.setSize(window.innerWidth, window.innerHeight);
  109. cam.aspect = (window.innerWidth / window.innerHeight);
  110. cam.updateProjectionMatrix();
  111. orb.target = mshBox.position;
  112. }
  113. function render() {
  114. lightHelper.update(); // required
  115. if ( spt.shadowCameraHelper ) spt.shadowCameraHelper.update();
  116. rnd.render(scn, cam);
  117. }
  118. function clearGui() {
  119. if ( gui ) gui.destroy();
  120. gui = new dat.GUI();
  121. gui.open();
  122. }
  123. function buildGui() {
  124. clearGui();
  125. addGui( 'light color', spt.color.getHex(), function( val ) {
  126. spt.color.setHex( val );
  127. render();
  128. }, true );
  129. addGui( 'intensity', spt.intensity, function( val ) {
  130. spt.intensity = val;
  131. render();
  132. }, false, 0, 10 );
  133. addGui( 'distance', spt.distance, function( val ) {
  134. spt.distance = val;
  135. render();
  136. }, false, 0, 1000 );
  137. addGui( 'angle', spt.angle, function( val ) {
  138. spt.angle = val;
  139. render();
  140. }, false, 0, 1.56 );
  141. addGui( 'penumbra', spt.penumbra, function( val ) {
  142. spt.penumbra = val;
  143. render();
  144. }, false, 0, 1 );
  145. addGui( 'decay', spt.decay, function( val ) {
  146. spt.decay = val;
  147. render();
  148. }, false, 0, 100 );
  149. }
  150. function addGui( name, value, callback, isColor, min, max ) {
  151. var node;
  152. param[ name ] = value;
  153. if ( isColor ) {
  154. node = gui.addColor( param, name ).onChange( function() {
  155. callback( param[ name ] );
  156. } );
  157. }
  158. else if ( typeof value == 'object' ) {
  159. node = gui.add( param, name, value ).onChange( function() {
  160. callback( param[ name ] );
  161. } );
  162. }
  163. else {
  164. node = gui.add( param, name, min, max ).onChange( function() {
  165. callback( param[ name ] );
  166. } );
  167. }
  168. return node;
  169. }
  170. function onDocumentClick( event ) {
  171. event.preventDefault();
  172. var rndDom = rnd.domElement;
  173. if( event.type === 'mousedown' ) {
  174. mouseDown.x = ( event.clientX / rndDom.clientWidth ) * 2 - 1;
  175. mouseDown.y = - ( event.clientY / rndDom.clientHeight ) * 2 + 1;
  176. }
  177. else {
  178. mouse.x = ( event.clientX / rndDom.clientWidth ) * 2 - 1;
  179. mouse.y = - ( event.clientY / rndDom.clientHeight ) * 2 + 1;
  180. console.log(mouseDown.distanceTo(mouse));
  181. if (mouseDown.distanceTo(mouse) < 0.0075) {
  182. ray.setFromCamera( mouse, cam );
  183. var found = ray.intersectObjects( [ mshBox, mshFloor ] );
  184. if ( found.length > 0 ) randomColor( found[0].object );
  185. }
  186. }
  187. }
  188. function randomColor( target ) {
  189. if ( target !== undefined ) {
  190. if ( target.material !== undefined ) target = target.material;
  191. if ( target.color !== undefined ) {
  192. target.color.setHex( 0xffffff * Math.random() );
  193. render();
  194. }
  195. }
  196. }
  197. init();
  198. buildGui();
  199. render();
  200. </script>
  201. </body>
  202. </html>