Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CREST_ prefix to keywords #769

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions crest/Assets/Crest/Crest/Scripts/Editor/MaterialHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Crest Ocean System

// This file is subject to the MIT License as seen in the root of this folder structure (LICENSE)

namespace Crest.Editor
{
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using System.Linq;

public static class MaterialHelper
{
internal static readonly Dictionary<string, string> RenamedKeywords = new Dictionary<string, string>
{
{ "_Foam", "CREST_FOAM" },
{ "_Shadows", "CREST_SHADOWS" },
{ "_Caustics", "CREST_CAUSTICS" },
{ "_Flow", "CREST_FLOW" },
};

internal static void MigrateKeywords(Material material, SerializedObject serializedObject,
SerializedProperty floatProperties, SerializedProperty keywordProperties)
{
foreach (var entry in RenamedKeywords)
{
RenameKeyword(entry.Key, entry.Value, floatProperties, keywordProperties);
}

// Order is important. This is what it takes to save our changes to the material.
serializedObject.ApplyModifiedProperties();
EditorUtility.SetDirty(material);
}

internal static void MigrateKeywordsGUI(Material material, SerializedObject serializedObject)
{
var floatProperties = serializedObject.FindProperty("m_SavedProperties.m_Floats");
var keywordProperties = serializedObject.FindProperty("m_ShaderKeywords");

if (ContainsRenamedKeyword(floatProperties) && GUILayout.Button("Upgrade Material"))
{
foreach (var entry in RenamedKeywords)
{
RenameKeyword(entry.Key, entry.Value, floatProperties, keywordProperties);
}

// Order is important. This is what it takes to save our changes to the material.
serializedObject.ApplyModifiedProperties();
EditorUtility.SetDirty(material);
AssetDatabase.SaveAssets();
GUIUtility.ExitGUI();
}
}

internal static bool ContainsRenamedKeyword(SerializedProperty floatProperties)
{
if (floatProperties != null && floatProperties.isArray)
{
for (int index = 0; index < floatProperties.arraySize; index++)
{
if (RenamedKeywords.ContainsKey(floatProperties.GetArrayElementAtIndex(index).displayName))
{
return true;
}
}
}

return false;
}

internal static void RenameKeyword(string oldName, string newName, SerializedProperty floatProperties,
SerializedProperty keywordProperties)
{
if (floatProperties == null || !floatProperties.isArray || keywordProperties == null)
{
// TODO: Error
return;
}

for (int i = 0; i < floatProperties.arraySize; i++)
{
var oldProperty = floatProperties.GetArrayElementAtIndex(i);
if (oldProperty.displayName != oldName)
{
continue;
}

// If the material/shader has been loaded, it will already have created the new properties.
var isFound = false;
for (int ii = 0; ii < floatProperties.arraySize; ii++)
{
SerializedProperty newProperty = floatProperties.GetArrayElementAtIndex(ii);

if (newProperty.displayName == newName)
{
RenameKeyword(oldName, newName, oldProperty, newProperty, keywordProperties);
isFound = true;
break;
}
}

if (!isFound)
{
// Insert at the end of the array.
floatProperties.InsertArrayElementAtIndex(floatProperties.arraySize);
// Fetch newly inserted property. arraySize dynamically increases on insertion.
SerializedProperty newProperty = floatProperties.GetArrayElementAtIndex(floatProperties.arraySize - 1);
RenameKeyword(oldName, newName, oldProperty, newProperty, keywordProperties);
}

// Delete the old property.
floatProperties.DeleteArrayElementAtIndex(i);
return;
}
}

internal static void RenameKeyword(string oldName, string newName, SerializedProperty oldProperty,
SerializedProperty newProperty, SerializedProperty keywordProperties)
{
// A property is a pair so we need to navigate down a level.
oldProperty.Next(true);
// Skip the first value which is the label.
oldProperty.Next(false);

// Navigate to the label.
newProperty.Next(true);
// Set the label just in case this is newly inserted.
newProperty.stringValue = newName;
// Navigate to the value.
newProperty.Next(false);
// Copy the value over.
newProperty.floatValue = oldProperty.floatValue;

var keywords = keywordProperties.stringValue.Split(' ').ToList();
keywords.Remove($"{oldName.ToUpper()}_ON");
if (newProperty.floatValue == 1)
{
keywords.Add($"{newName.ToUpper()}_ON");
}
keywordProperties.stringValue = string.Join(" ", keywords);
}
}
}

11 changes: 11 additions & 0 deletions crest/Assets/Crest/Crest/Scripts/Editor/MaterialHelper.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 73 additions & 0 deletions crest/Assets/Crest/Crest/Scripts/Editor/MaterialUpgrader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Crest Ocean System

// This file is subject to the MIT License as seen in the root of this folder structure (LICENSE)

