Ejemplo n.º 1
0
        private static Matrix CreateShadowMatrix(LightId id)
        {
            var     worldMatrix         = MatrixD.CreateTranslation(MyRender11.Environment.Matrices.CameraPosition);
            MatrixD worldViewProjection = worldMatrix * GetSpotlightViewProjection(id);

            return(Matrix.Transpose(worldViewProjection * MyMatrixHelpers.ClipspaceToTexture));
        }
Ejemplo n.º 2
0
        internal static ISrvBindable WriteSpotlightConstants(LightId lid, ref SpotlightConstants data)
        {
            data.Spotlight = m_spotlights[lid.Index].Spotlight;
            data.Spotlight.ShadowsRange   = m_lights.Data[lid.Index].CastsShadowsThisFrame ? m_lights.Data[lid.Index].ShadowsDistance : 0;
            data.Spotlight.Light.Position = m_lights.Data[lid.Index].SpotPosition - MyRender11.Environment.Matrices.CameraPosition;
            data.Spotlight.Direction      = m_lights.Data[lid.Index].Direction;
            data.Spotlight.Up             = m_lights.Data[lid.Index].Up;

            float ratio = (float)Math.Sqrt(1 - data.Spotlight.ApertureCos * data.Spotlight.ApertureCos) / data.Spotlight.ApertureCos;
            float h     = ratio * data.Spotlight.Light.Range;

            Matrix viewProjAt0 = MyRender11.Environment.Matrices.ViewProjectionAt0;

            if (MyStereoRender.Enable)
            {
                if (MyStereoRender.RenderRegion == MyStereoRegion.LEFT)
                {
                    viewProjAt0 = MyStereoRender.EnvMatricesLeftEye.ViewProjectionAt0;
                }
                if (MyStereoRender.RenderRegion == MyStereoRegion.RIGHT)
                {
                    viewProjAt0 = MyStereoRender.EnvMatricesRightEye.ViewProjectionAt0;
                }
            }
            data.ProxyWorldViewProj = Matrix.Transpose(Matrix.CreateScale(2 * h, 2 * h, data.Spotlight.Light.Range) *
                                                       Matrix.CreateWorld(data.Spotlight.Light.Position, data.Spotlight.Direction, data.Spotlight.Up) *
                                                       viewProjAt0);

            data.ShadowMatrix = CreateShadowMatrix(lid);

            return(m_spotlights[lid.Index].ReflectorTexture);
        }
Ejemplo n.º 3
0
        private static void CheckDirty(LightId light)
        {
            var enabled = m_pointlights[light.Index].Enabled;

            if (enabled)
            {
                var difference = Vector3D.RectangularDistance(ref m_pointlights[light.Index].LastBvhUpdatePosition, ref m_lights.Data[light.Index].PointPosition);

                bool dirty = difference > MOVE_TOLERANCE;

                if (dirty)
                {
                    m_dirtyPointlights.Add(light);
                }
            }

            enabled = m_spotlights[light.Index].Enabled;
            if (enabled)
            {
                var positionDifference = Vector3D.RectangularDistance(ref m_spotlights[light.Index].LastBvhUpdatePosition, ref m_lights.Data[light.Index].SpotPosition);
                var upDifference       = Vector3.DistanceSquared(m_spotlights[light.Index].LastBvhUpdateUp, m_lights.Data[light.Index].Up);
                var dirDifference      = Vector3.DistanceSquared(m_spotlights[light.Index].LastBvhUpdateDir, m_lights.Data[light.Index].Direction);

                bool dirty = positionDifference > MOVE_TOLERANCE || upDifference > DIR_TOLARENCE || dirDifference > DIR_TOLARENCE;

                if (dirty)
                {
                    m_dirtySpotlights.Add(light);
                }
            }
        }
