Esempio n. 1
0
    static public FBOManager Get()
    {
        if (instance != null)
        {
            return(instance);
        }

        foreach (FBOManager mainBuffer in Object.FindObjectsOfType(typeof(FBOManager)))
        {
            instance = mainBuffer;
            return(instance);
        }

        GameObject setFBOManager = new GameObject();

        setFBOManager.transform.parent = LightingManager2D.Get().transform;
        setFBOManager.name             = "FBO Manager";
        setFBOManager.layer            = LightingManager2D.lightingLayer;

        instance = setFBOManager.AddComponent <FBOManager> ();

        instance.Initialize();

        return(instance);
    }
Esempio n. 2
0
    // Management
    static public LightingBuffer2D AddBuffer(int textureSize, LightingSource2D light)
    {
        LightingManager2D manager = LightingManager2D.Get();

        if (manager.fixedLightBufferSize)
        {
            textureSize = LightingManager2D.GetTextureSize(manager.fixedLightTextureSize);
        }

        FBOManager fboManager = LightingManager2D.Get().fboManager;

        if (fboManager == null)
        {
            Debug.LogError("Lighting Manager Instance is Out-Dated.");
            Debug.LogError("Try Re-Initializing 'Lighting Manager 2D' Component");
            return(null);
        }

        GameObject buffer = new GameObject();

        buffer.name             = "Buffer " + GetCount();
        buffer.transform.parent = LightingManager2D.Get().fboManager.transform;
        buffer.layer            = LightingManager2D.lightingLayer;

        LightingBuffer2D lightingBuffer = buffer.AddComponent <LightingBuffer2D> ();

        lightingBuffer.Initiate(textureSize);
        lightingBuffer.lightSource = light;         // Unnecessary?
        lightingBuffer.free        = false;
        lightingBuffer.bufferCamera.orthographicSize = light.lightSize;

        return(lightingBuffer);
    }
Esempio n. 3
0
    public void Initialize()
    {
        instance = this;

        // Lighting Materials
        additiveMaterial = new Material(Shader.Find(Max2D.shaderPath + "Particles/Additive"));

        penumbraMaterial             = new Material(Shader.Find(Max2D.shaderPath + "Particles/Multiply"));
        penumbraMaterial.mainTexture = Resources.Load("textures/penumbra") as Texture;

        occlusionEdgeMaterial             = new Material(Shader.Find(Max2D.shaderPath + "Particles/Multiply"));
        occlusionEdgeMaterial.mainTexture = Resources.Load("textures/occlusionedge") as Texture;

        shadowBlurMaterial             = new Material(Shader.Find(Max2D.shaderPath + "Particles/Multiply"));
        shadowBlurMaterial.mainTexture = Resources.Load("textures/shadowblur") as Texture;

        occlusionBlurMaterial             = new Material(Shader.Find(Max2D.shaderPath + "Particles/Multiply"));
        occlusionBlurMaterial.mainTexture = Resources.Load("textures/occlussionblur") as Texture;

        whiteSpriteMaterial = new Material(Shader.Find("SmartLighting2D/SpriteWhite"));

        blackSpriteMaterial = new Material(Shader.Find("SmartLighting2D/SpriteBlack"));

        transform.position = Vector3.zero;

        mainBuffer = LightingMainBuffer2D.Get();

        fboManager = FBOManager.Get();

        version = VERSION;
    }
Esempio n. 4
0
    public void OnDisable()
    {
        list.Remove(this);

        ///// Free Buffer!
        FBOManager.FreeBuffer(buffer);
        buffer   = null;
        inScreen = false;
    }
Esempio n. 5
0
    public void Initialize()
    {
        instance = this;

        transform.position = Vector3.zero;

        mainBuffer = LightingMainBuffer2D.Get();

        fboManager = FBOManager.Get();

        spriteAtlas = SpriteAtlasManager.Get();

        meshRendererManager = MeshRendererManager.Get();

        version = VERSION;
    }
