webgl_lights_spotlight.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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, 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="../examples/js/libs/dat.gui.min.js"></script>
  40. <script src="../examples/js/controls/OrbitControls.js"></script>
  41. <script src="js/Detector.js"></script>
  42. <script>
  43. var container = document.getElementById( 'container' );
  44. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  45. var rnd = new THREE.WebGLRenderer();
  46. var cam = new THREE.PerspectiveCamera(34, window.innerWidth / window.innerHeight, 0.1, 20000);
  47. var orb = new THREE.OrbitControls(cam, rnd.domElement);
  48. var scn = new THREE.Scene();
  49. var matFloor = new THREE.MeshPhongMaterial();
  50. var matBox = new THREE.MeshPhongMaterial();
  51. var geoFloor = new THREE.BoxGeometry(2000, 0.1, 2000);
  52. var geoBox = new THREE.BoxGeometry(Math.PI, Math.sqrt(2), Math.E);
  53. var mshFloor = new THREE.Mesh(geoFloor, matFloor);
  54. var mshBox = new THREE.Mesh(geoBox, matBox);
  55. var amb = new THREE.AmbientLight(0x121422);
  56. var spt = new THREE.SpotLight(0xFFFFFF);
  57. var lightHelper;
  58. var ray = new THREE.Raycaster();
  59. var mouseDown = new THREE.Vector2();
  60. var mouse = new THREE.Vector2();
  61. var gui, guiElements, param = { color: '0xffffff' };
  62. function init() {
  63. rnd.shadowMap.enabled = true;
  64. rnd.shadowMap.type = THREE.PCFSoftShadowMap;
  65. rnd.gammaInput = true;
  66. rnd.gammaOutput = true;
  67. rnd.antialias = true;
  68. rnd.domElement.addEventListener('mousedown', onDocumentClick);
  69. rnd.domElement.addEventListener('mouseup', onDocumentClick);
  70. cam.position.set(65, 8, -10);
  71. spt.position.set(15, 40, 35);
  72. spt.castShadow = true;
  73. spt.angle = 0.75;
  74. spt.exponent = 2.0;
  75. spt.penumbra = 0.05;
  76. spt.decay = 2;
  77. spt.distance = 500;
  78. spt.shadow.mapSize.width = 1024;
  79. spt.shadow.mapSize.height = 1024;
  80. spt.shadow.camera.near = 0.1;
  81. spt.shadow.camera.far = 20000;
  82. lightHelper = new THREE.SpotLightHelper( spt );
  83. matFloor.color.set( 0x808080 );
  84. randomColor( matBox );
  85. mshFloor.receiveShadow = true;
  86. mshFloor.position.set(0, -0.05, 0);
  87. mshBox.castShadow = true;
  88. mshBox.receiveShadow = true;
  89. mshBox.position.set(40, 1.8, 0);
  90. scn.add(cam);
  91. scn.add(mshFloor);
  92. scn.add(mshBox);
  93. scn.add(amb);
  94. scn.add(spt);
  95. scn.add( new THREE.AxisHelper( 10 ) );
  96. scn.add( lightHelper );
  97. document.body.appendChild(rnd.domElement);
  98. onResize();
  99. window.addEventListener('resize', onResize, false);
  100. orb.addEventListener('change', render);
  101. orb.update();
  102. }
  103. function onResize() {
  104. rnd.setSize(window.innerWidth, window.innerHeight);
  105. cam.aspect = (window.innerWidth / window.innerHeight);
  106. cam.updateProjectionMatrix();
  107. orb.target = mshBox.position;
  108. }
  109. function render() {
  110. lightHelper.update(); // required
  111. rnd.render(scn, cam);
  112. }
  113. function clearGui() {
  114. if ( gui ) gui.destroy();
  115. gui = new dat.GUI();
  116. gui.width = 190;
  117. var gStyle = gui.domElement.style;
  118. gStyle.position = "absolute";
  119. gStyle.top = "48px";
  120. gStyle.height = "220px";
  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. if (mouseDown.distanceTo(mouse) < 0.0075) {
  181. ray.setFromCamera( mouse, cam );
  182. var found = ray.intersectObjects( [ mshBox, mshFloor ] );
  183. if ( found.length > 0 ) {
  184. if( event.ctrlKey === false ) randomColor( found[0].object );
  185. else found[0].object.material.color.set( 0xffffff );
  186. render();
  187. }
  188. }
  189. }
  190. }
  191. function randomColor( target ) {
  192. if ( target !== undefined ) {
  193. if ( target.material !== undefined ) target = target.material;
  194. if ( target.color !== undefined ) {
  195. target.color.setHex( 0xffffff * Math.random() );
  196. }
  197. }
  198. }
  199. init();
  200. buildGui();
  201. render();
  202. </script>
  203. </body>
  204. </html>