Advertisement
JontePonte

jobs draft

Nov 19th, 2024
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.50 KB | None | 0 0
  1. using Unity.Burst;
  2. using Unity.Collections;
  3. using Unity.Collections.LowLevel.Unsafe;
  4. using Unity.Jobs;
  5. using Unity.Mathematics;
  6. using UnityEngine;
  7. using Particle = Simulation.Particle;
  8.  
  9. [BurstCompile]
  10. struct UpdateParticlesJob : IJobParallelFor
  11. {
  12.     public NativeArray<Particle> particles;
  13.     [ReadOnly] public NativeArray<Color> gradient;
  14.     [ReadOnly] public int gradientResolution;
  15.     [ReadOnly] public float deltaTime;
  16.     [ReadOnly] public float minPosX;
  17.     [ReadOnly] public float maxPosX;
  18.     [ReadOnly] public float minPosY;
  19.     [ReadOnly] public float maxPosY;
  20.     [ReadOnly] public Unity.Mathematics.Random rng;
  21.    
  22.     public void Execute(int i)
  23.     {
  24.         Particle particle = particles[i];
  25.         particle.position += particle.velocity * deltaTime;
  26.        
  27.         // float len = math.lengthsq(particle.velocity) / 5000f;
  28.         // int gradientIndex = math.clamp((int)(len * gradientResolution), 0, gradientResolution - 1);
  29.         // particle.color = gradient[gradientIndex];
  30.  
  31.         Vector2 absPos = new(Mathf.Abs(particle.position.x), Mathf.Abs(particle.position.y));
  32.         if (absPos.x < minPosX || absPos.x > maxPosX || absPos.y < minPosY || absPos.y > maxPosY)
  33.         {
  34.             float posX = rng.NextFloat(minPosX, maxPosX);
  35.             float posY = rng.NextFloat(minPosY, maxPosY);
  36.             particle.position = new Vector2(posX, posY);
  37.             particle.velocity = Vector2.zero;
  38.         }
  39.  
  40.         particles[i] = particle;
  41.     }
  42. }
  43.  
  44. [BurstCompile]
  45. struct AccelerationJob : IJobParallelFor
  46. {
  47.     [ReadOnly] public NativeArray<UnsafeList<Node>> nodes;
  48.     public NativeArray<Particle> particles;
  49.  
  50.     [ReadOnly] public float theta;
  51.     [ReadOnly] public float epsilon;
  52.     [ReadOnly] public float deltaTime;
  53.    
  54.     public void Execute(int i)
  55.     {
  56.         float2 acceleration = new();
  57.         float thetaSqr = theta * theta;
  58.         float epsilonSqr = epsilon * epsilon;
  59.  
  60.         for (int j = 0; j < nodes.Length; j++)
  61.         {
  62.             int nodeIndex = 0;
  63.             while (true)
  64.             {
  65.                 Node node = nodes[j][nodeIndex];
  66.                 float2 diff = node.centerOfMass - particles[i].position;
  67.                 float sqrDst = math.max(math.lengthsq(diff), 0.05f);
  68.  
  69.                 if (node.childIndex == 0 || node.boundary.size * node.boundary.size < sqrDst * thetaSqr)
  70.                 {
  71.                     float denom = (sqrDst + epsilonSqr) * math.sqrt(sqrDst);
  72.                     acceleration += diff * node.mass / denom * deltaTime;
  73.  
  74.                     if (node.next == 0) break;
  75.                     nodeIndex = node.next;
  76.                 }
  77.                 else nodeIndex = node.childIndex;
  78.             }
  79.         }
  80.        
  81.        
  82.         Particle particle = particles[i];
  83.         particles[i] = new Particle
  84.         {
  85.             position = particle.position,
  86.             velocity = particle.velocity + acceleration,
  87.             mass = particle.mass,
  88.             color = particle.color
  89.         };
  90.     }
  91. }
  92.  
  93.    
  94. [BurstCompile]
  95. struct InsertJob : IJob
  96. {
  97.     [ReadOnly] public NativeArray<Particle> particles;
  98.     public NativeList<Node> nodes;
  99.  
  100.     public void Execute()
  101.     {
  102.         int count = particles.Length;
  103.         for (int i = 0; i < count; i++)
  104.         {
  105.             Insert(particles[i]);
  106.         }
  107.     }
  108.  
  109.     void Insert(Particle particle)
  110.     {
  111.         int nodeIndex = 0;
  112.         Node node = nodes[0];
  113.  
  114.         while (node.childIndex != 0)
  115.         {
  116.             int quad = node.boundary.FindNode(particle.position);
  117.             nodeIndex = quad + node.childIndex;
  118.             node = nodes[nodeIndex];
  119.         }
  120.  
  121.         if (node.mass == 0)
  122.         {
  123.             node.centerOfMass = particle.position;
  124.             node.mass = particle.mass;
  125.             nodes[nodeIndex] = node;
  126.             return;
  127.         }
  128.  
  129.         if (math.all(node.centerOfMass == particle.position))
  130.         {
  131.             node.mass += particle.mass;
  132.             nodes[nodeIndex] = node;
  133.             return;
  134.         }
  135.  
  136.         Vector2 nodePos = node.centerOfMass;
  137.         float nodeMass = node.mass;
  138.  
  139.         while (true)
  140.         {
  141.             Subdivide(nodeIndex);
  142.             node = nodes[nodeIndex];
  143.             int childIndex = node.childIndex;
  144.             int q1 = node.boundary.FindNode(nodePos);
  145.             int q2 = node.boundary.FindNode(particle.position);
  146.  
  147.             if (q1 == q2)
  148.             {
  149.                 nodeIndex = q1 + childIndex;
  150.             }
  151.             else
  152.             {
  153.                 int n1 = q1 + childIndex;
  154.                 int n2 = q2 + childIndex;
  155.  
  156.                 nodes[n1] = new Node
  157.                 {
  158.                     centerOfMass = nodePos,
  159.                     mass = nodeMass,
  160.                     boundary = nodes[n1].boundary,
  161.                     next = nodes[n1].next
  162.                 };
  163.  
  164.                 nodes[n2] = new Node
  165.                 {
  166.                     centerOfMass = particle.position,
  167.                     mass = particle.mass,
  168.                     boundary = nodes[n2].boundary,
  169.                     next = nodes[n2].next
  170.                 };
  171.  
  172.                 return;
  173.             }
  174.         }
  175.     }
  176.  
  177.     void Subdivide(int parentIndex)
  178.     {
  179.         Node parent = nodes[parentIndex];
  180.         parent.childIndex = nodes.Length;
  181.         nodes[parentIndex] = parent;
  182.  
  183.         float2 parentPos = parent.boundary.position;
  184.         float halfSize = parent.boundary.size / 2;
  185.  
  186.         float2 nwPosition = new float2(parentPos.x, parentPos.y + halfSize);
  187.         float2 nePosition = new float2(parentPos.x + halfSize, parentPos.y + halfSize);
  188.         float2 swPosition = new float2(parentPos.x, parentPos.y);
  189.         float2 sePosition = new float2(parentPos.x + halfSize, parentPos.y);
  190.  
  191.         Node nw = new Node
  192.         {
  193.             boundary = new Quad { position = nwPosition, size = halfSize },
  194.             next = parent.childIndex + 1
  195.         };
  196.  
  197.         Node ne = new Node
  198.         {
  199.             boundary = new Quad { position = nePosition, size = halfSize },
  200.             next = parent.childIndex + 2
  201.         };
  202.  
  203.         Node sw = new Node
  204.         {
  205.             boundary = new Quad { position = swPosition, size = halfSize },
  206.             next = parent.childIndex + 3
  207.         };
  208.  
  209.         Node se = new Node
  210.         {
  211.             boundary = new Quad { position = sePosition, size = halfSize },
  212.             next = parent.next
  213.         };
  214.  
  215.         nodes.Add(nw);
  216.         nodes.Add(ne);
  217.         nodes.Add(sw);
  218.         nodes.Add(se);
  219.     }
  220. }
  221.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement