-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathRenderTextureOutput.cs
More file actions
157 lines (122 loc) · 5.6 KB
/
RenderTextureOutput.cs
File metadata and controls
157 lines (122 loc) · 5.6 KB
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
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon;
using System.IO;
using static VRC.SDKBase.VRCShader;
#if UNITY_EDITOR && !COMPILER_UDONSHARP
using UnityEditor;
using UdonSharpEditor;
#endif
#pragma warning disable CS0612 // Type or member is obsolete
namespace UdonSharp.Video
{
[UdonBehaviourSyncMode(BehaviourSyncMode.NoVariableSync)]
[AddComponentMenu("Udon Sharp/Video/Utilities/Render Texture Output")]
public class RenderTextureOutput : UdonSharpBehaviour
{
#pragma warning disable CS0649
[SerializeField]
private USharpVideoPlayer sourceVideoPlayer;
private VideoPlayerManager videoPlayerManager;
#pragma warning restore CS0649
public CustomRenderTexture outputTexture;
public bool useUdonGlobalTexture;
private Material outputMat;
private void Start()
{
outputMat = outputTexture.material;
videoPlayerManager = sourceVideoPlayer.GetComponentInChildren<VideoPlayerManager>(true);
if(useUdonGlobalTexture)
{
SetGlobalTexture(PropertyToID("_Udon_VideoTex"), outputTexture);
}
}
private Texture lastTex;
private void LateUpdate()
{
Texture videoPlayerTex = videoPlayerManager.GetVideoTexture();
if (lastTex != videoPlayerTex)
{
outputMat.SetTexture("_SourceTexture", videoPlayerTex);
outputMat.SetInt("_IsAVPro", System.Convert.ToInt32(sourceVideoPlayer.IsUsingAVProPlayer()));
lastTex = videoPlayerTex;
}
}
}
#if UNITY_EDITOR && !COMPILER_UDONSHARP
[CustomEditor(typeof(RenderTextureOutput))]
internal class RenderTextureOutputInspector : Editor
{
internal class RenderTextureCreator : EditorWindow
{
public RenderTextureOutput targetOutput;
private Vector2Int resolution = new Vector2Int(1920, 1080);
private void OnGUI()
{
targetOutput = (RenderTextureOutput)EditorGUILayout.ObjectField("Target component", targetOutput, typeof(RenderTextureOutput), true);
resolution = EditorGUILayout.Vector2IntField("Resolution", resolution);
if (GUILayout.Button("Create Texture"))
{
CreateCRT();
}
}
void CreateCRT()
{
string filePath = EditorUtility.SaveFilePanelInProject("Texture save", "VideoOutputTexture", "asset", "Choose a name for the texture file");
if (string.IsNullOrEmpty(filePath))
return;
CustomRenderTexture newCRT = new CustomRenderTexture(resolution.x, resolution.y, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB);
newCRT.autoGenerateMips = false;
newCRT.initializationMode = CustomRenderTextureUpdateMode.OnLoad;
newCRT.initializationColor = Color.black;
newCRT.initializationSource = CustomRenderTextureInitializationSource.TextureAndColor;
newCRT.depth = 0;
newCRT.updateMode = CustomRenderTextureUpdateMode.Realtime;
Material updateMat = new Material(Shader.Find("Merlin/World/Render Texture Processor"));
updateMat.name = $"{Path.GetFileNameWithoutExtension(filePath)}_Update";
updateMat.SetFloat("_TargetAspectRatio", resolution.x / (float)resolution.y);
AssetDatabase.CreateAsset(newCRT, filePath);
AssetDatabase.AddObjectToAsset(updateMat, newCRT);
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(updateMat));
newCRT.material = updateMat;
AssetDatabase.SaveAssets();
targetOutput.outputTexture = newCRT;
targetOutput.ApplyProxyModifications();
}
}
private SerializedProperty sourceVideoPlayerProperty;
private SerializedProperty outputTextureProperty;
private SerializedProperty useUdonGlobalTextureProperty;
private void OnEnable()
{
sourceVideoPlayerProperty = serializedObject.FindProperty("sourceVideoPlayer");
outputTextureProperty = serializedObject.FindProperty("outputTexture");
useUdonGlobalTextureProperty = serializedObject.FindProperty("useUdonGlobalTexture");
}
public override void OnInspectorGUI()
{
if (UdonSharpGUI.DrawDefaultUdonSharpBehaviourHeader(target)) return;
EditorGUILayout.PropertyField(sourceVideoPlayerProperty);
if (sourceVideoPlayerProperty.objectReferenceValue == null)
EditorGUILayout.HelpBox("A source video player must be specified", MessageType.Error);
EditorGUILayout.PropertyField(outputTextureProperty);
EditorGUILayout.PropertyField(useUdonGlobalTextureProperty);
serializedObject.ApplyModifiedProperties();
RenderTextureOutput output = (RenderTextureOutput)target;
if (output.outputTexture == null)
{
EditorGUILayout.Space();
if (GUILayout.Button("Setup Output Texture", GUILayout.Height(30f)))
{
RenderTextureCreator window = EditorWindow.GetWindow<RenderTextureCreator>(false, "Render Texture Creator");
window.targetOutput = output;
window.maxSize = new Vector2(450f, 100f);
window.minSize = new Vector2(450f, 100f);
}
}
}
}
#endif
}