示例#1
0
        public void RotateAsteroid(VRageMath.Quaternion quaternion)
        {
            var sourceFile = SourceVoxelFilepath ?? VoxelFilepath;

            var asteroid = new MyVoxelMap();

            asteroid.Load(sourceFile, SpaceEngineersCore.Resources.GetDefaultMaterialName(), true);

            var newAsteroid = new MyVoxelMap();
            var transSize   = Vector3I.Transform(asteroid.Size, quaternion);
            var newSize     = Vector3I.Abs(transSize);

            newAsteroid.Init(Vector3D.Zero, newSize, SpaceEngineersCore.Resources.GetDefaultMaterialName());

            Vector3I coords;

            for (coords.Z = 0; coords.Z < asteroid.Size.Z; coords.Z++)
            {
                for (coords.Y = 0; coords.Y < asteroid.Size.Y; coords.Y++)
                {
                    for (coords.X = 0; coords.X < asteroid.Size.X; coords.X++)
                    {
                        byte   volume = 0xff;
                        string cellMaterial;

                        asteroid.GetVoxelMaterialContent(ref coords, out cellMaterial, out volume);

                        var newCoord = Vector3I.Transform(coords, quaternion);
                        // readjust the points, as rotation occurs arround 0,0,0.
                        newCoord.X = newCoord.X < 0 ? newCoord.X - transSize.X : newCoord.X;
                        newCoord.Y = newCoord.Y < 0 ? newCoord.Y - transSize.Y : newCoord.Y;
                        newCoord.Z = newCoord.Z < 0 ? newCoord.Z - transSize.Z : newCoord.Z;
                        newAsteroid.SetVoxelContent(volume, ref newCoord);
                        newAsteroid.SetVoxelMaterialAndIndestructibleContent(cellMaterial, 0xff, ref newCoord);
                    }
                }
            }

            var tempfilename = TempfileUtil.NewFilename(MyVoxelMap.V2FileExtension);

            newAsteroid.Save(tempfilename);

            SourceVoxelFilepath = tempfilename;
        }
示例#2
0
        public static double CreateExtendedFit(MyCubeBlock shield, Vector3D gridHalfExtents, List <IMySlimBlock> fitblocks, Vector3D[] fitPoints)
        {
            var subGrids = MyAPIGateway.GridGroups.GetGroup(shield.CubeGrid, GridLinkTypeEnum.Mechanical);

            foreach (var grid in subGrids)
            {
                if (grid.MarkedForClose)
                {
                    continue;
                }
                fitblocks.AddRange(((MyCubeGrid)grid).GetBlocks());
            }

            var bQuaternion = Quaternion.CreateFromRotationMatrix(shield.CubeGrid.PositionComp.WorldMatrixRef);

            var sqrt3  = Math.Sqrt(3);
            var sqrt5  = Math.Sqrt(5);
            var last   = 0;
            var repeat = 0;

            for (int i = 0; i <= 10; i++)
            {
                var ellipsoidAdjust = MathHelper.Lerp(sqrt3, sqrt5, i * 0.1);

                var shieldSize   = gridHalfExtents * ellipsoidAdjust;
                var mobileMatrix = MatrixD.CreateScale(shieldSize);
                mobileMatrix.Translation = shield.CubeGrid.PositionComp.LocalVolume.Center;
                var matrixInv = MatrixD.Invert(mobileMatrix * shield.CubeGrid.PositionComp.WorldMatrixRef);

                var c = 0;
                for (int j = 0; j < fitblocks.Count; j++)
                {
                    var block = fitblocks[j];
                    var fat   = block.FatBlock as MyCubeBlock;

                    if (fat != null && fat.MarkedForClose || block.IsDestroyed)
                    {
                        continue;
                    }

                    BoundingBoxD blockBox;
                    Vector3D     center;
                    if (fat != null)
                    {
                        blockBox = fat.PositionComp.LocalAABB;
                        center   = fat.PositionComp.WorldAABB.Center;
                    }
                    else
                    {
                        Vector3 halfExt;
                        block.ComputeScaledHalfExtents(out halfExt);
                        blockBox = new BoundingBoxD(-halfExt, halfExt);
                        block.ComputeWorldCenter(out center);
                    }

                    var bOriBBoxD = new MyOrientedBoundingBoxD(center, blockBox.HalfExtents, bQuaternion);

                    bOriBBoxD.GetCorners(fitPoints, 0);
                    foreach (var point in fitPoints)
                    {
                        if (!CustomCollision.PointInShield(point, matrixInv) && ((MyCubeGrid)block.CubeGrid).IsSameConstructAs(shield.CubeGrid))
                        {
                            c++;
                        }
                    }
                }

                if (c == last)
                {
                    repeat++;
                }
                else
                {
                    repeat = 0;
                }

                if (c == 0)
                {
                    return(MathHelper.Lerp(sqrt3, sqrt5, i * 0.1));
                }
                last = c;
                if (i == 10 && repeat > 2)
                {
                    return(MathHelper.Lerp(sqrt3, sqrt5, ((10 - repeat) + 1) * 0.1));
                }
            }
            return(sqrt5);
        }
