Static Texture no matter what the size of the object

Hey guys,

I’m looking for a way to have it such that I can texture a wall and dynamically adjust the height of the wall without the texture stretching. For example, if I texture a wall with bricks and graffiti, and I change the height of the wall to half its height, it should clip the top half of the texture instead of stretching the texture which results in a terrible looking squished bricks/graffiti.

Now someone in a different forum suggested that I could accomplish this by modifying the UVs of the wall mesh. Therefore, I tried to do the following : I created a cube and scaled it 1, 4, 1 so that it’s 4 units high and applied my dog’s picture to it as a texture, as you can see on the left half of my screenshot. Now for testing purposes I modify the UVs for one side of the cube and made it so that the top edge goes 3 units into negative land, my thinking being that if the top 3/4 of the image is “off-screen” (so to speak) then only the remaining bottom 1/4 will be rendered. I also changed the height of the cube to 1 to accurately portray the 1/4 size. Alas, I’m obviously barking up the wrong tree because instead of getting the bottom 1/4 of the texture with just my dogs tail I get the entire image tiled 4 times vertically.

Any thoughts?

Screenshot URL : Sample1 | anthonypaulo | Flickr

		Mesh mesh = GetComponent<MeshFilter>().mesh;
		Vector3[] vertices = mesh.vertices;
		Vector2[] uvs = new Vector2[vertices.Length];
    
		for (var i = 0 ; i < uvs.Length; i++)
			{
			uvs[i] = new Vector2(0f, 0f);
			}

		uvs[0] = new Vector2(0f, -3f);
		uvs[1] = new Vector2(1, -3f);
		uvs[2] = new Vector2(0f, 1f);
		uvs[3] = new Vector2(1f, 1f);

		mesh.uv = uvs;

El Diablo

Diablo,

I assume by “3 units into negative land” you mean you gave the top vertices a value of -3. If this is the case, then you are not thinking about the UV layout correctly. If you don’t want your image to tile, you will never be working with a value span larger than 1.0, meaning the difference between your minimum and maximum values used for UVs will never be greater than 1.

In the image below, Example 1 shows you the default UV selection on a un-clamped texture for the default cube face.

If instead of the whole texture you only want to get the bottom 1/3 of your texture, then you will want to modify your UV values to select a range from 0 → 0.33 on the Y axis. This should provide you with something similar to the selection shown in Example 2

“I see!!!” said the blind man to the deaf man. I’ve been looking at the UVs the wrong way… I thought that the vertexes represented the physical mesh coordinates that the texture is supposed to render on, but now I see that the UVs map to the texture instead.

Problem solved! :smile:

Thank you very much Shawn! And thanks to Krobill who gave me the idea in the first place!

El Diablo

Hey Shawn,

I got it working, but I’m curious about something which I still don’t understand. I’m assuming the following about the UV coordinates :

0,0 ---------- 1,0
  |             |
  |             | 0.33
  |             |
  |             | 0.66
  |             |
0,1 ---------- 1,1

Is this correct? If so, I would assume that to get the bottom 1/3 of the texture I would need to get 0.66 → 1; instead, I need 0 → 0.33 else I get the top half! What part am I wrong about?

Thanks again!

El Diablo

Probably the origin is bottom left and not top left?

Diablo,

I believe megmaltese is correct on that. You need to flip your Y coordinate values for the top and bottom vertices.