如果你看一下從3DViewer
項目上OpenJFX的FXMLExporter
類的最後一個版本,你會看到,材料僅漫反射顏色輸出:
if (PhongMaterial.class.isAssignableFrom(aClass)) {
res.add(new Property(aClass.getMethod("getDiffuseColor"), "diffuseColor"));
}
同樣的情況,在項目你提到。
您可以加入這一行:
res.add(new Property(aClass.getMethod("getDiffuseMap"), "diffuseMap"));
到getProperties()
:
if (PhongMaterial.class.isAssignableFrom(aClass)) {
res.add(new Property(aClass.getMethod("getDiffuseColor"), "diffuseColor"));
res.add(new Property(aClass.getMethod("getDiffuseMap"), "diffuseMap"));
}
所以,當您導出3D形狀,這將被添加到FXML文件:
<Box id="box" width="100.0" height="100.0" depth="100.0">
<material>
<PhongMaterial diffuseColor="0xffffffff">
<diffuseMap>
<Image/>
</diffuseMap>
</PhongMaterial>
</material>
</Box>
我們還需要導出圖片網址。這可以在exportToFXML
方法中完成。
由於漫反射貼圖中的Image
不存儲任何路徑,技巧是將圖像保存到fxml導出的相同路徑。這是一個快速的實現:
private FXML exportToFXML(Object object) {
...
for (Property property : properties) {
try {
Object[] parameters = new Object[property.getter.getParameterTypes().length];
Object value = property.getter.invoke(object, parameters);
if (value != null) {
...
} else if (value instanceof Image) {
FXML container = fxml.addContainer(property.name);
FXML fxmlImage=exportToFXML(value);
container.addChild(fxmlImage);
exportImage((Image)value,"image.png");
fxmlImage.addProperty("url","@image.png");
} else {
FXML container = fxml.addContainer(property.name);
container.addChild(exportToFXML(value));
}
}
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
Logger.getLogger(FXMLExporter.class.getName()).
log(Level.SEVERE, null, ex);
}
}
return fxml;
}
private void exportImage(Image image, String fileName){
try {
ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", new File(fileName));
} catch (IOException ex) {
System.out.println("Error saving image");
}
}
如果你現在運行它,這是你會得到什麼:
<Box id="box" width="100.0" height="100.0" depth="100.0">
<material>
<PhongMaterial diffuseColor="0xffffffff">
<diffuseMap>
<Image url="@image.png"/>
</diffuseMap>
</PhongMaterial>
</material>
</Box>
非常感謝您的建議。我遇到了一些問題。我將我的兩個立方體的每個面定義爲一個單獨的TriangleMesh,認爲最終我希望能夠在場景中選擇對象時區分每個面。我已將相同的材質分配給兩個立方體的每個面,導出嘗試將圖像文件寫入12次。看起來應該有一種方法可以向模型定義傳達所有圖像都是相同的。 – bluemonkey
即使在您有幾個3D形狀但使用相同材質的情況下,也可以執行一些檢查以避免再次導出圖像。如果你有不同的紋理,你需要添加一些計數器來使用不同的圖像。正如我所說,這是一個快速實施,所以你可以處理細節。 –
我同意我可以跟蹤不同的圖像。真正的問題在於我對原始文章的編輯中所描述的......目前還不清楚如何控制圖像的位置以及導入不再起作用。 – bluemonkey