Esempio n. 1
0
    ///// Light Texture
    public static void Draw(LightingBuffer2D buffer)
    {
        float z = buffer.transform.position.z;

        size.x = buffer.bufferCamera.orthographicSize;
        size.y = buffer.bufferCamera.orthographicSize;

        if (buffer.lightSource.rotationEnabled)
        {
            // Light Rotation!!!
            CalculatePoints(buffer);
            CalculateOffsets();

            GL.PushMatrix();
            buffer.lightSource.GetMaterial().SetPass(0);
            GL.Begin(GL.QUADS);

            Max2D.DrawImage_Batched(Vector2.zero, size, buffer.lightSource.transform.rotation.eulerAngles.z, z);

            buffer.lightSource.GetMaterial().color = Color.black;

            Max2DMatrix.DrawQuad(right0, right1, right2, right3, z);
            Max2DMatrix.DrawQuad(left0, left1, left2, left3, z);
            Max2DMatrix.DrawQuad(down0, down1, down2, down3, z);
            Max2DMatrix.DrawQuad(up0, up1, up2, up3, z);

            GL.End();
            GL.PopMatrix();
        }
        else
        {
            Max2D.DrawImage(buffer.lightSource.GetMaterial(), Vector2.zero, size, 0, z);
        }
    }
Esempio n. 2
0
    void Render_PostRenderMode()
    {
        // Post-Render Mode Drawing
        if (Render_Check() == false)
        {
            return;
        }

        if (renderingMode != RenderingMode.OnPostRender)
        {
            return;
        }

        if (Camera.current != Camera.main)
        {
            return;
        }

        LightingDebug.LightMainCameraUpdates += 1;

        Vector3 pos = Camera.main.transform.position;

        pos.z += Camera.main.nearClipPlane + 0.1f;

        Max2D.DrawImage(mainBuffer.material, pos, Render_Size(), Camera.main.transform.eulerAngles.z, pos.z);
    }
Esempio n. 3
0
    public void DrawSelf()
    {
        /*
         * float scaleX = (float)destructible.originalSprite.texture.width / destructible.outputTexture.width;
         * float scaleY = (float)destructible.originalSprite.texture.height / destructible.outputTexture.height;
         *
         * float posX = destructible.spriteRenderer.sprite.rect.center.x / destructible.pixelsPerUnit - 3;
         * float posY = destructible.spriteRenderer.sprite.rect.center.y / destructible.pixelsPerUnit - 3;
         *
         * //posX += destructible.originalSpriteOffSet.x;
         * //posY += destructible.originalSpriteOffSet.y;
         *
         * if (destructible.originalSpriteOffSet.Equals(Vector2.zero)) {
         *      destructible.originalSpriteOffSet = new Vector2(posX + 1.5f, posY + 1.5f);
         *
         *      Debug.Log(	destructible.originalSpriteOffSet );
         * }
         *
         * float sizeY = renderCamera.orthographicSize / renderCamera.transform.localScale.x;
         * Vector2D size = new Vector2D(sizeY * ((float)destructible.outputTexture.width / (float)destructible.outputTexture.height), sizeY);
         *
         * size.x *= scaleX;
         * size.y *= scaleY;
         *
         * Max2D.DrawImage(renderCamera.transform, destructible.originalSpriteMaterial, new Vector2D(-posX * 2, -posY * 2), size, 0.1f);
         */
        float    sizeY = renderCamera.orthographicSize / renderCamera.transform.localScale.x;
        Vector2D size  = new Vector2D(sizeY * ((float)destructible.outputTexture.width / (float)destructible.outputTexture.height), sizeY);

        Max2D.DrawImage(renderCamera.transform, destructible.originalSpriteMaterial, new Vector2D(0, 0), size, 0.1f);
    }
    // Lighting Buffers
    void DrawLightingBuffers(float z)
    {
        foreach (LightingSource2D id in LightingSource2D.GetList())
        {
            if (id.buffer == null)
            {
                continue;
            }
            if (id.isActiveAndEnabled == false)
            {
                continue;
            }

            if (id.buffer.bufferCamera == null)
            {
                continue;
            }

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

            Vector3 pos  = id.transform.position - Camera.main.transform.position;
            float   size = id.buffer.bufferCamera.orthographicSize;

            Color lightColor = id.lightColor;
            lightColor.a = id.lightAlpha / 2;

            id.buffer.material.SetColor("_TintColor", lightColor);

            Max2D.DrawImage(id.buffer.material, new Vector2D(pos), new Vector2D(size, size), z);
        }
    }
