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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| 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) { const targetRotationY = mouseX * Math.PI * 0.3; const targetRotationX = mouseY * Math.PI * 0.1;
const maxRotation = Math.PI / 4; const clampedRotationY = Math.max(-maxRotation, Math.min(maxRotation, targetRotationY)); const clampedRotationX = Math.max(-maxRotation/3, Math.min(maxRotation/3, targetRotationX));
head.rotation.y += (clampedRotationY - head.rotation.y) * lerpFactor; head.rotation.x += (clampedRotationX - head.rotation.x) * lerpFactor; }
if (body) { const targetRotationY = mouseX * Math.PI * 0.1; const maxRotation = Math.PI / 8; const clampedRotationY = Math.max(-maxRotation, Math.min(maxRotation, targetRotationY)); body.rotation.y += (clampedRotationY - body.rotation.y) * lerpFactor }
if (rightArm && leftArm) { const targetRotationY = mouseX * Math.PI * 0.1; const maxRotation = Math.PI / 8; const clampedRotationY = Math.max(-maxRotation, Math.min(maxRotation, targetRotationY)); rightArm.rotation.y += (clampedRotationY - rightArm.rotation.y) * lerpFactor; leftArm.rotation.y += (clampedRotationY - leftArm.rotation.y) * lerpFactor; }
if (rightLeg && leftLeg) { const targetRotationY = mouseX * Math.PI * 0.1; const maxRotation = Math.PI / 8; const clampedRotationY = Math.max(-maxRotation, Math.min(maxRotation, targetRotationY));
rightLeg.rotation.y += (clampedRotationY - rightLeg.rotation.y) * lerpFactor; leftLeg.rotation.y += (clampedRotationY - leftLeg.rotation.y) * lerpFactor; }
renderer.render(scene, camera); }
animate(); }
function onDocumentMouseMove(event) { const container = document.getElementById('threejs-container'); const rect = container.getBoundingClientRect(); mouseX = ((event.clientX - rect.left) / rect.width) * 2 - 1; mouseY = -((event.clientY - rect.top) / rect.height) * 2 + 1; }
document.addEventListener('DOMContentLoaded', function() { init(); });
|