Advertisement
Guest User

Batch Explode Improved

a guest
May 18th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. public Batch Explode(Vector3 position, float explosionRadius, int valueFilter, Exploder.ExplodeValueFilterOperation valueFilterOperation)
  2.         {
  3.             Batch batch = new Batch(this);
  4.  
  5.             Color tint = Material.GetColor("_Tint");
  6.  
  7.             Matrix4x4 transformMatrix = transform.worldToLocalMatrix;
  8.  
  9.             position += (transform.rotation * (Pivot));
  10.  
  11.             float explosionRadius2 = explosionRadius * explosionRadius;
  12.  
  13.             for (float x = position.x - explosionRadius; x <= position.x + explosionRadius; x += VoxelSize * 0.5f)
  14.                 for (float y = position.y - explosionRadius; y <= position.y + explosionRadius; y += VoxelSize * 0.5f)
  15.                     for (float z = position.z - explosionRadius; z <= position.z + explosionRadius; z += VoxelSize * 0.5f)
  16.                     {
  17.                         Vector3 checkPos = new Vector3(x, y, z);
  18.                         float distance = (checkPos - position).sqrMagnitude;
  19.                         if (distance <= explosionRadius2)
  20.                         {
  21.                             Vector3 localPos = transformMatrix.MultiplyPoint3x4(checkPos); //transform.InverseTransformPoint(pos);
  22.  
  23.                             //if (!Frames[CurrentFrame].IsLocalPositionInBounds(localPos)) continue;
  24.  
  25.                             int testX = (int)(localPos.x / VoxelSize);
  26.                             int testY = (int)(localPos.y / VoxelSize);
  27.                             int testZ = (int)(localPos.z / VoxelSize);
  28.                             if (testX < 0 || testY < 0 || testZ < 0 || testX >= XSize || testY >= YSize || testZ >= ZSize) continue;
  29.  
  30.                             if (Frames[CurrentFrame].Voxels[testX + XSize * (testY + YSize * testZ)].Active &&
  31.                             FilterExplosion(Frames[CurrentFrame].Voxels[testX + XSize * (testY + YSize * testZ)].Value, valueFilter, valueFilterOperation) &&
  32.                             distance <= explosionRadius2)
  33.                             {
  34.                                 Voxel v = Frames[CurrentFrame].Voxels[testX + XSize * (testY + YSize * testZ)];
  35.                                 v.Color *= tint;
  36.                                 batch.Add(v, testX, testY, testZ, checkPos - (transform.rotation * (Pivot)));
  37.                                 SetVoxelStateAtArrayPosition(testX, testY, testZ, VoxelState.Hidden);
  38.                             }
  39.                         }
  40.                     }
  41.  
  42.            
  43.  
  44.             return batch;
  45.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement