HemisphereLightHelper.js 3.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/**
 * @author alteredq / http://alteredqualia.com/
 *
 *	- shows hemisphere light intensity, sky and ground colors and directions
 */

THREE.HemisphereLightHelper = function ( light, sphereSize, arrowLength, domeSize ) {

	THREE.Object3D.call( this );

	this.light = light;

	// position

	this.position = light.position;

	//

	var intensity = THREE.Math.clamp( light.intensity, 0, 1 );

	// sky color

	this.color = light.color.clone();

	this.color.r *= intensity;
	this.color.g *= intensity;
	this.color.b *= intensity;

	var hexColor = this.color.getHex();

	// ground color

	this.groundColor = light.groundColor.clone();

	this.groundColor.r *= intensity;
	this.groundColor.g *= intensity;
	this.groundColor.b *= intensity;

	var hexColorGround = this.groundColor.getHex();

	// double colored light bulb

	var bulbGeometry = new THREE.SphereGeometry( sphereSize, 16, 8, 0, Math.PI * 2, 0, Math.PI * 0.5 );
	var bulbGroundGeometry = new THREE.SphereGeometry( sphereSize, 16, 8, 0, Math.PI * 2, Math.PI * 0.5, Math.PI );

	var bulbSkyMaterial = new THREE.MeshBasicMaterial( { color: hexColor, fog: false } );
	var bulbGroundMaterial = new THREE.MeshBasicMaterial( { color: hexColorGround, fog: false } );

	for ( var i = 0, il = bulbGeometry.faces.length; i < il; i ++ ) {

		bulbGeometry.faces[ i ].materialIndex = 0;

	}

	for ( var i = 0, il = bulbGroundGeometry.faces.length; i < il; i ++ ) {

57
		bulbGroundGeometry.faces[ i ].materialIndex = 1;
58 59 60 61 62

	}

	THREE.GeometryUtils.merge( bulbGeometry, bulbGroundGeometry );

63
	this.lightSphere = new THREE.Mesh( bulbGeometry, new THREE.MeshFaceMaterial( [ bulbSkyMaterial, bulbGroundMaterial ] ) );
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

	// arrows for sky and ground light directions

	this.lightArrow = new THREE.ArrowHelper( new THREE.Vector3( 0, 1, 0 ), new THREE.Vector3( 0, ( sphereSize + arrowLength ) * 1.1, 0 ), arrowLength, hexColor );
	this.lightArrow.rotation.x = Math.PI;

	this.lightArrowGround = new THREE.ArrowHelper( new THREE.Vector3( 0, 1, 0 ), new THREE.Vector3( 0, ( sphereSize + arrowLength ) * -1.1, 0 ), arrowLength, hexColorGround );

	var joint = new THREE.Object3D();
	joint.rotation.x = -Math.PI * 0.5;

	joint.add( this.lightSphere );
	joint.add( this.lightArrow );
	joint.add( this.lightArrowGround );

	this.add( joint );

	//

	this.lightSphere.properties.isGizmo = true;
	this.lightSphere.properties.gizmoSubject = light;
	this.lightSphere.properties.gizmoRoot = this;

	//

	this.properties.isGizmo = true;

	//

	this.target = new THREE.Vector3();
	this.lookAt( this.target );

}

THREE.HemisphereLightHelper.prototype = Object.create( THREE.Object3D.prototype );

THREE.HemisphereLightHelper.prototype.update = function () {

	// update sphere sky and ground colors to light color * light intensity

	var intensity = THREE.Math.clamp( this.light.intensity, 0, 1 );

	this.color.copy( this.light.color );
	this.groundColor.copy( this.light.groundColor );

	this.color.r *= intensity;
	this.color.g *= intensity;
	this.color.b *= intensity;

	this.groundColor.r *= intensity;
	this.groundColor.g *= intensity;
	this.groundColor.b *= intensity;

117 118
	this.lightSphere.material.materials[ 0 ].color.copy( this.color );
	this.lightSphere.material.materials[ 1 ].color.copy( this.groundColor );
119 120 121 122 123 124 125 126

	this.lightArrow.setColor( this.color.getHex() );
	this.lightArrowGround.setColor( this.groundColor.getHex() );

	this.lookAt( this.target );

}