Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何转换THREE.PointCloud对象中的顶点?_Javascript_3d_Three.js - Fatal编程技术网

Javascript 如何转换THREE.PointCloud对象中的顶点?

Javascript 如何转换THREE.PointCloud对象中的顶点?,javascript,3d,three.js,Javascript,3d,Three.js,我正在努力: 绘制一个THREE.PointCloud对象,其中约有150k个点是从web应用程序发送的 缩放THREE.PointCloud对象中的点,以获得与此类似的结果(使用MayaVi渲染): 问题在于: 传递到THREE.PointCloud对象的数据似乎不准确 在three.js中渲染时,由于未知原因,点被排列成八个立方体(我没有对点应用任何缩放或变换) 服务器响应示例(我在本文底部提供了示例数据): 用于创建点云的three.js代码: var container

我正在努力:

  • 绘制一个
    THREE.PointCloud
    对象,其中约有150k个点是从web应用程序发送的
  • 缩放
    THREE.PointCloud
    对象中的点,以获得与此类似的结果(使用
    MayaVi
    渲染):

问题在于:

  • 传递到
    THREE.PointCloud
    对象的数据似乎不准确
  • three.js
    中渲染时,由于未知原因,点被排列成八个立方体(我没有对点应用任何缩放或变换)

服务器响应示例(我在本文底部提供了示例数据):


用于创建点云的
three.js
代码:

  var container;
  var scene, camera, renderer, controls;
  var geometry, material, mesh;

  init();
  animate();

  function init() {
    scene = new THREE.Scene();
    camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 5, 5000);
    camera.position.z = 2750;

    //Add a buffer geometry for particle system
    var geometry = new THREE.BufferGeometry();
    var particles = {{ len(topology['geometry']) }};
    var geometry = new THREE.BufferGeometry();
    var positions = new Float32Array(particles * 3);
    var colors = new Float32Array(particles * 3);
    var color = new THREE.Color();

    var i = 0;
    {% for point in topology['geometry'] %}
      var x = {{ point[0] }};
      var y = {{ point[1] }};
      var z = {{ point[2] }};

      //Store the position of the point
      positions[i]     = x;
      positions[i + 1] = y;
      positions[i + 2] = z;

      //Assign a colour to the point
      color.setRGB(0.42, 0.42, 0.42);
      colors[i]     = color.r;
      colors[i + 1] = color.g;
      colors[i + 2] = color.b;
      i+=1;
    {% end %}

    geometry.addAttribute('position', new THREE.BufferAttribute(positions, 3));
    geometry.addAttribute('color', new THREE.BufferAttribute(colors, 3));
    geometry.computeBoundingSphere();

    var material = new THREE.PointCloudMaterial({ size: 15, vertexColors: THREE.VertexColors });
    particleSystem = new THREE.PointCloud(geometry, material);
    scene.add(particleSystem);

    //Lights
    light = new THREE.DirectionalLight(0xffffff);
    light.position.set(1, 1, 1);
    scene.add(light);

    //Set up renderer
    renderer = new THREE.WebGLRenderer({ antialias:false });
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.setPixelRatio(window.devicePixelRatio);

    //Attach renderer to #container DOM element
    container = document.getElementById('container');
    container.appendChild(renderer.domElement);

    //Add window listener for resize events
    window.addEventListener('resize', onWindowResize, false);

    //Call render loop
    animate();
  }

  function onWindowResize(){
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize(window.innerWidth, window.innerHeight);
    render();
  }

  function animate() {
    requestAnimationFrame(animate);
    renderer.render(scene, camera);
  }

  function render(){
    renderer.render(scene, camera);
  }
场景最终看起来是这样的:

var data = [
"-156 65 89",
"268 84 337",
"-205 68 170",
"-87 69 52",
...
];

有什么建议吗?我使用了以下示例代码,但我很难正确实现数据集中点的缩放:


链接到我正在使用的数据示例(2MB,180k行):

我使用了您的示例数据。将其放入数组中,如下所示:

var data = [
"-156 65 89",
"268 84 337",
"-205 68 170",
"-87 69 52",
...
];
并对PointCloud使用了三个.Geometry():

            var geometry = new THREE.Geometry();
            var colors = [];

            for ( var x = 0; x < data.length; x++){
                    var pointCoord = data[ x ].split(" ");
                    if ( pointCoord.length != 3 ) continue;
                    var currentColor = new THREE.Color( 0.5, 1, 0.5 );
                    colors.push( currentColor );
                    geometry.vertices.push(
                        new THREE.Vector3(
                            pointCoord[2],
                            pointCoord[1],
                            pointCoord[0]
                        )
                    );
                };
            //

            console.log( geometry.vertices.length );

            geometry.colors = colors;

            var material = new THREE.PointCloudMaterial( { size: 1, vertexColors: THREE.VertexColors } );

            particleSystem = new THREE.PointCloud( geometry, material );
            scene.add( particleSystem );
而不是

                        new THREE.Vector3(
                            pointCoord[0],
                            pointCoord[1],
                            pointCoord[2]
                        )
结果如下:


是的,示例数据中的某些行似乎不正确。意味着它们有1或2个值,而不是3个值。

你是最棒的。非常感谢!你的答案似乎很好,但对我不起作用。
                        new THREE.Vector3(
                            pointCoord[0],
                            pointCoord[1],
                            pointCoord[2]
                        )