👨⚕️ 主页: gis分享者
👨⚕️ 感谢各位大佬 点赞👍 收藏⭐ 留言📝 加关注✅!
👨⚕️ 收录于专栏:threejs gis工程师
文章目录
- 一、🍀前言
- 1.1 ☘️THREE.OBJLoader OBJ模型加载器
- 二、🍀利用THREE.OBJLoader加载obj模型
- 1. ☘️实现思路
- 2. ☘️代码样例
一、🍀前言
本文详细介绍如何基于threejs在三维场景中导入OBJ格式的模型,亲测可用。希望能帮助到您。一起学习,加油!加油!
1.1 ☘️THREE.OBJLoader OBJ模型加载器
THREE.OBJLoader 是 Three.js 中用于OBJ格式模型加载的加载器。此加载器内部使用FileLoader进行加载文件。
创建方法:
ObjectLoader( manager : LoadingManager )manager — 加载器所使用的loadingManager,默认值为THREE.DefaultLoadingManager.创建一个新的ObjectLoader。
参数说明
crossOrigin:如果设置了,在开始加载前, 将为图片分配 crossOrigin 属性,其值为 crossOrigin, 默认为"anonymous"。
manager:将要被加载的纹理的路径或者URL,详情请参考.setTexturePath。 默认值为空字符串。
二、🍀利用THREE.OBJLoader加载obj模型
1. ☘️实现思路
- 1、初始化renderer渲染器
- 2、初始化Scene三维场景
- 3、初始化camera相机,定义相机位置 camera.position.set,设置相机方向camera.lookAt。
- 4、初始化THREE.AmbientLight环境光源,scene场景加入环境光源,初始化THREE.PointLight点光源,设置点光源位置,设置点光源投影,scene添加点光源。
- 5、加载几何模型:创建THREE.AxesHelper坐标辅助工具,创建THREE.OBJLoader加载器加载pinecone.obj模型文件,生成mesh物体,scene场景加入mesh和THREE.AxesHelper坐标辅助工具。
- 6、加入controls控制,加入stats监控器,监控帧数信息。
2. ☘️代码样例
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>learn35(导入OBJ格式的模型)</title><script src="lib/threejs/127/three.js-master/build/three.js"></script><script src="lib/threejs/127/three.js-master/examples/js/loaders/OBJLoader.js"></script><script src="lib/threejs/127/three.js-master/examples/js/controls/OrbitControls.js"></script><script src="lib/threejs/127/three.js-master/examples/js/libs/stats.min.js"></script><script src="lib/threejs/127/three.js-master/examples/js/libs/dat.gui.min.js"></script>
</head>
<style>html, body {margin: 0;height: 100%;}canvas {display: block;}
</style>
<body onload="draw()">
</body>
<script>var renderervar initRender = () => {renderer = new THREE.WebGLRenderer({antialias: true})renderer.setClearColor(0xffffff)renderer.setSize(window.innerWidth, window.innerHeight)document.body.appendChild(renderer.domElement)}var scenevar initScene = () => {scene = new THREE.Scene()}var cameravar initCamera = () => {camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000)camera.position.set(0, 40, 50)camera.lookAt(new THREE.Vector3(0, 0, 0))}var lightvar initLight = () => {scene.add(new THREE.AmbientLight(0x444444))light = new THREE.PointLight(0xffffff)light.position.set(0, 0, 100)light.castShadow = truescene.add(light)}var initModel = () => {var helper = new THREE.AxesHelper(50)scene.add(helper)var loader = new THREE.OBJLoader()loader.load('data/model/pinecone.obj', (loadedMesh) => {var material = new THREE.MeshLambertMaterial({color: 0x5C3A21})loadedMesh.children.forEach(child => {child.material = materialchild.geometry.computeFaceNormals()child.geometry.computeVertexNormals()})loadedMesh.scale.set(100, 100, 100)scene.add(loadedMesh)})}var statsvar initStats = () => {stats = new Stats()document.body.appendChild(stats.dom)}var controlsvar initControls = () => {controls = new THREE.OrbitControls(camera, renderer.domElement)controls.enableDamping = true}var render = () => {renderer.render(scene, camera)}var onWindowResize = () => {camera.aspect = window.innerWidth / window.innerHeightcamera.updateProjectionMatrix()render()renderer.setSize(window.innerWidth, window.innerHeight)}var animate = () => {render()stats.update()requestAnimationFrame(animate)}var draw = () => {initRender()initScene()initCamera()initLight()initModel()initControls()initStats()animate()window.onresize = onWindowResize}
</script>
</html>
效果如下: