|
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"; |
| 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 | 5 | import { useFrame, useThree } from "@react-three/fiber"
|
6 |
| -import { useCameraState } from "../../contexts/cameraPositionContext"; |
7 |
| -import ControlsComponent from "../../miscellaneous/controls/cameracontrol"; |
| 6 | +import { useCameraState } from "../../contexts/cameraPositionContext" |
| 7 | +import ControlsComponent from "../../miscellaneous/controls/cameracontrol" |
8 | 8 |
|
9 | 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 |
| 10 | + const cameraRef = useRef<THREE.PerspectiveCamera | null>(null) // Create a ref for the camera |
| 11 | + const { currentPosition } = useCameraState() // Access currentPosition from context |
12 | 12 |
|
13 |
| - const raycasterRef = useRef(new THREE.Raycaster()) |
14 |
| - const intersectsRef = useRef<THREE.Intersection[]>([]); |
15 |
| - const { scene } = useThree(); |
| 13 | + const raycasterRef = useRef(new THREE.Raycaster()) |
| 14 | + const intersectsRef = useRef<THREE.Intersection[]>([]) |
| 15 | + const { scene } = useThree() |
16 | 16 |
|
17 |
| - const setCameraRef = (camera: THREE.PerspectiveCamera | null) => { |
18 |
| - if (camera) { |
19 |
| - cameraRef.current = camera; |
| 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 = ( |
| 26 | + object: THREE.Object3D, |
| 27 | + glowColor: THREE.ColorRepresentation = 0xfff000, |
| 28 | + ) => { |
| 29 | + if (object instanceof THREE.Mesh) { |
| 30 | + // Check if the object is already glowing |
| 31 | + if (object.userData.glowMesh) { |
| 32 | + console.warn("The object is already glowing.") |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + // Create a glow material |
| 37 | + const glowMaterial = new THREE.MeshBasicMaterial({ |
| 38 | + color: glowColor, |
| 39 | + transparent: true, |
| 40 | + opacity: 0.45, // Adjust for intensity |
| 41 | + }) |
| 42 | + |
| 43 | + // Clone the object's geometry for the glow effect |
| 44 | + const glowGeometry = object.geometry.clone() |
| 45 | + const glowMesh = new THREE.Mesh(glowGeometry, glowMaterial) |
| 46 | + |
| 47 | + // Scale the glow mesh slightly larger than the object |
| 48 | + glowMesh.position.copy(object.position) |
| 49 | + glowMesh.rotation.copy(object.rotation) |
| 50 | + |
| 51 | + // Add the glow mesh to the scene or as a child of the object |
| 52 | + object.add(glowMesh) |
| 53 | + |
| 54 | + // Store the glow mesh in the userData |
| 55 | + object.userData.glowMesh = glowMesh |
| 56 | + |
| 57 | + // Remove the glow effect after 3 seconds |
| 58 | + setTimeout(() => { |
| 59 | + if (object.userData.glowMesh) { |
| 60 | + object.remove(object.userData.glowMesh) |
| 61 | + object.userData.glowMesh = null |
20 | 62 | }
|
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); |
| 63 | + }, 3000) |
| 64 | + } else { |
| 65 | + console.warn("The object is not a Mesh.") |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + useEffect(() => { |
| 70 | + const updateRaycaster = async () => { |
| 71 | + if (cameraRef.current) { |
| 72 | + // Update the camera position |
| 73 | + // cameraRef.current.position.set(currentPosition.x, currentPosition.y, currentPosition.z); |
| 74 | + cameraRef.current.lookAt( |
| 75 | + currentPosition.x, |
| 76 | + currentPosition.y, |
| 77 | + currentPosition.z, |
| 78 | + ) |
| 79 | + // Update the raycaster from the camera's perspective |
| 80 | + raycasterRef.current.setFromCamera( |
| 81 | + new THREE.Vector2(0, 0), |
| 82 | + cameraRef.current, |
| 83 | + ) |
| 84 | + |
| 85 | + // Update raycaster settings |
| 86 | + raycasterRef.current.params.Points.threshold = 0.1 |
| 87 | + |
| 88 | + // Find intersections |
| 89 | + intersectsRef.current = raycasterRef.current.intersectObjects( |
| 90 | + scene.children, |
| 91 | + true, |
| 92 | + ) |
| 93 | + |
| 94 | + // Log the first intersected object, if any |
| 95 | + if (intersectsRef.current.length > 0) { |
| 96 | + console.log( |
| 97 | + "First object in view:", |
| 98 | + intersectsRef.current[0].object.userData.type, |
| 99 | + ) |
| 100 | + glow(intersectsRef.current[0].object) |
61 | 101 | } else {
|
62 |
| - console.warn("The object is not a Mesh."); |
| 102 | + console.log("No objects in view.") |
63 | 103 | }
|
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; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + updateRaycaster() |
| 108 | + }, [currentPosition, scene]) |
| 109 | + |
| 110 | + return ( |
| 111 | + <> |
| 112 | + <PerspectiveCamera |
| 113 | + ref={setCameraRef} // Attach the callback ref to ensure proper synchronization |
| 114 | + makeDefault |
| 115 | + position={[currentPosition.x, currentPosition.y, currentPosition.z]} // Initial position |
| 116 | + fov={75} |
| 117 | + aspect={window.innerWidth / window.innerHeight} |
| 118 | + near={0.1} |
| 119 | + far={1000} |
| 120 | + /> |
| 121 | + <ControlsComponent /> |
| 122 | + </> |
| 123 | + ) |
| 124 | +} |
| 125 | + |
| 126 | +export default Lab1Camera |
0 commit comments