我想对象对象THREE.js分配变量,并循环列表但不起作用

时间:2019-12-18 06:11:35

标签: javascript html reactjs three.js

我想绑定变量object3D(用于url)文件。所以我将arrray列表映射为孩子的名字。

您可以看到渲染方法,我想ul标签子名称列表,但是此组件未渲染数据

和按钮单击事件,针对子级列表分配状态。并且console.log(vis)没问题。

但是渲染方法{thsi.state.vis.length> 0? ...}不起作用

我要分配变量,并且选中的复选框是子级可见的true或false。

let scene;
let viscera = [];

function loadObj(url) {
  const objLoader = new THREE.OBJLoader();
  objLoader.load(
    url,
    function(object) {
      console.log(object);
      let mesh = object;
      scene.add(object);
      mesh.position.set(0, 2, 0);
      mesh.scale.set(2, 2, 2);

      object.traverse(child => {
        if (child instanceof THREE.Object3D) {
          viscera.push(child);
          const liver = child.getObjectByName('Liver_Liver.001');
          const stomach = child.getObjectByName('Stomach_Stomach.001');
          // if (liver && stomach) {
          //   liver.visible = false;
          //   stomach.visible = false;
          // }
        }
      })
      console.log(viscera);
    },
    function(xhr) {
      console.log((xhr.loaded / xhr.total) * 100 + "% loaded");
    },
    // called when loading has errors
    function(error) {
      console.log("An error happened" + error);
    });
}

class ObjectLoader extends Component {
  constructor(props) {
    super(props);
    this.state = {
      vis: viscera,
      get: false
    }
  }

  componentDidMount() {  
    const width = this.mount.clientWidth;
    const height = this.mount.clientHeight;
    scene = new THREE.Scene();
    this.camera = new THREE.PerspectiveCamera(75, width / height, 0.1, 2000);
    this.camera.position.z = 8;

    this.renderer = new THREE.WebGLRenderer({ antialias: true });
    this.renderer.setClearColor("#263238");
    this.renderer.setSize(width, height);
    this.mount.appendChild(this.renderer.domElement);
    // const geometry = new THREE.BoxGeometry(5, 5, 5);
    // const material = new THREE.MeshBasicMaterial({
    //   color: "#0F0",
    //   wireframe: true
    // });
    // this.cube = new THREE.Mesh(geometry, material);
    // scene.add(this.cube);

    const controls = new OrbitControls(this.camera, this.renderer.domElement);
    controls.enableDamping = true;
    controls.dampingFactor = 0.25;
    controls.enableZoom = true;


    //LIGHTS
    const keyLight = new THREE.DirectionalLight(new THREE.Color('hsl(30, 100%, 75%)'), 1.0);
    keyLight.position.set(-100, 0, 100);

    const fillLight = new THREE.DirectionalLight(new THREE.Color('hsl(240, 100%, 75%)'), 0.75);
    fillLight.position.set(100, 0, 100);

    const backLight = new THREE.DirectionalLight(0xffffff, 1.0);
    backLight.position.set(100, 0, -100).normalize();

    scene.add(keyLight);
    scene.add(fillLight);
    scene.add(backLight);

    // load Object
    this.animate();
    loadObj(this.props.url);
}

  componentWillUnmount() {
    this.stop();
    this.mount.removeChild(this.renderer.domElement);
  }

  getViscera = (vis) => {
    this.props.getViscera(vis);
  }

  start = () => {
    if (!this.frameId) {
      this.frameId = requestAnimationFrame(this.animate);
    }
  };

  stop = () => {
    cancelAnimationFrame(this.frameId);
  };

  animate = () => {
    this.renderScene();
    this.frameId = window.requestAnimationFrame(this.animate);
  };

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

  // handleViscera = () => {
  //   for (let i = 0; i < viscera.length; i++) {
  //     console.log(viscera[i].name);
  //   }
  // }

  onLoad = () => {
    this.renderScene();
    //start animation
    this.start();
  };

  onProgress = xhr => {
    console.log((xhr.loaded / xhr.total) * 100 + "% loaded");
  };

  // Function called when download errors
  onError = error => {
    console.log("An error happened" + error);
  };

  // getChildren = () => {
  //   this.setState({
  //     get: true
  //   })
  //   if (this.state.vis && this.state.get === true) {
  //     return (
  //       <ul>
  //         {this.state.vis.map(item => {
  //           console.log(item);
  //           <li key={item}>{item.name}</li>
  //         })}
  //       </ul>
  //     )
  //   }
  // }


  render() {
    return (
      <div className="objLoader">
        <div
          style={{ width: "500px", height: "500px" }}
          ref={mount => {
            this.mount = mount;
          }}
        />
        <h2>Children</h2>
        <button className="children-btn" onClick={() => this.getViscera(this.state.vis)}>Get Children</button>
        {
          this.state.vis.length > 0 ? 
          <ul>
            {this.state.vis.map(v => {
              <li>{v.name}</li>
            })}
          </ul>
          : <div>no data</div>
        }
      </div>
    )
  }
}

0 个答案:

没有答案