示例#1
0
        void RenderPolygon(Polygon polygon, Vector2 lightPos)
        {
            var totalVerts = polygon.Points.Length;

            for (var i = 0; i < totalVerts; i++)
            {
                var vertex     = polygon.Points[i] + polygon.position;
                var nextVertex = polygon.Points[(i + 1) % totalVerts] + polygon.position;
                var startToEnd = nextVertex - vertex;
                var normal     = new Vector2(startToEnd.Y, -startToEnd.X);
                normal.Normalize();
                var lightToStart = lightPos - vertex;

                Vector2.Dot(ref normal, ref lightToStart, out var nDotL);
                if (nDotL > 0)
                {
                    var midpoint = (nextVertex + vertex) * 0.5f;
                    Debug.DrawLine(vertex, nextVertex, Color.Green);
                    Debug.DrawLine(midpoint, midpoint + normal * 20, Color.Green);

                    var point1 = vertex + (Vector2.Normalize(vertex - lightPos) * Screen.Width);
                    var point2 = nextVertex + (Vector2.Normalize(nextVertex - lightPos) * Screen.Width);

                    var poly = new Vector2[] { nextVertex, point2, point1, vertex };
                    _primitiveBatch.DrawPolygon(poly, 4, Color.Black);
                }
            }
        }
示例#2
0
        void RenderVerts(System.Numerics.Vector2 position, System.Numerics.Vector2 lightPos, System.Numerics.Vector2[] verts)
        {
            // TODO: should we check to see if the light is inside the verts and early out?
            for (var i = 0; i < verts.Length; i++)
            {
                var vertex       = verts[i] + position;
                var nextVertex   = verts[(i + 1) % verts.Length] + position;
                var startToEnd   = nextVertex - vertex;
                var lightToStart = lightPos - vertex;
                var normal       = new System.Numerics.Vector2(startToEnd.Y, -startToEnd.X);
                normal.Normalize();

                var nDotL = System.Numerics.Vector2.Dot(normal, lightToStart);
                if (nDotL > 0)
                {
                    var midpoint = (nextVertex + vertex) * 0.5f;
                    Debug.DrawLine(vertex, nextVertex, Color.Green);
                    Debug.DrawLine(midpoint, midpoint + normal * 20, Color.Green);

                    var point1 = nextVertex + (System.Numerics.Vector2.Normalize(nextVertex - lightPos) * Screen.Width);
                    var point2 = vertex + (System.Numerics.Vector2.Normalize(vertex - lightPos) * Screen.Width);

                    var poly = new System.Numerics.Vector2[] { nextVertex, point1, point2, vertex };
                    _vertBuffer[0] = nextVertex;
                    _vertBuffer[1] = point1;
                    _vertBuffer[2] = point2;
                    _vertBuffer[3] = vertex;
                    _primitiveBatch.DrawPolygon(poly, 4, Color.Black);
                }
            }
        }