当前位置:   article > 正文

【Three.js】手把手教你在三维场景中实现人物行走动画效果_threejs indoorwalking

threejs indoorwalking

three.js的官方例程里面有个人物行走的案例,链接如下:

three.js官方案例-人物行走

这里简单剖析下人物行走的原理:

(1)

首先需要有个动画素材。gltf是可以集成动画的,像行人行走的动画,一般是通过骨骼动画实现的。一个gltf里面可以有多组动画,加载到three.js引擎里面之后,可以选择所需要的动画进行播放。

(2)

一般来说,人物行走的轨迹需要通过程序指定。当然,glb里面直接集成了位移的动画也不是不可以,但这样的轨迹控制就不方便。所以一般来说,glb动画就是人物在原地走动甩腿就可以了,通过程序来变更其位置。

官方的glb可以在windows下的3d查看器打开。

右下角可以切换不同的动画,这个例程里面有4个动画,奔跑、慢走、站立、伸展。有兴趣的小伙伴自己切换来看下。

那接下来我们开始写代码。

1、首先引入threejs的相关库

  1. <script type="importmap">
  2. {
  3. "imports": {
  4. "three": "./threejs/build/three.module.js",
  5. "three/addons/": "./threejs/jsm/"
  6. }
  7. }
  8. </script>
  1. <script type="module">
  2. import * as THREE from 'three';
  3. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  4. import * as SkeletonUtils from 'three/addons/utils/SkeletonUtils.js';
  5. </script>

2、定义好相关的变量。

各个变量的含义可以直接根据变量名读出来

  1. let camera, scene, renderer;
  2. let clock;
  3. let model1,model2;
  4. const mixers = [];

3、接下来写一个初始化函数init。

这个是搭建three环境的厂员工操作了

  1. function init() {
  2. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  3. camera.position.set( 2, 10, -25 );
  4. camera.lookAt( 0, 1, 0 );
  5. clock = new THREE.Clock();
  6. scene = new THREE.Scene();
  7. scene.background = new THREE.Color( 0xa0a0a0 );
  8. scene.fog = new THREE.Fog( 0xa0a0a0, 10, 50 );
  9. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444 );
  10. hemiLight.position.set( 0, 20, 0 );
  11. scene.add( hemiLight );
  12. const dirLight = new THREE.DirectionalLight( 0xffffff );
  13. dirLight.position.set( - 3, 10, - 10 );
  14. dirLight.castShadow = true;
  15. dirLight.shadow.camera.top = 4;
  16. dirLight.shadow.camera.bottom = - 4;
  17. dirLight.shadow.camera.left = - 4;
  18. dirLight.shadow.camera.right = 4;
  19. dirLight.shadow.camera.near = 0.1;
  20. dirLight.shadow.camera.far = 40;
  21. scene.add( dirLight );
  22. // scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) );
  23. // ground
  24. const mesh = new THREE.Mesh( new THREE.PlaneGeometry( 200, 200 ), new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) );
  25. mesh.rotation.x = - Math.PI / 2;
  26. mesh.receiveShadow = true;
  27. scene.add( mesh );
  28. const loader = new GLTFLoader();
  29. loader.load( 'models/gltf/Soldier.glb', function ( gltf ) {
  30. gltf.scene.traverse( function ( object ) {
  31. if ( object.isMesh ) object.castShadow = true;
  32. } );
  33. model1 = SkeletonUtils.clone( gltf.scene );
  34. model2 = SkeletonUtils.clone( gltf.scene );
  35. // const model3 = SkeletonUtils.clone( gltf.scene );
  36. const mixer1 = new THREE.AnimationMixer( model1 );
  37. const mixer2 = new THREE.AnimationMixer( model2 );
  38. // const mixer3 = new THREE.AnimationMixer( model3 );
  39. // mixer1.clipAction( gltf.animations[ 2 ] ).play(); // idle
  40. mixer1.clipAction( gltf.animations[ 1 ] ).play(); // run
  41. mixer2.clipAction( gltf.animations[ 3 ] ).play(); // walk
  42. model1.position.x = - 2;
  43. model2.position.x = 0;
  44. // model3.position.x = 2;
  45. // scene.add( model1, model2, model3 );
  46. scene.add( model1, model2);
  47. // mixers.push( mixer1, mixer2, mixer3 );
  48. mixers.push( mixer1, mixer2);
  49. animate();
  50. } );
  51. renderer = new THREE.WebGLRenderer( { antialias: true } );
  52. renderer.setPixelRatio( window.devicePixelRatio );
  53. renderer.setSize( window.innerWidth, window.innerHeight );
  54. renderer.outputEncoding = THREE.sRGBEncoding;
  55. renderer.shadowMap.enabled = true;
  56. document.body.appendChild( renderer.domElement );
  57. window.addEventListener( 'resize', onWindowResize );
  58. }

关键代码剖析:

加载gltf、glb的函数是:loader.load( 'models/gltf/Soldier.glb', function ( gltf ) {}),

第二个参数是一个回调函数,当加载完毕之后,我们就开始调用骨骼动画了。这里用到了SkeletonUtils这个类。

注意选择具体的动画的地方就是mixer1.clipAction一句。gltf.animations就是上面提到的动画数组,有4个元素。

4、增加一个animate方法。

  1. function animate() {
  2. requestAnimationFrame( animate );
  3. const delta = clock.getDelta();
  4. for ( const mixer of mixers ) mixer.update( delta );
  5. model1.position.z -= 0.03;
  6. model2.position.z -= 0.01;
  7. renderer.render( scene, camera );
  8. }

代码剖析:

在这段代码里面,model1和model2的position属性被不停地更改,所以在场景刷新的时候,人物就走动起来了。

5、当然最后还是要调用一下这两个方法:

  1. init();
  2. animate();

至此,整个步行动画效果就出来了。

threejs下人物行走

完整的代码下载:

完整代码包下载(含模型资源)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/521363
推荐阅读
相关标签
  

闽ICP备14008679号