Wawa Sensei logo
LoginEnroll now

Animaciones

Starter pack

Las animaciones son clave para dar vida a tu escena 3D. Pueden ser activadas por interacciones del usuario, basadas en scroll o en tiempo, y aplicadas a objetos 3D, luces y c谩maras.

Dominar las animaciones es una habilidad clave para crear experiencias inmersivas. Cuantas m谩s t茅cnicas conozcas, m谩s podr谩s expresar tu creatividad.

Nota: Las animaciones de modelos 3D se cubren en el cap铆tulo de modelos.

Lerp

Lerp es una funci贸n matem谩tica que interpola entre dos valores. Es 煤til para animar un valor desde un punto a otro.

const value = THREE.MathUtils.lerp(start, end, t);

El primer par谩metro es el valor inicial, el segundo par谩metro es el valor final y el tercer par谩metro es el factor de interpolaci贸n entre 0 y 1.

Cuanto m谩s cercano est茅 el factor de interpolaci贸n a 0, m谩s lenta ser谩 la animaci贸n. Cuanto m谩s cercano est茅 el factor de interpolaci贸n a 1, m谩s r谩pido alcanzar谩 la animaci贸n el valor final o de destino.

Experimentemos esto en el componente AnimatedBox del paquete de inicio.

El componente tiene un array boxPositions que contiene las posiciones de la caja en diferentes momentos. Vamos a a帽adir un hook useFrame para actualizar la posici贸n de la caja en funci贸n del tiempo:

import { RoundedBox } from "@react-three/drei";
import { useRef } from "react";

export const AnimatedBox = ({ boxPositions, ...props }) => {
  const box = useRef();

  useFrame(({ clock }) => {
    const seconds = parseInt(clock.getElapsedTime());
    const targetPosition = boxPositions[seconds % boxPositions.length];

    box.current.position.x = targetPosition.x;
    box.current.position.y = targetPosition.y;
    box.current.position.z = targetPosition.z;
  });
  // ...
};

Aqu铆 nuestra caja no est谩 animada. Se teletransporta de una posici贸n a otra. Usemos la funci贸n lerp para animarla:

import * as THREE from "three";
// ...

export const AnimatedBox = ({ boxPositions, ...props }) => {
  const box = useRef();

  useFrame(({ clock }) => {
    const seconds = parseInt(clock.getElapsedTime());
    const targetPosition = boxPositions[seconds % boxPositions.length];
    box.current.position.x = THREE.MathUtils.lerp(
      box.current.position.x,
      targetPosition.x,
      0.05
    );
    box.current.position.y = THREE.MathUtils.lerp(
      box.current.position.y,
      targetPosition.y,
      0.05
    );
    box.current.position.z = THREE.MathUtils.lerp(
      box.current.position.z,
      targetPosition.z,
      0.05
    );
  });
  // ...
};

La caja ahora est谩 animada. Se mueve suavemente de una posici贸n a otra.

La clase Vector3 tiene un m茅todo lerp que puede ser usado en lugar de la funci贸n THREE.MathUtils.lerp:

// box.current.position.x = THREE.MathUtils.lerp(
//   box.current.position.x,
//   targetPosition.x,
//   0.05
// );
// box.current.position.y = THREE.MathUtils.lerp(
//   box.current.position.y,
//   targetPosition.y,
//   0.05
// );
// box.current.position.z = THREE.MathUtils.lerp(
//   box.current.position.z,
//   targetPosition.z,
//   0.05
// );
box.current.position.lerp(targetPosition, 0.05);

隆Esas son muchas l铆neas de c贸digo ahorradas para el mismo resultado!

No dudes en jugar con el tiempo y hacer lerp en otras propiedades como el rotation o el scale para probar diferentes efectos.

Float

Float es un componente contenedor de la librer铆a Drei para simplemente dar la impresi贸n de que un objeto est谩 flotando.

Contiene las siguientes propiedades:

  • speed: Velocidad de la animaci贸n, por defecto 1
  • rotationIntensity: Intensidad de la rotaci贸n en XYZ, por defecto 1
  • floatIntensity: Intensidad de la flotaci贸n hacia arriba/abajo, funciona como un multiplicador con floatingRange, por defecto 1
  • floatingRange: Rango de valores en el eje y en los que el objeto flotar谩, por defecto [-0.1,0.1]

Hagamos que nuestro Duck flote en Experience.jsx:

End of lesson preview

To get access to the entire lesson, you need to purchase the course.