Skip to content

Commit

Permalink
Add Realtime GI support
Browse files Browse the repository at this point in the history
- Alongside with some inspector tweaks
  • Loading branch information
JLChnToZ committed Dec 27, 2023
1 parent 44f5687 commit 1d8d9fa
Show file tree
Hide file tree
Showing 5 changed files with 352 additions and 142 deletions.
75 changes: 59 additions & 16 deletions Packages/idv.jlchntoz.vvmw/Editor/VVMW/CoreEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class CoreEditor : VVMWEditorBase {
SerializedProperty screenTargetPropertyNamesProperty;
SerializedProperty screenTargetDefaultTexturesProperty;
SerializedProperty avProPropertyNamesProperty;
SerializedProperty realtimeGIUpdateIntervalProperty;
SerializedReorderableList playerHandlersList, audioSourcesList, targetsList;
string[] playerNames;
bool[] playerTypes;
Expand Down Expand Up @@ -82,6 +83,7 @@ protected override void OnEnable() {
screenTargetDefaultTexturesProperty = serializedObject.FindProperty("screenTargetDefaultTextures");
avProPropertyNamesProperty = serializedObject.FindProperty("avProPropertyNames");
defaultTextureProperty = serializedObject.FindProperty("defaultTexture");
realtimeGIUpdateIntervalProperty = serializedObject.FindProperty("realtimeGIUpdateInterval");
targetsList = new SerializedReorderableList(serializedObject.FindProperty("targets"));
screenTargetVisibilityState = new List<bool>();
for (int i = 0, count = screenTargetsProperty.arraySize; i < count; i++)
Expand All @@ -104,6 +106,8 @@ public override void OnInspectorGUI() {
if (defaultTextureProperty.objectReferenceValue == null)
EditorGUILayout.HelpBox("It is required to set a default texture to display when no video is playing.", MessageType.Error);
DrawScreenList();
EditorGUILayout.PropertyField(realtimeGIUpdateIntervalProperty);
EditorGUILayout.Space();
audioSourcesList.DoLayoutList();
var newAudioSource = EditorGUILayout.ObjectField("Add Audio Source", null, typeof(AudioSource), true) as AudioSource;
if (newAudioSource != null) {
Expand Down Expand Up @@ -461,29 +465,34 @@ static bool AppendScreen(
SerializedProperty screenTargetDefaultTexturesProperty,
SerializedProperty avProPropertyNamesProperty
) {
int screenTargetMode;
Texture defaultTexture;
string mainTexturePropertyName = null, avProPropertyName = null;
if (newTarget is CustomRenderTexture crt)
newTarget = crt.material;
if (newTarget is Material material) {
AppendElement(screenTargetsProperty, material);
AppendElement(screenTargetModesProperty, 0);
var mainTexturePropertyName = FindMainTexturePropertyName(material);
AppendElement(screenTargetPropertyNamesProperty, mainTexturePropertyName);
AppendElement(screenTargetDefaultTexturesProperty, material.GetTexture(mainTexturePropertyName));
mainTexturePropertyName = FindMainTexturePropertyName(material);
avProPropertyName = FindAVProPropertyName(material);
screenTargetMode = avProPropertyName == null ? 8 : 0;
defaultTexture = material.GetTexture(mainTexturePropertyName);
} else if (newTarget is Renderer renderer || (newTarget is GameObject rendererGO && rendererGO.TryGetComponent(out renderer))) {
AppendElement(screenTargetsProperty, renderer);
AppendElement(screenTargetModesProperty, 1);
newTarget = renderer;
material = renderer.sharedMaterial;
var mainTexturePropertyName = FindMainTexturePropertyName(material);
AppendElement(screenTargetPropertyNamesProperty, mainTexturePropertyName);
AppendElement(screenTargetDefaultTexturesProperty, material != null ? material.GetTexture(mainTexturePropertyName) : null);
mainTexturePropertyName = FindMainTexturePropertyName(material);
avProPropertyName = FindAVProPropertyName(material);
screenTargetMode = avProPropertyName == null ? 9 : 1;
defaultTexture = material != null ? material.GetTexture(mainTexturePropertyName) : null;
} else if (newTarget is RawImage rawImage || (newTarget is GameObject rawImageGO && rawImageGO.TryGetComponent(out rawImage))) {
AppendElement(screenTargetsProperty, rawImage);
AppendElement(screenTargetModesProperty, 4);
AppendElement(screenTargetPropertyNamesProperty, "");
AppendElement(screenTargetDefaultTexturesProperty, rawImage.texture);
newTarget = rawImage;
screenTargetMode = 4;
defaultTexture = rawImage.texture;
} else return false;
AppendElement(screenTargetsProperty, newTarget);
AppendElement(screenTargetModesProperty, screenTargetMode);
AppendElement(screenTargetIndecesProperty, -1);
AppendElement(avProPropertyNamesProperty, "_IsAVProVideo");
AppendElement(screenTargetPropertyNamesProperty, mainTexturePropertyName ?? "_MainTex");
AppendElement(screenTargetDefaultTexturesProperty, defaultTexture);
AppendElement(avProPropertyNamesProperty, avProPropertyName ?? "_IsAVProVideo");
return true;
}

Expand All @@ -499,7 +508,7 @@ static void AppendShaderPropertiesToMenu(GenericMenu menu, Shader shader, Serial
if (ShaderUtil.GetPropertyType(shader, j) != type) continue;
var propertyName = ShaderUtil.GetPropertyName(shader, j);
menu.AddItem(
new GUIContent($"{ShaderUtil.GetPropertyDescription(shader, j)} ({propertyName})"),
new GUIContent($"{ShaderUtil.GetPropertyDescription(shader, j)} ({propertyName})".Replace('/', '.')),
property.stringValue == propertyName, SetValue, (property, propertyName)
);
}
Expand All @@ -508,6 +517,7 @@ static void AppendShaderPropertiesToMenu(GenericMenu menu, Shader shader, Serial
static string FindMainTexturePropertyName(Material material) {
if (material != null) {
var shader = material.shader;
if (shader == null) return "";
int count = shader.GetPropertyCount();
for (int i = 0; i < count; i++)
if (shader.GetPropertyType(i) == ShaderPropertyType.Texture && shader.GetPropertyFlags(i).HasFlag(ShaderPropertyFlags.MainTexture))
Expand All @@ -516,6 +526,39 @@ static string FindMainTexturePropertyName(Material material) {
return "_MainTex";
}

static string FindAVProPropertyName(Material material) {
if (material == null) return null;
var shader = material.shader;
if (shader == null) return null;
string matchedName = null;
int count = shader.GetPropertyCount();
int score = 0;
for (int i = 0; i < count; i++) {
var propertyType = shader.GetPropertyType(i);
int currentScore = 0;
switch (propertyType) {
case ShaderPropertyType.Float:
case ShaderPropertyType.Range:
currentScore = 1;
break;
case ShaderPropertyType.Int:
currentScore = 2;
break;
}
if (currentScore == 0) continue;
var name = shader.GetPropertyName(i);
if (name.StartsWith("_Is", StringComparison.OrdinalIgnoreCase))
currentScore++;
if (name.Contains("AVPro", StringComparison.OrdinalIgnoreCase))
currentScore += 2;
if (currentScore > score && currentScore > 3) {
score = currentScore;
matchedName = name;
}
}
return matchedName;
}

static GUIContent GetTempContent(SerializedProperty property) => GetTempContent(property.displayName, property.tooltip);

static GUIContent GetTempContent(string text, string tooltip = "") {
Expand Down
15 changes: 12 additions & 3 deletions Packages/idv.jlchntoz.vvmw/Materials/VVMW/VideoSurface.mat
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,37 @@
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
serializedVersion: 8
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: VideoSurface
m_Shader: {fileID: 4800000, guid: 02bd37a149eb24949bf986a6cedd92c8, type: 3}
m_ShaderKeywords: _
m_LightmapFlags: 4
m_Parent: {fileID: 0}
m_ModifiedSerializedProperties: 0
m_ValidKeywords:
- _EMISSION
m_InvalidKeywords:
- _
m_LightmapFlags: 1
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_LockedProperties:
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _MainTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Ints: []
m_Floats:
- _AspectRatio: 1.777778
- _EmissionIntensity: 10
- _Glossiness: 0.2
- _IsAVProVideo: 0
- _IsMirror: 1
Expand All @@ -34,3 +42,4 @@ Material:
- _Color: {r: 0, g: 0, b: 0, a: 1}
- _StereoExtend: {r: 1, g: 1, b: 0, a: 0}
- _StereoShift: {r: 0, g: 0, b: 0, a: 0}
m_BuildTextureStacks: []
Loading

0 comments on commit 1d8d9fa

Please sign in to comment.