// Function to convert lat/lon to 3D position on sphere
function latLonToVector3(lat, lon, radius = 5) {
const phi = (90 - lat) * (Math.PI / 180); // Convert latitude to radians
const theta = (lon + 180) * (Math.PI / 180); // Convert longitude to radians
const x = -(radius * Math.sin(phi) * Math.cos(theta));
const y = radius * Math.cos(phi);
const z = radius * Math.sin(phi) * Math.sin(theta);
return new THREE.Vector3(x, y, z);
}
// Add markers to the globe
markerData.forEach((data) => {
const markerGeometry = new THREE.RingGeometry(0.08, 0.15, 50);
const markerMaterial = new THREE.MeshBasicMaterial({
color: 0xffffff,
});
const marker = new THREE.Mesh(markerGeometry, markerMaterial);
// Position marker using lat/lon
const position = latLonToVector3(data.lat, data.lon);
marker.position.copy(position);
// Store label data in marker's userData
marker.userData.label = data.label;
// Face the marker outward (towards the outside of the sphere)
marker.lookAt(marker.position.clone().normalize().multiplyScalar(10));
globeGroup.add(marker);
markers.push(marker);
});
// Raycaster for mouse interaction
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
let isHovering = false;
// Create a DOM element for displaying text on hover
// Clock for animation
const clock = new THREE.Clock();
// Animation loop
function animate() {
// Get elapsed time
const elapsedTime = clock.getElapsedTime();
// Smooth levitation using a sine wave
const levitationHeight = 0.2; // Adjust for how high it levitates
globeGroup.position.y = Math.sin(elapsedTime * 2) * levitationHeight;
// Update Orbit Controls
controls.update();
// Render the Scene
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
convert to react three fiber
To convert your three.js code into a React application using React Three Fiber, we can break down the original code and structure it in a way that leverages React components. Here's a step-by-step breakdown of how to do it.
1. Setup the React Project
Make sure you have a React project set up. You can create one using Create React App:
npx create-react-app my-three-app
cd my-three-app
2. Install Required Dependencies
You'll need to install @react-three/fiber and @react-three/drei:
npm install @react-three/fiber @react-three/drei
3. Convert the Code to React Three Fiber
Below is the conversion of your three.js code into a React component using React Three Fiber:
React Components: The code is structured into functional components (Sphere, Globe, Markers, Animation, and the main App).
Shaders and Textures: The loading of textures and shader materials is managed with hooks like useLoader.
Rendering: The rendering is handled by <Canvas> from React Three Fiber which acts as a wrapper for the Three.js scene.
Hover Effects: You can implement hover effects using a combination of event listeners and state management in React (the example provides a template).
Add Interaction Features
To handle the hover events detected in your original code, you can use raycasting while utilizing useFrame hook for checking interactions during the animation loop.
React Three Fiber makes it relatively straightforward to integrate advanced features with components, so feel free to expand on this base example!
Your conversion is on the right track! You've effectively structured your React application using React Three Fiber, with separate components for the World, Sphere, and Globe. Below, I will highlight the improvements you can make and ensure everything is clear and functioning well together.
1. Consolidate Exports
Make sure to export your components properly. If you are defining them in separate files, you should only introduce one export statement in each file. For instance, Sphere and Globe should have their exports correctly defined.
2. Add Markers
If you decide to include markers on the globe later, consider adding a Markers component, as initially highlighted.
3. Interaction (Mouse Events)
For interactivity, you may want to build hover effects or clickable markers on the globe. Consider utilizing useFrame or other hooks to achieve this.
4. Overall Structure
Adjust your imports and structure for better readability and maintainability.
Here’s how you can structure your application effectively:
Component Imports: Each component is cleanly separated, allowing for easy maintenance.
TypeScript: Ensure that your components can benefit from TypeScript's type-checking if applicable.
Reactivity and Interaction: Use hooks from React to handle user interactions later, such as changing the hover behavior or marker clicks.
Next Steps:
Markers Implementation: If you decide to implement markers, create another component for them.
Interactivity: Develop hover and click functionalities using raycasting for user engagement.
Responsive Design: Ensure that your 3D elements respond to resizing events to keep the user experience smooth.
Your implementation already exhibits good structure. Keep exploring and adjusting based on your project's needs! If you have specific questions or need help with particular features, feel free to ask!
To add markers to your globe in a React Three Fiber project, we will create a new component called Markers that will leverage the latitude and longitude data of each marker to position them on the globe. You'll also need to implement mouse interaction so that the markers can respond to hover events.
Here’s how to implement this in steps:
Create a Markers Component: This component will read the latitude and longitude of each marker and compute their position on the globe.
Add Interaction: Integrate hover effects for displaying information when the markers are hovered over.
Markers Data: The markersData array defines the latitude, longitude, and label for each marker.
Markers Component: In the Markers component, we calculate the 3D position of each marker using the latLonToVector3 function. We use the onPointerOver and onPointerOut events to set the hovered marker’s state.
Hover State: In the World component, we manage hover state through setHoveredMarker that updates the local state when a marker is hovered over.
Marker Details Display: When a marker is hovered, a small label with the marker's name appears on the screen.
Final Touches
Make sure you have the necessary textures (/map.jpg and /circle.png). The texture paths should align with your project’s folder structure.
You can also further customize the appearance of the markers and the label as desired, adding CSS for styling if necessary.
Now your globe will display markers representing cities, and you can see their names when you hover over them! If you have any more questions or need further help, feel free to ask!