.glb格式的模型怎么在three.js中展示

2020-11-24 11:24:21 浏览数 (1)

3D软件中导出的格式一般有.obj 和.glb ,下面是blender 2.8.2 生成模型并在three.js中展示的流程

一、先创建一个图形,选择UV Editing 进行uv展开,把UV展开的图形导出UV布局图,然后用ps进行处理,再导入处理好的图进行贴图,uv贴图可以选择上面的shading,再选择下面的添加-纹理-图片纹理,然后连到基础色

UV贴图后导出 .glb 格式

二、由于是在vue中使用把导出的文件放到public/models/cylinder.glb

三、代码实现,首先要引入GLTFLoader

代码语言:javascript复制
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader"
代码语言:javascript复制
export default {
  name: "ThreeTest",
  data() {
    return {};
  },
  mounted() {
    // this.getData();
    this.timer = null;
    this.myReq = null;
    this.container;
    this.scene;
    this.camera;
    this.renderer;
    this.labelRenderer;
    this.controls;
    this.initScene();
    this.initCamera();
    this.initRender();
    this.initModel();
    this.initControls();
    this.initLight()
    this.animate();
    // this.render()
    window.onresize = this.onWindowResize;
  },


  methods: {
    initScene() {
      this.scene = new THREE.Scene();
    },
    initCamera() {
      this.container = document.getElementById("container");
      this.camera = new THREE.PerspectiveCamera(
        70,
        this.container.clientWidth / this.container.clientHeight,
        1,
        1000
      );
      console.log(this.camera);
      this.camera.position.z = 5; //z设置小写会使图像变形小
    },
    initRender: function () {
      this.renderer = new THREE.WebGLRenderer({ antialias: true });
      this.renderer.setSize(
        this.container.clientWidth,
        this.container.clientHeight
      );
      this.container.appendChild(this.renderer.domElement);
    },

    initLight() {
      this.scene.add(new THREE.AmbientLight(0xffffff));
      this.light = new THREE.DirectionalLight(0xffffff);
      this.light.position.set(0, 0, 50);
      this.scene.add(this.light);
    },
    initModel() {
       let loader = new GLTFLoader()
       let gltScene
       loader.load("models/cylinder.glb",(gltf)=>{
         console.log(gltf)
         gltf.scene.position.set(0,0,0)
         this.scene.add(gltf.scene)
       })
    },
    initControls() {
      //controls = new THREE.OrbitControls( camera, renderer.domElement );
      this.controls = new OrbitControls(
        this.camera,
        this.labelRenderer.domElement
      );

      // 如果使用animate方法时,将此函数删除

      //controls.addEventListener( 'change', render );

      // 使动画循环使用时阻尼或自转 意思是否有惯性

      this.controls.enableDamping = false;

      //动态阻尼系数 就是鼠标拖拽旋转灵敏度

      //controls.dampingFactor = 0.25;

      //是否可以缩放

      this.controls.enableZoom = true;

      //是否自动旋转

      this.controls.autoRotate = false;

      //设置相机距离原点的最远距离

      this.controls.minDistance = 1;

      //设置相机距离原点的最远距离

      this.controls.maxDistance = 10;

      //是否开启右键拖拽

      this.controls.enablePan = false;
    },
    render() {
      this.renderer.render(this.scene, this.camera);
      this.labelRenderer.render(this.scene, this.camera);
    },
    onWindowResize() {
      this.camera.aspect = window.innerWidth / window.innerHeight;

      this.camera.updateProjectionMatrix();

      this.render();

    },
    animate() {
      this.render();
      this.myReq = requestAnimationFrame(this.animate);
    },
  }
};
</script>

注意:要开启灯光,否则会显示不出模型

0 人点赞