Cant apply texture =(

Hai!

Iv imported a model (3ds), and its textured in blender.

But the texture doesnt apply by itself, tho its in same dir as the model.

So I tried to apply it after adding it.

Like this:

        MaxToJme maxtojme = new MaxToJme();
        Node node = null; //Where to dump mesh.
        ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream(); //For loading the raw file
        URL url;
        try {
            url = new URL(new File("C:\Users\Andreas\Desktop\ammoBox.3ds").toURL().toString()); //ModelGroup.class.getClassLoader().getResource("Model.3ds"); // File Path for the model
            try {
                maxtojme.convert(url.openStream(), bytearrayoutputstream); // Converts the file into a jme usable file
            } catch (IOException ex) {
                Logger.getLogger(HelloWorld.class.getName()).log(Level.SEVERE, null, ex);
            }
            BinaryImporter jmebinaryreader = new BinaryImporter(); // Used to convert the jme usable file to a TriMesh
            ByteArrayInputStream in=new ByteArrayInputStream(bytearrayoutputstream.toByteArray());
            try {
                node = (Node) jmebinaryreader.load(in);
            } catch (IOException ex) {
                Logger.getLogger(HelloWorld.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (MalformedURLException ex) {
            Logger.getLogger(HelloWorld.class.getName()).log(Level.SEVERE, null, ex);
        }
        URL tex;
        TextureState ts1;
        try {
            tex = new URL(new File("C:\Users\Andreas\Desktop\ammoBoxTex.jpg").toURL().toString());
            ts1=display.getRenderer().createTextureState();
            ts1.setTexture(TextureManager.loadTexture(tex,Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear));
            ts1.setEnabled(true);
            node.setRenderState(ts1);
        } catch (MalformedURLException ex) {
            Logger.getLogger(HelloWorld.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
        node.updateRenderState();


But it doesnt work!

All I get is the mesh, painted in clear white.
How do I fix this? :|

I would recommend using paths relative to your project instead of static file paths.

Perhaps using a data folder or rsrc folder with subfolders in your project to have an easier structure of your data?

Also, there's the ResourceLocatorTool, where you specify different folders for content, then you can easily get it everywhere in your project and if you want to change it you only have to do it in one place.



Like this:

ResourceLocatorTool.addResourceLocator(

ResourceLocatorTool.TYPE_TEXTURE,

new SimpleResourceLocator(YourClass.class.getResource("/data/textures/").toURI()));



and:

ResourceLocatorTool.locateResource(ResourceLocatorTool.TYPE_TEXTURE, "tex.jpg")

will then fetch it from your data/textures folder.

Okej, but that is just a weird method for me :frowning:

I'v tried to do it by doing:

        try {
            ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_TEXTURE, new SimpleResourceLocator(HelloWorld.class.getResource("C:\Users\Andreas\Desktop\").toURI()));
        } catch (URISyntaxException ex) {
            Logger.getLogger(HelloWorld.class.getName()).log(Level.SEVERE, null, ex);
        }
        TextureState texture;
        URL textureR = ResourceLocatorTool.locateResource(ResourceLocatorTool.TYPE_TEXTURE, "ammoBoxTex.jpg");
        texture = display.getRenderer().createTextureState();
        texture.setTexture(TextureManager.loadTexture(textureR,Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear));
        texture.setEnabled(true);
        node.setRenderState(texture);
        node.updateRenderState();
        node.setLocalTranslation(new Vector3f(-80f,70f,-30f));


but then I get nullpointException at "ResourceLocatorTool.addResourceLocator"

So I changed that line to:

 ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_TEXTURE, new SimpleResourceLocator(HelloWorld.class.getResource("").toURI()));


and changed the other line to:

URL textureR = ResourceLocatorTool.locateResource(ResourceLocatorTool.TYPE_TEXTURE, "C:\Users\Andreas\Desktop\ammoBoxTex.jpg");



Then I dont get nullpoint exception tho the texture still doesnt show...

So is it the texture that isnt correctly imported?

You're still using a static file path.

I've not used that, so I don't know what pitfalls come with it.

It's generally better to set up a content structure from the start, as you're going to need that later anyway, when you need to distribute the game or use it on another of your computers.



Put your texture image in a folder called data/textures in your project (like you have your packages for your code being folders - com.company.something.game). So put the data -> textures -> ammoBoxTex.jpg at the root of your project folder.



Then try this (although you should probably put your adding of ResourceLocators somewhere central, easy to access):

try {
            ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_TEXTURE, new SimpleResourceLocator(HelloWorld.class.getResource("/data/textures/").toURI()));
        } catch (URISyntaxException ex) {
            Logger.getLogger(HelloWorld.class.getName()).log(Level.SEVERE, null, ex);
        }
        TextureState texture;
        URL textureR = ResourceLocatorTool.locateResource(ResourceLocatorTool.TYPE_TEXTURE, "ammoBoxTex.jpg");
        texture = display.getRenderer().createTextureState();
        texture.setTexture(TextureManager.loadTexture(textureR,Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear));
        texture.setEnabled(true);
        node.setRenderState(texture);
        node.updateRenderState();
        node.setLocalTranslation(new Vector3f(-80f,70f,-30f));


Make sure your class is called HelloWorld if you use that class' class loader as in this example.
Good luck!

Okej :slight_smile:

Now I got it to work without nullpointException.

But it's still showing up just in white without texture :frowning:



And also, when I do this:

node.setLocalTranslation(new Vector3f(-80f,70f,-30f));

then the box moves to the location, but if u look from certain directions, it dicappears :frowning:



But the important, for now, is that the texture isnt showing…

Should I set some more settings or something?

Oh look!

I changed these lines:

node.setRenderState(texture);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
       node.updateRenderState();


to:

player2.setRenderState(texture);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        player2.updateRenderState();


and then it apply the texture over player2 which is a simple box and I saw the texture!

So the problem must be with the node!
since the node is a 3ds imported model, it got some problems with the texture..

Can you help me whats wrong with the node?

how is it textured in blender in blender u can apply textures as part of a material applied to a model it will look the part in blender but not in jme, unless your texture is applied as a uvmap and your model has uv coordinates

In blender I use uvmapping yea.

I'm not so sure what to do…

all I do is:

-select model go to edit mode

-click u>unwrap

-open uvwindow

place the faces so it looks ok…





Should I do something else too?

And netbeans says i must do:

List<Spatial> tris = node.descendantMatches(TriMesh.class);


instead of TriMesh :(

UNBELIVEABLE!

TOTALY UNBELIVEABLE!!!

I MADE IT!!!

YES YES YES YES YESSSSS!!!



Thanks SO SO much guys!

You have been soo helpfull and helped me so much!

I would never be able to have done this without you!

Tytytytytyvm!



I used objtojme instead of 3dstojme

then edited some stuff and now it all works! BOTH 3d model and texture!!!

YEAS!!! Ty so much :slight_smile: