Hi,
I’m attempting to build a wall via scripting using a primitive cube that could be of any size.
That part is no problem, it’s just tiling the texture that’s causing a headache.
As a standard cube the tiling is fine, however as soon as the sides are no longer even the texture stretches on one or more sides.
I’ve hard-coded the majority of the dimensions into the example function below to show the kind of thing I’m attempting.
void BuildWall(Texture texture, float wallLength) {
GameObject westWall = GameObject.CreatePrimitive(PrimitiveType.Cube);
westWall.name = "Outer Wall West";
westWall.transform.parent = room.transform;
westWall.transform.position = new Vector3(-49.0f, 2.5f, 0.0f);
westWall.transform.localScale = new Vector3(2.0f, 5.0f, wallLength - 4.0f);
westWall.renderer.material.mainTexture = texture;
westWall.renderer.material.mainTextureScale = new Vector2(0.2f, 4.6f);
}
Is this possible? Or is a UV map required as soon as a cube’s faces are not of equal size?
Thanks in advance to anyone that can help!
Take a look at this professional drawing of a textured cube.
You can stretch the cube along two axes, without distorting the textures on their faces non-uniformly - but not all three axes:
- Stretch along X: the stripes on the 4 side faces will remain the same as the cube is stretched along the stripe’s direction. the other 2 sides remain unchanged.
- Stretch along Y: the stripes on the 4 side faces will appear thicker as the cube is stretched against the stripe’s direction. the other 2 sides remain unchanged.
- Stretch along Z: whoa. two sides will be stretched along the stripe’s direction, and two sides perpendicular to that! You cannot compensate this with texture scale either, as two faces would again stretch/replicate with the stripe’s direction, and the other two against it (or in other words, two faces need x scaling, while the other two faces need y scaling. at the same time the two remaining faces must not be scaled.
(EDITED, as the first version was wrong:)
Note as long as you scale only one axis (just don’t scale the Unity Primitive Cube along Z (might be a different axis for modeled cubes, but there will always be one that doesn’t work)), you should be fine for the 4 stretched faces, but you cannot map all 6 faces without distortion. As soon as you start to scale two or more axes, you can’t even compensate to make 4 faces look correct.
For example, a cuboid 1x2x4 would need the texture mapped 2x4 times on the X faces, 1x4 times on the Y faces, and 1x2 times on the Z faces. Not possible without a matching UV map.