Esempio n. 6
0
    void Update()
    {
        CheckIfUpdateNeeded();

        LightingManager2D manager = LightingManager2D.Get();
        bool disabled             = manager.disableEngine;

        if (InCamera())
        {
            if (update == true)
            {
                if (inScreen == false)
                {
                    buffer = FBOManager.PullBuffer(LightingManager2D.GetTextureSize(textureSize), this);

                    update = false;
                    if (buffer != null)
                    {
                        if (disabled == false)
                        {
                            buffer.bufferCamera.enabled          = true;                    // //UpdateLightBuffer(True)
                            buffer.bufferCamera.orthographicSize = lightSize;
                        }
                    }
                    //Debug.Log(3);

                    inScreen = true;
                }
                else
                {
                    update = false;
                    if (buffer != null)
                    {
                        if (disabled == false)
                        {
                            buffer.bufferCamera.enabled          = true;                    // //UpdateLightBuffer(True)
                            buffer.bufferCamera.orthographicSize = lightSize;
                        }
                    }
                }
            }
            else
            {
                if (buffer != null)
                {
                    //	Debug.Log(1);
                }
                else
                {
                    buffer = FBOManager.PullBuffer(LightingManager2D.GetTextureSize(textureSize), this);

                    update = false;
                    if (buffer != null)
                    {
                        if (disabled == false)
                        {
                            buffer.bufferCamera.enabled          = true;                    // //UpdateLightBuffer(True)
                            buffer.bufferCamera.orthographicSize = lightSize;
                        }
                    }


                    inScreen = true;

                    //Debug.Log(4);
                }
            }
        }
        else
        {
            ///// Free Buffer!
            if (buffer != null)
            {
                FBOManager.FreeBuffer(buffer);
                buffer = null;
            }
            inScreen = false;
        }



        if (eventHandling)
        {
            Vector2D zero             = Vector2D.Zero();
            float    lightSizeSquared = Mathf.Sqrt(lightSize * lightSize + lightSize * lightSize);

            List <LightCollision2D> collisions = new List <LightCollision2D>();

            foreach (LightingCollider2D id in LightingCollider2D.GetList())
            {
                if (id.colliderType == LightingCollider2D.ColliderType.None)
                {
                    continue;
                }
                if (LightingManager2D.culling && Vector2.Distance(id.transform.position, transform.position) > id.GetCullingDistance() + lightSize)
                {
                    continue;
                }
                LightCollision2D collision = new LightCollision2D();
                collision.lightSource     = this;
                collision.collider        = id;
                collision.pointsColliding = id.GetShadowCollisionPolygons()[0].ToWorldSpace(id.transform).ToOffset(new Vector2D(-transform.position)).pointsList;
                collisions.Add(collision);
            }

            foreach (LightingCollider2D id in LightingCollider2D.GetList())
            {
                if (LightingManager2D.culling && Vector2.Distance(id.transform.position, transform.position) > id.GetCullingDistance() + lightSize)
                {
                    continue;
                }

                if (id.colliderType == LightingCollider2D.ColliderType.None)
                {
                    continue;
                }

                List <Polygon2D> polygons = id.GetShadowCollisionPolygons();

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

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

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

                    Vector2D vA, pA, vB, pB;
                    float    angleA, angleB;
                    foreach (Pair2D p in Pair2D.GetList(poly.pointsList, false))
                    {
                        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 * occlusionSize, lightSizeSquared);

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

                        if (eventHandling)
                        {
                            Polygon2D triPoly = new Polygon2D();
                            triPoly.AddPoint(p.A);
                            triPoly.AddPoint(p.B);
                            triPoly.AddPoint(pB);
                            triPoly.AddPoint(pA);

                            foreach (LightCollision2D col in new List <LightCollision2D>(collisions))
                            {
                                if (col.collider == id)
                                {
                                    continue;
                                }
                                foreach (Vector2D point in new List <Vector2D>(col.pointsColliding))
                                {
                                    if (triPoly.PointInPoly(point))
                                    {
                                        col.pointsColliding.Remove(point);
                                    }
                                }
                                if (col.pointsColliding.Count < 1)
                                {
                                    collisions.Remove(col);
                                }
                            }
                        }
                    }

                    LightingManager2D.LightingDebug.shadowGenerations++;
                }
            }

            if (collisions.Count > 0)
            {
                foreach (LightCollision2D collision in collisions)
                {
                    collision.collider.CollisionEvent(collision);
                }
            }
        }
    }
