瀏覽代碼

HemisphereLightHelper: Refactoring

Mugen87 8 年之前
父節點
當前提交
8494767bd1
共有 2 個文件被更改,包括 36 次插入25 次删除
  1. 1 4
      docs/api/extras/helpers/HemisphereLightHelper.html
  2. 35 21
      src/extras/helpers/HemisphereLightHelper.js

+ 1 - 4
docs/api/extras/helpers/HemisphereLightHelper.html

@@ -32,7 +32,7 @@ scene.add( helper );
 		<h3>[name]([page:HemisphereLight light], [page:Number sphereSize])</h3>
 		<div>
 		[page:HemisphereLight light] -- The light being visualized. <br />
-		[page:Number sphereSize] -- The size of the sphere used to visualize te light.
+		[page:Number size] -- The size of the mesh used to visualize the light.
 		</div>
 
 
@@ -42,9 +42,6 @@ scene.add( helper );
 		<h3>[property:HemisphereLight light]</h3>
 		<div>Reference to the HemisphereLight being visualized.</div>
 
-		<h3>[property:Mesh lightSphere]</h3>
-		<div>The sphere mesh that shows the location of the hemispherelight.</div>
-
 		<h3>[property:object matrix]</h3>
 		<div>Reference to the hemisphereLight's [page:Object3D.matrixWorld matrixWorld].</div>
 

+ 35 - 21
src/extras/helpers/HemisphereLightHelper.js

@@ -1,17 +1,19 @@
 import { Vector3 } from '../../math/Vector3';
+import { Color } from '../../math/Color';
 import { Object3D } from '../../core/Object3D';
 import { Mesh } from '../../objects/Mesh';
-import { FaceColors } from '../../constants';
+import { VertexColors } from '../../constants';
 import { MeshBasicMaterial } from '../../materials/MeshBasicMaterial';
-import { SphereGeometry } from '../../geometries/SphereGeometry';
-import { Color } from '../../math/Color';
+import { OctahedronBufferGeometry } from '../../geometries/OctahedronBufferGeometry';
+import { BufferAttribute } from '../../core/BufferAttribute';
 
 /**
  * @author alteredq / http://alteredqualia.com/
  * @author mrdoob / http://mrdoob.com/
+ * @author Mugen87 / https://github.com/Mugen87
  */
 
-function HemisphereLightHelper( light, sphereSize ) {
+function HemisphereLightHelper( light, size ) {
 
 	Object3D.call( this );
 
@@ -21,21 +23,17 @@ function HemisphereLightHelper( light, sphereSize ) {
 	this.matrix = light.matrixWorld;
 	this.matrixAutoUpdate = false;
 
-	this.colors = [ new Color(), new Color() ];
-
-	var geometry = new SphereGeometry( sphereSize, 4, 2 );
-	geometry.rotateX( - Math.PI / 2 );
+	var geometry = new OctahedronBufferGeometry( size );
+	geometry.rotateY( Math.PI * 0.5 );
 
-	for ( var i = 0, il = 8; i < il; i ++ ) {
+	var material = new MeshBasicMaterial( { vertexColors: VertexColors, wireframe: true } );
 
-		geometry.faces[ i ].color = this.colors[ i < 4 ? 0 : 1 ];
+	var position = geometry.getAttribute( 'position' );
+	var colors = new Float32Array( position.count * 3 );
 
-	}
+	geometry.addAttribute( 'color', new BufferAttribute( colors, 3 ) );
 
-	var material = new MeshBasicMaterial( { vertexColors: FaceColors, wireframe: true } );
-
-	this.lightSphere = new Mesh( geometry, material );
-	this.add( this.lightSphere );
+	this.add( new Mesh( geometry, material ) );
 
 	this.update();
 
@@ -46,8 +44,8 @@ HemisphereLightHelper.prototype.constructor = HemisphereLightHelper;
 
 HemisphereLightHelper.prototype.dispose = function () {
 
-	this.lightSphere.geometry.dispose();
-	this.lightSphere.material.dispose();
+	this.children[ 0 ].geometry.dispose();
+	this.children[ 0 ].material.dispose();
 
 };
 
@@ -55,13 +53,29 @@ HemisphereLightHelper.prototype.update = function () {
 
 	var vector = new Vector3();
 
+	var color1 = new Color();
+	var color2 = new Color();
+
 	return function update() {
 
-		this.colors[ 0 ].copy( this.light.color ).multiplyScalar( this.light.intensity );
-		this.colors[ 1 ].copy( this.light.groundColor ).multiplyScalar( this.light.intensity );
+		var mesh = this.children[ 0 ];
+
+		var colors = mesh.geometry.getAttribute( 'color' );
+
+		color1.copy( this.light.color ).multiplyScalar( this.light.intensity );
+		color2.copy( this.light.groundColor ).multiplyScalar( this.light.intensity );
+
+		for ( var i = 0, l = colors.count; i < l; i ++ ) {
+
+			var color = ( i < ( l / 2 ) ) ? color1 : color2;
+
+			colors.setXYZ( i, color.r, color.g, color.b );
+
+		}
+
+		mesh.lookAt( vector.setFromMatrixPosition( this.light.matrixWorld ).negate() );
 
-		this.lightSphere.lookAt( vector.setFromMatrixPosition( this.light.matrixWorld ).negate() );
-		this.lightSphere.geometry.colorsNeedUpdate = true;
+		colors.needsUpdate = true;
 
 	};