Exemple #1
0
        public Vector3D DetectEntitiesInBox(MyObjectBuilder_CubeGrid grid, Vector3D spawnOrigin)
        {
            BoundingBoxD box = CalculateBoundingBox(grid);

            box.Translate(spawnOrigin);
            List <MyEntity> pruneList;

            do
            {
                pruneList = new List <MyEntity>();
                MyGamePruningStructure.GetAllEntitiesInBox(ref box, pruneList, MyEntityQueryType.Both);
                if (pruneList.Count <= 0)
                {
                    continue;
                }
                foreach (MyEntity entity in pruneList)
                {
                    Core.GeneralLog.WriteToLog("DetectEntitiesInBox", $"{entity}");
                }
                //box = box.Translate(new Vector3D(10, 0, 0));
                //box = box.Translate(new Vector3D(.5, .5, .5));
                box = box.Translate(Vector3D.Up + .25);
            } while (pruneList.Count > 0);
            //MyGamePruningStructure.GetAllTopMostEntitiesInSphere(ref pruneSphere, pruneList, MyEntityQueryType.Dynamic);
            return(box.Center);
        }
        public unsafe void QuerySurfaceParameters(Vector3D localOrigin, ref BoundingBoxD queryBounds, List <Vector3> queries, List <MySurfaceParams> results)
        {
            localOrigin -= Planet.PositionLeftBottomCorner;

            using (Planet.Storage.Pin())
            {
                var bounds = (BoundingBox)queryBounds.Translate(-Planet.PositionLeftBottomCorner);

                Planet.Provider.Shape.PrepareCache();
                Planet.Provider.Material.PrepareRulesForBox(ref bounds);

                if (results.Capacity != queries.Count)
                {
                    results.Capacity = queries.Count;
                }

                fixed(MySurfaceParams *pars = results.GetInternalArray())
                {
                    for (int i = 0; i < queries.Count; ++i)
                    {
                        Planet.Provider.ComputeCombinedMaterialAndSurface(queries[i] + localOrigin, true, out pars[i]);
                        pars[i].Position -= localOrigin;
                    }
                }

                results.SetSize(queries.Count);
            }
        }
Exemple #3
0
        public static unsafe void GetItemsInAabb(this MyEnvironmentSector sector, ref BoundingBoxD aabb, List <int> itemsInBox)
        {
            if (sector.DataView == null)
            {
                return;
            }

            aabb.Translate(-sector.SectorCenter);

            for (int sectorInd = 0; sectorInd < sector.DataView.LogicalSectors.Count; sectorInd++)
            {
                var logicalSector = sector.DataView.LogicalSectors[sectorInd];
                var logicalItems  = logicalSector.Items;
                var cnt           = logicalItems.Count;

                fixed(ItemInfo *items = logicalItems.GetInternalArray())
                for (int i = 0; i < cnt; ++i)
                {
                    if (items[i].DefinitionIndex >= 0 && aabb.Contains(items[i].Position) != ContainmentType.Disjoint)
                    {
                        itemsInBox.Add(i);
                    }
                }
            }
        }
Exemple #4
0
        public static BoundingBoxD GetBoundingBox(MyObjectBuilder_CubeGrid entity)
        {
            var min = new Vector3D(int.MaxValue, int.MaxValue, int.MaxValue);
            var max = new Vector3D(int.MinValue, int.MinValue, int.MinValue);

            foreach (var block in entity.CubeBlocks)
            {
                min.X = Math.Min(min.X, block.Min.X);
                min.Y = Math.Min(min.Y, block.Min.Y);
                min.Z = Math.Min(min.Z, block.Min.Z);
                max.X = Math.Max(max.X, block.Min.X);       // TODO: resolve cubetype size.
                max.Y = Math.Max(max.Y, block.Min.Y);
                max.Z = Math.Max(max.Z, block.Min.Z);
            }

            // scale box to GridSize
            var size = max - min;
            var len  = entity.GridSizeEnum.ToLength();

            size = new Vector3D(size.X * len, size.Y * len, size.Z * len);

            // translate box according to min/max, but reset origin.
            var bb = new BoundingBoxD(Vector3D.Zero, size);

            // TODO: translate for rotation.
            //bb. ????

            // translate position.
            bb.Translate(entity.PositionAndOrientation.Value.Position);


            return(bb);
        }
Exemple #5
0
        protected virtual HashSet <IMyEntity> GetMissilesInBoundingBox()
        {
            BoundingBoxD box = Entity.WorldAABB.GetInflated(BbInflateAmount);

            if (Math.Abs(BoundingBoxOffsetFront) > double.Epsilon)
            {
                box = box.Translate(Entity.LocalMatrix.Forward * (float)BoundingBoxOffsetFront);
            }
            //Log.Info("Bounding box in GetMissilesInBoundingBox for "+ GetType()+ " is " + box.Size);
            List <IMyEntity>    entitiesFound = MyAPIGateway.Entities.GetEntitiesInAABB(ref box);
            HashSet <IMyEntity> entitySet     = new HashSet <IMyEntity>();

            foreach (IMyEntity ent in entitiesFound)
            {
                // Log.Info("in guidedmissilehook: found Entity : " + ent);
                if ((ent.GetType().ToString() == GuidedMissileSingleton.SandboxGameWeaponsMyMissile) && (!GuidedMissileSingleton.IsGuidedMissile(ent)))
                {
                    Log.Info("detected something not yet added with speed: " + (ent.Physics.LinearVelocity - Entity.GetTopMostParent().Physics.LinearVelocity).Length());
                    Log.Info("topmostparent velocity was " + Entity.GetTopMostParent().Physics.LinearVelocity);
                    if ((ent.Physics.LinearVelocity - Entity.GetTopMostParent().Physics.LinearVelocity).Length() < MaxSpeedForGuidance)
                    {
                        entitySet.Add(ent);
                    }
                    else
                    {
                        Log.Info("Missile was too fast! Adding to ignoreSet");
                        GuidedMissileSingleton.GetInstance().IgnoreSet.Add(ent);
                    }
                }
            }
            return(entitySet);
        }
Exemple #6
0
        private void updateCamera(IMyEntity target)
        {
            if (target == null)
            {
                return;
            }

            hasCamTarget = true;

            var          Material = MyStringId.GetOrCompute("Crosshair");
            float        width    = (float)target.WorldAABB.Extents.X;
            float        height   = (float)target.WorldAABB.Extents.Z;
            BoundingBoxD box      = target.WorldAABB;

            box.Translate(target.WorldMatrix);
            Vector3D center = target.WorldAABB.Min;

            center.X += box.HalfExtents.X;
            center.Y += box.HalfExtents.Y;
            center.Z += box.HalfExtents.Z;

            try {
                MyTransparentGeometry.AddBillboardOriented(Material, Color.WhiteSmoke.ToVector4(), center, MyAPIGateway.Session.Camera.WorldMatrix.Left, MyAPIGateway.Session.Camera.WorldMatrix.Up, height, BlendTypeEnum.SDR);
            } catch (NullReferenceException ex) {
                //MyLog.Default.Error("Error while drawing billboard: " + ex);
            }
            //MyLog.Default.WriteLine("Drawing Billbord at: " + center);
        }
        /// <summary>
        /// Writes all surrounding blocks around the given one with the given size.
        /// </summary>
        private void GetSurroundingBlocksFromStaticGrids(MySlimBlock block, MyCubeSize cubeSizeEnum, HashSet <MyCubeBlock> outBlocks)
        {
            outBlocks.Clear();

            BoundingBoxD aabb = new BoundingBoxD(block.Min * block.CubeGrid.GridSize - block.CubeGrid.GridSize / 2, block.Max * block.CubeGrid.GridSize + block.CubeGrid.GridSize / 2);

            if (block.FatBlock != null)
            {
                aabb = (BoundingBoxD)block.FatBlock.Model.BoundingBox;
                aabb.Translate(block.Position * block.CubeGrid.GridSize);
            }

            aabb = aabb.Transform(block.CubeGrid.WorldMatrix);
            aabb.Inflate(0.125);

            List <MyEntity> boxOverlapList = new List <MyEntity>();

            MyEntities.GetElementsInBox(ref aabb, boxOverlapList);

            for (int i = 0; i < boxOverlapList.Count; i++)
            {
                var otherBlock = boxOverlapList[i] as MyCubeBlock;
                if (otherBlock != null && otherBlock.CubeGrid.IsStatic && otherBlock.CubeGrid.EnableSmallToLargeConnections && otherBlock.SlimBlock != block && otherBlock.CubeGrid != block.CubeGrid &&
                    otherBlock.CubeGrid.GridSizeEnum == cubeSizeEnum && !(otherBlock is MyCompoundCubeBlock) && !(otherBlock is MyFracturedBlock))
                {
                    outBlocks.Add(otherBlock);
                }
            }

            boxOverlapList.Clear();
        }
Exemple #8
0
        public override void DebugDraw()
        {
            base.DebugDraw();

            if (m_aabbPhantom != null && MyDebugDrawSettings.DEBUG_DRAW_VOXEL_MAP_AABB && IsInWorld)
            {
                var offset = ClusterToWorld(Vector3D.Zero);

                BoundingBoxD phantom = (BoundingBoxD)m_aabbPhantom.Aabb;
                phantom.Translate(offset);

                MyRenderProxy.DebugDrawAABB(phantom, Color.Orange, 1.0f, 1.0f, true);
            }

            if (MyDebugDrawSettings.DEBUG_DRAW_VOXEL_PHYSICS_PREDICTION)
            {
                foreach (var entity in m_nearbyEntities)
                {
                    if (entity.MarkedForClose)
                    {
                        continue;
                    }

                    var worldAabb = entity.WorldAABB;
                    MyRenderProxy.DebugDrawAABB(worldAabb, Color.Bisque, 1f, 1f, true);
                    MyRenderProxy.DebugDrawLine3D(GetWorldMatrix().Translation, worldAabb.Center, Color.Bisque, Color.BlanchedAlmond, true);

                    BoundingBoxD predAabb;
                    GetPrediction(entity, out predAabb);
                    MyRenderProxy.DebugDrawAABB(predAabb, Color.Crimson, 1f, 1f, true);
                }

                using (var batch = MyRenderProxy.DebugDrawBatchAABB(GetWorldMatrix(), new Color(Color.Cyan, 0.2f), true, false))
                {
                    int i = 0;
                    foreach (var entry in m_workTracker)
                    {
                        i++;
                        BoundingBoxD localAabb;
                        var          localCell = entry.Key;

                        localAabb.Min  = localCell.CoordInLod << localCell.Lod;
                        localAabb.Min *= MyVoxelConstants.GEOMETRY_CELL_SIZE_IN_METRES;
                        localAabb.Min -= m_voxelMap.SizeInMetresHalf;
                        localAabb.Max  = localAabb.Min + MyVoxelConstants.GEOMETRY_CELL_SIZE_IN_METRES;

                        batch.Add(ref localAabb);
                        if (i > 250)
                        {
                            break;
                        }
                    }
                }
            }
        }
