Пример #1
0
        public void Update()
        {
            Renderer renderer = GetComponent<Renderer>();
            m_HasRenderer = renderer != null;

            bool rebuildMesh = LightUtility.CheckForChange(m_ShapePathHash, ref m_PreviousPathHash);
            if (rebuildMesh)
                ShadowUtility.GenerateShadowMesh(m_Mesh, m_ShapePath);

            m_PreviousShadowCasterGroup = m_ShadowCasterGroup;
            bool addedToNewGroup = LightUtility.AddToShadowCasterGroup(this, ref m_ShadowCasterGroup);
            if (addedToNewGroup && m_ShadowCasterGroup != null)
            {
                if (m_PreviousShadowCasterGroup == this)
                    ShadowCasterGroup2DManager.RemoveGroup(this);

                LightUtility.RemoveFromShadowCasterGroup(this, m_PreviousShadowCasterGroup);
                if (m_ShadowCasterGroup == this)
                    ShadowCasterGroup2DManager.AddGroup(this);
            }

            if (LightUtility.CheckForChange(m_ShadowGroup, ref m_PreviousShadowGroup))
            {
                ShadowCasterGroup2DManager.RemoveGroup(this);
                ShadowCasterGroup2DManager.AddGroup(this);
            }

            if (LightUtility.CheckForChange(m_CastsShadows, ref m_PreviousCastsShadows))
            {
                if(m_CastsShadows)
                    ShadowCasterGroup2DManager.AddGroup(this);
                else
                    ShadowCasterGroup2DManager.RemoveGroup(this);
            }
        }
 public static void RemoveGroup(ShadowCasterGroup2D group)
 {
     if (group != null && s_ShadowCasterGroups != null)
     {
         LightUtility.RemoveShadowCasterGroupFromList(group, s_ShadowCasterGroups);
     }
 }
Пример #3
0
        public void Update()
        {
            if (player != null && Vector2.Distance(this.transform.position, player.transform.position) > 8)
            {
                return;
            }

            Renderer renderer = GetComponent <Renderer>();

            m_HasRenderer = renderer != null;

            bool rebuildMesh = LightUtility.CheckForChange(m_ShapePathHash, ref m_PreviousPathHash);

            if (rebuildMesh)
            {
                ShadowUtility.GenerateShadowMesh(m_Mesh, m_ShapePath);
            }

            m_PreviousShadowCasterGroup = m_ShadowCasterGroup;
            bool addedToNewGroup = ShadowCasterGroup2DManager.AddToShadowCasterGroup(this, ref m_ShadowCasterGroup);

            if (addedToNewGroup && m_ShadowCasterGroup != null)
            {
                if (m_PreviousShadowCasterGroup == this)
                {
                    ShadowCasterGroup2DManager.RemoveGroup(this);
                }

                ShadowCasterGroup2DManager.RemoveFromShadowCasterGroup(this, m_PreviousShadowCasterGroup);
                if (m_ShadowCasterGroup == this)
                {
                    ShadowCasterGroup2DManager.AddGroup(this);
                }
            }

            if (LightUtility.CheckForChange(m_ShadowGroup, ref m_PreviousShadowGroup))
            {
                ShadowCasterGroup2DManager.RemoveGroup(this);
                ShadowCasterGroup2DManager.AddGroup(this);
            }

            if (LightUtility.CheckForChange(m_CastsShadows, ref m_PreviousCastsShadows))
            {
                if (m_CastsShadows)
                {
                    ShadowCasterGroup2DManager.AddGroup(this);
                }
                else
                {
                    ShadowCasterGroup2DManager.RemoveGroup(this);
                }
            }
        }
        public static void AddGroup(ShadowCasterGroup2D group)
        {
            if (group == null)
            {
                return;
            }

            if (s_ShadowCasterGroups == null)
            {
                s_ShadowCasterGroups = new List <ShadowCasterGroup2D>();
            }

            LightUtility.AddShadowCasterGroupToList(group, s_ShadowCasterGroups);
        }
        internal List <Vector2> GetFalloffShape()
        {
            var shape        = new List <Vector2>();
            var extrusionDir = LightUtility.GetFalloffShape(m_ShapePath);

            for (var i = 0; i < m_ShapePath.Length; i++)
            {
                shape.Add(new Vector2
                {
                    x = m_ShapePath[i].x + this.shapeLightFalloffSize * extrusionDir[i].x,
                    y = m_ShapePath[i].y + this.shapeLightFalloffSize * extrusionDir[i].y
                });
            }
            return(shape);
        }
