Example #1
0
    static public void Shadow(LightingBuffer2D buffer, LightingTilemapCollider2D id, float lightSizeSquared, float z)
    {
        if (id.colliderType == LightingTilemapCollider2D.ColliderType.None)
        {
            return;
        }

        if (id.colliderType == LightingTilemapCollider2D.ColliderType.Collider)
        {
            // Adjust Color?
            Fill_Component(buffer, id, lightSizeSquared, z);
            Penumbra_Component(buffer, id, lightSizeSquared, z);

            return;
        }

        if (id.map == null)
        {
            return;
        }

        manager = LightingManager2D.Get();

        SetupLocation(buffer, id);
        CalculatePenumbra();

        bool penumbra  = manager.drawPenumbra;
        bool drawAbove = buffer.lightSource.whenInsideCollider == LightingSource2D.WhenInsideCollider.DrawAbove;

        for (int x = newPositionInt.x - sizeInt; x < newPositionInt.x + sizeInt; x++)
        {
            for (int y = newPositionInt.y - sizeInt; y < newPositionInt.y + sizeInt; y++)
            {
                if (x < 0 || y < 0)
                {
                    continue;
                }

                if (x >= id.area.size.x || y >= id.area.size.y)
                {
                    continue;
                }

                tile = id.map[x, y];
                if (tile == null)
                {
                    continue;
                }

                polygons = tile.GetPolygon(id);

                if (polygons == null || polygons.Count < 1)
                {
                    continue;
                }

                polyOffset.x = x + tilemapOffset.x;
                polyOffset.y = y + tilemapOffset.y;

                if (id.mapType == LightingTilemapCollider2D.MapType.SuperTilemapEditor)
                {
                    polyOffset.x -= id.area.size.x / 2;
                    polyOffset.y -= id.area.size.y / 2;
                }

                polyOffset.x *= scale.x;
                polyOffset.y *= scale.y;

                polyOffset2.x = (float)polyOffset.x;
                polyOffset2.y = (float)polyOffset.y;

                if (LightingManager2D.culling && Vector2.Distance(polyOffset2, buffer.lightSource.transform.position) > 2 + buffer.lightSource.lightSize)
                {
                    LightingDebug.culled++;
                    continue;
                }

                polyOffset.x += offset.x;
                polyOffset.y += offset.y;

                inverseOffset.x = -polyOffset.x;
                inverseOffset.y = -polyOffset.y;

                pairList2 = tile.GetPairs(id);

                for (int i = 0; i < polygons.Count; i++)
                {
                    polygon = polygons[i];

                    if (drawAbove && polygon.PointInPoly(inverseOffset))
                    {
                        continue;
                    }

                    LightingDebug.shadowGenerations++;

                    pairList = pairList2[i];

                    GL.Color(Color.black);
                    for (int e = 0; e < pairList.Count; e++)
                    {
                        p = pairList[e];

                        vA.x = p.A.x + polyOffset.x;
                        vA.y = p.A.y + polyOffset.y;

                        vB.x = p.B.x + polyOffset.x;
                        vB.y = p.B.y + polyOffset.y;

                        vC.x = p.A.x + polyOffset.x;
                        vC.y = p.A.y + polyOffset.y;

                        vD.x = p.B.x + polyOffset.x;
                        vD.y = p.B.y + polyOffset.y;

                        rot   = System.Math.Atan2(vA.y - zero.y, vA.x - zero.x);
                        vA.x += System.Math.Cos(rot) * lightSizeSquared;
                        vA.y += System.Math.Sin(rot) * lightSizeSquared;

                        rot   = System.Math.Atan2(vB.y - zero.y, vB.x - zero.x);
                        vB.x += System.Math.Cos(rot) * lightSizeSquared;
                        vB.y += System.Math.Sin(rot) * lightSizeSquared;

                        GL.TexCoord3(Max2DMatrix.c_x, Max2DMatrix.c_y, 0);
                        GL.Vertex3((float)vC.x, (float)vC.y, z);

                        GL.TexCoord3(Max2DMatrix.c_x, Max2DMatrix.c_y, 0);
                        GL.Vertex3((float)vD.x, (float)vD.y, z);

                        GL.TexCoord3(Max2DMatrix.c_x, Max2DMatrix.c_y, 0);
                        GL.Vertex3((float)vA.x, (float)vA.y, z);


                        GL.TexCoord3(Max2DMatrix.c_x, Max2DMatrix.c_y, 0);
                        GL.Vertex3((float)vA.x, (float)vA.y, z);

                        GL.TexCoord3(Max2DMatrix.c_x, Max2DMatrix.c_y, 0);
                        GL.Vertex3((float)vB.x, (float)vB.y, z);

                        GL.TexCoord3(Max2DMatrix.c_x, Max2DMatrix.c_y, 0);
                        GL.Vertex3((float)vD.x, (float)vD.y, z);
                    }

                    if (penumbra)
                    {
                        GL.Color(Color.white);
                        for (int e = 0; e < pairList.Count; e++)
                        {
                            p = pairList[e];

                            vA.x = p.A.x + polyOffset.x;
                            vA.y = p.A.y + polyOffset.y;

                            pA.x = p.A.x + polyOffset.x;
                            pA.y = p.A.y + polyOffset.y;

                            pB.x = p.B.x + polyOffset.x;
                            pB.y = p.B.y + polyOffset.y;

                            vB.x = p.B.x + polyOffset.x;
                            vB.y = p.B.y + polyOffset.y;

                            vC.x = p.A.x + polyOffset.x;
                            vC.y = p.A.y + polyOffset.y;

                            vD.x = p.B.x + polyOffset.x;
                            vD.y = p.B.y + polyOffset.y;

                            angleA = (float)System.Math.Atan2(vA.y - zero.y, vA.x - zero.x);
                            angleB = (float)System.Math.Atan2(vB.y - zero.y, vB.x - zero.x);

                            vA.x += System.Math.Cos(angleA) * lightSizeSquared;
                            vA.y += System.Math.Sin(angleA) * lightSizeSquared;

                            vB.x += System.Math.Cos(angleB) * lightSizeSquared;
                            vB.y += System.Math.Sin(angleB) * lightSizeSquared;

                            rot   = angleA - Mathf.Deg2Rad * buffer.lightSource.occlusionSize;
                            pA.x += System.Math.Cos(rot) * lightSizeSquared;
                            pA.y += System.Math.Sin(rot) * lightSizeSquared;

                            rot   = angleB + Mathf.Deg2Rad * buffer.lightSource.occlusionSize;
                            pB.x += System.Math.Cos(rot) * lightSizeSquared;
                            pB.y += System.Math.Sin(rot) * lightSizeSquared;

                            GL.TexCoord3(uvRectX, uvRectY, 0);
                            GL.Vertex3((float)vC.x, (float)vC.y, z);

                            GL.TexCoord3(uvRectWidth, uvRectHeight, 0);
                            GL.Vertex3((float)vA.x, (float)vA.y, z);

                            GL.TexCoord3((float)uvRectX, uvRectHeight, 0);
                            GL.Vertex3((float)pA.x, (float)pA.y, z);


                            GL.TexCoord3(uvRectX, uvRectY, 0);
                            GL.Vertex3((float)vD.x, (float)vD.y, z);

                            GL.TexCoord3(uvRectWidth, uvRectY, 0);
                            GL.Vertex3((float)vB.x, (float)vB.y, z);

                            GL.TexCoord3(uvRectX, uvRectHeight, 0);
                            GL.Vertex3((float)pB.x, (float)pB.y, z);
                        }
                    }
                }
            }
        }
    }
