- Home /
how do i wrap a texture around a cuboid so it repeats?
i made a texture for a cube in blender and added some code for it to wrap around it and it works fine, but is there any way for a texture to repeat on a single object and wrap around parts of that? ive made it just repeat but the when the texture wraps around it's just 1 texture wrapping around the whole thing.
Answer by AaronBacon · Aug 17, 2021 at 01:04 PM
You just use the Tiling value of the material.
With Default Tiling
With Tiling set to 3x 3y
Also important to note that the texture needs to be set to Repeat Wrap mode:
If you want more control you'd probably want to learn about UV Unwrapping in 3D modelling software, but that's all for just simply adding a texture.
Answer by thomaslijohnson · Aug 17, 2021 at 09:06 PM
i tried it but it doesn't work. it's probably because my texture is different on every side. here's the code i used to wrap it:
public class cubewrap : MonoBehaviour
{
MeshFilter cubemesh;
Mesh mesh;
// Start is called before the first frame update
void Start()
{
cubemesh = GetComponent<MeshFilter>();
mesh = cubemesh.mesh;
Vector2[] uvMap = mesh.uv;
uvMap[0] = new Vector2(0.375f, 1);
uvMap[1] = new Vector2(0.375f, 0.75f);
uvMap[2] = new Vector2(0.625f, 1);
uvMap[3] = new Vector2(0.625f, 0.75f);
uvMap[4] = new Vector2(0.625f, 0.75f);
uvMap[5] = new Vector2(0.625f, 0.5f);
uvMap[8] = new Vector2(0.875f, 0.75f);
uvMap[9] = new Vector2(0.875f, 0.5f);
uvMap[6] = new Vector2(0.375f, 0.5f);
uvMap[7] = new Vector2(0.375f, 0.25f);
uvMap[10] = new Vector2(0.625f, 0.5f);
uvMap[11] = new Vector2(0.625f, 0.25f);
uvMap[12] = new Vector2(0.125f, 0.5f);
uvMap[13] = new Vector2(0.125f, 0.75f);
uvMap[14] = new Vector2(0.375f, 0.75f);
uvMap[15] = new Vector2(0.375f, 0.5f);
uvMap[16] = new Vector2(0.375f, 0.25f);
uvMap[17] = new Vector2(0.625f, 0.25f);
uvMap[18] = new Vector2(0.625f, 0);
uvMap[19] = new Vector2(0.375f, 0);
uvMap[20] = new Vector2(0.375f, 0.75f);
uvMap[21] = new Vector2(0.625f, 0.75f);
uvMap[22] = new Vector2(0.625f, 0.5f);
uvMap[23] = new Vector2(0.375f, 0.5f);
mesh.uv = uvMap;
}
}
Put simply, if your texture isn't designed for tiling (i.e. it doesn't fill the entire UV space and/or portions of the texture are explicitly distributed among specific parts of the mesh), then it won't tile well using the "Tiling" and "Offset" values.
That doesn't mean exceptions can't be made. You could define a bunch of wrapping regions to your texture with a Shader made to specifically accommodate that, or you could use a Texture2DArray to hold the different face textures (and, again, write a Shader that utilizes them, based on some vertex attribute that dictates which texture in the array to use).
In either case, those would be more-computationally-expensive ways of applying tiling, so there's not really a "perfect" solution to this.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Dice Value 1 Answer
Why Do I Suddenly Have This Problem? 0 Answers
holding GUITextures in a array? and changing the size from script? 2 Answers