Пример #6
0
        public void Update()
        {
            Renderer renderer;

            m_HasRenderer = TryGetComponent <Renderer>(out renderer);

            bool rebuildMesh = LightUtility.CheckForChange(m_ShapePathHash, ref m_PreviousPathHash);

            if (rebuildMesh)
            {
                m_ProjectedBoundingSphere = ShadowUtility.GenerateShadowMesh(m_Mesh, m_ShapePath);
            }

            m_PreviousShadowCasterGroup = m_ShadowCasterGroup;
            bool addedToNewGroup = ShadowCasterGroup2DManager.AddToShadowCasterGroup(this, ref m_ShadowCasterGroup);

            if (addedToNewGroup && m_ShadowCasterGroup != null)
            {
                if (m_PreviousShadowCasterGroup == this)
                {
                    ShadowCasterGroup2DManager.RemoveGroup(this);
                }

                ShadowCasterGroup2DManager.RemoveFromShadowCasterGroup(this, m_PreviousShadowCasterGroup);
                if (m_ShadowCasterGroup == this)
                {
                    ShadowCasterGroup2DManager.AddGroup(this);
                }
            }

            if (LightUtility.CheckForChange(m_ShadowGroup, ref m_PreviousShadowGroup))
            {
                ShadowCasterGroup2DManager.RemoveGroup(this);
                ShadowCasterGroup2DManager.AddGroup(this);
            }

            if (LightUtility.CheckForChange(m_CastsShadows, ref m_PreviousCastsShadows))
            {
                if (m_CastsShadows)
                {
                    ShadowCasterGroup2DManager.AddGroup(this);
                }
                else
                {
                    ShadowCasterGroup2DManager.RemoveGroup(this);
                }
            }
        }
Пример #7
0
        public void Update()
        {
            bool rebuildMesh = LightUtility.CheckForChange(m_ShapePathHash, ref m_PreviousPathHash);

            if (rebuildMesh)
            {
                ShadowUtility.GenerateShadowMesh(m_Mesh, m_ShapePath);
            }

            m_PreviousShadowCasterGroup = m_ShadowCasterGroup;
            bool addedToNewGroup = LightUtility.AddToShadowCasterGroup(this, ref m_ShadowCasterGroup);

            if (addedToNewGroup && m_ShadowCasterGroup != null)
            {
                if (m_PreviousShadowCasterGroup == this)
                {
                    ShadowCasterGroup2DManager.RemoveGroup(this);
                }

                LightUtility.RemoveFromShadowCasterGroup(this, m_PreviousShadowCasterGroup);
                if (m_ShadowCasterGroup == this)
                {
                    ShadowCasterGroup2DManager.AddGroup(this);
                }
            }

            if (LightUtility.CheckForChange(m_ShadowGroup, ref m_PreviousShadowGroup))
            {
                ShadowCasterGroup2DManager.RemoveGroup(this);
                ShadowCasterGroup2DManager.AddGroup(this);
            }

            if (LightUtility.CheckForChange(m_CastsShadows, ref m_PreviousCastsShadows))
            {
                if (m_CastsShadows)
                {
                    ShadowCasterGroup2DManager.AddGroup(this);
                }
                else
                {
                    ShadowCasterGroup2DManager.RemoveGroup(this);
                }
            }
        }
 protected void OnDisable()
 {
     LightUtility.RemoveFromShadowCasterGroup(this, m_ShadowCasterGroup);
 }
