C# 从远程图像编辑立方体映射Skybox

C# 从远程图像编辑立方体映射Skybox,c#,unity3d,textures,unity5,skybox,C#,Unity3d,Textures,Unity5,Skybox,我需要从服务器下载一个图像,然后将其转换为立方体贴图,最后将这个立方体贴图放入我的Skybox中 我和C#一起工作 我想出了这个密码: public string url = "image/url.jpg"; void Update() { // When trigger, we start the process if (Input.GetKeyDown("f")) { // start Coroutine to handle the WWW asynchr

我需要从服务器下载一个图像,然后将其转换为立方体贴图,最后将这个立方体贴图放入我的Skybox中

我和C#一起工作

我想出了这个密码:

public string url = "image/url.jpg";

void Update() {
    // When trigger, we start the process
    if (Input.GetKeyDown("f")) {

        // start Coroutine to handle the WWW asynchronous process
        StartCoroutine("setImage");
    }
}

IEnumerator setImage () {

    Texture2D tex;
    tex = new Texture2D(2048, 2048, TextureFormat.RGBA32, false);

    WWW www = new WWW(url);
    //Texture myGUITexture = Resources.Load("23") as Texture;

    Debug.Log (www.bytesDownloaded);
    Debug.Log (www.progress);
    Debug.Log (www.texture);

    yield return www;

    // we put the downloaded image into the new texture
    www.LoadImageIntoTexture(tex);

    // new cubemap 
    Cubemap c = new Cubemap(2048, TextureFormat.RGBA32, false);
    Color[] CubeMapColors;
    CubeMapColors = tex.GetPixels();
    c.SetPixels(CubeMapColors, CubemapFace.PositiveX);
    // we set the cubemap from the texture pixel by pixel
    c.Apply();

    //NewTexture.isPowerOfTwo = true;

    //Debug.Log (RenderSettings.skybox.GetTexture ("_Tex"));

    // We change the Cubemap of the Skybox
    RenderSettings.skybox.SetTexture("_Tex", c);
}
我对所有代码进行了注释,以解释我认为我在做什么

我做了这个逐像素创建立方体贴图的“把戏”,因为编辑器的方法(顺便说一句,这是非常简单的)似乎不可能从我能读到的其他人的帖子中实现

最终,结果只是一堆灰色像素

我真的不知道是什么在我的过程中搞砸了这么多,我看到的唯一的“阴影”点是TextureFormat

我之所以选择RGBA32,是因为当我查看Unity编辑器时,我看到BC7格式,它被错误记录为SetTexture不可能,并且从他们的解释中,它可能在RGBA32中解压缩

当然,控制台中没有剩余的错误


我真的很惊讶没有一个简单的方法可以做到这一点。我的意思是,不必从http获取图像并将其放入skybox,只需更改skybox的纹理即可。我遗漏了什么吗?

我想我已经为你找到了解决办法

下面的代码基本上是对代码的扩展

我修复了一些困扰我的东西,但是代码基本上是一样的

using System.Collections;
using UnityEngine;

public class ReplaceCubemap : MonoBehaviour
{
    public string url = "your file name";
    public int CubemapResolution = 256;

    private Texture2D source;

