SpotLightHelper.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 raysGeometry = new THREE.AsteriskGeometry( sphereSize * 1.25, sphereSize * 2.25 );
  22. var coneGeometry = new THREE.CylinderGeometry( 0.0001, 1, 1, 8, 1, true );
  23. var coneMatrix = new THREE.Matrix4();
  24. coneMatrix.rotateX( -Math.PI/2 );
  25. coneMatrix.translate( new THREE.Vector3( 0, -0.5, 0 ) );
  26. coneGeometry.applyMatrix( coneMatrix );
  27. var bulbMaterial = new THREE.MeshBasicMaterial( { color: hexColor, fog: false } );
  28. var raysMaterial = new THREE.LineBasicMaterial( { color: hexColor, fog: false } );
  29. var coneMaterial = new THREE.MeshBasicMaterial( { color: hexColor, fog: false, wireframe: true, opacity: 0.3, transparent: true } );
  30. this.lightSphere = new THREE.Mesh( bulbGeometry, bulbMaterial );
  31. this.lightCone = new THREE.Mesh( coneGeometry, coneMaterial );
  32. var coneLength = light.distance ? light.distance : 10000;
  33. var coneWidth = coneLength * Math.tan( light.angle * 0.5 ) * 2;
  34. this.lightCone.scale.set( coneWidth, coneWidth, coneLength );
  35. this.lightRays = new THREE.Line( raysGeometry, raysMaterial, THREE.LinePieces );
  36. this.gyroscope = new THREE.Gyroscope();
  37. this.gyroscope.add( this.lightSphere );
  38. this.gyroscope.add( this.lightRays );
  39. this.add( this.gyroscope );
  40. this.add( this.lightCone );
  41. this.lookAt( light.target.position );
  42. this.lightSphere.properties.isGizmo = true;
  43. this.lightSphere.properties.gizmoSubject = light;
  44. this.lightSphere.properties.gizmoRoot = this;
  45. // light target helper
  46. this.targetSphere = null;
  47. if ( light.target.properties.targetInverse ) {
  48. var targetGeo = new THREE.SphereGeometry( sphereSize, 8, 4 );
  49. var targetMaterial = new THREE.MeshBasicMaterial( { color: hexColor, wireframe: true, fog: false } );
  50. this.targetSphere = new THREE.Mesh( targetGeo, targetMaterial );
  51. this.targetSphere.position = light.target.position;
  52. this.targetSphere.properties.isGizmo = true;
  53. this.targetSphere.properties.gizmoSubject = light.target;
  54. this.targetSphere.properties.gizmoRoot = this.targetSphere;
  55. var lineMaterial = new THREE.LineDashedMaterial( { color: hexColor, dashSize: 4, gapSize: 4, opacity: 0.75, transparent: true, fog: false } );
  56. var lineGeometry = new THREE.Geometry();
  57. lineGeometry.vertices.push( this.position.clone() );
  58. lineGeometry.vertices.push( this.targetSphere.position.clone() );
  59. lineGeometry.computeLineDistances();
  60. this.targetLine = new THREE.Line( lineGeometry, lineMaterial );
  61. this.targetLine.properties.isGizmo = true;
  62. }
  63. //
  64. this.properties.isGizmo = true;
  65. }
  66. THREE.SpotLightHelper.prototype = Object.create( THREE.Object3D.prototype );
  67. THREE.SpotLightHelper.prototype.update = function () {
  68. // update arrow orientation
  69. // pointing from light to target
  70. this.direction.subVectors( this.light.target.position, this.light.position );
  71. // update light cone orientation and size
  72. this.lookAt( this.light.target.position );
  73. var coneLength = this.light.distance ? this.light.distance : 10000;
  74. var coneWidth = coneLength * Math.tan( this.light.angle * 0.5 ) * 2;
  75. this.lightCone.scale.set( coneWidth, coneWidth, coneLength );
  76. // update arrow, spheres, rays and line colors to light color * light intensity
  77. var intensity = THREE.Math.clamp( this.light.intensity, 0, 1 );
  78. this.color.copy( this.light.color );
  79. this.color.multiplyScalar( intensity );
  80. this.lightSphere.material.color.copy( this.color );
  81. this.lightRays.material.color.copy( this.color );
  82. this.lightCone.material.color.copy( this.color );
  83. // Only update targetSphere and targetLine if available
  84. if ( this.targetSphere ) {
  85. this.targetSphere.material.color.copy( this.color );
  86. this.targetLine.material.color.copy( this.color );
  87. // update target line vertices
  88. this.targetLine.geometry.vertices[ 0 ].copy( this.light.position );
  89. this.targetLine.geometry.vertices[ 1 ].copy( this.light.target.position );
  90. this.targetLine.geometry.computeLineDistances();
  91. this.targetLine.geometry.verticesNeedUpdate = true;
  92. }
  93. };