示例#3
0
        public static double CreateNormalFit(MyCubeBlock shield, Vector3D gridHalfExtents, List <IMySlimBlock> fitblocks, Vector3D[] fitPoints)
        {
            var subGrids = MyAPIGateway.GridGroups.GetGroup(shield.CubeGrid, GridLinkTypeEnum.Mechanical);

            foreach (var grid in subGrids)
            {
                if (grid.MarkedForClose)
                {
                    continue;
                }
                fitblocks.AddRange(((MyCubeGrid)grid).GetBlocks());
            }

            var bQuaternion = Quaternion.CreateFromRotationMatrix(shield.CubeGrid.PositionComp.WorldMatrixRef);

            var end        = 10;
            var wasOutside = false;

            for (int i = 0; i <= end + 1; i++)
            {
                var fitSeq = Session.Instance.FitSeq[i];

                var ellipsoidAdjust = MathHelper.Lerp(fitSeq.SqrtStart, fitSeq.SqrtEnd, fitSeq.SeqMulti);

                var shieldSize   = gridHalfExtents * ellipsoidAdjust;
                var mobileMatrix = MatrixD.CreateScale(shieldSize);
                mobileMatrix.Translation = shield.CubeGrid.PositionComp.LocalAABB.Center;
                var matrixInv = MatrixD.Invert(mobileMatrix * shield.CubeGrid.PositionComp.WorldMatrixRef);

                var pointOutside = false;
                for (int j = 0; j < fitblocks.Count; j++)
                {
                    var block = fitblocks[j];
                    var fat   = block.FatBlock as MyCubeBlock;
                    if (fat != null && fat.MarkedForClose || block.IsDestroyed)
                    {
                        continue;
                    }

                    BoundingBoxD blockBox;
                    Vector3D     center;
                    if (fat != null)
                    {
                        blockBox = fat.PositionComp.LocalAABB;
                        center   = fat.PositionComp.WorldAABB.Center;
                    }
                    else
                    {
                        Vector3 halfExt;
                        block.ComputeScaledHalfExtents(out halfExt);
                        blockBox = new BoundingBoxD(-halfExt, halfExt);
                        block.ComputeWorldCenter(out center);
                    }

                    var bOriBBoxD = new MyOrientedBoundingBoxD(center, blockBox.HalfExtents, bQuaternion);

                    bOriBBoxD.GetCorners(fitPoints, 0);
                    foreach (var point in fitPoints)
                    {
                        if (!CustomCollision.PointInShield(point, matrixInv) && ((MyCubeGrid)block.CubeGrid).IsSameConstructAs(shield.CubeGrid))
                        {
                            pointOutside = true;
                            break;
                        }
                    }
                }

                if (pointOutside)
                {
                    wasOutside = true;
                    if (i == 0)
                    {
                        return(ellipsoidAdjust);
                    }

                    if (i == 2)
                    {
                        i   = 10;
                        end = 19;
                    }
                }

                if (!pointOutside)
                {
                    if (i == 1)
                    {
                        return(ellipsoidAdjust);
                    }
                    if (wasOutside)
                    {
                        return(ellipsoidAdjust);
                    }
                }

                if (i == end)
                {
                    return(ellipsoidAdjust);
                }
            }
            return(Math.Sqrt(5));
        }
        public static void DebugDrawCylinder(Vector3D position, Quaternion orientation, float radius, float height, Color color, float alpha, bool depthRead, bool smooth)
        {
            MatrixD m = MatrixD.CreateFromQuaternion(orientation);
            m.Right *= 2f * radius;
            m.Forward *= 2f * radius;
            m.Up *= height;
            m.Translation = position;

            DebugDrawCylinder(m, color, alpha, depthRead, smooth);
        }
        public static void UpdateRenderEnvironment(
            Vector3 sunDirection,
            Color sunColor,
            Color[] additionalSunColors,
            Color sunSpecularColor,
            float sunIntensity,
            float[] additionalSunIntensities,
            Vector2[] additionalSunDirections,
            bool sunLightOn,
            Color ambientColor,
            float ambientMultiplier,
            float envAmbientIntensity,
            Color backgroundColor,
            string backgroundTexture,
            Quaternion backgroundOrientation,
            float sunSizeMultiplier,
            float distanceToSun,
            string sunMaterial,
            float dayTime,
            bool resetEyeAdaptation = false,
            bool enableSunBillboard = false,
            float planetFactor = 0.0f
)
        {
            var message = MessagePool.Get<MyRenderMessageUpdateRenderEnvironment>(MyRenderMessageEnum.UpdateRenderEnvironment);

            message.SunDirection = sunDirection;
            message.SunColor = sunColor;
            message.SunSpecularColor = sunSpecularColor;
            message.SunIntensity = sunIntensity;

            Debug.Assert(additionalSunDirections == null || additionalSunDirections.Length <= message.AdditionalSunDirections.Length);
            if (additionalSunDirections != null)
            {
                for (int lightIndex = 0; lightIndex < additionalSunDirections.Length; ++lightIndex)
                {
                    message.AdditionalSunColors[lightIndex] = additionalSunColors[lightIndex];
                    message.AdditionalSunIntensities[lightIndex] = additionalSunIntensities[lightIndex];
                    message.AdditionalSunDirections[lightIndex] = -additionalSunDirections[lightIndex];
                    message.AdditionalSunCount = additionalSunDirections.Length;
                }
            }
            message.SunLightOn = sunLightOn;
            message.AmbientColor = ambientColor;
            message.AmbientMultiplier = ambientMultiplier;
            message.EnvAmbientIntensity = envAmbientIntensity;
            message.BackgroundColor = backgroundColor;
            message.BackgroundTexture = backgroundTexture;
            message.BackgroundOrientation = backgroundOrientation;
            message.SunSizeMultiplier = sunSizeMultiplier;
            message.DistanceToSun = distanceToSun;
            message.SunMaterial = sunMaterial;
            message.DayTime = dayTime;
            message.ResetEyeAdaptation = resetEyeAdaptation;
            message.SunBillboardEnabled = enableSunBillboard;
            message.PlanetFactor = planetFactor;

            EnqueueMessage(message);
        }
