Пример #1
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);
        }
    }
    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;
    }