var FrontTexture = (Texture2D)SkyBox.GetTexture("_FrontTex");
exportTexture(FrontTexture, Path.Combine(path,"front.png"));
var BackTexture = (Texture2D)SkyBox.GetTexture("_BackTex");
exportTexture(BackTexture, Path.Combine(path, "back.png"));
var LeftTexture = (Texture2D)SkyBox.GetTexture("_LeftTex");
exportTexture(LeftTexture, Path.Combine(path, "left.png"));
var RightTexture = (Texture2D)SkyBox.GetTexture("_RightTex");
exportTexture(RightTexture, Path.Combine(path, "right.png"));
var UpTexture = (Texture2D)SkyBox.GetTexture("_UpTex");
exportTexture(UpTexture, Path.Combine(path, "up.png"));
var DownTexture = (Texture2D)SkyBox.GetTexture("_DownTex");
exportTexture(DownTexture, Path.Combine(path, "down.png"));
private static void exportTexture(Texture2D texture, string outputPath)
{
//这些操作主要是避免readable的问题
var destRenderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB);
Graphics.Blit(texture, destRenderTexture);
var exportTexture = new Texture2D(texture.width, texture.height);
exportTexture.ReadPixels(new Rect(0, 0, destRenderTexture.width, destRenderTexture.height), 0, 0);
exportTexture.Apply();
File.WriteAllBytes(outputPath, exportTexture.EncodeToPNG());
RenderTexture.ReleaseTemporary(destRenderTexture);
}
右键获取 Six Sided 的 shader 属性
获得 _FrontTex _BackTex _LeftTex _RightTex _UpTex _DownTex
关于 SkyBoxGetTexture 函数.
RenderTexture.GetTemporary 分配临时渲染纹理
RenderTextureFormat --> 颜色渲染纹理
RenderTextureReadWrite --> 渲染纹理包括颜色数据 实现线性到颜色的转换
Graphics.Blit
using UnityEngine;
public class Example : MonoBehaviour
{ // 复制 aTexture 到 rTex and displays it in all cameras.
Texture aTexture;
RenderTexture rTex;
void Start()
{
if (!aTexture || !rTex)
{
Debug.LogError("A texture or a render texture are missing, assign them.");
}
}
void Update()
{
Graphics.Blit(aTexture, rTex);
}
Texture2D.ReadPixels
// Attach this script to a Camera
//Also attach a GameObject that has a Renderer (e.g. a cube) in the Display field
//Press the space key in Play mode to capture
using UnityEngine;
public class Example : MonoBehaviour
{
// Grab the camera's view when this variable is true.
bool grab;
// The "m_Display" is the GameObject whose Texture will be set to the captured image.
public Renderer m_Display;
private void Update()
{
//Press space to start the screen grab
if (Input.GetKeyDown(KeyCode.Space))
grab = true;
}
private void OnPostRender()
{
if (grab)
{
//Create a new texture with the width and height of the screen
Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
//Read the pixels in the Rect starting at 0,0 and ending at the screen's width and height
texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, false);
texture.Apply();
//Check that the display field has been assigned in the Inspector
if (m_Display != null)
//Give your GameObject with the renderer this texture
m_Display.material.mainTexture = texture;
//Reset the grab state
grab = false;
}
}
}
Texture2D.Apply
File.WriteAllBytes
仅供自己学习