webgl_lights_spotlight.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 = 8192;
  79. spt.shadow.mapSize.height = 8192;
  80. // shadow camera helper
  81. spt.shadowCameraHelper = new THREE.CameraHelper( spt.shadow.camera ); // colored lines
  82. spt.shadow.camera.near = 0.1;
  83. spt.shadow.camera.far = 20000;
  84. //spt.shadow.camera.fov = (spt.angle * (360 / Math.PI));
  85. lightHelper = new THREE.SpotLightHelper( spt );
  86. matFloor.color.set( 0x808080 );
  87. randomColor( matBox );
  88. mshFloor.receiveShadow = true;
  89. mshFloor.position.set(0, -0.05, 0);
  90. mshBox.castShadow = true;
  91. mshBox.receiveShadow = true;
  92. mshBox.position.set(40, 1.8, 0);
  93. scn.add(cam);
  94. scn.add(mshFloor);
  95. scn.add(mshBox);
  96. scn.add(amb);
  97. scn.add(spt);
  98. scn.add( spt.shadowCameraHelper );
  99. scn.add( new THREE.AxisHelper( 10 ) );
  100. scn.add( lightHelper );
  101. document.body.appendChild(rnd.domElement);
  102. onResize();
  103. window.addEventListener('resize', onResize, false);
  104. orb.addEventListener('change', render);
  105. //orb.maxPolarAngle = (Math.PI / 2);
  106. orb.update();
  107. }
  108. function onResize() {
  109. rnd.setSize(window.innerWidth, window.innerHeight);
  110. cam.aspect = (window.innerWidth / window.innerHeight);
  111. cam.updateProjectionMatrix();
  112. orb.target = mshBox.position;
  113. }
  114. function render() {
  115. lightHelper.update(); // required
  116. if ( spt.shadowCameraHelper ) spt.shadowCameraHelper.update();
  117. rnd.render(scn, cam);
  118. }
  119. function clearGui() {
  120. if ( gui ) gui.destroy();
  121. gui = new dat.GUI();
  122. gui.width = 178;
  123. var gStyle = gui.domElement.style;
  124. gStyle.position = "absolute";
  125. gStyle.top = "48px";
  126. gStyle.height = "220px";
  127. gui.open();
  128. }
  129. function buildGui() {
  130. clearGui();
  131. addGui( 'light color', spt.color.getHex(), function( val ) {
  132. spt.color.setHex( val );
  133. render();
  134. }, true );
  135. addGui( 'intensity', spt.intensity, function( val ) {
  136. spt.intensity = val;
  137. render();
  138. }, false, 0, 10 );
  139. addGui( 'distance', spt.distance, function( val ) {
  140. spt.distance = val;
  141. render();
  142. }, false, 0, 1000 );
  143. addGui( 'angle', spt.angle, function( val ) {
  144. spt.angle = val;
  145. render();
  146. }, false, 0, 1.56 );
  147. addGui( 'penumbra', spt.penumbra, function( val ) {
  148. spt.penumbra = val;
  149. render();
  150. }, false, 0, 1 );
  151. addGui( 'decay', spt.decay, function( val ) {
  152. spt.decay = val;
  153. render();
  154. }, false, 0, 100 );
  155. }
  156. function addGui( name, value, callback, isColor, min, max ) {
  157. var node;
  158. param[ name ] = value;
  159. if ( isColor ) {
  160. node = gui.addColor( param, name ).onChange( function() {
  161. callback( param[ name ] );
  162. } );
  163. }
  164. else if ( typeof value == 'object' ) {
  165. node = gui.add( param, name, value ).onChange( function() {
  166. callback( param[ name ] );
  167. } );
  168. }
  169. else {
  170. node = gui.add( param, name, min, max ).onChange( function() {
  171. callback( param[ name ] );
  172. } );
  173. }
  174. return node;
  175. }
  176. function onDocumentClick( event ) {
  177. event.preventDefault();
  178. var rndDom = rnd.domElement;
  179. if( event.type === 'mousedown' ) {
  180. mouseDown.x = ( event.clientX / rndDom.clientWidth ) * 2 - 1;
  181. mouseDown.y = - ( event.clientY / rndDom.clientHeight ) * 2 + 1;
  182. }
  183. else {
  184. mouse.x = ( event.clientX / rndDom.clientWidth ) * 2 - 1;
  185. mouse.y = - ( event.clientY / rndDom.clientHeight ) * 2 + 1;
  186. if (mouseDown.distanceTo(mouse) < 0.0075) {
  187. ray.setFromCamera( mouse, cam );
  188. var found = ray.intersectObjects( [ mshBox, mshFloor ] );
  189. if ( found.length > 0 ) {
  190. if( event.ctrlKey === false ) randomColor( found[0].object );
  191. else found[0].object.material.color.set( 0xffffff );
  192. render();
  193. }
  194. }
  195. }
  196. }
  197. function randomColor( target ) {
  198. if ( target !== undefined ) {
  199. if ( target.material !== undefined ) target = target.material;
  200. if ( target.color !== undefined ) {
  201. target.color.setHex( 0xffffff * Math.random() );
  202. }
  203. }
  204. }
  205. init();
  206. buildGui();
  207. render();
  208. </script>
  209. </body>
  210. </html>