在react 中还有另外一种three.js 渲染方式

npm install @react-three/drei

利用标签去渲染模型

javascript 复制代码
import React, { useRef,useEffect, useState } from 'react';
import { Canvas, useFrame, useThree,useLoader } from '@react-three/fiber';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader';
import { OrbitControls } from '@react-three/drei';
import * as THREE from 'three';
function BuildingModel({  }) {
  const [position, setPosition] = useState([130,120,10]);
  
    // 使用useLoader钩子加载GLTF模型
  const gltf = useLoader(GLTFLoader, './绿植单元模型.glb');
  useFrame(() => {
    // 更新模型状态,如动画等
   
  });


  return (
        <mesh
            position={position}
            rotation={[0.9, 0.09,0.02]}  
             >
            <primitive object={gltf.scene} />
        </mesh>
  );
}



export default function App() {


  

    return (
        <Canvas 
            camera={{   type: 'perspective',position: [0, 188, 60] ,fov:120}} style={{width:'100%',height:'100vh'}}>
            <ambientLight  intensity={1}/>
            <directionalLight position={[0, 10, 0]} intensity={1} color="#ffffff" /> 添加定向光源
            <BuildingModel position={[0, 0, 60]}  />
            <OrbitControls 
                    // ref={controlsRef }
                    enableZoom={false}
                    target={[0,180, 60]} // 设置围绕的目标点
                    enablePan={false} // 禁止平移
                    minDistance={5} // 最小距离
                    maxDistance={22} // 最大距离
                    minPolarAngle={Math.PI/16} // 最小俯仰角(从上方)
                    maxPolarAngle={Math.PI/16} // 最大俯仰角(到水平 />
                />
        </Canvas>
  );
}
相关推荐
祝余呀3 分钟前
html初学者第一天
前端·html
脑袋大大的1 小时前
JavaScript 性能优化实战:减少 DOM 操作引发的重排与重绘
开发语言·javascript·性能优化
速易达网络2 小时前
RuoYi、Vue CLI 和 uni-app 结合构建跨端全家桶方案
javascript·vue.js·低代码
耶啵奶膘3 小时前
uniapp+firstUI——上传视频组件fui-upload-video
前端·javascript·uni-app
JoJo_Way3 小时前
LeetCode三数之和-js题解
javascript·算法·leetcode
视频砖家3 小时前
移动端Html5播放器按钮变小的问题解决方法
前端·javascript·viewport功能
lyj1689973 小时前
vue-i18n+vscode+vue 多语言使用
前端·vue.js·vscode
小白变怪兽5 小时前
一、react18+项目初始化(vite)
前端·react.js
ai小鬼头5 小时前
AIStarter如何快速部署Stable Diffusion?**新手也能轻松上手的AI绘图
前端·后端·github