Your implementation of the OrbitControls looks fine, but the issue is likely caused by using the CSS2DRenderer together with the THREE.WebGLRenderer. Typically, when using multiple renderers, you need to ensure you're properly calling update() on each renderer and providing respective DOM elements to OrbitControls.
The problem with OrbitControls not working could be due to the fact that you've attached it to the WebGL renderer (renderer.domElement), while the CSS2DRenderer also intercepts events, such as mouse interactions, if it's placed above the WebGL canvas in the DOM hierarchy.
Solution
To fix this issue, you need to ensure that both the WebGLRenderer and CSS2DRenderer work together harmoniously. Below are some steps to resolve the issue:
1. Ensure CSS2DRenderer is appended properly
When appending CSS2DRenderer to the DOM, it should not prevent the WebGLRenderer from receiving mouse events (like the controls). You already appended CSS2DRenderer in absolute mode, so this part appears correct.
labelRenderer.domElement.style.position = "absolute"; // Correct
labelRenderer.domElement.style.pointerEvents = "none"; // Add this line
The pointerEvents: "none" ensures that the CSS2DRenderer does not block mouse input handled by the WebGLRenderer.
2. Attach OrbitControls to both renderers
Make sure the OrbitControls instance controls both renderer.domElement and labelRenderer.domElement. For CSS2DRenderer, just render its content in sync with the WebGL renderer.
Here's what that looks like:
// OrbitControls should only take the WebGLRenderer dom element
const controls = new OrbitControls(camera, renderer.domElement);
// Update and render both renderers synchronously
function render() {
requestAnimationFrame(render);
controls.update();
renderer.render(scene, camera);
labelRenderer.render(scene, camera); // Call this to render labels
}
3. Correct auto-rotate when hovering
You seem to want to toggle autoRotate on hover and have raycasting as part of this logic. Ensure that controls.update() for OrbitControls continues to update properly, even during hover.
The following code also handles enabling or disabling auto-rotation when hovering:
function onMouseMove(event) {
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
// Update raycaster
raycaster.setFromCamera(mouse, camera);
// Check for intersection with the globe
const intersects = raycaster.intersectObject(blackGlobe, true); // Check against blackGlobe
isHovering = intersects.length > 0;
if (isHovering) {
controls.autoRotate = false; // Stop auto-rotation
} else {
controls.autoRotate = true; // Resume auto-rotation
}
}
4. Ensure Resizing is handled
Your resize logic looks good. Just ensure CSS2DRenderer is also resized along with the WebGLRenderer during a window resize:
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
labelRenderer.setSize(window.innerWidth, window.innerHeight); // Add this
});
Final Simplified Full Code
Here’s a streamlined version of your setup:
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import { CSS2DRenderer, CSS2DObject } from "three/examples/jsm/renderers/CSS2DRenderer";
// Scene
const scene = new THREE.Scene();
// Camera
const camera = new THREE.PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
1,
1000
);
camera.position.set(0, 0, 14);
// Renderer
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// CSS2DRenderer
const labelRenderer = new CSS2DRenderer();
labelRenderer.setSize(window.innerWidth, window.innerHeight);
labelRenderer.domElement.style.position = "absolute";
labelRenderer.domElement.style.top = "0";
labelRenderer.domElement.style.pointerEvents = "none"; // Prevent interference
document.body.appendChild(labelRenderer.domElement);
// Controls
const controls = new OrbitControls(camera, renderer.domElement);
controls.autoRotate = true;
controls.autoRotateSpeed = 1.0;
// Globe and texture
const geom = new THREE.SphereBufferGeometry(5, 180, 90);
const loader = new THREE.TextureLoader();
const texture = loader.load("/earthspec1k.jpg");
const globe = new THREE.Mesh(
geom,
new THREE.MeshBasicMaterial({ map: texture })
);
scene.add(globe);
// Pinpoints with labels
function createPinpoint(latitude, longitude, labelText) {
const radius = 5;
const phi = (90 - latitude) * (Math.PI / 180);
const theta = (longitude + 180) * (Math.PI / 180);
const x = -(radius * Math.sin(phi) * Math.cos(theta));
const y = radius * Math.cos(phi);
const z = radius * Math.sin(phi) * Math.sin(theta);
const pinpoint = new THREE.Mesh(
new THREE.SphereGeometry(0.1, 16, 16),
new THREE.MeshBasicMaterial({ color: 0xff0000 })
);
pinpoint.position.set(x, y, z);
// Label
const labelDiv = document.createElement("div");
labelDiv.className = "label";
labelDiv.style.color = "#fff";
labelDiv.style.backgroundColor = "rgba(0,0,0,0.8)";
labelDiv.style.padding = "4px";
labelDiv.style.borderRadius = "4px";
labelDiv.innerText = labelText;
const label = new CSS2DObject(labelDiv);
pinpoint.add(label);
scene.add(pinpoint);
}
createPinpoint(0, 0, "Location A");
createPinpoint(45, 45, "Location B");
createPinpoint(-30, -60, "Location C");
createPinpoint(-20, 60, "Location D");
// Raycaster and mouse handling
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
function onMouseMove(event) {
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
const intersects = raycaster.intersectObject(globe, true);
controls.autoRotate = intersects.length === 0; // Stop auto-rotation on hover
}
window.addEventListener("mousemove", onMouseMove);
// Handle resizing
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
labelRenderer.setSize(window.innerWidth, window.innerHeight);
});
// Render loop
function render() {
requestAnimationFrame(render);
controls.update();
renderer.render(scene, camera);
labelRenderer.render(scene, camera);
}
render();
With this setup, you should have a working OrbitControls while ensuring CSS2DRenderer works correctly and raycasting handles hovering behavior appropriately.