Esempio n. 7
0
        static public void OnGUI()
        {
            if (timer == null)
            {
                LightingDebug.timer = TimerHelper.Create();
            }
            if (timer.GetMillisecs() > 1000)
            {
                ShowLightBufferUpdates = LightBufferUpdates;

                LightBufferUpdates = 0;

                ShowLightMainBufferUpdates = LightMainBufferUpdates;

                LightMainBufferUpdates = 0;

                ShowLightMainCameraUpdates = LightMainCameraUpdates;

                LightMainCameraUpdates = 0;

                timer = TimerHelper.Create();
            }

            if (SpriteRenderersDrawn > 0)
            {
                ShowSpriteRenderersDrawn = SpriteRenderersDrawn;
                SpriteRenderersDrawn     = 0;
            }


            int count = 0;

            foreach (LightingSource2D light in LightingSource2D.GetList())
            {
                if (light.InCamera() == false)
                {
                    continue;
                }
                count++;
            }

            GUI.Label(new Rect(10, 10, 500, 20), "Lights in Camera: " + count + "/" + LightingSource2D.GetList().Count);
            GUI.Label(new Rect(10, 30, 500, 20), "Free Buffers: " + FBOManager.GetFreeCount() + "/" + FBOManager.GetList().Count);
            GUI.Label(new Rect(10, 50, 500, 20), "Light Buffer Updates: " + ShowLightBufferUpdates);
            GUI.Label(new Rect(10, 70, 500, 20), "Total Light Updates: " + totalLightUpdates);
            GUI.Label(new Rect(10, 90, 500, 20), "=========================");

            GUI.Label(new Rect(10, 110, 500, 20), "Mask Generations: " + maskGenerations);
            GUI.Label(new Rect(10, 130, 500, 20), "Shadow Generations: " + shadowGenerations);
            GUI.Label(new Rect(10, 150, 500, 20), "Penumbra Generations: " + penumbraGenerations);
            GUI.Label(new Rect(10, 170, 500, 20), "Objects Culled: " + culled);

            GUI.Label(new Rect(10, 190, 500, 20), "=========================");

            GUI.Label(new Rect(10, 210, 500, 20), "Sprite Renderers Drawn: " + ShowSpriteRenderersDrawn);

            GUI.Label(new Rect(10, 230, 500, 20), "=========================");

            GUI.Label(new Rect(10, 250, 500, 20), "Light Main Buffer Updates: " + ShowLightMainBufferUpdates);
            GUI.Label(new Rect(10, 270, 500, 20), "Light Main Camera Updates: " + ShowLightMainCameraUpdates);

            GUI.Label(new Rect(10, 290, 500, 20), "=========================");

            Texture texture = LightingMainBuffer2D.Get().bufferCamera.activeTexture;

            if (texture != null)
            {
                GUI.Label(new Rect(10, 310, 500, 20), "Main Buffer Resolution: " + texture.width + "x" + texture.height);
            }
            else
            {
                GUI.Label(new Rect(10, 330, 500, 20), "Main Buffer Resolution: NULL");
            }

            GUI.Label(new Rect(10, 350, 500, 20), "Glow Particles Generated: " + BlurManager.dictionary.Count);

            GUIStyle style = new GUIStyle();

            style.alignment        = TextAnchor.LowerRight;
            style.normal.textColor = Color.white;
            style.fontSize         = 13;

                        #if UNITY_2018_1_OR_NEWER
            Object[] tilemaps = Object.FindObjectsOfType(typeof(LightingTilemapCollider2D));
                        #endif
            Object[] lights    = Object.FindObjectsOfType(typeof(LightingSource2D));
            Object[] colliders = Object.FindObjectsOfType(typeof(LightingCollider2D));
            Object[] sprites   = Object.FindObjectsOfType(typeof(LightingSpriteRenderer2D));

                        #if UNITY_2018_1_OR_NEWER
            GUI.Label(new Rect(0, -10, Screen.width - 10, Screen.height), "Tilemap Collider Count: " + tilemaps.Length, style);
                        #endif

            GUI.Label(new Rect(0, -30, Screen.width - 10, Screen.height), "Lights Count: " + lights.Length, style);
            GUI.Label(new Rect(0, -50, Screen.width - 10, Screen.height), "Colliders Count: " + colliders.Length, style);
            GUI.Label(new Rect(0, -70, Screen.width - 10, Screen.height), "Sprites Count: " + sprites.Length, style);
        }
Esempio n. 8
0
 public LightingBuffer2D GetBuffer()
 {
     buffer = FBOManager.PullBuffer(LightingManager2D.GetTextureSize(textureSize), this);
     return(buffer);
 }
