首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从texture2D到立方图

从texture2D到立方图
EN

Stack Overflow用户
提问于 2017-03-12 19:08:20
回答 1查看 1.1K关注 0票数 0

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

代码语言:javascript
复制
 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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-12 19:18:29

这是我用来在立方体的每一面上放置不同图像的代码。它需要一个包含一组图像的Texture2d。

代码语言:javascript
复制
    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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42746635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档