- Home /
How can I extract the individual faces from a generated cubemap?
We've generated a cubemap using the editor script sample found in the Camera.RenderToCubemap documentation. We'd like to edit the faces by hand but can't find a way to access the individual face textures that make up the Cubemap texture. Is it possible to do so? If so, how?
Answer by Nawash · Sep 21, 2010 at 04:50 AM
In case somebody needs this, here is how to extract all the faces in png format, in the asset folder. Copy this script in an "editor" folder in a project "asset" folder
import System.IO;
class SaveCubeMapToPngWizard extends ScriptableWizard {
var cubemap : Cubemap;
function OnWizardUpdate () { helpString = "Select cubemap to save to individual png"; isValid = (cubemap != null); }
function OnWizardCreate () { var width = cubemap.width; var height = cubemap.height;
Debug.Log(Application.dataPath + "/" +cubemap.name +"_PositiveX.png");
var tex = new Texture2D (width, height, TextureFormat.RGB24, false);
// Read screen contents into the texture
tex.SetPixels(cubemap.GetPixels(CubemapFace.PositiveX));
// Encode texture into PNG
var bytes = tex.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/" + cubemap.name +"_PositiveX.png", bytes);
tex.SetPixels(cubemap.GetPixels(CubemapFace.NegativeX));
bytes = tex.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/" + cubemap.name +"_NegativeX.png", bytes);
tex.SetPixels(cubemap.GetPixels(CubemapFace.PositiveY));
bytes = tex.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/" + cubemap.name +"_PositiveY.png", bytes);
tex.SetPixels(cubemap.GetPixels(CubemapFace.NegativeY));
bytes = tex.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/" + cubemap.name +"_NegativeY.png", bytes);
tex.SetPixels(cubemap.GetPixels(CubemapFace.PositiveZ));
bytes = tex.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/" + cubemap.name +"_PositiveZ.png", bytes);
tex.SetPixels(cubemap.GetPixels(CubemapFace.NegativeZ));
bytes = tex.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/" + cubemap.name +"_NegativeZ.png", bytes);
DestroyImmediate(tex);
}
@MenuItem("GameObject/Save CubeMap To Png ") static function SaveCubeMapToPng () { ScriptableWizard.DisplayWizard( "Save CubeMap To Png", SaveCubeMapToPngWizard , "Save"); }
}
Simplified version of above code:
var tex = new Texture2D (cubemap.width, cubemap.height, TextureFormat.RGB24, false);
CubemapFace[] faces = new CubemapFace[] {
CubemapFace.PositiveX, CubemapFace.NegativeX,
CubemapFace.PositiveY, CubemapFace.NegativeY,
CubemapFace.PositiveZ, CubemapFace.NegativeZ };
foreach (CubemapFace face in faces) {
tex.SetPixels(cubemap.GetPixels(face));
File.WriteAllBytes(Application.dataPath + "/" + cubemap.name + "_" + face.ToString() + ".png",
tex.EncodeToPNG());
}
Answer by ameierinco · Oct 24, 2012 at 07:49 PM
Finally got this working. The above code didn't work for me. Appears the problem is with the first line "import http://System.IO;". I'm not much of a coder, but this had to be wrong since any "//" automatically comments out anything that follows it. Anyway, here's the full script that worked for me. Hope it helps someone.
import System;
import System.IO;
class SaveCubeMapToPngWizard extends ScriptableWizard {
var cubemap : Cubemap;
function OnWizardUpdate () {
helpString = "Select cubemap to save to individual png";
isValid = (cubemap != null);
}
function OnWizardCreate ()
{
var width = cubemap.width;
var height = cubemap.height;
Debug.Log(Application.dataPath + "/" +cubemap.name +"_PositiveX.png");
var tex = new Texture2D (width, height, TextureFormat.RGB24, false);
// Read screen contents into the texture
tex.SetPixels(cubemap.GetPixels(CubemapFace.PositiveX));
// Encode texture into PNG
var bytes = tex.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/" + cubemap.name +"_PositiveX.png", bytes);
tex.SetPixels(cubemap.GetPixels(CubemapFace.NegativeX));
bytes = tex.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/" + cubemap.name +"_NegativeX.png", bytes);
tex.SetPixels(cubemap.GetPixels(CubemapFace.PositiveY));
bytes = tex.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/" + cubemap.name +"_PositiveY.png", bytes);
tex.SetPixels(cubemap.GetPixels(CubemapFace.NegativeY));
bytes = tex.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/" + cubemap.name +"_NegativeY.png", bytes);
tex.SetPixels(cubemap.GetPixels(CubemapFace.PositiveZ));
bytes = tex.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/" + cubemap.name +"_PositiveZ.png", bytes);
tex.SetPixels(cubemap.GetPixels(CubemapFace.NegativeZ));
bytes = tex.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/" + cubemap.name +"_NegativeZ.png", bytes);
DestroyImmediate(tex);
}
@MenuItem("GameObject/Save CubeMap To Png ")
static function SaveCubeMapToPng ()
{
ScriptableWizard.DisplayWizard(
"Save CubeMap To Png", SaveCubeMapToPngWizard , "Save");
}
}
I downloaded this script as well, thank you very much, man :)
Answer by MS-3D · Dec 21, 2011 at 06:20 AM
Many thanks for the script !
But some noobs (including me) have problems with this "not so good" documentation. I try to complete the docu:
Greate a Javascript with the name "SaveCubeMapToPngWizard" in a Editor folder in Assets (how described above).
Copy the script, but including the first lines "import....", "class SaveCube..." and the brace at the end of the script (in the green area).
The first line should be: "import System.IO;" and NOT "import http://System.IO;"
I hope it helps someone !
Regards - MS
Answer by JunkyardSam · May 11, 2012 at 08:13 PM
On #3, MS-3D meant to say: "import System.IO;" and NOT "import http://System.IO;"
To be clear, the first line should be: import System.IO
Your answer
Follow this Question
Related Questions
I'm trying to get a picture in game and display it on a plane 2 Answers
How to make the Health bar on Enemys head not be in relation to Player(main) Camera? 2 Answers
Assigning UV Map to model at runtime 0 Answers
Free-look camera y-rotation shake 2 Answers
OpenGLES error 0x0502 when TextureFormat.BGRA32 is used 0 Answers