-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
81 lines (64 loc) · 2.17 KB
/
main.js
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
// Escena
const scene = new THREE.Scene();
// Cámara
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
camera.position.z = 2;
// Renderizador
const renderer = new THREE.WebGLRenderer({ alpha: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
document.body.appendChild(renderer.domElement);
// Cargar la textura
const textureLoader = new THREE.TextureLoader();
const texture = textureLoader.load("../assets/texture.jpg");
const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshStandardMaterial({
map: texture,
side: THREE.DoubleSide,
});
const cube = new THREE.Mesh(geometry, material);
cube.castShadow = true;
cube.position.y = 0.1;
scene.add(cube);
// Añadir una fuente de luz
const light = new THREE.PointLight(0xffffff, 1.5, 100);
light.position.set(5, 5, 5);
light.castShadow = true;
scene.add(light);
// Añadir un plano para recibir sombras
const planeGeometry = new THREE.PlaneGeometry(10, 10);
const planeMaterial = new THREE.ShadowMaterial({ opacity: 0.5 });
const plane = new THREE.Mesh(planeGeometry, planeMaterial);
plane.rotation.x = -Math.PI / 2;
plane.position.y = -0.8;
plane.receiveShadow = true;
scene.add(plane);
// Vector para la posición del ratón
const mouse = new THREE.Vector2();
const targetQuaternion = new THREE.Quaternion();
// Función para actualizar la posición del ratón
function onMouseMove(event) {
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
}
// Evento de movimiento del ratón
window.addEventListener("mousemove", onMouseMove, false);
const lerpFactor = 0.1;
function animate() {
requestAnimationFrame(animate);
const vector = new THREE.Vector3(mouse.x, mouse.y, 0.5);
vector.unproject(camera);
const direction = vector.sub(camera.position).normalize();
const targetPosition = camera.position.clone().add(direction);
targetQuaternion.setFromRotationMatrix(
new THREE.Matrix4().lookAt(cube.position, targetPosition, camera.up)
);
cube.quaternion.slerp(targetQuaternion, lerpFactor);
renderer.render(scene, camera);
}
animate();