Esempio n. 5
0
    void Update()
    {
        Transform transform = renderCamera.transform;

        if (destructible.initialized == false)
        {
            Initialize();
            return;
        }

        if (destructible.eraseEvents.Count > 0 || destructible.modifiersAdded == true)
        {
            //DrawSelf

            foreach (DestructionModifier modifier in destructible.modifiers)
            {
                Vector3 pos = (Vector3)modifier.position;

                float ratioX = (destructible.transform.localScale.x / destructible.transform.localScale.y);

                Vector2 size = modifier.size;

                pos.x *= transform.localScale.x;
                pos.y *= transform.localScale.y * ratioX;

                size.y *= ratioX;

                Vector2 scale = new Vector2(destructible.transform.localScale.x, destructible.transform.localScale.y);

                Max2D.DrawImage(transform, modifier.material, pos, size, modifier.rotation, 0.3f, new Vector2D(scale));
            }

            // New Event Meshes That Erase Generation
            foreach (DestructionEvent e in destructible.eraseEvents)
            {
                EraseMesh mesh = e.eraseBrush.GetMesh(transform, destructible.transform);

                destructible.EraseBrushes.Add(mesh);
            }

            // Erase Mesh
            foreach (EraseMesh e in destructible.EraseBrushes)
            {
                float ratioX = destructible.transform.localScale.x / destructible.transform.localScale.y;

                Vector2 scale = destructible.transform.localScale;
                scale.y *= ratioX;

                Max2D.DrawMesh(GetEraseMaterial(), e.mesh, transform, Vector2D.Zero(), transform.position.z + 0.2f, new Vector2D(scale));
            }

            SaveRenderTextureToSprite();

            destructible.eraseEvents.Clear();
            destructible.modifiersAdded = false;
        }
    }
    public void OnRender(Vector2 pos)
    {
        if (modifierID < modifierTextures.Length)
        {
            Material material = new Material(Shader.Find("SmartDestruction2D/ModifierShader"));
            material.mainTexture = modifierTextures[modifierID];

            Max2D.DrawImage(material, pos, modifierSize, modifierRotation, visuals.zPosition);
        }
    }
Esempio n. 7
0
    void Initialize()
    {
        Transform transform = renderCamera.transform;

        //DrawSelf

        foreach (DestructionModifier modifier in destructible.modifiers)
        {
            Vector3 pos = (Vector3)modifier.position;

            float ratioX = (destructible.transform.localScale.x / destructible.transform.localScale.y);

            Vector2 size = modifier.size;

            pos.x *= transform.localScale.x;
            pos.y *= transform.localScale.y * ratioX;

            size.y *= ratioX;

            Vector2 scale = new Vector2(destructible.transform.localScale.x, destructible.transform.localScale.y);

            Max2D.DrawImage(transform, modifier.material, pos, size, modifier.rotation, 0.3f, new Vector2D(scale));
        }

        foreach (Polygon2D p in destructible.erasePolygons)
        {
            Vector2 scale = new Vector2(1, 1);

            Polygon2D polygon = p.ToScale(scale);

            Mesh mesh = polygon.CreateMesh(Vector2.zero, Vector2.zero);

            EraseMesh eraseMesh = new EraseMesh();

            eraseMesh.mesh = mesh;

            destructible.EraseBrushes.Add(eraseMesh);
        }

        // Not Necessary? Why?
        foreach (EraseMesh e in destructible.EraseBrushes)
        {
            float ratioX = destructible.transform.localScale.x / destructible.transform.localScale.y;

            Vector2 scale = destructible.transform.localScale;
            scale.y *= ratioX;

            Max2D.DrawMesh(GetEraseMaterial(), e.mesh, transform, Vector2D.Zero(), transform.position.z + 0.2f, new Vector2D(scale));
        }

        SaveRenderTextureToSprite();

        destructible.initialized = true;
    }
Esempio n. 8
0
    static public void DrawTint()
    {
        float  ratio        = (float)Screen.width / Screen.height;
        Camera camera       = LightingManager2D.Get().GetCamera();
        Camera bufferCamera = LightingMainBuffer2D.Get().bufferCamera;

        Vector2 size = new Vector2(bufferCamera.orthographicSize * ratio, bufferCamera.orthographicSize);
        Vector3 pos  = camera.transform.position;

        LightingManager2D manager = LightingManager2D.Get();

        Max2D.DrawImage(manager.materials.GetAdditive(), new Vector2D(pos), new Vector2D(size), pos.z);
    }
Esempio n. 9
0
    public void OnRenderObject()
    {
        if (Camera.current != bufferCamera)
        {
            return;
        }

        LightingManager2D manager = LightingManager2D.Get();

        Camera camera = manager.GetCamera();

        if (camera == null)
        {
            return;
        }

        LightingDebug.LightMainBufferUpdates += 1;

        float z = transform.position.z;

        Vector2D offset = new Vector2D(-camera.transform.position);

        Max2D.Check();

        if (manager.drawDayShadows)
        {
            LightingDayLighting.Draw(offset, z);

            float darkness = 1f - manager.shadowDarkness;

            manager.materials.GetAdditive().SetColor("_TintColor", new Color(0.5f, 0.5f, 0.5f, darkness));

            Max2D.DrawImage(manager.materials.GetAdditive(), Vector2.zero, LightingManager2D.Render_Size(), 0, z);
        }

        if (manager.drawRooms)
        {
            DrawRooms(offset, z);

            DrawTilemapRooms(offset, z);
        }

        LightingSpriteBuffer.Draw(offset, z);

        DrawLightingBuffers(z);

        if (manager.drawOcclusion)
        {
            LightingOcclussion.Draw(offset, z);
        }
    }
Esempio n. 10
0
    public void OnRenderObject()
    {
        if (Camera.current != bufferCamera)
        {
            return;
        }

        if (Camera.main == null)
        {
            return;
        }

        LightingManager2D.LightingDebug.LightMainBufferUpdates += 1;

        float z = transform.position.z;

        Vector2D offset = new Vector2D(-Camera.main.transform.position);

        Max2D.Check();

        LightingSoftDayShadows.Draw(offset, z);

        foreach (LightingCollider2D id in LightingCollider2D.GetList())
        {
            if (id.dayHeight == false)
            {
                continue;
            }
            Max2D.SetColor(Color.white);
            Max2D.iDrawMesh(id.GetMesh(), id.transform, offset, z);
        }

        float darkness = 1f - LightingManager2D.Get().shadowDarkness;

        LightingManager2D.Get().additiveMaterial.SetColor("_TintColor", new Color(0.5f, 0.5f, 0.5f, darkness));

        float ratio = (float)Screen.width / Screen.height;

        Vector2 size = new Vector2(Camera.main.orthographicSize * ratio, Camera.main.orthographicSize);

        Max2D.DrawImage(LightingManager2D.Get().additiveMaterial, Vector2.zero, size, 0, z);

        DrawRooms(offset, z);

        DrawLightingBuffers(z);

        LightingOcclussion.Draw(offset, z);
    }
