在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>
  );
}
相关推荐
be or not to be2 小时前
深入理解 CSS 浮动布局(float)
前端·css
LYFlied2 小时前
【每日算法】LeetCode 1143. 最长公共子序列
前端·算法·leetcode·职场和发展·动态规划
老华带你飞2 小时前
农产品销售管理|基于java + vue农产品销售管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
小徐_23333 小时前
2025 前端开源三年,npm 发包卡我半天
前端·npm·github
C_心欲无痕3 小时前
vue3 - 类与样式的绑定
javascript·vue.js·vue3
GIS之路3 小时前
GIS 数据转换:使用 GDAL 将 Shp 转换为 GeoJSON 数据
前端
JIngJaneIL3 小时前
基于springboot + vue房屋租赁管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
天天扭码3 小时前
以浏览器多进程的角度解构页面渲染的整个流程
前端·面试·浏览器
你们瞎搞4 小时前
Cesium加载20GB航测影像.tif
前端·cesium·gdal·地图切片