vue + threejs实现导入blender动画并控制其播放

<template>
<div class="modelsBox">
  <div class="modelsBox_wrapper"></div>
  <div class="opara-pannel" @click.stop>
    <div>
      <button @click="playAnimate(idx)" v-for="(v, idx) in this.animations" :key="idx">{{`动画${idx + 1} 播放`}}</button>
    </div>
  </div>
</div>
</template>

<script>

import { webglOBJ, labelTag, getPointRay, getFitScaleValue, deepCopyObject } from '@/utils/webGL/webGL.js';
import TWEEN from '@tweenjs/tween.js';
export default {
  name: 'modelsBox',
  data () {
    return {
      animations: [], // 动画对象
      isAn: false, // 单个动画停止标志
      current: -1, // 当前视角定索引
      controls: '',
      isAnimate: false,
      point3d: {},
      zoom: 1,
      target: '',
      sence: null,
      camera: '',
      renderer: ''
    };
  },
  beforeDestroy () {
    document.removeEventListener('click', this.get3D);
  },
  mounted () {
    this.int();
  },
  methods: {
    playAnimate (index) {
      // 先停止动画
      for (let i = 0; i < this.animations.length; i++) {
        this.mixer.clipAction(this.animations[i]).stop();
      }
      this.stopAn(index);
    },
    // 单个模型控制动画开始和停止
    stopAn (index) {
      if (!this.isAn) {
        this.mixer.clipAction(this.animations[index]).play();
        // this.scenceAnimate();
      } else {
        this.mixer.clipAction(this.animations[index]).stop();
      }
      this.isAn = !this.isAn;
    },
    scenceAnimate () {
      const a = new TWEEN.Tween(this.sence.rotation)
        .to({ x: 0, y: 10, z: 0}, 10000)
        .start().repeat(Infinity);
    },
    // 点击模型显示对应视角
    get3DmodeView () {
      const point3d = getPointRay(this.sence, this.camera).point;
      if (!point3d) {return;}
      const time = 1000;
      // 克隆相机用户计算点击后相机聚焦的位置
      const cloneCamera = this.camera.clone();
      // this.camera.lookAt(point3d);
      cloneCamera.lookAt(point3d);

      new TWEEN.Tween(this.camera.position)
      .to({ x: cloneCamera.position.x, y: cloneCamera.position.y, z: cloneCamera.position.z}, time)
      .easing(TWEEN.Easing.Back.Out).start();

      new TWEEN.Tween(this.camera.rotation)
      .to({ x: cloneCamera.rotation.x, y: cloneCamera.rotation.y, z: cloneCamera.rotation.z}, time)
      .easing(TWEEN.Easing.Back.Out).start();
      this.camera.lookAt(new THREE.Vector3(point3d.x, point3d.y, point3d.z));
      this.camera.updateProjectionMatrix();
      this.controls.reset();  // 一定要重置对应的控制轴的状态,不然视角还原后放大滚轮会从上一次的视角那边放大。
      this.renderer.render(this.sence, this.camera);
    },
    int () {
      const vm = this;
      const position = window.sessionStorage.getItem('position');
      const target = window.sessionStorage.getItem('target');
      let mixer = null;

      const imgBG = require('./img.jpg');
      const gltf = '/static/models/animate.gltf';
      // const gltf = 'https://scqilin.github.io/learning-threejs-third/assets/models/CesiumMan/CesiumMan.gltf';

      const loader = new THREE.GLTFLoader();
      const webGLdom = document.querySelector('.modelsBox_wrapper');
      const sence = webglOBJ.createSence(webGLdom);
      const camera = webglOBJ.createCamera({
        fov: 45,
        aspect: 1,
        near: 100,
        far: 5000,
        position: {
          x: 550,
          y: 550,
          z: -90
        }
      });

      if (position) {
        camera.position.set(JSON.parse(position).x, JSON.parse(position).y, JSON.parse(position).z);
        camera.lookAt(JSON.parse(target));
      }

      const renderer = webglOBJ.createRenderer();
      const plane = webglOBJ.createPlane(imgBG, imgBG);
      const spotLight = webglOBJ.createSpotLight();
      const directionalLight = webglOBJ.createDirectionalLight({ x: 100000, y: 100000, z: 100000 });
      const ambient = webglOBJ.createAmbient();
      const datGui = webglOBJ.createDatGui();
      const controls = webglOBJ.createControls();
      const axisHelper = webglOBJ.createAxisHelper();

      const earthDiv = document.createElement('div');
      earthDiv.className = 'label';
      earthDiv.textContent = 'Earth';
      const earthLabel = new THREE.CSS2DObject(earthDiv);
      earthLabel.position.set(150, 550, -190);
      console.log(earthLabel, 'earthLabel');
      loader.load(gltf, (gltf) => {
        let object = gltf.scene;
        console.log(gltf, 'gltf');
        object.scale.set(30, 30, 30);
        sence.add(object);
        // vm.scenceAnimate();

        // 动画效果
        mixer = new THREE.AnimationMixer(gltf.scene);
        vm.mixer = mixer;
        console.log(gltf.animations, 'gltf.animations[i]');
        vm.animations = gltf.animations;
        //同时将这个外部模型的动画全部绑定到动画混合器里面
        for (var i = 0; i < gltf.animations.length; i++){
          // mixer.clipAction(gltf.animations[i]).play();
        }
      });
      // 滚动缩小获取比例
      controls.addEventListener('change', function(evt) {
        console.log(controls.target, evt, 'zoom');
      });

      this.sence = sence;
      this.controls = controls;
      this.camera = camera;
      this.plane = plane;
      this.renderer = renderer;

      // 将对象添加到场景中去
      webglOBJ.senceAdd([plane, earthLabel, directionalLight, ambient, datGui, controls, axisHelper]);
      // webglOBJ.webglRender(sence, camera, renderer);

     // 动画显示
     const clock = new THREE.Clock();
      function render (html) {

        // 动画显示
        const time = clock.getDelta();
        if (mixer) {
          mixer.update(time);
        }

        TWEEN.update();
        renderer.render(sence, camera);
        vm.sence = sence;
        vm.camera = camera;
        requestAnimationFrame(render);
      };
      this.render = render;
      render();

    }
  }
};
</script>
<style lang="scss" scoped>
.label {
  width: 100px;
  height: 100px;
  background: #000;
}
div.active {
  border: 3px solid red;
  .sign div {
    color: red !important;
  }
}
.modelsBox_wrapper {
  position: relative;
  width: 100%;
  height: 100vh;
  border: 1px solid #ccc;
  overflow: hidden;
}
.opara-pannel {
  position: absolute;
  right: 15px;
  top: 100px;
  width: 200px;
  height: 400px;
  background: rgba(0, 0, 0, 0.7);
  div, p{
    color: #fff;
  }
}
.modelsBox {
  position:relative;
  overflow: hidden;
}
div[id *= "sign"] {
  width: 250px;
  height: 100px;
  padding:10px 10px 10px 70px;
  background: rgba(0, 0, 0, .65);
  background: url('~assets/label-bg.png') center center no-repeat;
  .sign{
    div {
      color: #fff;
      text-align: left;
      padding: 0 5px;
    }
  }
}
</style>

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
VueThree.js是两个热门的Web开发框架和库,分别用于构建用户界面和创建3D场景。结合使用VueThree.js可以非常方便地搭建一个3D仓房。 首先,在Vue项目安装并引入Three.js库。可以使用npm或者直接在HTML引入CDN链接。然后,使用Vue的组件化开发思想,创建一个3D仓房组件。 在Vue的3D仓房组件,可以使用Three.js的场景(Scene)、相机(Camera)、渲染器(Renderer)等基本元素来创建一个空白的3D场景。可以设定相机的位置和方向,调整渲染器的大小和样式。 接下来,可以使用Three.js提供的几何体(Geometry)和材质(Material)来创建具体的仓房模型。例如,可以使用BoxGeometry创建一个长方体模型,然后使用MeshBasicMaterial设置其颜色或者使用纹理材质来进行贴图。 在几何体和材质创建好之后,可以将其合并成一个网格(Mesh),并添加到场景。 为了使3D场景更加生动,可以使用Three.js的灯光(Light)来设置光照效果。例如,太阳光照射到仓房模型上,可以使用光源和颜色来模拟阳光的效果。 最后,在Vue的3D仓房组件添加交互功能,例如旋转、缩放或者平移等,可以使用Three.js提供的控制器(Controller)或者自定义事件监听器来实现。 在Vue项目的相应页面引入3D仓房组件,并传入相应的参数,即可在浏览器看到搭建好的3D仓房场景。 总之,使用VueThree.js搭建3D仓房的过程大致如上所述,需要使用Vue的组件化开发和Three.js的渲染和建模功能来实现。这样可以充分利用两个框架和库的优势,简化开发流程,创建出生动逼真的3D仓房场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值