yusq456 2021-02-26 15:47 采纳率: 40%
浏览 519
已结题

react项目使用three.js导入obj模型不成功,不清楚原因,请大佬们帮忙看看

react项目使用three.js导入obj模型不成功

代码如下:

import React, { Component } from 'react';
import * as THREE from 'three';
// @ts-ignore
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader';
import { RGBELoader } from 'three/examples/jsm/loaders/RGBELoader';
import { RoughnessMipmapper } from 'three/examples/jsm/utils/RoughnessMipmapper';
import { OBJLoader } from 'three/examples/jsm/loaders/OBJLoader';
import { MTLLoader } from 'three/examples/jsm/loaders/MTLLoader';
import {FBXLoader} from 'three/examples/jsm/loaders/FBXLoader';



class GroundPressuer extends Component {
  constructor(props: {} | Readonly<{}>) {
    super(props);
    this.state = {};
  }

  initThree() {
    threeStart();

    var renderer: THREE.WebGLRenderer, width: number, height: number, controls,loader: OBJLoader,mtlLoader,fbxloader;

    function init() {      
      // @ts-ignore
      width = document.getElementById('canvas-frame').clientWidth;
      // @ts-ignore
      height = document.getElementById('canvas-frame').clientHeight;

      renderer = new THREE.WebGLRenderer({
        antialias: true,
      });
      renderer.setSize(width, height);
      // @ts-ignore
      document.getElementById('canvas-frame').appendChild(renderer.domElement);
      renderer.setClearColor(0xffffff, 1.0);
    }
    function render() {
      renderer.render(scene, camera);
    }
    var camera: THREE.PerspectiveCamera | THREE.Camera;
    function initCamera() {
      camera = new THREE.PerspectiveCamera(45, width / height, 1, 10000);
      camera.position.set(0, 0, 0);      
      camera.lookAt(0, 0, 0);
      camera.position.set(100, 200, 300);
      //相机控件
      controls = new OrbitControls(camera, renderer.domElement);      
      controls.update();
    }

    var scene: THREE.Scene | THREE.Object3D;
    function initScene() {
      scene = new THREE.Scene();
    }

    var light;
    function initLight() {
      light = new THREE.AmbientLight(0xffffff);
      light.position.set(300, 300, 0);
      scene.add(light);      
    }
    function initObject() {     
      loader = new OBJLoader();  //obj格式      
          //loader.setPath('/ground-pressure-page/src/pages/index/components/');
          loader.load(
            './3D-YKS-XD.obj',
            function ( object ) {
              object.name='YKSmodel';              
              console.log(object);
              scene.add( object );
            },
            // called when loading is in progresses
            function ( xhr ) {
              console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
              console.log(xhr.total);
            },
            // called when loading has errors
            function ( error ) {
              console.log( 'An error happened' );
            }
          );        
    }

    function threeStart() {
      init();
      initCamera();
      initScene();
      initLight();
      initObject();
      animation();
    }
    function animation() {
      renderer.render(scene, camera);
      requestAnimationFrame(animation);
    }
  }

  /**
   * 开始Three
   *
   * @memberof ThreeBim
   */
  componentDidMount() {
    this.initThree();
  }
  render() {
    return (
      <div id="canvas-frame" style={{ width: '100%', height: '100%' }}></div>
    );
  }
}

export default GroundPressuer;

obj模型和GroundPressuer.tsx在统一目录下。

怎么解决问题,请大佬们告知!多谢

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 已结题 (查看结题原因) 6月8日

    悬赏问题

    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!
    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
    • ¥15 求daily translation(DT)偏差订正方法的代码
    • ¥15 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3