SpotLightHelper.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. *
  4. * - shows spot light color, intensity, position, orientation, light cone and target
  5. */
  6. THREE.SpotLightHelper = function ( light, sphereSize ) {
  7. THREE.Object3D.call( this );
  8. this.light = light;
  9. // position
  10. this.position = light.position;
  11. // direction
  12. this.direction = new THREE.Vector3();
  13. this.direction.subVectors( light.target.position, light.position );
  14. // color
  15. var intensity = THREE.Math.clamp( light.intensity, 0, 1 );
  16. this.color = light.color.clone();
  17. this.color.multiplyScalar( intensity );
  18. var hexColor = this.color.getHex();
  19. // light helper
  20. var bulbGeometry = new THREE.SphereGeometry( sphereSize, 16, 8 );
  21. var coneGeometry = new THREE.CylinderGeometry( 0.0001, 1, 1, 8, 1, true );
  22. var coneMatrix = new THREE.Matrix4();
  23. coneMatrix.rotateX( -Math.PI/2 );
  24. coneMatrix.translate( new THREE.Vector3( 0, -0.5, 0 ) );
  25. coneGeometry.applyMatrix( coneMatrix );
  26. var bulbMaterial = new THREE.MeshBasicMaterial( { color: hexColor, fog: false } );
  27. var coneMaterial = new THREE.MeshBasicMaterial( { color: hexColor, fog: false, wireframe: true, opacity: 0.3, transparent: true } );
  28. this.lightSphere = new THREE.Mesh( bulbGeometry, bulbMaterial );
  29. this.lightCone = new THREE.Mesh( coneGeometry, coneMaterial );
  30. var coneLength = light.distance ? light.distance : 10000;
  31. var coneWidth = coneLength * Math.tan( light.angle * 0.5 ) * 2;
  32. this.lightCone.scale.set( coneWidth, coneWidth, coneLength );
  33. this.gyroscope = new THREE.Gyroscope();
  34. this.gyroscope.add( this.lightSphere );
  35. this.add( this.gyroscope );
  36. this.add( this.lightCone );
  37. this.lookAt( light.target.position );
  38. this.lightSphere.userData.isGizmo = true;
  39. this.lightSphere.userData.gizmoSubject = light;
  40. this.lightSphere.userData.gizmoRoot = this;
  41. // light target helper
  42. this.targetSphere = null;
  43. if ( light.target.userData.targetInverse !== undefined ) {
  44. var targetGeo = new THREE.SphereGeometry( sphereSize, 8, 4 );
  45. var targetMaterial = new THREE.MeshBasicMaterial( { color: hexColor, wireframe: true, fog: false } );
  46. this.targetSphere = new THREE.Mesh( targetGeo, targetMaterial );
  47. this.targetSphere.position = light.target.position;
  48. this.targetSphere.userData.isGizmo = true;
  49. this.targetSphere.userData.gizmoSubject = light.target;
  50. this.targetSphere.userData.gizmoRoot = this.targetSphere;
  51. var lineMaterial = new THREE.LineDashedMaterial( { color: hexColor, dashSize: 4, gapSize: 4, opacity: 0.75, transparent: true, fog: false } );
  52. var lineGeometry = new THREE.Geometry();
  53. lineGeometry.vertices.push( this.position.clone() );
  54. lineGeometry.vertices.push( this.targetSphere.position.clone() );
  55. lineGeometry.computeLineDistances();
  56. this.targetLine = new THREE.Line( lineGeometry, lineMaterial );
  57. this.targetLine.userData.isGizmo = true;
  58. }
  59. //
  60. this.userData.isGizmo = true;
  61. }
  62. THREE.SpotLightHelper.prototype = Object.create( THREE.Object3D.prototype );
  63. THREE.SpotLightHelper.prototype.update = function () {
  64. // update arrow orientation
  65. // pointing from light to target
  66. this.direction.subVectors( this.light.target.position, this.light.position );
  67. // update light cone orientation and size
  68. this.lookAt( this.light.target.position );
  69. var coneLength = this.light.distance ? this.light.distance : 10000;
  70. var coneWidth = coneLength * Math.tan( this.light.angle * 0.5 ) * 2;
  71. this.lightCone.scale.set( coneWidth, coneWidth, coneLength );
  72. // update arrow, spheres and line colors to light color * light intensity
  73. var intensity = THREE.Math.clamp( this.light.intensity, 0, 1 );
  74. this.color.copy( this.light.color );
  75. this.color.multiplyScalar( intensity );
  76. this.lightSphere.material.color.copy( this.color );
  77. this.lightCone.material.color.copy( this.color );
  78. // Only update targetSphere and targetLine if available
  79. if ( this.targetSphere !== null ) {
  80. this.targetSphere.material.color.copy( this.color );
  81. this.targetLine.material.color.copy( this.color );
  82. // update target line vertices
  83. this.targetLine.geometry.vertices[ 0 ].copy( this.light.position );
  84. this.targetLine.geometry.vertices[ 1 ].copy( this.light.target.position );
  85. this.targetLine.geometry.computeLineDistances();
  86. this.targetLine.geometry.verticesNeedUpdate = true;
  87. }
  88. };