Esempio n. 11
0
    // Lighting Buffers
    void DrawLightingBuffers(float z)
    {
        Camera   camera = LightingManager2D.Get().GetCamera();
        Vector2D pos2D  = Vector2D.Zero();
        Vector2D size2D = Vector2D.Zero();

        foreach (LightingSource2D id in LightingSource2D.GetList())
        {
            if (id.buffer == null)
            {
                continue;
            }
            if (id.isActiveAndEnabled == false)
            {
                continue;
            }

            if (id.buffer.bufferCamera == null)
            {
                continue;
            }

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

            Vector3 pos = id.transform.position - camera.transform.position;
            pos2D.x = pos.x;
            pos2D.y = pos.y;

            float size = id.buffer.bufferCamera.orthographicSize;
            size2D.x = size;
            size2D.y = size;

            Color lightColor = id.lightColor;
            lightColor.a = id.lightAlpha / 2;

            id.buffer.GetMaterial().SetColor("_TintColor", lightColor);

            Max2D.DrawImage(id.buffer.GetMaterial(), pos2D, size2D, z);
        }
    }
Esempio n. 12
0
    public void DrawModifiers(Transform transform)
    {
        foreach (DestructionModifier modifier in destructible.modifiers)
        {
            Vector3 pos = (Vector3)modifier.position;

            float ratioX = (destructible.transform.localScale.x / destructible.transform.localScale.y);

            Vector2 size = modifier.size;

            pos.x *= transform.localScale.x;
            pos.y *= transform.localScale.y * ratioX;

            size.y *= ratioX;

            Vector2 scale = new Vector2(destructible.transform.localScale.x, destructible.transform.localScale.y);

            Max2D.DrawImage(transform, modifier.material, pos, size, modifier.rotation, 0.3f, new Vector2D(scale));
        }
    }
Esempio n. 13
0
    void Render_PostRenderMode()
    {
        // Post-Render Mode Drawing
        if (Render_Check() == false)
        {
            return;
        }

        if (renderingMode != RenderingMode.OnPostRender)
        {
            return;
        }

        Camera camera = GetCamera();

        if (Camera.current != camera)
        {
            return;
        }

        LightingDebug.LightMainCameraUpdates += 1;

        Max2D.DrawImage(mainBuffer.material, Render_Position(), Render_Size(), camera.transform.eulerAngles.z, Render_Position().z);
    }