Esempio n. 9
0
    static public void OnGUI()
    {
        if (lights == null)
        {
            lights    = Object.FindObjectsOfType(typeof(LightingSource2D));
            colliders = Object.FindObjectsOfType(typeof(LightingCollider2D));
            sprites   = Object.FindObjectsOfType(typeof(LightingSpriteRenderer2D));
            tilemaps  = Object.FindObjectsOfType(typeof(LightingTilemapCollider2D));
        }

        if (timer == null)
        {
            LightingDebug.timer = TimerHelper.Create();
        }

        if (timer.GetMillisecs() > 1000)
        {
            SecondUpdate();
        }

        ShowUpdate();

        int count = 0;

        foreach (LightingSource2D light in LightingSource2D.GetList())
        {
            if (light.InCamera() == false)
            {
                continue;
            }
            count++;
        }

        LightingManager2D manager2D = LightingManager2D.Get();

        int y = 10;

        GUI.Label(new Rect(10, y, 500, 20), "Camera Size: " + manager2D.mainBuffer.cameraSize);

        y += 10;

        GUI.Label(new Rect(10, y, 500, 20), "New Render Textures: " + NewRenderTextures);

        y += 10;

        GUI.Label(new Rect(10, y, 500, 20), "Lights in Camera: " + count + "/" + LightingSource2D.GetList().Count);

        y += 10;

        GUI.Label(new Rect(10, y, 500, 20), "Free Buffers: " + FBOManager.GetFreeCount() + "/" + FBOManager.GetList().Count);

        y += 10;

        GUI.Label(new Rect(10, y, 500, 20), "Light Buffer Updates: " + ShowLightBufferUpdates); // In Frame

        y += 10;

        GUI.Label(new Rect(10, y, 500, 20), "Total Light Updates: " + totalLightUpdates);

        y += 10;

        GUI.Label(new Rect(10, y, 500, 20), "=========================");

        y += 10;

        GUI.Label(new Rect(10, y, 500, 20), "Mask Generations: " + show_maskGenerations);

        y += 10;

        GUI.Label(new Rect(10, y, 500, 20), "Shadow Generations: " + show_shadowGenerations);

        y += 10;

        GUI.Label(new Rect(10, y, 500, 20), "Objects Culled: " + show_culled);

        y += 10;

        GUI.Label(new Rect(10, y, 500, 20), "=========================");

        y += 10;

        GUI.Label(new Rect(10, y, 500, 20), "Shadow Collider World Generations C: " + ShadowColliderTotalGenerationsWorld_collider + " (re: " + ShadowColliderTotalGenerationsWorld_collider_re + ") Pair: " + LightingDebug.ShadowColliderTotalGenerationsWorld_collider_pair);

        y += 10;

        GUI.Label(new Rect(10, y, 500, 20), "Shadow Collider Local Generations C: " + ShadowColliderTotalGenerationsLocal_collider);

        y += 10;

        GUI.Label(new Rect(10, y, 500, 20), "Shadow Collider World Generations S: " + ShadowColliderTotalGenerationsWorld_shape + " (re: " + ShadowColliderTotalGenerationsWorld_shape_re + ") Pair: " + LightingDebug.ShadowColliderTotalGenerationsWorld_shape_pair);

        y += 10;

        GUI.Label(new Rect(10, y, 500, 20), "Shadow Collider Local Generations S: " + ShadowColliderTotalGenerationsLocal_shape);

        y += 10;

        GUI.Label(new Rect(10, y, 500, 20), "Total Object SHAPE Generations: " + totalObjectMaskShapeGenerations);

        y += 10;

        GUI.Label(new Rect(10, y, 500, 20), "Total Object MESH Generations : " + totalObjectMaskMeshGenerations);

        y += 10;

        GUI.Label(new Rect(10, y, 500, 20), "=========================");

        y += 10;

        GUI.Label(new Rect(10, y, 500, 20), "Convex Hull Generations: " + ShowConvexHullGenerations);

        y += 10;

        GUI.Label(new Rect(10, y, 500, 20), "=========================");

        y += 10;

        GUI.Label(new Rect(10, y, 500, 20), "Sprite Renderers Drawn: " + ShowSpriteRenderersDrawn);

        y += 10;

        GUI.Label(new Rect(10, y, 500, 20), "=========================");

        y += 10;

        GUI.Label(new Rect(10, y, 500, 20), "Light Main Buffer Updates: " + ShowLightMainBufferUpdates);

        y += 10;

        GUI.Label(new Rect(10, y, 500, 20), "Light Main Camera Updates: " + ShowLightMainCameraUpdates);

        y += 10;

        GUI.Label(new Rect(10, y, 500, 20), "=========================");

        y += 10;

        Texture texture = LightingMainBuffer2D.Get().bufferCamera.activeTexture;

        if (texture != null)
        {
            GUI.Label(new Rect(10, y, 500, 20), "Main Buffer Resolution: " + texture.width + "x" + texture.height);
        }
        else
        {
            GUI.Label(new Rect(10, y, 500, 20), "Main Buffer Resolution: NULL");
        }

        y += 10;

        GUI.Label(new Rect(10, y, 500, 20), "Glow Particles Generated: " + BlurManager.dictionary.Count);

        RightBottomPanel();
    }