Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //sample the pixels in the texture. they each are a velocity and a position
- //if a pixel is black, it doesn't count
- //if it is not black, use its position to calculate cohesion and separation, while using its velocity for alignment
- float2 SteerUsingTexture(uint3 id, Boid b)
- {
- float2 cohesion = float2(0,0);
- float2 separation = float2(0,0);
- float2 alignment = float2(0,0);
- int range = (int)round(neighborhoodRadius);
- int i = 0;
- for (int x = -range; x <= range; x++)
- {
- for (int y = -range; y <= range; y++)
- {
- if (!(x == 0 && y == 0))
- {
- //pixels
- float2 direction = float2(x, y);
- float2 normalizedDirection = normalize(direction);
- //pixels
- float2 myPos = (((b.position / spawnRadius) * rez) * .5) + (rez * .5);
- //pixels -> uv
- float2 coord = (myPos + direction)/(float)rez;
- float4 pixel = readTex.SampleLevel(sampler_readTex, coord, 0);
- //velocity is in xy, so z is 1 if the pixel is occupied, 0 if it is not.
- if (pixel.z > 0.0)
- {
- //pixels
- float dist = distance(x,y);
- cohesion += dist * -normalizedDirection;
- separation += 1.0 / dist * normalizedDirection;
- alignment += 1.0 / dist * pixel.xy;
- }
- }
- }
- }
- float2 v = ((cohesion * cohesionFactor) + (separation * separationFactor) + (alignment * alignmentFactor));
- if ((float)v != 0.0)
- {
- v = normalize(v);
- }
- return v;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement