简体   繁体   English

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

[英]How to assign texture to a loaded OBJ file in java3d

I have a java 3d application and this application I load an OBJ file into my scene. 我有一个Java 3D应用程序,此应用程序将OBJ文件加载到场景中。 How can I assign a texture (a jpg file) to this model? 如何为该模型分配纹理(jpg文件)? To be more precise, when I want to assign texture to a primitive java object (eg sphere) I use the following: 更精确地说,当我想将纹理分配给原始java对象(例如sphere)时,我使用以下方法:

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

However, when loading and adding an obj file I do: 但是,在加载和添加obj文件时,我会这样做:

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? 我应该怎么做?

You would have to use a program that you made the obj file or were you can load the file. 您将必须使用创建obj文件的程序,或者可以加载该文件。 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 假设3D模型易变称为模型

model.setAppearance(ap);

IIRC you need to get the Shape3D node you want to apply the texture to (calling setAppearance(...) ) from your branch group, eg 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. IIRC,您需要从分支组中获取要对其应用纹理的Shape3D节点(调用setAppearance(...) ),例如,使用getChild(index)等。请注意,您可能要通过子代递归迭代,因为您获得的分支组实际上可能包含其他组,因此您可能会在组树的下方找到形状。

Alternatively you should be able to add an AlternateAppearance object to the branch group. 或者,您应该能够将AlternateAppearance对象添加到分支组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM