maps icon indicating copy to clipboard operation
maps copied to clipboard

Optimizing raycasting operations

Open IllusionTheDev opened this issue 2 years ago • 6 comments

Feature description

Hey,

I've been experimenting with making my own map rendering engine, and I'd like to leave some tips for just better overall performance:

There are 3 ways you can approach raycasting, it seems like you're doing method 1:

Method 1:

  • Grab the start location, and add a very tiny miniscule offset, increment by that offset until you reach a frame. (It takes about 5 * 1/offset * distance operations, if your offset is 1/128, you're doing 640 iterations, measures at about 3ms / cast on my machine

Method 2 (1+):

  • Grab the start location, and increment by the normalized offset, until there's a frame at the current block. Then, do a finer cast through method one (1-5 + (1/offset iterations, if there is a frame)) - About 5x better, measures at about 0.1ms / cast on my machine

Method 3:

  • Grab the start location, and increment by the normalized offset. If there is a frame at the current block, estimate the frame's bounding box by grabbing the frame's center location, and shifting in the opposite direction of where the frame is looking at, you'll always end up with ceil(distance) iterations, instead of <DISTANCE * STEPS_PER_BLOCK>, measures at about 0.015ms / cast on my machine

Further optimizations:

  • Cache the converted colors of your ColorMap, from what I've measured, mapping colors takes about 90 microseconds per color, but marginally less if cached.

Relevant issues

No response

IllusionTheDev avatar Sep 16 '22 07:09 IllusionTheDev

Thank you for opening an issue! I didn't think much about performance when I implemented raycasting stuff to be honest, 3ms seemed "good enough" to me. If you want to contribute you can optimize the raycasting stuff yourself, just let me know :D (If you want to participate in Hacktoberfest you can also wait until October)


Cache the converted colors of your ColorMap, from what I've measured, mapping colors takes about 90 microseconds per color, but marginally less if cached.

The ColorCache class is responsible for caching colors, I think that's what you're looking for.

cerus avatar Sep 16 '22 08:09 cerus

when I implemented raycasting stuff to be honest, 3ms seemed "good enough" to me.

Given that 50mspt is your upper limit, anything above 1ms can cause tons of issues, specially with multiple players interacting at the same time.

Possible issue with the color cache: Constant mapping if the the color is transparent (id 0 according to minecraft's color mappings)

IllusionTheDev avatar Sep 16 '22 08:09 IllusionTheDev

Possible issue with the color cache: Constant mapping if the the color is transparent (id 0 according to minecraft's color mappings)

Good catch, that should be a simple fix. I'll open an issue for that.

cerus avatar Sep 16 '22 08:09 cerus

Possible issue with the color cache: Constant mapping if the the color is transparent (id 0 according to minecraft's color mappings)

Good catch, that should be a simple fix. I'll open an issue for that.

I just remembered that it's impossible to feed transparent colors to the ColorCache class since you're only able to provide (r, g, b), so this shouldn't be an issue

cerus avatar Sep 16 '22 08:09 cerus

Possible issue with the color cache: Constant mapping if the the color is transparent (id 0 according to minecraft's color mappings)

Good catch, that should be a simple fix. I'll open an issue for that.

I just remembered that it's impossible to feed transparent colors to the ColorCache class since you're only able to provide (r, g, b), so this shouldn't be an issue

This leads to another issue then - How are you gonna handle transparent images?

IllusionTheDev avatar Sep 16 '22 08:09 IllusionTheDev

This leads to another issue then - How are you gonna handle transparent images?

When converting an image you simply have to check if the pixel you're converting has an alpha value of zero (or whatever threshold you'd like for transparency). maps doesn't have any built-in image converters iirc.

Pseudo code, should be roughly the same for BufferedImages:

Image img = ...;
bool hasAlpha = img.getColorModel().hasAlpha(); // Works for BufferedImage
MapGraphics<?, ?> graphics = MapGraphics.standalone(img.width, img.height);
for (x = 0; x < img.width; x++) {
  for (y = 0; y < img.height; y++) {
    int pixel = img.getRGB(x, y);
    java.awt.Color col = new java.awt.Color(pixel, hasAlpha);
    if(col.alpha == 0) {
      graphics.setPixel(x, y, (byte) 0);
    } else {
      graphics.setPixel(x, y, ColorCache.rgbToMap(col.r, col.g, col.b));
    }
  }
}

cerus avatar Sep 16 '22 08:09 cerus