示例#6
0
        public static void UpdateRenderEnvironment(
            Vector3 sunDirection,
            Color sunColor,
            Color sunBackColor,
            Color sunSpecularColor,
            float sunIntensity,
            float sunBackIntensity,
            bool sunLightOn,
            Color ambientColor,
            float ambientMultiplier,
            float envAmbientIntensity,
            Color backgroundColor,
            string backgroundTexture,
            Quaternion backgroundOrientation,
            float sunSizeMultiplier,
            float distanceToSun,
            string sunMaterial,
            float dayTime,
            bool resetEyeAdaptation = false,
            bool enableSunBillboard = false
)
        {
            var message = MessagePool.Get<MyRenderMessageUpdateRenderEnvironment>(MyRenderMessageEnum.UpdateRenderEnvironment);

            message.SunDirection = sunDirection;
            message.SunColor = sunColor;
            message.SunBackColor = sunBackColor;
            message.SunSpecularColor = sunSpecularColor;
            message.SunIntensity = sunIntensity;
            message.SunBackIntensity = sunBackIntensity;
            message.SunLightOn = sunLightOn;
            message.AmbientColor = ambientColor;
            message.AmbientMultiplier = ambientMultiplier;
            message.EnvAmbientIntensity = envAmbientIntensity;
            message.BackgroundColor = backgroundColor;
            message.BackgroundTexture = backgroundTexture;
            message.BackgroundOrientation = backgroundOrientation;
            message.SunSizeMultiplier = sunSizeMultiplier;
            message.DistanceToSun = distanceToSun;
            message.SunMaterial = sunMaterial;
            message.DayTime = dayTime;
            message.ResetEyeAdaptation = resetEyeAdaptation;
            message.SunBillboardEnabled = enableSunBillboard;

            EnqueueMessage(message);
        }
