[英]Java3D 1.5.1 - How to place different textures on Box parts?
I am trying to place different textures on sides of a Box
, but without any success. 我试图在Box
侧面放置不同的纹理,但没有成功。
Here is my code: 这是我的代码:
BufferedImage texture1 = ...; // brown image
BufferedImage texture2 = ...; // green image
Box box = new Box(1f, 1f, 1f, Box.GENERATE_TEXTURE_COORDS, new Appearance());
TextureAttributes ta = new TextureAttributes();
ta.setTextureMode(TextureAttributes.MODULATE);
Appearance app = new Appearance();
app.setTexCoordGeneration(new TexCoordGeneration(TexCoordGeneration.OBJECT_LINEAR, TexCoordGeneration.TEXTURE_COORDINATE_2));
app.setTexture(new TextureLoader(texture1).getTexture());
app.setTextureAttributes(ta);
box.setAppearance(Box.TOP, app);
Appearance app2 = new Appearance();
app2.setTexCoordGeneration(new TexCoordGeneration(TexCoordGeneration.OBJECT_LINEAR, TexCoordGeneration.TEXTURE_COORDINATE_2));
app2.setTexture(new TextureLoader(texture2).getTexture());
app2.setTextureAttributes(ta);
box.setAppearance(Box.RIGHT, app2);
Result: 结果:
Well, it places the images on both sides, but as you can see, they are blured. 好了,它把图像放在了两面,但是正如您所看到的,它们是模糊的。
I thought it could be caused by wrong TexCoordGeneration
applied to the appearance of sides. 我认为这可能是由错误的TexCoordGeneration
应用于侧面的外观引起的。 But I am also not sure if I create the Box
instance with correct parameters. 但是我也不确定是否用正确的参数创建Box
实例。
How should I fix this? 我该如何解决?
Thank you very much for answers! 非常感谢您的回答!
OK I fixed that by generating my own TexCoordGeneration
object with Vector4f
objects. 好的,我通过使用Vector4f
对象生成自己的TexCoordGeneration
对象来Vector4f
。
Code: 码:
app.setTexCoordGeneration(this.generateTexCoord(box.getShape(Box.TOP)));
and the method generateTexCoord()
: 和方法generateTexCoord()
:
private TexCoordGeneration generateTexCoord(Shape3D shape) {
BoundingBox bb = new BoundingBox(shape.getBounds());
Point3d lower = new Point3d();
Point3d upper = new Point3d();
bb.getLower(lower);
bb.getUpper(upper);
double width = upper.x - lower.x;
double height = upper.y - lower.y;
double deep = upper.z - lower.z;
Vector4f planeX = new Vector4f((float)(1.0/width), 0.0f, 0.0f, (float)(-lower.x/width));
Vector4f planeY = new Vector4f(0.0f, (float)(1.0/height), 0.0f, (float)(-lower.y/height));
Vector4f planeZ = new Vector4f(0.0f, 0.0f, (float)(1.0/deep), (float)(-lower.z/deep));
TexCoordGeneration tcg = new TexCoordGeneration(TexCoordGeneration.OBJECT_LINEAR, TexCoordGeneration.TEXTURE_COORDINATE_2);
if(width == 0) { // RIGHT, LEFT: YZ
tcg.setPlaneS(planeZ);
tcg.setPlaneT(planeY);
} else if(height == 0) { // TOP, BOTTOM: XZ
tcg.setPlaneS(planeX);
tcg.setPlaneT(planeZ);
} else { // FRONT, BACK: XY
tcg.setPlaneS(planeX);
tcg.setPlaneT(planeY);
}
return tcg;
}
Hope that helped someone with same problem. 希望对遇到同样问题的人有所帮助。 :) :)
I had the same problem as you and I was reading pages on the internet and with everything I finished the cube with different textures. 我遇到了与您相同的问题,我在互联网上阅读网页,并且用不同的纹理完成了立方体的所有工作。 I used images from my computer. 我使用了计算机上的图像。 Hope it works for everybody, it took me 3 days! 希望它对每个人都有效,我花了三天时间!
protected Node buildShape() {
TextureLoader loader;
Texture texture;
Box caja=new Box(1.5f,1.5f,1.5f,Box.GENERATE_TEXTURE_COORDS,new Appearance());
Appearance ap = new Appearance();
loader = new TextureLoader("blue.jpg",this);
texture = loader.getTexture();
ap.setTexture(texture);
caja.setAppearance(Box.BACK,ap);
Appearance ap2 = new Appearance();
loader = new TextureLoader("white.jpg",this);
texture = loader.getTexture();
ap2.setTexture(texture);
caja.setAppearance(Box.TOP,ap2);
Appearance ap3 = new Appearance();
loader = new TextureLoader("red.jpg",this);
texture = loader.getTexture();
ap3.setTexture(texture);
caja.setAppearance(Box.BOTTOM,ap3);
Appearance ap4 = new Appearance();
loader = new TextureLoader("green.jpg",this);
texture = loader.getTexture();
ap4.setTexture(texture);
caja.setAppearance(Box.LEFT,ap4);
Appearance ap5 = new Appearance();
loader = new TextureLoader("orange.jpg",this);
texture = loader.getTexture();
ap5.setTexture(texture);
caja.setAppearance(Box.RIGHT,ap5);
Appearance ap6 = new Appearance();
loader = new TextureLoader("yellow.jpg",this);
texture = loader.getTexture();
ap6.setTexture(texture);
caja.setAppearance(Box.FRONT,ap6);
return caja;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.