Split a texture?

How can I split a texture into 9 parts (3x3) to make a skybox using get and setpixels?
EDIT: Here is the error I get.

alt text

Have you tried using a material for this?

I would assume if you stretch the material then off set it you can split it.

http://unity3d.com/support/documentation/ScriptReference/Material.SetTextureScale.html

scale it to 1/3

http://unity3d.com/support/documentation/ScriptReference/Material.SetTextureOffset.html

offset by x/3

But I’m just wondering why you couldn’t just model a box with set uvs to get your skybox the way it should be…

Good Luck!

I’m assuming the bitmap will have the 6 of the 9 tiles contain the image. The classic ‘cross’ shape would have a 3x4 arrangement by the way.

You need to identify which of those tiles go to which face, and in what orientation. Then use 6 sets of ‘for’ loops to iterate over x,y on each of those, transferring the ‘get’ pixels to ‘set’ them to 6 new textures.

Let’s say you have a texture ‘skyTiles’ and create 6 new textures tex1, tex2, etc. Get the pixels from each tile something like (untested)

var tileWidth = skyTiles.width / 3;
var tileHeight = skyTiles.height / 3;

// center tile
for (x=tileWidth/3; x < tileWidth/3+tileWidth; x++)
  for (y=tileHeight/3; y < tileHeight/3+tileHeight; y++)
    tex1.SetPixel (tileWidth-x, y-tileHeight, skyTiles.GetPixel(x,y));

I have no idea if that will work right for you, but you should be able to get the idea. Do that for all 6 faces. Then create the skybox from them:

var skymat = new Material (Shader.Find ("RenderFX/Skybox"));

tex1.texture.wrapMode = TextureWrapMode.Clamp;
skymat.SetTexture("_FrontTex", text.texture);

tex2.texture.wrapMode = TextureWrapMode.Clamp;
skymat.SetTexture("_BackTex", tex2.texture);

tex3.texture.wrapMode = TextureWrapMode.Clamp;
skymat.SetTexture("_LeftTex", tex3.texture);

tex4.texture.wrapMode = TextureWrapMode.Clamp;
skymat.SetTexture("_RightTex", tex4.texture);

tex5.texture.wrapMode = TextureWrapMode.Clamp;
skymat.SetTexture("_UpTex", tex5.texture);

tex6.texture.wrapMode = TextureWrapMode.Clamp;
skymat.SetTexture("_DownTex", tex6.texture);

RenderSettings.skybox = skymat;