DirectionalLightHelper.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. *
  4. * - shows directional light color, intensity, position, orientation and target
  5. */
  6. THREE.DirectionalLightHelper = function ( light, arrowLength ) {
  7. THREE.Object3D.call( this );
  8. this.light = light;
  9. this.position = light.position;
  10. this.properties.isGizmo = true;
  11. this.color = light.color.clone();
  12. this.color.r *= light.intensity;
  13. this.color.g *= light.intensity;
  14. this.color.b *= light.intensity;
  15. var hexColor = this.color.getHex();
  16. this.direction = new THREE.Vector3();
  17. this.direction.sub( light.target.position, light.position );
  18. var targetGeo = new THREE.SphereGeometry( 5, 8, 4 );
  19. var lightGeo = new THREE.SphereGeometry( 5, 16, 8 );
  20. var lightMaterial = new THREE.MeshBasicMaterial( { color: hexColor } );
  21. this.lightArrow = new THREE.ArrowHelper( this.direction, null, arrowLength, hexColor );
  22. this.lightSphere = new THREE.Mesh( lightGeo, lightMaterial );
  23. this.add( this.lightArrow );
  24. this.add( this.lightSphere );
  25. this.targetSphere = null;
  26. if ( light.target.properties.targetInverse ) {
  27. var targetMaterial = new THREE.MeshBasicMaterial( { color: hexColor, wireframe: true } );
  28. this.targetSphere = new THREE.Mesh( targetGeo, targetMaterial );
  29. this.targetSphere.position = light.target.position;
  30. this.targetSphere.properties.isGizmo = true;
  31. this.targetSphere.properties.gizmoSubject = light.target;
  32. this.targetSphere.properties.gizmoRoot = this.targetSphere;
  33. var lineMaterial = new THREE.LineDashedMaterial( { color: hexColor, dashSize: 4, gapSize: 4, opacity: 0.75, transparent: true } );
  34. var lineGeometry = new THREE.Geometry();
  35. lineGeometry.vertices.push( this.position.clone() );
  36. lineGeometry.vertices.push( this.targetSphere.position.clone() );
  37. lineGeometry.computeLineDistances();
  38. this.targetLine = new THREE.Line( lineGeometry, lineMaterial );
  39. this.targetLine.properties.isGizmo = true;
  40. }
  41. this.lightSphere.properties.isGizmo = true;
  42. this.lightSphere.properties.gizmoSubject = light;
  43. this.lightSphere.properties.gizmoRoot = this;
  44. }
  45. THREE.DirectionalLightHelper.prototype = Object.create( THREE.Object3D.prototype );
  46. THREE.DirectionalLightHelper.prototype.update = function () {
  47. // set arrow orientation
  48. // pointing from light to target
  49. this.direction.sub( this.light.target.position, this.light.position );
  50. this.lightArrow.setDirection( this.direction );
  51. // set arrow and spheres colors to light color * light intensity
  52. this.color.copy( this.light.color );
  53. var intensity = THREE.Math.clamp( this.light.intensity, 0, 1 );
  54. this.color.r *= intensity;
  55. this.color.g *= intensity;
  56. this.color.b *= intensity;
  57. this.lightArrow.setColor( this.color.getHex() );
  58. this.lightSphere.material.color.copy( this.color );
  59. this.targetSphere.material.color.copy( this.color );
  60. this.targetLine.material.color.copy( this.color );
  61. // set target line
  62. this.targetLine.geometry.vertices[ 0 ].copy( this.light.position );
  63. this.targetLine.geometry.vertices[ 1 ].copy( this.light.target.position );
  64. this.targetLine.geometry.computeLineDistances();
  65. this.targetLine.geometry.verticesNeedUpdate = true;
  66. }