    /// <summary>
    /// These are the faces of a cube
    /// </summary>
    private Vector3[][] faces =
    {
        new Vector3[] {
            new Vector3(1.0f, 1.0f, -1.0f),
            new Vector3(1.0f, 1.0f, 1.0f),
            new Vector3(1.0f, -1.0f, -1.0f),
            new Vector3(1.0f, -1.0f, 1.0f)
        },
        new Vector3[] {
            new Vector3(-1.0f, 1.0f, 1.0f),
            new Vector3(-1.0f, 1.0f, -1.0f),
            new Vector3(-1.0f, -1.0f, 1.0f),
            new Vector3(-1.0f, -1.0f, -1.0f)
        },
        new Vector3[] {
            new Vector3(-1.0f, 1.0f, 1.0f),
            new Vector3(1.0f, 1.0f, 1.0f),
            new Vector3(-1.0f, 1.0f, -1.0f),
            new Vector3(1.0f, 1.0f, -1.0f)
        },
        new Vector3[] {
            new Vector3(-1.0f, -1.0f, -1.0f),
            new Vector3(1.0f, -1.0f, -1.0f),
            new Vector3(-1.0f, -1.0f, 1.0f),
            new Vector3(1.0f, -1.0f, 1.0f)
        },
        new Vector3[] {
            new Vector3(-1.0f, 1.0f, -1.0f),
            new Vector3(1.0f, 1.0f, -1.0f),
            new Vector3(-1.0f, -1.0f, -1.0f),
            new Vector3(1.0f, -1.0f, -1.0f)
        },
        new Vector3[] {
            new Vector3(1.0f, 1.0f, 1.0f),
            new Vector3(-1.0f, 1.0f, 1.0f),
            new Vector3(1.0f, -1.0f, 1.0f),
            new Vector3(-1.0f, -1.0f, 1.0f)
        }
    };

    void Update()
    {
        // When trigger, we start the process
        if (Input.GetKeyDown(KeyCode.F))
        {

            // start Coroutine to handle the WWW asynchronous process
            StartCoroutine(setImage());
        }
    }

    IEnumerator setImage()
    {
        WWW www = new WWW(url);
        //Texture myGUITexture = Resources.Load("23") as Texture;

        Debug.Log(www.bytesDownloaded);
        Debug.Log(www.progress);
        Debug.Log(www.texture);

        yield return www;

        source = new Texture2D(www.texture.width, www.texture.height);
        // we put the downloaded image into the new texture
        www.LoadImageIntoTexture(source);

        // new cubemap 
        Cubemap c = new Cubemap(CubemapResolution, TextureFormat.RGBA32, false);

        Color[] CubeMapColors;

        for (int i = 0; i < 6; i++)
        {
            CubeMapColors = CreateCubemapTexture(CubemapResolution, (CubemapFace)i);
            c.SetPixels(CubeMapColors, (CubemapFace)i);
        }
        // we set the cubemap from the texture pixel by pixel
        c.Apply();

        //Destroy all unused textures
        DestroyImmediate(source);
        DestroyImmediate(www.texture);
        Texture2D[] texs = FindObjectsOfType<Texture2D>();
        for (int i = 0; i < texs.Length; i++)
        {
            DestroyImmediate(texs[i]);
        }

        // We change the Cubemap of the Skybox
        RenderSettings.skybox.SetTexture("_Tex", c);
    }

    /// <summary>
    /// Generates a Texture that represents the given face for the cubemap.
    /// </summary>
    /// <param name="resolution">The targetresolution in pixels</param>
    /// <param name="face">The target face</param>
    /// <returns></returns>
    private Color[] CreateCubemapTexture(int resolution, CubemapFace face)
    {
        Texture2D texture = new Texture2D(resolution, resolution, TextureFormat.RGB24, false);

        Vector3 texelX_Step = (faces[(int)face][1] - faces[(int)face][0]) / resolution;
        Vector3 texelY_Step = (faces[(int)face][3] - faces[(int)face][2]) / resolution;

        float texelSize = 1.0f / resolution;
        float texelIndex = 0.0f;

        //Create textured face
        Color[] cols = new Color[resolution];
        for (int y = 0; y < resolution; y++)
        {
            Vector3 texelX = faces[(int)face][0];
            Vector3 texelY = faces[(int)face][2];
            for (int x = 0; x < resolution; x++)
            {
                cols[x] = Project(Vector3.Lerp(texelX, texelY, texelIndex).normalized);
                texelX += texelX_Step;
                texelY += texelY_Step;
            }
            texture.SetPixels(0, y, resolution, 1, cols);
            texelIndex += texelSize;
        }
        texture.wrapMode = TextureWrapMode.Clamp;
        texture.Apply();

        Color[] colors = texture.GetPixels();
        DestroyImmediate(texture);

        return colors;
    }

