未捕获的TypeError:无法读取未定义的属性“position”(node.js)

时间:2018-05-08 04:09:19

标签: javascript node.js three.js

目标是使用node.js 在服务器端实现3D建模。

 document.addEventListener("DOMContentLoaded", function(){

        var canvas = document.querySelector("canvas");
        var OBJselector = document.querySelector("nav select[name='obj']");
        var infos = document.querySelector("div#infos");
        var generatedImage = infos.querySelector("img#generated");

我会跳过不必要的部分。

        var renderer = new THREE.WebGLRenderer({
            canvas: canvas,
            precision: "highp",
            premultipliedAlpha: true,
            alpha: true,
            antialias: true
        });

        renderer.setSize(width, height);

        var camera = new THREE.PerspectiveCamera(75, aspect, 0.1, 10000);
        camera.position.set(0, 0, 3);

        var controls = new THREE.TrackballControls(camera, renderer.domElement);
        controls.rotateSpeed = 2.0;
        controls.zoomSpeed = 0.2;
        controls.minDistance = 0;

        window.addEventListener("resize", function( event ){

            width = window.innerWidth;
            height = window.innerHeight;

            renderer.setSize(width, height);

            camera.aspect = width / height;
            camera.updateProjectionMatrix();

        }, false);

        var scene = new THREE.Scene();

        var ambient = new THREE.AmbientLight(0x000000);
        scene.add(ambient);

        var light = new THREE.PointLight(0xFFFFFF);
        light.position.set(15, 5, 5);
        scene.add(light);

        var hemisphereLight = new THREE.HemisphereLight(0xFFFFFF, 0x001133, 1);
        scene.add(hemisphereLight);

        var object3d;

这是代码的第二部分。

function doMagicThings( fileName ){

            generatedImage.src = "models/" + fileName + "/" + fileName + ".obj.png";
            infos.style.opacity = 1;

            // Generate the THREE-JS Object3D from the image.
            var obj = new OBJImg({
                image: "models/" + fileName + "/" + fileName + ".obj.png",
                receiveShadow: false,
                castShadow: false,
                useWorker: false,
                renderer: renderer,
                onComplete: function( event ){

                    console.log("RAW datas:", event.detail, " (usefull for vanilla WebGL)");

原始数据由objimg.js输入

},
                    onError: function( event ){

                    console.error(event);

                }
            });

            scene.remove(object3d);
            controls.reset();

            object3d = obj.getObject3D();
            object3d.position.set(0, 0, 0);
            scene.add(object3d);

        };

        OBJselector.addEventListener("change", function(){

            doMagicThings(this.value);

        }, false);

        doMagicThings(OBJselector.value);

        function render( now ){

            window.requestAnimationFrame(render);

            controls.update();

            renderer.render(scene, camera);

        };

        window.requestAnimationFrame(render);

    }, false);

但是,发生以下错误

Uncaught TypeError: Cannot read property 'position' of undefined

计算3D模型的每个原始数据。但是,它不是作为3D模型创建的。 我应该如何更改此代码?

0 个答案:

没有答案