ThreeJs学习-加载外部三维模型gltf格式

import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';

const loader = new GLTFLoader();
const group = new THREE.Group();

loader.load('./guntong.glb', function (gltf) {
  const scene = gltf.scene;
  // scene.translateZ(10)

  // 返回的场景对象gltf.scene插入到threejs场景中
  group.add(scene);
})

loader.load('./Soldier.glb', (gltf) => {
  console.log(gltf.scene)
  gltf.scene.rotateY(Math.PI)

  // 返回的场景对象gltf.scene插入到threejs场景中

  const mixer = new THREE.AnimationMixer(gltf.scene);
  // const clipAction = mixer.clipAction(gltf.animations[0]);
  // clipAction.play(); //播放动画

  const ResetAction = mixer.clipAction(gltf.animations[0]);
  const RunAction = mixer.clipAction(gltf.animations[1]);
  const StopAction = mixer.clipAction(gltf.animations[2]);
  const WalkAction = mixer.clipAction(gltf.animations[3]);
  ResetAction.play();
  RunAction.play();
  StopAction.play();
  WalkAction.play();

  // gltf.animations[0]休息
  // gltf.animations[1]跑步
  // gltf.animations[2]静止展开
  // gltf.animations[3]走路

  // 跑步和走路动画对人影响程度为0,人处于休闲状态
  ResetAction.weight = 1.0;
  RunAction.weight = 0.0;
  StopAction.weight = 0.0;
  WalkAction.weight = 0.0;

  const clock = new THREE.Clock();

  let ActionState = ResetAction;//当前处于播放状态的动画动作对象

  document.getElementById('run').addEventListener('click', function () {
    ActionState.weight = 0.0;//播放状态动画权重设置为0

    RunAction.weight = 1.0;

    ActionState = RunAction;
  })

  document.getElementById('stop').addEventListener('click', function () {
    ActionState.weight = 0.0;//播放状态动画权重设置为0

    StopAction.weight = 1.0;
    ActionState = StopAction;
  })

  document.getElementById('walk').addEventListener('click', function () {
    ActionState.weight = 0.0;//播放状态动画权重设置为0

    WalkAction.weight = 1.0;
    ActionState = WalkAction;
  })
  document.getElementById('reset').addEventListener('click', function () {
    ActionState.weight = 0.0;//播放状态动画权重设置为0

    ResetAction.weight = 1.0;
    ActionState = ResetAction;
  })
  // 骨骼辅助显示
  const skeletonHelper = new THREE.SkeletonHelper(gltf.scene);
  group.add(skeletonHelper);
  function loop() {
    requestAnimationFrame(loop);
    //clock.getDelta()方法获得loop()两次执行时间间隔
    const frameT = clock.getDelta();
    // 更新播放器相关的时间
    mixer.update(frameT);
  }
  loop();

  group.add(gltf.scene);

})

export default group

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值