DirectionalLightHelper.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. *
  4. * - shows directional light color, intensity, position, orientation and target
  5. */
  6. THREE.DirectionalLightHelper = function ( light, sphereSize, arrowLength ) {
  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.sub( light.target.position, light.position );
  14. // color
  15. this.color = light.color.clone();
  16. var intensity = THREE.Math.clamp( light.intensity, 0, 1 );
  17. this.color.r *= intensity;
  18. this.color.g *= intensity;
  19. this.color.b *= intensity;
  20. var hexColor = this.color.getHex();
  21. // light helper
  22. var bulbGeometry = new THREE.SphereGeometry( sphereSize, 16, 8 );
  23. var raysGeometry = new THREE.AsteriskGeometry( sphereSize * 1.25, sphereSize * 2.25 );
  24. var bulbMaterial = new THREE.MeshBasicMaterial( { color: hexColor, fog: false } );
  25. var raysMaterial = new THREE.LineBasicMaterial( { color: hexColor, fog: false } );
  26. this.lightArrow = new THREE.ArrowHelper( this.direction, null, arrowLength, hexColor );
  27. this.lightSphere = new THREE.Mesh( bulbGeometry, bulbMaterial );
  28. this.lightArrow.cone.material.fog = false;
  29. this.lightArrow.line.material.fog = false;
  30. this.lightRays = new THREE.Line( raysGeometry, raysMaterial, THREE.LinePieces );
  31. this.add( this.lightArrow );
  32. this.add( this.lightSphere );
  33. this.add( this.lightRays );
  34. this.lightSphere.properties.isGizmo = true;
  35. this.lightSphere.properties.gizmoSubject = light;
  36. this.lightSphere.properties.gizmoRoot = this;
  37. // light target helper
  38. this.targetSphere = null;
  39. if ( light.target.properties.targetInverse ) {
  40. var targetGeo = new THREE.SphereGeometry( sphereSize, 8, 4 );
  41. var targetMaterial = new THREE.MeshBasicMaterial( { color: hexColor, wireframe: true, fog: false } );
  42. this.targetSphere = new THREE.Mesh( targetGeo, targetMaterial );
  43. this.targetSphere.position = light.target.position;
  44. this.targetSphere.properties.isGizmo = true;
  45. this.targetSphere.properties.gizmoSubject = light.target;
  46. this.targetSphere.properties.gizmoRoot = this.targetSphere;
  47. var lineMaterial = new THREE.LineDashedMaterial( { color: hexColor, dashSize: 4, gapSize: 4, opacity: 0.75, transparent: true, fog: false } );
  48. var lineGeometry = new THREE.Geometry();
  49. lineGeometry.vertices.push( this.position.clone() );
  50. lineGeometry.vertices.push( this.targetSphere.position.clone() );
  51. lineGeometry.computeLineDistances();
  52. this.targetLine = new THREE.Line( lineGeometry, lineMaterial );
  53. this.targetLine.properties.isGizmo = true;
  54. }
  55. //
  56. this.properties.isGizmo = true;
  57. }
  58. THREE.DirectionalLightHelper.prototype = Object.create( THREE.Object3D.prototype );
  59. THREE.DirectionalLightHelper.prototype.update = function () {
  60. // update arrow orientation
  61. // pointing from light to target
  62. this.direction.sub( this.light.target.position, this.light.position );
  63. this.lightArrow.setDirection( this.direction );
  64. // update arrow, spheres, rays and line colors to light color * light intensity
  65. this.color.copy( this.light.color );
  66. var intensity = THREE.Math.clamp( this.light.intensity, 0, 1 );
  67. this.color.r *= intensity;
  68. this.color.g *= intensity;
  69. this.color.b *= intensity;
  70. this.lightArrow.setColor( this.color.getHex() );
  71. this.lightSphere.material.color.copy( this.color );
  72. this.lightRays.material.color.copy( this.color );
  73. this.targetSphere.material.color.copy( this.color );
  74. this.targetLine.material.color.copy( this.color );
  75. // update target line vertices
  76. this.targetLine.geometry.vertices[ 0 ].copy( this.light.position );
  77. this.targetLine.geometry.vertices[ 1 ].copy( this.light.target.position );
  78. this.targetLine.geometry.computeLineDistances();
  79. this.targetLine.geometry.verticesNeedUpdate = true;
  80. }