gltf模型浏览器_Vue使用Three.js加载glTF模型的方法详解

前言

Three.js是一个跨浏览器的脚本,使用JavaScript函数库或API来在网页浏览器中创建和展示动画的三维计算机图形,基于WebGL实现,对WebGL进行了进一步的封装,简化了多数复杂的接口。

Three.js支持包括 .obj、.gltf等类型的模型结构。glTF(GL传输格式)是Khronos的一个开放项目,它为3D资产提供了一种通用的、可扩展的格式,这种格式既高效又与现代web技术高度互操作。

obj格式的模型只支持顶点、法线、纹理坐标和基本材质,而glTF模型除上述所有内容外,glTF还提供了如下功能:

层级对象

场景信息(光源,相机)

骨骼结构与动画

更可靠的材质和着色器

一、安装引入Three.js

npm install three

在需要使用3D模型的页面导入包:

import * as Three from "three"

在Vue中导入glTF模型需要使用 Three.js 中的 GLTFLoader:

import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader"

// 导入轨道模型控制器

import { OrbitControls } from "three/examples/jsm/controls/OrbitControls

二、页面DOM元素渲染

在Vue中,我们需要使用一个 div 元素来作为3D模型的容器:

页面打开之后,Three.js会给 div 元素添加一个 canvas 子元素用来作为3D模型的画布。

三、初始化

Three.js中最重要的三大组件:

场景——Scene

相机——Camera

渲染器——Renderer

初始化:

mounted(){

this.initScene()

this.initContainer()

this.initCamera()

this.initRenderer()

this.initControls()

},

methods:{

initModelContainer() {

this.model_container = document.getElementById("container");

this.model_container.style.height = window.innerHeight + "px";

this.model_container.style.width = window.innerWidth + "px";

this.height = this.model_container.clientHeight;

this.width = this.model_container.clientWidth;

},

initScene() {

this.scene = new Three.Scene();

},

initCamera() {

// 照相机

this.camera = new Three.PerspectiveCamera(70, this.width / this.height, 0.01, 1000);

this.camera.position.set(-100, 60, 0);

},

initRenderer() {

this.renderer = new Three.WebGLRenderer({ antialias: true, alpha: true });

this.renderer.setSize(this.width, this.height);

// 兼容高清屏幕

this.renderer.setPixelRatio(window.devicePixelRatio);

// 消除canvas的外边框

this.renderer.domElement.style.outline = "none";

this.model_container.appendChild(this.renderer.domElement);

},

initControls() {

this.orbitControls = new OrbitControls(

this.camera,

this.renderer.domElement

);

// 惯性

this.orbitControls.enableDamping = true;

// 动态阻尼系数

this.orbitControls.dampingFactor = 0.25;

// 缩放

this.orbitControls.enableZoom = true;

// 右键拖拽

this.orbitControls.enablePan = true;

// 水平旋转范围

this.orbitControls.maxAzimuthAngle = Math.PI / 6;

this.orbitControls.minAzimuthAngle = -Math.PI / 6;

// 垂直旋转范围

this.orbitControls.maxPolarAngle = Math.PI / 6;

this.orbitControls.minPolarAngle = -Math.PI / 6;

},

}

四、导入glTF模型

将你的 gltf 模型放在 Vue 项目中的 public 文件夹下,注意,只有将 gltf 模型放在静态资源文件夹下才能被访问到。

在钩子函数 mounted 中进行模型加载:

mounted(){

this.loadModel()

},

methods:{

loadModel(){

let that = this

// gltf模型加载器

let loader = new GLTFLoader()

return new Promise(function(resolve, reject){

loader.load(

// 模型在 /public/static/building/文件夹下

"static/building/scene.gltf",

gltf => {

console.log(gltf)

gltf.scene.traverse(object => {

// 修改模型材质

let material = ...

object.material = material

})

let group = new Three.Group()

group.add(gltf.scene)

let box = new Three.Box3()

box.setFromObject(group)

let wrapper = new Three.Object3D()

wrapper.add(group)

// 根据自己模型的大小设置位置

wrapper.position.set(100, -300, 120)

// 将模型加入到场景中 ! important

that.scene.add(wrapper)

},

xhr => {

// 模型加载期间的回调函数

console.log(`${(xhr.loaded / xhr.total) * 100% building model loaded`

);

},

error => {

// 模型加载出错的回调函数

console.log("error while loading", error);

reject("load model error", error);

}

)

})

}

}

启动项目,模型导入成功,可以根据自己的需求为模型渲染材质。

总结

到此这篇关于Vue使用Three.js加载glTF模型的文章就介绍到这了,更多相关Vue用Three.js加载glTF模型内容请搜索龙方网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持龙方网络!

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值