Browse Source

Added sortPoints to webgl_custom_attributes_points2. See #6461#7097.

Mr.doob 10 years ago
parent
commit
8406cd7c6e
1 changed files with 67 additions and 2 deletions
  1. 67 2
      examples/webgl_custom_attributes_points2.html

+ 67 - 2
examples/webgl_custom_attributes_points2.html

@@ -69,8 +69,6 @@
 
 				vec4 color = vec4( color * vColor, 1.0 ) * texture2D( texture, gl_PointCoord );
 
-				if ( color.w < 0.5 ) discard;
-
 				gl_FragColor = color;
 
 			}
@@ -192,6 +190,71 @@
 
 		}
 
+		function sortPoints() {
+
+			var vector = new THREE.Vector3();
+
+			// Model View Projection matrix
+
+			var matrix = new THREE.Matrix4();
+			matrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
+			matrix.multiply( sphere.matrixWorld );
+
+			//
+
+			var geometry = sphere.geometry;
+
+			var index = geometry.getIndex();
+			var positions = geometry.getAttribute( 'position' ).array;
+			var length = positions.length / 3;
+
+			if ( index === null ) {
+
+				var array = new Uint16Array( length );
+
+				for ( var i = 0; i < length; i ++ ) {
+
+					array[ i ] = i;
+
+				}
+
+				index = new THREE.BufferAttribute( array, 1 );
+
+				geometry.setIndex( index );
+
+			}
+
+			var sortArray = [];
+
+			for ( var i = 0; i < length; i ++ ) {
+
+				vector.fromArray( positions, i * 3 );
+				vector.applyProjection( matrix );
+
+				sortArray.push( [ vector.z, i ] );
+
+			}
+
+			function numericalSort( a, b ) {
+
+				return b[ 0 ] - a[ 0 ];
+
+			}
+
+			sortArray.sort( numericalSort );
+
+			var indices = index.array;
+
+			for ( var i = 0; i < length; i ++ ) {
+
+				indices[ i ] = sortArray[ i ][ 1 ];
+
+			}
+
+			geometry.index.needsUpdate = true;
+
+		}
+
 		function animate() {
 
 			requestAnimationFrame( animate );
@@ -223,6 +286,8 @@
 
 			attributes.size.needsUpdate = true;
 
+			sortPoints();
+
 			renderer.render( scene, camera );
 
 		}