    /// <summary>
    /// Projects a directional vector to the texture using spherical mapping
    /// </summary>
    /// <param name="direction">The direction in which you view</param>
    /// <returns></returns>
    private Color Project(Vector3 direction)
    {
        float theta = Mathf.Atan2(direction.z, direction.x) + Mathf.PI / 180.0f;
        float phi = Mathf.Acos(direction.y);

        int texelX = (int)(((theta / Mathf.PI) * 0.5f + 0.5f) * source.width);
        if (texelX < 0) texelX = 0;
        if (texelX >= source.width) texelX = source.width - 1;
        int texelY = (int)((phi / Mathf.PI) * source.height);
        if (texelY < 0) texelY = 0;
        if (texelY >= source.height) texelY = source.height - 1;

        return source.GetPixel(texelX, source.height - texelY - 1);
    }
}
使用系统集合;
使用UnityEngine;
公共类ReplaceCubemap:单行为
{
公共字符串url=“您的文件名”;
公共整数立方预解=256;
私有纹理2d源;
/// 
///这些是立方体的面
/// 
专用矢量3[][]面=
{
新矢量3[]{
新矢量3(1.0f,1.0f,-1.0f),
新矢量3(1.0f、1.0f、1.0f),
新矢量3(1.0f,-1.0f,-1.0f),
新矢量3(1.0f,-1.0f,1.0f)
},
新矢量3[]{
新矢量3(-1.0f,1.0f,1.0f),
新矢量3(-1.0f,1.0f,-1.0f),
新矢量3(-1.0f,-1.0f,1.0f),
新矢量3(-1.0f,-1.0f,-1.0f)
},
新矢量3[]{
新矢量3(-1.0f,1.0f,1.0f),
新矢量3(1.0f、1.0f、1.0f),
新矢量3(-1.0f,1.0f,-1.0f),
新矢量3(1.0f,1.0f,-1.0f)
},
新矢量3[]{
新矢量3(-1.0f,-1.0f,-1.0f),
新矢量3(1.0f,-1.0f,-1.0f),
新矢量3(-1.0f,-1.0f,1.0f),
新矢量3(1.0f,-1.0f,1.0f)
},
新矢量3[]{
新矢量3(-1.0f,1.0f,-1.0f),
新矢量3(1.0f,1.0f,-1.0f),
新矢量3(-1.0f,-1.0f,-1.0f),
新矢量3(1.0f,-1.0f,-1.0f)
},
新矢量3[]{
新矢量3(1.0f、1.0f、1.0f),
新矢量3(-1.0f,1.0f,1.0f),
新矢量3(1.0f,-1.0f,1.0f),
新矢量3(-1.0f,-1.0f,1.0f)
}
};
无效更新()
{
//当触发时,我们开始这个过程
if(Input.GetKeyDown(KeyCode.F))
{
//启动协同路由以处理WWW异步进程
start例程(setImage());
}
}
IEnumerator setImage()
{
WWW=新的WWW(url);
//纹理myGUITexture=Resources.Load(“23”)作为纹理;
Debug.Log(www.bytes下载);
Debug.Log(www.progress);
Debug.Log(www.texture);
收益率;
来源=新纹理2d(www.texture.width,www.texture.height);
//我们将下载的图像放入新的纹理中
www.LoadImageIntoTexture(来源);
//新立方地图
Cubemap c=新的Cubemap(CubemapResolution,TextureFormat.RGBA32,false);
颜色[]立方色;
对于(int i=0;i<6;i++)
{
CubeMapColors=CreateCubemapTexture(CubemapResolution,(CubemapFace)i);
c、 设置像素(立方色,(立方面)i);
}
//我们从纹理逐像素设置立方体贴图
c、 应用();
//销毁所有未使用的纹理
直接(来源);
(www.texture);
Texture2D[]texs=FindObjectsOfType();
对于(int i=0;i