Browse Source

Merge remote-tracking branch 'verma/configurable-colors-for-grid-helper' into dev

Mr.doob 12 years ago
parent
commit
025ae3ed38
2 changed files with 113 additions and 2 deletions
  1. 100 0
      examples/webgl_grid_helper.html
  2. 13 2
      src/extras/helpers/GridHelper.js

+ 100 - 0
examples/webgl_grid_helper.html

@@ -0,0 +1,100 @@
+<!DOCTYPE html>
+<html lang="en">
+	<head>
+		<title>three.js webgl - grid helper</title>
+		<meta charset="utf-8">
+		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
+		<style>
+			html, body {
+				color: #808080;
+				font-family:Monospace;
+				font-size:13px;
+				text-align:center;
+
+				background-color: #fff;
+				margin: 0px;
+				overflow: hidden;
+				width: 100%;
+				height: 100%;
+			}
+
+			#container {
+				text-align: center;
+			}
+
+		</style>
+	</head>
+	<body>
+		<div id="container">
+		</div>
+		<div id="info"><a href="http://threejs.org" target="_blank">three.js</a> webgl - grid helper</div>
+
+		<script src="../build/three.min.js"></script>
+
+		<script src="js/Detector.js"></script>
+		<script src="js/controls/TrackballControls.js"></script>
+		<script src="js/libs/stats.min.js"></script>
+
+		<script>
+
+			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
+
+			init();
+			animate();
+
+			var controls, camera, scene, renderer;
+			var gh;
+
+			function init() {
+
+				container = document.getElementById( "container" );
+
+				camera = new THREE.PerspectiveCamera( 60, 640 / 480.0, 1, 10000 );
+				camera.position.set( 150, 150, 150 );
+
+				scene = new THREE.Scene();
+
+				gh = new THREE.GridHelper( 100, 10 );
+				scene.add( gh );
+
+				controls = new THREE.TrackballControls( camera );
+
+				renderer = new THREE.WebGLRenderer();
+				renderer.setSize( 640, 480 );
+
+				container.appendChild( renderer.domElement );
+
+			}
+
+			function time() {
+
+				return Date.now() * 0.001;
+			}
+
+			function randomColor() {
+
+				return Math.floor(Math.random() * 0xFFFFFF) + 1;
+			}
+
+			var lastChangeTime = time()
+			function animate() {
+
+				var t = time();
+				if (t - lastChangeTime > 5.0) {
+					gh.setColors( randomColor(), randomColor() );
+					lastChangeTime = t;
+				}
+
+				render();
+				requestAnimationFrame( animate );
+			}
+
+			function render() {
+
+				controls.update();
+				renderer.render( scene, camera );
+			}
+		</script>
+
+	</body>
+</html>

+ 13 - 2
src/extras/helpers/GridHelper.js

@@ -6,7 +6,9 @@ THREE.GridHelper = function ( size, step ) {
 
 
 	var geometry = new THREE.Geometry();
 	var geometry = new THREE.Geometry();
 	var material = new THREE.LineBasicMaterial( { vertexColors: THREE.VertexColors } );
 	var material = new THREE.LineBasicMaterial( { vertexColors: THREE.VertexColors } );
-	var color1 = new THREE.Color( 0x444444 ), color2 = new THREE.Color( 0x888888 );
+
+	this.color1 = new THREE.Color( 0x444444 );
+	this.color2 = new THREE.Color( 0x888888 );
 
 
 	for ( var i = - size; i <= size; i += step ) {
 	for ( var i = - size; i <= size; i += step ) {
 
 
@@ -15,7 +17,7 @@ THREE.GridHelper = function ( size, step ) {
 			new THREE.Vector3( i, 0, - size ), new THREE.Vector3( i, 0, size )
 			new THREE.Vector3( i, 0, - size ), new THREE.Vector3( i, 0, size )
 		);
 		);
 
 
-		var color = i === 0 ? color1 : color2;
+		var color = i === 0 ? this.color1 : this.color2;
 
 
 		geometry.colors.push( color, color, color, color );
 		geometry.colors.push( color, color, color, color );
 
 
@@ -26,3 +28,12 @@ THREE.GridHelper = function ( size, step ) {
 };
 };
 
 
 THREE.GridHelper.prototype = Object.create( THREE.Line.prototype );
 THREE.GridHelper.prototype = Object.create( THREE.Line.prototype );
+
+THREE.GridHelper.prototype.setColors = function( colorCenterLine, colorGrid ) {
+
+	this.color1.set( colorCenterLine );
+	this.color2.set( colorGrid );
+
+	this.geometry.colorsNeedUpdate = true;
+
+}