Skip to content

Commit 139b76f

Browse files
committed
v0.06
1 parent 7cae6bb commit 139b76f

20 files changed

+828
-100
lines changed

src/Assisstant/alltogether.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const ChatComponent: React.FC<ChatComponentProps> = ({ onMessageClicked }) => {
1010
const [chatBoxOpen, setChatBoxOpen] = useState(false)
1111

1212
const toggleChatBox = () => {
13-
console.log("AAA")
1413
setChatBoxOpen((prev) => !prev)
1514
}
1615

src/Assisstant/assiantIcon.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from "react"
2-
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"
32
import { faUserTie } from "@fortawesome/free-solid-svg-icons"
3+
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"
44
import "./style.css" // Import the external CSS file
55

66
interface CircleButtonProps {
@@ -26,20 +26,16 @@ const CircleButton: React.FC<CircleButtonProps> = ({
2626
}
2727
}
2828

29-
const assistantIcon = (
30-
<FontAwesomeIcon
31-
icon={faUserTie}
32-
style={{ fontSize: "2em", color: "rgba(0, 0, 0, 0.5)" }} // Customize icon style here
33-
/>
34-
)
35-
3629
return (
3730
<div
3831
className="circle-button"
3932
onClick={onClick}
4033
style={getPositionStyles()}
4134
>
42-
{assistantIcon}
35+
<FontAwesomeIcon
36+
icon={faUserTie}
37+
style={{ fontSize: "2em", color: "rgba(0, 0, 0, 0.5)" }} // Customize icon style here
38+
/>
4339
</div>
4440
)
4541
}

src/LABS/Lab1MainComponent.tsx

Lines changed: 12 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,32 @@
11
import React, { useState, useRef, Suspense } from "react";
2-
import { Canvas, useFrame } from "@react-three/fiber";
3-
import { OrbitControls, PerspectiveCamera, Environment } from "@react-three/drei";
2+
import { Canvas } from "@react-three/fiber";
3+
import { PerspectiveCamera, Environment } from "@react-three/drei";
44
import * as THREE from "three";
55
import CornerText from "../miscellaneous/2DTexts/2dText.tsx";
6-
import ChatComponent from "../Assisstant/alltogether";
7-
import CircularTherm from "../labComponents/SmallInstruments/circularTherm/ThermometerMainComponent.tsx";
8-
import SmallKnob from "../labComponents/knobs/smallKnob.tsx";
9-
import BigKnob from "../labComponents/knobs/currentKnob.tsx";
106
import Grid from "../miscellaneous/planks/grid.tsx";
117
import RaycastingComponent from "../raycasters/lab1Raycaster.tsx";
12-
import LightSwitch from "../clickables/VVR_switch/switchAndCasing.tsx";
138
import DVM from "../labComponents/FrankHertzMainComp/digitalVoltmeter.tsx";
14-
import { FrontFaceContextProvider } from "../contexts/frontFaceContext.tsx";
15-
import { useFrontFaceContext } from "../hooks/useFrontFaceContext.tsx";
169
import TripleOutput from "../labComponents/FrankHertzMainComp/tripleOutPutPowerSupply/mainComponent.tsx";
1710
import CurrentInstrument from "../labComponents/FrankHertzMainComp/currentInstrument.tsx";
1811
import OakPlank from "../miscellaneous/planks/lightOak.tsx";
1912
import FrankHertzMain from "../labComponents/FrankHertzMainComp/frankHertz.tsx";
2013
import CurrentRegulator from "../labComponents/FrankHertzMainComp/currentRegulator.tsx";
2114
import ControlsComponent from "../miscellaneous/controls/cameracontrol.tsx";
2215
import VVR from "../labComponents/FrankHertzMainComp/VariableVoltageRegulator/mainframe.tsx";
23-
24-
interface CameraProps {
25-
xN: number
26-
yN: number
27-
zN: number
28-
}
29-
30-
const Camera: React.FC<CameraProps> = ({ xN, yN, zN }) => {
31-
const frameCounter = useRef(0) // Counter for frames
32-
const interval = 60 // Check visibility every 60 frames (~1 second at 60 FPS)
33-
34-
const { isFrontFaceVisible, setFrontFaceVisibility } = useFrontFaceContext()
35-
36-
useFrame(({ camera }) => {
37-
frameCounter.current += 1
38-
39-
if (frameCounter.current >= interval) {
40-
const isCameraBehind = camera.position.z < 0
41-
42-
if (isCameraBehind && !isFrontFaceVisible) {
43-
setFrontFaceVisibility(true)
44-
console.log("Camera is behind: Dial is updating", isFrontFaceVisible)
45-
} else if (!isCameraBehind && isFrontFaceVisible) {
46-
setFrontFaceVisibility(false)
47-
console.log(
48-
"Camera is in front: Dial is not updating",
49-
isFrontFaceVisible,
50-
)
51-
}
52-
53-
frameCounter.current = 0 // Reset the frame counter after 1 second
54-
}
55-
})
56-
57-
return (
58-
<PerspectiveCamera
59-
makeDefault
60-
position={[xN, yN, zN]}
61-
fov={75}
62-
aspect={window.innerWidth / window.innerHeight}
63-
near={0.1}
64-
far={1000}
65-
/>
66-
)
67-
}
16+
import FloatingSquare from "../taskbar/mainBlock.tsx";
17+
import { CameraProvider } from "../contexts/cameraPositionContext.tsx";
18+
import Lab1Camera from "./cameras/lab1Camera.tsx";
6819

6920
const GraphPaperComponent: React.FC = () => {
70-
const [position, setPosition] = useState({ x: 0, y: 50, z: 80 });
71-
const [key, setKey] = useState(0);
72-
const [reversedString, setReversedString] = useState<string | null>(null);
73-
74-
const handleStubMessageClick = () => {
75-
setPosition({ x: 10, y: 10, z: 10 })
76-
setKey((prev) => prev + 1)
77-
}
78-
7921
return (
8022
<Suspense
8123
fallback={<CornerText position="top-left" text="Loading your Lab..." />}
8224
>
25+
<CameraProvider>
26+
8327
<div style={{ width: "100vw", height: "100vh", overflow: "hidden" }}>
8428
<CornerText position="top-left" text="Frank-Hertz Lab" />
85-
<ChatComponent onMessageClicked={handleStubMessageClick} />
86-
{reversedString && (
87-
<div
88-
style={{ position: "absolute", top: 0, left: 0, color: "white" }}
89-
>
90-
{`Reversed: ${reversedString}`}
91-
</div>
92-
)}
93-
29+
<FloatingSquare />
9430
<Canvas
9531
gl={{ antialias: true }}
9632
onCreated={({ gl, scene }) => {
@@ -103,16 +39,14 @@ const GraphPaperComponent: React.FC = () => {
10339
<RaycastingComponent />
10440

10541
{/* Camera and Thermometer */}
106-
<FrontFaceContextProvider>
107-
<Camera key={key} xN={position.x} yN={position.y} zN={position.z} />
108-
</FrontFaceContextProvider>
42+
<Lab1Camera/>
10943

11044
{/* Ambient Light */}
11145
<Environment files="/environment/sky.hdr" background />
11246
<ambientLight intensity={1}/>
11347

11448
{/* Controls */}
115-
<ControlsComponent />
49+
{/* <ControlsComponent /> */}
11650

11751
{/* Components: From Bottom to Top*/}
11852
<Grid />
@@ -123,9 +57,9 @@ const GraphPaperComponent: React.FC = () => {
12357
<CurrentRegulator position={[40, 26, 5]}/>
12458
<DVM position={[20, 30, 23]} scale={[1.7, 1.7, 1.85]} unique_id="DVM_2"/>
12559
<VVR position={[-30, 24, 2]} />
126-
<DVM position={[30, 80, 8]} scale={[1.7, 1.7, 1.85]} rotation={[0, Math.PI, 0]} unique_id="DVM_1"/>
127-
</Canvas>
60+
<DVM position={[30, 80, 8]} scale={[1.7, 1.7, 1.85]} rotation={[0, Math.PI, 0]} unique_id="DVM_1"/> </Canvas>
12861
</div>
62+
</CameraProvider>
12963
</Suspense>
13064
)
13165
}

src/LABS/cameras/lab1Camera.tsx

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import React, { useEffect, useRef } from "react";
2+
import { useMemo } from "react";
3+
import { PerspectiveCamera } from "@react-three/drei";
4+
import * as THREE from "three";
5+
import { useFrame, useThree } from "@react-three/fiber"
6+
import { useCameraState } from "../../contexts/cameraPositionContext";
7+
import ControlsComponent from "../../miscellaneous/controls/cameracontrol";
8+
9+
const Lab1Camera: React.FC = () => {
10+
const cameraRef = useRef<THREE.PerspectiveCamera | null>(null); // Create a ref for the camera
11+
const { currentPosition } = useCameraState(); // Access currentPosition from context
12+
13+
const raycasterRef = useRef(new THREE.Raycaster())
14+
const intersectsRef = useRef<THREE.Intersection[]>([]);
15+
const { scene } = useThree();
16+
17+
const setCameraRef = (camera: THREE.PerspectiveCamera | null) => {
18+
if (camera) {
19+
cameraRef.current = camera;
20+
}
21+
};
22+
23+
const objectDictionaryRef = useRef<Record<string, THREE.Object3D>>({});
24+
25+
const glow = (object: THREE.Object3D, glowColor: THREE.ColorRepresentation = 0xfff000) => {
26+
if (object instanceof THREE.Mesh) {
27+
// Check if the object is already glowing
28+
if (object.userData.glowMesh) {
29+
console.warn("The object is already glowing.");
30+
return;
31+
}
32+
33+
// Create a glow material
34+
const glowMaterial = new THREE.MeshBasicMaterial({
35+
color: glowColor,
36+
transparent: true,
37+
opacity: 0.45, // Adjust for intensity
38+
});
39+
40+
// Clone the object's geometry for the glow effect
41+
const glowGeometry = object.geometry.clone();
42+
const glowMesh = new THREE.Mesh(glowGeometry, glowMaterial);
43+
44+
// Scale the glow mesh slightly larger than the object
45+
glowMesh.position.copy(object.position);
46+
glowMesh.rotation.copy(object.rotation);
47+
48+
// Add the glow mesh to the scene or as a child of the object
49+
object.add(glowMesh);
50+
51+
// Store the glow mesh in the userData
52+
object.userData.glowMesh = glowMesh;
53+
54+
// Remove the glow effect after 3 seconds
55+
setTimeout(() => {
56+
if (object.userData.glowMesh) {
57+
object.remove(object.userData.glowMesh);
58+
object.userData.glowMesh = null;
59+
}
60+
}, 3000);
61+
} else {
62+
console.warn("The object is not a Mesh.");
63+
}
64+
};
65+
66+
67+
useEffect(() => {
68+
const updateRaycaster = async () => {
69+
if (cameraRef.current) {
70+
// Update the camera position
71+
// cameraRef.current.position.set(currentPosition.x, currentPosition.y, currentPosition.z);
72+
cameraRef.current.lookAt(currentPosition.x, currentPosition.y, currentPosition.z);
73+
// Update the raycaster from the camera's perspective
74+
raycasterRef.current.setFromCamera(new THREE.Vector2(0, 0), cameraRef.current);
75+
76+
// Update raycaster settings
77+
raycasterRef.current.params.Points.threshold = 0.1;
78+
79+
// Find intersections
80+
intersectsRef.current = raycasterRef.current.intersectObjects(scene.children, true);
81+
82+
// Log the first intersected object, if any
83+
if (intersectsRef.current.length > 0) {
84+
console.log("First object in view:", intersectsRef.current[0].object.userData.type);
85+
glow(intersectsRef.current[0].object);
86+
} else {
87+
console.log("No objects in view.");
88+
}
89+
}
90+
};
91+
92+
updateRaycaster();
93+
}, [currentPosition, scene]);
94+
95+
return (
96+
<>
97+
<PerspectiveCamera
98+
ref={setCameraRef} // Attach the callback ref to ensure proper synchronization
99+
makeDefault
100+
position={[currentPosition.x, currentPosition.y, currentPosition.z]} // Initial position
101+
fov={75}
102+
aspect={window.innerWidth / window.innerHeight}
103+
near={0.1}
104+
far={1000}
105+
/>
106+
<ControlsComponent />
107+
</>
108+
);
109+
};
110+
111+
export default Lab1Camera;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import React, { createContext, useContext, useState } from "react";
2+
3+
interface CameraPosition {
4+
x: number;
5+
y: number;
6+
z: number;
7+
}
8+
9+
interface CameraState {
10+
currentPosition: CameraPosition;
11+
setPosition: (objectIntention: string) => void;
12+
}
13+
14+
const CameraContext = createContext<CameraState | null>(null);
15+
16+
export const CameraProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
17+
// Dictionary for predefined positions
18+
const initialPositions: Record<string, CameraPosition> = {
19+
circular_thermometer: { x: -15, y: 120, z: -10 },
20+
dcv: { x: -80, y: 40, z: 5 },
21+
VVR_knob: { x: 4, y: 5, z: 6 },
22+
vvr: { x: -80, y: 35, z: 5 },
23+
frank_hertz_chasis: { x: 10, y: 11, z: 12 },
24+
};
25+
26+
// State to track only the current camera position
27+
const [currentPosition, setCurrentPosition] = useState<CameraPosition>({
28+
x: 0,
29+
y: 50,
30+
z: 80, // Default starting position
31+
});
32+
33+
const setPosition = (objectIntention: string) => {
34+
const position = initialPositions[objectIntention];
35+
if (!position) {
36+
console.warn(`No camera position found for object: ${objectIntention}`);
37+
return;
38+
}
39+
const randomOffset = Math.random() * 0.001 + 0.001; // Random value between 0.001 and 0.002
40+
position.x += randomOffset;
41+
42+
// Update the current camera position
43+
setCurrentPosition(position);
44+
console.log(`Camera updated to position for ${objectIntention}:`, position);
45+
};
46+
47+
return (
48+
<CameraContext.Provider value={{ currentPosition, setPosition }}>
49+
{children}
50+
</CameraContext.Provider>
51+
);
52+
};
53+
54+
export const useCameraState = () => {
55+
const context = useContext(CameraContext);
56+
if (!context) {
57+
throw new Error("useCameraState must be used within a CameraProvider");
58+
}
59+
return context;
60+
};

src/miscellaneous/controls/cameracontrol.tsx

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
import React, { useRef, useEffect } from 'react';
1+
import React, { useRef, useEffect, useMemo } from 'react';
22
import { OrbitControls } from '@react-three/drei';
33
import * as THREE from 'three';
4+
import { useThree } from '@react-three/fiber';
5+
import { useCameraState } from '../../contexts/cameraPositionContext';
46

5-
const ControlsComponent = () => {
6-
// const controlsRef = useRef<THREE.OrbitControls>(null);
7-
8-
// useEffect(() => {
9-
// if (controlsRef.current) {
10-
// // Update the controls' target
11-
// controlsRef.current.target.set(1, 2, 3); // Example: set to position (1, 2, 3)
12-
// controlsRef.current.update(); // Update the controls to reflect the new target
13-
// }
14-
// }, []);
7+
const ControlsComponent: React.FC = () => {
8+
const controlsRef = useRef(null);
159

1610
return (
1711
<OrbitControls
12+
ref={controlsRef}
1813
enableDamping
1914
dampingFactor={0.1}
2015
rotateSpeed={0.4}

src/raycasters/lab1Raycaster.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const RaycastingComponent: React.FC = () => {
2020
mouseRef.current.x = ((event.clientX - rect.left) / rect.width) * 2 - 1
2121
mouseRef.current.y = -((event.clientY - rect.top) / rect.height) * 2 + 1
2222
}
23-
23+
2424
const handleMouseDown = (event: MouseEvent) => {
2525
if (event.button === 0) {
2626
setIsMouseDown(true)

0 commit comments

Comments
 (0)