function setupCameraRestriction(gCamera, gRenderer, gSphereScene) { const polygonVertices = [ new THREE.Vector2(-0.25, -0.25), new THREE.Vector2(-0.25, 0.15), new THREE.Vector2(0.0, 0.15), new THREE.Vector2(0.0, 0.25), new THREE.Vector2(0.25, 0.25), new THREE.Vector2(0.25, 0.0), new THREE.Vector2(0.15, 0.0), new THREE.Vector2(0.15, -0.25), // Ajoutez d'autres points pour définir votre forme de "L" ]; const fixedHeight = 0.5; // Hauteur fixe sur l'axe y // Fonction pour créer une visualisation du polygone function createPolygonVisualization() { const material = new THREE.LineBasicMaterial({ color: 0x00ff00 }); const points = []; // Ajouter chaque sommet comme un point 3D (x, fixedHeight, z) polygonVertices.forEach(vertex => { points.push(new THREE.Vector3(vertex.x, fixedHeight, vertex.y)); }); // Ajouter le premier point à la fin pour fermer le polygone points.push(new THREE.Vector3(polygonVertices[0].x, fixedHeight, polygonVertices[0].y)); // Créer une géométrie de ligne avec les points const geometry = new THREE.BufferGeometry().setFromPoints(points); const line = new THREE.Line(geometry, material); gSphereScene.add(line); // Ajouter des sphères aux sommets pour marquer les points const sphereGeometry = new THREE.SphereGeometry(0.01, 16, 16); const sphereMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 }); polygonVertices.forEach(vertex => { const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial); sphere.position.set(vertex.x, fixedHeight, vertex.y); gSphereScene.add(sphere); }); } // Appeler la fonction pour créer la visualisation du polygone createPolygonVisualization(); // Fonction pour vérifier si un point est dans le polygone en utilisant Ray Casting function isPointInPolygon(point, vertices) { let intersects = 0; for (let i = 0; i < vertices.length; i++) { const v1 = vertices[i]; const v2 = vertices[(i + 1) % vertices.length]; if ((v1.y > point.y) !== (v2.y > point.y) && point.x < ((v2.x - v1.x) * (point.y - v1.y)) / (v2.y - v1.y) + v1.x) { intersects++; } } return intersects % 2 === 1; } function restrictCameraPosition() { gCamera.position.y = fixedHeight; const cameraPos2D = new THREE.Vector2(gCamera.position.x, gCamera.position.z); if (!isPointInPolygon(cameraPos2D, polygonVertices)) { let closestPoint = cameraPos2D; let minDistance = Infinity; for (let i = 0; i < polygonVertices.length; i++) { const v1 = polygonVertices[i]; const v2 = polygonVertices[(i + 1) % polygonVertices.length]; const closestPointOnEdge = closestPointOnLineSegment(cameraPos2D, v1, v2); const distance = cameraPos2D.distanceTo(closestPointOnEdge); if (distance < minDistance) { minDistance = distance; closestPoint = closestPointOnEdge; } } gCamera.position.x = closestPoint.x; gCamera.position.z = closestPoint.y; } } function closestPointOnLineSegment(point, v1, v2) { const lineVec = new THREE.Vector2().subVectors(v2, v1); const pointVec = new THREE.Vector2().subVectors(point, v1); const lineLen = lineVec.length(); const lineUnitVec = lineVec.normalize(); const projection = pointVec.dot(lineUnitVec); const t = Math.max(0, Math.min(lineLen, projection)); return new THREE.Vector2().copy(v1).add(lineUnitVec.multiplyScalar(t)); } function animate() { requestAnimationFrame(animate); restrictCameraPosition(); gRenderer.render(gSphereScene, gCamera); } animate(); }