mkkellogg / GaussianSplats3D

Three.js-based implementation of 3D Gaussian splatting
MIT License
1.09k stars 134 forks source link

NextJS Issue #247

Open runabol opened 3 weeks ago

runabol commented 3 weeks ago

I was able to successfully create a React component around this library:

import React, { useState, useEffect, useRef } from "react";
import * as GaussianSplats3D from "@mkkellogg/gaussian-splats-3d";

export default function GaussianSplat3({
  src,
  camera,
}: {
  src: string;
  camera?: Camera;
}) {
  const [rootElementId] = useState(
    `gaussian-splat-viewer-${Math.random().toString(36).substring(2)}`,
  );

  useEffect(() => {
    const rootElement = document.getElementById(rootElementId);
    if (!rootElement) return;
    const viewer = new GaussianSplats3D.Viewer({
      cameraUp: camera ? camera.up : [0, 1, 0],
      initialCameraPosition: camera ? camera.position : [0, 1, 0],
      initialCameraLookAt: camera ? camera.lookAt : [1, 0, 0],
      rootElement: rootElement,
      sharedMemoryForWorkers: false,
    });

    viewer
      .addSplatScene(src, {
        streamView: true,
        showLoadingUI: false,
        format: SceneFormat.Splat,
      })
      .then(() => {
        viewer.start();
        viewer.perspectiveControls.stopListenToKeyEvents();
        viewer.orthographicControls.stopListenToKeyEvents();
      });

    // Cleanup function to remove event listeners
    return () => {
      viewer
        .dispose()
        .then(() => {})
        .catch((err) => {
          console.log("error disposing of GS3 viewer:", err);
        });
    };
  }, []);

  return <div className="h-full w-full" id={rootElementId}></div>;
}

It generally works quite well. My only issue now is when running in NextJS in development mode with reactStrictMode=true (which is the recommended setting) I run into this error:

Screenshot 2024-06-04 at 12 15 16 PM

I tries wrapping it in a try catch block but this doesn't help either.

This doesn't happen when I run the NextJS app in production mode. i.e.:

npm run build && npm run start

or when I turn off reactStrictMode

Wondering if anyone else ran into this and if they found a way around it.

mkkellogg commented 3 weeks ago

It's possible that react has a safeguard that requires AbortController.abort() to have a valid reason parameter. Are you building this library from source or pulling from the npm registry? If you're building from source, try adding a valid reason at this call to AbortController.abort(): https://github.com/mkkellogg/GaussianSplats3D/blob/ec323ad69dab6cfbfbef419019d626a3dcacd1b5/src/Util.js#L64 If that fixes the issue I will update it so there's always a valid reason.

mkkellogg commented 4 days ago

I made an update in the 2dgs branch that I think might fix this problem, want to give it a try?