开发者

Java3d: Texture is not applied to OBJ model properly

开发者 https://www.devze.com 2023-04-11 02:51 出处:网络
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\");

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);
ImageCompo开发者_JS百科nent2D 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号