Exemplo n.º 1
0
        public void UpdateDebugInput(Matrix worldMatrix, Keys toggleKey = Keys.L)
        {
            //if (!Input.GameIsActive) return;

            //if (Input.KeyPressed(toggleKey))
            //    EnableDebugLight = !EnableDebugLight;

            if (EnableDebugLight)
            {
                if (debugLight == null)
                {
                    debugLight = new Light2D();
                }

                if (!Lights.Contains(debugLight))
                {
                    Lights.Add(debugLight);
                }

                debugLight.Position = Q19.Q19Game.Instance.MousePosition;// Vector2.Transform(Game.Frames.Main.TransformScreenToViewport(Input.MousePos), Matrix.Invert(worldMatrix));  //TODO: Should use the current frame
            }
            else
            {
                if (Lights.Contains(debugLight))
                {
                    Lights.Remove(debugLight);
                }
                return;
            }

            //Ward.Watch("Lighting2D.lights", Lights.Count);
            //Ward.Watch("Lighting2D.objs", VertexObjs.Count);
            //Ward.Watch("Lighting2D.debuglight", debugLight.ToString());

            //var scrollDelta = Input.States.MouseScrollDelta;
            //if (scrollDelta != 0)
            //{
            //var hsl = new Color(debugLight.Color);
            //hsl.SetBrightness(0.5f);
            //if (Input.KeyDown(Keys.LeftAlt))
            //    hsl.SetSaturation(hsl.GetSaturation() + scrollDelta * 0.025f);
            //else if (Input.KeyDown(Keys.LeftShift))
            //    hsl.SetHue(hsl.GetHue() + scrollDelta * 0.025f);
            //else if (Input.KeyDown(Keys.LeftControl))
            //    debugLight.Falloff = debugLight.Falloff * (1 + scrollDelta * 0.1f);
            //else if (Input.KeyDown(Keys.Z))
            //    debugLight.AngleOuter += scrollDelta * 2f;
            //else if (Input.KeyDown(Keys.X))
            //    debugLight.AngleInner += scrollDelta * 2f;
            //else if (Input.KeyDown(Keys.C))
            //    debugLight.Direction += scrollDelta * 0.05f;
            //else
            //    debugLight.Size = debugLight.Size * (1 + scrollDelta * 0.1f);

            //    debugLight.Color = hsl.ToVector4();
            //}

            //if (Input.KeyPressed(Keys.K))
            //{
            //    if (Input.KeyPressed(Keys.D1)) debugLight.Type = Light2D.LightType.Omni;
            //    if (Input.KeyPressed(Keys.D2)) debugLight.Type = Light2D.LightType.Cone;
            //    if (Input.KeyPressed(Keys.D3)) debugLight.Type = Light2D.LightType.Directional;
            //}

            //if (Input.KeyPressed(MouseKeys.Left))
            //    Lights.Insert(0, debugLight.Clone());
            //if (lights.Count > 0)
            //    lights[0].Position = (Point2)Input.MousePos;
        }
Exemplo n.º 2
0
        private void drawLight(Light2D light, ref AABB lightQuad)
        {
            //TODO: Move vertex calculation to seperate update and draw when using for split screen
            if (!light.Enabled)
            {
                return;
            }
            var gd = Graphics;

            // Light parameters
            lightPos.SetValue(light.Position);
            lightColor.SetValue(light.Color);
            lightFalloff.SetValue(light.Falloff);
            lightFalloff2.SetValue(light.Falloff2);
            lightSize.SetValue(light.Size);
            lightDirection.SetValue(light.Direction.ToDirectionVector());
            lightAngle.SetValue(light.AngleOuter / 360f);
            lightAngleInner.SetValue(light.AngleInner / 360f);


            if (light.CastShadows)
            {
                // Calculate shadows from vertices and draw to stencil
                gd.DepthStencilState = stencilOnly;
                gd.BlendState        = blendStencil;

                effect.Techniques[0].Passes[0].Apply(); // Stencil only simple pixelshader

                foreach (var obj in VertexObjs)
                {
                    shadowcasts++;
                    Vector2[] vertices = obj.GetVertices();
                    for (int v = 0; v < vertices.Length; v++)
                    {
                        Vector2 currentVertex = vertices[v];
                        Vector2 nextVertex    = vertices[(v + 1) % vertices.Length];
                        /// Doubles the amount of vertices draw, but below code doesnt work to remove backfaced vertices
                        /// Maybe its cheaper to skip all these checks and just send double count of vertices?
                        /// Should probably find proper solution to cull backsides (test by placing light within object)
                        //Vector2 edge = Vector2.Subtract(nextVertex, currentVertex);
                        //Vector2 normal = new Vector2(edge.Y, -edge.X);
                        //Vector2 normal = Vector2.Normalize(edge);
                        //Vector2 lightToCurrent = Vector2.Subtract(currentVertex, light.Position);
                        //if (Vector2.Dot(normal, lightToCurrent) > 0)
                        DrawQuad(Vector2.Add(currentVertex, Vector2.Subtract(currentVertex, light.Position) * Width * 10),
                                 currentVertex,
                                 Vector2.Add(nextVertex, Vector2.Subtract(nextVertex, light.Position) * Width * 10),
                                 nextVertex);
                    }
                }
            }

            // Disable stencil buffer and draw to color
            gd.DepthStencilState = stencilNormal;
            gd.BlendState        = blendNormal;

            // Draw light on quad where stencil is empty
            effect.Techniques[0].Passes[(int)light.Type + 1].Apply(); // Choose light pixel shader (based on light type)
            DrawQuad(new Vector2(lightQuad.Left, lightQuad.Top), new Vector2(lightQuad.Left, lightQuad.Bottom), new Vector2(lightQuad.Right, lightQuad.Top), new Vector2(lightQuad.Right, lightQuad.Bottom));

            // Clear stencil for next light
            gd.Clear(ClearOptions.Stencil, Color.Transparent, 0, 0);
        }