three完全开源扩展案例03-模型加载

在这里插入图片描述
https://www.threelab.cn/three-cesium-examples/public/index.html#/codeMirror?navigation=Three.js%E6%A1%88%E4%BE%8B[r166]&classify=basic&id=modelLoad

更多内容:https://threelab.cn/

import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader.js'
import { FBXLoader } from 'three/examples/jsm/loaders/FBXLoader.js'
import { OBJLoader } from 'three/examples/jsm/loaders/OBJLoader.js'
import { MTLLoader } from 'three/examples/jsm/loaders/MTLLoader.js'

const box = document.getElementById('box')

const scene = new THREE.Scene()

const camera = new THREE.PerspectiveCamera(75, box.clientWidth / box.clientHeight, 0.1, 1000)

camera.position.set(5, 5, 5)

const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true, logarithmicDepthBuffer: true })

renderer.setSize(box.clientWidth, box.clientHeight)

box.appendChild(renderer.domElement)

new OrbitControls(camera, renderer.domElement)

window.onresize = () => {

    renderer.setSize(box.clientWidth, box.clientHeight)

    camera.aspect = box.clientWidth / box.clientHeight

    camera.updateProjectionMatrix()

}

animate()

function animate() {

    requestAnimationFrame(animate)

    renderer.render(scene, camera)

}

scene.add(new THREE.AmbientLight(0xffffff, 4))

scene.add(new THREE.AxesHelper(1000))

// 加载模型 gltf/ glb  draco解码器
const loader = new GLTFLoader()

loader.setDRACOLoader(new DRACOLoader().setDecoderPath('/three-cesium-examples/public/js/three/draco/'))

loader.load(

    '/three-cesium-examples/public/files/model/car.glb',

    gltf => {

        scene.add(gltf.scene)

    }

)

// 加载模型 fbx
new FBXLoader().load('/three-cesium-examples/public/files/model/city.FBX', (object3d) => {

    scene.add(object3d)

    object3d.scale.set(0.0005, 0.0005, 0.0005)

})

// 加载模型 obj/ mtl
const objLoader = new OBJLoader()

const mtlLoader = new MTLLoader()

mtlLoader.load('/three-cesium-examples/public/files/model/house/house.mtl', (mtl) => {

    mtl.preload()

    objLoader.setMaterials(mtl)

    objLoader.load(

        '/three-cesium-examples/public/files/model/house/house.obj',

        (obj) => {

            scene.add(obj)

            obj.position.x += 3

        }

    )

})

在这里插入图片描述

相关推荐

  1. Three.js的THREE.LOD如何gltf模型

    2024-07-17 02:10:01       56 阅读
  2. 从Unity到Three.js(模型文件

    2024-07-17 02:10:01       44 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-17 02:10:01       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-17 02:10:01       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-17 02:10:01       58 阅读
  4. Python语言-面向对象

    2024-07-17 02:10:01       69 阅读

热门阅读

  1. c++将utf8转gb2312

    2024-07-17 02:10:01       24 阅读
  2. 理解 extern “C“:跨语言链接的桥梁

    2024-07-17 02:10:01       23 阅读
  3. 记录第一次因为数据库事务产生的BUG

    2024-07-17 02:10:01       19 阅读
  4. 量化机器人如何提升交易透明度?

    2024-07-17 02:10:01       23 阅读
  5. Flutter基本概念&常用命名

    2024-07-17 02:10:01       23 阅读
  6. AI对开发者的影响:重塑技能、职业与生活

    2024-07-17 02:10:01       24 阅读
  7. CloudCone服务器2核1G一年只需15刀

    2024-07-17 02:10:01       19 阅读
  8. zookeeper+kafka消息队列群集部署

    2024-07-17 02:10:01       20 阅读