model.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. const appData = getApp().globalData;
  2. // import "./three/ColladaLoader";
  3. // import "./three/DeviceOrientationControls";
  4. //import "./three/NURBSCurve";
  5. // import "./three/NURBSUtils";
  6. //import "./three/OrbitControls_new";
  7. // import "./three/Tween_new";
  8. //var THREE = require("./three/three_new");
  9. import {
  10. registerFBXLoader
  11. } from './FBXLoader_new.js'
  12. var fbxModelLoadPig = (canvas, animationUrl, THREE, startTime) => {
  13. appData.innerWidth = wx.getSystemInfoSync().windowWidth;
  14. appData.innerHeight = wx.getSystemInfoSync().windowHeight;
  15. if (!/.fbx/i.test(animationUrl)) {
  16. return;
  17. }
  18. let play_type = 1;
  19. registerFBXLoader(THREE);
  20. let container, stats, controls;
  21. let camera, scene, renderer, light;
  22. let frame = 0;
  23. let clock = new THREE.Clock();
  24. let mixers = [];
  25. var model;
  26. var actions, activeAction;
  27. init();
  28. function init() {
  29. camera = new THREE.PerspectiveCamera(75, appData.innerWidth / appData.innerHeight, 0.15, 2000);
  30. camera.position.set(0, 0, 500);
  31. camera.lookAt(new THREE.Vector3(0, 0, 0));
  32. scene = new THREE.Scene();
  33. light = new THREE.HemisphereLight(0xffffff, 0x444444, 1.0);
  34. light.position.set(0, 1, 0);
  35. scene.add(light);
  36. light = new THREE.DirectionalLight(0xffffff, 1.0);
  37. light.position.set(0, 1, 0);
  38. scene.add(light);
  39. let onError = function(xhr) {
  40. console.log(xhr);
  41. };
  42. var loader = new THREE.FBXLoader(); //'/static/Celebration/model/RenminUniversity_animation.fbx'
  43. loader.load(animationUrl, function(fbx) { //animationConfigInfo.animationModelUrl:fbx模型的文件名
  44. model = fbx;
  45. scene.add(model);
  46. fbx.mixer = new THREE.AnimationMixer(fbx);
  47. mixers.push(fbx.mixer);
  48. let action = fbx.mixer.clipAction(fbx.animations[0]);
  49. action.setEffectiveTimeScale(0.8).play();
  50. // let stop=function(event){
  51. // object.mixer.clipAction(object.animations[0]).paused = true ;
  52. // document.body.removeEventListener("estop",stop,false);
  53. // };
  54. // let stop1=function(event){
  55. // object.mixer.clipAction(object.animations[0]).paused = true ;
  56. // document.body.removeEventListener("estop1",stop1,false);
  57. // };
  58. // let play=function(event){
  59. // object.mixer.clipAction(object.animations[0]).paused = false ;
  60. // frame = 0;
  61. // play_type = 1;
  62. // document.addEventListener("estop1",stop1,false);
  63. // document.body.removeEventListener("estop",stop,false);
  64. // };
  65. // document.addEventListener("estop",stop,false);
  66. // //document.addEventListener("estop1",stop1,false);
  67. // document.addEventListener("play",play,false);
  68. // //action.play();
  69. // //action.setDuration(18).play();
  70. // action.setEffectiveTimeScale ( 0.8 ).play();
  71. // scene.add(object);
  72. // object.position.set(0,-10,0);
  73. // object.scale.set(3,3,3);
  74. // // console.log(camera);
  75. console.log("动画加载用了" + (Date.now() - startTime) / 1000 + "秒");
  76. }, undefined, onError);
  77. renderer = new THREE.WebGLRenderer({
  78. antialias: true,
  79. alpha: true
  80. });
  81. renderer.setPixelRatio(wx.getSystemInfoSync().pixelRatio);
  82. renderer.setSize(appData.innerWidth, appData.innerWidth);
  83. renderer.gammaOutput = true;
  84. renderer.gammaFactor = 2.2;
  85. // container.appendChild( renderer.domElement ); //?????原来有
  86. // controls = new THREE.OrbitControls( camera, renderer.domElement ); //???原有
  87. // camera.position.set(0.001,40,150);
  88. // controls.target.set(0,0,-40);
  89. // controls.update();
  90. //window.addEventListener( 'resize', onWindowResize, false ); //????原有
  91. animate();
  92. }
  93. function onWindowResize() {
  94. camera.aspect = window.innerWidth / window.innerHeight;
  95. camera.updateProjectionMatrix();
  96. renderer.setSize(window.innerWidth, window.innerHeight);
  97. }
  98. function createGUI(model, animations) {
  99. var states = ['Idle', 'Walking', 'Running', 'Dance', 'Death', 'Sitting', 'Standing'];
  100. var emotes = ['Jump', 'Yes', 'No', 'Wave', 'Punch', 'ThumbsUp'];
  101. mixer = new THREE.AnimationMixer(model);
  102. actions = {};
  103. for (var i = 0; i < animations.length; i++) {
  104. var clip = animations[i];
  105. var action = mixer.clipAction(clip);
  106. actions[clip.name] = action;
  107. if (emotes.indexOf(clip.name) >= 0 || states.indexOf(clip.name) >= 4) {
  108. action.clampWhenFinished = true;
  109. action.loop = THREE.LoopOnce;
  110. }
  111. }
  112. // expressions
  113. face = model.getObjectByName('Head_2');
  114. activeAction = actions['Walking'];
  115. activeAction.play();
  116. }
  117. //three.js播放动画
  118. function animate() {
  119. canvas.requestAnimationFrame(animate);
  120. if (mixers.length > 0) {
  121. for (let i = 0; i < mixers.length; i++) {
  122. let delta = clock.getDelta();
  123. mixers[i].update(delta);
  124. frame = frame + delta;
  125. if (play_type == 0) {
  126. if (frame > 0.01) {
  127. let estop = new Event("estop");
  128. document.dispatchEvent(estop);
  129. play_type = -1;
  130. }
  131. }
  132. }
  133. }
  134. render();
  135. }
  136. //three.js动画渲染
  137. function render() {
  138. renderer.render(scene, camera);
  139. }
  140. }
  141. module.exports = {
  142. fbxModelLoadPig: fbxModelLoadPig
  143. }