JavaFX 3D Coloring再次面临……
作者:互联网
我研究了这个question,但我仍然没有得到它.下面最短的代码显示了一个完全灰色的金字塔,而我试图给出构成金字塔的6个三角形不同的颜色.那么……为什么不出现这些颜色?
请注意,我从该问题中借用了getTexCoords().addAll(..)语句,但显然我仍然做错了.这是uv映射吗?那到底是什么?我已经看到了拓扑解释(球体< - >地图),但这与纹理/颜色有什么关系……?
感谢你的帮助 – 迈克尔
public class ColoredPyramid extends Application {
public void start(Stage primaryStage) {
Group root = new Group();
Scene scene = new Scene(root, 200, 200, true);
primaryStage.setTitle("Colored Pyramid");
primaryStage.setScene(scene);
primaryStage.show();
TriangleMesh colouredPyramid = new TriangleMesh();
float height = 100;
float hypotenuse = 150;
colouredPyramid.getPoints().addAll(0, 0, 0); //0-index:: top
colouredPyramid.getPoints().addAll(0, height, -hypotenuse / 2); //1-index:: x=0, z=-hyp/2 ==> Closest to user
colouredPyramid.getPoints().addAll(-hypotenuse / 2, height, 0); //2-index:: x=hyp/2, z=0 ==> Leftest
colouredPyramid.getPoints().addAll(hypotenuse / 2, height, 0); //3-index:: x=hyp/2, z=0 ==> rightest
colouredPyramid.getPoints().addAll(0, height, hypotenuse / 2); ////4-index:: x=0, z=hyp/2 ==> Furthest from user
//Next statement copied from stackoverflow.com/questions/26831871/coloring-individual-triangles-in-a-triangle-mesh-on-javafx
colouredPyramid.getTexCoords().addAll(
0.1f, 0.5f, // 0 red
0.3f, 0.5f, // 1 green
0.5f, 0.5f, // 2 blue
0.7f, 0.5f, // 3 yellow
0.9f, 0.5f // 4 orange
);
colouredPyramid.getFaces().addAll(0, 0, 2, 0, 1, 0); //Left front face ---> RED
colouredPyramid.getFaces().addAll(0, 1, 1, 1, 3, 1); //Right front face ---> GREEN
colouredPyramid.getFaces().addAll(0, 2, 3, 2, 4, 2); //Right back face ---> BLUE
colouredPyramid.getFaces().addAll(0, 3, 4, 3, 2, 3); //Left back face ---> RED
colouredPyramid.getFaces().addAll(4, 4, 1, 4, 2, 4); //Base: left triangle face ---> YELLOW
colouredPyramid.getFaces().addAll(4, 0, 3, 0, 1, 0); //Base: right triangle face ---> ORANGE
MeshView meshView = new MeshView(colouredPyramid);
Group group = new Group(meshView);
group.setTranslateX(100);
group.setTranslateY(80);
root.getChildren().add(group);
}
public static void main(String[] args) {
launch(args);
}
}
解决方法:
要了解JavaFX 3D如何定义任何给定3D形状的颜色,请查看PhongMaterial javadoc(粗体是我的):
The PhongMaterial class provides definitions of properties that represent a Phong shaded material. It describes the interaction of light with the surface of the Mesh it is applied to. The PhongMaterial reflects light in terms of a diffuse and specular component together with an ambient and a self illumination term. The color of a point on a geometric surface is mathematical function of these four components.
这意味着您需要首先提供材料,然后您需要指定任何这些组件,例如漫反射组件.
如果您复制引用的question中的图像:
并用它创建一个材质实例:
PhongMaterial material = new PhongMaterial();
material.setDiffuseMap(new Image(getClass().getResourceAsStream("bB2jV.png")));
meshView.setMaterial(material);
你可以看到这个图像用于将颜色应用到金字塔:
如果修改面的纹理索引,将根据纹理坐标获得不同的颜色.
要了解更多信息,您可以查看FXyz3D library,它提供了基于此概念的TexturedMesh类.在那里你会发现许多不同的3D形状“纹理”基元,可以使用不同的纹理“模式”.大多数这些模式甚至不需要图像,因为这是在内部创建的.这允许基于数学函数创建例如颜色梯度.
这是TetrahedraMesh的一个示例,它使用3D函数来定义密度图:
TetrahedraMesh tetra = new TetrahedraMesh(10, 5, null);
tetra.setTextureModeVertices3D(1530, p -> p.magnitude());
标签:java,javafx,javafx-3d 来源: https://codeday.me/bug/20190522/1153408.html