Texture only one side of a cube

Hi,

I’m new. I’ve searched the forums and Google, read a lot, but nothing that helps me.

All this is at runtime. I can’t do this in the IDE :slight_smile:

I want to dynamically load in X images, and generate X Cubes.
I then only want to texture one side of the cube. The other sides I want to apply different textures to.

I’m guessing I need to use cubemaps, right?

For testing, I’m trying a basic example. At first I’ve got a 2x2x2 cube in the IDE. I’ve applied a 2x2 texture (called ‘checker’) to it.

I’m now trying to generate a cubemap from the 2x2 texture using this code:

var tex:Texture2D = Resources.Load("checker");
	gameObject.renderer.material.mainTexture = tex;
	var cubemap:Cubemap = new Cubemap(2, TextureFormat.ARGB32, false);
	cubemap.SetPixels(tex.GetPixels(),CubemapFace.NegativeX);
	cubemap.SetPixels(tex.GetPixels(),CubemapFace.NegativeY);
	cubemap.SetPixels(tex.GetPixels(),CubemapFace.NegativeZ);
	cubemap.SetPixels(tex.GetPixels(),CubemapFace.PositiveX);
	cubemap.SetPixels(tex.GetPixels(),CubemapFace.PositiveY);
	cubemap.SetPixels(tex.GetPixels(),CubemapFace.PositiveZ);
	cubemap.Apply();
	gameObject.renderer.material.mainTexture = cubemap;

I get this error:

Error assigning cubemap texture to 2D texture property '_MainTex': Dimensions must match
UnityEngine.Material:SetTexture(String, Texture)
UnityEngine.Material:SetTexture(String, Texture)
UnityEngine.Material:set_mainTexture(Texture)
CubeMapTest:Start() (at Assets\CubeMapTest.js:26)

[..\..\External\shaderlab\Library\properties.cpp line 260]

Can anyone see what I’m doing wrong? Or can someone suggest a better way of doing this?

Thanks! :slight_smile:

I have a second, related question. The end result is for all the cubes to not be cuboid - eg: 30x20x4.

Might I be better off arranging Planes into a cuboid shape?

I have already loaded in images into planes, so am happy with this process.

1 word. UVMapping : D ( you need to do that in your modeling tool).

Tricky, as I don’t want to model anything. I want to generate everything with code at runtime.

I guess I have 3 choices:

  1. Give up

  2. Use 6 planes as one cube (not ideal! Also uses many more polys)

  3. Find some way of calculating the mesh and UV mapping at runtime.

Is there anything built into Unity that will help me with number 3? Or do I need to work this out myself?

It sounds like you need to create a script that will take 6 existing textures, and combine them into one large texture.