1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| let model, mixer, head, body, rightArm, leftArm, rightLeg, leftLeg; const clock = new THREE.Clock(); let mouseX = 0, mouseY = 0;
function init() { var container = document.getElementById('threejs-container'); if (!container) { console.error("Element with ID 'threejs-container' not found."); return; }
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, container.clientWidth / container.clientHeight, 0.1, 1000); camera.position.z = 3;
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true }); renderer.setSize(container.clientWidth, container.clientHeight); renderer.setClearColor(0xffffff,0); container.appendChild(renderer.domElement);
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5); scene.add(ambientLight);
const hemisphereLight = new THREE.HemisphereLight(0xffffff, 0x444444, 1); hemisphereLight.position.set(0, 1, 0); scene.add(hemisphereLight);
const loader = new THREE.GLTFLoader(); loader.load('/models/model.gltf', function(gltf) { model = gltf.scene; model.rotation.y = Math.PI; model.position.set(0, -1, 0); scene.add(model);
head = model.getObjectByName('Head'); body = model.getObjectByName('Body'); rightArm = model.getObjectByName('RightArm'); leftArm = model.getObjectByName('LeftArm'); rightLeg = model.getObjectByName('RightLeg'); leftLeg = model.getObjectByName('LeftLeg');
mixer = new THREE.AnimationMixer(model);
const rightArmRotationKF = new THREE.QuaternionKeyframeTrack('RightArm.quaternion', [0, 0.5, 1], [ ...new THREE.Quaternion().setFromEuler(new THREE.Euler(0.3, 0, 0)).toArray(), ...new THREE.Quaternion().setFromEuler(new THREE.Euler(-0.3, 0, 0)).toArray(), ...new THREE.Quaternion().setFromEuler(new THREE.Euler(0.3, 0, 0)).toArray() ]);
const leftArmRotationKF = new THREE.QuaternionKeyframeTrack('LeftArm.quaternion', [0, 0.5, 1], [ ...new THREE.Quaternion().setFromEuler(new THREE.Euler(-0.3, 0, 0)).toArray(), ...new THREE.Quaternion().setFromEuler(new THREE.Euler(0.3, 0, 0)).toArray(), ...new THREE.Quaternion().setFromEuler(new THREE.Euler(-0.3, 0, 0)).toArray() ]);
const rightLegRotationKF = new THREE.QuaternionKeyframeTrack('RightLeg.quaternion', [0, 0.5, 1], [ ...new THREE.Quaternion().setFromEuler(new THREE.Euler(-0.3, 0, 0)).toArray(), ...new THREE.Quaternion().setFromEuler(new THREE.Euler(0.3, 0, 0)).toArray(), ...new THREE.Quaternion().setFromEuler(new THREE.Euler(-0.3, 0, 0)).toArray() ]);
const leftLegRotationKF = new THREE.QuaternionKeyframeTrack('LeftLeg.quaternion', [0, 0.5, 1], [ ...new THREE.Quaternion().setFromEuler(new THREE.Euler(0.3, 0, 0)).toArray(), ...new THREE.Quaternion().setFromEuler(new THREE.Euler(-0.3, 0, 0)).toArray(), ...new THREE.Quaternion().setFromEuler(new THREE.Euler(0.3, 0, 0)).toArray() ]);
const clip = new THREE.AnimationClip('walk', 1, [ rightArmRotationKF, leftArmRotationKF, rightLegRotationKF, leftLegRotationKF ]);
const action = mixer.clipAction(clip); action.setLoop(THREE.LoopRepeat); action.play(); });
document.addEventListener('mousemove', onDocumentMouseMove, false);
function animate() { requestAnimationFrame(animate);
if (mixer) { mixer.update(clock.getDelta()); }
if (head) { head.rotation.y = mouseX * Math.PI * 0.3; head.rotation.x = mouseY * Math.PI * 0.1; }
if (body) { body.rotation.y = mouseX * Math.PI * 0.1; }
if (rightArm && leftArm) { rightArm.rotation.y = mouseX * Math.PI * 0.05; leftArm.rotation.y = mouseX * Math.PI * 0.05; }
if (rightLeg && leftLeg) { rightLeg.rotation.y = mouseX * Math.PI * 0.05; leftLeg.rotation.y = mouseX * Math.PI * 0.05; }
renderer.render(scene, camera); }
animate(); }
function onDocumentMouseMove(event) { mouseX = (event.clientX / window.innerWidth) * 2 - 1; mouseY = -(event.clientY / window.innerHeight) * 2 + 1; }
document.addEventListener('DOMContentLoaded', function() { init(); });
|