1

绘制 BufferGeometry 线时,我将索引设置为: indices = [1,2,2,3,3,4],颜色设置为:colors = [r1,g1,b1,r1,g1,b1, r2,g2,b2,r2,g2,b2, r3,g3,b3,r3,g3]。然而,颜色不会粘在片段上并超越它们,最终与下一种颜色混合。我注意到的一点是它不会绘制所有颜色,只绘制第一种颜色,就像它为每种颜色着色段半一样。我创建了一个小提琴:http: //jsfiddle.net/0f1oxdjx/

var positions = new Float32Array( MAX_POINTS * 3 ); // 3 vertices per point
var colors = new Float32Array(2*(MAX_POINTS-1)*3);
var indices = new Uint16Array(2*(MAX_POINTS-1));
var x = y = z = index = 0;

for ( var i = 0, l = MAX_POINTS; i < l; i ++ ) {
    positions[ index ++ ] = x;
    positions[ index ++ ] = y;
    positions[ index ++ ] = z;
    x += ( Math.random() - 0.5 ) * 300;
    y += ( Math.random() - 0.5 ) * 300;
    z += ( Math.random() - 0.5 ) * 300;
}
var iindex = 0, cindex = 0;
for ( var i = 1, l = MAX_POINTS; i < l; i ++ ) {
    indices[iindex++] = i-1;
    indices[iindex++] = i;
    x = ( Math.random() );
    y = ( Math.random() );
    z = ( Math.random() );
    colors[ cindex ++ ] = x;
    colors[ cindex ++ ] = y;
    colors[ cindex ++ ] = z;

    colors[ cindex ++ ] = x;
    colors[ cindex ++ ] = y;
    colors[ cindex ++ ] = z;
}
geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ));
geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
geometry.setIndex(new THREE.BufferAttribute( indices, 1 ));

// material
var material = new THREE.LineBasicMaterial({vertexColors:THREE.VertexColors, linewidth:2});    

编辑了小提琴。

4

1 回答 1

0

正如 WestLangley 指出的那样,通过使用非索引几何我能够解决问题。你可以在这里看到一个工作小提琴:

http://jsfiddle.net/ed00u25s/

positions[ index ++ ] = x;
positions[ index ++ ] = y;
positions[ index ++ ] = z;
for ( var i = 1, l = MAX_POINTS-1; i < l; i ++ ) {
    x += ( Math.random() - 0.5 ) * 300;
    y += ( Math.random() - 0.5 ) * 300;
    z += ( Math.random() - 0.5 ) * 300;
    positions[ index ++ ] = x;
    positions[ index ++ ] = y;
    positions[ index ++ ] = z;

    positions[ index ++ ] = x;
    positions[ index ++ ] = y;
    positions[ index ++ ] = z;
}
positions[ index ++ ] = x;
positions[ index ++ ] = y;
positions[ index ++ ] = z;
var iindex = 0, cindex = 0;
for ( var i = 1, l = MAX_POINTS; i < l; i ++ ) {
    x = ( Math.random() );
    y = ( Math.random() );
    z = ( Math.random() );
    colors[ cindex ++ ] = x;
    colors[ cindex ++ ] = y;
    colors[ cindex ++ ] = z;

    colors[ cindex ++ ] = x;
    colors[ cindex ++ ] = y;
    colors[ cindex ++ ] = z;
}
于 2017-01-16T17:48:06.680 回答