Java - Problems with normal map generation

Asked

Viewed 35 times

0

Hello, I have a problem in generating a normal texture map, I wonder if there is any way to "soften" tone transitions.

Original image (left) and generated map (right) Imagem original (esquerda) e mapa gerado (direita) As you can see, the generated map has "steps" in almost all color tone transitions.

The map generation code:

public static BufferedImage[] getNormal(BufferedImage source, int contrast, int brightness, float smoothness, float power) {
        BufferedImageOp op = new RescaleOp(contrast / 100f, brightness, null);
        BufferedImage input = op.filter(toGrayscale(source), null);
        BufferedImage output = new BufferedImage(input.getWidth(), input.getHeight(), BufferedImage.TYPE_INT_RGB);
        WritableRaster outRaster = output.getRaster();

        for (int y = 1; y < output.getHeight() - 1; y++) {
            for (int x = 1; x < output.getWidth() - 1; x++) {
                Vec3d xNormal = new Vec3d(1, 0, power * Math.sin(Math.atan((input.getRGB(x - 1, y) - input.getRGB(x + 1, y)))));
                Vec3d yNormal = new Vec3d(0, 1, power * Math.sin(Math.atan((input.getRGB(x, y - 1) - input.getRGB(x, y + 1)))));

                Vec3d normal = new Vec3d();
                normal.cross(xNormal, yNormal);
                normal.normalize();

                int[] rgb = {
                        (int) (((normal.x + 1f) / 2) * 255),
                        (int) (((normal.y + 1f) / 2) * 255),
                        (int) (((normal.z + 1f) / 2) * 255)
                };

                outRaster.setPixel(x, y, rgb);
            }
        }

        output.setData(outRaster);
        return new BufferedImage[]{input, output};
    }

This is my doubt, I am already grateful.

  • What do you want to do specifically? Make the difference sharper, a gradient between colors?

  • I would like to remove, or at least soften the "degrais" that can be observed on the map, I tried several methods but they are always present in the X and Y axes of the normal map.

  • Has the project in git?

  • I have, but I’ve managed to do otherwise, thanks for the help anyway! Still, I’m curious why it looked like this in this example.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.