SpotLightHelper.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @author mrdoob / http://mrdoob.com/
  4. * @author WestLangley / http://github.com/WestLangley
  5. */
  6. THREE.SpotLightHelper = function ( light ) {
  7. THREE.Object3D.call( this );
  8. this.light = light;
  9. this.light.updateMatrixWorld();
  10. this.matrix = light.matrixWorld;
  11. this.matrixAutoUpdate = false;
  12. var geometry = new THREE.BufferGeometry();
  13. var positions = [];
  14. positions.push(
  15. 0, 0, 0, 0, 0, 1,
  16. 0, 0, 0, 1, 0, 1,
  17. 0, 0, 0, - 1, 0, 1,
  18. 0, 0, 0, 0, 1, 1,
  19. 0, 0, 0, 0, - 1, 1
  20. );
  21. var length = 32;
  22. for ( var i = 0; i < length; i ++ ) {
  23. var p1 = ( i / length ) * Math.PI * 2;
  24. var p2 = ( ( i + 1 ) / length ) * Math.PI * 2;
  25. positions.push( Math.cos( p1 ), Math.sin( p1 ), 1 );
  26. positions.push( Math.cos( p2 ), Math.sin( p2 ), 1 );
  27. }
  28. geometry.addAttribute( 'position', new THREE.Float32Attribute( positions, 3 ) );
  29. var material = new THREE.LineBasicMaterial( { fog: false } );
  30. this.cone = new THREE.LineSegments( geometry, material );
  31. this.add( this.cone );
  32. this.update();
  33. };
  34. THREE.SpotLightHelper.prototype = Object.create( THREE.Object3D.prototype );
  35. THREE.SpotLightHelper.prototype.constructor = THREE.SpotLightHelper;
  36. THREE.SpotLightHelper.prototype.dispose = function () {
  37. this.cone.geometry.dispose();
  38. this.cone.material.dispose();
  39. };
  40. THREE.SpotLightHelper.prototype.update = function () {
  41. var vector = new THREE.Vector3();
  42. var vector2 = new THREE.Vector3();
  43. return function () {
  44. var coneLength = this.light.distance ? this.light.distance : 1000;
  45. var coneWidth = coneLength * Math.tan( this.light.angle );
  46. this.cone.scale.set( coneWidth, coneWidth, coneLength );
  47. vector.setFromMatrixPosition( this.light.matrixWorld );
  48. vector2.setFromMatrixPosition( this.light.target.matrixWorld );
  49. this.cone.lookAt( vector2.sub( vector ) );
  50. this.cone.material.color.copy( this.light.color ).multiplyScalar( this.light.intensity );
  51. };
  52. }();