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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
using System;
using UnityEditor;
using UnityEngine;
namespace VRM
{
/// <summary>
/// based
///
/// * https://gist.github.com/radiatoryang/a2282d44ba71848e498bb2e03da98991
/// </summary>
/// <summary>
/// PreviewRenderUtilityを管理する
/// PreviewSceneをレンダリングする
/// </summary>
public class PreviewFaceRenderer : IDisposable
{
PreviewRenderUtility m_previewUtility;
public Camera PreviewCamera
{
get
{
#if UNITY_2017_1_OR_NEWER
return m_previewUtility.camera;
#else
return m_previewUtility.m_Camera;
#endif
}
}
public Light[] PreviewLights
{
get
{
#if UNITY_2017_1_OR_NEWER
return m_previewUtility.lights;
#else
return m_previewUtility.m_Light;
#endif
}
}
public void SetAmbientColor(Color color)
{
#if UNITY_2017_1_OR_NEWER
m_previewUtility.ambientColor = color;
#else
// ?
#endif
}
public PreviewFaceRenderer()
{
m_previewUtility = new PreviewRenderUtility();
foreach (var light in PreviewLights)
{
if (light == null) continue;
light.intensity = 0f;
}
if (PreviewLights.Length > 0 && PreviewLights[0] != null)
{
PreviewLights[0].intensity = 1f;
PreviewLights[0].transform.rotation = Quaternion.Euler(20f, 200f, 0);
PreviewLights[0].color = new Color(1f, 1f, 1f, 1f);
}
SetAmbientColor(new Color(0.1f, 0.1f, 0.1f, 1f));
}
class FogScope : IDisposable
{
bool fog;
public FogScope()
{
fog = RenderSettings.fog; // ... let's remember the current fog setting...
// we are technically rendering everything in the scene, so scene fog might affect it...
Unsupported.SetRenderSettingsUseFogNoDirty(false); // ... and then temporarily turn it off
}
public void Dispose()
{
Unsupported.SetRenderSettingsUseFogNoDirty(fog);
}
}
//const float FACTOR = 0.1f;
public Texture Render(Rect r, GUIStyle background, PreviewSceneManager scene,
float yaw, float pitch, Vector3 position)
{
if (scene == null) return null;
using (var fog = new FogScope())
{
m_previewUtility.BeginPreview(r, background); // set up the PreviewRenderUtility's mini internal scene
// setup the ObjectPreview's camera
scene.SetupCamera(PreviewCamera, scene.TargetPosition, yaw, pitch, position);
foreach (var item in scene.EnumRenderItems)
{
// now, actually render out the RenderTexture
//RenderMeshPreview(previewMesh, skinMeshRender.sharedMaterials);
// submesh support, in case the mesh is made of multiple parts
int subMeshCount = item.Mesh.subMeshCount;
for (int i = 0; i < subMeshCount; i++)
{
m_previewUtility.DrawMesh(item.Mesh,
item.Position, item.Rotation,
item.Materials[i], i);
}
}
// VERY IMPORTANT: this manually tells the camera to render and produce the render texture
PreviewCamera.Render();
//m_previewUtility.Render(false, false);
// reset the scene's fog from before
return m_previewUtility.EndPreview();
}
}
#region IDisposable Support
private bool disposedValue = false; // 重複する呼び出しを検出するには
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
// TODO: マネージ状態を破棄します (マネージ オブジェクト)。
if (this.m_previewUtility != null)
{
this.m_previewUtility.Cleanup();
this.m_previewUtility = null;
}
}
// TODO: アンマネージ リソース (アンマネージ オブジェクト) を解放し、下のファイナライザーをオーバーライドします。
// TODO: 大きなフィールドを null に設定します。
disposedValue = true;
}
}
// TODO: 上の Dispose(bool disposing) にアンマネージ リソースを解放するコードが含まれる場合にのみ、ファイナライザーをオーバーライドします。
// ~PreviewFaceRenderer() {
// // このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
// Dispose(false);
// }
// このコードは、破棄可能なパターンを正しく実装できるように追加されました。
public void Dispose()
{
// このコードを変更しないでください。クリーンアップ コードを上の Dispose(bool disposing) に記述します。
Dispose(true);
// TODO: 上のファイナライザーがオーバーライドされる場合は、次の行のコメントを解除してください。
// GC.SuppressFinalize(this);
}
#endregion
}
}
|