1.模型放到public/static下
2.导入加载器
import * as THREE from 'three'
import { STLLoader } from 'three/examples/jsm/loaders/STLLoader'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'
import { OBJLoader, MTLLoader } from 'three-obj-mtl-loader'
3.加载模型
/**
* OBJ和材质文件mtl加载
*/
const OBJLoader1 = new OBJLoader() // obj加载器
const MTLLoader1 = new MTLLoader() // 材质文件加载器
MTLLoader1.load('/static/1.mtl', (materials: any) => {
// 返回一个包含材质的对象MaterialCreator
console.log(materials)
// obj的模型会和MaterialCreator包含的材质对应起来
OBJLoader1.setMaterials(materials)
OBJLoader1.load('/static/1.obj', (obj: any) => {
console.log(obj)
obj.scale.set(10, 10, 10) // 放大obj组对象
this.scene.add(obj) // 返回的组对象插入场景中
})
})
网友评论