Esempio n. 14
0
    public static void Draw(Vector2D offset, float z)
    {
        float sunDirection = LightingManager2D.GetSunDirection();

        // Day Soft Shadows
        GL.PushMatrix();
        Max2D.defaultMaterial.SetPass(0);
        GL.Begin(GL.TRIANGLES);
        GL.Color(Color.black);

        foreach (LightingCollider2D id in LightingCollider2D.GetList())
        {
            if (id.dayHeight == false || id.height <= 0)
            {
                continue;
            }

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

            List <Polygon2D> polygons = null;
            switch (id.colliderType)
            {
            case LightingCollider2D.ColliderType.Collider:
                polygons = id.GetColliderPolygons();
                break;

            case LightingCollider2D.ColliderType.SpriteCustomPhysicsShape:
                polygons = id.GetShapePolygons();
                break;
            }

            Polygon2D poly;
            Vector3   vecA;
            Vector3   vecB;
            Vector3   vecC;

            foreach (Polygon2D polygon in polygons)
            {
                poly = polygon.ToWorldSpace(id.gameObject.transform);
                Polygon2D convexHull = Polygon2D.GenerateShadow(new Polygon2D(poly.pointsList), sunDirection, id.height);

                Mesh mesh = convexHull.CreateMesh(Vector2.zero, Vector2.zero);

                for (int i = 0; i < mesh.triangles.GetLength(0); i = i + 3)
                {
                    vecA = mesh.vertices [mesh.triangles [i]];
                    vecB = mesh.vertices [mesh.triangles [i + 1]];
                    vecC = mesh.vertices [mesh.triangles [i + 2]];
                    Max2DMatrix.DrawTriangle(vecA.x, vecA.y, vecB.x, vecB.y, vecC.x, vecC.y, offset, z);
                }
            }
        }

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

        GL.PushMatrix();
        // Null Check?
        LightingManager2D.Get().shadowBlurMaterial.SetPass(0);
        GL.Begin(GL.TRIANGLES);
        Max2D.SetColor(Color.white);

        foreach (LightingCollider2D id in LightingCollider2D.GetList())
        {
            if (id.dayHeight == false || id.height <= 0)
            {
                continue;
            }

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

            List <Polygon2D> polygons = null;
            switch (id.colliderType)
            {
            case LightingCollider2D.ColliderType.Collider:
                polygons = id.GetColliderPolygons();
                break;

            case LightingCollider2D.ColliderType.SpriteCustomPhysicsShape:
                polygons = id.GetShapePolygons();
                break;
            }

            foreach (Polygon2D polygon in polygons)
            {
                Polygon2D poly       = polygon.ToWorldSpace(id.gameObject.transform);
                Polygon2D convexHull = Polygon2D.GenerateShadow(new Polygon2D(poly.pointsList), sunDirection, id.height);

                foreach (DoublePair2D p in DoublePair2D.GetList(convexHull.pointsList))
                {
                    Vector2D zA = new Vector2D(p.A + offset);
                    Vector2D zB = new Vector2D(p.B + offset);
                    Vector2D zC = zB.Copy();

                    Vector2D pA = zA.Copy();
                    Vector2D pB = zB.Copy();

                    zA.Push(Vector2D.Atan2(p.A, p.B) + pi2, .5f);
                    zB.Push(Vector2D.Atan2(p.A, p.B) + pi2, .5f);
                    zC.Push(Vector2D.Atan2(p.B, p.C) + pi2, .5f);

                    GL.TexCoord2(uv0, uv0);
                    Max2D.Vertex3(pB, z);
                    GL.TexCoord2(0.5f - uv0, uv0);
                    Max2D.Vertex3(pA, z);
                    GL.TexCoord2(0.5f - uv0, uv1);
                    Max2D.Vertex3(zA, z);

                    GL.TexCoord2(uv0, uv1);
                    Max2D.Vertex3(zA, z);
                    GL.TexCoord2(0.5f - uv0, uv1);
                    Max2D.Vertex3(zB, z);
                    GL.TexCoord2(0.5f - uv0, uv0);
                    Max2D.Vertex3(pB, z);

                    GL.TexCoord2(uv0, uv1);
                    Max2D.Vertex3(zB, z);
                    GL.TexCoord2(0.5f - uv0, uv0);
                    Max2D.Vertex3(pB, z);
                    GL.TexCoord2(0.5f - uv0, uv1);
                    Max2D.Vertex3(zC, z);
                }
            }
        }

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

                #if UNITY_2018_1_OR_NEWER
        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;
            }
            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)
                    {
                        DrawSoftShadowTile(offset + new Vector2D(x, y), z, id.height);
                    }
                }
            }
        }

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



        GL.PushMatrix();
        LightingManager2D.Get().shadowBlurMaterial.SetPass(0);
        GL.Begin(GL.TRIANGLES);
        Max2D.SetColor(Color.white);



        // Day Soft Shadows
        foreach (LightingTilemapCollider2D id in LightingTilemapCollider2D.GetList())
        {
            if (id.map == null)
            {
                continue;
            }
            if (id.dayHeight == false)
            {
                continue;
            }
            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)
                    {
                        DrawSoftShadowTileBlur(offset + new Vector2D(x, y), z, id.height);
                    }
                }
            }
        }

        GL.End();
        GL.PopMatrix();
                #endif

        /*
         *
         * Material material = LightingManager2D.Get().whiteSpriteMaterial;
         * foreach (LightingSprite2D id in LightingSprite2D.GetList()) {
         *      if (id.GetSpriteRenderer() == null) {
         *              continue;
         *      }
         *      material.mainTexture = id.GetSpriteRenderer().sprite.texture; //Debug.Log(sprite.pivot);
         *
         *      Vector2 p = id.transform.position;
         *      Vector2 scale = id.transform.lossyScale;
         *
         *      if (id.GetSpriteRenderer().flipX) {
         *              scale.x = -scale.x;
         *      }
         *
         *      if (id.GetSpriteRenderer().flipY) {
         *              scale.y = -scale.y;
         *      }
         *
         *      Max2D.DrawImage(material, offset.ToVector2() + p, scale, id.transform.rotation.eulerAngles.z, z);
         * } */

        float   ratio        = (float)Screen.width / Screen.height;
        Camera  bufferCamera = LightingMainBuffer2D.Get().bufferCamera;
        Vector2 size         = new Vector2(bufferCamera.orthographicSize * ratio, bufferCamera.orthographicSize);
        Vector3 pos          = Camera.main.transform.position;

        Max2D.DrawImage(LightingManager2D.Get().additiveMaterial, new Vector2D(pos), new Vector2D(size), pos.z);

        // Day Lighting Masking
        foreach (LightingCollider2D id in LightingCollider2D.GetList())
        {
            if (id.generateDayMask == false)
            {
                continue;
            }

            switch (id.maskType)
            {
            case LightingCollider2D.MaskType.SpriteCustomPhysicsShape:
                Max2D.SetColor(Color.white);
                Max2D.DrawMesh(Max2D.defaultMaterial, id.GetShapeMesh(), id.transform, offset, z);

                break;

            case LightingCollider2D.MaskType.Collider:
                Max2D.SetColor(Color.white);
                Max2D.DrawMesh(Max2D.defaultMaterial, id.GetColliderMesh(), id.transform, offset, z);

                break;

            case LightingCollider2D.MaskType.Sprite:
                if (id.spriteRenderer == null || id.spriteRenderer.sprite == null)
                {
                    break;
                }

                Material material = LightingManager2D.Get().whiteSpriteMaterial;
                material.mainTexture = id.spriteRenderer.sprite.texture;

                Max2D.DrawSprite(material, id.spriteRenderer, new Vector2(id.transform.position.x, id.transform.position.y) + offset.ToVector2(), new Vector2(1, 1), id.transform.rotation.eulerAngles.z, z);

                break;
            }
        }
    }
    public void OnRenderObject()
    {
        if (Camera.current != bufferCamera)
        {
            return;
        }

        LightingManager2D.LightingDebug.LightBufferUpdates++;

        if (lightSource == null)
        {
            return;
        }

        if (lightSource.update == false)
        {
            bufferCamera.enabled = false;
        }

        LateUpdate();

        lightSource.update = false;

        material.SetColor("_TintColor", lightSource.color);

        Vector2D vZero = new Vector2D(0, 0);
        float    z     = transform.position.z;

        Max2D.Check();

        GL.PushMatrix();
        Max2D.defaultMaterial.SetPass(0);

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

        foreach (LightingCollider2D id in LightingCollider2D.GetList())
        {
            Polygon2D poly = id.GetPolygon();
            poly = poly.ToWorldSpace(id.gameObject.transform);
            poly = poly.ToOffset(new Vector2D(-lightSource.transform.position));

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

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

                vA.Push(Vector2D.Atan2(vA, vZero), 15);
                vB.Push(Vector2D.Atan2(vB, vZero), 15);

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

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

        GL.PushMatrix();

        LightingManager2D.Get().penumbraMaterial.SetPass(0);

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

        const float uv0 = 1f / 128f;
        const float uv1 = 1f - uv0;

        foreach (LightingCollider2D id in LightingCollider2D.GetList())
        {
            Polygon2D poly = id.GetPolygon();
            poly = poly.ToWorldSpace(id.gameObject.transform);
            poly = poly.ToOffset(new Vector2D(-lightSource.transform.position));

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

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

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

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

                vA.Push(angleA, lightSource.lightSize);
                pA.Push(angleA - Mathf.Deg2Rad * 25, lightSource.lightSize);

                vB.Push(angleB, lightSource.lightSize);
                pB.Push(angleB + Mathf.Deg2Rad * 25, lightSource.lightSize);

                GL.TexCoord2(uv0, uv0);
                GL.Vertex3((float)p.A.x, (float)p.A.y, z);
                GL.TexCoord2(uv1, uv0);
                GL.Vertex3((float)vA.x, (float)vA.y, z);
                GL.TexCoord2((float)uv0, uv1);
                GL.Vertex3((float)pA.x, (float)pA.y, z);

                GL.TexCoord2(uv0, uv0);
                GL.Vertex3((float)p.B.x, (float)p.B.y, z);
                GL.TexCoord2(uv1, uv0);
                GL.Vertex3((float)vB.x, (float)vB.y, z);
                GL.TexCoord2(uv0, uv1);
                GL.Vertex3((float)pB.x, (float)pB.y, z);
            }
        }

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

        Max2D.SetColor(Color.white);
        foreach (LightingCollider2D id in LightingCollider2D.GetList())
        {
            Max2D.iDrawMesh(id.GetMesh(), id.transform, new Vector2D(-lightSource.transform.position), z);
        }

        Vector2 size = new Vector2(bufferCamera.orthographicSize, bufferCamera.orthographicSize);

        if (lightSource.rotationEnabled)
        {
            Max2D.DrawImage(lightSource.GetMaterial(), Vector2.zero, size, lightSource.transform.rotation.eulerAngles.z, z);
        }
        else
        {
            Max2D.DrawImage(lightSource.GetMaterial(), Vector2.zero, size, 0, z);
        }

        GL.PushMatrix();
        Max2D.SetColor(Color.black);
        Max2D.defaultMaterial.color = Color.black;
        Max2D.defaultMaterial.SetPass(0);

        float rotation    = lightSource.transform.rotation.eulerAngles.z;
        float squaredSize = Mathf.Sqrt((size.x * size.x) + (size.y * size.y));

        Vector2 p0 = Vector2D.RotToVec((double)rotation).ToVector2() * squaredSize;
        Vector2 p1 = Vector2D.RotToVec((double)rotation + Mathf.PI / 4).ToVector2() * squaredSize;

        Max2DMatrix.DrawTriangle(Vector2.zero, p0, p1, Vector2.zero, z);
        Max2DMatrix.DrawTriangle(Vector2.zero, p1, p0, Vector2.zero, z);

        //Max2DMatrix.DrawTriangle(vA, vB, p.B, vZero, z);

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

        Max2D.defaultMaterial.color = Color.white;

        //lightSource = null;
        //bufferCamera.enabled = false;
    }
