Tile texture on Spatial

Hi, I’m trying to tile a texture onto a spatial (imported terrain grid mesh from blender) by doing the following:



protected void setTextureOfObject(Spatial spatial)

{



Texture tex = assetManager.loadTexture(“Models/white-tile-texture.jpg”);

tex.setWrap(WrapMode.Repeat);



Material mat1 = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);



mat1.setTexture(“ColorMap”, tex);

spatial.setMaterial(mat1);

}



-But this doesn’t work, I get a 1x1 version of the texture. From reading other threads, I understand that it is also necessary to use Texture.setSize(); But Texture.setSize() doesn’t seem to exist anymore–the threads I have been reading are pretty old.



-Can someone provide me with a code sample for this?



Thanks!

for a box/mesh/etc:



box.scaleTextureCoordinates(new Vector2f(2, 2));





edit: try somethin like this:

[java]



public void setTextureScale(Spatial spatial, Vector2f vector) {

if (spatial instanceof Node) {

Node findingnode = (Node) spatial;

for (int i = 0; i < findingnode.getQuantity(); i++) {

Spatial child = findingnode.getChild(i);

setTextureScale(child, vector);

}

} else if (spatial instanceof Geometry) {

((Geometry) spatial).getMesh().scaleTextureCoordinates(vector);

}

}

[/java]



edit2: ok its final try version ;p

1 Like

I think I’m close to having this working, but I get an exception when trying to cast my spatial as a geometry:

java.lang.ClassCastException: com.jme3.scene.Node cannot be cast to com.jme3.scene.Geometry

This happened to me yesterday also; what do I need to change?





protected void setTextureOfObject(Spatial spatial)

{

Texture tex = assetManager.loadTexture(“Models/white-tile-texture.jpg”);

tex.setWrap(WrapMode.Repeat);



Material mat1 = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);

mat1.setTexture(“ColorMap”, tex);



((Geometry)spatial).getMesh().scaleTextureCoordinates(new Vector2f(2, 2));



spatial.setMaterial(mat1);

}

you cant just do:



java[/java]



its impossible if spatial is not instance of Geometry…



i said you how to do this. use my example code to do this:



[java]

public void setTextureScale(Spatial spatial, Vector2f vector) {

if (spatial instanceof Node) {

Node findingnode = (Node) spatial;

for (int i = 0; i < findingnode.getQuantity(); i++) {

Spatial child = findingnode.getChild(i);

setTextureScale(child, vector);

}

} else if (spatial instanceof Geometry) {

((Geometry) spatial).getMesh().scaleTextureCoordinates(vector);

}

}



protected void setTextureOfObject(Spatial spatial)

{

Texture tex = assetManager.loadTexture(“Models/white-tile-texture.jpg”);

tex.setWrap(WrapMode.Repeat);



Material mat1 = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);

mat1.setTexture(“ColorMap”, tex);



setTextureScale(spatial, new Vector2f(2, 2));



spatial.setMaterial(mat1);

} [/java]



new Vector2f(2, 2) give us 4 tiles, new Vector2f(3, 3) give 9 tiles, etc…

Thanks oxplay–I was able to get it working using your code :slight_smile:

it’s good i helped you.



I hope you understand it now :slight_smile:



Spatial have known subclasses like Geometry and Node



So if you was loading 3d object it loaded Node Spatial and this Node Spatial had other Node Spatials or just Geometry Spatial.

And you had exception doing ((Geometry)spatial) when actual typing spatial was Node Spatial.

Okay–that makes more sense now. Your explanation cleared that up for me.

Incidentally, if I were to fix my code as it was, it would look like this. But your code is obviously much more reusable. This code will only work for this one situation.



Texture tex = assetManager.loadTexture(“Models/white-tile-texture.jpg”);

tex.setWrap(WrapMode.Repeat);



Material mat1 = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);

mat1.setTexture(“ColorMap”, tex);

((Geometry)((Node)spatial).getChild(0)).getMesh().scaleTextureCoordinates(new Vector2f(2, 2));



spatial.setMaterial(mat1);

in your situation propably yes



but remember: getChild(0) can be Node spatial too :wink:



its like a tree





…Node…

…/…|…

…Node…Node…

…/… /…|…

Geometry…Node…Geometry…

…|…

…Geometry…





and before function set textureScale for ALL Geometries

For good measure I’ll add this link to this thread, explains Spatial, Geometry, Node and more :stuck_out_tongue:

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:scenegraph_for_dummies

@normen: certainly good tutorial, but i like to help people without links



its like: here you have link and go to hell :stuck_out_tongue:





edit: if someone want to learn more he will seek tutorials himself :slight_smile:

@oxplay2 In my case its “Here you have the link to a document that I spend hours on creating and that explains exactly and in detail what you want to know.” :stuck_out_tongue:

@normen: good said “in detail”, but some people want only know “how to do somethin” not “how to do somethin and how it work”.



remember he wrote: “-Can someone provide me with a code sample for this?” not “is there any tutorial of this?” :slight_smile:

Thanks to both of you. Oxplay was able to resolve 2 of my issues at the same time. On the one hand, I was having trouble tiling a texture, but equally importantly, I was still battling the set up of spatials. Regarding the second issue, I was thinking in the back of my mind that I’d need to go back and re-read one of the tutorials.

The problem is that if you stick around for a few months you will see the same five questions asked almost daily. It’s nice to point to the tutorials for the people who didn’t see them or didn’t even bother to look.



Edit: note this was to oxplay and not the original poster… and I’m not trying to discourage oxplay from answering questions as much and as repeatedly as he wants… just pointing out why some people point to the links already there right there in “Drag & Drop Help” on the right.

naaah, you have me to explain daily questions :roll:

oxplay2 said:
naaah, you have me to explain daily questions :roll:


Heheh... everyone gets tired eventually. My problem is that I'm too old now. :)

Yeah, just let people copy&paste your code into their applications :slight_smile:

you convinced me normen :smiley:

Yeah probably we should mention that. The thing is that if you know that texture coordinates are saved in the mesh its kind of obvious to look for a method to modify them on the mesh object…

Edit: Oops, post vanished, answer obsolete ^^