如何将纹理分配给 java3d 中加载的 OBJ 文件

发布于 2024-12-04 07:05:41 字数 372 浏览 1 评论 0原文

我有一个 java 3d 应用程序,该应用程序将 OBJ 文件加载到我的场景中。如何为该模型分配纹理(jpg 文件)? 更准确地说,当我想将纹理分配给原始 java 对象(例如球体)时,我使用以下方法:

Sphere sphere = new Sphere(Radius, Primflags, Appearance);

但是,在加载和添加 obj 文件时,我这样做:

Scene scene = getSceneFromFile("OBJ file");
myBranchGroup = scene.getSceneGroup();

在第二种情况下,我找不到分配纹理的方法质地。我该怎么做呢?

I have a java 3d application and this application I load an OBJ file into my scene. How can I assign a texture (a jpg file) to this model?
To be more precise, when I want to assign texture to a primitive java object (e.g. sphere) I use the following:

Sphere sphere = new Sphere(Radius, Primflags, Appearance);

However, when loading and adding an obj file I do:

Scene scene = getSceneFromFile("OBJ file");
myBranchGroup = scene.getSceneGroup();

And in second case, I can find no way of assigning the texture. How should I do that?

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

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

发布评论

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

评论(2

回忆那么伤 2024-12-11 07:05:41

您必须使用制作 obj 文件的程序或者可以加载该文件。绘制它,然后导出该文件。然后在任何方法之外添加此代码

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);
 int primflags = Primitive.GENERATE_NORMALS
    + Primitive.GENERATE_TEXTURE_COORDS;
    ObjectFile loader = new ObjectFile(ObjectFile.RESIZE);

然后在将模型分配到场景之前添加此代码。假设 3D 模型变量称为 model

model.setAppearance(ap);

You would have to use a program that you made the obj file or were you can load the file. Paint it, then export that file. Then add this code to it outside any methods

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

Then

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);
 int primflags = Primitive.GENERATE_NORMALS
    + Primitive.GENERATE_TEXTURE_COORDS;
    ObjectFile loader = new ObjectFile(ObjectFile.RESIZE);

Then add this before you assign the model to the scene. Assuming the 3D model varrible is called model

model.setAppearance(ap);
瞄了个咪的 2024-12-11 07:05:41

IIRC 您需要从分支组获取要应用纹理的 Shape3D 节点(调用 setAppearance(...)),例如使用 getChild (index) 等。请注意,您可能会递归地遍历子级,因为您获得的分支组实际上可能包含其他组,因此您可能会在组树的下方找到形状。

或者,您应该能够将 AlternateAppearance 对象添加到分支组中。

IIRC you need to get the Shape3D node you want to apply the texture to (calling setAppearance(...)) from your branch group, e.g. by using getChild(index) etc. Note that you might to iterate recursively through the children, since the branch group you get might actually contain other groups, so you might find the shapes further down the group tree.

Alternatively you should be able to add an AlternateAppearance object to the branch group.

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