如何访问导入的点云对象的任意属性

发布于 2025-01-16 03:04:26 字数 3241 浏览 2 评论 0原文

我正在尝试访问导入的点云模型的任何属性。 当我 console.log 函数 loader.load 中的模型时,它可以工作。第一个问题是我不知道如何在函数外获得相同的结果。我只使用最后三行代码来完成此操作,但使用不同的名称初始化对象。 我使用了 Three.js 文档中的点云模型代码,一切正常,除非我想更改点的大小。我想这是由于我的模型结构不同(附照片)。 这是我使用的代码。

import './style.css'
import * as THREE from 'three'

import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
import { PCDLoader } from 'three/examples/jsm/loaders/PCDLoader'
import { BufferAttribute, BufferGeometry } from 'three';


let camera, scene, renderer;

            init();
            render();
            

            function init() {

                renderer = new THREE.WebGLRenderer( { antialias: true } );
                renderer.setPixelRatio( window.devicePixelRatio );
                renderer.setSize( window.innerWidth, window.innerHeight );
                document.body.appendChild( renderer.domElement );

                scene = new THREE.Scene();

                camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 0.01, 40 );
                camera.position.set( 0, 0, 7 );
                scene.add( camera );

                const controls = new OrbitControls( camera, renderer.domElement );
                controls.addEventListener( 'change', render ); // use if there is no animation loop
                controls.minDistance = 0.5;
                controls.maxDistance = 10;

                //scene.add( new THREE.AxesHelper( 1 ) );

                const loader = new PCDLoader();
                loader.load( 'pointcloud.pcd', function ( points ) {

                    points.geometry.center();
                    points.geometry.rotateY( Math.PI );
                    points.geometry.rotateX( Math.PI );
                    points.geometry.rotateZ( Math.PI );
                    scene.add( points );
                    console.log(points)

                    render();
                } );

                    window.addEventListener( 'resize', onWindowResize );

                window.addEventListener( 'keypress', keyboard );
                }

            function onWindowResize() {

                camera.aspect = window.innerWidth / window.innerHeight;
                camera.updateProjectionMatrix();

                renderer.setSize( window.innerWidth, window.innerHeight );

            }
            
            function keyboard( ev ) {

                const points = scene.getObjectByProperty( 'pointcloud.pcd' );

                switch ( ev.key || String.fromCharCode( ev.keyCode || ev.charCode ) ) {

                    case '+':
                        points.material.size *= 1.2;
                        break;

                    case '-':
                        points.material.size /= 1.2;
                        break;

                }

                render();
            }

            function render() {

                renderer.render( scene, camera );

            }

            // I used this to acces the object
            const points2 = scene.getObjectByProperty( 'points' );
            console.log(points2)

在此处输入图片说明

I’m trying to access any property of my imported point-cloud model.
When I console.log the model within the function loader.load it works. The first problem is that I don’t know how to get the same result outside the function. I did it only with the last three lines of code but initializing the object with different name.
I used the code for point-cloud models from three.js docs and everything works fine except when I want to change the size of the points. I guess it is due to the different structure of my model (photo attached).
Here’s the code that I use.

import './style.css'
import * as THREE from 'three'

import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
import { PCDLoader } from 'three/examples/jsm/loaders/PCDLoader'
import { BufferAttribute, BufferGeometry } from 'three';


let camera, scene, renderer;

            init();
            render();
            

            function init() {

                renderer = new THREE.WebGLRenderer( { antialias: true } );
                renderer.setPixelRatio( window.devicePixelRatio );
                renderer.setSize( window.innerWidth, window.innerHeight );
                document.body.appendChild( renderer.domElement );

                scene = new THREE.Scene();

                camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 0.01, 40 );
                camera.position.set( 0, 0, 7 );
                scene.add( camera );

                const controls = new OrbitControls( camera, renderer.domElement );
                controls.addEventListener( 'change', render ); // use if there is no animation loop
                controls.minDistance = 0.5;
                controls.maxDistance = 10;

                //scene.add( new THREE.AxesHelper( 1 ) );

                const loader = new PCDLoader();
                loader.load( 'pointcloud.pcd', function ( points ) {

                    points.geometry.center();
                    points.geometry.rotateY( Math.PI );
                    points.geometry.rotateX( Math.PI );
                    points.geometry.rotateZ( Math.PI );
                    scene.add( points );
                    console.log(points)

                    render();
                } );

                    window.addEventListener( 'resize', onWindowResize );

                window.addEventListener( 'keypress', keyboard );
                }

            function onWindowResize() {

                camera.aspect = window.innerWidth / window.innerHeight;
                camera.updateProjectionMatrix();

                renderer.setSize( window.innerWidth, window.innerHeight );

            }
            
            function keyboard( ev ) {

                const points = scene.getObjectByProperty( 'pointcloud.pcd' );

                switch ( ev.key || String.fromCharCode( ev.keyCode || ev.charCode ) ) {

                    case '+':
                        points.material.size *= 1.2;
                        break;

                    case '-':
                        points.material.size /= 1.2;
                        break;

                }

                render();
            }

            function render() {

                renderer.render( scene, camera );

            }

            // I used this to acces the object
            const points2 = scene.getObjectByProperty( 'points' );
            console.log(points2)

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅忆流年 2025-01-23 03:04:26

因为“points”对象仅存在于函数内部,所以尝试在 loader.load 函数外部创建一个变量,然后在 render() 之前将“points”对象分配给新变量;现在您可以从该函数外部访问“点”

because the 'points' object only live inside the function, try create a variable outside the loader.load function, then assign the 'points' object to the new variable before render(); now you can access the 'points' from outside this function

天暗了我发光 2025-01-23 03:04:26

letcamera, scene, renderer; 更改为 letcamera, scene, renderer,points
使“点”全球化。

change let camera, scene, renderer; to let camera, scene, renderer, points
to make 'points' global.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文