Exemple #9
0
        private void GetSurroundingBlocksFromStaticGrids(MySlimBlock block, MyCubeSize cubeSizeEnum, HashSet <MyCubeBlock> outBlocks)
        {
            outBlocks.Clear();
            BoundingBoxD boundingBox = new BoundingBoxD((Vector3D)((block.Min * block.CubeGrid.GridSize) - (block.CubeGrid.GridSize / 2f)), (block.Max * block.CubeGrid.GridSize) + (block.CubeGrid.GridSize / 2f));

            if (block.FatBlock != null)
            {
                Matrix matrix;
                boundingBox = block.FatBlock.Model.BoundingBox;
                block.FatBlock.Orientation.GetMatrix(out matrix);
                boundingBox = boundingBox.TransformFast(matrix);
                boundingBox.Translate(boundingBox.Center);
            }
            boundingBox = boundingBox.TransformFast(block.CubeGrid.WorldMatrix);
            boundingBox.Inflate((double)0.125);
            List <VRage.Game.Entity.MyEntity> foundElements = new List <VRage.Game.Entity.MyEntity>();

            Sandbox.Game.Entities.MyEntities.GetElementsInBox(ref boundingBox, foundElements);
            int num = 0;

            while (true)
            {
                while (true)
                {
                    if (num >= foundElements.Count)
                    {
                        foundElements.Clear();
                        return;
                    }
                    MyCubeBlock item = foundElements[num] as MyCubeBlock;
                    if (((item != null) && (!ReferenceEquals(item.SlimBlock, block) && (item.CubeGrid.IsStatic && (item.CubeGrid.EnableSmallToLargeConnections && (item.CubeGrid.SmallToLargeConnectionsInitialized && (!ReferenceEquals(item.CubeGrid, block.CubeGrid) && ((item.CubeGrid.GridSizeEnum == cubeSizeEnum) && !(item is MyFracturedBlock)))))))) && !item.Components.Has <MyFractureComponentBase>())
                    {
                        MyCompoundCubeBlock block3 = item as MyCompoundCubeBlock;
                        if (block3 != null)
                        {
                            foreach (MySlimBlock block4 in block3.GetBlocks())
                            {
                                if (ReferenceEquals(block4, block))
                                {
                                    continue;
                                }
                                if (!block4.FatBlock.Components.Has <MyFractureComponentBase>())
                                {
                                    outBlocks.Add(block4.FatBlock);
                                }
                            }
                            break;
                        }
                        outBlocks.Add(item);
                    }
                    break;
                }
                num++;
            }
        }
Exemple #10
0
        private void GetSurroundingBlocksFromStaticGrids(MySlimBlock block, MyCubeSize cubeSizeEnum, HashSet <MySlimBlock> outBlocks)
        {
            outBlocks.Clear();
            BoundingBoxD aabbForNeighbors = new BoundingBoxD((Vector3D)(block.Min * block.CubeGrid.GridSize), (Vector3D)(block.Max * block.CubeGrid.GridSize));
            BoundingBoxD box = new BoundingBoxD((Vector3D)((block.Min * block.CubeGrid.GridSize) - (block.CubeGrid.GridSize / 2f)), (block.Max * block.CubeGrid.GridSize) + (block.CubeGrid.GridSize / 2f));

            if (block.FatBlock != null)
            {
                Matrix matrix;
                box = block.FatBlock.Model.BoundingBox;
                block.FatBlock.Orientation.GetMatrix(out matrix);
                box = box.TransformFast(matrix);
                box.Translate(box.Center);
            }
            box.Inflate((double)0.125);
            BoundingBoxD boundingBox = box.TransformFast(block.CubeGrid.WorldMatrix);
            List <VRage.Game.Entity.MyEntity> foundElements = new List <VRage.Game.Entity.MyEntity>();

            Sandbox.Game.Entities.MyEntities.GetElementsInBox(ref boundingBox, foundElements);
            for (int i = 0; i < foundElements.Count; i++)
            {
                MyCubeGrid objA = foundElements[i] as MyCubeGrid;
                if (((objA != null) && (objA.IsStatic && (!ReferenceEquals(objA, block.CubeGrid) && (objA.EnableSmallToLargeConnections && objA.SmallToLargeConnectionsInitialized)))) && (objA.GridSizeEnum == cubeSizeEnum))
                {
                    m_tmpSlimBlocksList.Clear();
                    objA.GetBlocksIntersectingOBB(box, block.CubeGrid.WorldMatrix, m_tmpSlimBlocksList);
                    CheckNeighborBlocks(block, aabbForNeighbors, objA, m_tmpSlimBlocksList);
                    foreach (MySlimBlock block2 in m_tmpSlimBlocksList)
                    {
                        if (block2.FatBlock == null)
                        {
                            outBlocks.Add(block2);
                            continue;
                        }
                        if (!(block2.FatBlock is MyFracturedBlock) && !block2.FatBlock.Components.Has <MyFractureComponentBase>())
                        {
                            if (block2.FatBlock is MyCompoundCubeBlock)
                            {
                                foreach (MySlimBlock block3 in (block2.FatBlock as MyCompoundCubeBlock).GetBlocks())
                                {
                                    if (!block3.FatBlock.Components.Has <MyFractureComponentBase>())
                                    {
                                        outBlocks.Add(block3);
                                    }
                                }
                                continue;
                            }
                            outBlocks.Add(block2);
                        }
                    }
                    m_tmpSlimBlocksList.Clear();
                }
            }
            foundElements.Clear();
        }
        /// <summary>
        /// Writes all surrounding blocks around the given one with the given size.
        /// </summary>
        private void GetSurroundingBlocksFromStaticGrids(MySlimBlock block, MyCubeSize cubeSizeEnum, HashSet <MyCubeBlock> outBlocks)
        {
            outBlocks.Clear();

            BoundingBoxD aabb = new BoundingBoxD(block.Min * block.CubeGrid.GridSize - block.CubeGrid.GridSize / 2, block.Max * block.CubeGrid.GridSize + block.CubeGrid.GridSize / 2);

            if (block.FatBlock != null)
            {
                var aabbCenter = aabb.Center;
                aabb = (BoundingBoxD)block.FatBlock.Model.BoundingBox;
                Matrix m;
                block.FatBlock.Orientation.GetMatrix(out m);
                aabb = aabb.TransformFast(m);
                aabb.Translate(aabbCenter);
            }

            aabb = aabb.TransformFast(block.CubeGrid.WorldMatrix);
            aabb.Inflate(0.125);

            List <MyEntity> boxOverlapList = new List <MyEntity>();

            MyEntities.GetElementsInBox(ref aabb, boxOverlapList);

            for (int i = 0; i < boxOverlapList.Count; i++)
            {
                var otherBlock = boxOverlapList[i] as MyCubeBlock;
                if (otherBlock != null && otherBlock.SlimBlock != block &&
                    otherBlock.CubeGrid.IsStatic && otherBlock.CubeGrid.EnableSmallToLargeConnections && otherBlock.CubeGrid.SmallToLargeConnectionsInitialized &&
                    otherBlock.CubeGrid != block.CubeGrid && otherBlock.CubeGrid.GridSizeEnum == cubeSizeEnum && !(otherBlock is MyFracturedBlock) &&
                    !(otherBlock.Components.Has <MyFractureComponentBase>()))
                {
                    var compound = otherBlock as MyCompoundCubeBlock;
                    if (compound != null)
                    {
                        foreach (var blockInCompound in compound.GetBlocks())
                        {
                            if (blockInCompound != block && !(blockInCompound.FatBlock.Components.Has <MyFractureComponentBase>()))
                            {
                                outBlocks.Add(blockInCompound.FatBlock);
                            }
                        }
                    }
                    else
                    {
                        outBlocks.Add(otherBlock);
                    }
                }
            }

            boxOverlapList.Clear();
        }
        /// <summary>
        /// Writes all surrounding blocks around the given one with the given size.
        /// </summary>
        private void GetSurroundingBlocksFromStaticGrids(MySlimBlock block, MyCubeSize cubeSizeEnum, HashSet <MyCubeBlock> outBlocks)
        {
            outBlocks.Clear();

            BoundingBoxD aabb = new BoundingBoxD(block.Min * block.CubeGrid.GridSize - block.CubeGrid.GridSize / 2, block.Max * block.CubeGrid.GridSize + block.CubeGrid.GridSize / 2);

            if (block.FatBlock != null)
            {
                aabb = (BoundingBoxD)block.FatBlock.Model.BoundingBox;
                aabb.Translate(block.Position * block.CubeGrid.GridSize);
            }

            aabb = aabb.Transform(block.CubeGrid.WorldMatrix);
            aabb.Inflate(0.125);

            List <MyEntity> boxOverlapList = new List <MyEntity>();

            MyEntities.GetElementsInBox(ref aabb, boxOverlapList);

            for (int i = 0; i < boxOverlapList.Count; i++)
            {
                var otherGrid = boxOverlapList[i] as MyCubeGrid;
                if (otherGrid != null)
                {
                    foreach (var gridBlock in otherGrid.CubeBlocks)
                    {
                        if (IsValidForConnection(gridBlock.FatBlock, block, cubeSizeEnum))
                        {
                            if (!IsCompoundAndProcessed(gridBlock.FatBlock, block, outBlocks))
                            {
                                outBlocks.Add(gridBlock.FatBlock);
                            }
                        }
                    }
                }
                else
                {
                    var otherBlock = boxOverlapList[i] as MyCubeBlock;
                    if (IsValidForConnection(otherBlock, block, cubeSizeEnum))
                    {
                        if (!IsCompoundAndProcessed(otherBlock, block, outBlocks))
                        {
                            outBlocks.Add(otherBlock);
                        }
                    }
                }
            }

            boxOverlapList.Clear();
        }
Exemple #13
0
 public override unsafe void DebugDraw()
 {
     base.DebugDraw();
     if (((this.m_aabbPhantom != null) && MyDebugDrawSettings.DEBUG_DRAW_VOXEL_MAP_AABB) && this.IsInWorld)
     {
         BoundingBoxD aabb = this.m_aabbPhantom.Aabb;
         aabb.Translate(this.ClusterToWorld(Vector3.Zero));
         MyRenderProxy.DebugDrawAABB(aabb, Color.Orange, 1f, 1f, true, false, false);
     }
     if (MyDebugDrawSettings.DEBUG_DRAW_VOXEL_PHYSICS_PREDICTION)
     {
         foreach (IMyEntity entity in this.m_nearbyEntities)
         {
             if (!entity.MarkedForClose)
             {
                 BoundingBoxD xd3;
                 BoundingBoxD worldAABB = entity.WorldAABB;
                 MyRenderProxy.DebugDrawAABB(worldAABB, Color.Bisque, 1f, 1f, true, false, false);
                 MyRenderProxy.DebugDrawLine3D(this.GetWorldMatrix().Translation, worldAABB.Center, Color.Bisque, Color.BlanchedAlmond, true, false);
                 this.GetPrediction(entity, out xd3);
                 MyRenderProxy.DebugDrawAABB(xd3, Color.Crimson, 1f, 1f, true, false, false);
             }
         }
         using (IMyDebugDrawBatchAabb aabb = MyRenderProxy.DebugDrawBatchAABB(this.GetWorldMatrix(), new Color(Color.Cyan, 0.2f), true, false))
         {
             int num = 0;
             foreach (KeyValuePair <MyCellCoord, MyPrecalcJobPhysicsPrefetch> pair in this.m_workTracker)
             {
                 BoundingBoxD xd5;
                 num++;
                 MyCellCoord key = pair.Key;
                 xd5.Min = (Vector3D)(key.CoordInLod << key.Lod);
                 Vector3D *vectordPtr1 = (Vector3D *)ref xd5.Min;
                 vectordPtr1[0] *= 8.0;
                 Vector3D *vectordPtr2 = (Vector3D *)ref xd5.Min;
                 vectordPtr2[0] -= this.m_voxelMap.SizeInMetresHalf;
                 BoundingBoxD *xdPtr1 = (BoundingBoxD *)ref xd5;
                 xdPtr1->Max = xd5.Min + 8f;
                 Color?color = null;
                 aabb.Add(ref xd5, color);
                 if (num > 250)
                 {
                     break;
                 }
             }
         }
     }
 }
