-
Notifications
You must be signed in to change notification settings - Fork 226
/
BakeTexture.cs
52 lines (49 loc) · 1.52 KB
/
BakeTexture.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
/*
Resolution = Dilation
256 = 2px
512 = 4px
1024 = 8px
2048 = 16px
4096 = 32 px
8192 = 64 px
*/
using UnityEngine;
public class BakeTexture : MonoBehaviour
{
public Mesh SourceMesh;
public Shader BakeTextureShader;
public int Resolution = 2048;
public float Dilation = 16;
public Rendering RenderMode = Rendering.DirectX;
public enum Rendering
{
DirectX,
OpenGL
}
void Start()
{
if (SourceMesh != null)
{
RenderTexture renderTexture = new RenderTexture(Resolution, Resolution, 32, RenderTextureFormat.ARGB32);
renderTexture.filterMode = FilterMode.Trilinear;
renderTexture.Create();
Material material = new Material(BakeTextureShader);
RenderTexture currentTexture = RenderTexture.active;
RenderTexture.active = renderTexture;
GL.Clear(true, true, Color.black, 1.0f);
material.SetInt("_TextureSize", Resolution);
material.SetFloat("_Dilation", Dilation);
material.SetInt("_RenderMode", RenderMode == Rendering.DirectX ? 0 : 1);
material.SetPass(0);
Graphics.DrawMeshNow(SourceMesh, Vector3.zero, Quaternion.identity);
Texture2D texture = new Texture2D(Resolution, Resolution, TextureFormat.ARGB32, false, false);
texture.ReadPixels( new Rect(0, 0, Resolution, Resolution), 0, 0);
RenderTexture.active = currentTexture;
byte[] bytes = texture.EncodeToPNG();
System.IO.File.WriteAllBytes(System.IO.Path.Combine(Application.dataPath, "Texture.png"), bytes);
Destroy(material);
Destroy(texture);
renderTexture.Release();
}
}
}