Skip to content

Nextjs 14 - Clouds example - Does not work #185

Description

@aaadamgo

So, from going through nearly every thread here to figure out how to get it to render the globe and compile without errors, I did this with success.

I'm utilising the clouds example, to render the cloud image layer. Even using the exact same code, this is the console error;

Screenshot 2024-12-04 at 22 51 37

I've tried everything I possibly can and the error persists.

Versions;
"three": "^0.149.0"
"react-globe.gl": "^2.28.0",

For full view of the code:

On a parent file i'm importing my 'world' component using the following;

// Dynamic Import
const World = dynamic(() => import("../World").then((mod) => mod.default), {
    ssr: false,
    loading: () => (
        <div>
            <span>Loading the world</span>
        </div>
    ),
});

Then on the World component...

"use client";

// Imports
// ------------
import React, { useRef, useEffect } from "react";
import Globe from "react-globe.gl";
import * as THREE from "three";

// Styles
// ------------
import { Jacket } from "./styles";

// Component
// ------------
const World = () => {
    // NOTE • Refs
    const globeEl = useRef();
    const jacketRef = useRef();

    // NOTE • States
    const [dimensions, setDimensions] = React.useState({ width: 0, height: 0 });

    // NOTE • Effects
    useEffect(() => {
        const globe = globeEl.current;

        // Auto-rotate
        globe.controls().autoRotate = true;
        globe.controls().autoRotateSpeed = 0.35;
        globe.pointOfView({ altitude: 1.66 });

        // Add clouds sphere
        const CLOUDS_IMG_URL =
            "//unpkg.com/three-globe/example/clouds/clouds.png";
        // const CLOUDS_IMG_URL = "/cloudstest.png";
        const CLOUDS_ALT = 0.004;
        const CLOUDS_ROTATION_SPEED = -0.006; // deg/frame

        new THREE.TextureLoader().load(CLOUDS_IMG_URL, (cloudsTexture) => {
            const clouds = new THREE.Mesh(
                new THREE.SphereGeometry(
                    globe.getGlobeRadius() * (1 + CLOUDS_ALT),
                    75,
                    75
                ),
                new THREE.MeshPhongMaterial({
                    map: cloudsTexture,
                    transparent: true,
                })
            );
            globe.scene().add(clouds);

            (function rotateClouds() {
                clouds.rotation.y += (CLOUDS_ROTATION_SPEED * Math.PI) / 180;
                requestAnimationFrame(rotateClouds);
            })();
        });
    }, []);

    // Update dimensions when container size changes
    useEffect(() => {
        const updateDimensions = () => {
            if (jacketRef.current) {
                setDimensions({
                    width: jacketRef.current.offsetWidth,
                    height: jacketRef.current.offsetHeight,
                });
            }
        };

        updateDimensions();
        window.addEventListener("resize", updateDimensions);

        return () => {
            window.removeEventListener("resize", updateDimensions);
        };
    }, []);

    return (
        <Jacket ref={jacketRef}>
            <Globe
                ref={globeEl}
                animateIn={false}
                backgroundColor="#00000000"
                width={dimensions.width}
                height={dimensions.height}
                globeImageUrl="//unpkg.com/three-globe/example/img/earth-blue-marble.jpg"
                bumpImageUrl="//unpkg.com/three-globe/example/img/earth-topology.png"
                showAtmosphere={false}
            />
        </Jacket>
    );
};

// Exports
// ------------
export default World;

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions