Guest User

Untitled

a guest
Jul 5th, 2019
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. Volume pvo = hit.collider.GetComponentInParent<Volume>();
  2. if (pvo != null) // check to see if we have hit a PicaVoxel Volume. because the Hitbox is a child object on the Volume, we use GetComponentInParent
  3. {
  4. ray = new Ray(hit.point, ray.direction); // now create a new ray starting at the hit position of the old ray
  5. var foundVoxel = false; // Keep track of whether we have previously found a voxel
  6. for (float d = 0; d < pvo.VoxelSize * (pvo.XSize >= pvo.YSize ? (pvo.ZSize >= pvo.XSize ? pvo.ZSize : pvo.XSize) : (pvo.ZSize >= pvo.YSize ? pvo.ZSize : pvo.YSize)) * 1.5f; d += pvo.VoxelSize * 0.5f) // iterate along the ray. We test for the largest XYZ dimension to make sure we pass all the way through the volume
  7. {
  8. Voxel? v = pvo.GetVoxelAtWorldPosition(ray.GetPoint(d)); // see if there's a voxel at the ray position
  9.  
  10. if (v.HasValue)
  11. {
  12. //print("hI");
  13. foundVoxel = true;
  14.  
  15. if (v.Value.Active)
  16. {
  17. // We have a voxel, and it's active, but we want to build a voxel so let's back up the ray a step
  18. Vector3 buildPos = ray.GetPoint(d - (pvo.VoxelSize * 0.5f));
  19. GameManager.instance.isTerrainUpdated = true;
  20. // Check that there is a voxel here, and it is inactive
  21. Voxel? v2 = pvo.GetVoxelAtWorldPosition(buildPos);
  22. if (v2.HasValue && !v2.Value.Active)
  23. {
  24. // Set this voxel to active, with a random color!
  25. pvo.SetVoxelAtWorldPosition(buildPos, new Voxel()
  26. {
  27. State = VoxelState.Active,
  28. Color = color,
  29. Value = 128
  30. });
  31. }
  32.  
  33. break;
  34. }
  35. }
  36. else
  37. {
  38.  
  39. if (!foundVoxel) continue; // We haven't found any voxels yet, so we can't have passed through the volume
  40. //print("hI ELSE");
  41. // We don't have a voxel, so we must have passed through the volume
  42. // Back the ray up a step
  43. Vector3 buildPos = ray.GetPoint(d - (pvo.VoxelSize * 0.5f));
  44.  
  45. // Check that there is a voxel here, and it is inactive
  46. Voxel? v2 = pvo.GetVoxelAtWorldPosition(buildPos);
  47. if (v2.HasValue && !v2.Value.Active)
  48. {
  49. // Set this voxel to active, with a random color!
  50. pvo.SetVoxelAtWorldPosition(buildPos, new Voxel()
  51. {
  52. State = VoxelState.Active,
  53. Color = color,
  54. Value = 128
  55. });
  56. }
  57.  
  58. break;
  59. }
  60. }
  61. }
  62.  
  63. }
Add Comment
Please, Sign In to add comment