Example #2
0
    static public void DrawShadow(LightingBuffer2D buffer, LightingTile tile, Vector2D polyOffset, LightingTilemapCollider2D tilemap, float lightSizeSquared, float z)
    {
        if (tile.GetPairs(tilemap).Count < 1)
        {
            return;
        }

        LightingBufferTilemapRectangle.pairList = tile.GetPairs(tilemap)[0];

        Pair2D p;

        Vector2D zero = Vector2D.Zero();

        GL.Color(Color.black);

        for (int s = 0; s < LightingBufferTilemapRectangle.pairList.Count; s++)
        {
            p = LightingBufferTilemapRectangle.pairList[s];

            vA.x = p.A.x + polyOffset.x;
            vA.y = p.A.y + polyOffset.y;

            vB.x = p.B.x + polyOffset.x;
            vB.y = p.B.y + polyOffset.y;

            vC.x = p.A.x + polyOffset.x;
            vC.y = p.A.y + polyOffset.y;

            vD.x = p.B.x + polyOffset.x;
            vD.y = p.B.y + polyOffset.y;

            vA.Push(Vector2D.Atan2(vA, zero), lightSizeSquared);
            vB.Push(Vector2D.Atan2(vB, zero), lightSizeSquared);

            Max2DMatrix.DrawTriangle(vC, vD, vA, zero, z);
            Max2DMatrix.DrawTriangle(vA, vB, vD, zero, z);
        }

        Sprite penumbraSprite = LightingManager2D.Get().materials.GetAtlasPenumbraSprite();

        float angleA, angleB;

        Rect uvRect = new Rect((float)penumbraSprite.rect.x / penumbraSprite.texture.width, (float)penumbraSprite.rect.y / penumbraSprite.texture.height, (float)penumbraSprite.rect.width / penumbraSprite.texture.width, (float)penumbraSprite.rect.height / penumbraSprite.texture.height);

        uvRect.width  += uvRect.x;
        uvRect.height += uvRect.y;

        uvRect.x      += 1f / 2048;
        uvRect.y      += 1f / 2048;
        uvRect.width  -= 1f / 2048;
        uvRect.height -= 1f / 2048;

        GL.Color(Color.white);

        for (int s = 0; s < LightingBufferTilemapRectangle.pairList.Count; s++)
        {
            p = LightingBufferTilemapRectangle.pairList[s];

            vA.x = p.A.x + polyOffset.x;
            vA.y = p.A.y + polyOffset.y;

            pA.x = p.A.x + polyOffset.x;
            pA.y = p.A.y + polyOffset.y;

            pB.x = p.B.x + polyOffset.x;
            pB.y = p.B.y + polyOffset.y;

            vB.x = p.B.x + polyOffset.x;
            vB.y = p.B.y + polyOffset.y;

            vC.x = p.A.x + polyOffset.x;
            vC.y = p.A.y + polyOffset.y;

            vD.x = p.B.x + polyOffset.x;
            vD.y = p.B.y + polyOffset.y;

            angleA = (float)Vector2D.Atan2(vA, zero);
            angleB = (float)Vector2D.Atan2(vB, zero);

            vA.Push(angleA, lightSizeSquared);
            pA.Push(angleA - Mathf.Deg2Rad * buffer.lightSource.occlusionSize, lightSizeSquared);

            vB.Push(angleB, lightSizeSquared);
            pB.Push(angleB + Mathf.Deg2Rad * buffer.lightSource.occlusionSize, lightSizeSquared);

            GL.TexCoord2(uvRect.x, uvRect.y);
            GL.Vertex3((float)vC.x, (float)vC.y, z);

            GL.TexCoord2(uvRect.width, uvRect.y);
            GL.Vertex3((float)vA.x, (float)vA.y, z);

            GL.TexCoord2((float)uvRect.x, uvRect.height);
            GL.Vertex3((float)pA.x, (float)pA.y, z);

            GL.TexCoord2(uvRect.x, uvRect.y);
            GL.Vertex3((float)vD.x, (float)vD.y, z);

            GL.TexCoord2(uvRect.width, uvRect.y);
            GL.Vertex3((float)vB.x, (float)vB.y, z);

            GL.TexCoord2(uvRect.x, uvRect.height);
            GL.Vertex3((float)pB.x, (float)pB.y, z);
        }

        LightingDebug.shadowGenerations++;
    }