前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >.glb格式的模型怎么在three.js中展示

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

作者头像
tianyawhl
发布2020-11-24 11:24:21
15.4K2
发布2020-11-24 11:24:21
举报
文章被收录于专栏:前端之攻略前端之攻略

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>

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档