2
0
Эх сурвалжийг харах

add vertexcolors to canvasrenderer
This also has an example

OpenShift guest 12 жил өмнө
parent
commit
edd768501c

+ 261 - 0
examples/canvas_lines_colors.html

@@ -0,0 +1,261 @@
+<!DOCTYPE html>
+<html lang="en">
+	<head>
+		<title>three.js webgl - lines - cubes - colors</title>
+		<meta charset="utf-8">
+		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
+		<style>
+			body {
+				background-color: #000000;
+				margin: 0px;
+				overflow: hidden;
+			}
+
+			a {
+				color:#0078ff;
+			}
+
+			#info {
+				position: absolute;
+				top: 10px; width: 100%;
+				color: #ffffff;
+				padding: 5px;
+				font-family: Monospace;
+				font-size: 13px;
+				text-align: center;
+				z-index:100;
+			}
+
+			a {
+				color: orange;
+				text-decoration: none;
+			}
+
+			a:hover {
+				color: #0080ff;
+			}
+
+		</style>
+	</head>
+	<body>
+
+		<div id="info">
+			<a href="http://threejs.org" target="_blank">three.js</a> - color lines WebGL demo
+			[<a href="http://en.wikipedia.org/wiki/Hilbert_curve">Hilbert curve</a> thanks to <a href="http://www.openprocessing.org/visuals/?visualID=15599">Thomas Diewald</a>]
+		</div>
+
+		<script src="../build/three.min.js"></script>
+
+		<script src="js/Detector.js"></script>
+		<script src="js/libs/stats.min.js"></script>
+
+		<script>
+
+			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
+
+
+			var mouseX = 0, mouseY = 0,
+
+			windowHalfX = window.innerWidth / 2,
+			windowHalfY = window.innerHeight / 2,
+
+			camera, scene, renderer, material;
+
+			init();
+			animate();
+
+			function init() {
+
+				var i, container;
+
+				container = document.createElement( 'div' );
+				document.body.appendChild( container );
+
+				camera = new THREE.PerspectiveCamera( 33, window.innerWidth / window.innerHeight, 1, 10000 );
+				camera.position.z = 700;
+
+				scene = new THREE.Scene();
+
+				renderer = new THREE.CanvasRenderer( { clearColor: 0x000000, clearAlpha: 1, antialias: false } );
+				renderer.setSize( window.innerWidth, window.innerHeight );
+
+				container.appendChild( renderer.domElement );
+
+				var geometry3 = new THREE.Geometry(),
+					points = hilbert3D( new THREE.Vector3( 0,0,0 ), 200.0, 2, 0, 1, 2, 3, 4, 5, 6, 7 ),
+					colors3 = [];
+
+				for ( i = 0; i < points.length; i ++ ) {
+
+					geometry3.vertices.push( points[ i ] );
+
+					colors3[ i ] = new THREE.Color( 0xffffff );
+					colors3[ i ].setHSL( i / points.length, 1.0, 0.5 );
+
+				}
+
+				geometry3.colors = colors3;
+
+				// lines
+
+				material = new THREE.LineBasicMaterial( { color: 0xffffff, opacity: 1, linewidth: 3, vertexColors: THREE.VertexColors } );
+
+				var line, p, scale = 0.3, d = 225;
+
+				line = new THREE.Line(geometry3, material );
+				line.scale.x = line.scale.y = line.scale.z =  scale*1.5;
+				line.position.x = 0;
+				line.position.y = 0;
+				line.position.z = 0;
+				scene.add( line );
+                
+                var axis = new THREE.AxisHelper(100);
+                axis.position.x = -d;
+                scene.add(axis)
+
+
+				//
+
+				stats = new Stats();
+				stats.domElement.style.position = 'absolute';
+				stats.domElement.style.top = '0px';
+				//container.appendChild( stats.domElement );
+
+				//
+
+				document.addEventListener( 'mousemove', onDocumentMouseMove, false );
+				document.addEventListener( 'touchstart', onDocumentTouchStart, false );
+				document.addEventListener( 'touchmove', onDocumentTouchMove, false );
+
+				//
+
+				window.addEventListener( 'resize', onWindowResize, false );
+
+			}
+
+			function onWindowResize() {
+
+				windowHalfX = window.innerWidth / 2;
+				windowHalfY = window.innerHeight / 2;
+
+				camera.aspect = window.innerWidth / window.innerHeight;
+				camera.updateProjectionMatrix();
+
+				renderer.setSize( window.innerWidth, window.innerHeight );
+
+				effectFXAA.uniforms[ 'resolution' ].value.set( 1 / window.innerWidth, 1 / window.innerHeight );
+
+				composer.reset();
+
+			}
+
+			// port of Processing Java code by Thomas Diewald
+			// http://www.openprocessing.org/visuals/?visualID=15599
+
+			function hilbert3D( center, side, iterations, v0, v1, v2, v3, v4, v5, v6, v7 ) {
+
+				var half = side / 2,
+
+					vec_s = [
+
+					new THREE.Vector3( center.x - half, center.y + half, center.z - half ),
+					new THREE.Vector3( center.x - half, center.y + half, center.z + half ),
+					new THREE.Vector3( center.x - half, center.y - half, center.z + half ),
+					new THREE.Vector3( center.x - half, center.y - half, center.z - half ),
+					new THREE.Vector3( center.x + half, center.y - half, center.z - half ),
+					new THREE.Vector3( center.x + half, center.y - half, center.z + half ),
+					new THREE.Vector3( center.x + half, center.y + half, center.z + half ),
+					new THREE.Vector3( center.x + half, center.y + half, center.z - half )
+
+					],
+
+					vec = [ vec_s[ v0 ], vec_s[ v1 ], vec_s[ v2 ], vec_s[ v3 ], vec_s[ v4 ], vec_s[ v5 ], vec_s[ v6 ], vec_s[ v7 ] ];
+
+				if( --iterations >= 0 ) {
+
+					var tmp = [];
+
+					Array.prototype.push.apply( tmp, hilbert3D ( vec[ 0 ], half, iterations, v0, v3, v4, v7, v6, v5, v2, v1 ) );
+					Array.prototype.push.apply( tmp, hilbert3D ( vec[ 1 ], half, iterations, v0, v7, v6, v1, v2, v5, v4, v3 ) );
+					Array.prototype.push.apply( tmp, hilbert3D ( vec[ 2 ], half, iterations, v0, v7, v6, v1, v2, v5, v4, v3 ) );
+					Array.prototype.push.apply( tmp, hilbert3D ( vec[ 3 ], half, iterations, v2, v3, v0, v1, v6, v7, v4, v5 ) );
+					Array.prototype.push.apply( tmp, hilbert3D ( vec[ 4 ], half, iterations, v2, v3, v0, v1, v6, v7, v4, v5 ) );
+					Array.prototype.push.apply( tmp, hilbert3D ( vec[ 5 ], half, iterations, v4, v3, v2, v5, v6, v1, v0, v7 ) );
+					Array.prototype.push.apply( tmp, hilbert3D ( vec[ 6 ], half, iterations, v4, v3, v2, v5, v6, v1, v0, v7 ) );
+					Array.prototype.push.apply( tmp, hilbert3D ( vec[ 7 ], half, iterations, v6, v5, v2, v1, v0, v3, v4, v7 ) );
+
+					return tmp;
+
+				}
+
+				return vec;
+			}
+
+			//
+
+			function onDocumentMouseMove( event ) {
+
+				mouseX = event.clientX - windowHalfX;
+				mouseY = event.clientY - windowHalfY;
+
+			}
+
+			function onDocumentTouchStart( event ) {
+
+				if ( event.touches.length > 1 ) {
+
+					event.preventDefault();
+
+					mouseX = event.touches[ 0 ].pageX - windowHalfX;
+					mouseY = event.touches[ 0 ].pageY - windowHalfY;
+
+				}
+
+			}
+
+			function onDocumentTouchMove( event ) {
+
+				if ( event.touches.length == 1 ) {
+
+					event.preventDefault();
+
+					mouseX = event.touches[ 0 ].pageX - windowHalfX;
+					mouseY = event.touches[ 0 ].pageY - windowHalfY;
+				}
+
+			}
+
+			//
+
+			function animate() {
+
+				requestAnimationFrame( animate );
+				render();
+
+				stats.update();
+
+			}
+
+			function render() {
+
+				camera.position.x += ( mouseX - camera.position.x ) * .05;
+				camera.position.y += ( - mouseY + 200 - camera.position.y ) * .05;
+
+				camera.lookAt( scene.position );
+
+				var time = Date.now() * 0.0005;
+
+				for ( var i = 0; i < scene.children.length; i ++ ) {
+
+					var object = scene.children[ i ];
+					if ( object instanceof THREE.Line ) object.rotation.y = time * ( i % 2 ? 1 : -1 );
+
+				}
+
+				renderer.render(scene, camera );
+
+			}
+
+		</script>
+	</body>
+</html>

