Пример #1
0
    // Y Axis
    public static void MaskDepthWithoutAtlas(LightingBuffer2D buffer, LightingCollider2D id, Material material, Vector2D offset, float z)
    {
        if (id.shape.maskType == LightingCollider2D.MaskType.None)
        {
            return;
        }

        if (id.shape.maskType != LightingCollider2D.MaskType.Sprite)
        {
            return;
        }

        if (id.isVisibleForLight(buffer) == false)
        {
            return;
        }

        Sprite sprite = id.shape.GetOriginalSprite();

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

        Vector2 p     = id.transform.position;
        Vector2 scale = id.transform.lossyScale;

        material.mainTexture = sprite.texture;

        Max2D.DrawSprite(material, id.spriteRenderer, offset.ToVector2() + p, scale, id.transform.rotation.eulerAngles.z, z);

        material.mainTexture = null;

        LightingDebug.maskGenerations++;
    }
Пример #2
0
    public static void Shadow(LightingBuffer2D buffer, LightingCollider2D id, float lightSizeSquared, float z)
    {
        if (id.shape.colliderType != LightingCollider2D.ColliderType.Mesh)
        {
            return;
        }

        if (id.isVisibleForLight(buffer) == false)
        {
            return;
        }

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

        List <Polygon2D> polygons = new List <Polygon2D>();

        Mesh mesh = id.meshFilter.sharedMesh;

        Vector3 vecA, vecB, vecC;

        Vector2D vA, pA, vB, pB;
        float    angleA, angleB;

        for (int i = 0; i < mesh.triangles.GetLength(0); i = i + 3)
        {
            vecA = buffer.transform.TransformPoint(mesh.vertices [mesh.triangles [i]]);
            vecB = buffer.transform.TransformPoint(mesh.vertices [mesh.triangles [i + 1]]);
            vecC = buffer.transform.TransformPoint(mesh.vertices [mesh.triangles [i + 2]]);

            Polygon2D poly = new Polygon2D();
            poly.AddPoint(vecA.x, vecA.y);
            poly.AddPoint(vecB.x, vecB.y);
            poly.AddPoint(vecC.x, vecC.y);
            //polygons.Add(poly);
        }

        if (polygons.Count < 1)
        {
            return;
        }

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

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

        foreach (Polygon2D polygon in polygons)
        {
            Polygon2D poly = polygon.ToWorldSpace(id.gameObject.transform);
            poly.ToOffsetItself(new Vector2D(-buffer.lightSource.transform.position));

            if (poly.PointInPoly(zero))
            {
                continue;
            }

            foreach (Pair2D p in Pair2D.GetList(poly.pointsList))
            {
                vA = p.A.Copy();
                pA = p.A.Copy();

                vB = p.B.Copy();
                pB = p.B.Copy();

                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)p.A.x, (float)p.A.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)p.B.x, (float)p.B.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.penumbraGenerations ++;
        }

        GL.Color(Color.black);

        foreach (Polygon2D polygon in polygons)
        {
            Polygon2D poly = polygon.ToWorldSpace(id.gameObject.transform);
            poly.ToOffsetItself(new Vector2D(-buffer.lightSource.transform.position));

            if (poly.PointInPoly(zero))
            {
                continue;
            }

            foreach (Pair2D p in Pair2D.GetList(poly.pointsList, false))
            {
                vA = p.A.Copy();
                vB = p.B.Copy();

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

                Max2DMatrix.DrawTriangle(p.A, p.B, vA, zero, z);
                Max2DMatrix.DrawTriangle(vA, vB, p.B, zero, z);
            }

            LightingDebug.shadowGenerations++;
        }
    }
Пример #3
0
    public static void MaskWithAtlas(LightingBuffer2D buffer, LightingCollider2D id, Vector2D offset, float z)
    {
        if (id.shape.maskType == LightingCollider2D.MaskType.None)
        {
            return;
        }

        if (id.shape.maskType != LightingCollider2D.MaskType.Sprite)
        {
            return;
        }

        if (id.isVisibleForLight(buffer) == false)
        {
            return;
        }

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

        if (id.shape.GetOriginalSprite() == null)
        {
            return;
        }

        Sprite sprite = id.shape.GetAtlasSprite();

        if (sprite == null)
        {
            Sprite reqSprite = SpriteAtlasManager.RequestSprite(id.shape.GetOriginalSprite(), SpriteRequest.Type.WhiteMask);
            if (reqSprite == null)
            {
                PartiallyBatched_Collider batched = new PartiallyBatched_Collider();

                batched.collider2D = id;

                buffer.partiallyBatchedList_Collider.Add(batched);
                return;
            }
            else
            {
                id.shape.SetAtlasSprite(reqSprite);
                sprite = reqSprite;
            }
        }

        Vector2 p = id.transform.position;

        p.x += (float)offset.x;
        p.y += (float)offset.y;

        Vector2 scale = id.transform.lossyScale;

        spriteRenderer.sprite = sprite;

        Max2D.DrawSpriteBatched_Tris(spriteRenderer, buffer.lightSource.layerSetting[0], id.maskMode, p, scale, id.transform.rotation.eulerAngles.z, z);

        LightingDebug.maskGenerations++;
    }