示例#7
0
        public static double CreateExtendedFit(IMyCubeBlock shield, Vector3D gridHalfExtents)
        {
            var blockPoints = new Vector3D[8];
            var blocks      = new List <IMySlimBlock>();

            var subGrids = MyAPIGateway.GridGroups.GetGroup(shield.CubeGrid, GridLinkTypeEnum.Mechanical);

            foreach (var grid in subGrids)
            {
                grid.GetBlocks(blocks);
            }

            var bQuaternion = Quaternion.CreateFromRotationMatrix(shield.CubeGrid.WorldMatrix);
            var sqrt3       = Math.Sqrt(3);
            var sqrt5       = Math.Sqrt(5);
            var last        = 0;
            var repeat      = 0;

            for (int i = 0; i <= 10; i++)
            {
                var ellipsoidAdjust = MathHelper.Lerp(sqrt3, sqrt5, i * 0.1);

                var shieldSize   = gridHalfExtents * ellipsoidAdjust;
                var mobileMatrix = MatrixD.CreateScale(shieldSize);
                mobileMatrix.Translation = shield.CubeGrid.PositionComp.LocalVolume.Center;
                var matrixInv = MatrixD.Invert(mobileMatrix * shield.CubeGrid.WorldMatrix);

                var c = 0;
                foreach (var block in blocks)
                {
                    BoundingBoxD blockBox;
                    Vector3D     center;
                    if (block.FatBlock != null)
                    {
                        blockBox = block.FatBlock.LocalAABB;
                        center   = block.FatBlock.WorldAABB.Center;
                    }
                    else
                    {
                        Vector3 halfExt;
                        block.ComputeScaledHalfExtents(out halfExt);
                        blockBox = new BoundingBoxD(-halfExt, halfExt);
                        block.ComputeWorldCenter(out center);
                    }

                    var bOriBBoxD = new MyOrientedBoundingBoxD(center, blockBox.HalfExtents, bQuaternion);

                    bOriBBoxD.GetCorners(blockPoints, 0);
                    foreach (var point in blockPoints)
                    {
                        if (!CustomCollision.PointInShield(point, matrixInv))
                        {
                            c++;
                        }
                    }
                }

                if (c == last)
                {
                    repeat++;
                }
                else
                {
                    repeat = 0;
                }

                if (c == 0)
                {
                    return(MathHelper.Lerp(sqrt3, sqrt5, i * 0.1));
                }
                last = c;
                if (i == 10 && repeat > 2)
                {
                    return(MathHelper.Lerp(sqrt3, sqrt5, ((10 - repeat) + 1) * 0.1));
                }
            }
            return(sqrt5);
        }
示例#8
0
        public static double CreateNormalFit(IMyCubeBlock shield, Vector3D gridHalfExtents)
        {
            var blockPoints = new Vector3D[8];
            var blocks      = new List <IMySlimBlock>();

            var subGrids = MyAPIGateway.GridGroups.GetGroup(shield.CubeGrid, GridLinkTypeEnum.Mechanical);

            foreach (var grid in subGrids)
            {
                grid.GetBlocks(blocks);
            }

            var bQuaternion = Quaternion.CreateFromRotationMatrix(shield.CubeGrid.WorldMatrix);

            var end        = 10;
            var wasOutside = false;

            for (int i = 0; i <= end + 1; i++)
            {
                var fitSeq = Session.Instance.FitSeq[i];

                var ellipsoidAdjust = MathHelper.Lerp(fitSeq.SqrtStart, fitSeq.SqrtEnd, fitSeq.SeqMulti);

                var shieldSize   = gridHalfExtents * ellipsoidAdjust;
                var mobileMatrix = MatrixD.CreateScale(shieldSize);
                mobileMatrix.Translation = shield.CubeGrid.PositionComp.LocalAABB.Center;
                var matrixInv = MatrixD.Invert(mobileMatrix * shield.CubeGrid.WorldMatrix);

                var pointOutside = false;
                foreach (var block in blocks)
                {
                    BoundingBoxD blockBox;
                    Vector3D     center;
                    if (block.FatBlock != null)
                    {
                        blockBox = block.FatBlock.LocalAABB;
                        center   = block.FatBlock.WorldAABB.Center;
                    }
                    else
                    {
                        Vector3 halfExt;
                        block.ComputeScaledHalfExtents(out halfExt);
                        blockBox = new BoundingBoxD(-halfExt, halfExt);
                        block.ComputeWorldCenter(out center);
                    }

                    var bOriBBoxD = new MyOrientedBoundingBoxD(center, blockBox.HalfExtents, bQuaternion);

                    bOriBBoxD.GetCorners(blockPoints, 0);
                    foreach (var point in blockPoints)
                    {
                        if (!CustomCollision.PointInShield(point, matrixInv))
                        {
                            pointOutside = true;
                            break;
                        }
                    }
                }

                //Log.Line($"i:{i} - end:{end} - sqrtStart:{fitSeq.SqrtStart} - sqrtEnd:{fitSeq.SqrtEnd} - multi:{fitSeq.SeqMulti}");
                if (pointOutside)
                {
                    wasOutside = true;
                    //Log.Line($"Point is Outside");
                    if (i == 0)
                    {
                        //Log.Line("i == 0 and points outside returning maxSize");
                        return(ellipsoidAdjust);
                    }

                    if (i == 2)
                    {
                        //Log.Line($"Mid outside, jumping to 11");
                        i   = 10;
                        end = 19;
                    }
                }

                if (!pointOutside)
                {
                    //Log.Line($"Point is Inside");
                    if (i == 1)
                    {
                        //Log.Line($"i == 1, returning minValue");
                        return(ellipsoidAdjust);
                    }
                    if (wasOutside)
                    {
                        //Log.Line($"wasOutside and now in, returning value");
                        return(ellipsoidAdjust);
                    }
                }

                if (i == end)
                {
                    //Log.Line($"reached end, returning current value");
                    return(ellipsoidAdjust);
                }
            }
            //Log.Line($"fitNoMatch");
            return(Math.Sqrt(5));
        }