Exemple #14
0
        private void buildAttached(int buildID)
        {
            if (last_build_ID == buildID)
            {
                //log("already building for : " + myGrid.DisplayName, "buildAttached()", Logger.severity.TRACE);
                return;
            }
            last_build_ID = buildID;
            log("building for : " + myGrid.DisplayName, "buildAttached()", Logger.severity.TRACE);

            // get all the potentially attached grids
            BoundingBoxD world     = myGrid.WorldAABB;
            MatrixD      scale     = MatrixD.CreateScale(1.1);
            BoundingBoxD searchBox = world.Transform(scale);

            searchBox = searchBox.Translate(world.Center - searchBox.Center);
            foreach (IMyEntity entity in MyAPIGateway.Entities.GetEntitiesInAABB(ref searchBox))
            {
                IMyCubeGrid grid = entity as IMyCubeGrid;
                if (grid == null || grid == myGrid)
                {
                    continue;
                }
                AttachedGrids partner = getFor(grid);
                if (attachedToMe.Contains(partner))
                {
                    //log("already attached: " + grid.DisplayName, "buildAttached()", Logger.severity.TRACE);
                    continue;
                }

                // check each grid for isAttached
                log("checking grid: " + grid.DisplayName, "buildAttached()", Logger.severity.TRACE);
                if (isAttached_piston(partner) || partner.isAttached_piston(this) ||
                    isAttached_motor(partner) || partner.isAttached_motor(this) ||
                    isAttached_connector(partner) || partner.isAttached_connector(this) ||
                    isAttached_landingGear(partner) || partner.isAttached_landingGear(this))
                {
                    continue;
                }
            }
            HashSet <AttachedGrids> copy = new HashSet <AttachedGrids>(attachedToMe);

            foreach (AttachedGrids attached in copy)
            {
                //log("building for attached: " + grid.DisplayName, "buildAttached()", Logger.severity.TRACE);
                attached.buildAttached(buildID);
            }
        }
Exemple #15
0
        public static void DebugDrawChunk(this VRage.Game.Voxels.IMyStorage self, Vector3I start, Vector3I end, Color?c = new Color?())
        {
            if (c == null)
            {
                c = new Color?(Color.Blue);
            }
            BoundingBoxD box = new BoundingBoxD((Vector3D)start, end + 1);

            box.Translate((Vector3D)(-(self.Size * 0.5) - 0.5));
            foreach (MyVoxelBase base2 in from x in MySession.Static.VoxelMaps.Instances
                     where ReferenceEquals(x.Storage, self)
                     select x)
            {
                MyRenderProxy.DebugDrawOBB(new MyOrientedBoundingBoxD(box, base2.WorldMatrix), c.Value, 0.5f, true, true, false);
            }
        }
