public void Init(IMy2DClipmapManager parent, int x, int y, int lod, ref BoundingBox2D bounds) { m_manager = (MyPlanetEnvironmentComponent)parent; var bounds3D = new BoundingBoxD(new Vector3D(bounds.Min, 0), new Vector3D(bounds.Max, 0)); Lod = lod; Face = m_manager.ActiveFace; var matrix = m_manager.ActiveClipmap.WorldMatrix; bounds3D = bounds3D.Transform(matrix); Coords = new Vector2I(x, y); Id = MyPlanetSectorId.MakeSectorId(x, y, m_manager.ActiveFace, lod); m_manager.RegisterProxy(this); MyEnvironmentSectorParameters sectorParams; matrix.Translation = Vector3D.Zero; sectorParams.SurfaceBasisX = Vector3.Transform(new Vector3(bounds.Width / 2, 0, 0), matrix); sectorParams.SurfaceBasisY = Vector3.Transform(new Vector3(0, bounds.Height / 2, 0), matrix); sectorParams.Center = bounds3D.Center; if (lod > m_manager.MaxLod) { return; } if (!m_manager.TryGetSector(Id, out EnvironmentSector)) { sectorParams.SectorId = Id; sectorParams.EntityId = MyPlanetSectorId.MakeSectorId(x, y, m_manager.ActiveFace, lod); sectorParams.Bounds = m_manager.GetBoundingShape(ref sectorParams.Center, ref sectorParams.SurfaceBasisX, ref sectorParams.SurfaceBasisY);; sectorParams.Environment = m_manager.EnvironmentDefinition; sectorParams.DataRange = new BoundingBox2I(Coords << lod, ((Coords + 1) << lod) - 1); sectorParams.Provider = m_manager.Providers[m_manager.ActiveFace]; EnvironmentSector = m_manager.EnvironmentDefinition.CreateSector(); EnvironmentSector.Init(m_manager, ref sectorParams); m_manager.Planet.AddChildEntity((MyEntity)EnvironmentSector); } m_manager.EnqueueOperation(this, lod); LodSet = lod; EnvironmentSector.OnLodCommit += sector_OnMyLodCommit; }
private unsafe void RasterSectorsForCollision(MyEntity entity) { if (!(entity is MyCubeGrid)) { return; } BoundingBoxD range = entity.PositionComp.WorldAABB; range.Inflate(8); range.Translate(-PlanetTranslation); Vector2I top = new Vector2I(1 << m_clipmaps[0].Depth) - 1; Vector3D *pos = stackalloc Vector3D[8]; range.GetCornersUnsafe(pos); // bitmask for faces, 7th bit is simple bit int markedFaces = 0; int firstFace = 0; for (var i = 0; i < 8; ++i) { Vector3D copy = pos[i]; int index = MyPlanetCubemapHelper.FindCubeFace(ref copy); firstFace = index; index = 1 << index; if ((markedFaces & ~index) != 0) { markedFaces |= 0x40; } markedFaces |= index; } // This way we can ensure a single code path. int startFace = 0; int endFace = 5; // If we only encounter one face we narrow it down. if ((markedFaces & 0x40) == 0) { startFace = endFace = firstFace; } for (int face = startFace; face <= endFace; ++face) { if (((1 << face) & markedFaces) == 0) { continue; } // Offset var offset = 1 << m_clipmaps[face].Depth - 1; BoundingBox2D bounds = BoundingBox2D.CreateInvalid(); for (int i = 0; i < 8; ++i) { Vector3D copy = pos[i]; Vector2D normCoords; MyPlanetCubemapHelper.ProjectForFace(ref copy, face, out normCoords); bounds.Include(normCoords); } bounds.Min += 1; bounds.Min *= offset; bounds.Max += 1; bounds.Max *= offset; // Calculate bounds in sectors. var start = new Vector2I((int)bounds.Min.X, (int)bounds.Min.Y); var end = new Vector2I((int)bounds.Max.X, (int)bounds.Max.Y); Vector2I.Max(ref start, ref Vector2I.Zero, out start); Vector2I.Min(ref end, ref top, out end); for (int x = start.X; x <= end.X; ++x) { for (int y = start.Y; y <= end.Y; ++y) { long sect = MyPlanetSectorId.MakeSectorId(x, y, face); List <MyOrientedBoundingBoxD> boxes; if (!m_obstructorsPerSector.TryGetValue(sect, out boxes)) { boxes = new List <MyOrientedBoundingBoxD>(); m_obstructorsPerSector.Add(sect, boxes); } var bb = entity.PositionComp.LocalAABB; bb.Inflate(8); // inflate by 8m to increase the likellyhood of overlap with trees' roots. boxes.Add(new MyOrientedBoundingBoxD((BoundingBoxD)bb, entity.PositionComp.WorldMatrix)); } } } }