【问题标题】:three.js GLTFLoader is not displaying the model in React and giving infinite% loaded in consolethree.js GLTFLoader 没有在 React 中显示模型并在控制台中加载了无限%
【发布时间】:2021-05-06 20:31:24
【问题描述】:

我正在尝试将 GLTF 模型加载到反应网页中。我已经检查了 gltf 文件在多个网站上是否有效,并且确实有效。如果您查看图片,我已经发布了错误,指出已加载无限%。我到处搜索,我现在真的不知道出了什么问题。我从 npm 安装了三个,也导入了 GLTF 加载器,但没有任何运气。谁能告诉我我做错了什么? Click here to see the error on the console

import React, { Component } from 'react';
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader';

import filePath from './resources/thing.glb';

export default class Viewer extends Component {
    componentDidMount() {
        //Add Scene

        this.scene = new THREE.Scene();

        //Add Renderer
        this.renderer = new THREE.WebGLRenderer({ antialias: true });
        this.renderer.setClearColor('#808080');
        this.renderer.shadowMap.enabled = true;
        this.renderer.shadowMap.type = THREE.PCFSoftShadowMap;
        this.renderer.setPixelRatio(window.devicePixelRatio);
        this.renderer.setSize(window.innerWidth, window.innerHeight);
        this.mount.appendChild(this.renderer.domElement);

        //Add Camera
        const fov = 60;
        const aspect = 1920 / 1080;
        const near = 1.0;
        const far = 1000.0;
        this.camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
        this.camera.position.set(75, 20, 0);

        //After loading GLTF file resize

        // //Add Geometry
        // const cubeGeometry = new THREE.BoxGeometry(5, 5, 5);
        // const material = new THREE.MeshBasicMaterial({ color: '#0F0' });

        // //Add mesh which is also the model
        // this.cubeMesh = new THREE.Mesh(cubeGeometry, material);
        // this.scene.add(this.cubeMesh);

        //Load GLTF file
        // Instantiate a loader
        const loader = new GLTFLoader();

        
        /// Load a glTF resource
loader.load(
    // resource URL
    filePath,
    // called when the resource is loaded
    function ( gltf ) {

        this.scene.add( gltf.scene );

        
    },
    // called while loading is progressing
    function ( xhr ) {

        console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );

    },
    // called when loading has errors
    function ( error ) {

        console.log( 'An error happened' );

    }
);

        //Settings
        //Add Camera Controls
        const controls = new OrbitControls(this.camera, this.renderer.domElement);
        controls.update();

        ///Add AMBIENT LIGHT
        let light = new THREE.DirectionalLight(0xffffff, 1.0);
        light.position.set(20, 100, 10);
        light.target.position.set(0, 0, 0);
        light.castShadow = true;
        light.shadow.bias = -0.001;
        light.shadow.mapSize.width = 2048;
        light.shadow.mapSize.height = 2048;
        light.shadow.camera.near = 0.1;
        light.shadow.camera.far = 500.0;
        light.shadow.camera.near = 0.5;
        light.shadow.camera.far = 500.0;
        light.shadow.camera.left = 100;
        light.shadow.camera.right = -100;
        light.shadow.camera.top = 100;
        light.shadow.camera.bottom = -100;
        this.scene.add(light);
        light = new THREE.AmbientLight(0xffffff, 4.0);
        this.scene.add(light);

        //Start animation
        this.start();
    }

    //Unmount when animation has stopped
    componentWillUnmount() {
        this.stop();
        this.mount.removeChild(this.renderer.domElement);
    }

    //Function to start animation
    start = () => {
        //Rotate Models
        if (!this.frameId) {
            this.frameId = requestAnimationFrame(this.animate);
        }
    };

    //Function to stop animation
    stop = () => {
        cancelAnimationFrame(this.frameId);
    };

    //Animate models here
    animate = () => {
        //ReDraw scene with camera and scene object
        if (this.cubeMesh) this.cubeMesh.rotation.y += 0.01;
        this.renderScene();
        this.frameId = window.requestAnimationFrame(this.animate);
    };

    //Render the scene
    renderScene = () => {
        if (this.renderer) this.renderer.render(this.scene, this.camera);
    };

    render() {
        return (
            <div
                style={{ width: '800px', height: '800px' }}
                ref={(mount) => {
                    this.mount = mount;
                }}
            />
        );
    }
}

【问题讨论】:

  • 在错误函数中,您可以登录error 以获取有关错误本身的更多详细信息吗?
  • 嗨@mglonnro。我在控制台中收到以下错误。 *TypeError:无法在 GLTFLoader.js:176 在 GLTFLoader.js:1988 处读取 Viewer.js:53 处未定义的属性“场景”
  • 好吧,酷!我在下面的一个单独的答案中写了一些更改应该可以工作(至少在 ES6 中)。

标签: javascript reactjs three.js gltf


【解决方案1】:

在加载程序回调中,this 未绑定/可用。您可以尝试以下重写:


    loader.load(
        filePath,
        (gltf) => {
            this.scene.add( gltf.scene );
        },
        (xhr) => {
            console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
        },
        (error) => {
            console.log( 'An error happened' );
        }
    );

【讨论】:

    猜你喜欢
    • 2021-07-20
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    • 2020-08-02
    • 2021-02-01
    • 2014-12-28
    相关资源
    最近更新 更多