Skip to content

Free Motion Capture for Everyone πŸ’€βœ¨

TypeScript

nvm fixed it lol

jonmatthisa5431d591bdf
diff --git a/freemocap-ui/src/components/viewport3d/ImageMesh.tsx b/freemocap-ui/src/components/viewport3d/ImageMesh.tsx
index 21ff5ff9..fb946922 100644
--- a/freemocap-ui/src/components/viewport3d/ImageMesh.tsx
+++ b/freemocap-ui/src/components/viewport3d/ImageMesh.tsx
@@ -1,6 +1,7 @@
import React, { useEffect, useRef, useState } from 'react';
import * as THREE from 'three';
-import {CameraImageData} from "@/context/websocket-context/useWebsocketBinaryMessageProcessor";
+import { CameraImageData } from "@/context/websocket-context/useWebsocketBinaryMessageProcessor";
+import { useWebSocketContext } from "@/context/websocket-context/WebSocketContext";
interface ImageMeshProps {
cameraImageData: CameraImageData;
@@ -8,59 +9,60 @@ interface ImageMeshProps {
}
export const ImageMesh: React.FC<ImageMeshProps> = ({ cameraImageData, position }) => {
+ const { cameraId, imageBitmap, imageWidth, imageHeight, frameNumber } = cameraImageData;
+ const { acknowledgeFrameRendered } = useWebSocketContext();
const meshRef = useRef<THREE.Mesh>(null);
const textureRef = useRef<THREE.Texture | null>(null);
const [aspectRatio, setAspectRatio] = useState(2);
- // Create and update texture from ImageBitmap
+ // Initialize texture once
useEffect(() => {
- const { imageBitmap } = cameraImageData;
- if (!imageBitmap || !meshRef.current) return;
+ // Create texture only once and reuse it
+ if (!textureRef.current) {
+ const newTexture = new THREE.Texture();
+ newTexture.minFilter = THREE.LinearFilter;
+ newTexture.magFilter = THREE.LinearFilter;
+ textureRef.current = newTexture;
+ }
- let isMounted = true;
-
- const updateTexture = async () => {
- if (!imageBitmap) return;
- try {
+ return () => {
+ // Clean up texture on unmount
+ if (textureRef.current) {
+ textureRef.current.dispose();
+ textureRef.current = null;
+ }
+ };
+ }, []);
- if (!isMounted) {
- imageBitmap.close();
- return;
- }
+ // Update texture with new image data
+ useEffect(() => {
+ if (!imageBitmap || !meshRef.current || !textureRef.current) return;
- // Update aspect ratio
- const newAspectRatio = imageBitmap.width / imageBitmap.height;
+ try {
+ // Update aspect ratio only when dimensions change
+ const newAspectRatio = imageWidth / imageHeight;
+ if (aspectRatio !== newAspectRatio) {
setAspectRatio(newAspectRatio);
+ }
- // Create texture from ImageBitmap
- if (textureRef.current) {
- textureRef.current.dispose(); // Clean up previous texture
- }
-
- const newTexture = new THREE.CanvasTexture(imageBitmap);
- textureRef.current = newTexture;
+ // Update texture with new image data without creating a new texture
+ textureRef.current.image = imageBitmap;
+ textureRef.current.needsUpdate = true;
- // Update material
- if (meshRef.current?.material instanceof THREE.MeshBasicMaterial) {
- meshRef.current.material.map = newTexture;
- meshRef.current.material.needsUpdate = true;
+ // Apply texture to material if not already applied
+ if (meshRef.current.material instanceof THREE.MeshBasicMaterial) {
+ if (meshRef.current.material.map !== textureRef.current) {
+ meshRef.current.material.map = textureRef.current;
}
- } catch (error) {
- console.error('Error processing image:', error);
+ meshRef.current.material.needsUpdate = true;
}
- };
- updateTexture();
-
- return () => {
- isMounted = false;
- // Clean up texture on unmount
- if (textureRef.current) {
- textureRef.current.dispose();
- textureRef.current = null;
- }
- };
- }, [cameraImageData]);
+ // Acknowledge that the frame was rendered
+ acknowledgeFrameRendered(cameraId, frameNumber);
+ } catch (error) {
+ console.error('Error processing image:', error);
+ }
+ }, [cameraId, imageBitmap, imageWidth, imageHeight, frameNumber, aspectRatio, acknowledgeFrameRendered]);
// Calculate dimensions for the plane geometry
const width = 2;
@@ -72,4 +74,4 @@ export const ImageMesh: React.FC<ImageMeshProps> = ({ cameraImageData, position
<meshBasicMaterial transparent={true} />
</mesh>
);
-};
+};
\ No newline at end of file
diff --git a/freemocap-ui/src/components/viewport3d/multi-camera-scene.tsx b/freemocap-ui/src/components/viewport3d/multi-camera-scene.tsx
deleted file mode 100644
index d2b66165..00000000
--- a/freemocap-ui/src/components/viewport3d/multi-camera-scene.tsx
+++ /dev/null
@@ -1,120 +0,0 @@
-"use client"
-
-import { useRef, useState, useMemo } from "react"
-import { Canvas, useFrame, useThree } from "@react-three/fiber"
-import { PerspectiveCamera, OrbitControls } from "@react-three/drei"
-import * as THREE from "three"
-import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
-
-const CameraObject = ({ position, lookAt }) => {
- const mesh = useRef()
-
- useFrame(() => {
- mesh.current.lookAt(lookAt)
- })
-
- return (
- <mesh ref={mesh} position={position}>
- <boxGeometry args={[0.2, 0.2, 0.3]} />
- <meshStandardMaterial color="gray" />
- </mesh>
- )
-}
-
-const Scene = ({ selectedCamera, setSelectedCamera }) => {
- const { scene, camera } = useThree()
- const insetRef = useRef()
-
- const cameras = useMemo(
- () => [
- new THREE.PerspectiveCamera(75, 1, 0.1, 1000),
- new THREE.PerspectiveCamera(75, 1, 0.1, 1000),
- new THREE.PerspectiveCamera(75, 1, 0.1, 1000),
- new THREE.PerspectiveCamera(75, 1, 0.1, 1000),
- ],
- [],
- )
-
- const cameraPositions = [
- [3, 1, 3],
- [-3, 1, 3],
- [3, 1, -3],
- [-3, 1, -3],
- ]
-
- useFrame(({ gl }) => {
- gl.autoClear = true
- gl.setViewport(0, 0, gl.domElement.width, gl.domElement.height)
- gl.render(scene, camera)
-
- gl.autoClear = false
- gl.clearDepth()
- gl.setScissorTest(true)
-
- const insetWidth = gl.domElement.width / 4
- const insetHeight = gl.domElement.height / 4
-
- gl.setScissor(10, gl.domElement.height - insetHeight - 10, insetWidth, insetHeight)
- gl.setViewport(10, gl.domElement.height - insetHeight - 10, insetWidth, insetHeight)
-
- gl.render(scene, cameras[selectedCamera])
-
- gl.setScissorTest(false)
- })
-
- return (
- <>
- <ambientLight intensity={0.5} />
- <pointLight position={[10, 10, 10]} />
-
- <mesh position={[0, 0, 0]}>
- <boxGeometry args={[1, 1, 1]} />
- <meshStandardMaterial color="hotpink" />
- </mesh>
-
- {cameras.map((_, index) => (
- <CameraObject key={index} position={cameraPositions[index]} lookAt={[0, 0, 0]} />
- ))}
-
- {cameras.map((cam, index) => (
- <PerspectiveCamera
- key={index}
- makeDefault={index === selectedCamera}
- position={cameraPositions[index]}
- fov={75}
- aspect={1}
- near={0.1}
- far={1000}
- />
- ))}
-
- <OrbitControls />
- </>
- )
-}
-
-export default function Component() {
- const [selectedCamera, setSelectedCamera] = useState(0)
-
- return (
- <div className="w-full h-screen relative">
- <Canvas>
- <Scene selectedCamera={selectedCamera} setSelectedCamera={setSelectedCamera} />
- </Canvas>
- <div className="absolute top-4 right-4">
- <Select value={selectedCamera.toString()} onValueChange={(value) => setSelectedCamera(Number.parseInt(value))}>
- <SelectTrigger className="w-[180px]">
- <SelectValue placeholder="Select a camera" />
- </SelectTrigger>
- <SelectContent>
- <SelectItem value="0">Camera 1</SelectItem>
- <SelectItem value="1">Camera 2</SelectItem>
- <SelectItem value="2">Camera 3</SelectItem>
- <SelectItem value="3">Camera 4</SelectItem>
- </SelectContent>
- </Select>
- </div>
- </div>
- )
-}
-