Пример #4
0
    public static void Shadow(LightingBuffer2D buffer, LightingCollider2D id, float lightSizeSquared, float z, Vector2D offset)
    {
        if (false == (id.shape.colliderType == LightingCollider2D.ColliderType.Collider || id.shape.colliderType == LightingCollider2D.ColliderType.SpriteCustomPhysicsShape || id.shape.colliderType == LightingCollider2D.ColliderType.Mesh))
        {
            return;
        }

        if (id.isVisibleForLight(buffer) == false)
        {
            return;
        }

        spriteRenderer.sprite = id.shape.GetOriginalSprite();

        if (id.spriteRenderer != null)
        {
            spriteRenderer.flipX = id.spriteRenderer.flipX;
            spriteRenderer.flipY = id.spriteRenderer.flipY;
        }
        else
        {
            spriteRenderer.flipX = false;
            spriteRenderer.flipY = false;
        }

        polygons = id.shape.GetPolygons_World_ColliderType(id.transform, spriteRenderer);

        if (polygons.Count < 1)
        {
            return;
        }

        polygonPairs = id.shape.GetPolygons_Pair_World_ColliderType(id.transform, spriteRenderer);

        inverseOffset.x = -offset.x;
        inverseOffset.y = -offset.y;

        CalculatePenumbra();

        bool penumbra  = LightingManager2D.Get().drawPenumbra;
        bool drawAbove = buffer.lightSource.whenInsideCollider == LightingSource2D.WhenInsideCollider.DrawAbove;

        // Draw Inside Collider Works Fine?
        for (int i = 0; i < polygons.Count; i++)
        {
            if (drawAbove && polygons[i].PointInPoly(inverseOffset))
            {
                continue;
            }

            LightingDebug.shadowGenerations++;

            pairList = polygonPairs[i];

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

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

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

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

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

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

                    vD.x = p.B.x + offset.x;
                    vD.y = p.B.y + offset.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, uvRectY, 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);
                }
            }

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

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

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

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

                vD.x = p.B.x + offset.x;
                vD.y = p.B.y + offset.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);
            }
        }
    }
Пример #5
0
    public static void Mask(LightingBuffer2D buffer, LightingCollider2D id, LayerSetting layerSetting, Vector2D offset, float z)
    {
        if (false == (id.shape.maskType == LightingCollider2D.MaskType.Collider || id.shape.maskType == LightingCollider2D.MaskType.SpriteCustomPhysicsShape || id.shape.maskType == LightingCollider2D.MaskType.Mesh))
        {
            return;
        }

        if (id.isVisibleForLight(buffer) == false)
        {
            return;
        }

        Mesh         mesh     = null;
        MeshVertices vertices = null;

        if (id.shape.maskType == LightingCollider2D.MaskType.Mesh)
        {
            if (id.meshFilter == null)
            {
                return;
            }
            mesh = id.meshFilter.sharedMesh;
        }
        else
        {
            mesh     = id.shape.GetMesh_MaskType(id.transform);
            vertices = id.shape.GetMesh_Vertices_MaskType(id.transform);
        }

        if (mesh == null)
        {
            return;
        }

        bool maskEffect = (layerSetting.effect == LightingLayerEffect.InvisibleBellow);

        LightingMaskMode maskMode = id.maskMode;
        MeshVertice      vertice;

        if (maskMode == LightingMaskMode.Invisible)
        {
            GL.Color(Color.black);
        }
        else if (layerSetting.effect == LightingLayerEffect.InvisibleBellow)
        {
            float c = (float)offset.y / layerSetting.maskEffectDistance + layerSetting.maskEffectDistance * 2;
            if (c < 0)
            {
                c = 0;
            }

            color.r = c;
            color.g = c;
            color.b = c;
            color.a = 1;

            GL.Color(color);
        }
        else
        {
            GL.Color(Color.white);
        }

        for (int i = 0; i < vertices.list.Count; i++)
        {
            vertice = vertices.list[i];
            Max2DMatrix.DrawTriangle(vertice.a, vertice.b, vertice.c, offset, z);
        }

        LightingDebug.maskGenerations++;
    }