将纹理用于三角形网格,而无需读取/写入图像文件

阿尔瓦罗·阿尔瓦雷斯·帕里利亚

这是对上一个问题(请参见在javafx上为三角形网格中的各个三角形着色)的后续工作,我认为这是一个单独的主题。

有没有一种方法(使用javafx)可以摆脱实际上必须将图像文件写入磁盘(或外部设备)以使用纹理的方法?

换句话说:我可以使用特定的纹理而不必使用图像吗?

由于我的颜色映射将在运行时更改,因此我不需要每次运行时都写入磁盘。另外,对于使用我的应用程序的人(使用javafx),这可能是一个安全问题(写入磁盘)。

何塞·佩雷达(Jose Pereda)

正如@ Jens-Peter-Haack所建议的那样,Snapshot您可以创建所需的任何图像,然后将该图像用作扩散贴图。为此,您需要创建一些节点,用所需的颜色填充它们,将它们分组在某个容器中,然后拍摄快照。

有一种直接的方法,您可以使用来构建带有颜色图案的图像PixelWriter

假设您要使用256种颜色,此方法将返回256个像素的图像,其中每个像素都具有这些颜色之一。为简单起见,我添加了两种简单的方式来构建调色板。

public static Image colorPallete(int numColors){
    int width=(int)Math.sqrt(numColors);
    int height=numColors/width;

    WritableImage img = new WritableImage(width, height);
    PixelWriter   pw  = img.getPixelWriter();
    AtomicInteger count = new AtomicInteger();
    IntStream.range(0, height).boxed()
            .forEach(y->IntStream.range(0, width).boxed()
                    .forEach(x->pw.setColor(x, y, getColor(count.getAndIncrement(),numColors))));

    // save for testing purposes
    try {            
        ImageIO.write(SwingFXUtils.fromFXImage(img, null), "jpg", new File("palette.jpg"));
    } catch (IOException ex) { }
    return img;
}

private Color getColor(int iColor, int numColors){
    // nice palette of colors 
    java.awt.Color c = java.awt.Color.getHSBColor((float) iColor / (float) numColors, 1.0f, 1.0f);
    return Color.rgb(c.getRed(), c.getGreen(), c.getBlue());

    // raw palette
    //return Color.rgb((iColor >> 16) & 0xFF, (iColor >> 8) & 0xFF, iColor & 0xFF);
}

一旦有了图像对象,就可以设置漫射贴图:

IcosahedronMesh mesh = new IcosahedronMesh();
PhongMaterial mat = new PhongMaterial();
mat.setDiffuseMap(colorPallete(256));
mesh.setMaterial(mat);

但是您仍然必须提供对新纹理的正确映射。

为此,您需要将网格的顶点映射到图像中的像素。

首先,我们需要一种在网格上映射带有纹理坐标的颜色的方法。此方法将返回给定颜色索引的一对坐标:

public static float[] getTextureLocation(int iPoint, int numColors){
    int width=(int)Math.sqrt(numColors);
    int height=numColors/width;
    int y = iPoint/width; 
    int x = iPoint-width*y;
    return new float[]{(((float)x)/((float)width)),(((float)y)/((float)height))};
}

最后,我们这些纹理增加m.getTextCoords(),并于面m.getFaces(),如图所示这里

如果我们为二十面体中的每个顶点分配颜色,则从所有调色板中选择一种颜色(根据颜色和顶点的数量向上或向下缩放),然后将每个面设置为t0 = p0,t1 = p1,t2 = p2 :

IntStream.range(0,numVertices).boxed()
    .forEach(i->m.getTexCoords()
                 .addAll(getTextureLocation(i*numColors/numVertices,numColors)));

m.getFaces().addAll(
            1, 1, 11, 11, 7, 7, 
            1, 1, 7, 7, 6, 6, 
            1, 1, 6, 6, 10, 10, 
            1, 1, 10, 10, 3, 3, 
            1, 1, 3, 3, 11, 11,
            4, 4, 8, 8, 0, 0, 
            5, 5, 4, 4, 0, 0, 
            9, 9, 5, 5, 0, 0, 
            2, 2, 9, 9, 0, 0, 
            8, 8, 2, 2, 0, 0,
            11, 11, 9, 9, 7, 7,
            7, 7, 2, 2, 6, 6, 
            6, 6, 8, 8, 10, 10, 
            10, 10, 4, 4, 3, 3, 
            3, 3, 5, 5, 11, 11,
            4, 4, 10, 10, 8, 8, 
            5, 5, 3, 3, 4, 4, 
            9, 9, 11, 11, 5, 5, 
            2, 2, 7, 7, 9, 9, 
            8, 8, 6, 6, 2, 2
    );

这会给我们这样的东西:

在此处输入图片说明

编辑

可以使用纹理坐标,而不是使用颜色映射节点,可以添加一些功能并轻松创建轮廓图,如下所示:

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章