+ 7 - 1
src/core/Projector.js

@@ -428,7 +428,13 @@ THREE.Projector = function () {
 						_line.z = Math.max( _clippedVertex1PositionScreen.z, _clippedVertex2PositionScreen.z );
 
 						_line.material = object.material;
-
+                        
+                        if (object.material.vertexColors === THREE.VertexColors){
+                            
+                            _line.vertexColors[0].set(object.geometry.colors[v]);
+                            _line.vertexColors[1].set(object.geometry.colors[v-1]);
+                            
+                        }
 						_renderData.elements.push( _line );
 
 					}

+ 35 - 9
src/renderers/CanvasRenderer.js

@@ -429,7 +429,7 @@ THREE.CanvasRenderer = function ( parameters ) {
 
 					_pointLights.add( lightColor );
 
-				}
+				}setStrokeStyle
 
 			}
 
@@ -602,14 +602,40 @@ THREE.CanvasRenderer = function ( parameters ) {
 			_context.lineTo( v2.positionScreen.x, v2.positionScreen.y );
 
 			if ( material instanceof THREE.LineBasicMaterial ) {
-
-				setLineWidth( material.linewidth );
-				setLineCap( material.linecap );
-				setLineJoin( material.linejoin );
-				setStrokeStyle( material.color.getStyle() );
-
-				_context.stroke();
-				_elemBox.expandByScalar( material.linewidth * 2 );
+                
+                setLineWidth( material.linewidth );
+    			setLineCap( material.linecap );
+    			setLineJoin( material.linejoin );
+                
+                if (material.vertexColors !== THREE.VertexColors){
+
+    				setStrokeStyle( material.color.getStyle() );
+    
+
+                } else {
+                
+                    var colorStyle1 = element.vertexColors[0].getStyle();
+                    var colorStyle2 = element.vertexColors[1].getStyle();
+                    
+                    if (colorStyle1 === colorStyle2){
+                        
+                        setStrokeStyle( colorStyle1 );
+                        
+                    }else {
+                
+                        var grad= _context.createLinearGradient(v1.positionScreen.x, 
+                                                            v1.positionScreen.y, 
+                                                            v2.positionScreen.x, 
+                                                            v2.positionScreen.y);
+                        grad.addColorStop(0, colorStyle1);
+                        grad.addColorStop(1, colorStyle2);
+                        setStrokeStyle(grad);
+                        
+                    }
+                    
+			    }
+                _context.stroke();
+    			_elemBox.expandByScalar( material.linewidth * 2 );
 
 			} else if ( material instanceof THREE.LineDashedMaterial ) {
 

+ 2 - 0
src/renderers/renderables/RenderableLine.js

@@ -9,6 +9,8 @@ THREE.RenderableLine = function () {
 	this.v1 = new THREE.RenderableVertex();
 	this.v2 = new THREE.RenderableVertex();
 
+
+    this.vertexColors = [ new THREE.Color(), new THREE.Color()];
 	this.material = null;
 
 };