问题描述
我正在使用JavaFX制作类,并且试图将渐变应用于球体,但是(很明显),我不知道该怎么做.我之所以陷入困境是因为我知道球体是一个物体,因此它需要一种材料,但是(就颜色而言),PhongMaterial仅采用一种颜色,因此不会采用渐变,因为渐变是一种颜色范围.所以基本上我想做的是以下事情:
I'm working in JavaFX for a class, and I'm trying to apply a gradient to a sphere, but (obviously), I can't figure out how to do it. I'm stuck because I know that a sphere is an object, and so it needs to have a material, but (as far as colors go), a PhongMaterial only takes one color, and so it won't take a gradient because a gradient is a range of colors. So basically what I'm trying to is the following:
Sphere sphere = new Sphere(50);
RadialGradient rg = new RadialGradient(0, 0, 0, 0, 5, true, CycleMethod.REPEAT, /*arbitrary/irrelevant color Stop objects*/));
PhongMaterial pm = new PhongMaterial();
pm.setDiffuseMap(pm);
sphere.setMaterial(asdf);
现在显然该代码不起作用,但我想这就是我要尝试执行的操作的想法/流程.
Now obviously this code doesn't work, but I guess it's the idea/flow of what I'm trying to do.
推荐答案
您对一件事是正确的,PhongMaterial
将Color
用作漫反射颜色,而不允许使用Gradient
.为此,它应该接受Paint
,但事实并非如此.
You are right about one thing, PhongMaterial
takes a Color
as diffuse color, and that doesn't allow a Gradient
. For that, it should accept Paint
, but that is not the case.
所以我们必须寻找不同的选择.
So we have to look for different alternatives.
DiffuseMap
如果选中PhongMaterial
,则可以设置漫反射贴图与图片.这意味着您可以使用具有一定梯度的现有图像并将其应用于球体.
If you check PhongMaterial
, you can set the diffuse map with an image. That means that you can use an existing image with some gradient and apply it to the sphere.
类似这样的东西:
Sphere sphere = new Sphere(100);
PhongMaterial material = new PhongMaterial();
material.setDiffuseMap(new Image("http://westciv.com/images/wdblogs/radialgradients/simpleclorstops.png"));
sphere.setMaterial(material);
将产生以下结果:
动态DiffuseMap
显然,这具有依赖静态图像的缺点.如果要动态修改该怎么办?
Obviously, this has the disadvantage of depending on a static image. What if you want to modify that dynamically?
如果生成径向渐变,则可以执行此操作,将其渲染到辅助场景上并对其进行快照.此快照返回WritableImage
,您可以直接将其用作漫反射贴图.
You can do that, if you generate your radial gradient, render it on a secondary scene and take a snapshot of it. This snapshot returns a WritableImage
that you can use directly as diffuse map.
类似这样的东西:
Scene aux = new Scene(new StackPane(), 100, 100,
new RadialGradient(0, 0, 0.5, 0.5, 1, true, CycleMethod.REPEAT,
new Stop(0, Color.GREEN), new Stop(0.4, Color.YELLOW),
new Stop(0.6, Color.BLUE), new Stop(0.7, Color.RED)));
WritableImage snapshot = aux.snapshot(null);
Sphere sphere = new Sphere(100);
PhongMaterial material = new PhongMaterial();
material.setDiffuseMap(snapshot);
sphere.setMaterial(material);
您现在将拥有:
密度图
还有另一种使用数学函数生成密度图的选项,颜色将通过对该函数的映射来给出.
There is still another option to use a mathematical function to generate a density map, and the colors will be given by a mapping to that function.
为此,您不能使用内置的Sphere
,但是必须创建自己的TriangleMesh
并使用纹理坐标,或者可以仅使用开源JavaFX 3D FXyz 库,其中包含许多不同的基元和纹理选项.
For that you can't use the built-in Sphere
, but you have to either create your own TriangleMesh
and play with the texture coordinates, or you can simply use FXyz, an open source JavaFX 3D library with a number of different primitives and texture options.
在这种情况下,您可以从Maven Central(org.fxyz3d:fxyz3d:0.3.0
)获取库,使用SegmentedSphereMesh
控件,然后选择纹理模式`Vertices3D:
For this case, you can get the library from Maven Central (org.fxyz3d:fxyz3d:0.3.0
), use a SegmentedSphereMesh
control, and then select the texture mode `Vertices3D:
SegmentedSphereMesh sphere = new SegmentedSphereMesh(100);
sphere.setTextureModeVertices3D(1530, p -> p.z);
请注意,这种情况下的功能仅基于z
坐标,但是显然您可以根据需要进行修改.
Note the function in this case is just based on the z
coordinate, but obviously you can modify that as needed.
检查库(有一个采样器),以探索其他选项.
Check the library (there is a sampler), to explore other options.
这篇关于使用JavaFX将渐变应用于球体对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!