Created
October 16, 2015 09:51
-
-
Save toruta39/65c2cba177d21db90bca to your computer and use it in GitHub Desktop.
Solve z-buffer problem of transparent Points by sorting depth of vertices on each draw
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/mrdoob/three.js/commit/8406cd7c6ecbdaca8e9d985158e5403492da48b5 | |
import THREE from 'three'; | |
export default function sortPoints(points, camera) { | |
var vector = new THREE.Vector3(); | |
// Model View Projection matrix | |
var matrix = new THREE.Matrix4(); | |
matrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse ); | |
matrix.multiply( points.matrixWorld ); | |
// | |
var geometry = points.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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment