Exemple #1
0
    static public void SetupLocation(LightingBuffer2D buffer, LightingTilemapCollider2D id)
    {
        Vector3 rot = Math2D.GetPitchYawRollRad(id.transform.rotation);

        float rotationYScale = Mathf.Sin(rot.x + Mathf.PI / 2);
        float rotationXScale = Mathf.Sin(rot.y + Mathf.PI / 2);

        scale.x = id.transform.lossyScale.x * rotationXScale * id.cellSize.x;
        scale.y = id.transform.lossyScale.y * rotationYScale * id.cellSize.y;

        sizeInt = LightTilemapSize(id, buffer);

        LightTilemapOffset(id, scale, buffer);

        offset.x = -buffer.lightSource.transform.position.x;
        offset.y = -buffer.lightSource.transform.position.y;

        tilemapOffset.x = id.transform.position.x + id.area.position.x + id.cellAnchor.x;
        tilemapOffset.y = id.transform.position.y + id.area.position.y + id.cellAnchor.y;

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

        tileSize.x = scale.x / id.cellSize.x;
        tileSize.y = scale.y / id.cellSize.y;
    }
    static public void DrawMask(LightingBuffer2D buffer, LightingTile tile, LightingLayerEffect maskEffect, Material materialA, Material materialB, Vector2D polyOffset, LightingTilemapCollider2D tilemap, float lightSizeSquared, float z)
    {
        VirtualSpriteRenderer spriteRenderer = new VirtualSpriteRenderer();

        spriteRenderer.sprite = tile.GetOriginalSprite();

        if (spriteRenderer.sprite == null)
        {
            return;
        }

        Vector3 rot = Math2D.GetPitchYawRollRad(tilemap.transform.rotation);

        float rotationYScale = Mathf.Sin(rot.x + Mathf.PI / 2);
        float rotationXScale = Mathf.Sin(rot.y + Mathf.PI / 2);

        float scaleX = tilemap.transform.lossyScale.x * rotationXScale * tilemap.cellSize.x;
        float scaleY = tilemap.transform.lossyScale.y * rotationYScale * tilemap.cellSize.y;

        Vector2 tileSize = new Vector2(scaleX / tilemap.cellSize.x, scaleY / tilemap.cellSize.y);

        Material material = materialA;

        if (tilemap.maskMode == LightingMaskMode.Invisible || (maskEffect == LightingLayerEffect.InvisibleBellow && polyOffset.y < 0))
        {
            material = materialB;
        }

        material.mainTexture = spriteRenderer.sprite.texture;

        Max2D.DrawSprite(material, spriteRenderer, polyOffset.ToVector2(), tileSize, 0, z);

        material.mainTexture = null;

        LightingDebug.maskGenerations++;
    }
    static public void DrawTilemaps(Vector2D offset, float z)
    {
                #if UNITY_2018_1_OR_NEWER
        LightingManager2D manager = LightingManager2D.Get();

        GL.PushMatrix();

        Max2D.defaultMaterial.SetPass(0);

        GL.Begin(GL.TRIANGLES);
        GL.Color(Color.black);

        // Day Soft Shadows
        foreach (LightingTilemapCollider2D id in LightingTilemapCollider2D.GetList())
        {
            if (id.map == null)
            {
                continue;
            }

            if (id.dayHeight == false)
            {
                continue;
            }

            Vector2D tilesetOffset = new Vector2D(offset);
            tilesetOffset += new Vector2D(id.area.position.x, id.area.position.y);

            for (int x = 0; x < id.area.size.x; x++)
            {
                for (int y = 0; y < id.area.size.y; y++)
                {
                    if (id.map[x, y] == null)
                    {
                        continue;
                    }

                    Vector2D tileOffset = tilesetOffset.Copy();
                    tileOffset += new Vector2D(x, y);

                    DrawSoftShadowTile(tileOffset, z, id.height);
                }
            }
        }

        GL.End();
        GL.PopMatrix();

        GL.PushMatrix();

        manager.materials.GetShadowBlur().SetPass(0);

        GL.Begin(GL.TRIANGLES);
        Max2D.SetColor(Color.white);

        // Day Soft Shadows Penumbra
        foreach (LightingTilemapCollider2D id in LightingTilemapCollider2D.GetList())
        {
            if (id.map == null)
            {
                continue;
            }

            if (id.dayHeight == false)
            {
                continue;
            }

            Vector2D tilesetOffset = new Vector2D(offset);
            tilesetOffset += new Vector2D(id.area.position.x, id.area.position.y);

            for (int x = 0; x < id.area.size.x; x++)
            {
                for (int y = 0; y < id.area.size.y; y++)
                {
                    if (id.map[x, y] == null)
                    {
                        continue;
                    }

                    Vector2D tileOffset = tilesetOffset.Copy();
                    tileOffset += new Vector2D(x, y);

                    DrawSoftShadowTileBlur(tileOffset, z, id.height);
                }
            }
        }

        GL.End();
        GL.PopMatrix();

        Material materialWhite = manager.materials.GetWhiteSprite();

        // Tilemap Daylighting Masks
        foreach (LightingTilemapCollider2D id in LightingTilemapCollider2D.GetList())
        {
            if (id.map == null)
            {
                continue;
            }

            if (id.dayHeight == false)
            {
                continue;
            }

            Vector3 rot = Math2D.GetPitchYawRollRad(id.transform.rotation);

            float rotationYScale = Mathf.Sin(rot.x + Mathf.PI / 2);
            float rotationXScale = Mathf.Sin(rot.y + Mathf.PI / 2);

            float scaleX = id.transform.lossyScale.x * rotationXScale * id.cellSize.x;
            float scaleY = id.transform.lossyScale.y * rotationYScale * id.cellSize.y;

            Vector2D tilesetOffset = new Vector2D(offset);
            tilesetOffset += new Vector2D(id.area.position.x, id.area.position.y);
            tilesetOffset += new Vector2D(id.cellAnchor.x, id.cellAnchor.y);

            //GL.PushMatrix ();

            for (int x = 0; x < id.area.size.x; x++)
            {
                for (int y = 0; y < id.area.size.y; y++)
                {
                    LightingTile tile = id.map[x, y];
                    if (tile == null)
                    {
                        continue;
                    }

                    if (tile.GetOriginalSprite() == null)
                    {
                        continue;
                    }

                    VirtualSpriteRenderer spriteRenderer = new VirtualSpriteRenderer();
                    spriteRenderer.sprite = tile.GetOriginalSprite();

                    materialWhite.mainTexture = tile.GetOriginalSprite().texture;

                    Vector2D tileOffset = tilesetOffset.Copy();
                    tileOffset += new Vector2D(x, y);


                    //Max2D.DrawSprite(materialWhite, spriteRenderer, tileOffset.ToVector2(), new Vector2(scaleX / id.cellSize.x, scaleY / id.cellSize.y), 0, z);

                    materialWhite.mainTexture = null;
                }
            }
        }

        //GL.PopMatrix ();
                #endif
    }
    static public void SortTilemap(LightingBuffer2D buffer, LightingTilemapCollider2D id)
    {
        //if (id.maskType != LightingTilemapCollider2D.MaskType.Sprite) {
        //	return;
        //}

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

        Vector3 rot = Math2D.GetPitchYawRollRad(id.transform.rotation);

        float rotationYScale = Mathf.Sin(rot.x + Mathf.PI / 2);
        float rotationXScale = Mathf.Sin(rot.y + Mathf.PI / 2);

        float scaleX = id.transform.lossyScale.x * rotationXScale * id.cellSize.x;
        float scaleY = id.transform.lossyScale.y * rotationYScale * id.cellSize.y;

        int sizeInt = LightingBufferTilemapRectangle.LightTilemapSize(id, buffer);

        LightingBufferTilemapRectangle.LightTilemapOffset(id, new Vector2(scaleX, scaleY), buffer);

        Vector2Int newPositionInt = LightingBufferTilemapRectangle.newPositionInt;

        Vector2D tilemapOffset = LightingBufferTilemapRectangle.tilemapOffset;
        Vector2D polyOffset    = LightingBufferTilemapRectangle.polyOffset;
        Vector2D inverseOffset = LightingBufferTilemapRectangle.inverseOffset;

        tilemapOffset.x = id.area.position.x + id.cellAnchor.x + id.transform.position.x;
        tilemapOffset.y = id.area.position.y + id.cellAnchor.y + id.transform.position.y;

        offset.x = -buffer.lightSource.transform.position.x;
        offset.y = -buffer.lightSource.transform.position.y;

        VirtualSpriteRenderer spriteRenderer = new VirtualSpriteRenderer();

        Vector2 tileSize = new Vector2(scaleX / id.cellSize.x, scaleY / id.cellSize.y);

        LightingTile tile;

        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;
                }

                if (tile.GetOriginalSprite() == null)
                {
                    return;
                }

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

                polyOffset.x *= scaleX;
                polyOffset.y *= scaleY;

                if (LightingManager2D.culling && Vector2.Distance(polyOffset.ToVector2(), 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;

                //if (poly.PointInPoly (inverseOffset)) {
                //	continue;
                //}

                if (layerSettings.renderingOrder == LightRenderingOrder.YAxis)
                {
                    list.Add(id, tile, -(float)polyOffset.y - (float)offset.y, polyOffset);
                }
                else
                {
                    list.Add(id, tile, -Vector2.Distance(polyOffset.ToVector2() - offset.ToVector2(), buffer.lightSource.transform.position), polyOffset);
                }
            }
        }
    }