Java3d: Texture is not applied to OBJ model properly

久未见 提交于 2019-12-13 14:14:25

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/7645674/java3d-texture-is-not-applied-to-obj-model-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!