Ejemplo n.º 1
0
                      /*
        protected override void Explode()
        {
            MyExplosion newExplosion = MyExplosions.AddExplosion();
            if (newExplosion != null)
            {
                BoundingSphere explosionSphere = WorldVolumeHr;
                explosionSphere.Radius *= m_config.ExplosionRadiusMultiplier;
                MyVoxelMap voxelMap = MyVoxelMaps.GetOverlappingWithSphere(ref explosionSphere);
                MyExplosionDebrisModel.CreateExplosionDebris(ref explosionSphere, MyGroupMask.Empty, this, voxelMap);
                newExplosion.Start(0, MyPrefabConstants.VOLUME_DAMAGE_MULTIPLIER * m_config.ExplosionDamageMultiplier * WorldVolumeHr.Radius, 0, m_config.ExplosionType, explosionSphere, MyExplosionsConstants.EXPLOSION_LIFESPAN);
            }
        }               */

        public override bool GetIntersectionWithLine(ref MyLine line, out MyIntersectionResultLineTriangleEx? t, IntersectionFlags flags = IntersectionFlags.ALL_TRIANGLES)
        {
            return m_gun.GetIntersectionWithLine(ref line, out t);
        }
Ejemplo n.º 2
0
 private bool IsVisibleFromSun(MyIntersectionResultLineTriangleEx? result)
 {
     if (!MyRender.EnableAsteroidShadows)
     {
         if (result.HasValue && (result.Value.Entity is MyStaticAsteroid) && (result.Value.Entity as MyStaticAsteroid).IsGenerated)
             return true;
         else
             return !result.HasValue;
     }
     else
         return !result.HasValue;
 }
Ejemplo n.º 3
0
        //  Add decal and all surounding triangles for voxel intersection
        static void AddDecalVoxel(MyDecalTexturesEnum decalTexture, float decalSize, float decalScale, Vector4 color, bool alphaBlendByAngle,
            ref MyIntersectionResultLineTriangleEx intersection, ref MyPlane rightPlane, ref MyPlane upPlane, float lightSize, float emissivity, float decalNormalOffset)
        {
            MyVoxelMap voxelMap = (MyVoxelMap)intersection.Entity;

            MyMwcVector3Int renderCellCoord = voxelMap.GetVoxelRenderCellCoordinateFromMeters(ref intersection.IntersectionPointInWorldSpace);
            BoundingSphere decalBoundingSphere = new BoundingSphere(intersection.IntersectionPointInWorldSpace, decalSize);

            //  If whole decal can't fit inside of render cell, we won't add any of its triangles. This is because
            //  when hiding/removing triangles after explosion, it is easier to check only one render cell.
            BoundingBox renderCellBoundingBox;
            voxelMap.GetRenderCellBoundingBox(ref renderCellCoord, out renderCellBoundingBox);

            // TODO simon - commented as an experiment. If there are bugs with decals on voxels, remove the comment below
            //if (renderCellBoundingBox.Contains(decalBoundingSphere) != ContainmentType.Contains) return;

            //  If we get null, buffer is full so no new decals can't be placed
            MyDecalsForVoxelsTriangleBuffer decalsBuffer = m_decalsForVoxels.GetTrianglesBuffer(voxelMap, ref renderCellCoord, decalTexture, ref renderCellBoundingBox);
            if (decalsBuffer == null) return;

            //  We need to create decals on neighborhood triangles too, so we check all triangles if they fall in decal's sphere and if yes, we place decal on them.
            //  We check triangles from same voxelmap or model only.

            m_neighbourTriangles.Clear();
            //intersection.VoxelMap.GetTrianglesIntersectingSphere(ref decalBoundingSphere, intersection.TriangleHelperIndex, m_neighbourTriangles, decalsBuffer.MaxNeighbourTriangles);
            voxelMap.GetTrianglesIntersectingSphere(ref decalBoundingSphere, m_neighbourTriangles, decalsBuffer.MaxNeighbourTriangles, false);

            int trianglesToAdd = m_neighbourTriangles.Count;// +1;

            if (trianglesToAdd == 0)
            {
                return;
            }

            if (decalsBuffer.CanAddTriangles(trianglesToAdd) == true)
            {
                var normalSum = CalculateDominantNormal(m_neighbourTriangles);
                normalSum *= decalNormalOffset;

                //  Create decal for every neighbour triangleVertexes
                for (int i = 0; i < m_neighbourTriangles.Count; i++)
                {
                    trianglesToAdd--;

                    var triangle = m_neighbourTriangles[i];
                    triangle.Vertexes.Vertex0 += normalSum;
                    triangle.Vertexes.Vertex1 += normalSum;
                    triangle.Vertexes.Vertex2 += normalSum;
                    m_neighbourTriangles[i] = triangle;

                    decalsBuffer.Add(m_neighbourTriangles[i], intersection.NormalInWorldSpace, ref rightPlane,
                        ref upPlane, decalScale, trianglesToAdd, color, alphaBlendByAngle, lightSize, intersection.IntersectionPointInWorldSpace, emissivity);
                }
            }
        }
