How to make a 2D flashlight effect?

I am trying to make a flashlight effect in my 2D game. My flashlight is presented as a segment of a line extending from an object at a certain angle. Flashlight can point in any direction. The flashlight also varies in intensity (beam length of the flashlight).

I have a problem trying to figure out the best (easiest?) And most flexible way to visualize the flashlight effect. In particular, with a tiled map.

I can think of two methods. But I do not know how to implement them:

  • ONLY draw a conical / round portion of the map tile
  • the covering screen is black texture, and with a code punching a hole in the black texture. This way I can change the attributes of the hole.

I don't know where to start, what they are called / if libGDX can execute them.

+3
source share
2 answers

Number one is called the stencil buffer. But it will be difficult for you to achieve soft effects. - Easily.

Number two: You need a texture with light. Completely black areas can be drawn by repeating a black sprite around a light sprite (left, right, up, down). Or you can mix it with a stencil buffer. Or you can carefully calculate your texture coordinates and use GL.clamp_to_edge to spread all black pixels. Depending on how you render your scene, you can first make your light alpha information and then mix the scene (dimming according to dst_alpha). - Not so difficult to implement.

- (GLES 2.0). , . - ( ).

, , , , . , .

** , , . , .

+6

BufferedImage Graphics2D, .

Flashlight using incandescent (yellow) beam

Flashlight using halogen (blue) beam

import java.awt.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class FlashLight {

    public static void main(String[] args) throws Exception {
        Robot robot = new Robot();
        int w = 500, h = 200;
        Rectangle rect = new Rectangle(0, 0, w, h);
        final BufferedImage bi = robot.createScreenCapture(rect);
        final BufferedImage bi2 = FlashLight.draw(
                bi, 10, 180, 420, 90, .3,
                new Color(255, 255, 120, 15), new Color(0, 0, 0, 220));
        final BufferedImage bi3 = FlashLight.draw(
                bi, 10, 180, 420, 90, .3,
                new Color(180, 250, 255, 15), new Color(0, 0, 0, 220));

        Runnable r = new Runnable() {

            @Override
            public void run() {
                JPanel gui = new JPanel(new GridLayout(3,0,2,2));
                gui.add(new JLabel(new ImageIcon(bi2)));
                gui.add(new JLabel(new ImageIcon(bi)));
                gui.add(new JLabel(new ImageIcon(bi3)));

                JOptionPane.showMessageDialog(null,gui);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }

    public static BufferedImage draw(
            BufferedImage source,
            double x1, double y1, double x2, double y2,
            double beamWidth,
            Color beamColor, Color darknessColor) {
        RenderingHints hints = new RenderingHints(
              RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        BufferedImage bi = new BufferedImage(
                source.getWidth(), source.getHeight(),
                BufferedImage.TYPE_INT_ARGB);

        Graphics2D g = bi.createGraphics();
        g.setRenderingHints(hints);

        g.drawImage(source, 0, 0, null);

        // Create a conical shape to constrain the beam
        double distance = Math.sqrt(Math.pow(x1 - x2, 2d) + Math.pow(y1 - y2, 2d));
        double tangent = (y2 - y1) / (x2 - x1);
        double theta = Math.atan(tangent);
        System.out.println(
                "distance: " + distance
                + "  tangent: " + tangent
                + "  theta: " + theta);
        double minTheta = theta + beamWidth / 2;
        double maxTheta = theta - beamWidth / 2;
        double xMin = x1 + distance * Math.cos(minTheta);
        double yMin = y1 + distance * Math.sin(minTheta);

        double xMax = x1 + distance * Math.cos(maxTheta);
        double yMax = y1 + distance * Math.sin(maxTheta);

        Polygon beam = new Polygon();
        beam.addPoint((int) x1, (int) y1);
        beam.addPoint((int) xMax, (int) yMax);
        beam.addPoint((int) xMin, (int) yMin);

        g.setColor(beamColor);
        GradientPaint gp = new GradientPaint(
                (int)x1,(int)y1, beamColor,
                (int)x2,(int)y2, darknessColor);
        g.setClip(beam);
        g.setPaint(gp);
        g.fillRect(0, 0, bi.getWidth(), bi.getHeight());

        // create an area the size of the image, but lacking the beam area
        Area darknessArea = new Area(new Rectangle(0, 0, bi.getWidth(), bi.getHeight()));
        darknessArea.subtract(new Area(beam));
        g.setColor(darknessColor);
        g.setClip(darknessArea);
        g.fillRect(0, 0, bi.getWidth(), bi.getHeight());

        // fill in the beam edges with black (mostly to smooth lines)
        g.setClip(null);
        g.setColor(Color.BLACK);
        g.setStroke(new BasicStroke(2));
        g.draw(new Line2D.Double(x1,y1,xMin,yMin));
        g.draw(new Line2D.Double(x1,y1,xMax,yMax));

        g.dispose();

        return bi;
    }
}
+5

Source: https://habr.com/ru/post/1625170/


All Articles