Пример #9
0
 internal List<Vector2> GetFalloffShape()
 {
     return LightUtility.GetOutlinePath(m_ShapePath, m_ShapeLightFalloffSize);
 }
Пример #10
0
        static private void RenderShadows(CommandBuffer cmdBuffer, int layerToRender, Light2D light, float shadowIntensity, RenderTargetIdentifier renderTexture, RenderTargetIdentifier depthTexture)
        {
            cmdBuffer.SetGlobalFloat("_ShadowIntensity", 1 - light.shadowIntensity);
            cmdBuffer.SetGlobalFloat("_ShadowVolumeIntensity", 1 - light.shadowVolumeIntensity);

            if (shadowIntensity > 0)
            {
                CreateShadowRenderTexture(cmdBuffer, light.blendStyleIndex);

                cmdBuffer.SetRenderTarget(s_ShadowsRenderTarget.Identifier()); // This isn't efficient if this light doesn't cast shadow.
                cmdBuffer.ClearRenderTarget(true, true, Color.black);

                BoundingSphere lightBounds  = light.GetBoundingSphere(); // Gets the local bounding sphere...
                float          shadowRadius = 1.42f * lightBounds.radius;

                cmdBuffer.SetGlobalVector("_LightPos", light.transform.position);
                cmdBuffer.SetGlobalFloat("_ShadowRadius", shadowRadius);

                Material shadowMaterial                       = GetShadowMaterial(1);
                Material removeSelfShadowMaterial             = GetRemoveSelfShadowMaterial(1);
                List <ShadowCasterGroup2D> shadowCasterGroups = ShadowCasterGroup2DManager.shadowCasterGroups;
                if (shadowCasterGroups != null && shadowCasterGroups.Count > 0)
                {
                    int previousShadowGroupIndex = -1;
                    int incrementingGroupIndex   = 0;
                    for (int group = 0; group < shadowCasterGroups.Count; group++)
                    {
                        ShadowCasterGroup2D shadowCasterGroup = shadowCasterGroups[group];

                        List <ShadowCaster2D> shadowCasters = shadowCasterGroup.GetShadowCasters();

                        int shadowGroupIndex = shadowCasterGroup.GetShadowGroup();
                        if (LightUtility.CheckForChange(shadowGroupIndex, ref previousShadowGroupIndex) || shadowGroupIndex == 0)
                        {
                            incrementingGroupIndex++;
                            shadowMaterial           = GetShadowMaterial(incrementingGroupIndex);
                            removeSelfShadowMaterial = GetRemoveSelfShadowMaterial(incrementingGroupIndex);
                        }

                        if (shadowCasters != null)
                        {
                            // Draw the shadow casting group first, then draw the silhouttes..
                            for (int i = 0; i < shadowCasters.Count; i++)
                            {
                                ShadowCaster2D shadowCaster = (ShadowCaster2D)shadowCasters[i];

                                if (shadowCaster != null && shadowMaterial != null && shadowCaster.IsShadowedLayer(layerToRender))
                                {
                                    if (shadowCaster.castsShadows)
                                    {
                                        cmdBuffer.DrawMesh(shadowCaster.mesh, shadowCaster.transform.localToWorldMatrix, shadowMaterial);
                                    }
                                }
                            }

                            for (int i = 0; i < shadowCasters.Count; i++)
                            {
                                ShadowCaster2D shadowCaster = (ShadowCaster2D)shadowCasters[i];

                                if (shadowCaster != null && shadowMaterial != null && shadowCaster.IsShadowedLayer(layerToRender))
                                {
                                    if (shadowCaster.useRendererSilhouette)
                                    {
                                        Renderer renderer = shadowCaster.GetComponent <Renderer>();
                                        if (renderer != null)
                                        {
                                            if (!shadowCaster.selfShadows)
                                            {
                                                cmdBuffer.DrawRenderer(renderer, removeSelfShadowMaterial);
                                            }
                                            else
                                            {
                                                cmdBuffer.DrawRenderer(renderer, shadowMaterial, 0, 1);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (!shadowCaster.selfShadows)
                                        {
                                            Matrix4x4 meshMat = shadowCaster.transform.localToWorldMatrix;
                                            cmdBuffer.DrawMesh(shadowCaster.mesh, meshMat, removeSelfShadowMaterial);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                ReleaseShadowRenderTexture(cmdBuffer);
                cmdBuffer.SetRenderTarget(renderTexture, depthTexture);
            }
        }
Пример #11
0
        public static void RenderShadows(IRenderPass2D pass, RenderingData renderingData, CommandBuffer cmdBuffer, int layerToRender, Light2D light, float shadowIntensity, RenderTargetIdentifier renderTexture)
        {
            cmdBuffer.SetRenderTarget(renderTexture, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.DontCare);
            cmdBuffer.ClearRenderTarget(true, true, Color.black);  // clear stencil

            var shadowRadius = 1.42f * light.boundingSphere.radius;

            cmdBuffer.SetGlobalVector(k_LightPosID, light.transform.position);
            cmdBuffer.SetGlobalFloat(k_ShadowRadiusID, shadowRadius);

            var shadowMaterial           = pass.rendererData.GetShadowMaterial(1);
            var removeSelfShadowMaterial = pass.rendererData.GetRemoveSelfShadowMaterial(1);
            var shadowCasterGroups       = ShadowCasterGroup2DManager.shadowCasterGroups;

            if (shadowCasterGroups != null && shadowCasterGroups.Count > 0)
            {
                var previousShadowGroupIndex = -1;
                var incrementingGroupIndex   = 0;
                for (var group = 0; group < shadowCasterGroups.Count; group++)
                {
                    var shadowCasterGroup = shadowCasterGroups[group];
                    var shadowCasters     = shadowCasterGroup.GetShadowCasters();

                    var shadowGroupIndex = shadowCasterGroup.GetShadowGroup();
                    if (LightUtility.CheckForChange(shadowGroupIndex, ref previousShadowGroupIndex) || shadowGroupIndex == 0)
                    {
                        incrementingGroupIndex++;
                        shadowMaterial           = pass.rendererData.GetShadowMaterial(incrementingGroupIndex);
                        removeSelfShadowMaterial = pass.rendererData.GetRemoveSelfShadowMaterial(incrementingGroupIndex);
                    }

                    if (shadowCasters != null)
                    {
                        // Draw the shadow casting group first, then draw the silhouettes..
                        for (var i = 0; i < shadowCasters.Count; i++)
                        {
                            var shadowCaster = shadowCasters[i];

                            if (shadowCaster != null && shadowMaterial != null && shadowCaster.IsShadowedLayer(layerToRender))
                            {
                                if (shadowCaster.castsShadows)
                                {
                                    cmdBuffer.DrawMesh(shadowCaster.mesh, shadowCaster.transform.localToWorldMatrix, shadowMaterial);
                                }
                            }
                        }

                        for (var i = 0; i < shadowCasters.Count; i++)
                        {
                            var shadowCaster = shadowCasters[i];

                            if (shadowCaster != null && shadowMaterial != null && shadowCaster.IsShadowedLayer(layerToRender))
                            {
                                if (shadowCaster.useRendererSilhouette)
                                {
                                    var renderer = shadowCaster.GetComponent <Renderer>();
                                    if (renderer != null)
                                    {
                                        if (!shadowCaster.selfShadows)
                                        {
                                            cmdBuffer.DrawRenderer(renderer, removeSelfShadowMaterial);
                                        }
                                        else
                                        {
                                            cmdBuffer.DrawRenderer(renderer, shadowMaterial, 0, 1);
                                        }
                                    }
                                }
                                else
                                {
                                    if (!shadowCaster.selfShadows)
                                    {
                                        var meshMat = shadowCaster.transform.localToWorldMatrix;
                                        cmdBuffer.DrawMesh(shadowCaster.mesh, meshMat, removeSelfShadowMaterial);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }