/
cubemap_generator.cs
54 lines (50 loc) · 2.24 KB
/
cubemap_generator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//In project window create new cubemap from command "Create\Legacy\Cubemap" and set it to "Readable".
//Run script. Then create material with shader "Skybox/Cubemap" and set cubemap.
//Set new skybox: Window/Lighting/Settings ->Environment/Skybox Material
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.IO;
public class cubemap_generator : ScriptableWizard
{
public Transform origin;
public Cubemap cubemap;
void OnWizardCreate()
{
GameObject game_object = new GameObject("Camera");
game_object.AddComponent<Camera>();
game_object.transform.position = origin.position;
game_object.transform.rotation = Quaternion.identity;
game_object.GetComponent<Camera>().RenderToCubemap(cubemap);
DestroyImmediate(game_object);
SaveCubemap();
}
[MenuItem("GameObject/Render to cubemap")]
static void RenderCubemap()
{
ScriptableWizard.DisplayWizard<cubemap_generator>("Render to cubemap", "Generate");
}
void SaveCubemap()
{
Texture2D image = new Texture2D (cubemap.width, cubemap.height, TextureFormat.RGB24, false);
image.SetPixels(cubemap.GetPixels(CubemapFace.PositiveX));
byte[] bytes = image.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/" + cubemap.name +"_PositiveX.png", bytes);
image.SetPixels(cubemap.GetPixels(CubemapFace.NegativeX));
bytes = image.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/" + cubemap.name +"_NegativeX.png", bytes);
image.SetPixels(cubemap.GetPixels(CubemapFace.PositiveY));
bytes = image.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/" + cubemap.name +"_PositiveY.png", bytes);
image.SetPixels(cubemap.GetPixels(CubemapFace.NegativeY));
bytes = image.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/" + cubemap.name +"_NegativeY.png", bytes);
image.SetPixels(cubemap.GetPixels(CubemapFace.PositiveZ));
bytes = image.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/" + cubemap.name +"_PositiveZ.png", bytes);
image.SetPixels(cubemap.GetPixels(CubemapFace.NegativeZ));
bytes = image.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/" + cubemap.name +"_NegativeZ.png", bytes);
DestroyImmediate(image);
}
}