【问题标题】:React and Three.jsReact 和 Three.js
【发布时间】:2018-06-27 05:35:30
【问题描述】:

我的 3d 图像没有渲染到屏幕上。我将 React 与 Three.js 结合使用。我收到此错误:

“THREE.Object3D.add:对象不是 THREE.Object3D 的实例。”

这是我的组件:(下面的文件树)。

import React, { Component } from "react"
import * as THREE from "three"
import * as MTLLoader from "three-mtl-loader"
import * as OBJLoader from "three-obj-loader"

OBJLoader(THREE)
MTLLoader(THREE)
const mesh = null
let obj

const SPEED = 0.015
class Scene extends Component {
  // constructor(props) {
  //   super(props)
  // }

  componentDidMount() {
    const scene = new THREE.Scene()
    this.scene = scene

    const onError = xhr => {}
    const width = 200
    const height = 850
    const onProgress = xhr => {
      if (xhr.lengthComputable) {
        const percentComplete = xhr.loaded / xhr.total * 100
        console.log(`${Math.round(percentComplete, 2)}% downloaded`)
      }
    }

    const mtlLoader = new MTLLoader()
    mtlLoader.setPath("../3d/")
    this.THREE = THREE
    mtlLoader.load("./Bottle_v06.mtl", materials => {
      materials.preload()

      const objLoader = new this.THREE.OBJLoader()
      objLoader.setMaterials(materials)
      objLoader.setPath("../3d/")
      objLoader.load(
        "./Bottle_v06.obj",
        object => {
          console.log("hiiiii")
          object.position.y = -25
          object.position.x = 0
          object.scale.x = 1
          object.scale.y = 1
          object.scale.z = 1
          obj = this.object
          scene.add(obj)
        },
        onProgress,
        onError
      )
    })

    const camera = new THREE.PerspectiveCamera(45, 200 / 850, 1, 1000)
    camera.position.z = 320
    camera.lookAt(scene.position)
    this.camera = camera

    const light = new THREE.AmbientLight(0xffffff, 1)
    scene.add(light)

    const dirLight = new THREE.DirectionalLight(0xffffff, 1)
    dirLight.position.set(100, 100, 50)
    scene.add(dirLight)

    const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true })
    renderer.setSize(width, height)
    this.renderer = renderer

    this.mount.appendChild(this.renderer.domElement)
  }

  rotateMesh = () => {
    if (!obj) {
      return
    }
    obj.rotation.y -= SPEED
  }

  renderScene() {
    this.renderer.render(this.scene, this.camera)
    this.rotateMesh
  }

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

export default Scene

文件树:

   /src
     /3d
       -Bottle_v06.mtl
       -Bottle_v06.obj
       -Image_01.png
     /components
       -Scene.js

StackOverflow 说我应该输入更多细节,所以我会一直打字,直到错误消失,我才能真正发布这个。请忽略这一点。我不明白他们为什么不让我提交我的问题。

【问题讨论】:

  • 您正在打印“object”,但您正在添加“this.object”。为什么不添加“object”而不是“this.object”?
  • 修复了错误(谢谢!)。但不幸的是 3d 仍然没有渲染。
  • 您的代码在我看来有点错误。例如,您对 this.rotateMesh 的调用丢失() ....您应该开始使用 chrome 调试器和控制台来发现异常/错误.. 如果您需要帮助,也可以将其变成小提琴,或提供链接到人们可以运行的东西。让人们从心理上调试你的代码只会浪费大家的时间。
  • 我实际上没有看到你在任何地方使用 requestAnimationFrame 启动渲染循环......你的 renderScene 不会调用自己......

标签: javascript reactjs three.js


【解决方案1】:

您可能还需要这样的东西,可能在您创建渲染器之后:

function animationLoop(){
   requestAnimationFrame( animationLoop );
   renderScene();
}

animationLoop();

【讨论】:

    猜你喜欢
    • 2020-12-15
    • 2018-03-28
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 2021-05-22
    • 2021-10-09
    • 2018-01-23
    • 2018-02-25
    相关资源
    最近更新 更多