Ejemplo n.º 4
0
        //  Add decal and all surounding triangles for model intersection
        static void AddDecalModel(MyDecalTexturesEnum decalTexture, float decalSize, float decalScale, Vector4 color, bool alphaBlendByAngle, 
            ref MyIntersectionResultLineTriangleEx intersection, ref MyPlane rightPlane, ref MyPlane upPlane, float lightSize, float emissivity, float decalNormalOffset)
        {
            MyDecalsForPhysObjectsTriangleBuffer decalsBuffer = m_decalsForModels.GetTrianglesBuffer(intersection.Entity, decalTexture);

            //  If we get null, buffer is full so no new decals can't be placed
            if (decalsBuffer == null) return;

            //  We need to create decals on neighborhood triangles too, so we check all triangles if they fall in decal's sphere and if yes, we place decal on them.
            //  We check triangles from same voxelmap or model only.

            BoundingSphere decalSphere = new BoundingSphere(intersection.IntersectionPointInObjectSpace, decalSize);
            m_neighbourTriangles.Clear();
            
            intersection.Entity.GetTrianglesIntersectingSphere(ref decalSphere, intersection.NormalInObjectSpace, MyDecalsConstants.MAX_NEIGHBOUR_ANGLE, m_neighbourTriangles, decalsBuffer.MaxNeighbourTriangles);

            int trianglesToAdd = m_neighbourTriangles.Count;

            if (trianglesToAdd == 0)
            {
                return;
            }

            if (decalsBuffer.CanAddTriangles(trianglesToAdd))
            {
                Vector3 normalSum = Vector3.Zero;
                if (MyFakes.USE_DOMINANT_NORMAL_OFFSET_FOR_MODELS)
                {
                    normalSum = CalculateDominantNormal(m_neighbourTriangles);
                    normalSum *= decalNormalOffset;
                }

                //  Create decal for every neighbour triangleVertexes
                for (int i = 0; i < m_neighbourTriangles.Count; i++)
                {
                    trianglesToAdd--;

                    if (MyFakes.USE_DOMINANT_NORMAL_OFFSET_FOR_MODELS)
                    {
                        var triangle = m_neighbourTriangles[i];
                        triangle.Vertexes.Vertex0 += normalSum;
                        triangle.Vertexes.Vertex1 += normalSum;
                        triangle.Vertexes.Vertex2 += normalSum;
                        m_neighbourTriangles[i] = triangle;
                    }

                    decalsBuffer.Add(m_neighbourTriangles[i], intersection.Triangle.InputTriangleNormal,
                        ref rightPlane, ref upPlane, decalScale, decalSize, trianglesToAdd, color, alphaBlendByAngle, lightSize, intersection.IntersectionPointInObjectSpace, emissivity);
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Gets the intersection with line.
        /// </summary>
        /// <param name="line">The line.</param>
        /// <returns></returns>
        public override bool GetIntersectionWithLine(ref MyLine line, out MyIntersectionResultLineTriangleEx? t, IntersectionFlags flags = IntersectionFlags.ALL_TRIANGLES)
        {
            t = null;
            //  First check bounding sphere of whole physic object. We must trust that all his childs are inside of default model bounding sphere.
            Matrix worldMatrix = this.WorldMatrix;
            BoundingSphere boundingSphere = new BoundingSphere(MyUtils.GetTransform(ModelLod0.BoundingSphere.Center, ref worldMatrix), ModelLod0.BoundingSphere.Radius);
            if (MyUtils.IsLineIntersectingBoundingSphere(ref line, ref boundingSphere) == false)
                return false;

            //  Test against default object model
            MyIntersectionResultLineTriangleEx? intersectionWithBase;
            base.GetIntersectionWithLine(ref line, out intersectionWithBase);

            //  Test against childs of this phys object (in this case guns)
            MyIntersectionResultLineTriangleEx? intersectionWithWeapons = Weapons != null ? Weapons.GetIntersectionWithLine(ref line) : null;

            //  Find closer intersection of these two
            MyIntersectionResultLineTriangleEx? result = MyIntersectionResultLineTriangleEx.GetCloserIntersection(ref intersectionWithBase, ref intersectionWithWeapons);

            //  Only if this is "player's ship" - thus someone is siting in inside (this is an optimization - enemy ships don't render glass decals, they don't need this code)
            if (this == MySession.PlayerShip)
            {
                //  Intersection with ideal glass
                if (GetShipCockpitGlass() != null && GetShipCockpitGlass().ModelLod0 != null && GetShipCockpitGlass().ModelLod0.GetTrianglePruningStructure() != null)
                {
                    var intersectionGlass =
                        GetShipCockpitGlass().ModelLod0.GetTrianglePruningStructure().GetIntersectionWithLine(
                            GetShipCockpitGlass(), ref line, IntersectionFlags.FLIPPED_TRIANGLES);

                    const float GLASS_DISTANCE_TOLERANCE = 3 * MySmallShipConstants.ALL_SMALL_SHIP_MODEL_SCALE;
                    if (MyIntersectionResultLineTriangleEx.IsDistanceLessThanTolerance(ref result, ref intersectionGlass, GLASS_DISTANCE_TOLERANCE))
                    {
                        result = intersectionGlass;
                    }
                }
            }
            t = result;

            return result != null;
        }
Ejemplo n.º 6
0
        //  Add decal and all surounding triangles according to the type of intersection (model or voxel)
        public static void Add(MyDecalTexturesEnum decalTexture, float decalSize, float angle, Vector4 color, bool alphaBlendByAngle,
            ref MyIntersectionResultLineTriangleEx intersection, float lightSize, float emissivity, float decalNormalOffset)
        {
            if (!MyRenderConstants.RenderQualityProfile.EnableDecals)
                return;

            //  Ignore decals too far away
            if (Vector3.Distance(MyCamera.Position, intersection.IntersectionPointInWorldSpace) > (MyDecalsConstants.MAX_DISTANCE_FOR_ADDING_DECALS / MyCamera.Zoom.GetZoomLevel())) 
                return;

            //	Polomer decalu a scale faktor pre vypocet textury.
            //  Decal size is something as radius of a decal, so when converting from real metres to texture space, we need to divide by 2.0
            float decalScale = 1.0f / (2.0f * decalSize);

            // Fix: This is safer way to get right vector.
            Vector3 rightVector;
            MyTriangle_Vertexes triangle = intersection.Triangle.InputTriangle;

            if ((triangle.Vertex0 - intersection.IntersectionPointInObjectSpace).Length() > MyMwcMathConstants.EPSILON)
            {
                rightVector = MyMwcUtils.Normalize(triangle.Vertex0 - intersection.IntersectionPointInObjectSpace);
            }
            else if ((triangle.Vertex1 - intersection.IntersectionPointInObjectSpace).Length() > MyMwcMathConstants.EPSILON)
            {
                rightVector = MyMwcUtils.Normalize(triangle.Vertex1 - intersection.IntersectionPointInObjectSpace);
            }
            else if ((triangle.Vertex2 - intersection.IntersectionPointInObjectSpace).Length() > MyMwcMathConstants.EPSILON)
            {
                rightVector = MyMwcUtils.Normalize(triangle.Vertex2 - intersection.IntersectionPointInObjectSpace);
            }
            else
            {
                System.Diagnostics.Debug.Assert(false, "Normal has zero length! Probably invalid intersection point!");
                return;
            }

            Vector3 upVector = Vector3.Cross(rightVector, intersection.NormalInObjectSpace);

            if (!MyMwcUtils.HasValidLength(upVector))
            {
                //System.Diagnostics.Debug.Assert(false, "Invalid result of cross produt!");
                return;
            }

            upVector = MyMwcUtils.Normalize(upVector);

            //  We create world matrix for the decal and then rotate the matrix, so we can extract rotated right/up vectors/planes for texture coord0 calculations
            Matrix decalMatrix = Matrix.CreateRotationZ(angle) * Matrix.CreateWorld(intersection.IntersectionPointInObjectSpace, intersection.NormalInObjectSpace, upVector);
            
            //	Right plane
            MyPlane rightPlane;
            rightPlane.Point = intersection.IntersectionPointInObjectSpace;
            rightPlane.Normal = MyUtils.GetTransformNormalNormalized(Vector3.Right, ref decalMatrix);

            //	Up plane
            MyPlane upPlane;
            upPlane.Point = intersection.IntersectionPointInObjectSpace;
            upPlane.Normal = MyUtils.GetTransformNormalNormalized(Vector3.Up, ref decalMatrix);

            if (intersection.Entity is MyVoxelMap)
            {
                AddDecalVoxel(decalTexture, decalSize, decalScale, color, alphaBlendByAngle, ref intersection, ref rightPlane, ref upPlane, lightSize, emissivity, decalNormalOffset);
            }
            else
            {
                AddDecalModel(decalTexture, decalSize, decalScale, color, alphaBlendByAngle, ref intersection, ref rightPlane, ref upPlane, lightSize, emissivity, decalNormalOffset);
            }
        }
 //  Find if distance between two intersections is less than "tolerance distance".
 public static bool IsDistanceLessThanTolerance(ref MyIntersectionResultLineTriangleEx? a, ref MyIntersectionResultLineTriangleEx? b,
     float distanceTolerance)
 {
     if (((a == null) && (b != null)) ||
         ((a != null) && (b != null) && (Math.Abs(b.Value.Triangle.Distance - a.Value.Triangle.Distance) <= distanceTolerance)))
     {
         return true;
     }
     else
     {
         //  This will be returned also when ((a == null) && (b == null))
         return false;
     }
 }
        /*public MyLine GetInputLineInWorldSpace()
        {
            if (IntersectionType == MyIntersectionResultLineTriangleType.WITH_VOXEL)
            {
                return InputLineInObjectSpace;
            }
            else
            {
                return new MyLine(
                    MyUtils.GetTransform(InputLineInObjectSpace.From, ref PhysObject.WorldMatrix),
                    MyUtils.GetTransform(InputLineInObjectSpace.To, ref PhysObject.WorldMatrix), true);
            }
        }*/

        //  Find and return closer intersection of these two. If intersection is null then it's not really an intersection.
        public static MyIntersectionResultLineTriangleEx? GetCloserIntersection(ref MyIntersectionResultLineTriangleEx? a, ref MyIntersectionResultLineTriangleEx? b)
        {
            if (((a == null) && (b != null)) ||
                ((a != null) && (b != null) && (b.Value.Triangle.Distance < a.Value.Triangle.Distance)))
            {
                //  If only "b" contains valid intersection, or when it's closer than "a"
                return b;
            }
            else
            {
                //  This will be returned also when ((a == null) && (b == null))
                return a;
            }
        }
Ejemplo n.º 9
0
        //  Calculates intersection of line with any triangleVertexes in this model instance. Closest intersection and intersected triangleVertexes will be returned.
        public virtual bool GetIntersectionWithLine(ref MyLine line, out MyIntersectionResultLineTriangleEx? t, IntersectionFlags flags = IntersectionFlags.ALL_TRIANGLES)
        {
            //Cannot profile because of multithreading
            //MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().StartProfilingBlock("MyEntity.LineIntersection fast");

            bool ret = false;

            t = null;
            MyModel collisionModel = ModelLod0;

            if (collisionModel != null)
            {
                MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().StartProfilingBlock("MyEntity.GetIntersectionWithLine on model");
                MyIntersectionResultLineTriangleEx? result = collisionModel.GetTrianglePruningStructure().GetIntersectionWithLine(this, ref line, flags);
                MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().EndProfilingBlock();
                if (result != null)
                {
                    t = result.Value;
                    ret = true;
                }
            }

            //Cannot profile because of multithreading
            //MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().EndProfilingBlock();
            return ret;

        }
Ejemplo n.º 10
0
        //  Add cockpit decal and all surounding triangles. Method needs intersection, but result of the intersection must be with ideal glass, not any other part of a miner ship.
        public static void Add(MyCockpitGlassDecalTexturesEnum decalTexture, float decalSize, float angle, float alpha,
            ref MyIntersectionResultLineTriangleEx idealIntersection, bool alphaBlendByAngle)
        {
            MyCockpitGlassDecalsBuffer buffer = GetBuffer(decalTexture);

            //	Polomer decalu a scale faktor pre vypocet textury.
            //  Decal size is something as radius of a decal, so when converting from real metres to texture space, we need to divide by 2.0
            float decalScale = 1.0f / decalSize / 2.0f;

            Vector3 rightVector = MyMwcUtils.Normalize(idealIntersection.Triangle.InputTriangle.Vertex0 - idealIntersection.IntersectionPointInObjectSpace);
            Vector3 upVector = MyMwcUtils.Normalize(Vector3.Cross(rightVector, idealIntersection.NormalInObjectSpace));

            //  We create world matrix for the decal and then rotate the matrix, so we can extract rotated right/up vectors/planes for texture coord0 calculations
            Matrix decalMatrix = Matrix.CreateRotationZ(angle) * Matrix.CreateWorld(idealIntersection.IntersectionPointInObjectSpace, idealIntersection.NormalInObjectSpace, upVector);

            //	Right plane
            MyPlane rightPlane;
            rightPlane.Point = idealIntersection.IntersectionPointInObjectSpace;
            rightPlane.Normal = MyUtils.GetTransformNormalNormalized(Vector3.Right, ref decalMatrix);

            //	Up plane
            MyPlane upPlane;
            upPlane.Point = idealIntersection.IntersectionPointInObjectSpace;
            upPlane.Normal = MyUtils.GetTransformNormalNormalized(Vector3.Up, ref decalMatrix);

            float? maxAngle = null;
            if (alphaBlendByAngle == false) maxAngle = MyCockpitGlassDecalsConstants.MAX_NEIGHBOUR_ANGLE;

            BoundingSphere decalSphere = new BoundingSphere(idealIntersection.IntersectionPointInObjectSpace, decalSize);
            m_neighbourTriangles.Clear();
            //idealIntersection.PhysObject.GetTrianglesIntersectingSphere(ref decalSphere, idealIntersection.NormalInObjectSpace, maxAngle, idealIntersection.TriangleHelperIndex, m_neighbourTriangles, buffer.MaxNeighbourTriangles);
            idealIntersection.Entity.GetTrianglesIntersectingSphere(ref decalSphere, idealIntersection.NormalInObjectSpace, maxAngle, m_neighbourTriangles, buffer.MaxNeighbourTriangles);

            int trianglesToAdd = m_neighbourTriangles.Count;// +1;
            if (buffer.CanAddTriangles(trianglesToAdd) == true)
            {
                //  Decal on triangleVertexes we hit
//                buffer.Add(idealIntersection.Triangle.InputTriangle, idealIntersection.NormalInObjectSpace, ref rightPlane, ref upPlane, decalScale, color, alphaBlendByAngle, ref decalSphere);

                //  Create decal for every neighbour triangleVertexes
                for (int i = 0; i < m_neighbourTriangles.Count; i++)
                {
                    buffer.Add(m_neighbourTriangles[i].Vertexes, idealIntersection.NormalInObjectSpace, ref rightPlane, ref upPlane, decalScale, alpha, alphaBlendByAngle, ref decalSphere);
                }
            }
        }
Ejemplo n.º 11
0
        public override bool GetIntersectionWithLine(ref MyLine line, out MyIntersectionResultLineTriangleEx? t, IntersectionFlags flags = IntersectionFlags.ALL_TRIANGLES)
        {
            System.Diagnostics.Debug.Assert(!Closed);

            if (base.GetIntersectionWithLine(ref line, out t))
                return true;

            return m_barrel.GetIntersectionWithLine(ref line, out t);
        }