Learn how to add a stunning 3D model to your Next.js website using Three.js and TypeScript. Create interactive 3D scenes to captivate your users.
Are you ready to take your Next.js website to the next level by incorporating captivating 3D models? In this comprehensive guide, I'll walk you through the process of integrating a 3D model into your Next.js application using Three.js and TypeScript. With interactive 3D scenes, you can create an engaging and immersive user experience that will leave your visitors amazed.
Three.js is a popular JavaScript library that simplifies 3D graphics rendering in the browser. It provides a wide range of tools and utilities for creating and manipulating 3D scenes, making it an ideal choice for web developers who want to add interactive 3D elements to their websites.
Before we begin, ensure you have the following set up:
Let's start by creating a new Next.js project. Open your terminal and run the following command:
npx create-next-app my-3d-model-app
During the project setup, you'll be prompted to choose various options. Make sure to select the following:
Would you like to use TypeScript? => Yes Would you like to use ESLint? => Yes Would you like to use Tailwind CSS? => No Would you like to use `src/` directory? => Yes Would you like to use App Router? (recommended) => Yes Would you like to customize the default import alias? => No
After the setup is complete, navigate into the project folder:
cd my-3d-model-app
Next, let's install Three.js and other required dependencies. Run the following command in your project directory:
yarn add three @react-three/fiber yarn add --dev @types/three
The three
package contains the Three.js library, while @react-three/fiber
is an additional package that make working with Three.js in React applications much easier.
For our example, we will use the Ethereum 3D model created by jihambru. You can find free 3D models online on websites like Sketchfab or TurboSquid.
Download the Ethereum 3D model from my repository and place it in the /public
folder of your Next.js project.
In your Next.js project, create a new component to hold the 3D scene. For example, let's create a new /src/component
folder and a model-viewer.tsx
component with the following code:
'use client';
import { Canvas, useLoader } from '@react-three/fiber';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
export const ModelViewer: React.FC = () => {
const myModel = useLoader(GLTFLoader, '/ethereum-logo.glb');
return (
<Canvas style={{ height: '500px', width: '100%' }}>
<pointLight position={[-10, -10, -10]} color="#48cc90" intensity={5000} />
<pointLight position={[10, 10, 10]} color="#36e2e2" intensity={5000} />
<primitive object={myModel.scene} />
</Canvas>
);
};
The ModelViewer
component will load and display the 3D model in a canvas, along with adding some lights to the scene. The use client directive ensures that Next.js renders the component only on the client side.
Now, let's use the ModelViewer
component we created in our main page. Open the app/page.tsx
file and modify it as follows:
import { ModelViewer } from '../components/model-viewer';
export default function Home() {
return (
<div>
<h1>Welcome to My Next.js 3D Model App</h1>
<ModelViewer />
</div>
);
}
Save your changes and start the Next.js development server:
yarn dev
Visit http://localhost:3000
in your browser to see the 3D model integrated into your Next.js application!
Use the useFrame
hook from @react-three/fiber
in order to add a simple animation to your scene. As the hook must be used inside the <Canvas />
context, we need to modify the ModelViewer
component in order to have the context available on the EthereumModel
component:
'use client';
import { Canvas, useFrame, useLoader } from '@react-three/fiber';
import { useRef } from 'react';
import { Mesh } from 'three';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
export const ModelViewer: React.FC = () => {
return (
<Canvas style={{ height: '500px', width: '100%' }}>
<EthereumModel />
</Canvas>
);
};
export const EthereumModel: React.FC = () => {
const myModel = useLoader(GLTFLoader, '/ethereum-logo.glb');
const modelRef = useRef<Mesh>(null);
useFrame((_state, delta) => {
if (modelRef.current) {
modelRef.current.rotation.y += delta / 2;
}
});
return (
<>
<pointLight position={[-10, -10, -10]} color="#48cc90" intensity={5000} />
<pointLight position={[10, 10, 10]} color="#36e2e2" intensity={5000} />
<primitive object={myModel.scene} ref={modelRef} />
</>
);
};
Congratulations! You've successfully integrated a 3D model into your Next.js application using Three.js and TypeScript. You can now experiment with different 3D models, lighting and camera settings to create engaging and interactive 3D experiences for your website.
Happy coding and stay tuned for more blog posts and tutorials! If you have any questions or want to showcase your creations, feel free to connect with me on my socials.
0xforkitall
Tuesday, 1 August 2023