SpotLightHelper.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. 0, 0, 0, 0, 0, 1,
  15. 0, 0, 0, 1, 0, 1,
  16. 0, 0, 0, - 1, 0, 1,
  17. 0, 0, 0, 0, 1, 1,
  18. 0, 0, 0, 0, - 1, 1
  19. ];
  20. for ( var i = 0, j = 1, l = 32; i < l; i ++, j ++ ) {
  21. var p1 = ( i / l ) * Math.PI * 2;
  22. var p2 = ( j / l ) * Math.PI * 2;
  23. positions.push(
  24. Math.cos( p1 ), Math.sin( p1 ), 1,
  25. Math.cos( p2 ), Math.sin( p2 ), 1
  26. );
  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. }();