[Solved] One Material for different sized objects?

My situation is that I have a number of walls. Walls have different lengths, though they have the same height. I have one texture I’d like to use for those walls. The problem is when I apply the texture to a wall that is say 10x10, the texture is fine, but if the wall is 50x10, the texture is stretched to fit all 50 feet.



I’ve thought about breaking up the walls into 10x10 chunks, but that blows up the number of Geometries I use, which I would like to avoid if possible.



I looked at scaling the Texture, but I don’t see how to scale it for each individual wall, rather than for all of them at once.



I thought about trying to get the wrap mode to repeat but I may not be doing something right.



This seems to be a common situation, but I’m fairly new to all of this and would appreciate some pointers/kicks in the butt.



Here is some pseudocode:



[java]Texture tex = m_application.getAssetManager().loadTexture(“Textures/wall.jpg”);

tex.setWrap(Texture.WrapMode.Repeat);

Material mat = new Material(m_application.getAssetManager(), “Common/MatDefs/Misc/Unshaded.j3md”);

mat.setTexture(“ColorMap”, tex);

Quad q = new Quad(10.0f, 50.0f);

q.setStatic();

Geometry g = new Geometry(“wall”, q);

g.setMaterial(mat);

[/java]



Thanks!

The current shaders don’t support scaling… however… it’s an easy thing to add. I’ll post the unshaded here with scaling (tiling) as an example and you can modify whichever you need to do the same.



Ah… actually… I can just explain it simply enough. I believe most texture2D lookups are done using a variable named texCoord (vec2 storing the current pixel fragment coords) in the frag shaders. if you do something like texCoord *= 13.0; the texture would be tiled 13 times. Hoever, you’ll want to set the scale up as a uniform you can pass in or all of your textures using this shader set would be tiled 13x.



This is a great way of bringing significant detail at to large textured models… as long as you tiled textures aren’t two obvious where they repeat. This is where texture splatting can help.



Hope that helps.

You can also just add some vertices, you don’t have to make it separate geometry.

Or… set the texture coordinates properly.

As pspeed said:

Set the texture to repeat.

Set the uv mapping texture co-ordinates to correct. (i.e. wall of 1010 = 1,1 - wall of 5010 = 5,1).



I can’t remember where I set it but I know I create quads (using the JME3 primitive shape) and am able to set a texture scale on them at some point…

Please note that when you do what @pspeed suggested, then changing the tiling at runtime means modifying the mesh at runtime, which isn’t a good idea.



Here’s a shaderish solution instead



Snippets:



Vertex Shader

[java]



attribute vec2 inTexCoord;

varying vec2 scaledUV;

uniform vec2 m_texScale;





void main() {



scaledUV = inTexCoord * m_texScale;



}[/java]



In your pixel shader sample textures with scaledUV.



MatDef

[java]

MaterialDef Electricity3 {



MaterialParameters {



Vector2 texScale



}



}

[/java]





Please note that you can also just as easy add an texture coords offset.

@cvlad said:
Please note that when you do what @pspeed suggested, then changing the tiling at runtime means modifying the mesh at runtime, which isn't a good idea.


That was never mentioned as a requirement. Even so, periodically resending a small set of texture coordinates still might be better than one material per wall. But if we are going to make up requirements this will get to be a long thread. ;)
@pspeed said:
That was never mentioned as a requirement. Even so, periodically resending a small set of texture coordinates still might be better than one material per wall. But if we are going to make up requirements this will get to be a long thread. ;)


True, I just wanted to point it out for reference.

That was fast. Looks like Quad.scaleTextureCoordinates() does the trick. I just call it when I create the quad. My walls are procedurally generated, so it works out. I figured it would be something easy. Once again, you all have my appreciation! Thanks again!