Threejs-13、加载glft模型和加载压缩过的模型

1、完整代码

<script setup>
// 导入threejs
import * as THREE from "three"
// 引入轨道控制器
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
// 引入lil.gui
import { GUI } from "three/examples/jsm/libs/lil-gui.module.min.js";
import{ RGBELoader} from "three/examples/jsm/loaders/RGBELoader.js"
// 导入gltf加载器
import {GLTFLoader} from "three/examples/jsm/loaders/GLTFLoader";
//导入dracoloader
import {DRACOLoader} from "three/examples/jsm/loaders/DRACOLoader";
// 创建场景
const scene = new THREE.Scene();
//创建相机
const camera = new THREE.PerspectiveCamera(
    800,// 视角
    window.innerWidth / window.innerHeight,
    0.1, // 近平面
    1000  // 远平面
);
// 创建渲染器
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth,window.innerHeight);
document.body.appendChild(renderer.domElement);
//设置相机位置
camera.position.x=5;
camera.position.y=5;
camera.position.z=5;
//相机默认看向远点
camera.lookAt(0,0,0);
// 添加世界坐标辅助器
const axesHelper =  new THREE.AxesHelper(100);
scene.add(axesHelper);
// 添加轨道控制器
const controls = new OrbitControls(camera,renderer.domElement);
// 设置带阻尼的惯性
controls.enableDamping=true;
// 设置阻尼系数
controls.dampingFactor = 0.05;
// 设置自动旋转
//controls.autoRotate = true;
//渲染函数
function animate(){
    controls.update();
    requestAnimationFrame(animate);
    renderer.render(scene,camera);
}
animate();
// 监听窗口变化
window.addEventListener("resize",()=>{
  camera.aspect = window.innerWidth / window.innerHeight;
  //   更新摄像机的投影矩阵
  camera.updateProjectionMatrix();
  //   更新渲染器
  renderer.setSize(window.innerWidth, window.innerHeight);
  //   设置渲染器的像素比
  renderer.setPixelRatio(window.devicePixelRatio);
})
// 创建GUI
const gui =  new GUI();
// 创建场景雾
scene.fog = new THREE.Fog("white",0.1,50);
scene.background = new THREE.Color("white");
// 实例化加载器gltf加载器
const loader = new GLTFLoader();
//加载模型
loader.load(
    "./model/Duck.glb",
    (gltf)=>{
      console.log(gltf)
      scene.add(gltf.scene)
    })
// 实例化draco加载器
const dracoLoader = new DRACOLoader();
//设置draco加载器路径
dracoLoader.setDecoderPath("./draco/");
//设置gltf加载器draco解码器
loader.setDRACOLoader(dracoLoader);
loader.load(
    "./model/city.glb",
    (gltf)=>{
  scene.add(gltf.scene)
})
// 加载环境贴图
let rgbeLoader = new RGBELoader();
rgbeLoader.load("./texture/Alex_Hart-Nature_Lab_Bones_2k.hdr",(texture)=>{
  // 球面全景
  texture.mapping = THREE.EquirectangularReflectionMapping;
  //设置环境贴图
  scene.environment = texture;
})

</script>
<template>
  <div></div>
</template>
<style >
*{
  margin: 0;
  padding: 0;
}
canvas{
  display: block;
  position: fixed;
  left: 0;
  top: 0;
  width: 100vw;
  height: 100vh;
}
</style>

2、效果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一叶飘零晋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值