Esempio n. 16
0
    void DrawCollideMask()
    {
        float z = transform.position.z;

        Vector2D offset = new Vector2D(-lightSource.transform.position);

        Material material = LightingManager2D.Get().whiteSpriteMaterial;

        // For Collider Sprite Mask
        foreach (LightingCollider2D id in LightingCollider2D.GetList())
        {
            Sprite sprite = id.lightSprite;

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

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

            material.mainTexture = sprite.texture;

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

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

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

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

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

        GL.PushMatrix();
        Max2D.defaultMaterial.SetPass(0);
        GL.Begin(GL.TRIANGLES);
        GL.Color(Color.white);

        // For Collider Mask
        foreach (LightingCollider2D id in LightingCollider2D.GetList())
        {
            Mesh mesh = id.GetMesh();

            if (mesh == null)
            {
                continue;
            }

            if (id.maskType != LightingCollider2D.MaskType.Collider)
            {
                continue;
            }

            for (int i = 0; i < mesh.triangles.GetLength(0); i = i + 3)
            {
                Vector2 a = id.transform.TransformPoint(mesh.vertices [mesh.triangles [i]]);
                Vector2 b = id.transform.TransformPoint(mesh.vertices [mesh.triangles [i + 1]]);
                Vector2 c = id.transform.TransformPoint(mesh.vertices [mesh.triangles [i + 2]]);
                Max2DMatrix.DrawTriangle(a, b, c, offset.ToVector2(), z);
            }
        }


        GL.End();
        GL.PopMatrix();
    }
Esempio n. 17
0
    void Update()
    {
        Transform transform = renderCamera.transform;

        if (destructible.initialized == false)
        {
            Initialize();
            return;
        }

        if (destructible.eraseEvents.Count > 0 || destructible.modifiersAdded == true)
        {
            MeshRenderer meshRenderer = destructible.gameObject.GetComponent <MeshRenderer>();
            destructible.outputMaterial.mainTexture = destructible.outputTexture;

            DrawSelf();

            foreach (DestructionModifier modifier in destructible.modifiers)
            {
                Vector3 pos = (Vector3)modifier.position;

                float ratioX = (destructible.transform.localScale.x / destructible.transform.localScale.y);

                Vector2 size = modifier.size;

                pos.x *= transform.localScale.x;
                pos.y *= transform.localScale.y * ratioX;

                size.y *= ratioX;

                Vector2 scale = new Vector2(destructible.transform.localScale.x, destructible.transform.localScale.y);

                Max2D.DrawImage(transform, modifier.material, pos, size, modifier.rotation, 0.3f, new Vector2D(scale));
            }

            // New Event Meshes That Erase Generation
            foreach (DestructionEvent e in destructible.eraseEvents)
            {
                EraseMesh mesh = e.eraseBrush.GetMesh(transform, destructible.transform);
                destructible.EraseBrushes.Add(mesh);
            }

            // Erase Mesh
            foreach (EraseMesh e in destructible.EraseBrushes)
            {
                float ratioX = destructible.transform.localScale.x / destructible.transform.localScale.y;

                Vector2 scale = destructible.transform.localScale;
                scale.y *= ratioX;

                Max2D.DrawMesh(GetEraseMaterial(), e.mesh, transform, Vector2D.Zero(), transform.position.z + 0.2f, new Vector2D(scale));
            }

            RenderTexture.active = destructible.renderTexture;
            destructible.outputTexture.ReadPixels(new Rect(0, 0, destructible.outputTexture.width, destructible.outputTexture.height), 0, 0);
            destructible.outputTexture.Apply();

            destructible.outputMaterial.mainTexture = destructible.renderTexture;
            meshRenderer.material = destructible.outputMaterial;

            destructible.eraseEvents.Clear();
        }
    }
Esempio n. 18
0
    void DrawCollideMask()
    {
        float z = transform.position.z;

        Vector2D offset = new Vector2D(-lightSource.transform.position);

        Material material = LightingManager2D.Get().whiteSpriteMaterial;

        // For Collider Sprite Mask
        foreach (LightingCollider2D id in LightingCollider2D.GetList())
        {
            Sprite sprite = id.lightSprite;

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

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

            material.mainTexture = sprite.texture;

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

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

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

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

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

        GL.PushMatrix();
        Max2D.defaultMaterial.SetPass(0);
        GL.Begin(GL.TRIANGLES);
        GL.Color(Color.white);

        // For Collider Mask
        foreach (LightingCollider2D id in LightingCollider2D.GetList())
        {
            Mesh mesh = id.GetMesh();

            if (mesh == null)
            {
                continue;
            }

            if (id.maskType != LightingCollider2D.MaskType.Collider)
            {
                continue;
            }

            for (int i = 0; i < mesh.triangles.GetLength(0); i = i + 3)
            {
                Vector2 a = id.transform.TransformPoint(mesh.vertices [mesh.triangles [i]]);
                Vector2 b = id.transform.TransformPoint(mesh.vertices [mesh.triangles [i + 1]]);
                Vector2 c = id.transform.TransformPoint(mesh.vertices [mesh.triangles [i + 2]]);
                Max2DMatrix.DrawTriangle(a, b, c, offset.ToVector2(), z);
            }
        }

        Mesh tileMesh = GetTileMesh();

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

            Vector3 rot = 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;
            float scaleY = id.transform.lossyScale.y * rotationYScale;

            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] == false)
                    {
                        continue;
                    }

                    Vector2D polyOffset = Vector2D.Zero();
                    polyOffset += new Vector2D(x + 0.5f, y + 0.5f);
                    polyOffset += new Vector2D(id.area.position.x, id.area.position.y);
                    polyOffset += new Vector2D(id.transform.position.x, id.transform.position.y);

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

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

                    polyOffset += offset;

                    for (int i = 0; i < tileMesh.triangles.GetLength(0); i = i + 3)
                    {
                        Vector2 a = tileMesh.vertices [tileMesh.triangles [i]];
                        Vector2 b = tileMesh.vertices [tileMesh.triangles [i + 1]];
                        Vector2 c = tileMesh.vertices [tileMesh.triangles [i + 2]];
                        Max2DMatrix.DrawTriangle(a, b, c, polyOffset.ToVector2(), z, new Vector2D(scaleX, scaleY));
                    }
                }
            }
        }

        GL.End();
        GL.PopMatrix();
    }
