To animate your 3D model using GSAP's ScrollTrigger, you'll need to integrate GSAP and its ScrollTrigger plugin with your existing Three.js setup. Here's a basic guide on how you can achieve this:
-
Set up GSAP and ScrollTrigger: Ensure you have GSAP and ScrollTrigger available in your project. You can do this by importing them from a CDN, similar to how you imported Three.js, or by using npm if it's a node environment.
-
Integrate ScrollTrigger with your Three.js animation: Use the ScrollTrigger to handle updates to the model's rotation as the user scrolls.
Here's how you can modify your existing code to include GSAP with ScrollTrigger:
import * as THREE from "https://cdn.jsdelivr.net/npm/three@0.121.1/build/three.module.js";
import { GLTFLoader } from "https://cdn.jsdelivr.net/npm/three@0.121.1/examples/jsm/loaders/GLTFLoader.js";
import gsap from "https://cdn.jsdelivr.net/npm/gsap@3.10.4/dist/gsap.min.js";
import { ScrollTrigger } from "https://cdn.jsdelivr.net/npm/gsap@3.10.4/dist/ScrollTrigger.min.js";
gsap.registerPlugin(ScrollTrigger);
document.addEventListener("DOMContentLoaded", () => {
const container = document.getElementById("custom-three-js");
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
75,
container.clientWidth / container.clientHeight,
0.1,
1000
);
const renderer = new THREE.WebGLRenderer({
canvas: container,
alpha: true,
});
renderer.setSize(container.clientWidth, container.clientHeight);
renderer.setClearColor(0x000000, 0);
const loader = new GLTFLoader();
let model; // Declare a variable to store the loaded model
loader.load(
"/react/red-diamond.glb",
(gltf) => {
model = gltf.scene; // Assign the loaded scene (model) to the variable
model.rotation.y = -Math.PI * 0.3;
scene.add(model);
// Set up the ScrollTrigger timeline
gsap.to(model.rotation, {
scrollTrigger: {
trigger: container,
start: "top top", // Adjust these values to your preference
end: "bottom top",
scrub: true,
},
x: Math.PI * 2, // Rotate a full circle on the x-axis
y: Math.PI * 2, // Rotate a full circle on the y-axis
ease: "none",
});
},
undefined,
(error) => {
console.error("An error occurred while loading the model", error);
}
);
const light = new THREE.AmbientLight(0xffffff); // Soft white light
scene.add(light);
const environmentTexture = new THREE.CubeTextureLoader()
.setPath("/react/env/")
.load(["px.png", "nx.png", "py.png", "ny.png", "pz.png", "nz.png"]);
scene.environment = environmentTexture;
camera.position.z = 4;
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();
});
Explanation:
-
GSAP and ScrollTrigger Import: We import both GSAP and ScrollTrigger and register ScrollTrigger as a plugin.
-
ScrollTrigger Configuration: After loading the GLTF model, we set up a GSAP animation that uses ScrollTrigger. The scrub: true option allows the model's rotation to be in sync with the scroll position.
-
Rotation: Modify the degrees or axes for the rotation according to your needs. You can customize the start, end, and other parameters to achieve the desired animation effect.
Ensure your HTML and CSS are appropriately structured to allow scrolling, and make sure your Three.js and GSAP imports align correctly with your project setup. Adjust paths for models and textures as needed.