Java3d:纹理未正确应用于 OBJ 模型

发布于 2024-12-07 19:36:06 字数 808 浏览 0 评论 0原文

我使用以下几行将环面(obj 文件)的 3D 模型加载到我的程序中:

Shape3D torus=null;
Scene t1 = getSceneFromFile("Some local path\torus.obj");

然后我使用以下代码从场景中抓取模型:

BranchGroup branchGroup = t1.getSceneGroup();
torus = (Shape3D) branchGroup.getChild(0);

以下代码段将图像设置为纹理,然后将该纹理应用于外观目的。

TextureLoader textureLoader=new TextureLoader("Another local path\myImage.jpg",null);
ImageComponent2D image=textureLoader.getImage();
Texture2D texture=new Texture2D(Texture.BASE_LEVEL,Texture.RGBA,image.getWidth(),image.getHeight());
texture.setImage(0, image);

Appearance app = new Appearance();
app.setTexture(texture);

torus.setAppearance(app);

当我运行代码时,环面模型已正确加载,但纹理未正确分配。更准确地说,整个 3D 模型具有单一颜色而不是图像作为纹理。提到的颜色是图像左下角像素的颜色。

解决办法是什么? 提前致谢。

I load a 3d model of a torus (obj file) into my program, using these lines:

Shape3D torus=null;
Scene t1 = getSceneFromFile("Some local path\torus.obj");

Then I grab the model from the scene using this code:

BranchGroup branchGroup = t1.getSceneGroup();
torus = (Shape3D) branchGroup.getChild(0);

The following piece of code set an image as texture and then apply that texture to an Appearance object.

TextureLoader textureLoader=new TextureLoader("Another local path\myImage.jpg",null);
ImageComponent2D image=textureLoader.getImage();
Texture2D texture=new Texture2D(Texture.BASE_LEVEL,Texture.RGBA,image.getWidth(),image.getHeight());
texture.setImage(0, image);

Appearance app = new Appearance();
app.setTexture(texture);

torus.setAppearance(app);

When I run the code the torus model is loaded correctly but the texture is not assigned correctly. More accurately, the whole 3d model has a single color instead of an image as texture. Mentioned color, is the color of the pixel at bottom-left of the image.

What's the solution?
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈独秀 2024-12-14 19:36:06

用于创建 obj 文件的程序(例如 Mudbox)还允许您分配 UV 模型图像。指定一个油漆层,然后将其导出为正确的格式。以为你可以加载纹理,但你不会获得对象的最佳细节。尝试使用此纹理加载器代码。

static TextureLoader loader = new TextureLoader("C:\\Users\\Sawyera\\Desktop\\Paint Layer 1.jpg",
    "RGP", new Container());
static Texture texture = loader.getTexture();

   texture.setBoundaryModeS(Texture.WRAP);
texture.setBoundaryModeT(Texture.WRAP);
texture.setBoundaryColor(new Color4f(0.0f, 1.0f, 0.0f, 0.0f));
TextureAttributes texAttr = new TextureAttributes();
texAttr.setTextureMode(TextureAttributes.MODULATE);
Appearance ap = new Appearance();
ap.setTexture(texture);
ap.setTextureAttributes(texAttr);

然后取你的 3D 模型名称,例如我巧妙命名的模型,并将外观设置为 ap 或

model.setAppearance(ap);

这将几乎 100% 加载它(如果你原来拥有它)。加载纹理海峡不会将其加载到所需的coridants。

A Program, for example Mudbox That you use to create the obj file, also allows you to assign UV model images. Assign a paint layer than export it to the correct format. Thought this you can load the texture, you wont have the best detail to the obj. Try using this textur loader code instead.

static TextureLoader loader = new TextureLoader("C:\\Users\\Sawyera\\Desktop\\Paint Layer 1.jpg",
    "RGP", new Container());
static Texture texture = loader.getTexture();

   texture.setBoundaryModeS(Texture.WRAP);
texture.setBoundaryModeT(Texture.WRAP);
texture.setBoundaryColor(new Color4f(0.0f, 1.0f, 0.0f, 0.0f));
TextureAttributes texAttr = new TextureAttributes();
texAttr.setTextureMode(TextureAttributes.MODULATE);
Appearance ap = new Appearance();
ap.setTexture(texture);
ap.setTextureAttributes(texAttr);

Then take your 3d models name, example mines cleverly named model, and set apperance to ap or

model.setAppearance(ap);

This will load it almost 100% were you origanly had it. Loading a texture strait up is not going to load it to the desired coridants.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文