Inheritance: UnityEngine.Experimental.Rendering.Universal.ShadowCasterGroup2D
Example #1
0
        public void FromGridTilesSprites()
        {
            Tilemaps.Tilemap tilemap = GetComponent <Tilemaps.Tilemap>();

            foreach (var position in tilemap.cellBounds.allPositionsWithin)
            {
                Sprite sprite = tilemap.GetSprite(position);
                if (sprite == null)
                {
                    continue;
                }

                int PhysicsShape = sprite.GetPhysicsShapeCount();
                if (PhysicsShape > 0)
                {
                    GameObject scHost = new GameObject();
                    scHost.name             = "ShadowCaster2D";//TODO give them a better name
                    scHost.transform.parent = transform;

                    List <Vector2> shapeVertices = new List <Vector2>(PhysicsShape);
                    sprite.GetPhysicsShape(0, shapeVertices);
                    ShadowCaster2D sc = scHost.AddComponent <ShadowCaster2D>();
                    sc.shapePath = Array.ConvertAll <Vector2, Vector3>(shapeVertices.ToArray(), vec2To3);

                    RegisterShadowCaster2D(sc);
                    sc.transform.position    = tilemap.GetCellCenterWorld(new Vector3Int(position.x, position.y, 0));
                    sc.useRendererSilhouette = false;
                    sc.selfShadows           = true;

                    sc.enabled = false;
                    sc.mesh    = null;
                    sc.enabled = true;
                }
            }
        }
 public static void RemoveFromShadowCasterGroup(ShadowCaster2D shadowCaster, ShadowCasterGroup2D shadowCasterGroup)
 {
     if (shadowCasterGroup != null)
     {
         shadowCasterGroup.UnregisterShadowCaster2D(shadowCaster);
     }
 }
 public void UnregisterShadowCaster2D(ShadowCaster2D shadowCaster2D)
 {
     if (m_ShadowCasters != null)
     {
         m_ShadowCasters.Remove(shadowCaster2D);
     }
 }
Example #4
0
        public static void SetShadowProjectionGlobals(CommandBuffer cmdBuffer, ShadowCaster2D shadowCaster)
        {
            Vector3   shadowCasterScale = shadowCaster.transform.lossyScale;
            Matrix4x4 shadowMatrix      = Matrix4x4.TRS(shadowCaster.transform.position, shadowCaster.transform.rotation, Vector3.one);

            cmdBuffer.SetGlobalVector(k_ShadowModelScaleID, new Vector3(shadowCasterScale.x, shadowCasterScale.y, shadowCasterScale.z));
            cmdBuffer.SetGlobalMatrix(k_ShadowModelMatrixID, shadowMatrix);
            cmdBuffer.SetGlobalMatrix(k_ShadowModelInvMatrixID, shadowMatrix.inverse);
        }
        public void RegisterShadowCaster2D(ShadowCaster2D shadowCaster2D)
        {
            if (m_ShadowCasters == null)
            {
                m_ShadowCasters = new List <ShadowCaster2D>();
            }

            m_ShadowCasters.Add(shadowCaster2D);
        }
        public static bool AddToShadowCasterGroup(ShadowCaster2D shadowCaster, ref ShadowCasterGroup2D shadowCasterGroup)
        {
            ShadowCasterGroup2D newShadowCasterGroup = FindTopMostCompositeShadowCaster(shadowCaster) as ShadowCasterGroup2D;

            if (newShadowCasterGroup == null)
            {
                newShadowCasterGroup = shadowCaster;
            }

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

            return(false);
        }
Example #7
0
    public void Start()
    {
        tilemapCollider = GetComponent <CompositeCollider2D>();
        GameObject shadowCasterContainer = GameObject.Find("shadow_casters");

        for (int i = 0; i < tilemapCollider.pathCount; i++)
        {
            Vector2[] pathVertices = new Vector2[tilemapCollider.GetPathPointCount(i)];
            tilemapCollider.GetPath(i, pathVertices);
            GameObject        shadowCaster  = new GameObject("shadow_caster_" + i);
            PolygonCollider2D shadowPolygon = (PolygonCollider2D)shadowCaster.AddComponent(typeof(PolygonCollider2D));
            shadowCaster.transform.parent = shadowCasterContainer.transform;
            shadowPolygon.points          = pathVertices;
            shadowPolygon.enabled         = false;
            UnityEngine.Experimental.Rendering.Universal.ShadowCaster2D shadowCasterComponent = shadowCaster.AddComponent <UnityEngine.Experimental.Rendering.Universal.ShadowCaster2D>();
            shadowCasterComponent.selfShadows = true;
        }
    }
        static CompositeShadowCaster2D FindTopMostCompositeShadowCaster(ShadowCaster2D shadowCaster)
        {
            CompositeShadowCaster2D retGroup = null;

            Transform transformToCheck = shadowCaster.transform.parent;

            while (transformToCheck != null)
            {
                CompositeShadowCaster2D currentGroup;
                if (transformToCheck.TryGetComponent <CompositeShadowCaster2D>(out currentGroup))
                {
                    retGroup = currentGroup;
                }

                transformToCheck = transformToCheck.parent;
            }

            return(retGroup);
        }
        static CompositeShadowCaster2D FindTopMostCompositeShadowCaster(ShadowCaster2D shadowCaster)
        {
            CompositeShadowCaster2D retGroup = null;

            Transform transformToCheck = shadowCaster.transform.parent;

            while (transformToCheck != null)
            {
                CompositeShadowCaster2D.CompositeShadowCaster2DsByTransform.TryGetValue(transformToCheck,
                                                                                        out CompositeShadowCaster2D currentGroup);
                if (currentGroup != null)
                {
                    retGroup = currentGroup;
                }

                transformToCheck = transformToCheck.parent;
            }

            return(retGroup);
        }
Example #10
0
        public void FromCompositeCollider2D()
        {
            Tilemaps.TilemapCollider2D tileMapcollider = GetComponent <Tilemaps.TilemapCollider2D>();
            bool tilemapCompositeUsage = tileMapcollider.usedByComposite;

            tileMapcollider.usedByComposite = true;
            CompositeCollider2D compositeCollider = gameObject.AddComponent <CompositeCollider2D>();

            if (compositeCollider.pathCount != 0)
            {
                for (int pathIndex = 0; pathIndex < compositeCollider.pathCount; pathIndex++)
                {
                    Vector2[] pathVertices = new Vector2[compositeCollider.GetPathPointCount(pathIndex)];
                    compositeCollider.GetPath(pathIndex, pathVertices);

                    GameObject scHost = new GameObject();
                    scHost.name             = "ShadowCaster2D";
                    scHost.transform.parent = transform;

                    ShadowCaster2D sc = scHost.AddComponent <ShadowCaster2D>();
                    sc.shapePath = Array.ConvertAll <Vector2, Vector3>(pathVertices, vec2To3);

                    sc.useRendererSilhouette = false;
                    sc.selfShadows           = true;

                    RegisterShadowCaster2D(sc);

                    sc.enabled = false;
                    sc.mesh    = null;
                    sc.enabled = true;
                }
            }
            else
            {
                Debug.Log("Composite collider had no path");
            }

            DestroyImmediate(compositeCollider);
            DestroyImmediate(GetComponent <Rigidbody2D>());
            tileMapcollider.usedByComposite = tilemapCompositeUsage;
        }
Example #11
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);
            }
        }