-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Three.js state refactor #14
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Still a WIP. Will let you know when this is ready for review. |
This is ready for review now. I decided to move the three.js class into its own module, which seems like a reasonable way to keep a clean interface between react and three for now. Didn't think too hard about names - happy to take suggestions! |
This looks great! As for naming I think The other thought that came during review is to use a custom hook. It's perhaps not important for reuse at this point, but it moves more of the related logic into diff --git a/src/canvas.ts b/src/canvas.ts
index d348202..bf4ee33 100644
--- a/src/canvas.ts
+++ b/src/canvas.ts
@@ -1,3 +1,4 @@
+import { useEffect, useRef } from 'react';
import {
AdditiveBlending,
AxesHelper,
@@ -208,4 +209,24 @@ export class Canvas {
}
this.selectionHelper.dispose();
}
-}
\ No newline at end of file
+}
+
+export function useCanvas(
+ div: React.RefObject<HTMLDivElement>,
+ initRenderWidth: number,
+ initRenderHeight: number,
+) {
+ const canvas = useRef<Canvas>();
+ useEffect(() => {
+ // initialize the canvas
+ canvas.current = new Canvas(initRenderWidth, initRenderHeight);
+ div.current?.appendChild(canvas.current.renderer.domElement);
+ canvas.current.animate();
+ return () => {
+ canvas.current?.renderer.domElement.remove();
+ canvas.current?.dispose();
+ }
+ }, [/* no dependencies, run only on init */]);
+
+ return canvas.current || null;
+}
diff --git a/src/scene.tsx b/src/scene.tsx
index 62e05db..c977b32 100644
--- a/src/scene.tsx
+++ b/src/scene.tsx
@@ -1,6 +1,6 @@
import { useEffect, useRef, useState } from 'react';
import { InputSlider, InputText, InputToggle } from "@czi-sds/components";
-import { Canvas } from './canvas';
+import { Canvas, useCanvas } from './canvas';
// @ts-expect-error
import { ZarrArray, slice, openArray } from "zarr";
@@ -9,7 +9,7 @@ import { ZarrArray, slice, openArray } from "zarr";
const DEFAULT_ZARR_URL = new URL("https://public.czbiohub.org/royerlab/zebrahub/imaging/single-objective/tracks_benchmark/ZSNS001_nodes.zarr");
interface SceneProps {
- renderWidth: number;
+ renderWidth?: number;
renderHeight?: number;
}
@@ -30,19 +30,13 @@ export default function Scene(props: SceneProps) {
// * manage objects that should never change, even when the component re-renders
// * avoid triggering re-renders when these *do* change
const divRef: React.RefObject<HTMLDivElement> = useRef(null);
- const canvas = useRef<Canvas>();
+ const canvas: Canvas | null = useCanvas(
+ divRef, renderWidth, renderHeight
+ );
// this useEffect is intended to make this part run only on mount
// this requires keeping the dependency array empty
useEffect(() => {
- // initialize the canvas
- canvas.current = new Canvas(renderWidth, renderHeight);
-
- // append renderer canvas
- const divCurrent = divRef.current;
- const renderer = canvas.current!.renderer;
- divCurrent?.appendChild(renderer.domElement);
-
const keyDown = (event: KeyboardEvent) => {
console.log("keyDown: %s", event.key);
if (event.repeat) { return; } // ignore repeats (key held down)
@@ -62,18 +56,13 @@ export default function Scene(props: SceneProps) {
document.addEventListener('keydown', keyDown);
document.addEventListener('keyup', keyUp);
- // start animating - this keeps the scene rendering when controls change, etc.
- canvas.current.animate();
-
return () => {
- renderer.domElement.remove();
- canvas.current?.dispose();
document.removeEventListener('keydown', keyDown);
document.removeEventListener('keyup', keyUp);
}
}, []); // dependency array must be empty to run only on mount!
- canvas.current?.setSelecting(selecting);
+ canvas?.setSelecting(selecting);
// update the array when the dataUrl changes
useEffect(() => {
@@ -92,7 +81,7 @@ export default function Scene(props: SceneProps) {
// set the controls to auto-rotate
useEffect(() => {
- canvas.current && (canvas.current.controls.autoRotate = autoRotate);
+ canvas && (canvas.controls.autoRotate = autoRotate);
}, [autoRotate]);
// playback time points
@@ -112,7 +101,7 @@ export default function Scene(props: SceneProps) {
// update the geometry buffers when the array changes
useEffect(() => {
if (!array) return;
- canvas.current?.initPointsGeometry(array.shape[1] / 3);
+ canvas?.initPointsGeometry(array.shape[1] / 3);
}, [array]);
// update the points when the array or timepoint changes
@@ -129,7 +118,7 @@ export default function Scene(props: SceneProps) {
console.debug('IGNORE SET points at time %d', curTime);
return;
}
- canvas.current?.setPointsPositions(data);
+ canvas?.setPointsPositions(data);
});
} else {
console.debug('IGNORE FETCH points at time %d', curTime);
@@ -141,7 +130,7 @@ export default function Scene(props: SceneProps) {
// update the renderer and composer when the render size changes
// TODO: check performance and avoid if unchanged
- canvas.current?.setSize(renderWidth, renderHeight);
+ canvas?.setSize(renderWidth, renderHeight);
// set up marks for the time slider
const spacing = 100; |
src/canvas.ts
Outdated
setSize(width: number, height: number) { | ||
this.bloomPass.resolution.set(width, height); | ||
this.renderer.setSize(width, height); | ||
this.composer.setSize(width, height); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this also update the camera aspect ratio?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, looks like we're not doing it on main
. But makes sense to catch it in this PR.
Co-authored-by: Ashley Anderson <[email protected]>
I also thought of
Part of the motivation of this PR is to keep React code separate, so I'd rather not put the custom hook in |
I went for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All good with me - merge when you're ready.
In my mind this pretty much wraps up the migration to React, and we can hopefully move on to adding features again.
This refactors the three.js state that is currently stored as individual references in the main React component into a separate class and module.
This makes the main React component simpler, makes the initialization effect easier to read, and also means the code in the class constructor/methods don't need to look up
current
for references. The separation is relatively clean, though there's some overlap in the DOM and callbacks.