Ejemplo n.º 4
0
        internal static LightId Create(uint GID)
        {
            var id = new LightId {
                Index = m_lights.Allocate()
            };

            m_lights.Data[id.Index] = new MyLightInfo
            {
                FlareId = FlareId.NULL
            };

            MyArrayHelpers.Reserve(ref m_pointlights, id.Index + 1);
            MyArrayHelpers.Reserve(ref m_spotlights, id.Index + 1);

            var p1 = new MyPointlightInfo
            {
                LastBvhUpdatePosition = Vector3.PositiveInfinity,
                BvhProxyId            = -1
            };

            m_pointlights[id.Index] = p1;

            var p2 = new MySpotlightInfo
            {
                LastBvhUpdatePosition = Vector3.PositiveInfinity,
                BvhProxyId            = -1
            };

            m_spotlights[id.Index] = p2;

            m_idIndex[GID] = id;

            return(id);
        }
Ejemplo n.º 5
0
        public static void UpdateFlare(LightId id, ref MyFlareDesc glare)
        {
            var oldFlareId = m_lights.Data[id.Index].FlareId;

            m_lights.Data[id.Index].FlareId = MyFlareRenderer.Set(m_lights.Data[id.Index].FlareId, glare);
            if (m_lights.Data[id.Index].FlareId != FlareId.NULL && !m_spotlights[id.Index].Enabled && !m_pointlights[id.Index].Enabled)
            {
                DistantFlaresWithoutLight.Add(id);
            }
            else if (oldFlareId != FlareId.NULL)
            {
                DistantFlaresWithoutLight.Remove(id);
            }
        }
Ejemplo n.º 6
0
        internal static MatrixD GetSpotlightViewProjection(LightId id)
        {
            if (m_spotlights[id.Index].ViewProjectionDirty)
            {
                m_spotlights[id.Index].ViewProjectionDirty = false;

                var nearPlaneDistance = 0.5f;
                var viewMatrix        = MatrixD.CreateLookAt(m_lights.Data[id.Index].SpotPosition,
                                                             m_lights.Data[id.Index].SpotPosition + m_lights.Data[id.Index].Direction, m_lights.Data[id.Index].Up);
                var projectionMatrix = MatrixD.CreatePerspectiveFieldOfView((float)(Math.Acos(m_spotlights[id.Index].Spotlight.ApertureCos) * 2), 1.0f, nearPlaneDistance, Math.Max(id.ShadowDistance, nearPlaneDistance));
                m_spotlights[id.Index].ViewProjection = viewMatrix * projectionMatrix;
            }
            return(m_spotlights[id.Index].ViewProjection);
        }
Ejemplo n.º 7
0
        internal static void UpdatePointlight(LightId light, bool enabled, float intensity, MyLightLayout data)
        {
            data.Color *= intensity;

            if (enabled && (Math.Abs(data.Range - m_pointlights[light.Index].Light.Range) > 0.1f ||
                            m_pointlights[light.Index].BvhProxyId == -1))
            {
                m_dirtyPointlights.Add(light);
            }

            if (m_pointlights[light.Index].Enabled != enabled)
            {
                m_pointlights[light.Index].Enabled = enabled;
                m_dirtyPointlights.Add(light);
            }

            m_pointlights[light.Index].Light = data;
        }
Ejemplo n.º 8
0
        private static int UpdateBvh(MyDynamicAABBTreeD bvh, LightId lid, bool enabled, int proxy, ref BoundingBoxD aabb)
        {
            if (enabled && proxy == -1)
            {
                return(bvh.AddProxy(ref aabb, lid, 0));
            }
            else if (enabled && proxy != -1)
            {
                bvh.MoveProxy(proxy, ref aabb, Vector3.Zero);
                return(proxy);
            }
            else if (proxy != -1)
            {
                bvh.RemoveProxy(proxy);
            }

            return(-1);
        }
Ejemplo n.º 9
0
        private static void Remove(uint GID, LightId id)
        {
            m_idIndex.Remove(GID);

            if (m_pointlights[id.Index].BvhProxyId != -1)
            {
                PointlightsBvh.RemoveProxy(m_pointlights[id.Index].BvhProxyId);
            }

            if (m_spotlights[id.Index].BvhProxyId != -1)
            {
                SpotlightsBvh.RemoveProxy(m_spotlights[id.Index].BvhProxyId);
            }
            MyFlareRenderer.Remove(id.FlareId);
            m_dirtyPointlights.Remove(id);
            m_dirtySpotlights.Remove(id);
            m_ignoredShadowEntities.Remove(id);
            m_lights.Free(id.Index);
        }
