Search in sources :

Example 1 with Vector2D

use of de.gurkenlabs.litiengine.util.geom.Vector2D in project litiengine by gurkenlabs.

the class AmbientLight method renderLightSource.

private void renderLightSource(final Graphics2D g, final LightSource light, final double longerDimension, Rectangle2D section) {
    final Point2D lightCenter = light.getCenter();
    Area lightArea = null;
    if (light.getLightShapeType().equals(LightSource.RECTANGLE)) {
        g.setColor(light.getColor());
        g.fill(light.getBoundingBox());
        return;
    }
    // into and out of rooms)
    for (final StaticShadow col : this.getEnvironment().getStaticShadows()) {
        if (!light.getBoundingBox().intersects(col.getBoundingBox())) {
            continue;
        }
        if (lightArea == null) {
            lightArea = new Area(light.getLightShape());
        }
        if (!lightArea.intersects(col.getBoundingBox())) {
            continue;
        }
        final Area boxInLight = new Area(col.getBoundingBox());
        final Line2D[] bounds = GeometricUtilities.getLines(col.getBoundingBox());
        for (final Line2D line : bounds) {
            final Vector2D lineVector = new Vector2D(line.getP1(), line.getP2());
            final Vector2D lightVector = new Vector2D(lightCenter, line.getP1());
            if (light.getCenter().getY() < line.getY1() && light.getCenter().getY() < line.getY2() && col.getBoundingBox().contains(light.getCenter()) || lineVector.normalVector().dotProduct(lightVector) >= 0) {
                continue;
            }
            final Path2D shadowParallelogram = new Path2D.Double();
            final Point2D shadowPoint1 = GeometricUtilities.project(lightCenter, line.getP1(), longerDimension);
            final Point2D shadowPoint2 = GeometricUtilities.project(lightCenter, line.getP2(), longerDimension);
            // construct a shape from our points
            shadowParallelogram.moveTo(line.getP1().getX(), line.getP1().getY());
            shadowParallelogram.lineTo(shadowPoint1.getX(), shadowPoint1.getY());
            shadowParallelogram.lineTo(shadowPoint2.getX(), shadowPoint2.getY());
            shadowParallelogram.lineTo(line.getP2().getX(), line.getP2().getY());
            shadowParallelogram.closePath();
            final Area shadowArea = new Area(shadowParallelogram);
            if (light.getCenter().getY() < col.getBoundingBox().getMaxY() && !col.getBoundingBox().contains(light.getCenter())) {
                shadowArea.add(boxInLight);
            }
            shadowArea.intersect(lightArea);
            lightArea.subtract(shadowArea);
        }
    }
    final Paint oldPaint = g.getPaint();
    // render parts that lie within the shadow with a gradient from the light
    // color to transparent
    final Shape lightShape = light.getLightShape();
    final double radius = lightShape.getBounds2D().getWidth() > lightShape.getBounds2D().getHeight() ? lightShape.getBounds2D().getWidth() : lightShape.getBounds2D().getHeight();
    final Color[] transColors = new Color[] { light.getColor(), new Color(light.getColor().getRed(), light.getColor().getGreen(), light.getColor().getBlue(), 0) };
    final Point2D center = new Point2D.Double(lightShape.getBounds2D().getCenterX() - section.getX(), lightShape.getBounds2D().getCenterY() - section.getY());
    final Point2D focus = new Point2D.Double(center.getX() + lightShape.getBounds2D().getWidth() * light.getFocusOffsetX(), center.getY() + lightShape.getBounds2D().getHeight() * light.getFocusOffsetY());
    RadialGradientPaint paint = new RadialGradientPaint(center, (float) (radius / 2d), focus, new float[] { 0.0f, 1.00f }, transColors, CycleMethod.NO_CYCLE);
    g.setPaint(paint);
    Shape fillShape;
    if (lightArea != null) {
        lightArea.transform(AffineTransform.getTranslateInstance(-section.getX(), -section.getY()));
        fillShape = lightArea;
    } else {
        fillShape = new Rectangle2D.Double(light.getBoundingBox().getX() - section.getX(), light.getBoundingBox().getY() - section.getY(), light.getBoundingBox().getWidth(), light.getBoundingBox().getHeight());
    }
    g.fill(fillShape);
    g.setPaint(oldPaint);
}
Also used : Shape(java.awt.Shape) Path2D(java.awt.geom.Path2D) Color(java.awt.Color) Rectangle2D(java.awt.geom.Rectangle2D) RadialGradientPaint(java.awt.RadialGradientPaint) Paint(java.awt.Paint) RadialGradientPaint(java.awt.RadialGradientPaint) Line2D(java.awt.geom.Line2D) Area(java.awt.geom.Area) Vector2D(de.gurkenlabs.litiengine.util.geom.Vector2D) Point2D(java.awt.geom.Point2D)

Example 2 with Vector2D

use of de.gurkenlabs.litiengine.util.geom.Vector2D in project litiengine by gurkenlabs.

the class AmbientLight method renderLightSource.

