コード例 #1
0
 public static void RemoveFromShadowCasterGroup(LightReactor2D shadowCaster, ShadowCasterGroup2D shadowCasterGroup)
 {
     if (shadowCasterGroup != null)
     {
         shadowCasterGroup.UnregisterShadowCaster2D(shadowCaster);
     }
 }
コード例 #2
0
 public void UnregisterShadowCaster2D(LightReactor2D shadowCaster2D)
 {
     if (m_ShadowCasters != null)
     {
         m_ShadowCasters.Remove(shadowCaster2D);
     }
 }
コード例 #3
0
        public void RegisterShadowCaster2D(LightReactor2D shadowCaster2D)
        {
            if (m_ShadowCasters == null)
            {
                m_ShadowCasters = new List <LightReactor2D>();
            }

            m_ShadowCasters.Add(shadowCaster2D);
        }
コード例 #4
0
        public static bool AddToShadowCasterGroup(LightReactor2D shadowCaster, ref ShadowCasterGroup2D shadowCasterGroup)
        {
            ShadowCasterGroup2D newShadowCasterGroup = FindTopMostCompositeLightReactor(shadowCaster) as ShadowCasterGroup2D;

            if (newShadowCasterGroup == null)
            {
                newShadowCasterGroup = shadowCaster.GetComponent <LightReactor2D>();
            }

            if (newShadowCasterGroup != null && shadowCasterGroup != newShadowCasterGroup)
            {
                newShadowCasterGroup.RegisterShadowCaster2D(shadowCaster);
                shadowCasterGroup = newShadowCasterGroup;
                return(true);
            }

            return(false);
        }
コード例 #5
0
        static CompositeLightReactor2D FindTopMostCompositeLightReactor(LightReactor2D shadowCaster)
        {
            CompositeLightReactor2D retGroup = null;

            Transform transformToCheck = shadowCaster.transform.parent;

            while (transformToCheck != null)
            {
                CompositeLightReactor2D currentGroup = transformToCheck.GetComponent <CompositeLightReactor2D>();
                if (currentGroup != null)
                {
                    retGroup = currentGroup;
                }

                transformToCheck = transformToCheck.parent;
            }

            return(retGroup);
        }
コード例 #6
0
        static private void RenderShadows(CommandBuffer cmdBuffer, int layerToRender, Light2D light, float shadowIntensity, RenderTargetIdentifier renderTexture)
        {
            // Render light's shadows
            cmdBuffer.SetRenderTarget(s_ShadowsRenderTarget.Identifier()); // This isn't efficient if this light doesn't cast shadow.
            cmdBuffer.ClearRenderTarget(true, true, Color.black);

            cmdBuffer.SetGlobalFloat("_ShadowIntensity", 1 - light.shadowIntensity);
            cmdBuffer.SetGlobalFloat("_ShadowVolumeIntensity", 1 - light.shadowVolumeIntensity);

            // TODO: We need an alternate (more efficient) code path if the light has a shadowIntensity of 0
            if (shadowIntensity > 0)
            {
                BoundingSphere lightBounds = light.GetBoundingSphere(); // Gets the local bounding sphere...

                cmdBuffer.SetGlobalVector("_LightPos", light.transform.position);
                cmdBuffer.SetGlobalFloat("_LightRadius", lightBounds.radius);

                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 <LightReactor2D> 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++)
                            {
                                LightReactor2D shadowCaster = (LightReactor2D)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++)
                            {
                                LightReactor2D shadowCaster = (LightReactor2D)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);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            cmdBuffer.SetRenderTarget(renderTexture);
        }