@osobh/reactar
Version:
AR Library for React Native and Expo
94 lines • 3.46 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlaneDetection = void 0;
const expo_three_1 = require("expo-three");
class PlaneDetection {
constructor() {
this.scene = null;
this.camera = null;
this.planes = new Map();
this.planeMeshes = new Map();
this.planeMaterial = new expo_three_1.THREE.MeshBasicMaterial({
color: 0x00ff00,
transparent: true,
opacity: 0.3,
side: expo_three_1.THREE.DoubleSide
});
}
setScene(scene) {
this.scene = scene;
}
setCamera(camera) {
this.camera = camera;
}
async detectPlanes() {
if (!this.scene || !this.camera) {
throw new Error('Scene and camera must be set before detecting planes');
}
try {
// Simulate plane detection with sample data
// In a real implementation, this would process camera frames and use
// computer vision algorithms to detect planes
const detectedPlanes = this.simulatePlaneDetection();
this.updatePlanes(detectedPlanes);
return Array.from(this.planes.values());
}
catch (error) {
console.error('Error detecting planes:', error);
throw error;
}
}
simulatePlaneDetection() {
// This is a placeholder implementation
// In a real app, this would use actual plane detection algorithms
return [
{
id: 'plane1',
center: new expo_three_1.THREE.Vector3(0, -1, -3),
normal: new expo_three_1.THREE.Vector3(0, 1, 0),
extent: { width: 2, length: 2 },
confidence: 0.9
}
];
}
updatePlanes(detectedPlanes) {
// Remove planes that are no longer detected
const currentPlaneIds = new Set(detectedPlanes.map(plane => plane.id));
for (const [id, mesh] of this.planeMeshes) {
if (!currentPlaneIds.has(id)) {
this.scene.remove(mesh);
this.planeMeshes.delete(id);
this.planes.delete(id);
}
}
// Update or add new planes
for (const plane of detectedPlanes) {
this.planes.set(plane.id, plane);
let planeMesh = this.planeMeshes.get(plane.id);
if (!planeMesh) {
// Create new mesh for newly detected plane
const geometry = new expo_three_1.THREE.PlaneGeometry(plane.extent.width, plane.extent.length);
planeMesh = new expo_three_1.THREE.Mesh(geometry, this.planeMaterial);
this.planeMeshes.set(plane.id, planeMesh);
this.scene.add(planeMesh);
}
// Update mesh position and rotation
planeMesh.position.copy(plane.center);
planeMesh.lookAt(plane.center.clone().add(plane.normal));
}
}
getDetectedPlanes() {
return Array.from(this.planes.values());
}
clear() {
var _a;
// Remove all plane meshes from the scene
for (const mesh of this.planeMeshes.values()) {
(_a = this.scene) === null || _a === void 0 ? void 0 : _a.remove(mesh);
}
this.planeMeshes.clear();
this.planes.clear();
}
}
exports.PlaneDetection = PlaneDetection;
//# sourceMappingURL=PlaneDetection.js.map