示例#1
0
    public DayLightingShadowCollider GetDayLightingShadow(float sunDirection)
    {
        if (dayLightingShadow == null)
        {
            dayLightingShadow = new DayLightingShadowCollider();
        }
        else
        {
            if ((int)sunDirection == (int)dayLightingShadow.sunDirection)
            {
                return(dayLightingShadow);
            }
        }

        dayLightingShadow.sunDirection = sunDirection;
        dayLightingShadow.polygons.Clear();
        dayLightingShadow.meshes.Clear();

        sunDirection *= Mathf.Deg2Rad;

        Polygon2D poly;
        Mesh      mesh;

        List <Polygon2D> polygons = shape.GetPolygons_Local_ColliderType(transform);

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

            poly.ToOffsetItself(new Vector2D(-gameObject.transform.position));

            dayLightingShadow.polygons.Add(poly.Copy());

            mesh = poly.CreateMesh(Vector2.zero, Vector2.zero);

            dayLightingShadow.meshes.Add(mesh);

            LightingDebug.ConvexHullGenerations++;
        }

        return(dayLightingShadow);
    }
示例#2
0
    static public void DrawColliders(Vector2D offset, float z)
    {
        float sunDirection = LightingManager2D.GetSunDirection();

        LightingManager2D manager = LightingManager2D.Get();

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

        Vector3  vecA, vecB, vecC;
        Vector2D zA, zB, zC;
        Vector2D pA, pB;

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

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

            // Distance Check?
            if (id.InCamera() == false)
            {
                continue;
            }

            DayLightingShadowCollider shadow = id.GetDayLightingShadow(sunDirection * Mathf.Rad2Deg);

            foreach (Mesh mesh in shadow.meshes)
            {
                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 + new Vector2D(id.transform.position), z);
                }
            }
        }

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

        GL.PushMatrix();

        manager.materials.GetShadowBlur().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.shape.colliderType == LightingCollider2D.ColliderType.Mesh)
            {
                continue;
            }

            // Distance Check?
            if (id.InCamera() == false)
            {
                continue;
            }

            DayLightingShadowCollider shadow = id.GetDayLightingShadow(sunDirection * Mathf.Rad2Deg);

            Vector2D gameObjectOffset = new Vector2D(id.transform.position);

            foreach (Polygon2D polygon in shadow.polygons)
            {
                foreach (DoublePair2D p in DoublePair2D.GetList(polygon.pointsList))
                {
                    zA  = p.A.Copy();
                    zA += offset + gameObjectOffset;

                    zB  = p.B.Copy();
                    zB += offset + gameObjectOffset;

                    zC = zB.Copy();

                    pA = zA.Copy();
                    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();
    }