我具有使用“ Sprite Renderer”组件动态实例化的多维数据集。
使用下面的代码,我将纹理从URL加载到多维数据集的精灵中。
只要场景中没有立方体贴图,一切都清晰可见。但是,在加载多维数据集贴图时,所有的精灵都不可见,并且只有多维数据集可见。是什么原因导致此问题?
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class urlSkybox : MonoBehaviour
{
//public string url = "http://smupromp.altervista.org/f1.png?fbclid=IwAR2IcpdveA4vXP1_o0b9PdQ1-mxsxO6kkqdyhSQoxY48Dpv_uOhpNfYlRhg";
public int CubemapResolution = 2048;
private int cont=0;
private int contRoom = 0;
List<string> urlRoom = new List<string>();
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 Start()
{
string tmp = string.Empty;
do
{
Debug.Log("Attesa...");
} while (!System.IO.File.Exists("config.php"));
tmp = File.ReadAllText("config.php");
string[] lines = tmp.Split('\n', '\r');
foreach (string line in lines)
{
if (!string.IsNullOrEmpty(line.Trim()))
{
string[] d = line.Split(' ');
if (d[0][0] == '@') //Qundi se trova il link della 360
{
urlRoom.Add("http://smupromp.altervista.org/uploads/"+d[1]);
Debug.Log(d[1]);
cont++;
}
}
}
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (contRoom < cont)
{
StartCoroutine(setImage(urlRoom[contRoom++]));
}
}
/*// When trigger, we start the process
if (Input.GetMouseButtonDown(0))
{
switch (cont)
{
case 0:
StartCoroutine(setImage());
cont++;
break;
case 1:
url = "http://smupromp.altervista.org/f2.png";
StartCoroutine(setImage());
cont = 0;
break;
}
}*/
}
IEnumerator setImage(string url)
{
WWW www = new WWW(url);
//Texture myGUITexture = Resources.Load("23") as Texture;
Debug.Log(www.bytesDownloaded);
//Debug.Log(www.progress);
string _download;
while (!www.isDone)
{
_download = (www.progress*100).ToString();
Debug.Log(_download);
yield return null;
}
yield return www;
Debug.Log("Importazione terminata");
Debug.Log(www.texture);
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);
}
}
动态建立立方体贴图纹理的代码非常慢;如果您对此有任何建议,它会被接受。