Esempio n. 19
0
    void DrawLightTexture()
    {
        float   z    = transform.position.z;
        Vector2 size = new Vector2(bufferCamera.orthographicSize, bufferCamera.orthographicSize);

        if (lightSource.rotationEnabled)
        {
            Max2D.DrawImage(lightSource.GetMaterial(), Vector2.zero, size, lightSource.transform.rotation.eulerAngles.z, z);


            // Light Rotation!!!

            GL.PushMatrix();
            Max2D.SetColor(Color.black);
            GL.Begin(GL.TRIANGLES);

            Max2D.defaultMaterial.color = Color.black;
            Max2D.defaultMaterial.SetPass(0);

            float rotation    = lightSource.transform.rotation.eulerAngles.z * Mathf.Deg2Rad + Mathf.PI / 4;
            float squaredSize = Mathf.Sqrt((size.x * size.x) + (size.y * size.y));

            Vector2 p0 = Vector2D.RotToVec((double)rotation).ToVector2() * squaredSize;
            Vector2 p1 = Vector2D.RotToVec((double)rotation + Mathf.PI / 2).ToVector2() * squaredSize;
            Vector2 p2 = Vector2D.RotToVec((double)rotation + Mathf.PI).ToVector2() * squaredSize;
            Vector2 p3 = Vector2D.RotToVec((double)rotation - Mathf.PI / 2).ToVector2() * squaredSize;

            Vector2 up0 = p1 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 + Mathf.PI / 2).ToVector2() * squaredSize;
            Vector2 up1 = p1 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4).ToVector2() * squaredSize;
            up1 += Vector2D.RotToVec((double)rotation + Mathf.PI / 4 + Mathf.PI / 2).ToVector2() * squaredSize;

            Vector2 up2 = p0 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4).ToVector2() * squaredSize;
            up2 += Vector2D.RotToVec((double)rotation + Mathf.PI / 4 - Mathf.PI / 2).ToVector2() * squaredSize;

            Vector2 up3 = p0 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 - Mathf.PI / 2).ToVector2() * squaredSize;


            Vector2 down0 = p3 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 + Mathf.PI / 2 - Mathf.PI).ToVector2() * squaredSize;
            Vector2 down1 = p3 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 - Mathf.PI).ToVector2() * squaredSize;
            down1 += Vector2D.RotToVec((double)rotation + Mathf.PI / 4 + Mathf.PI / 2 - Mathf.PI).ToVector2() * squaredSize;

            Vector2 down2 = p2 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 - Mathf.PI).ToVector2() * squaredSize;
            down2 += Vector2D.RotToVec((double)rotation + Mathf.PI / 4 - Mathf.PI / 2 - Mathf.PI).ToVector2() * squaredSize;

            Vector2 down3 = p2 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 - Mathf.PI / 2 - Mathf.PI).ToVector2() * squaredSize;


            Vector2 left0 = p0 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 + Mathf.PI / 2 - Mathf.PI / 2).ToVector2() * squaredSize;
            Vector2 left1 = p0 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 - Mathf.PI / 2).ToVector2() * squaredSize;
            left1 += Vector2D.RotToVec((double)rotation + Mathf.PI / 4 + Mathf.PI / 2 - Mathf.PI / 2).ToVector2() * squaredSize;

            Vector2 left2 = p3 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 - Mathf.PI / 2).ToVector2() * squaredSize;
            left2 += Vector2D.RotToVec((double)rotation + Mathf.PI / 4 - Mathf.PI / 2 - Mathf.PI / 2).ToVector2() * squaredSize;

            Vector2 left3 = p3 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 - Mathf.PI / 2 - Mathf.PI / 2).ToVector2() * squaredSize;


            Vector2 right0 = p2 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 + Mathf.PI / 2 + Mathf.PI / 2).ToVector2() * squaredSize;
            Vector2 right1 = p2 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 + Mathf.PI / 2).ToVector2() * squaredSize;
            left1 += Vector2D.RotToVec((double)rotation + Mathf.PI / 4 + Mathf.PI / 2 + Mathf.PI / 2).ToVector2() * squaredSize;

            Vector2 right2 = p1 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 + Mathf.PI / 2).ToVector2() * squaredSize;
            left2 += Vector2D.RotToVec((double)rotation + Mathf.PI / 4 - Mathf.PI / 2 + Mathf.PI / 2).ToVector2() * squaredSize;

            Vector2 right3 = p1 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 - Mathf.PI / 2 + Mathf.PI / 2).ToVector2() * squaredSize;

            Max2DMatrix.DrawTriangle(right0, right1, right2, Vector2.zero, z);
            Max2DMatrix.DrawTriangle(right2, right3, right0, Vector2.zero, z);

            Max2DMatrix.DrawTriangle(left0, left1, left2, Vector2.zero, z);
            Max2DMatrix.DrawTriangle(left2, left3, left0, Vector2.zero, z);


            Max2DMatrix.DrawTriangle(down0, down1, down2, Vector2.zero, z);
            Max2DMatrix.DrawTriangle(down2, down3, down0, Vector2.zero, z);


            Max2DMatrix.DrawTriangle(up0, up1, up2, Vector2.zero, z);
            Max2DMatrix.DrawTriangle(up2, up3, up0, Vector2.zero, z);

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

            Max2D.defaultMaterial.color = Color.white;
        }
        else
        {
            Max2D.DrawImage(lightSource.GetMaterial(), Vector2.zero, size, 0, z);
        }
    }