Ejemplo n.º 10
0
 internal static void UpdateEntity(LightId light, ref MyLightInfo info)
 {
     info.FlareId            = m_lights.Data[light.Index].FlareId;
     info.LocalSpotPosition  = info.SpotPosition;
     info.LocalPointPosition = info.PointPosition;
     info.LocalDirection     = info.Direction;
     info.LocalUp            = info.Up;
     if (light.ParentGID != -1)
     {
         info.SpotPosition  = m_lights.Data[light.Index].SpotPosition;
         info.PointPosition = m_lights.Data[light.Index].PointPosition;
         info.Direction     = m_lights.Data[light.Index].Direction;
         info.Up            = m_lights.Data[light.Index].Up;
     }
     m_lights.Data[light.Index] = info;
     if (light.ParentGID == -1)
     {
         m_spotlights[light.Index].ViewProjectionDirty = true;
     }
     CheckDirty(light);
 }
Ejemplo n.º 11
0
        internal static void WritePointlightConstants(LightId lid, ref MyPointlightConstants data)
        {
            if (lid == LightId.NULL)
            {
                data = default(MyPointlightConstants);
                return;
            }

            data.Light          = m_pointlights[lid.Index].Light;
            data.Light.Position = Vector3.Transform(m_lights.Data[lid.Index].PointPosition - MyRender11.Environment.Matrices.CameraPosition, ref MyRender11.Environment.Matrices.ViewAt0);
            if (MyStereoRender.Enable)
            {
                if (MyStereoRender.RenderRegion == MyStereoRegion.LEFT)
                {
                    data.Light.Position = Vector3.Transform(m_lights.Data[lid.Index].PointPosition - MyStereoRender.EnvMatricesLeftEye.CameraPosition, ref MyStereoRender.EnvMatricesLeftEye.ViewAt0);
                }
                else if (MyStereoRender.RenderRegion == MyStereoRegion.RIGHT)
                {
                    data.Light.Position = Vector3.Transform(m_lights.Data[lid.Index].PointPosition - MyStereoRender.EnvMatricesRightEye.CameraPosition, ref MyStereoRender.EnvMatricesRightEye.ViewAt0);
                }
            }
        }
Ejemplo n.º 12
0
        internal static void UpdateSpotlight(LightId light, bool enabled, float intensity, float reflectorConeMaxAnglecos, MySpotLightLayout data, ISrvBindable reflectorTexture)
        {
            float coneMaxAngleCos = 1 - reflectorConeMaxAnglecos;

            coneMaxAngleCos = (float)Math.Min(Math.Max(coneMaxAngleCos, 0.01), 0.99f);

            data.Light.Color *= intensity;
            data.ApertureCos  = coneMaxAngleCos;

            var info = m_spotlights[light.Index];

            m_spotlights[light.Index].Spotlight        = data;
            m_spotlights[light.Index].ReflectorTexture = reflectorTexture;

            if (m_spotlights[light.Index].Enabled != enabled ||
                enabled && (Math.Abs(info.Spotlight.Light.Range - data.Light.Range) > 0.1f ||
                            Math.Abs(info.Spotlight.ApertureCos - data.ApertureCos) > 0.01f ||
                            m_spotlights[light.Index].BvhProxyId == -1))
            {
                m_spotlights[light.Index].Enabled = enabled;
                m_dirtySpotlights.Add(light);
                m_spotlights[light.Index].ViewProjectionDirty = true;
            }
        }
Ejemplo n.º 13
0
 public static void ClearIgnoredEntities(LightId id)
 {
     m_ignoredShadowEntities.Remove(id);
 }
Ejemplo n.º 14
0
 public static HashSet <uint> GetEntitiesIgnoringShadow(LightId id)
 {
     return(m_ignoredShadowEntities.ContainsKey(id) ? m_ignoredShadowEntities[id] : null);
 }
Ejemplo n.º 15
0
 internal static void SetCastsShadowsThisFrame(LightId id, bool b)
 {
     m_lights.Data[id.Index].CastsShadowsThisFrame = b;
 }