在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>
  );
}
相关推荐
集成显卡4 小时前
Bun v1.3.6 发布:内置 Tarball 归档支持、JSONC 解析、Bundle 分析增强等重磅更新!
javascript·新版本·bun.js
奔跑的web.4 小时前
TypeScript Enum 类型入门:从基础到实战
前端·javascript·typescript
盐真卿4 小时前
python2
java·前端·javascript
梦梦代码精4 小时前
BuildingAI vs Dify vs 扣子:三大开源智能体平台架构风格对比
开发语言·前端·数据库·后端·架构·开源·推荐算法
seabirdssss5 小时前
《bootstrap is not defined 导致“获取配置详情失败”?一次前端踩坑实录》
前端·bootstrap·html
kgduu5 小时前
js之表单
开发语言·前端·javascript
摘星编程6 小时前
React Native for OpenHarmony 实战:Picker 选择器组件详解
javascript·react native·react.js
摘星编程7 小时前
React Native for OpenHarmony 实战:VirtualizedList 虚拟化列表
javascript·react native·react.js
谢尔登7 小时前
Vue3 响应式系统——computed 和 watch
前端·架构
愚公移码7 小时前
蓝凌EKP产品:主文档权限机制浅析
java·前端·数据库·蓝凌