private void renderLightSource(final Graphics2D g, final LightSource light, Rectangle2D section) {
    final double mapWidth = this.getEnvironment().getMap().getSizeInPixels().width;
    final double mapHeight = this.getEnvironment().getMap().getSizeInPixels().height;
    double longerDimension = mapWidth < mapHeight ? mapHeight : mapWidth;
    final Point2D lightCenter = light.getCenter();
    final Point2D lightFocus = new Point2D.Double(lightCenter.getX() + light.getBoundingBox().getWidth() * light.getFocusOffsetX(), lightCenter.getY() + light.getBoundingBox().getHeight() * light.getFocusOffsetY());
    Shape fillShape;
    Area lightArea = null;
    if (light.getLightShapeType() == LightSource.Type.RECTANGLE) {
        g.setColor(new Color(light.getColor().getRed(), light.getColor().getGreen(), light.getColor().getBlue(), light.getColor().getAlpha()));
        fillShape = new Rectangle2D.Double(light.getBoundingBox().getX() - section.getX(), light.getBoundingBox().getY() - section.getY(), light.getBoundingBox().getWidth(), light.getBoundingBox().getHeight());
        g.fill(fillShape);
        return;
    }
    // into and out of rooms)
    for (final StaticShadow col : this.getEnvironment().getStaticShadows()) {
        if (!light.getBoundingBox().intersects(col.getBoundingBox())) {
            continue;
        }
        if (lightArea == null) {
            lightArea = new Area(light.getLightShape());
        }
        if (!lightArea.intersects(col.getBoundingBox())) {
            continue;
        }
        final Area boxInLight = new Area(col.getBoundingBox());
        final Line2D[] bounds = GeometricUtilities.getLines(col.getBoundingBox());
        for (final Line2D line : bounds) {
            final Vector2D lineVector = new Vector2D(line.getP1(), line.getP2());
            final Vector2D lightVector = new Vector2D(lightFocus, line.getP1());
            if (light.getCenter().getY() < line.getY1() && light.getCenter().getY() < line.getY2() && col.getBoundingBox().contains(light.getCenter()) || lineVector.normalVector().dotProduct(lightVector) >= 0) {
                continue;
            }
            final Path2D shadowParallelogram = new Path2D.Double();
            final Point2D shadowPoint1 = GeometricUtilities.project(lightFocus, line.getP1(), longerDimension);
            final Point2D shadowPoint2 = GeometricUtilities.project(lightFocus, line.getP2(), longerDimension);
            // construct a shape from our points
            shadowParallelogram.moveTo(line.getP1().getX(), line.getP1().getY());
            shadowParallelogram.lineTo(shadowPoint1.getX(), shadowPoint1.getY());
            shadowParallelogram.lineTo(shadowPoint2.getX(), shadowPoint2.getY());
            shadowParallelogram.lineTo(line.getP2().getX(), line.getP2().getY());
            shadowParallelogram.closePath();
            final Area shadowArea = new Area(shadowParallelogram);
            if (light.getCenter().getY() < col.getBoundingBox().getMaxY() && !col.getBoundingBox().contains(light.getCenter())) {
                shadowArea.add(boxInLight);
            }
            shadowArea.intersect(lightArea);
            lightArea.subtract(shadowArea);
        }
    }
    final Paint oldPaint = g.getPaint();
    // render parts that lie within the shadow with a gradient from the light
    // color to transparent
    final Shape lightShape = light.getLightShape();
    final double radius = lightShape.getBounds2D().getWidth() > lightShape.getBounds2D().getHeight() ? lightShape.getBounds2D().getWidth() : lightShape.getBounds2D().getHeight();
    final Color[] transColors = new Color[] { light.getColor(), new Color(light.getColor().getRed(), light.getColor().getGreen(), light.getColor().getBlue(), 0) };
    final Point2D center = new Point2D.Double(lightShape.getBounds2D().getCenterX() - section.getX(), lightShape.getBounds2D().getCenterY() - section.getY());
    final Point2D focus = new Point2D.Double(center.getX() + lightShape.getBounds2D().getWidth() * light.getFocusOffsetX(), center.getY() + lightShape.getBounds2D().getHeight() * light.getFocusOffsetY());
    RadialGradientPaint paint = new RadialGradientPaint(center, (float) (radius / 2d), focus, new float[] { 0.0f, 1.00f }, transColors, CycleMethod.NO_CYCLE);
    g.setPaint(paint);
    if (lightArea != null) {
        lightArea.transform(AffineTransform.getTranslateInstance(-section.getX(), -section.getY()));
        fillShape = lightArea;
    } else {
        fillShape = new Rectangle2D.Double(light.getBoundingBox().getX() - section.getX(), light.getBoundingBox().getY() - section.getY(), light.getBoundingBox().getWidth(), light.getBoundingBox().getHeight());
    }
    g.fill(fillShape);
    g.setPaint(oldPaint);
}
Also used : Shape(java.awt.Shape) Color(java.awt.Color) Path2D(java.awt.geom.Path2D) Rectangle2D(java.awt.geom.Rectangle2D) RadialGradientPaint(java.awt.RadialGradientPaint) Paint(java.awt.Paint) RadialGradientPaint(java.awt.RadialGradientPaint) Line2D(java.awt.geom.Line2D) Area(java.awt.geom.Area) StaticShadow(de.gurkenlabs.litiengine.entities.StaticShadow) Vector2D(de.gurkenlabs.litiengine.util.geom.Vector2D) Point2D(java.awt.geom.Point2D)

Aggregations

Vector2D (de.gurkenlabs.litiengine.util.geom.Vector2D)2 Color (java.awt.Color)2 Paint (java.awt.Paint)2 RadialGradientPaint (java.awt.RadialGradientPaint)2 Shape (java.awt.Shape)2 Area (java.awt.geom.Area)2 Line2D (java.awt.geom.Line2D)2 Path2D (java.awt.geom.Path2D)2 Point2D (java.awt.geom.Point2D)2 Rectangle2D (java.awt.geom.Rectangle2D)2 StaticShadow (de.gurkenlabs.litiengine.entities.StaticShadow)1