在THREEJS中以60FPS显示25000行

时间:2017-09-06 15:46:39

标签: performance three.js line buffer-geometry

我在THREEJS中显示25000行,性能非常差。 (小于5fps的)

我遵循“最简单”的方法,每行创建1个网格并将其添加到场景中:

  // trks is an array that contains 25000 geometries
  var trks = ...;

  var material = new THREE.LineBasicMaterial({ vertexColors: THREE.VertexColors });

  for(var i = 0; i<trks.length; i++) {
    var line = new THREE.Line( trks[i].geometry, material);
    scene.add( line );
  }

我生成常规几何图形(对比缓冲区几何图形):

var geometry = new THREE.Geometry();
...
// push n vertices
geometry.vertices.push(new THREE.Vector3(track.points[k][0], track.points[k][1], track.points[k][2]));
...
// push n colors
geometry.colors.push(new THREE.Color(diff[0], diff[1], diff[2]));

Live JSFiddle:http://jsfiddle.net/16fnyb8k/34/

创建几何图形:https://github.com/FNNDSC/ami/blob/dev/src/loaders/loaders.trk.js#L328-L408

我应该以某种方式合并所有几何图形吗?使用缓冲区几何(会对性能产生任何影响)吗?既?

=== UPDATE ===

我使用N BufferGeometries进行了虚拟测试,但似乎没有任何影响:

  for(var i = 0; i<trks.length; i++) {

    // create buffer geometry per track
    var bufferGeometry = new THREE.BufferGeometry();
    var vertices = [];
    for(var j=0; j<trks[i].geometry.vertices.length; j++) {
      vertices.push(trks[i].geometry.vertices[j].x);
      vertices.push(trks[i].geometry.vertices[j].y);
      vertices.push(trks[i].geometry.vertices[j].z);
     }
     var fvertices = new Float32Array(vertices);
     bufferGeometry.addAttribute( 'position', new THREE.BufferAttribute( fvertices, 3 ) );

    // create mesh
    var line = new THREE.Line( bufferGeometry, material);
    line.applyMatrix(series[0].stack[0].ijk2LPS);
    scene.add( line );
  }

Live Fiddle:http://jsfiddle.net/16fnyb8k/51/

enter image description here

===更新2 ===

使用缓冲区几何图形并将所有线条合并在一起使其更加平滑,但现在所有线条都已连接:

var bufferGeometry = new THREE.BufferGeometry();
var vertices = [];
var colors = [];

for(var i = 0; i<trks.length; i++) {

  for(var j=0; j<trks[i].geometry.vertices.length; j++) {

    vertices.push(trks[i].geometry.vertices[j].x);
    vertices.push(trks[i].geometry.vertices[j].y);
    vertices.push(trks[i].geometry.vertices[j].z);

    colors.push(trks[i].geometry.colors[j].r);
    colors.push(trks[i].geometry.colors[j].g);
    colors.push(trks[i].geometry.colors[j].b);

  }

}

var fvertices = new Float32Array(vertices);
var fcolors = new Float32Array(colors);
bufferGeometry.addAttribute( 'position', new THREE.BufferAttribute( fvertices, 3 ) );
bufferGeometry.addAttribute( 'color', new THREE.BufferAttribute( fcolors, 3 ) );

var line = new THREE.Line( bufferGeometry, material);
line.applyMatrix(series[0].stack[0].ijk2LPS);
scene.add( line );

JS小提琴:http://jsfiddle.net/16fnyb8k/59/

enter image description here

由于

1 个答案:

答案 0 :(得分:0)

从缓冲区几何体创建线段。

现场演示:http://jsfiddle.net/16fnyb8k/61/

var material = new THREE.LineBasicMaterial({ vertexColors: THREE.VertexColors });
var bufferGeometry = new THREE.BufferGeometry();

var vertices = [];
var colors = [];

for(var i = 0; i<trks.length; i++) {
  for(var j=0; j<trks[i].geometry.vertices.length; j++) {

    vertices.push(trks[i].geometry.vertices[j].x);
    vertices.push(trks[i].geometry.vertices[j].y);
    vertices.push(trks[i].geometry.vertices[j].z);

    colors.push(trks[i].geometry.colors[j].r);
    colors.push(trks[i].geometry.colors[j].g);
    colors.push(trks[i].geometry.colors[j].b);

    if(j > 0 && j<trks[i].geometry.vertices.length - 1) {
      vertices.push(trks[i].geometry.vertices[j].x);
      vertices.push(trks[i].geometry.vertices[j].y);
      vertices.push(trks[i].geometry.vertices[j].z);

      colors.push(trks[i].geometry.colors[j].r);
      colors.push(trks[i].geometry.colors[j].g);
      colors.push(trks[i].geometry.colors[j].b);
    }
  }

}

var fvertices = new Float32Array(vertices);
var fcolors = new Float32Array(colors);
// itemSize = 3 because there are 3 values (components) per vertex
bufferGeometry.addAttribute( 'position', new THREE.BufferAttribute( fvertices, 3 ) );
bufferGeometry.addAttribute( 'color', new THREE.BufferAttribute( fcolors, 3 ) );
var line = new THREE.LineSegments( bufferGeometry, material);
line.applyMatrix(series[0].stack[0].ijk2LPS);
scene.add( line );