namespace Crest.Editor
{
using UnityEditor;
using UnityEngine;

public class MaterialUpgrader
{
static bool DisplayMaterialUpgradeDialogue() => EditorUtility.DisplayDialog
(
"Upgrade Crest Materials?",
"Some property names have changed since the last version of Crest. They will need upgrading.",
"Upgrade Crest Materials",
"Leave Crest Materials Alone"
);

[MenuItem("Edit/Crest/Upgrade Materials")]
[InitializeOnLoadMethod]
static void UpgradeMaterials()
{
if (MaterialHelper.RenamedKeywords.Count == 0)
{
return;
}

var wasUpgradeChosen = false;

AssetDatabase.StartAssetEditing();
try
{
// TODO: Might have to restrict to assets found in user editable locations (like Assets).
// Process all materials.
foreach (var guid in AssetDatabase.FindAssets("t:Material"))
{
var material = AssetDatabase.LoadAssetAtPath<Material>(AssetDatabase.GUIDToAssetPath(guid));
if (material.shader.name == "Crest/Ocean")
{
// Get all of the properties to query and manipulate.
var serializedObject = new SerializedObject(material);
var floatProperties = serializedObject.FindProperty("m_SavedProperties.m_Floats");
var keywordProperties = serializedObject.FindProperty("m_ShaderKeywords");

if (MaterialHelper.ContainsRenamedKeyword(floatProperties))
{
if (wasUpgradeChosen || DisplayMaterialUpgradeDialogue())
{
wasUpgradeChosen = true;
MaterialHelper.MigrateKeywords(material, serializedObject, floatProperties, keywordProperties);
}
else
{
return;
}
}
}
}

if (wasUpgradeChosen)
{
AssetDatabase.SaveAssets();
}
}
finally
{
AssetDatabase.StopAssetEditing();
}
}
}
}

11 changes: 11 additions & 0 deletions crest/Assets/Crest/Crest/Scripts/Editor/MaterialUpgrader.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions crest/Assets/Crest/Crest/Scripts/Editor/OceanShaderGUI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Crest Ocean System

// This file is subject to the MIT License as seen in the root of this folder structure (LICENSE)

namespace Crest.Editor
{
using UnityEngine;
using UnityEditor;

public class OceanShaderGUI : ShaderGUI
{
public override void OnGUI(MaterialEditor editor, MaterialProperty[] properties)
{
base.OnGUI(editor, properties);
MaterialHelper.MigrateKeywordsGUI((Material)editor.target, editor.serializedObject);
}
}
}
11 changes: 11 additions & 0 deletions crest/Assets/Crest/Crest/Scripts/Editor/OceanShaderGUI.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crest/Assets/Crest/Crest/Scripts/Helpers/UnderwaterEffect.cs
Original file line number Diff line number Diff line change
@@ -236,8 +236,8 @@ public partial class UnderwaterEffect : IValidated
"_SUBSURFACESCATTERING_ON",
"_SUBSURFACESHALLOWCOLOUR_ON",
"_TRANSPARENCY_ON",
"_CAUSTICS_ON",
"_SHADOWS_ON",
"CREST_CAUSTICS_ON",
"CREST_SHADOWS_ON",
};

public bool Validate(OceanRenderer ocean, ValidatedHelper.ShowMessage showMessage)
2 changes: 1 addition & 1 deletion crest/Assets/Crest/Crest/Scripts/LodData/LodDataMgrFlow.cs
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ public class LodDataMgrFlow : LodDataMgr
protected override GraphicsFormat RequestedTextureFormat => GraphicsFormat.R16G16_SFloat;
protected override bool NeedToReadWriteTextureData { get { return false; } }

internal const string MATERIAL_KEYWORD = "_FLOW_ON";
internal const string MATERIAL_KEYWORD = "CREST_FLOW_ON";
internal const string ERROR_MATERIAL_KEYWORD_MISSING = "Flow must be enabled on the ocean material. Tick the <i>Enable</i> option in the <i>Flow</i> parameter section on the material currently assigned to the OceanRenderer component.";
internal const string ERROR_MATERIAL_KEYWORD_ON_FEATURE_OFF = "The Flow feature is disabled on the this but is enabled on the ocean material. If this is not intentional, either enable the <i>Create Flow Data</i> option on this component to turn it on, or disable the Flow feature on the ocean material to save performance.";
bool _targetsClear = false;
3 changes: 1 addition & 2 deletions crest/Assets/Crest/Crest/Scripts/LodData/LodDataMgrFoam.cs
Original file line number Diff line number Diff line change
@@ -18,8 +18,7 @@ public class LodDataMgrFoam : LodDataMgrPersistent
protected override int krnl_ShaderSim { get { return _shader.FindKernel(ShaderSim); } }
public override string SimName { get { return "Foam"; } }
protected override GraphicsFormat RequestedTextureFormat => Settings._renderTextureGraphicsFormat;

internal const string MATERIAL_KEYWORD = "_FOAM_ON";
internal const string MATERIAL_KEYWORD = "CREST_FOAM_ON";
internal const string ERROR_MATERIAL_KEYWORD_MISSING = "Foam must be enabled on the ocean material. Tick the <i>Enable</i> option in the <i>Foam</i> parameter section on the material currently assigned to the OceanRenderer component.";
internal const string ERROR_MATERIAL_KEYWORD_ON_FEATURE_OFF = "The Foam feature is disabled on this component but is enabled on the ocean material. If this is not intentional, either enable the <i>Create Foam Sim</i> option on this component to turn it on, or disable the Foam feature on the ocean material to save performance.";

Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ public class LodDataMgrShadow : LodDataMgr
protected override GraphicsFormat RequestedTextureFormat => GraphicsFormat.R8G8_UNorm;
protected override bool NeedToReadWriteTextureData { get { return true; } }

internal const string MATERIAL_KEYWORD = "_SHADOWS_ON";
internal const string MATERIAL_KEYWORD = "CREST_SHADOWS_ON";
internal const string ERROR_MATERIAL_KEYWORD_MISSING = "Shadowing must be enabled on the ocean material. Tick the <i>Shadowing</i> option in the <i>Scattering</i> parameter section on the material currently assigned to the OceanRenderer component.";
internal const string ERROR_MATERIAL_KEYWORD_ON_FEATURE_OFF = "The shadow feature is disabled on this component but is enabled on the ocean material. If this is not intentional, either enable the <i>Create Shadow Data</i> option on this component to turn it on, or disable the Shadowing feature on the ocean material to save performance.";

Loading