public void ClampTo(SgtBoundsL other) { if (minX < other.minX) { minX = other.minX; } else if (minX > other.maxX) { minX = other.maxX; } if (minY < other.minY) { minY = other.minY; } else if (minY > other.maxY) { minY = other.maxY; } if (minZ < other.minZ) { minZ = other.minZ; } else if (minZ > other.maxZ) { minZ = other.maxZ; } if (maxX < other.minX) { maxX = other.minX; } else if (maxX > other.maxX) { maxX = other.maxX; } if (maxY < other.minY) { maxY = other.minY; } else if (maxY > other.maxY) { maxY = other.maxY; } if (maxZ < other.minZ) { maxZ = other.minZ; } else if (maxZ > other.maxZ) { maxZ = other.maxZ; } }
public void UpdateDebris() { var size = (long)System.Math.Ceiling(SgtHelper.Divide(HideDistance, (float)CellSize)); if (Target != null && CellSize > 0.0f && Prefabs != null && DebrisCountTarget > 0 && size > 0) { var worldPoint = Target.position; var centerX = (long)System.Math.Round(worldPoint.x / CellSize); var centerY = (long)System.Math.Round(worldPoint.y / CellSize); var centerZ = (long)System.Math.Round(worldPoint.z / CellSize); var newBounds = new SgtBoundsL(centerX, centerY, centerZ, size); if (newBounds != bounds) { var probability = DebrisCountTarget / (size * size * size); var cellMin = (float)CellSize * (0.5f - CellNoise); var cellMax = (float)CellSize * (0.5f + CellNoise); for (var z = newBounds.minZ; z <= newBounds.maxZ; z++) { for (var y = newBounds.minY; y <= newBounds.maxY; y++) { for (var x = newBounds.minX; x <= newBounds.maxX; x++) { if (bounds.Contains(x, y, z) == false) { // Calculate seed for this grid cell and try to minimize visible symmetry var seed = Seed ^ (x * (1 << 8)) ^ (y * (1 << 16)) ^ (z * (1 << 24)); SgtHelper.BeginRandomSeed((int)(seed % int.MaxValue)); { // Can debris potentially spawn in this cell? if (Random.value < probability) { var debrisX = x * CellSize + Random.Range(cellMin, cellMax); var debrisY = y * CellSize + Random.Range(cellMin, cellMax); var debrisZ = z * CellSize + Random.Range(cellMin, cellMax); var debrisPoint = new Vector3((float)debrisX, (float)debrisY, (float)debrisZ); // Spawn everywhere, or only inside specified shapes? if (SpawnInside == null || Random.value < SpawnInside.GetDensity(debrisPoint)) { Spawn(x, y, z, debrisPoint); } } } SgtHelper.EndRandomSeed(); } } } } bounds = newBounds; if (Debris != null) { for (var i = Debris.Count - 1; i >= 0; i--) { var debris = Debris[i]; if (debris == null) { Debris.RemoveAt(i); } else if (bounds.Contains(debris.Cell) == false) { Despawn(debris, i); } } } } UpdateDebrisScale(worldPoint); } else { ClearDebris(); } }
public void Write(SgtBoundsL bounds, SgtTerrain.Shell shell, int side, int index) { ring.Limit = shell.Limit; ring.Detail = shell.Detail; switch (side) { case 0: { if (bounds.minX >= shell.Outer.minX && bounds.maxX <= shell.Outer.maxX) { bounds.ClampTo(shell.Outer); ring.Outer = bounds.RectZY; ring.Inner = shell.Inner.IsInsideX(bounds.minX) == true ? shell.Inner.RectZY : default(SgtRectL); ring.Center = negativeX; ring.ExtentsX = negativeZ; ring.ExtentsY = positiveY; ring.Outer.SwapX(); ring.Inner.SwapX(); GetLevel(index).Write(ring); } } break; case 3: { if (bounds.minX >= shell.Outer.minX && bounds.maxX <= shell.Outer.maxX) { bounds.ClampTo(shell.Outer); ring.Outer = bounds.RectZY; ring.Inner = shell.Inner.IsInsideX(bounds.maxX - 1) == true ? shell.Inner.RectZY : default(SgtRectL); ring.Center = positiveX; ring.ExtentsX = positiveZ; ring.ExtentsY = positiveY; GetLevel(index).Write(ring); } } break; case 1: { if (bounds.minY >= shell.Outer.minY && bounds.maxY <= shell.Outer.maxY) { bounds.ClampTo(shell.Outer); ring.Outer = bounds.RectXZ; ring.Inner = shell.Inner.IsInsideY(bounds.minY) == true ? shell.Inner.RectXZ : default(SgtRectL); ring.Center = negativeY; ring.ExtentsX = negativeX; ring.ExtentsY = positiveZ; ring.Outer.SwapX(); ring.Inner.SwapX(); GetLevel(index).Write(ring); } } break; case 4: { if (bounds.minY >= shell.Outer.minY && bounds.maxY <= shell.Outer.maxY) { bounds.ClampTo(shell.Outer); ring.Outer = bounds.RectXZ; ring.Inner = shell.Inner.IsInsideY(bounds.maxY - 1) == true ? shell.Inner.RectXZ : default(SgtRectL); ring.Center = positiveY; ring.ExtentsX = negativeX; ring.ExtentsY = negativeZ; ring.Outer.SwapX(); ring.Outer.SwapY(); ring.Inner.SwapX(); ring.Inner.SwapY(); GetLevel(index).Write(ring); } } break; case 2: { if (bounds.minZ >= shell.Outer.minZ && bounds.maxZ <= shell.Outer.maxZ) { bounds.ClampTo(shell.Outer); ring.Outer = bounds.RectXY; ring.Inner = shell.Inner.IsInsideZ(bounds.minZ) == true ? shell.Inner.RectXY : default(SgtRectL); ring.Center = negativeZ; ring.ExtentsX = positiveX; ring.ExtentsY = positiveY; GetLevel(index).Write(ring); } } break; case 5: { if (bounds.minZ >= shell.Outer.minZ && bounds.maxZ <= shell.Outer.maxZ) { bounds.ClampTo(shell.Outer); ring.Outer = bounds.RectXY; ring.Inner = shell.Inner.IsInsideZ(bounds.maxZ - 1) == true ? shell.Inner.RectXY : default(SgtRectL); ring.Center = positiveZ; ring.ExtentsX = negativeX; ring.ExtentsY = positiveY; ring.Outer.SwapX(); ring.Inner.SwapX(); GetLevel(index).Write(ring); } } break; } }