Last active
October 18, 2023 08:58
-
-
Save FriendSea/657ed2ac3d8801f04444c1238d6fef46 to your computer and use it in GitHub Desktop.
A wizard to render a scene and create a cubemap image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using System.IO; | |
public class CubeMapRenderer : ScriptableWizard { | |
[SerializeField] | |
Shader TransformShader; | |
[SerializeField] | |
Camera RenderCamera; | |
[SerializeField] | |
string Path = "Assets/NewCubemap.png"; | |
const int resolution = 2048; | |
void OnWizardCreate() | |
{ | |
if(RenderCamera==null) | |
RenderCamera = Camera.main; | |
Cubemap newcubemap = new Cubemap(resolution, TextureFormat.ARGB32, false); | |
RenderCamera.RenderToCubemap(newcubemap); | |
Material mat = new Material(TransformShader); | |
RenderTexture newtexture = new RenderTexture(resolution * 2, resolution, 16, RenderTextureFormat.ARGB32); | |
Graphics.Blit(newcubemap, newtexture, mat); | |
DestroyImmediate(mat); | |
SaveImage(newtexture, Path); | |
DestroyImmediate(newcubemap); | |
DestroyImmediate(newtexture); | |
} | |
void SaveImage(RenderTexture texture, string path){ | |
Texture2D savetexture = new Texture2D(texture.width, texture.height, TextureFormat.RGB24, false); | |
var activeamp = RenderTexture.active; | |
RenderTexture.active = texture; | |
savetexture.ReadPixels(new Rect(0, 0, texture.width, texture.height), 0, 0); | |
savetexture.Apply(); | |
RenderTexture.active = activeamp; | |
byte[] data = savetexture.EncodeToPNG(); | |
File.WriteAllBytes(path, data); | |
DestroyImmediate(savetexture); | |
} | |
[MenuItem("Window/Render2Cubemap")] | |
static void ShowREnderWizard() | |
{ | |
DisplayWizard<CubeMapRenderer>("cubemap renderer", "Render"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/TransformCube2D" | |
{ | |
Properties | |
{ | |
_MainTex("Texture", CUBE) = "white" {} | |
} | |
SubShader | |
{ | |
Tags { "RenderType" = "Opaque" } | |
LOD 100 | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
struct appdata | |
{ | |
float4 vertex : POSITION; | |
float2 uv : TEXCOORD0; | |
}; | |
struct v2f | |
{ | |
float2 uv : TEXCOORD0; | |
float4 vertex : SV_POSITION; | |
}; | |
samplerCUBE _MainTex; | |
v2f vert(appdata v) | |
{ | |
v2f o; | |
o.vertex = UnityObjectToClipPos(v.vertex); | |
o.uv = v.uv; | |
return o; | |
} | |
fixed4 frag(v2f i) : SV_Target | |
{ | |
i.uv.x *= UNITY_PI * 2; | |
i.uv.y *= UNITY_PI; | |
float3 vec; | |
vec.x = sin(i.uv.y)*sin(i.uv.x); | |
vec.y = -cos(i.uv.y); | |
vec.z = sin(i.uv.y)*cos(i.uv.x); | |
fixed4 col = texCUBE(_MainTex, vec); | |
return col; | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment