Here's a version with dynamic cluster formation that maintains distinct structures while allowing particles to flow between clusters:
<!DOCTYPE html>
<html>
<head>
<title>Cluster-Based Particle Flow</title>
<style>
body { margin: 0; overflow: hidden; background: #000; }
canvas { display: block; }
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r134/three.min.js"></script>
<script>
// Scene Setup
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Particle System Parameters
const particleCount = 300;
const clusterCount = 3;
const clusterCenters = [
new THREE.Vector3(8, 0, 0),
new THREE.Vector3(-8, 0, 0),
new THREE.Vector3(0, 8, 0)
];
const params = {
attraction: 0.001,
repulsion: 0.008,
randomForce: 0.0005,
targetDistance: 2.5,
damping: 0.98,
boundary: 20
};
// Create Particles
const particlesGeometry = new THREE.BufferGeometry();
const positions = new Float32Array(particleCount * 3);
const velocities = new Float32Array(particleCount * 3);
const clusterAssignments = new Array(particleCount);
// Initialize particles with random positions and cluster assignments
for (let i = 0; i < particleCount; i++) {
positions[i * 3] = (Math.random() - 0.5) * 20;
positions[i * 3 + 1] = (Math.random() - 0.5) * 20;
positions[i * 3 + 2] = (Math.random() - 0.5) * 20;
// Random initial velocity
velocities[i * 3] = (Math.random() - 0.5) * 0.1;
velocities[i * 3 + 1] = (Math.random() - 0.5) * 0.1;
velocities[i * 3 + 2] = (Math.random() - 0.5) * 0.1;
// Assign to random cluster
clusterAssignments[i] = Math.floor(Math.random() * clusterCount);
}
particlesGeometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
particlesGeometry.setAttribute('velocity', new THREE.BufferAttribute(velocities, 3));
const particleMaterial = new THREE.PointsMaterial({
color: 0x88ccff,
size: 0.15,
transparent: true,
opacity: 0.8
});
const particles = new THREE.Points(particlesGeometry, particleMaterial);
scene.add(particles);
// Camera Positioning
camera.position.z = 30;
// Animation Loop
function animate() {
requestAnimationFrame(animate);
const positions = particlesGeometry.attributes.position.array;
const velocities = particlesGeometry.attributes.velocity.array;
// Apply forces
for (let i = 0; i < particleCount; i++) {
const i3 = i * 3;
const pos = new THREE.Vector3(
positions[i3],
positions[i3 + 1],
positions[i3 + 2]
);
// 1. Cluster Attraction/Repulsion
const cluster = clusterAssignments[i];
const clusterCenter = clusterCenters[cluster];
const toCluster = clusterCenter.clone().sub(pos);
const distToCluster = toCluster.length();
if (distToCluster > 0.1) {
const direction = toCluster.normalize();
const forceMagnitude = params.attraction * (distToCluster - params.targetDistance);
velocities[i3] += direction.x * forceMagnitude;
velocities[i3 + 1] += direction.y * forceMagnitude;
velocities[i3 + 2] += direction.z * forceMagnitude;
}
// 2. Inter-Particle Repulsion (Same Cluster)
for (let j = 0; j < particleCount; j++) {
if (i !== j && clusterAssignments[i] === clusterAssignments[j]) {
const j3 = j * 3;
const otherPos = new THREE.Vector3(
positions[j3],
positions[j3 + 1],
positions[j3 + 2]
);
const diff = pos.clone().sub(otherPos);
const dist = diff.length();
if (dist < 1.5 && dist > 0.1) {
const repulsionForce = params.repulsion / (dist * dist);
const force = diff.normalize().multiplyScalar(repulsionForce);
velocities[i3] += force.x;
velocities[i3 + 1] += force.y;
velocities[i3 + 2] += force.z;
}
}
}
// 3. Random Motion
velocities[i3] += (Math.random() - 0.5) * params.randomForce;
velocities[i3 + 1] += (Math.random() - 0.5) * params.randomForce;
velocities[i3 + 2] += (Math.random() - 0.5) * params.randomForce;
// 4. Apply Damping
velocities[i3] *= params.damping;
velocities[i3 + 1] *= params.damping;
velocities[i3 + 2] *= params.damping;
// 5. Update Position
positions[i3] += velocities[i3];
positions[i3 + 1] += velocities[i3 + 1];
positions[i3 + 2] += velocities[i3 + 2];
// 6. Boundary Conditions (Wrap-around)
if (Math.abs(positions[i3]) > params.boundary) {
positions[i3] = -Math.sign(positions[i3]) * params.boundary;
}
if (Math.abs(positions[i3 + 1]) > params.boundary) {
positions[i3 + 1] = -Math.sign(positions[i3 + 1]) * params.boundary;
}
if (Math.abs(positions[i3 + 2]) > params.boundary) {
positions[i3 + 2] = -Math.sign(positions[i3 + 2]) * params.boundary;
}
}
particlesGeometry.attributes.position.needsUpdate = true;
particlesGeometry.attributes.velocity.needsUpdate = true;
renderer.render(scene, camera);
}
// Handle resizing
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
animate();
</script>
</body>
</html>
Key Cluster Formation Features:
-
Multi-Cluster System:
- 3 distinct cluster centers positioned in different parts of the scene
- Particles assigned to random clusters at initialization
-
Dual-Force Dynamics:
- Cluster Attraction: Pulls particles toward their assigned cluster center
- Target Distance: Creates equilibrium at a specific radius from cluster center
- Inter-Particle Repulsion: Prevents collapse within clusters
-
Balanced Parameters:
attraction: Strength of pull toward cluster center
repulsion: Strength of particle-to-particle repulsion
targetDistance: Ideal spacing from cluster center
randomForce: Small random jitter to maintain motion
-
Visual Characteristics:
- Particles form distinct cloud-like clusters
- Clusters maintain separation through weak inter-cluster repulsion
- Individual particles exhibit fluid-like movement within clusters
-
Tunable Parameters:
- Increase
attraction for tighter clusters
- Increase
repulsion for more spacing within clusters
- Adjust
targetDistance to control cluster size
- Modify
randomForce to add more/less chaotic motion
To Enhance Clusters:
- Add color differentiation between clusters (modify particleMaterial.color)
- Implement cluster merging/分裂 logic for dynamic behavior
- Add velocity visualization trails
- Create attraction points between clusters for complex patterns
This creates a system where particles form stable, distinct clusters that maintain their identity while allowing individual particles to move freely within their cluster structure.