Top 5 Skills that AI Can't Replace
5 mins
Jan 7, 2025
Published on
Dec 17, 2024
6 min
Anmol
3D graphics and animations bring depth and interactivity to applications. In this blog, we’ll explore using the Three.js library with the @react-three/fiber package in React Native to create stunning 3D visualizations. We’ll cover installation, setup for Android and iOS, writing code, and using platforms like CGTrader to download or create custom 3D models.
Three.js is a popular JavaScript library for creating 3D content using WebGL. It’s widely used for building 3D models, interactive visualizations, animations, and even games.
@react-three/fiber is a React renderer for Three.js. It abstracts Three.js into declarative React components, making it easier to integrate 3D graphics into React applications.
npx react-native init ThreeExample
cd ThreeExample
Run the following command to install Three.js, @react-three/fiber, and type definitions:
npm install three @types/three @react-three/fiber
npm install react-native-webview
If you’re using React Native below version 0.60, link the libraries manually:
react-native link react-native-webview
Navigate to the ios directory and install CocoaPods dependencies:
cd ios
pod install
Here’s an example of a basic 3D scene in React Native using Three.js and @react-three/fiber.
import React from 'react';
import { View, StyleSheet } from 'react-native';
import { Canvas } from '@react-three/fiber';
import { Mesh } from 'three';
export default function ThreeDVisualization() {
return (
<View style={styles.container}>
<Canvas style={{ flex: 1 }}>
<ambientLight intensity={0.5} />
<pointLight position={[10, 10, 10]} />
<mesh>
<boxGeometry args={[2, 2, 2]} />
<meshStandardMaterial color="orange" />
</mesh>
</Canvas>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#000',
},
});
CGTrader is a platform for downloading and purchasing 3D models. It offers a vast collection of free and paid models, including those for games, animations, and visualizations.
If you need bespoke 3D models, you can create them using powerful tools like:
Here’s how to import a 3D model (e.g., .gltf file) and render it in your app:
import React from 'react';
import { View, StyleSheet } from 'react-native';
import { Canvas, useLoader } from '@react-three/fiber';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader';
export default function ThreeDModel() {
const model = useLoader(GLTFLoader, './path-to-your-model.gltf');
return (
<View style={styles.container}>
<Canvas style={{ flex: 1 }}>
<ambientLight intensity={0.5} />
<pointLight position={[10, 10, 10]} />
<primitive object={model.scene} scale={1.5} />
</Canvas>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#000',
},
});
Run:
npx react-native run-android
Run:
npx react-native run-ios
Using Three.js and @react-three/fiber in React Native opens up endless possibilities for creating 3D graphics and animations. Platforms like CGTrader provide a vast repository of models, while tools like Blender and Maya enable custom model creation.
By combining these technologies, you can build visually engaging and interactive applications for Android and iOS. Dive into the world of 3D graphics and take your apps to the next level!
LEARN MORE
Video 1 : https://www.youtube.com/watch?v=1ahs5R1mPhw
Video 2: https://www.youtube.com/watch?v=GyPyBysxvg4
Happy coding! 🎉