7

我正在尝试加载一个对象(.obj)文件以与 three.js 一起使用并做出反应(使用 react-three-renderer),但得到一个我的代码如下所示:

import React from 'react';
import ReactDOM from 'react-dom';
import React3 from 'react-three-renderer';
import TrackballControls from './TrackballControls';
import * as THREE from 'three';
import * as OBJLoader from 'three-obj-loader';
OBJLoader(THREE);

class MyClass extends React.Component {
...
  render() {
    ...
    const objLoader = new THREE.OBJLoader();
  }
}

但是,我不断收到:“在'三个'中找不到导出'OBJLoader'(导入为'THREE')有人有想法吗?

4

3 回答 3

8

所以似乎添加this.THREE = THREE到 react 组件可以解决问题(很奇怪,嗯?)。所以我的代码目前看起来像:

import React from 'react';
import ReactDOM from 'react-dom';
import React3 from 'react-three-renderer';
import TrackballControls from './TrackballControls';
import * as THREE from 'three';
import * as OBJLoader from 'three-obj-loader';
OBJLoader(THREE);

class MyClass extends React.Component {
...
  render() {
    ...
    this.THREE = THREE;
    const objLoader = new this.THREE.OBJLoader();
  }
}
于 2017-07-22T19:18:10.730 回答
0

OBJLoader 现在是核心 three.js 库的一部分,因此您只需执行以下操作即可访问它:

 const objLoader = new THREE.OBJLoader();

删除行时:

import * as OBJLoader from 'three-obj-loader';
OBJLoader(THREE);

由于您已经在代码中导入了 three.js 库。

于 2019-06-27T02:46:33.833 回答
0

2019 年更新使用“three-obj-mtl-loader”代替'three-obj-loader'

  import { MTLLoader, OBJLoader } from "three-obj-mtl-loader";

使用此方法加载材质和 OBJ 模型

   /**
   * Load and Display OBJ Model
   */
  loadObjModel = (materialURL, objectURL) => {
    new MTLLoader().load(materialURL, materials => {
      materials.preload();
      //materials.Material.side = THREE.DoubleSide;
      console.log("Loaded Materials");
      var objLoader = new OBJLoader();
      objLoader.setMaterials(materials);
      objLoader.load(
        objectURL,
        object => {
          //const root = object.detail.loaderRootNode;
          console.log("Loaded Obj" + object);
          let mesh = object;
          this.scene.add(object);
          mesh.position.set(0, 0, 0);
          mesh.scale.set(0.07, 0.07, 0.07);
        },
        xhr => {
          console.log((xhr.loaded / xhr.total) * 100 + "% loaded");
        },
        // called when loading has errors
        error => {
          console.log("An error happened" + error);
        }
      );
    });
  };

用这样的材料加载 obj 模型

this.loadObjModel("./assets/windmill-fixed.mtl", "./assets/windmill.obj");

于 2019-12-10T21:41:28.903 回答