首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >对三角形网格使用纹理,而不必读取/写入图像文件

对三角形网格使用纹理,而不必读取/写入图像文件
EN

Stack Overflow用户
提问于 2014-11-12 07:57:20
回答 2查看 1.6K关注 0票数 2

这是对前一个问题(见Coloring individual triangles in a triangle mesh on javafx)的后续,我认为这本身就是另一个主题。

是否有一种方法(使用javafx)可以避免将图像文件实际写入磁盘(或外部设备)来使用纹理?

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

因为我的颜色映射在运行时会改变,所以我不想每次运行它时都要写到磁盘上。此外,对于使用我的应用程序(使用javafx)的人来说,这可能是一个安全问题(写入磁盘)。

EN

Stack Overflow用户

回答已采纳

发布于 2014-11-12 11:53:41

正如@Jens-Peter-Haack所建议的,使用Snapshot,您可以创建任何您想要的图像,然后将此图像应用于扩散映射。为此,您需要创建一些节点,用所需的颜色填充它们,将它们分组到某个容器中,然后获取快照。

有一种直截了当的方法,您可以使用PixelWriter构建带有颜色图案的图像。

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

代码语言:javascript
运行
复制
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);
}

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

代码语言:javascript
运行
复制
IcosahedronMesh mesh = new IcosahedronMesh();
PhongMaterial mat = new PhongMaterial();
mat.setDiffuseMap(colorPallete(256));
mesh.setMaterial(mat);

但是,您仍然需要提供到新纹理的适当映射。

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

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

代码语言:javascript
运行
复制
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()和faces m.getFaces()中,如here所示。

如果我们为我们的二十面体中的每个顶点指定一个颜色,我们从所有调色板中选择一个颜色(根据颜色和顶点的数量来缩放),然后用t0=p0、t1=p1、t2=p2设置每个脸:

代码语言:javascript
运行
复制
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
    );

这会给我们这样的东西:

编辑

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

票数 3
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26881887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档