Exemple #16
0
        public static void DebugDrawChunk(this IMyStorage self, Vector3I start, Vector3I end, Color?c = null)
        {
            if (!c.HasValue)
            {
                c = Color.Blue;
            }

            var vmaps = MySession.Static.VoxelMaps.Instances.Where(x => x.Storage == self);

            var box = new BoundingBoxD(start, end + 1);

            box.Translate(-((Vector3D)self.Size * .5) - .5);

            foreach (var map in vmaps)
            {
                MyRenderProxy.DebugDrawOBB(new MyOrientedBoundingBoxD(box, map.WorldMatrix), c.Value, 0.5f, true, true);
            }
        }
        private void OnEntityPositionCompPositionChanged(MyPositionComponentBase myPositionComponentBase)
        {
            // Update BB position to respective entity position.
            // default translation keeps the relative offset to entity.
            switch (m_triggerType)
            {
            case TriggerType.AABB:
                var translation = Entity.PositionComp.GetPosition() - m_AABB.Matrix.Translation + DefaultTranslation;
                m_AABB.Translate(translation);
                break;

            case TriggerType.Sphere:
                m_boundingSphere.Center = Entity.PositionComp.GetPosition() + DefaultTranslation;
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemple #18
0
        /// <summary>
        /// Calculates amount of volume of a bounding box in voxels.
        /// </summary>
        /// <param name="localAabb">Local bounding box to query for.</param>
        /// <param name="worldMatrix">World matrix of the bounding box.</param>
        /// <returns>Pair of floats where 1st value is Volume amount and 2nd value is ratio of Volume amount to Whole volume.</returns>
        public MyTuple <float, float> GetVoxelContentInBoundingBox_Fast(BoundingBoxD localAabb, MatrixD worldMatrix)
        {
            MatrixD toVoxel = worldMatrix * PositionComp.WorldMatrixNormalizedInv;
            MatrixD toGrid; MatrixD.Invert(ref toVoxel, out toGrid);

            BoundingBoxD transAABB = localAabb.Transform(toVoxel);

            transAABB.Translate(SizeInMetresHalf + StorageMin);
            Vector3I minI = Vector3I.Floor(transAABB.Min);
            Vector3I maxI = Vector3I.Ceiling(transAABB.Max);

            double vol              = localAabb.Volume / MyVoxelConstants.VOXEL_VOLUME_IN_METERS;
            int    K                = Math.Max((MathHelper.Log2Ceiling((int)vol) - MathHelper.Log2Ceiling(100)) / 3, 0);
            float  voxelSizeAtLod   = MyVoxelConstants.VOXEL_SIZE_IN_METRES * (1 << K);
            float  voxelVolumeAtLod = voxelSizeAtLod * voxelSizeAtLod * voxelSizeAtLod;

            minI >>= K;
            maxI >>= K;

            // localAabb.Inflate(1 * voxelSizeAtLod);

            var offset = ((Size >> 1) + StorageMin) >> K;

            m_tempStorage.Resize(maxI - minI + 1);
            Storage.ReadRange(m_tempStorage, MyStorageDataTypeFlags.Content, K, minI, maxI);

            float resultVolume   = 0;
            float resultPercent  = 0;
            int   hitVolumeBoxes = 0;

            MyOrientedBoundingBoxD worldbbox = new MyOrientedBoundingBoxD(localAabb, worldMatrix);

            Vector3I coord, cache;

            for (coord.Z = minI.Z, cache.Z = 0; coord.Z <= maxI.Z; coord.Z++, cache.Z++)
            {
                for (coord.Y = minI.Y, cache.Y = 0; coord.Y <= maxI.Y; coord.Y++, cache.Y++)
                {
                    for (coord.X = minI.X, cache.X = 0; coord.X <= maxI.X; coord.X++, cache.X++)
                    {
                        Vector3D voxelPos = (coord - offset) * voxelSizeAtLod;

                        Vector3D gridPoint;
                        Vector3D.Transform(ref voxelPos, ref toGrid, out gridPoint);

                        ContainmentType cont;
                        //localAabb.Contains(ref gridPoint, out cont);

                        var voxelToWorld = WorldMatrix;
                        voxelToWorld.Translation -= (Vector3D)StorageMin + SizeInMetresHalf;

                        BoundingBoxD voxelBox = new BoundingBoxD();
                        voxelBox.Min = ((Vector3D)(coord) - .5) * voxelSizeAtLod;
                        voxelBox.Max = ((Vector3D)(coord) + .5) * voxelSizeAtLod;

                        MyOrientedBoundingBoxD voxelBbox = new MyOrientedBoundingBoxD(voxelBox, voxelToWorld);

                        cont = worldbbox.Contains(ref voxelBbox);

                        if (cont == ContainmentType.Disjoint)
                        {
                            //VRageRender.MyRenderProxy.DebugDrawOBB(
                            //new MyOrientedBoundingBoxD(voxelBox, voxelToWorld), Color.Red, 0.1f,
                            //true, false);
                            continue;
                        }

                        float content = m_tempStorage.Content(ref cache) / MyVoxelConstants.VOXEL_CONTENT_FULL_FLOAT;



                        //VRageRender.MyRenderProxy.DebugDrawOBB(voxelBbox, Color.Aqua, content,
                        //   true, false);

                        resultVolume  += content * voxelVolumeAtLod;
                        resultPercent += content;
                        hitVolumeBoxes++;
                    }
                }
            }

            resultPercent /= hitVolumeBoxes;
            //float localAABBVol = (float)localAabb.Volume;
            //if (localAABBVol < resultVolume)
            //    resultPercent *= (float)localAabb.Volume / resultVolume;


            //VRageRender.MyRenderProxy.DebugDrawOBB(worldbbox, Color.Yellow, 0,
            //                true, false);
            //VRageRender.MyRenderProxy.DebugWaitForFrameFinish();


            return(new MyTuple <float, float>(resultVolume, resultPercent));
        }
            public override void Draw()
            {
                base.Draw();

                if (MySession.Static == null)
                {
                    return;
                }

                if (m_showVoxelProbe)
                {
                    float halfSize = m_probeSize * .5f;
                    float lodSize  = 1 << m_probeLod;

                    if (m_moveProbe)
                    {
                        m_probePosition = MySector.MainCamera.Position + MySector.MainCamera.ForwardVector * m_probeSize * 3;
                    }

                    BoundingBox  bb;
                    BoundingBoxD bbp; // Box used for drawing and finding the probe and drawing

                    bb  = new BoundingBox(m_probePosition - halfSize, m_probePosition + halfSize);
                    bbp = (BoundingBoxD)bb;

                    m_voxels.Clear();
                    MyGamePruningStructure.GetAllVoxelMapsInBox(ref bbp, m_voxels);

                    MyVoxelBase map      = null;
                    double      distance = double.PositiveInfinity;

                    foreach (var vox in m_voxels)
                    {
                        var d = Vector3D.Distance(vox.WorldMatrix.Translation, m_probePosition);
                        if (d < distance)
                        {
                            distance = d;
                            map      = vox;
                        }
                    }

                    ContainmentType cont = ContainmentType.Disjoint;

                    if (map != null)
                    {
                        map = map.RootVoxel;

                        Vector3 localPos = Vector3.Transform(m_probePosition, map.PositionComp.WorldMatrixInvScaled);
                        localPos += map.SizeInMetresHalf;

                        // Create similar bounding box in storage space
                        bb = new BoundingBox(localPos - halfSize, localPos + halfSize);

                        m_probedVoxel = map;

                        Section("Probing {1}: {0}", map.StorageName, map.GetType().Name);
                        Text("Probe mode: {0}", m_mode);

                        if (m_mode == ProbeMode.Intersect)
                        {
                            Text("Local Pos: {0}", localPos);
                            Text("Probe Size: {0}", m_probeSize);
                            cont = map.Storage.Intersect(ref bb, false);
                            Text("Result: {0}", cont.ToString());
                            bbp = (BoundingBoxD)bb;
                        }
                        else
                        {
                            Vector3I min = Vector3I.Floor(bb.Min / lodSize + .5f);
                            Vector3I max = min + ((int)m_probeSize >> m_probeLod) - 1;

                            bbp = new BoundingBoxD(min << m_probeLod, (max + 1) << m_probeLod);
                            bbp.Translate(new Vector3D(-.5));

                            Text("Probe Size: {0}({1})", (max - min).X + 1, m_probeSize);
                            Text("Probe LOD: {0}", m_probeLod);

                            var requestData           = (MyStorageDataTypeEnum)(int)m_mode;
                            MyVoxelRequestFlags flags = MyVoxelRequestFlags.ContentChecked;

                            m_target.Resize(max - min + 1);
                            m_target.Clear(MyStorageDataTypeEnum.Content, 0);
                            m_target.Clear(MyStorageDataTypeEnum.Material, 0);
                            map.Storage.ReadRange(m_target, (MyStorageDataTypeFlags)(1 << (int)requestData), m_probeLod, ref min, ref max, ref flags);

                            if (requestData == MyStorageDataTypeEnum.Content)
                            {
                                if (flags.HasFlag(MyVoxelRequestFlags.EmptyContent))
                                {
                                    cont = ContainmentType.Disjoint;
                                }
                                else if (flags.HasFlag(MyVoxelRequestFlags.FullContent))
                                {
                                    cont = ContainmentType.Contains;
                                }
                                else
                                {
                                    int val = m_target.ValueWhenAllEqual(requestData);
                                    if (val == -1)
                                    {
                                        cont = ContainmentType.Intersects;
                                    }
                                    else if (val >= MyVoxelConstants.VOXEL_ISO_LEVEL)
                                    {
                                        cont = ContainmentType.Contains;
                                    }
                                    else
                                    {
                                        cont = ContainmentType.Disjoint;
                                    }
                                }

                                DrawContentsInfo(m_target);
                            }
                            else
                            {
                                cont = ContainmentType.Disjoint;
                                DrawMaterialsInfo(m_target);
                            }

                            Text(Color.Yellow, 1.5f, "Voxel Editing:");
                            Text("Value to set (Ctrl+Mousewheel): {0}", m_valueToSet);
                            if (m_probeLod != 0)
                            {
                                Text(Color.Red, "Writing to storage is only possible when probe is set to LOD 0");
                            }
                            else
                            {
                                Text("Use primary mouse button to set.");
                                Text("Position/Extents: {0}/{1}", bbp.Min, bbp.Extents);

                                if (MyInput.Static.IsLeftMousePressed())
                                {
                                    if (requestData == MyStorageDataTypeEnum.Content)
                                    {
                                        m_target.BlockFillContent(Vector3I.Zero, m_target.Size3D - Vector3I.One, m_valueToSet);
                                    }
                                    else
                                    {
                                        m_target.BlockFillMaterial(Vector3I.Zero, m_target.Size3D - Vector3I.One, m_valueToSet);
                                    }

                                    map.Storage.WriteRange(m_target, (MyStorageDataTypeFlags)(1 << (int)requestData), ref min, ref max);
                                }
                            }
                        }
                    }
                    else
                    {
                        Section("No Voxel Found");
                        Text("Probe mode: {0}", m_mode);
                        Text("Probe Size: {0}", m_probeSize);
                    }

                    Color c = ColorForContainment(cont);
                    if (map != null)
                    {
                        bbp = bbp.Translate(-map.SizeInMetresHalf);
                        MyOrientedBoundingBoxD oobb = new MyOrientedBoundingBoxD(bbp, map.WorldMatrix);
                        MyRenderProxy.DebugDrawOBB(oobb, c, 0.5f, true, false);
                    }
                    else
                    {
                        MyRenderProxy.DebugDrawAABB(bbp, c, 0.5f, 1.0f, true);
                    }
                }
            }
Exemple #20
0
            public override void Draw()
            {
                base.Draw();

                if (MySession.Static == null)
                {
                    return;
                }

                var ops = MySession.Static.GetSessionComponent <MyVoxelOperationsSessionComponent>();

                if (ops != null)
                {
                    Text("Cache Enabled: {0}", MyVoxelOperationsSessionComponent.EnableCache);

                    Text("Cache Writing: {0}", ops.ShouldWrite ? "Enabled" : "Disabled");
                    Text("Cache Flushing: {0}", ops.ShouldFlush ? "Enabled" : "Disabled");

                    var storages = ops.QueuedStorages.ToArray();

                    if (storages.Length == 0)
                    {
                        Text(Color.Orange, "No queued storages.");
                    }
                    else
                    {
                        Text(Color.Yellow, 1.2f, "{0} Queued storages:", storages.Length);

                        foreach (var storage in storages)
                        {
                            MyStorageBase.WriteCacheStats stats;
                            storage.GetStats(out stats);
                            Text("Voxel storage {0}:", storage.ToString());
                            Text(Color.White, .9f, "Pending Writes: {0}", stats.QueuedWrites);
                            Text(Color.White, .9f, "Cached Chunks: {0}", stats.CachedChunks);

                            if (DisplayDetails)
                            {
                                foreach (var chunk in stats.Chunks)
                                {
                                    var ck = chunk.Value;
                                    Text(Color.Wheat, .9f, "Chunk {0}: {1} hits; pending {2}", chunk.Key, ck.HitCount, ck.Dirty);
                                }
                            }

                            if (DebugDraw)
                            {
                                var vmap = MySession.Static.VoxelMaps.Instances.FirstOrDefault(x => x.Storage == storage);

                                if (vmap == null)
                                {
                                    continue;
                                }

                                foreach (var chunk in stats.Chunks)
                                {
                                    var box = new BoundingBoxD(chunk.Key << MyStorageBase.VoxelChunk.SizeBits, ((chunk.Key + 1) << MyStorageBase.VoxelChunk.SizeBits));

                                    box.Translate(-((Vector3D)storage.Size * .5) - .5);

                                    MyRenderProxy.DebugDrawOBB(new MyOrientedBoundingBoxD(box, vmap.WorldMatrix), GetColorForDirty(chunk.Value.Dirty), 0.1f, true, true);
                                }
                            }
                        }
                    }
                }
            }
        public static void DebugDrawChunk(this IMyStorage self, Vector3I start, Vector3I end, Color? c = null)
        {
            if (!c.HasValue) c = Color.Blue;

            var vmaps = MySession.Static.VoxelMaps.Instances.Where(x => x.Storage == self);

            var box = new BoundingBoxD(start, end + 1);

            box.Translate(-((Vector3D)self.Size * .5) - .5);

            foreach (var map in vmaps)
            {
                MyRenderProxy.DebugDrawOBB(new MyOrientedBoundingBoxD(box, map.WorldMatrix), c.Value, 0.5f, true, true);
            }
        }
        public override void Draw()
        {
            if (MySector.MainCamera == null)
                return;

            base.Draw();

            //Vector3D compassPosition = MySector.MainCamera.Position + MySector.MainCamera.ForwardVector + MySector.MainCamera.LeftVector / 1.5f - MySector.MainCamera.UpVector / 2;
            //VRageRender.MyRenderProxy.DebugDrawAxis(MatrixD.CreateTranslation(compassPosition), 0.1f, false);
            //VRageRender.MyRenderProxy.DebugDrawSphere(compassPosition, 0.02f, Color.White, 0.4f, false);

            if (m_voxelMap != null)
            {
                VRageRender.MyRenderProxy.DebugDrawAxis(m_voxelMap.WorldMatrix, 100f, false);
            }

            if (MyDebugDrawSettings.DEBUG_DRAW_FRACTURED_PIECES)
            {
                foreach (var entity in MyEntities.GetEntities())
                {
                    var piece = entity as MyFracturedPiece;
                    if (piece != null)
                    {
                        MyPhysicsDebugDraw.DebugDrawBreakable(piece.Physics.BreakableBody, piece.Physics.ClusterToWorld(Vector3D.Zero));
                    }
                }
            }

            return;

            VRageRender.MyRenderProxy.DebugDrawAxis(m_teapotMatrix, 100f, false);


            var viewVectors = new Matrix[s_viewVectorData.Length];
            for (int i = 0; i < s_viewVectorData.Length; i++)
            {
                Matrix mm = Matrix.Identity;
                string[] sp = s_viewVectorData[i].Split(',');
                mm.M11 = Convert.ToSingle(sp[0]);
                mm.M12 = Convert.ToSingle(sp[1]);
                mm.M13 = Convert.ToSingle(sp[2]);
                mm.M21 = Convert.ToSingle(sp[3]);
                mm.M22 = Convert.ToSingle(sp[4]);
                mm.M23 = Convert.ToSingle(sp[5]);
                mm.M31 = Convert.ToSingle(sp[6]);
                mm.M32 = Convert.ToSingle(sp[7]);
                mm.M33 = Convert.ToSingle(sp[8]);
                mm = Matrix.Normalize(mm);
                mm = mm * Matrix.CreateRotationX(MathHelper.PiOver2);
                mm.Up = -mm.Up;
                viewVectors[i] = mm;
            }
            
            
            

            VRageRender.MyRenderProxy.DebugDrawSphere(m_teapotMatrix.Translation, 1.5f, Color.White, 1, false);

            for (int i = 0; i < viewVectors.Length; i++)
            {
                Matrix am = viewVectors[i];
                am = am * m_teapotMatrix.GetOrientation();
                am.Translation = m_teapotMatrix.Translation - 20 * am.Forward;
                VRageRender.MyRenderProxy.DebugDrawAxis(am, 2, false);
                VRageRender.MyRenderProxy.DebugDrawText3D(am.Translation, i.ToString(), Color.White, 0.5f, false);
                //VRageRender.MyRenderProxy.DebugDrawLine3D(m_teapotMatrix.Translation, m_teapotMatrix.Translation + 20 * s_viewVectors[i].Forward, Color.White, Color.Wheat, false);
            }


            Matrix[] viewVectors2 = new Matrix[181];
            Vector3D genPos = m_teapotMatrix.Translation + m_teapotMatrix.Right * 80;
            VRageRender.MyRenderProxy.DebugDrawSphere(genPos, 1.5f, Color.Yellow, 1, false);














            //for (int i = -N; i <= N; ++i)
            //{
            //    for (int j = -N + Math.Abs(i); j <= N - Math.Abs(i); ++j)
            //    {

            //        float x = (i + j) / (float)N;
            //        float y = (j - i) / (float)N;
            //        float angle = 90.0f - Math.Max(Math.Abs(x), Math.Abs(y)) * 90.0f;
            //        float alpha = x == 0.0f && y == 0.0f ? 0.0f : (float)Math.Atan2(y, x) / MathHelper.Pi * 180.0f;

            //        Matrix cameraToWorld = Matrix.CreateRotationX(MathHelper.PiOver2) * Matrix.CreateRotationX(MathHelper.ToRadians(-angle));
            //        Matrix worldToCamera = Matrix.Invert(cameraToWorld);

            //        //box3f b;
            //        //b = b.enlarge((worldToCamera * vec4f(-1.0, -1.0, zmin, 1.0)).xyz());
            //        //b = b.enlarge((worldToCamera * vec4f(+1.0, -1.0, zmin, 1.0)).xyz());
            //        //b = b.enlarge((worldToCamera * vec4f(-1.0, +1.0, zmin, 1.0)).xyz());
            //        //b = b.enlarge((worldToCamera * vec4f(+1.0, +1.0, zmin, 1.0)).xyz());
            //        //b = b.enlarge((worldToCamera * vec4f(-1.0, -1.0, zmax, 1.0)).xyz());
            //        //b = b.enlarge((worldToCamera * vec4f(+1.0, -1.0, zmax, 1.0)).xyz());
            //        //b = b.enlarge((worldToCamera * vec4f(-1.0, +1.0, zmax, 1.0)).xyz());
            //        //b = b.enlarge((worldToCamera * vec4f(+1.0, +1.0, zmax, 1.0)).xyz());
            //        //mat4f c2s = mat4f::orthoProjection(b.xmax, b.xmin, b.ymax, b.ymin, -2.0 * b.zmax, -2.0 * b.zmin);
            //        Matrix w2s = /*c2s */ worldToCamera * Matrix.CreateRotationZ(MathHelper.ToRadians(-90.0f - alpha));
            //        //vec3f dir = ((mat4f::rotatez(90.0 + alpha) * cameraToWorld) * vec4f(0.0, 0.0, 1.0, 0.0)).xyz();

            //        Matrix am = w2s;
            //        am.Translation = genPos - 20 * am.Forward;

            //        int view = i * (1 - Math.Abs(i)) + j + 2 * N * i + N * (N + 1);

            //        VRageRender.MyRenderProxy.DebugDrawAxis(am, 2, false);
            //        VRageRender.MyRenderProxy.DebugDrawText3D(am.Translation, view.ToString(), Color.White, 0.5f, false);
            //    }
            //}
















            //float angleStepX = MathHelper.Pi / (2 * N);
            //float angleStepY = MathHelper.TwoPi / (4 * N);
            //int k = 0;
            //for (float x = 0; x < MathHelper.Pi; x += angleStepX)
            //{
            //    for (float y = 0; y < MathHelper.TwoPi; y += angleStepY)
            //    {
            //        Matrix am = Matrix.CreateRotationX(x) * Matrix.CreateRotationZ(y);
            //        am.Translation = genPos - 20 * am.Forward;

            //        if ((am.Translation - genPos).Y >= 0)
            //        {
            //            VRageRender.MyRenderProxy.DebugDrawAxis(am, 2, false);
            //            VRageRender.MyRenderProxy.DebugDrawText3D(am.Translation, k.ToString(), Color.White, 0.5f, false);
            //            k++;
            //        }
            //    }
            //}

            //for (int i = 0; i < viewVectors2.Length; i++)
            //{
            //    viewVectors[i] = Matrix.Identity;
            //}


            
            //for (int i = 0; i < viewVectors2.Length; i++)
            //{
            //    Matrix am = viewVectors[i];
            //    am = am * m_teapotMatrix.GetOrientation();
            //    am.Translation = genPos - 20 * am.Forward;
            //    VRageRender.MyRenderProxy.DebugDrawAxis(am, 2, false);
            //    VRageRender.MyRenderProxy.DebugDrawText3D(am.Translation, i.ToString(), Color.White, 0.5f, false);
            //    //VRageRender.MyRenderProxy.DebugDrawLine3D(m_teapotMatrix.Translation, m_teapotMatrix.Translation + 20 * s_viewVectors[i].Forward, Color.White, Color.Wheat, false);
            //}






            Vector3I vv;
            Vector3 rr;
            Vector3 cdir = Vector3.Normalize((Vector3)(m_teapotMatrix.Translation - MySector.MainCamera.Position));
            Matrix invT = Matrix.Invert(m_teapotMatrix.GetOrientation());
            
            cdir = Vector3.TransformNormal(cdir, invT);
            cdir.Z *= -1;
            //cdir = Vector3.TransformNormal(cdir, Matrix.CreateRotationX(MathHelper.Pi));
            findViews(0, cdir, out vv, out rr);

            VRageRender.MyRenderProxy.DebugDrawText3D(m_teapotMatrix.Translation, vv.ToString() + " " + rr.ToString(), Color.White, 0.5f, false);


            BoundingBoxD bbox = new BoundingBoxD(-new Vector3D(20), new Vector3D(20));
            bbox.Translate(new Vector3D(0, 0, -30));
          //  VRageRender.MyRenderProxy.DebugDrawAABB(bbox, Color.White, 1, 1, false);


            Matrix m = MatrixD.Identity;
            m.Translation = MySector.MainCamera.Position + MySector.MainCamera.ForwardVector * 50;

          //  VRageRender.MyRenderProxy.DebugDrawAxis(m, 100f, false);

            Vector3I vvLight;
            Vector3 rrLight;
            Vector3 ldir = -MySector.DirectionToSunNormalized;
            ldir.Z *= -1;
            //ldir.Y *= -1;
            findViews(0, ldir, out vvLight, out rrLight);

         //   VRageRender.MyRenderProxy.DebugDrawText3D(m_teapotMatrix.Translation, "L: " + vvLight.ToString() + " " + rrLight.ToString(), Color.White, 0.5f, false);
            VRageRender.MyRenderProxy.DebugDrawLine3D(m_teapotMatrix.Translation, m_teapotMatrix.Translation + MySector.DirectionToSunNormalized * 100, Color.Yellow, Color.Yellow, false);


            //VRageRender.MyRenderProxy.SetUvIndices(m_spawnedTree, vv, rr, vvLight, rrLight);
            //VRageRender.MyRenderProxy.SetImpostorMatrices(s_viewVectors);

            return;
            var mat = "WeaponLaser";
            float thickness = 0.05f;

            float spacing = 5;
            int count = 100;
            Vector4 color = Color.White.ToVector4();

            //Vector3 center = MySector.MainCamera.Position + new Vector3(0, -10, 0);
            Vector3 center = Vector3.Zero;
            Vector3 startPos = center - new Vector3(spacing * count / 2, 0, 0);

            for (int i = 0; i < count; i++)
            {
                Vector3D lineStart = startPos + new Vector3(spacing * i, 0, -spacing * count / 2);
                Vector3D lineEnd = startPos + new Vector3(spacing * i, 0, spacing * count / 2);

                Vector3D closestPoint = Vector3.Zero;
                Vector3D campos = MySector.MainCamera.Position;
                closestPoint = MyUtils.GetClosestPointOnLine(ref lineStart, ref lineEnd, ref campos);
                var distance = MySector.MainCamera.GetDistanceFromPoint(closestPoint);

                var lineThickness = thickness * MathHelper.Clamp(distance, 0.1f, 10);

                MySimpleObjectDraw.DrawLine(lineStart, lineEnd, mat, ref color, (float)lineThickness );
            }

        }
Exemple #23
0
        private Vector3D?FindSuitableJumpLocation(Vector3D desiredLocation)
        {
            BoundingBoxD shipBBox = GetAggregateBBox();

            // 1 Km distante to other objects to prevent spawning in bases
            shipBBox.Inflate(1000f);

            BoundingBoxD regionBBox = shipBBox.GetInflated(shipBBox.HalfExtents * 10);

            regionBBox.Translate(desiredLocation - regionBBox.Center);


            MyProceduralWorldGenerator.Static.OverlapAllPlanetSeedsInSphere(new BoundingSphereD(regionBBox.Center, regionBBox.HalfExtents.AbsMax()), m_objectsInRange);
            Vector3D currentSearchPosition = desiredLocation;

            foreach (var planet in m_objectsInRange)
            {
                if (planet.BoundingVolume.Contains(currentSearchPosition) != ContainmentType.Disjoint)
                {
                    Vector3D v = currentSearchPosition - planet.BoundingVolume.Center;
                    v.Normalize();
                    v *= planet.BoundingVolume.HalfExtents * 1.5;
                    currentSearchPosition = planet.BoundingVolume.Center + v;
                    break;
                }
            }
            m_objectsInRange.Clear();

            MyProceduralWorldGenerator.Static.OverlapAllAsteroidSeedsInSphere(new BoundingSphereD(regionBBox.Center, regionBBox.HalfExtents.AbsMax()), m_objectsInRange);
            foreach (var asteroid in m_objectsInRange)
            {
                m_obstaclesInRange.Add(asteroid.BoundingVolume);
            }
            m_objectsInRange.Clear();

            MyGamePruningStructure.GetTopMostEntitiesInBox(ref regionBBox, m_entitiesInRange);

            // Inflate the obstacles so we only need to check the center of the ship for collisions
            foreach (var entity in m_entitiesInRange)
            {
                if (!(entity is MyPlanet))
                {
                    m_obstaclesInRange.Add(entity.PositionComp.WorldAABB.GetInflated(shipBBox.HalfExtents));
                }
            }

            int maxStepCount = 10;
            int stepCount    = 0;

            // When we collide with an obsticle, we add it here
            BoundingBoxD?aggregateCollidedObstacles = null;
            bool         obstructed = false;
            bool         found      = false;

            while (stepCount < maxStepCount)
            {
                stepCount++;
                obstructed = false;
                foreach (var obstacle in m_obstaclesInRange)
                {
                    var contains = obstacle.Contains(currentSearchPosition);
                    if (contains == ContainmentType.Contains ||
                        contains == ContainmentType.Intersects)
                    {
                        if (!aggregateCollidedObstacles.HasValue)
                        {
                            aggregateCollidedObstacles = obstacle;
                        }
                        aggregateCollidedObstacles = aggregateCollidedObstacles.Value.Include(obstacle);
                        aggregateCollidedObstacles = aggregateCollidedObstacles.Value.Inflate(1.0);
                        currentSearchPosition      = ClosestPointOnBounds(aggregateCollidedObstacles.Value, currentSearchPosition);
                        obstructed = true;
                        break;
                    }
                }

                if (!obstructed)
                {
                    // No obstacle found, return current search position
                    found = true;
                    break;
                }
            }

            m_obstaclesInRange.Clear();
            m_entitiesInRange.Clear();
            m_objectsInRange.Clear();

            if (found)
            {
                return(currentSearchPosition);
            }
            else
            {
                return(null);
            }
        }
Exemple #24
0
        public static unsafe void DisableItemsInAabb(this MyEnvironmentSector sector, ref BoundingBoxD aabb)
        {
            if (sector.DataView == null)
                return;

            aabb.Translate(-sector.SectorCenter);

            for (int sectorInd = 0; sectorInd < sector.DataView.LogicalSectors.Count; sectorInd++)
            {
                var logicalSector = sector.DataView.LogicalSectors[sectorInd];
                var logicalItems = logicalSector.Items;
                var cnt = logicalItems.Count;

                fixed (ItemInfo* items = logicalItems.GetInternalArray())
                    for (int i = 0; i < cnt; ++i)
                    {
                        if (items[i].DefinitionIndex >= 0 && aabb.Contains(items[i].Position) != ContainmentType.Disjoint)
                            logicalSector.EnableItem(i, false);
                    }
            }
        }
Exemple #25
0
 internal void UpdateAfterSimulation10()
 {
     this.UpdateRigidBodyShape();
     if (this.m_voxelMap.Storage != null)
     {
         foreach (IMyEntity entity in this.m_nearbyEntities)
         {
             if (entity != null)
             {
                 bool          flag    = false;
                 MyPhysicsBody physics = entity.Physics as MyPhysicsBody;
                 if (((physics != null) && (physics.RigidBody != null)) && ((physics.RigidBody.Layer == 0x17) || (physics.RigidBody.Layer == 10)))
                 {
                     flag = true;
                 }
                 if ((((entity is MyCubeGrid) || flag) && !entity.MarkedForClose) && (entity.Physics != null))
                 {
                     BoundingBoxD xd;
                     this.GetPrediction(entity, out xd);
                     if (xd.Intersects(this.m_voxelMap.PositionComp.WorldAABB))
                     {
                         Vector3I           vectori;
                         Vector3I           vectori2;
                         HkUniformGridShape shape;
                         int num1;
                         if (flag || !UseLod1VoxelPhysics)
                         {
                             num1 = 0;
                         }
                         else
                         {
                             num1 = 1;
                         }
                         int          lod  = num1;
                         int          num2 = 1 << (lod & 0x1f);
                         BoundingBoxD xd2  = xd.TransformFast(this.m_voxelMap.PositionComp.WorldMatrixInvScaled);
                         xd2.Translate(this.m_voxelMap.SizeInMetresHalf);
                         Vector3 max = (Vector3)xd2.Max;
                         Vector3 min = (Vector3)xd2.Min;
                         this.ClampVoxelCoords(ref min, ref max, out vectori, out vectori2);
                         vectori  = vectori >> lod;
                         vectori2 = vectori2 >> lod;
                         int size = ((vectori2 - vectori) + 1).Size;
                         if (size >= m_cellsToGenerateBuffer.Length)
                         {
                             m_cellsToGenerateBuffer = new Vector3I[MathHelper.GetNearestBiggerPowerOfTwo(size)];
                         }
                         if (this.GetShape(lod, out shape))
                         {
                             if (shape.Base.IsZero)
                             {
                                 MyAnalyticsHelper.ReportBug("Null voxel shape", "SE-7366", true, @"E:\Repo1\Sources\Sandbox.Game\Engine\Voxels\MyVoxelPhysicsBody.cs", 680);
                             }
                             else
                             {
                                 int requiredCellsCount = shape.GetMissingCellsInRange(ref vectori, ref vectori2, m_cellsToGenerateBuffer);
                                 if (requiredCellsCount != 0)
                                 {
                                     BoundingBoxI box = new BoundingBoxI((vectori * 8) * num2, (Vector3I)(((vectori2 + 1) * 8) * num2));
                                     box.Translate(this.m_voxelMap.StorageMin);
                                     if ((requiredCellsCount > 0) && (this.m_voxelMap.Storage.Intersect(ref box, lod, true) != ContainmentType.Intersects))
                                     {
                                         this.SetEmptyShapes(lod, ref shape, requiredCellsCount);
                                     }
                                     else
                                     {
                                         for (int i = 0; i < requiredCellsCount; i++)
                                         {
                                             this.StartPrecalcJobPhysicsIfNeeded(lod, i);
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         this.ScheduleBatchJobs();
         if (this.m_bodiesInitialized)
         {
             this.CheckAndDiscardShapes();
         }
     }
 }
 public static void RenderCellCoordToWorldAABB(Vector3D referenceVoxelMapPosition, ref MyCellCoord renderCell, out BoundingBoxD worldAABB)
 {
     RenderCellCoordToLocalAABB(ref renderCell, out worldAABB);
     worldAABB = worldAABB.Translate(referenceVoxelMapPosition);
 }
Exemple #27
0
        private unsafe void AddVoxelMesh(MyVoxelBase voxelBase, IMyStorage storage, Dictionary <Vector3I, MyIsoMesh> cache, float border, Vector3D originPosition, MyOrientedBoundingBoxD obb, List <BoundingBoxD> bbList)
        {
            Vector3I vectori3;
            Vector3I vectori4;
            bool     flag = cache != null;

            if (flag)
            {
                this.CheckCacheValidity();
            }
            Vector3D *vectordPtr1 = (Vector3D *)ref obb.HalfExtent;

            vectordPtr1[0] += new Vector3D((double)border, 0.0, (double)border);
            BoundingBoxD  aABB   = obb.GetAABB();
            int           num    = (int)Math.Round((double)(aABB.HalfExtents.Max() * 2.0));
            BoundingBoxD *xdPtr1 = (BoundingBoxD *)ref aABB;

            xdPtr1 = (BoundingBoxD *)new BoundingBoxD(aABB.Min, aABB.Min + num);
            ((BoundingBoxD *)ref aABB).Translate(obb.Center - aABB.Center);
            bbList.Add(new BoundingBoxD(aABB.Min, aABB.Max));
            aABB = aABB.TransformFast(voxelBase.PositionComp.WorldMatrixInvScaled);
            aABB.Translate(voxelBase.SizeInMetresHalf);
            Vector3I voxelCoord = Vector3I.Round(aABB.Min);
            Vector3I vectori2   = (Vector3I)(voxelCoord + num);

            MyVoxelCoordSystems.VoxelCoordToGeometryCellCoord(ref voxelCoord, out vectori3);
            MyVoxelCoordSystems.VoxelCoordToGeometryCellCoord(ref vectori2, out vectori4);
            MyOrientedBoundingBoxD xd2 = obb;

            xd2.Transform(voxelBase.PositionComp.WorldMatrixInvScaled);
            Vector3D *vectordPtr2 = (Vector3D *)ref xd2.Center;

            vectordPtr2[0] += voxelBase.SizeInMetresHalf;
            Vector3I_RangeIterator iterator = new Vector3I_RangeIterator(ref vectori3, ref vectori4);
            MyCellCoord            coord    = new MyCellCoord {
                Lod = 0
            };
            int     num2        = 0;
            Vector3 offset      = (Vector3)(originPosition - voxelBase.PositionLeftBottomCorner);
            Vector3 up          = -Vector3.Normalize(MyGravityProviderSystem.CalculateTotalGravityInPoint(originPosition));
            Matrix  rotation    = Matrix.CreateFromQuaternion(Quaternion.Inverse(Quaternion.CreateFromForwardUp(Vector3.CalculatePerpendicularVector(up), up)));
            Matrix  orientation = (Matrix)voxelBase.PositionComp.WorldMatrix.GetOrientation();

            while (iterator.IsValid())
            {
                BoundingBox box;
                MyIsoMesh   mesh;
                if (flag && cache.TryGetValue(iterator.Current, out mesh))
                {
                    if (mesh != null)
                    {
                        this.AddMeshTriangles(mesh, offset, rotation, orientation);
                    }
                    iterator.MoveNext();
                    continue;
                }
                coord.CoordInLod = iterator.Current;
                MyVoxelCoordSystems.GeometryCellCoordToLocalAABB(ref coord.CoordInLod, out box);
                if (!xd2.Intersects(ref box))
                {
                    num2++;
                    iterator.MoveNext();
                }
                else
                {
                    BoundingBoxD item = new BoundingBoxD(box.Min, box.Max).Translate(-voxelBase.SizeInMetresHalf);
                    bbList.Add(item);
                    Vector3I  lodVoxelMin = (coord.CoordInLod * 8) - 1;
                    MyIsoMesh mesh2       = MyPrecalcComponent.IsoMesher.Precalc(storage, 0, lodVoxelMin, (Vector3I)(((lodVoxelMin + 8) + 1) + 1), MyStorageDataTypeFlags.Content, 0);
                    if (flag)
                    {
                        cache[iterator.Current] = mesh2;
                    }
                    if (mesh2 != null)
                    {
                        this.AddMeshTriangles(mesh2, offset, rotation, orientation);
                    }
                    iterator.MoveNext();
                }
            }
        }
        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));
                    }
                }
            }
        }
        /// <summary>
        /// Writes all surrounding blocks around the given one with the given size.
        /// </summary>
        private void GetSurroundingBlocksFromStaticGrids(MySlimBlock block, MyCubeSize cubeSizeEnum, HashSet <MySlimBlock> outBlocks)
        {
            outBlocks.Clear();

            BoundingBoxD aabbForNeighbors = new BoundingBoxD(block.Min * block.CubeGrid.GridSize, block.Max * block.CubeGrid.GridSize);
            BoundingBoxD aabb             = new BoundingBoxD(block.Min * block.CubeGrid.GridSize - block.CubeGrid.GridSize / 2, block.Max * block.CubeGrid.GridSize + block.CubeGrid.GridSize / 2);

            if (block.FatBlock != null)
            {
                var aabbCenter = aabb.Center;
                aabb = (BoundingBoxD)block.FatBlock.Model.BoundingBox;
                Matrix m;
                block.FatBlock.Orientation.GetMatrix(out m);
                aabb = aabb.TransformFast(m);
                aabb.Translate(aabbCenter);
            }

            aabb.Inflate(0.125);
            var aabbWorld = aabb.TransformFast(block.CubeGrid.WorldMatrix);

            List <MyEntity> boxOverlapList = new List <MyEntity>();

            MyEntities.GetElementsInBox(ref aabbWorld, boxOverlapList);

            for (int i = 0; i < boxOverlapList.Count; i++)
            {
                var cubeGrid = boxOverlapList[i] as MyCubeGrid;
                if (cubeGrid != null)
                {
                    if (cubeGrid.IsStatic && cubeGrid != block.CubeGrid && cubeGrid.EnableSmallToLargeConnections && cubeGrid.SmallToLargeConnectionsInitialized &&
                        cubeGrid.GridSizeEnum == cubeSizeEnum)
                    {
                        Debug.Assert(m_tmpSlimBlocksList.Count == 0);
                        m_tmpSlimBlocksList.Clear();
                        cubeGrid.GetBlocksIntersectingOBB(aabb, block.CubeGrid.WorldMatrix, m_tmpSlimBlocksList);

                        CheckNeighborBlocks(block, aabbForNeighbors, cubeGrid, m_tmpSlimBlocksList);

                        foreach (var slimBlock in m_tmpSlimBlocksList)
                        {
                            if (slimBlock.FatBlock != null)
                            {
                                if (slimBlock.FatBlock is MyFracturedBlock)
                                {
                                    continue;
                                }

                                if (slimBlock.FatBlock.Components.Has <MyFractureComponentBase>())
                                {
                                    continue;
                                }

                                if (slimBlock.FatBlock is MyCompoundCubeBlock)
                                {
                                    foreach (var blockInCompound in (slimBlock.FatBlock as MyCompoundCubeBlock).GetBlocks())
                                    {
                                        if (!(blockInCompound.FatBlock.Components.Has <MyFractureComponentBase>()))
                                        {
                                            outBlocks.Add(blockInCompound);
                                        }
                                    }
                                }
                                else
                                {
                                    outBlocks.Add(slimBlock);
                                }
                            }
                            else
                            {
                                outBlocks.Add(slimBlock);
                            }
                        }

                        m_tmpSlimBlocksList.Clear();
                    }
                }
            }

            boxOverlapList.Clear();
        }
Exemple #30
0
        static void ProcessDebugMessages()
        {
            var linesBatch        = MyLinesRenderer.CreateBatch();
            var noDepthLinesBatch = MyLinesRenderer.CreateBatch();

            noDepthLinesBatch.IgnoreDepth = true;
            var lines2D = MyLinesRenderer.CreateBatch();

            lines2D.IgnoreDepth = true;


            while (m_debugDrawMessages.Count > 0)
            {
                IMyRenderMessage debugDrawMessage = m_debugDrawMessages.Dequeue();

                MyRenderMessageEnum messageType = debugDrawMessage.MessageType;

                switch (messageType)
                {
                case MyRenderMessageEnum.DebugDrawLine3D:
                {
                    MyRenderMessageDebugDrawLine3D message = (MyRenderMessageDebugDrawLine3D)debugDrawMessage;

                    if (message.DepthRead)
                    {
                        linesBatch.Add(message.PointFrom - MyEnvironment.CameraPosition, message.PointTo - MyEnvironment.CameraPosition, message.ColorFrom, message.ColorTo);
                    }
                    else
                    {
                        noDepthLinesBatch.Add(message.PointFrom - MyEnvironment.CameraPosition, message.PointTo - MyEnvironment.CameraPosition, message.ColorFrom, message.ColorTo);
                    }

                    break;
                }

                case MyRenderMessageEnum.DebugDrawLine2D:
                {
                    MyRenderMessageDebugDrawLine2D message = (MyRenderMessageDebugDrawLine2D)debugDrawMessage;

                    var matrix = message.Projection ?? Matrix.CreateOrthographicOffCenter(0, MyRender11.ViewportResolution.X, MyRender11.ViewportResolution.Y, 0, 0, -1);

                    if (!lines2D.CustomViewProjection.HasValue || (lines2D.CustomViewProjection.HasValue && lines2D.CustomViewProjection.Value != matrix))
                    {
                        lines2D.Commit();
                        lines2D                      = MyLinesRenderer.CreateBatch();
                        lines2D.IgnoreDepth          = true;
                        lines2D.CustomViewProjection = matrix;
                    }

                    var p0 = new Vector3(message.PointFrom.X, message.PointFrom.Y, 0);
                    var p1 = new Vector3(message.PointTo.X, message.PointTo.Y, 0);
                    lines2D.Add(p0, p1, message.ColorFrom, message.ColorTo);

                    break;
                }

                case MyRenderMessageEnum.DebugDrawPoint:
                {
                    MyRenderMessageDebugDrawPoint message = (MyRenderMessageDebugDrawPoint)debugDrawMessage;

                    var batch = message.DepthRead ? linesBatch : noDepthLinesBatch;

                    var scale = 0.125f;

                    var borderDepth = MyRender11.UseComplementaryDepthBuffer ? 0.0f : 1.0f;
                    borderDepth = message.ClipDistance.HasValue ? Vector3.Transform(new Vector3(0, 0, -message.ClipDistance.Value), MyEnvironment.Projection).Z : borderDepth;

                    var clipPosition = Vector3D.Transform(message.Position, MyEnvironment.ViewProjectionAt0);
                    clipPosition.X = clipPosition.X * 0.5f + 0.5f;
                    clipPosition.Y = clipPosition.Y * -0.5f + 0.5f;

                    //Debug.Assert(MyRender11.UseComplementaryDepthBuffer);

                    Vector3 position = (Vector3)(message.Position - MyEnvironment.CameraPosition);

                    bool drawCondition =
                        MyRender11.UseComplementaryDepthBuffer
                                ? clipPosition.Z > borderDepth && clipPosition.Z < 1
                                : clipPosition.Z < borderDepth && clipPosition.Z > 0;

                    if (drawCondition)
                    {
                        batch.Add(position + Vector3.UnitX * scale, position - Vector3.UnitX * scale, message.Color);
                        batch.Add(position + Vector3.UnitY * scale, position - Vector3.UnitY * scale, message.Color);
                        batch.Add(position + Vector3.UnitZ * scale, position - Vector3.UnitZ * scale, message.Color);
                    }

                    break;
                }

                case MyRenderMessageEnum.DebugDrawSphere:
                {
                    MyRenderMessageDebugDrawSphere message = (MyRenderMessageDebugDrawSphere)debugDrawMessage;

                    var borderDepth = MyRender11.UseComplementaryDepthBuffer ? 0.0f : 1.0f;
                    borderDepth = message.ClipDistance.HasValue ? Vector3.Transform(new Vector3(0, 0, -message.ClipDistance.Value), MyEnvironment.Projection).Z : borderDepth;

                    Vector3D position = message.Position - MyEnvironment.CameraPosition;

                    var clipPosition = Vector3D.Transform(position, MyEnvironment.ViewProjectionAt0);
                    clipPosition.X = clipPosition.X * 0.5f + 0.5f;
                    clipPosition.Y = clipPosition.Y * -0.5f + 0.5f;

                    bool drawCondition =
                        MyRender11.UseComplementaryDepthBuffer
                                ? clipPosition.Z > borderDepth && clipPosition.Z < 1
                                : clipPosition.Z < borderDepth && clipPosition.Z > 0;

                    if (drawCondition)
                    {
                        var batch = message.DepthRead ? linesBatch : noDepthLinesBatch;

                        batch.AddSphereRing(new BoundingSphere(position, message.Radius), message.Color, Matrix.Identity);
                        batch.AddSphereRing(new BoundingSphere(position, message.Radius), message.Color, Matrix.CreateRotationX(MathHelper.PiOver2));
                        batch.AddSphereRing(new BoundingSphere(position, message.Radius), message.Color, Matrix.CreateRotationZ(MathHelper.PiOver2));
                    }

                    break;
                }

                case MyRenderMessageEnum.DebugDrawAABB:
                {
                    MyRenderMessageDebugDrawAABB message = (MyRenderMessageDebugDrawAABB)debugDrawMessage;

                    BoundingBoxD aabb = message.AABB;
                    aabb.Translate(-MyEnvironment.CameraPosition);

                    if (message.DepthRead)
                    {
                        linesBatch.AddBoundingBox((BoundingBox)aabb, message.Color);
                    }
                    else
                    {
                        noDepthLinesBatch.AddBoundingBox((BoundingBox)aabb, message.Color);
                    }

                    break;
                }

                case MyRenderMessageEnum.DebugDrawCone:
                {
                    MyRenderMessageDebugDrawCone message = (MyRenderMessageDebugDrawCone)debugDrawMessage;

                    var batch = message.DepthRead ? linesBatch : noDepthLinesBatch;

                    var axis = message.DirectionVector;
                    axis.Normalize();

                    var apex = message.Translation + message.DirectionVector;

                    var steps    = 32;
                    var stepsRcp = (float)(Math.PI * 2 / steps);
                    for (int i = 0; i < 32; i++)
                    {
                        float a0 = i * stepsRcp;
                        float a1 = (i + 1) * stepsRcp;

                        var A = message.Translation + Vector3D.Transform(message.BaseVector, MatrixD.CreateFromAxisAngle(axis, a0)) - MyEnvironment.CameraPosition;
                        var B = message.Translation + Vector3D.Transform(message.BaseVector, MatrixD.CreateFromAxisAngle(axis, a1)) - MyEnvironment.CameraPosition;

                        batch.Add(A, B, message.Color);
                        batch.Add(A, apex, message.Color);
                    }

                    break;
                }

                case MyRenderMessageEnum.DebugDrawAxis:
                {
                    MyRenderMessageDebugDrawAxis message = (MyRenderMessageDebugDrawAxis)debugDrawMessage;

                    var batch = message.DepthRead ? linesBatch : noDepthLinesBatch;

                    Vector3 position = message.Matrix.Translation - MyEnvironment.CameraPosition;

                    batch.Add(position, position + message.Matrix.Right * message.AxisLength, Color.Red);
                    batch.Add(position, position + message.Matrix.Up * message.AxisLength, Color.Green);
                    batch.Add(position, position + message.Matrix.Forward * message.AxisLength, Color.Blue);

                    break;
                }

                case MyRenderMessageEnum.DebugDrawOBB:
                {
                    MyRenderMessageDebugDrawOBB message = (MyRenderMessageDebugDrawOBB)debugDrawMessage;

                    Vector3D [] cornersD = new Vector3D[8];
                    MatrixD     matrix   = (MatrixD)message.Matrix;
                    new MyOrientedBoundingBoxD(matrix).GetCorners(cornersD, 0);

                    Vector3[] corners = new Vector3[8];
                    for (int i = 0; i < 8; i++)
                    {
                        corners[i] = (Vector3)(cornersD[i] - MyEnvironment.CameraPosition);
                    }

                    if (message.DepthRead)
                    {
                        linesBatch.Add6FacedConvex(corners, message.Color);
                    }
                    else
                    {
                        noDepthLinesBatch.Add6FacedConvex(corners, message.Color);
                    }

                    MyPrimitivesRenderer.Draw6FacedConvex(corners, message.Color, message.Alpha);

                    break;
                }

                case MyRenderMessageEnum.DebugDrawCylinder:
                {
                    MyRenderMessageDebugDrawCylinder message = (MyRenderMessageDebugDrawCylinder)debugDrawMessage;

                    var steps    = 32;
                    var stepsRcp = (float)(Math.PI * 2 / steps);
                    for (int i = 0; i < 32; i++)
                    {
                        float a0 = i * stepsRcp;
                        float a1 = (i + 1) * stepsRcp;

                        Vector3D A = new Vector3D(Math.Cos(a0), 1.0f, Math.Sin(a0)) * 0.5f;
                        Vector3D B = new Vector3D(Math.Cos(a1), 1.0f, Math.Sin(a1)) * 0.5f;
                        Vector3D C = A - Vector3D.UnitY;
                        Vector3D D = B - Vector3D.UnitY;

                        A = Vector3D.Transform(A, message.Matrix);
                        B = Vector3D.Transform(B, message.Matrix);
                        C = Vector3D.Transform(C, message.Matrix);
                        D = Vector3D.Transform(D, message.Matrix);

                        A -= MyEnvironment.CameraPosition;
                        B -= MyEnvironment.CameraPosition;
                        C -= MyEnvironment.CameraPosition;
                        D -= MyEnvironment.CameraPosition;

                        linesBatch.Add(A, B, message.Color);
                        linesBatch.Add(A, C, message.Color);
                        linesBatch.Add(C, D, message.Color);
                    }

                    break;
                }

                case MyRenderMessageEnum.DebugDrawTriangle:
                {
                    MyRenderMessageDebugDrawTriangle message = (MyRenderMessageDebugDrawTriangle)debugDrawMessage;

                    MyPrimitivesRenderer.DrawTriangle(message.Vertex0 - MyEnvironment.CameraPosition, message.Vertex1 - MyEnvironment.CameraPosition, message.Vertex2 - MyEnvironment.CameraPosition, message.Color);

                    break;
                }

                case MyRenderMessageEnum.DebugDrawTriangles:
                {
                    MyRenderMessageDebugDrawTriangles message = (MyRenderMessageDebugDrawTriangles)debugDrawMessage;

                    for (int i = 0; i < message.Indices.Count; i += 3)
                    {
                        var v0 = Vector3D.Transform(message.Vertices[message.Indices[i + 0]], message.WorldMatrix) - MyEnvironment.CameraPosition;
                        var v1 = Vector3D.Transform(message.Vertices[message.Indices[i + 1]], message.WorldMatrix) - MyEnvironment.CameraPosition;
                        var v2 = Vector3D.Transform(message.Vertices[message.Indices[i + 2]], message.WorldMatrix) - MyEnvironment.CameraPosition;

                        MyPrimitivesRenderer.DrawTriangle(v0, v1, v2, message.Color);
                    }

                    break;
                }

                case MyRenderMessageEnum.DebugDrawCapsule:
                {
                    MyRenderMessageDebugDrawCapsule message = (MyRenderMessageDebugDrawCapsule)debugDrawMessage;

                    var batch = message.DepthRead ? linesBatch : noDepthLinesBatch;

                    batch.AddSphereRing(new BoundingSphere(message.P0 - MyEnvironment.CameraPosition, message.Radius), message.Color, Matrix.Identity);
                    batch.AddSphereRing(new BoundingSphere(message.P0 - MyEnvironment.CameraPosition, message.Radius), message.Color, Matrix.CreateRotationX(MathHelper.PiOver2));
                    batch.AddSphereRing(new BoundingSphere(message.P1 - MyEnvironment.CameraPosition, message.Radius), message.Color, Matrix.Identity);
                    batch.AddSphereRing(new BoundingSphere(message.P1 - MyEnvironment.CameraPosition, message.Radius), message.Color, Matrix.CreateRotationX(MathHelper.PiOver2));

                    batch.Add(message.P0 - MyEnvironment.CameraPosition, message.P1 - MyEnvironment.CameraPosition, message.Color);


                    break;
                }

                case MyRenderMessageEnum.DebugDrawText2D:
                {
                    MyRenderMessageDebugDrawText2D message = (MyRenderMessageDebugDrawText2D)debugDrawMessage;

                    var text = new StringBuilder(message.Text);

                    MySpritesRenderer.DrawText(message.Coord, text, message.Color, message.Scale, message.Align);

                    break;
                }

                case MyRenderMessageEnum.DebugDrawText3D:
                {
                    MyRenderMessageDebugDrawText3D message = (MyRenderMessageDebugDrawText3D)debugDrawMessage;

                    Vector3D position = message.Coord;

                    var worldToClip = MyEnvironment.ViewProjectionD;
                    if (message.CustomViewProjection != -1)
                    {
                        if (!MyRenderProxy.BillboardsViewProjectionRead.ContainsKey(message.CustomViewProjection))
                        {
                            break;
                        }

                        var i = message.CustomViewProjection;

                        var scaleX  = MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.Width / (float)MyRender11.ViewportResolution.X;
                        var scaleY  = MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.Height / (float)MyRender11.ViewportResolution.Y;
                        var offsetX = MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.OffsetX / (float)MyRender11.ViewportResolution.X;
                        var offsetY = (MyRender11.ViewportResolution.Y - MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.OffsetY - MyRenderProxy.BillboardsViewProjectionRead[i].Viewport.Height)
                                      / (float)MyRender11.ViewportResolution.Y;

                        var viewportTransformation = new Matrix(
                            scaleX, 0, 0, 0,
                            0, scaleY, 0, 0,
                            0, 0, 1, 0,
                            offsetX, offsetY, 0, 1
                            );

                        worldToClip = MyRenderProxy.BillboardsViewProjectionRead[message.CustomViewProjection].View * MyRenderProxy.BillboardsViewProjectionRead[message.CustomViewProjection].Projection * viewportTransformation;
                    }

                    var clipPosition = Vector3D.Transform(position, ref worldToClip);
                    clipPosition.X = clipPosition.X * 0.5f + 0.5f;
                    clipPosition.Y = clipPosition.Y * -0.5f + 0.5f;

                    var borderDepth = MyRender11.UseComplementaryDepthBuffer ? 0.0f : 1.0f;
                    borderDepth = message.ClipDistance.HasValue ? Vector3.Transform(new Vector3(0, 0, -message.ClipDistance.Value), MyEnvironment.Projection).Z : borderDepth;

                    bool drawCondition =
                        MyRender11.UseComplementaryDepthBuffer
                                ? clipPosition.Z > borderDepth && clipPosition.Z < 1
                                : clipPosition.Z < borderDepth && clipPosition.Z > 0;

                    if (drawCondition)
                    {
                        MySpritesRenderer.DrawText(new Vector2((float)clipPosition.X, (float)clipPosition.Y) * MyRender11.ViewportResolution,
                                                   new StringBuilder(message.Text), message.Color, message.Scale, message.Align);
                    }

                    break;
                }

                case MyRenderMessageEnum.DebugDrawModel:
                {
                    MyRenderMessageDebugDrawModel message = (MyRenderMessageDebugDrawModel)debugDrawMessage;


                    break;
                }

                case MyRenderMessageEnum.DebugDrawPlane:
                {
                    MyRenderMessageDebugDrawPlane message = (MyRenderMessageDebugDrawPlane)debugDrawMessage;


                    break;
                }

                default:
                {
                    break;
                }
                }
            }

            linesBatch.Commit();
            noDepthLinesBatch.Commit();
            lines2D.Commit();
        }
Exemple #31
0
 public static void RenderCellCoordToWorldAABB(Vector3D referenceVoxelMapPosition, ref MyCellCoord renderCell, out BoundingBoxD worldAABB)
 {
     RenderCellCoordToLocalAABB(ref renderCell, out worldAABB);
     worldAABB = worldAABB.Translate(referenceVoxelMapPosition);
 }
        private void AddVoxelMesh(MyVoxelBase voxelBase, IMyStorage storage, Dictionary<Vector3I, MyIsoMesh> cache, float border, Vector3D originPosition, MyOrientedBoundingBoxD obb, List<BoundingBoxD> bbList)
        {
            bool useCache = cache != null;
            if (useCache)
                CheckCacheValidity();

            obb.HalfExtent += new Vector3D(border, 0, border);
            BoundingBoxD bb = obb.GetAABB();            
            int aabbSideSide = (int)Math.Round(bb.HalfExtents.Max() * 2);
            bb = new BoundingBoxD(bb.Min, bb.Min + aabbSideSide);
            bb.Translate(obb.Center - bb.Center);

            // For debug
            bbList.Add(new BoundingBoxD(bb.Min, bb.Max));

            bb = (BoundingBoxD)bb.TransformFast(voxelBase.PositionComp.WorldMatrixInvScaled);
            bb.Translate(voxelBase.SizeInMetresHalf);

            Vector3I min = Vector3I.Round(bb.Min);
            Vector3I max = min + aabbSideSide;
            Vector3I geomMin, geomMax;
            MyVoxelCoordSystems.VoxelCoordToGeometryCellCoord(ref min, out geomMin);
            MyVoxelCoordSystems.VoxelCoordToGeometryCellCoord(ref max, out geomMax);

            var cullBox = obb;
            cullBox.Transform(voxelBase.PositionComp.WorldMatrixInvScaled);
            cullBox.Center += voxelBase.SizeInMetresHalf;
            ProfilerShort.Begin("WOOOORK");

            Vector3I_RangeIterator it = new Vector3I_RangeIterator(ref geomMin, ref geomMax);
            MyCellCoord coord = new MyCellCoord();
            BoundingBox localAabb;
            coord.Lod = NAVMESH_LOD;
            int hits = 0;
            MyIsoMesh gMesh;
            Vector3 offset = originPosition - voxelBase.PositionLeftBottomCorner;

            // Calculate rotation
            Vector3 gravityVector = -Vector3.Normalize(GameSystems.MyGravityProviderSystem.CalculateTotalGravityInPoint(originPosition));
            Vector3 forwardVector = Vector3.CalculatePerpendicularVector(gravityVector);
            Quaternion quaternion = Quaternion.CreateFromForwardUp(forwardVector, gravityVector);
            Matrix rotation = Matrix.CreateFromQuaternion(Quaternion.Inverse(quaternion));

            Matrix ownRotation = voxelBase.PositionComp.WorldMatrix.GetOrientation();

            while (it.IsValid())
            {
                ProfilerShort.Begin("ITERATOR");

                if (useCache && cache.TryGetValue(it.Current, out gMesh))
                {
                    if (gMesh != null)
                    {
                        AddMeshTriangles(gMesh, offset, rotation, ownRotation);
                    }
                    it.MoveNext();
                    ProfilerShort.End();
                    continue;
                }
                    
                coord.CoordInLod = it.Current;
                MyVoxelCoordSystems.GeometryCellCoordToLocalAABB(ref coord.CoordInLod, out localAabb);
                
                if (!cullBox.Intersects(ref localAabb))
                {
                    hits++;
                    it.MoveNext();
                    ProfilerShort.End();
                    continue;
                }
                ProfilerShort.End();

                var debugBB = new BoundingBoxD(localAabb.Min, localAabb.Max).Translate(-voxelBase.SizeInMetresHalf);
                bbList.Add(debugBB);

                ProfilerShort.Begin("Mesh Calc");
                var voxelStart = coord.CoordInLod * MyVoxelConstants.GEOMETRY_CELL_SIZE_IN_VOXELS - 1;
                var voxelEnd = voxelStart + MyVoxelConstants.GEOMETRY_CELL_SIZE_IN_VOXELS //- 1
                    + 1 // overlap to neighbor so geometry is stitched together within same LOD
                    + 1; // for eg. 9 vertices in row we need 9 + 1 samples (voxels)

                var generatedMesh = MyPrecalcComponent.IsoMesher.Precalc(storage, NAVMESH_LOD, voxelStart, voxelEnd, false, false, true);
                ProfilerShort.End();

                if (useCache)
                    cache[it.Current] = generatedMesh;

                if (generatedMesh != null)
                {
                    ProfilerShort.Begin("Mesh NOT NULL");
                    AddMeshTriangles(generatedMesh, offset, rotation, ownRotation);
                    ProfilerShort.End();
                }
                it.MoveNext();
            }
            ProfilerShort.End();
        }
        private void AddVoxelMesh(MyVoxelBase voxelBase, IMyStorage storage, Dictionary <Vector3I, MyIsoMesh> cache, float border, Vector3D originPosition, MyOrientedBoundingBoxD obb, List <BoundingBoxD> bbList)
        {
            bool useCache = cache != null;

            if (useCache)
            {
                CheckCacheValidity();
            }

            obb.HalfExtent += new Vector3D(border, 0, border);
            BoundingBoxD bb           = obb.GetAABB();
            int          aabbSideSide = (int)Math.Round(bb.HalfExtents.Max() * 2);

            bb = new BoundingBoxD(bb.Min, bb.Min + aabbSideSide);
            bb.Translate(obb.Center - bb.Center);

            // For debug
            bbList.Add(new BoundingBoxD(bb.Min, bb.Max));

            bb = (BoundingBoxD)bb.TransformFast(voxelBase.PositionComp.WorldMatrixInvScaled);
            bb.Translate(voxelBase.SizeInMetresHalf);

            Vector3I min = Vector3I.Round(bb.Min);
            Vector3I max = min + aabbSideSide;
            Vector3I geomMin, geomMax;

            MyVoxelCoordSystems.VoxelCoordToGeometryCellCoord(ref min, out geomMin);
            MyVoxelCoordSystems.VoxelCoordToGeometryCellCoord(ref max, out geomMax);

            var cullBox = obb;

            cullBox.Transform(voxelBase.PositionComp.WorldMatrixInvScaled);
            cullBox.Center += voxelBase.SizeInMetresHalf;
            ProfilerShort.Begin("WOOOORK");

            Vector3I_RangeIterator it    = new Vector3I_RangeIterator(ref geomMin, ref geomMax);
            MyCellCoord            coord = new MyCellCoord();
            BoundingBox            localAabb;

            coord.Lod = NAVMESH_LOD;
            int       hits = 0;
            MyIsoMesh gMesh;
            Vector3   offset = originPosition - voxelBase.PositionLeftBottomCorner;

            // Calculate rotation
            Vector3    gravityVector = -Vector3.Normalize(GameSystems.MyGravityProviderSystem.CalculateTotalGravityInPoint(originPosition));
            Vector3    forwardVector = Vector3.CalculatePerpendicularVector(gravityVector);
            Quaternion quaternion    = Quaternion.CreateFromForwardUp(forwardVector, gravityVector);
            Matrix     rotation      = Matrix.CreateFromQuaternion(Quaternion.Inverse(quaternion));

            Matrix ownRotation = voxelBase.PositionComp.WorldMatrix.GetOrientation();

            while (it.IsValid())
            {
                ProfilerShort.Begin("ITERATOR");

                if (useCache && cache.TryGetValue(it.Current, out gMesh))
                {
                    if (gMesh != null)
                    {
                        AddMeshTriangles(gMesh, offset, rotation, ownRotation);
                    }
                    it.MoveNext();
                    ProfilerShort.End();
                    continue;
                }

                coord.CoordInLod = it.Current;
                MyVoxelCoordSystems.GeometryCellCoordToLocalAABB(ref coord.CoordInLod, out localAabb);

                if (!cullBox.Intersects(ref localAabb))
                {
                    hits++;
                    it.MoveNext();
                    ProfilerShort.End();
                    continue;
                }
                ProfilerShort.End();

                var debugBB = new BoundingBoxD(localAabb.Min, localAabb.Max).Translate(-voxelBase.SizeInMetresHalf);
                bbList.Add(debugBB);

                ProfilerShort.Begin("Mesh Calc");
                var voxelStart = coord.CoordInLod * MyVoxelConstants.GEOMETRY_CELL_SIZE_IN_VOXELS - 1;
                var voxelEnd   = voxelStart + MyVoxelConstants.GEOMETRY_CELL_SIZE_IN_VOXELS //- 1
                                 + 1                                                        // overlap to neighbor so geometry is stitched together within same LOD
                                 + 1;                                                       // for eg. 9 vertices in row we need 9 + 1 samples (voxels)

                var generatedMesh = MyPrecalcComponent.IsoMesher.Precalc(storage, NAVMESH_LOD, voxelStart, voxelEnd, false, false, true);
                ProfilerShort.End();

                if (useCache)
                {
                    cache[it.Current] = generatedMesh;
                }

                if (generatedMesh != null)
                {
                    ProfilerShort.Begin("Mesh NOT NULL");
                    AddMeshTriangles(generatedMesh, offset, rotation, ownRotation);
                    ProfilerShort.End();
                }
                it.MoveNext();
            }
            ProfilerShort.End();
        }
        public unsafe void QuerySurfaceParameters(Vector3D localOrigin, ref BoundingBoxD queryBounds, List<Vector3> queries, List<MySurfaceParams> results)
        {
            localOrigin -= Planet.PositionLeftBottomCorner;

            using (Planet.Storage.Pin())
            {
                var bounds = (BoundingBox)queryBounds.Translate(-Planet.PositionLeftBottomCorner);

                Planet.Provider.Shape.PrepareCache();
                Planet.Provider.Material.PrepareRulesForBox(ref bounds);

                if (results.Capacity != queries.Count)
                {
                    results.Capacity = queries.Count;
                }

                fixed (MySurfaceParams* pars = results.GetInternalArray())
                {
                    for (int i = 0; i < queries.Count; ++i)
                    {
                        Planet.Provider.ComputeCombinedMaterialAndSurface(queries[i] + localOrigin, true, out pars[i]);
                        pars[i].Position -= localOrigin;
                    }
                }

                results.SetSize(queries.Count);
            }
        }
            public override void Draw()
            {
                base.Draw();

                if (MySession.Static == null) return;

                var ops = MySession.Static.GetComponent<MyVoxelOperationsSessionComponent>();

                if (ops != null)
                {
                    Text("Cache Enabled: {0}", MyVoxelOperationsSessionComponent.EnableCache);

                    Text("Cache Writing: {0}", ops.ShouldWrite ? "Enabled" : "Disabled");
                    Text("Cache Flushing: {0}", ops.ShouldFlush ? "Enabled" : "Disabled");

                    var storages = ops.QueuedStorages.ToArray();

                    if (storages.Length == 0)
                    {
                        Text(Color.Orange, "No queued storages.");
                    }
                    else
                    {
                        Text(Color.Yellow, 1.2f, "{0} Queued storages:", storages.Length);

                        foreach (var storage in storages)
                        {
                            MyStorageBase.WriteCacheStats stats;
                            storage.GetStats(out stats);
                            Text("Voxel storage {0}:", storage.ToString());
                            Text(Color.White, .9f, "Pending Writes: {0}", stats.QueuedWrites);
                            Text(Color.White, .9f, "Cached Chunks: {0}", stats.CachedChunks);

                            if (DisplayDetails)
                            {
                                foreach (var chunk in stats.Chunks)
                                {
                                    var ck = chunk.Value;
                                    Text(Color.Wheat, .9f, "Chunk {0}: {1} hits; pending {2}", chunk.Key, ck.HitCount, ck.Dirty);
                                }
                            }

                            if (DebugDraw)
                            {
                                var vmap = MySession.Static.VoxelMaps.Instances.FirstOrDefault(x => x.Storage == storage);

                                if (vmap == null) continue;

                                foreach (var chunk in stats.Chunks)
                                {
                                    var box = new BoundingBoxD(chunk.Key << MyStorageBase.VoxelChunk.SizeBits, ((chunk.Key + 1) << MyStorageBase.VoxelChunk.SizeBits));

                                    box.Translate(-((Vector3D)storage.Size * .5) - .5);

                                    MyRenderProxy.DebugDrawOBB(new MyOrientedBoundingBoxD(box, vmap.WorldMatrix), GetColorForDirty(chunk.Value.Dirty), 0.1f, true, true);
                                }
                            }
                        }
                    }
                }
            }