【问题标题】:React-three-fiber for loopReact-three-fiber for 循环
【发布时间】:2021-05-13 19:22:42
【问题描述】:

我是 React.js 和 React-three-fiber 的新手。我想加100颗星。 createStars() 函数循环运行 100 次并创建 100 个 x,y,z 坐标。创建小球体。如何在循环中运行 100 次?

import './App.css';
import React, { useRef } from "react";
import ReactDOM from 'react-dom';
import { Canvas, useFrame } from "react-three-fiber";

function App() {

  function scrollPage() {
    console.log('Heelo');
  }

  function createStars() {
    console.log('Hello');
      for(var i = 0; i < 100; i++) {
        var x = Math.floor(Math.random() * 10);
        var y = Math.floor(Math.random() * 10);
        var z = Math.floor(Math.random() * 10);
        console.log(x + ' ' + y + ' ' + z);
      }
  }

  return (
    <>
      <Canvas colorManagement id="canvas">
      <ambientLight intensity={1} />
      <Star position={[0,0,0]} color='white' />
      <Star position={[1,0,0]} color='white' />
      <Star position={[0,3,0]} color='white' />
      <Star position={[0,-3,0]} color='white' />
      <Star position={[5,2,-3]} color='white' />
      <Star position={[10,0,0]} color='white' />
      <Star position={[-3,0,1]} color='white' />
      <Star position={[-5,2,0]} color='white' />
      <Star position={[-10,0,1]} color='white' />
      <Star position={[-6,-5,-4]} color='white' />
      <Star position={[-7,-3,0]} color='white' />
      <Star position={[7,-2,0]} color='white' />
      <Star position={[-7,3,0]} color='white' />
      <Star position={[0,0,9]} color='white' />
      </Canvas>
    </>
  );
}

const Star = ({position, color}) => {
  const mesh = useRef(null);
  useFrame(() => (mesh.current.rotation.x += 0.01));
  return(
    <>
      <mesh position={position}  ref={mesh}>
        <sphereBufferGeometry attach='geometry' args={[0.02,500, 500]} />
        <meshStandardMaterial attach='material' color={color}/>
      </mesh>
    </>
  );
}

export default App;

附: - 忽略任何小的拼写错误,代码可以正常工作。

【问题讨论】:

    标签: reactjs react-three-fiber


    【解决方案1】:

    我相信您希望将 Star 组件映射到随机生成的坐标。

    看这个:

    
    import './App.css';
    import React, { useRef } from "react";
    import ReactDOM from 'react-dom';
    import { Canvas, useFrame } from "react-three-fiber";
    
    function App() {
    
      function scrollPage() {
        console.log('Heelo');
      }
    
      function createStars() {
        let stars = []
        for (var i = 0; i < 100; i++) {
          var x = Math.floor(Math.random() * 10);
          var y = Math.floor(Math.random() * 10);
          var z = Math.floor(Math.random() * 10);
          stars[i] = [x, y, z]
        }
        return stars
      }
    
      const stars = createStars().map((cords, i) =>
        (<Star key={i} position={cords} color='white' />)
      )
    
      return (
        <>
          <Canvas colorManagement id="canvas">
          <ambientLight intensity={1} />
          {stars}
          </Canvas>
        </>
      );
    }
    
    const Star = ({position, color}) => {
      const mesh = useRef(null);
      useFrame(() => (mesh.current.rotation.x += 0.01));
      return(
        <>
          <mesh position={position}  ref={mesh}>
            <sphereBufferGeometry attach='geometry' args={[0.02,500, 500]} />
            <meshStandardMaterial attach='material' color={color}/>
          </mesh>
        </>
      );
    }
    
    export default App;
    

    【讨论】:

    • 不能在 Canvas 标记之外工作,for 循环表示在 Canvas 标记中工作
    • 是的,我弄错了,JSX 中的函数不能是子函数。我更新了它,所以 stars 是 JSX。我还添加了一个关键道具,这对于正确映射事物至关重要。那里的菜鸟错误
    猜你喜欢
    • 2020-10-31
    • 2021-08-20
    • 2019-10-27
    • 2022-01-25
    • 1970-01-01
    • 2020-01-20
    • 2022-11-30
    • 2022-01-04
    • 2021-07-02
    相关资源
    最近更新 更多