在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>
  );
}
相关推荐
蓝瑟2 分钟前
告别重复造轮子!业务组件多场景复用实战指南
前端·javascript·设计模式
dorisrv23 分钟前
高性能的懒加载与无限滚动实现
前端
韭菜炒大葱30 分钟前
别等了!用 Vue 3 让 AI 边想边说,字字蹦到你脸上
前端·vue.js·aigc
StarkCoder42 分钟前
求求你,别在 Swift 协程开头写 guard let self = self 了!
前端
清妍_42 分钟前
一文详解 Taro / 小程序 IntersectionObserver 参数
前端
电商API大数据接口开发Cris1 小时前
构建异步任务队列:高效批量化获取淘宝关键词搜索结果的实践
前端·数据挖掘·api
符方昊1 小时前
如何实现一个MCP服务器
前端
喝咖啡的女孩1 小时前
React useState 解读
前端
渴望成为python大神的前端小菜鸟1 小时前
浏览器及其他 面试题
前端·javascript·ajax·面试题·浏览器
1024肥宅1 小时前
手写 new 操作符和 instanceof:深入理解 JavaScript 对象创建与原型链检测
前端·javascript·ecmascript 6