从texture2D到cubemap

时间:2017-03-12 11:08:20

标签: unity3d textures unity5 material texture2d

我从图库中选择的图像创建了一个texture2D对象(我在Android应用程序上工作)。我有一种名为" main"在我的Resources文件夹中,它是一个Skybox / Cubemap材质。如果我想在我的脚本中设置材质,我需要一个Cubemap纹理,而不是Texture2D纹理。所以,我的问题是:如何将Texture2D转换为Cubemap?我发布了一段我的代码以更好地解释我的问题(我不知道写什么" //从mainImage到cubetex ??"部分)。

 private Texture2D mainImage;
 private Cubemap cubetex = new Cubemap (2048, TextureFormat.RGB24, false);
 private Material mat;

 mainImage = new Texture2D (www.texture.width, www.texture.height);
 mainImage.SetPixels32 (www.texture.GetPixels32());
 mainImage.Apply ();

 // from mainImage to cubetex ?????

 mat = Resources.Load("main") as Material;
 mat.SetTexture ("_Tex", cubetex);

1 个答案:

答案 0 :(得分:0)

所以继承我用来在立方体的每一面放置不同图像的代码。它需要一个带有一组图像的Texture2d。

    atlas = new Texture2D(512, 512);
    atlasUVs = atlas.PackTextures(atlasTextures, 1, 512);

    GetComponent<Renderer>().material.mainTexture = atlas;

    mesh = GetComponent<MeshFilter>().mesh;
    originalUVs = new Vector2[mesh.vertices.Length];

    // Front
    originalUVs[0] = new Vector2(atlasUVs[0].xMin, atlasUVs[0].yMin);
    originalUVs[1] = new Vector2(atlasUVs[0].xMax, atlasUVs[0].yMin);
    originalUVs[2] = new Vector2(atlasUVs[0].xMin, atlasUVs[0].yMax);
    originalUVs[3] = new Vector2(atlasUVs[0].xMax, atlasUVs[0].yMax);
    // Top
    originalUVs[4] = new Vector2(atlasUVs[1].xMin, atlasUVs[1].yMax);
    originalUVs[5] = new Vector2(atlasUVs[1].xMax, atlasUVs[1].yMax);
    originalUVs[8] = new Vector2(atlasUVs[1].xMin, atlasUVs[1].yMin);
    originalUVs[9] = new Vector2(atlasUVs[1].xMax, atlasUVs[1].yMin);
    // Back
    originalUVs[6] = new Vector2(atlasUVs[2].xMax, atlasUVs[2].yMin);
    originalUVs[7] = new Vector2(atlasUVs[2].xMin, atlasUVs[2].yMin);
    originalUVs[10] = new Vector2(atlasUVs[2].xMax, atlasUVs[2].yMax);
    originalUVs[11] = new Vector2(atlasUVs[2].xMin, atlasUVs[2].yMax);
    // Bottom
    originalUVs[12] = new Vector2(atlasUVs[3].xMin, atlasUVs[3].yMin);
    originalUVs[13] = new Vector2(atlasUVs[3].xMin, atlasUVs[3].yMax);
    originalUVs[14] = new Vector2(atlasUVs[3].xMax, atlasUVs[3].yMax);
    originalUVs[15] = new Vector2(atlasUVs[3].xMax, atlasUVs[3].yMin);
    // Left
    originalUVs[16] = new Vector2(atlasUVs[4].xMin, atlasUVs[4].yMin);
    originalUVs[17] = new Vector2(atlasUVs[4].xMin, atlasUVs[4].yMax);
    originalUVs[18] = new Vector2(atlasUVs[4].xMax, atlasUVs[4].yMax);
    originalUVs[19] = new Vector2(atlasUVs[4].xMax, atlasUVs[4].yMin);
    // Right        
    originalUVs[20] = new Vector2(atlasUVs[5].xMin, atlasUVs[5].yMin);
    originalUVs[21] = new Vector2(atlasUVs[5].xMin, atlasUVs[5].yMax);
    originalUVs[22] = new Vector2(atlasUVs[5].xMax, atlasUVs[5].yMax);
    originalUVs[23] = new Vector2(atlasUVs[5].xMax, atlasUVs[5].yMin);
    mesh.uv = originalUVs;

这是它所基于的链接:

http://answers.unity3d.com/questions/542787/change-texture-of-cube-sides.html