File size: 7,006 Bytes
b012e8c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | /**
* ConnectionManager.js
* Manages connections (lines) between spheres
*/
class ConnectionManager {
constructor(scene) {
this.scene = scene;
this.connections = new Map();
this.connectionLines = new Map();
this.materials = new Map();
}
/**
* Create a connection between two spheres
* @param {string} id - Unique identifier for this connection
* @param {THREE.Vector3} startPos - Starting position
* @param {THREE.Vector3} endPos - Ending position
* @param {object} config - Configuration object
* @param {string} config.color - Hex color (default: '#00ff00')
* @param {number} config.lineWidth - Line width (default: 2)
* @param {string} config.fromSphere - Name of source sphere
* @param {string} config.toSphere - Name of target sphere
* @returns {THREE.Line} The created line object
*/
addConnection(id, startPos, endPos, config = {}) {
// Prevent duplicate IDs
if (this.connections.has(id)) {
console.warn(`Connection "${id}" already exists.`);
return null;
}
const color = config.color || '#00ff00';
const lineWidth = config.lineWidth || 2;
// Create geometry
const geometry = new THREE.BufferGeometry();
geometry.setAttribute('position', new THREE.BufferAttribute(
new Float32Array([
startPos.x, startPos.y, startPos.z,
endPos.x, endPos.y, endPos.z
]), 3
));
// Create material
const material = new THREE.LineBasicMaterial({
color: new THREE.Color(color),
linewidth: lineWidth,
transparent: true,
opacity: 0.8
});
// Create line
const line = new THREE.Line(geometry, material);
line.name = id;
line.userData = {
connectionId: id,
fromSphere: config.fromSphere,
toSphere: config.toSphere,
color: color,
lineWidth: lineWidth,
startPos: startPos.clone(),
endPos: endPos.clone()
};
// Add to scene
this.scene.add(line);
this.connections.set(id, line.userData);
this.connectionLines.set(id, line);
this.materials.set(id, material);
// console.log(`✓ Connection added: ${id} (${config.fromSphere} -> ${config.toSphere})`);
return line;
}
/**
* Remove a connection
* @param {string} id - Connection ID
*/
removeConnection(id) {
const line = this.connectionLines.get(id);
if (!line) {
console.warn(`Connection "${id}" not found.`);
return;
}
// Remove from scene
this.scene.remove(line);
// Clean up geometry and material
const geometry = line.geometry;
const material = this.materials.get(id);
if (geometry) geometry.dispose();
if (material) material.dispose();
// Remove from maps
this.connections.delete(id);
this.connectionLines.delete(id);
this.materials.delete(id);
// console.log(`✓ Connection removed: ${id}`);
}
/**
* Update connection position (useful for dynamic connections)
* @param {string} id - Connection ID
* @param {THREE.Vector3} startPos - New start position
* @param {THREE.Vector3} endPos - New end position
*/
updateConnectionPositions(id, startPos, endPos) {
const line = this.connectionLines.get(id);
if (!line) return;
const positions = line.geometry.attributes.position.array;
positions[0] = startPos.x;
positions[1] = startPos.y;
positions[2] = startPos.z;
positions[3] = endPos.x;
positions[4] = endPos.y;
positions[5] = endPos.z;
line.geometry.attributes.position.needsUpdate = true;
line.userData.startPos = startPos.clone();
line.userData.endPos = endPos.clone();
}
/**
* Update connection color
* @param {string} id - Connection ID
* @param {string} newColor - New hex color
*/
updateConnectionColor(id, newColor) {
const material = this.materials.get(id);
if (!material) return;
material.color.set(new THREE.Color(newColor));
this.connections.get(id).color = newColor;
}
/**
* Get connection by ID
* @param {string} id - Connection ID
* @returns {Object} Connection data
*/
getConnection(id) {
return this.connections.get(id);
}
/**
* Get all connections
* @returns {Array} Array of all connections
*/
getAllConnections() {
return Array.from(this.connections.entries()).map(([id, data]) => ({
id,
...data,
line: this.connectionLines.get(id)
}));
}
/**
* Get connections for a specific sphere
* @param {string} sphereName - Name of the sphere
* @returns {Array} Connections connected to this sphere
*/
getConnectionsForSphere(sphereName) {
return Array.from(this.connections.values()).filter(conn =>
conn.fromSphere === sphereName || conn.toSphere === sphereName
);
}
/**
* Remove all connections for a sphere (when sphere is deleted)
* @param {string} sphereName - Name of the sphere
*/
removeConnectionsForSphere(sphereName) {
const connectionsToRemove = Array.from(this.connections.keys()).filter(id => {
const conn = this.connections.get(id);
return conn.fromSphere === sphereName || conn.toSphere === sphereName;
});
connectionsToRemove.forEach(id => this.removeConnection(id));
}
/**
* Add a pulse effect to a connection
* @param {string} id - Connection ID
* @param {number} duration - Duration of pulse in ms
*/
pulseConnection(id, duration = 500) {
const material = this.materials.get(id);
if (!material) return;
const startOpacity = material.opacity;
const startTime = Date.now();
const animate = () => {
const elapsed = Date.now() - startTime;
const progress = (elapsed % duration) / duration;
const opacityPulse = Math.sin(progress * Math.PI * 2);
material.opacity = startOpacity + opacityPulse * 0.2;
if (elapsed < duration * 2) {
requestAnimationFrame(animate);
} else {
material.opacity = startOpacity;
}
};
animate();
}
/**
* Clear all connections
*/
clearAll() {
const ids = Array.from(this.connections.keys());
ids.forEach(id => this.removeConnection(id));
console.log('✓ All connections cleared');
}
/**
* Get connection count
* @returns {number}
*/
getCount() {
return this.connections.size;
}
}
|