const appData = getApp().globalData; // import "./three/ColladaLoader"; // import "./three/DeviceOrientationControls"; //import "./three/NURBSCurve"; // import "./three/NURBSUtils"; //import "./three/OrbitControls_new"; // import "./three/Tween_new"; //var THREE = require("./three/three_new"); import { registerFBXLoader } from './FBXLoader_new.js' var fbxModelLoadPig = (canvas, animationUrl, THREE, startTime) => { appData.innerWidth = wx.getSystemInfoSync().windowWidth; appData.innerHeight = wx.getSystemInfoSync().windowHeight; if (!/.fbx/i.test(animationUrl)) { return; } let play_type = 1; registerFBXLoader(THREE); let container, stats, controls; let camera, scene, renderer, light; let frame = 0; let clock = new THREE.Clock(); let mixers = []; var model; var actions, activeAction; init(); function init() { camera = new THREE.PerspectiveCamera(75, appData.innerWidth / appData.innerHeight, 0.15, 2000); camera.position.set(0, 0, 500); camera.lookAt(new THREE.Vector3(0, 0, 0)); scene = new THREE.Scene(); light = new THREE.HemisphereLight(0xffffff, 0x444444, 1.0); light.position.set(0, 1, 0); scene.add(light); light = new THREE.DirectionalLight(0xffffff, 1.0); light.position.set(0, 1, 0); scene.add(light); let onError = function(xhr) { console.log(xhr); }; var loader = new THREE.FBXLoader(); //'/static/Celebration/model/RenminUniversity_animation.fbx' loader.load(animationUrl, function(fbx) { //animationConfigInfo.animationModelUrl:fbx模型的文件名 model = fbx; scene.add(model); fbx.mixer = new THREE.AnimationMixer(fbx); mixers.push(fbx.mixer); let action = fbx.mixer.clipAction(fbx.animations[0]); action.setEffectiveTimeScale(0.8).play(); // let stop=function(event){ // object.mixer.clipAction(object.animations[0]).paused = true ; // document.body.removeEventListener("estop",stop,false); // }; // let stop1=function(event){ // object.mixer.clipAction(object.animations[0]).paused = true ; // document.body.removeEventListener("estop1",stop1,false); // }; // let play=function(event){ // object.mixer.clipAction(object.animations[0]).paused = false ; // frame = 0; // play_type = 1; // document.addEventListener("estop1",stop1,false); // document.body.removeEventListener("estop",stop,false); // }; // document.addEventListener("estop",stop,false); // //document.addEventListener("estop1",stop1,false); // document.addEventListener("play",play,false); // //action.play(); // //action.setDuration(18).play(); // action.setEffectiveTimeScale ( 0.8 ).play(); // scene.add(object); // object.position.set(0,-10,0); // object.scale.set(3,3,3); // // console.log(camera); console.log("动画加载用了" + (Date.now() - startTime) / 1000 + "秒"); }, undefined, onError); renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true }); renderer.setPixelRatio(wx.getSystemInfoSync().pixelRatio); renderer.setSize(appData.innerWidth, appData.innerWidth); renderer.gammaOutput = true; renderer.gammaFactor = 2.2; // container.appendChild( renderer.domElement ); //?????原来有 // controls = new THREE.OrbitControls( camera, renderer.domElement ); //???原有 // camera.position.set(0.001,40,150); // controls.target.set(0,0,-40); // controls.update(); //window.addEventListener( 'resize', onWindowResize, false ); //????原有 animate(); } function onWindowResize() { camera.aspect = window.innerWidth / window.innerHeight; camera.updateProjectionMatrix(); renderer.setSize(window.innerWidth, window.innerHeight); } function createGUI(model, animations) { var states = ['Idle', 'Walking', 'Running', 'Dance', 'Death', 'Sitting', 'Standing']; var emotes = ['Jump', 'Yes', 'No', 'Wave', 'Punch', 'ThumbsUp']; mixer = new THREE.AnimationMixer(model); actions = {}; for (var i = 0; i < animations.length; i++) { var clip = animations[i]; var action = mixer.clipAction(clip); actions[clip.name] = action; if (emotes.indexOf(clip.name) >= 0 || states.indexOf(clip.name) >= 4) { action.clampWhenFinished = true; action.loop = THREE.LoopOnce; } } // expressions face = model.getObjectByName('Head_2'); activeAction = actions['Walking']; activeAction.play(); } //three.js播放动画 function animate() { canvas.requestAnimationFrame(animate); if (mixers.length > 0) { for (let i = 0; i < mixers.length; i++) { let delta = clock.getDelta(); mixers[i].update(delta); frame = frame + delta; if (play_type == 0) { if (frame > 0.01) { let estop = new Event("estop"); document.dispatchEvent(estop); play_type = -1; } } } } render(); } //three.js动画渲染 function render() { renderer.render(scene, camera); } } module.exports = { fbxModelLoadPig: fbxModelLoadPig }