|
| 1 | +uniform sampler2D tPositions; |
| 2 | +uniform sampler2D tVelocities; |
| 3 | +uniform float uDeltaTime; |
| 4 | + |
| 5 | +uniform float uMaxSpeed; |
| 6 | +uniform float uPerceptionRadius; |
| 7 | + |
| 8 | +uniform float uSeparationWeight; |
| 9 | +uniform float uAlignmentWeight; |
| 10 | +uniform float uCohesionWeight; |
| 11 | + |
| 12 | +uniform float uBorderForce; |
| 13 | +uniform float uBorderDistance; |
| 14 | + |
| 15 | +uniform float uPredatorRepulsionStrength; |
| 16 | +uniform float uPredatorRepulsionRadius; |
| 17 | + |
| 18 | +varying vec2 uv; |
| 19 | + |
| 20 | +float random(vec2 st) { |
| 21 | + return fract(sin(dot(st.xy, vec2(12.9898, 78.233))) * 43758.5453123); |
| 22 | +} |
| 23 | + |
| 24 | +vec2 flockingForces(vec2 position, vec2 velocity) { |
| 25 | + vec2 texSize = vec2(textureSize(tVelocities, 0)); |
| 26 | + vec2 separation = vec2(0.0); |
| 27 | + vec2 alignment = vec2(0.0); |
| 28 | + vec2 cohesion = vec2(0.0); |
| 29 | + int count = 0; |
| 30 | + |
| 31 | + // Accumulate forces from neighbors |
| 32 | + for(float y = 1.0; y < texSize.y; y++) { |
| 33 | + for(float x = 1.0; x < texSize.x; x++) { |
| 34 | + vec2 neighborUV = vec2(x, y) / texSize; |
| 35 | + if(neighborUV == uv) continue; |
| 36 | + |
| 37 | + vec2 neighborPos = texture2D(tPositions, neighborUV).xy; |
| 38 | + vec2 neighborVel = texture2D(tVelocities, neighborUV).xy; |
| 39 | + float dist = distance(position, neighborPos); |
| 40 | + |
| 41 | + if(dist < uPerceptionRadius && dist > 0.0) { |
| 42 | + separation += normalize(position - neighborPos) * (1.0 - dist/uPerceptionRadius); |
| 43 | + alignment += neighborVel; |
| 44 | + cohesion += neighborPos; |
| 45 | + count++; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + if(count > 0) { |
| 51 | + separation = normalize(separation) * uSeparationWeight; |
| 52 | + alignment = normalize(alignment/float(count)) * uAlignmentWeight; |
| 53 | + cohesion = normalize((cohesion/float(count)) - position) * uCohesionWeight; |
| 54 | + } |
| 55 | + return separation + alignment + cohesion; |
| 56 | +} |
| 57 | + |
| 58 | +vec2 borderRepulsion(vec2 position) { |
| 59 | + vec2 distToBorder = vec2(1.0 - abs(position.x), 1.0 - abs(position.y)); |
| 60 | + vec2 force = vec2(0.0); |
| 61 | + |
| 62 | + if(distToBorder.x < uBorderDistance) { |
| 63 | + force.x = (uBorderDistance - distToBorder.x) / uBorderDistance * -sign(position.x); |
| 64 | + } |
| 65 | + if(distToBorder.y < uBorderDistance) { |
| 66 | + force.y = (uBorderDistance - distToBorder.y) / uBorderDistance * -sign(position.y); |
| 67 | + } |
| 68 | + return force * uBorderForce; |
| 69 | +} |
| 70 | + |
| 71 | +vec2 randomWandering(vec2 position, vec2 velocity) { |
| 72 | + float rand = random(position.xy) * 2.0 - 1.0; |
| 73 | + float angle = rand * radians(10.0); |
| 74 | + mat2 rotationMatrix = mat2( |
| 75 | + cos(angle), -sin(angle), |
| 76 | + sin(angle), cos(angle) |
| 77 | + ); |
| 78 | + return normalize(rotationMatrix * velocity) * 5.; |
| 79 | +} |
| 80 | + |
| 81 | +vec2 predatorRepulsion(vec2 position, vec2 velocity) { |
| 82 | + vec2 predatorPos = texture2D(tPositions, vec2(0.0)).xy; |
| 83 | + vec2 toPredator = predatorPos - position; |
| 84 | + float predatorDist = length(toPredator); |
| 85 | + |
| 86 | + if (predatorDist < uPredatorRepulsionRadius) { |
| 87 | + return normalize(-1. * toPredator) * uPredatorRepulsionStrength * (1.0 - predatorDist / uPredatorRepulsionRadius); |
| 88 | + } |
| 89 | + return vec2(0.0); |
| 90 | +} |
| 91 | + |
| 92 | +void main() { |
| 93 | + vec2 position = texture2D(tPositions, uv).xy; |
| 94 | + vec2 velocity = texture2D(tVelocities, uv).xy; |
| 95 | + vec2 acceleration = borderRepulsion(position); |
| 96 | + |
| 97 | + bool isPredator = floor(gl_FragCoord.xy) == vec2(0.0); |
| 98 | + |
| 99 | + if (isPredator) { |
| 100 | + acceleration += randomWandering(position, velocity); |
| 101 | + } else { |
| 102 | + acceleration += flockingForces(position, velocity) + predatorRepulsion(position, velocity); |
| 103 | + } |
| 104 | + velocity += acceleration * uDeltaTime; |
| 105 | + |
| 106 | + float speedLimit = isPredator ? uMaxSpeed / 2.0 : uMaxSpeed; |
| 107 | + if(length(velocity) > speedLimit) { |
| 108 | + velocity = normalize(velocity) * speedLimit; |
| 109 | + } |
| 110 | + |
| 111 | + gl_FragColor = vec4(velocity, 0.0, 1.0); |
| 112 | +} |
0 commit comments