Esempio n. 20
0
    public static void Draw(Vector2D offset, float z)
    {
        float sunDirection = LightingManager2D.GetSunDirection();

        // Day Soft Shadows
        GL.PushMatrix();
        Max2D.defaultMaterial.SetPass(0);
        GL.Begin(GL.TRIANGLES);
        GL.Color(Color.black);

        foreach (LightingCollider2D id in LightingCollider2D.GetList())
        {
            if (id.dayHeight == false || id.height <= 0)
            {
                continue;
            }
            List <Polygon2D> polygons = id.GetPolygons();

            foreach (Polygon2D polygon in polygons)
            {
                Polygon2D poly = polygon;
                poly = poly.ToWorldSpace(id.gameObject.transform);

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

                    vA.Push(sunDirection, id.height);
                    vB.Push(sunDirection, id.height);

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

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

        GL.PushMatrix();
        LightingManager2D.Get().shadowBlurMaterial.SetPass(0);
        GL.Begin(GL.TRIANGLES);
        Max2D.SetColor(Color.white);

        foreach (LightingCollider2D id in LightingCollider2D.GetList())
        {
            if (id.dayHeight == false || id.height <= 0)
            {
                continue;
            }
            List <Polygon2D> polygons = id.GetPolygons();

            foreach (Polygon2D polygon in polygons)
            {
                Polygon2D poly = polygon;
                poly = poly.ToWorldSpace(id.gameObject.transform);

                Polygon2D convexHull = Polygon2D.GenerateShadow(new Polygon2D(poly.pointsList), sunDirection, id.height);

                foreach (DoublePair2D p in DoublePair2D.GetList(convexHull.pointsList))
                {
                    Vector2D zA = new Vector2D(p.A + offset);
                    Vector2D zB = new Vector2D(p.B + offset);
                    Vector2D zC = zB.Copy();

                    Vector2D pA = zA.Copy();
                    Vector2D pB = zB.Copy();

                    zA.Push(Vector2D.Atan2(p.A, p.B) + pi2, .5f);
                    zB.Push(Vector2D.Atan2(p.A, p.B) + pi2, .5f);
                    zC.Push(Vector2D.Atan2(p.B, p.C) + pi2, .5f);

                    GL.TexCoord2(uv0, uv0);
                    Max2D.Vertex3(pB, z);
                    GL.TexCoord2(0.5f - uv0, uv0);
                    Max2D.Vertex3(pA, z);
                    GL.TexCoord2(0.5f - uv0, uv1);
                    Max2D.Vertex3(zA, z);

                    GL.TexCoord2(uv0, uv1);
                    Max2D.Vertex3(zA, z);
                    GL.TexCoord2(0.5f - uv0, uv1);
                    Max2D.Vertex3(zB, z);
                    GL.TexCoord2(0.5f - uv0, uv0);
                    Max2D.Vertex3(pB, z);

                    GL.TexCoord2(uv0, uv1);
                    Max2D.Vertex3(zB, z);
                    GL.TexCoord2(0.5f - uv0, uv0);
                    Max2D.Vertex3(pB, z);
                    GL.TexCoord2(0.5f - uv0, uv1);
                    Max2D.Vertex3(zC, z);
                }
            }
        }

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

        GL.PushMatrix();
        Max2D.defaultMaterial.SetPass(0);
        GL.Begin(GL.TRIANGLES);
        GL.Color(Color.black);


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

        GL.PushMatrix();
        LightingManager2D.Get().shadowBlurMaterial.SetPass(0);
        GL.Begin(GL.TRIANGLES);
        Max2D.SetColor(Color.white);


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

        Material material = LightingManager2D.Get().whiteSpriteMaterial;

        foreach (LightingSprite2D id in LightingSprite2D.GetList())
        {
            if (id.GetSpriteRenderer() == null)
            {
                continue;
            }
            material.mainTexture = id.GetSpriteRenderer().sprite.texture;             //Debug.Log(sprite.pivot);

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

            if (id.GetSpriteRenderer().flipX)
            {
                scale.x = -scale.x;
            }

            if (id.GetSpriteRenderer().flipY)
            {
                scale.y = -scale.y;
            }

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

        material = LightingManager2D.Get().additiveMaterial;
        foreach (LightingSpriteRenderer2D id in LightingSpriteRenderer2D.GetList())
        {
            if (id.sprite == null)
            {
                continue;
            }
            material.mainTexture = id.sprite.texture;             //Debug.Log(sprite.pivot);

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

            float scaleX = (float)id.sprite.texture.width / (id.sprite.pixelsPerUnit * 2);
            float scaleY = (float)id.sprite.texture.width / (id.sprite.pixelsPerUnit * 2);
            //Debug.Log(scaleX + " " + scaleY);

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

            scale.x *= id.scale.x;
            scale.y *= id.scale.y;

            if (id.flipX)
            {
                scale.x = -scale.x;
            }

            if (id.flipY)
            {
                scale.y = -scale.y;
            }

            //material.color = id.color;
            Color color = id.color;
            color.a = id.alpha;

            material.SetColor("_TintColor", color);
            Max2D.DrawImage(material, offset.ToVector2() + position + id.offset, scale, id.transform.rotation.eulerAngles.z, z);
        }

        material.mainTexture = null;

        float   ratio        = (float)Screen.width / Screen.height;
        Camera  bufferCamera = LightingMainBuffer2D.Get().bufferCamera;
        Vector2 size         = new Vector2(bufferCamera.orthographicSize * ratio, bufferCamera.orthographicSize);
        Vector3 pos          = Camera.main.transform.position;

        Max2D.iDrawImage(LightingManager2D.Get().additiveMaterial, new Vector2D(pos), new Vector2D(size), pos.z);
    }