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++;
    }
Beispiel #2
0
    ////////////// Sprite Atlas Sprite
    static public void DrawSpriteBatched_Tris(VirtualSpriteRenderer spriteRenderer, LayerSetting layerSetting, LightingMaskMode maskMode, Vector2 pos, Vector2 size, float rot, float z = 0f)
    {
        Sprite sprite = spriteRenderer.sprite;

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

        // UV Coordinates Calculation
        float spriteSheetUV_X = (float)(sprite.texture.width) / sprite.rect.width;
        float spriteSheetUV_Y = (float)(sprite.texture.height) / sprite.rect.height;

        Rect rect = sprite.rect;

        uvRect.x      = rect.x / sprite.texture.width;
        uvRect.y      = rect.y / sprite.texture.height;
        uvRect.width  = rect.width / sprite.texture.width;
        uvRect.height = rect.height / sprite.texture.height;

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

        //uvRect.x += 1f / sprite.texture.width;
        //uvRect.y += 1f / sprite.texture.height;
        //uvRect.width -= 2f / sprite.texture.width;
        //uvRect.height -= 2f / sprite.texture.height;

        // Vertex Position Calculation
        scale.x = spriteSheetUV_X * rect.width / sprite.pixelsPerUnit;
        scale.y = spriteSheetUV_Y * rect.height / sprite.pixelsPerUnit;

        scale.x = (float)sprite.texture.width / sprite.rect.width;
        scale.y = (float)sprite.texture.height / sprite.rect.height;

        size.x /= scale.x;
        size.y /= scale.y;

        size.x *= (float)sprite.texture.width / (sprite.pixelsPerUnit * 2);
        size.y *= (float)sprite.texture.height / (sprite.pixelsPerUnit * 2);

        if (spriteRenderer.flipX)
        {
            size.x = -size.x;
        }

        if (spriteRenderer.flipY)
        {
            size.y = -size.y;
        }

        float rectAngle = Mathf.Atan2(size.y, size.x);
        float dist      = Mathf.Sqrt(size.x * size.x + size.y * size.y);

        rot = rot * Mathf.Deg2Rad + Mathf.PI;

        // Pivot Point Calculation
        Vector2 pivot = sprite.pivot;

        pivot.x /= sprite.rect.width;
        pivot.y /= sprite.rect.height;
        pivot.x -= 0.5f;
        pivot.y -= 0.5f;

        pivot.x *= size.x * 2;
        pivot.y *= size.y * 2;

        float pivotDist  = Mathf.Sqrt(pivot.x * pivot.x + pivot.y * pivot.y);
        float pivotAngle = Mathf.Atan2(pivot.y, pivot.x);

        pos.x += Mathf.Cos(pivotAngle + rot) * pivotDist;
        pos.y += Mathf.Sin(pivotAngle + rot) * pivotDist;

        // Vertext Coordinates
        pos1.x = pos.x + Mathf.Cos(rectAngle + rot) * dist;
        pos1.y = pos.y + Mathf.Sin(rectAngle + rot) * dist;

        pos2.x = pos.x + Mathf.Cos(-rectAngle + rot) * dist;
        pos2.y = pos.y + Mathf.Sin(-rectAngle + rot) * dist;

        pos3.x = pos.x + Mathf.Cos(rectAngle + Mathf.PI + rot) * dist;
        pos3.y = pos.y + Mathf.Sin(rectAngle + Mathf.PI + rot) * dist;

        pos4.x = pos.x + Mathf.Cos(-rectAngle + Mathf.PI + rot) * dist;
        pos4.y = pos.y + Mathf.Sin(-rectAngle + Mathf.PI + rot) * dist;

        if (maskMode == LightingMaskMode.Invisible)
        {
            GL.Color(Color.black);
        }
        else if (layerSetting.effect == LightingLayerEffect.InvisibleBellow)
        {
            //float lowestY = v1.y;
            //lowestY = Mathf.Min(lowestY, v2.y);
            //lowestY = Mathf.Min(lowestY, v3.y);
            //lowestY = Mathf.Min(lowestY, v3.y);

            float c = pos.y / layerSetting.maskEffectDistance + layerSetting.maskEffectDistance * 2;             // + 1f // pos.y
            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);
        }

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

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

        GL.TexCoord2(uvRect.width, uvRect.height);
        GL.Vertex3(pos3.x, pos3.y, z);

        GL.TexCoord2(uvRect.width, uvRect.height);
        GL.Vertex3(pos3.x, pos3.y, z);

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

        GL.TexCoord2(uvRect.x, uvRect.y);
        GL.Vertex3(pos1.x, pos1.y, z);
    }