我目前正在尝试使圆柱体的末端完全透明,同时保持侧面为 Material 。 我不确定如何实现这一目标。这thread提到它,但所有链接都已损坏
我想我需要使用裁剪平面?虽然我不知道从哪里开始。
这是我目前用来简单设置半透明 Material 的方法!
Cylinder line = new Cylinder(getRadius()/4, height);
lineMaterial = new PhongMaterial();
lineMaterial.setDiffuseColor(new Color(1,1,1,0.5));
lineMaterial.diffuseMapProperty();
line.setMaterial(lineMaterial);
最佳答案
获得透明度的一种可能方法是使用带有一些透明像素的 png 作为漫反射图像。
虽然这行得通,但如果您在内置 JavaFX Cylinder
上应用,您将不会获得预期的结果,因为 Cylinder 将相同的图像应用于两者垂直管和两个盖面。所以这对你的情况不起作用。
可以选择从圆柱体网格中移除盖子并只得到一个管,但不幸的是它不会导出其三角形网格。
到目前为止,最好的选择是直接创建 pipe 的网格,为此我们可以通过查看(新)OpenJDK/JFX 处的开源代码来重用圆柱体的网格。 GitHub 存储库。
下面的方法创建了一个TriangleMesh
管的高度h
,半径r
,有div
分割(生成 2 * div
三角形)。它与 Cylinder
中的代码完全相同,但没有端点、纹理坐标和面数组。
private static TriangleMesh createMesh(int div, float h, float r) {
final int nPoints = div * 2;
final int tcCount = (div + 1) * 2;
final int faceCount = div * 2;
float textureDelta = 1.f / 256;
float dA = 1.f / div;
h *= .5f;
float points[] = new float[nPoints * 3];
float tPoints[] = new float[tcCount * 2];
int faces[] = new int[faceCount * 6];
int smoothing[] = new int[faceCount];
int pPos = 0, tPos = 0;
for (int i = 0; i < div; ++i) {
double a = dA * i * 2 * Math.PI;
points[pPos + 0] = (float) (Math.sin(a) * r);
points[pPos + 1] = h;
points[pPos + 2] = (float) (Math.cos(a) * r);
tPoints[tPos + 0] = 1 - dA * i;
tPoints[tPos + 1] = 1 - textureDelta;
pPos += 3; tPos += 2;
}
// top edge
tPoints[tPos + 0] = 0;
tPoints[tPos + 1] = 1 - textureDelta;
tPos += 2;
for (int i = 0; i < div; ++i) {
double a = dA * i * 2 * Math.PI;
points[pPos + 0] = (float) (Math.sin(a) * r);
points[pPos + 1] = -h;
points[pPos + 2] = (float) (Math.cos(a) * r);
tPoints[tPos + 0] = 1 - dA * i;
tPoints[tPos + 1] = textureDelta;
pPos += 3; tPos += 2;
}
// bottom edge
tPoints[tPos + 0] = 0;
tPoints[tPos + 1] = textureDelta;
tPos += 2;
int fIndex = 0;
// build body faces
for (int p0 = 0; p0 < div; ++p0) {
int p1 = p0 + 1;
int p2 = p0 + div;
int p3 = p1 + div;
// add p0, p1, p2
faces[fIndex+0] = p0;
faces[fIndex+1] = p0;
faces[fIndex+2] = p2;
faces[fIndex+3] = p2 + 1;
faces[fIndex+4] = p1 == div ? 0 : p1;
faces[fIndex+5] = p1;
fIndex += 6;
// add p3, p2, p1
faces[fIndex+0] = p3 % div == 0 ? p3 - div : p3;
faces[fIndex+1] = p3 + 1;
faces[fIndex+2] = p1 == div ? 0 : p1;
faces[fIndex+3] = p1;
faces[fIndex+4] = p2;
faces[fIndex+5] = p2 + 1;
fIndex += 6;
}
for (int i = 0; i < div * 2; ++i) {
smoothing[i] = 1;
}
TriangleMesh m = new TriangleMesh();
m.getPoints().setAll(points);
m.getTexCoords().setAll(tPoints);
m.getFaces().setAll(faces);
m.getFaceSmoothingGroups().setAll(smoothing);
return m;
}
现在您需要创建一个MeshView
,这样您就可以将它添加到您的场景中:
private static MeshView createTube(int div, float h, float r) {
MeshView meshView = new MeshView(createMesh(div, h, r));
// meshView.setDrawMode(DrawMode.LINE);
meshView.setCullFace(CullFace.NONE);
PhongMaterial material = new PhongMaterial(Color.RED);
meshView.setMaterial(material);
return meshView;
}
创建并添加一个到你的场景:
MeshView tube = createTube(64, 5f, 1.6f);
Scene scene = new Scene(new Group(tube), 600, 600, true, SceneAntialiasing.BALANCED);
你会得到你的试管:
您还可以应用漫反射 image作为纹理:
material.setDiffuseMap(new Image(getClass.getResourceAsStream("440px-JavaFX_Logo.png")));
关于java - 如何在 JavaFX 中使 Cylinder 的末端透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58223312/