Advertisement
benimaru

Broken Texture Based Steering

May 1st, 2021
2,783
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     //sample the pixels in the texture. they each are a velocity and a position
  2.     //if a pixel is black, it doesn't count
  3.     //if it is not black, use its position to calculate cohesion and separation, while using its velocity for alignment
  4.  
  5. float2 SteerUsingTexture(uint3 id, Boid b)
  6. {
  7.     float2 cohesion = float2(0,0);
  8.     float2 separation = float2(0,0);
  9.     float2 alignment = float2(0,0);
  10.  
  11.     int range = (int)round(neighborhoodRadius);
  12.     int i = 0;
  13.  
  14.     for (int x = -range; x <= range; x++)
  15.     {
  16.         for (int y = -range; y <= range; y++)
  17.         {
  18.             if (!(x == 0 && y == 0))
  19.             {
  20.                 //pixels
  21.                 float2 direction = float2(x, y);
  22.                 float2 normalizedDirection = normalize(direction);
  23.                
  24.                 //pixels
  25.                 float2 myPos = (((b.position / spawnRadius) * rez) * .5) + (rez * .5);
  26.  
  27.                 //pixels -> uv
  28.                 float2 coord = (myPos + direction)/(float)rez;
  29.                
  30.                 float4 pixel = readTex.SampleLevel(sampler_readTex, coord, 0);
  31.                
  32.                 //velocity is in xy, so z is 1 if the pixel is occupied, 0 if it is not.
  33.                 if (pixel.z > 0.0)
  34.                 {
  35.                     //pixels
  36.                     float dist = distance(x,y);
  37.                     cohesion += dist * -normalizedDirection;
  38.                     separation += 1.0 / dist * normalizedDirection;
  39.                     alignment += 1.0 / dist * pixel.xy;
  40.                 }
  41.             }
  42.         }
  43.     }
  44.  
  45.     float2 v = ((cohesion * cohesionFactor) + (separation * separationFactor) + (alignment * alignmentFactor));
  46.     if ((float)v != 0.0)
  47.     {
  48.         v = normalize(v);
  49.     }
  50.  
  51.     return v;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement