SpotLightHelper.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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.matrixWorld = light.matrixWorld;
  11. this.matrixAutoUpdate = false;
  12. var geometry = new THREE.CylinderGeometry( 0, 1, 1, 8, 1, true );
  13. geometry.applyMatrix( new THREE.Matrix4().makeTranslation( 0, -0.5, 0 ) );
  14. geometry.applyMatrix( new THREE.Matrix4().makeRotationX( - Math.PI / 2 ) );
  15. var material = new THREE.MeshBasicMaterial( { wireframe: true, fog: false } );
  16. this.cone = new THREE.Mesh( geometry, material );
  17. this.add( this.cone );
  18. this.update();
  19. };
  20. THREE.SpotLightHelper.prototype = Object.create( THREE.Object3D.prototype );
  21. THREE.SpotLightHelper.prototype.dispose = function () {
  22. this.cone.geometry.dispose();
  23. this.cone.material.dispose();
  24. };
  25. THREE.SpotLightHelper.prototype.update = function () {
  26. var vector = new THREE.Vector3();
  27. var vector2 = new THREE.Vector3();
  28. return function () {
  29. var coneLength = this.light.distance ? this.light.distance : 10000;
  30. var coneWidth = coneLength * Math.tan( this.light.angle );
  31. this.cone.scale.set( coneWidth, coneWidth, coneLength );
  32. vector.setFromMatrixPosition( this.light.matrixWorld );
  33. vector2.setFromMatrixPosition( this.light.target.matrixWorld );
  34. this.cone.lookAt( vector2.sub( vector ) );
  35. this.cone.material.color.copy( this.light.color ).multiplyScalar( this.light.intensity );
  36. };
  37. }();