Skip to content

Commit d77d9b7

Browse files
committed
Alternate code for Unity 5.6.6 compatibility
1 parent 045c2f2 commit d77d9b7

7 files changed

+842
-907
lines changed

ProceduralStochasticTexturing/Editor/StandardStochasticShaderGUI.cs

+55-67
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,31 @@ public enum SmoothnessMapChannel
4343

4444
private static class Styles
4545
{
46-
public static GUIContent uvSetLabel = EditorGUIUtility.TrTextContent("UV Set");
47-
48-
public static GUIContent albedoText = EditorGUIUtility.TrTextContent("Albedo", "Albedo (RGB) and Transparency (A)");
49-
public static GUIContent alphaCutoffText = EditorGUIUtility.TrTextContent("Alpha Cutoff", "Threshold for alpha cutoff");
50-
public static GUIContent specularMapText = EditorGUIUtility.TrTextContent("Specular", "Specular (RGB) and Smoothness (A)");
51-
public static GUIContent metallicMapText = EditorGUIUtility.TrTextContent("Metallic", "Metallic (R) and Smoothness (A)");
52-
public static GUIContent smoothnessText = EditorGUIUtility.TrTextContent("Smoothness", "Smoothness value");
53-
public static GUIContent smoothnessScaleText = EditorGUIUtility.TrTextContent("Smoothness", "Smoothness scale factor");
54-
public static GUIContent smoothnessMapChannelText = EditorGUIUtility.TrTextContent("Source", "Smoothness texture and channel");
55-
public static GUIContent highlightsText = EditorGUIUtility.TrTextContent("Specular Highlights", "Specular Highlights");
56-
public static GUIContent reflectionsText = EditorGUIUtility.TrTextContent("Reflections", "Glossy Reflections");
57-
public static GUIContent normalMapText = EditorGUIUtility.TrTextContent("Normal Map", "Normal Map");
58-
public static GUIContent heightMapText = EditorGUIUtility.TrTextContent("Height Map", "Height Map (G)");
59-
public static GUIContent occlusionText = EditorGUIUtility.TrTextContent("Occlusion", "Occlusion (G)");
60-
public static GUIContent emissionText = EditorGUIUtility.TrTextContent("Color", "Emission (RGB)");
61-
public static GUIContent detailMaskText = EditorGUIUtility.TrTextContent("Detail Mask", "Mask for Secondary Maps (A)");
62-
public static GUIContent detailAlbedoText = EditorGUIUtility.TrTextContent("Detail Albedo x2", "Albedo (RGB) multiplied by 2");
63-
public static GUIContent detailNormalMapText = EditorGUIUtility.TrTextContent("Normal Map", "Normal Map");
46+
public static GUIContent uvSetLabel = new GUIContent("UV Set");
47+
48+
public static GUIContent albedoText = new GUIContent("Albedo", "Albedo (RGB) and Transparency (A)");
49+
public static GUIContent alphaCutoffText = new GUIContent("Alpha Cutoff", "Threshold for alpha cutoff");
50+
public static GUIContent specularMapText = new GUIContent("Specular", "Specular (RGB) and Smoothness (A)");
51+
public static GUIContent metallicMapText = new GUIContent("Metallic", "Metallic (R) and Smoothness (A)");
52+
public static GUIContent smoothnessText = new GUIContent("Smoothness", "Smoothness value");
53+
public static GUIContent smoothnessScaleText = new GUIContent("Smoothness", "Smoothness scale factor");
54+
public static GUIContent smoothnessMapChannelText = new GUIContent("Source", "Smoothness texture and channel");
55+
public static GUIContent highlightsText = new GUIContent("Specular Highlights", "Specular Highlights");
56+
public static GUIContent reflectionsText = new GUIContent("Reflections", "Glossy Reflections");
57+
public static GUIContent normalMapText = new GUIContent("Normal Map", "Normal Map");
58+
public static GUIContent heightMapText = new GUIContent("Height Map", "Height Map (G)");
59+
public static GUIContent occlusionText = new GUIContent("Occlusion", "Occlusion (G)");
60+
public static GUIContent emissionText = new GUIContent("Color", "Emission (RGB)");
61+
public static GUIContent detailMaskText = new GUIContent("Detail Mask", "Mask for Secondary Maps (A)");
62+
public static GUIContent detailAlbedoText = new GUIContent("Detail Albedo x2", "Albedo (RGB) multiplied by 2");
63+
public static GUIContent detailNormalMapText = new GUIContent("Normal Map", "Normal Map");
6464

6565
public static string primaryMapsText = "Main Maps";
6666
public static string secondaryMapsText = "Secondary Maps";
6767
public static string forwardText = "Forward Rendering Options";
6868
public static string renderingMode = "Rendering Mode";
6969
public static string advancedText = "Advanced Options";
70+
public static GUIContent emissiveWarning = new GUIContent("Emissive value is animated but the material has not been configured to support emissive. Please make sure the material itself has some amount of emissive.");
7071
public static readonly string[] blendNames = Enum.GetNames(typeof(BlendMode));
7172
}
7273

@@ -103,6 +104,7 @@ private static class Styles
103104

104105
MaterialEditor m_MaterialEditor;
105106
WorkflowMode m_WorkflowMode = WorkflowMode.Specular;
107+
ColorPickerHDRConfig m_ColorPickerHDRConfig = new ColorPickerHDRConfig(0f, 99f, 1 / 99f, 3f);
106108

107109
bool m_FirstTimeApply = true;
108110

@@ -169,7 +171,7 @@ public void FindProperties(MaterialProperty[] props)
169171
bumpMapDXTScalers = FindProperty("_BumpMapDXTScalers", props);
170172
detailNormalMapDXTScalers = FindProperty("_DetailNormalMapDXTScalers", props);
171173
emissionMapDXTScalers = FindProperty("_EmissionMapDXTScalers", props);
172-
174+
173175
mainTexColorSpaceOrigin = FindProperty("_MainTexColorSpaceOrigin", props);
174176
mainTexColorSpaceVector1 = FindProperty("_MainTexColorSpaceVector1", props);
175177
mainTexColorSpaceVector2 = FindProperty("_MainTexColorSpaceVector2", props);
@@ -250,7 +252,7 @@ public void ShaderPropertiesGUI(Material material)
250252
GUILayout.Label(Styles.primaryMapsText, EditorStyles.boldLabel);
251253
DoAlbedoArea(material);
252254
DoSpecularMetallicArea();
253-
DoNormalArea();
255+
m_MaterialEditor.TexturePropertySingleLine(Styles.normalMapText, bumpMap, bumpMap.textureValue != null ? bumpScale : null);
254256
m_MaterialEditor.TexturePropertySingleLine(Styles.heightMapText, heightMap, heightMap.textureValue != null ? heigtMapScale : null);
255257
m_MaterialEditor.TexturePropertySingleLine(Styles.occlusionText, occlusionMap, occlusionMap.textureValue != null ? occlusionStrength : null);
256258
m_MaterialEditor.TexturePropertySingleLine(Styles.detailMaskText, detailMask);
@@ -286,8 +288,8 @@ public void ShaderPropertiesGUI(Material material)
286288

287289
EditorGUILayout.Space();
288290

289-
// NB renderqueue editor is not shown on purpose: we want to override it based on blend mode
290291
GUILayout.Label(Styles.advancedText, EditorStyles.boldLabel);
292+
m_MaterialEditor.RenderQueueField();
291293
m_MaterialEditor.EnableInstancingField();
292294
m_MaterialEditor.DoubleSidedGIField();
293295
}
@@ -361,18 +363,6 @@ void BlendModePopup()
361363
EditorGUI.showMixedValue = false;
362364
}
363365

364-
void DoNormalArea()
365-
{
366-
m_MaterialEditor.TexturePropertySingleLine(Styles.normalMapText, bumpMap, bumpMap.textureValue != null ? bumpScale : null);
367-
if (bumpScale.floatValue != 1 && UnityEditorInternal.InternalEditorUtility.IsMobilePlatform(EditorUserBuildSettings.activeBuildTarget))
368-
if (m_MaterialEditor.HelpBoxWithButton(
369-
EditorGUIUtility.TrTextContent("Bump scale is not supported on mobile platforms"),
370-
EditorGUIUtility.TrTextContent("Fix Now")))
371-
{
372-
bumpScale.floatValue = 1;
373-
}
374-
}
375-
376366
void DoAlbedoArea(Material material)
377367
{
378368
m_MaterialEditor.TexturePropertySingleLine(Styles.albedoText, albedoMap, albedoColor);
@@ -390,7 +380,7 @@ void DoEmissionArea(Material material)
390380
bool hadEmissionTexture = emissionMap.textureValue != null;
391381

392382
// Texture and HDR color controls
393-
m_MaterialEditor.TexturePropertyWithHDRColor(Styles.emissionText, emissionMap, emissionColorForRendering, false);
383+
m_MaterialEditor.TexturePropertyWithHDRColor(Styles.emissionText, emissionMap, emissionColorForRendering, m_ColorPickerHDRConfig, false);
394384

395385
// If texture was assigned and color was black set color to white
396386
float brightness = emissionColorForRendering.colorValue.maxColorComponent;
@@ -546,15 +536,14 @@ private bool CheckIfPreprocessWillBeLong()
546536
}
547537

548538

549-
550539
/*********************************************************************/
551540
/*********************************************************************/
552541
/*************Procedural Stochastic Texturing Pre-process*************/
553542
/*********************************************************************/
554543
/*********************************************************************/
555-
const float GAUSSIAN_AVERAGE = 0.5f; // Expectation of the Gaussian distribution
556-
const float GAUSSIAN_STD = 0.16666f; // Std of the Gaussian distribution
557-
const int LUT_WIDTH = 128; // Size of the look-up table
544+
const float GAUSSIAN_AVERAGE = 0.5f; // Expectation of the Gaussian distribution
545+
const float GAUSSIAN_STD = 0.16666f; // Std of the Gaussian distribution
546+
const int LUT_WIDTH = 128; // Size of the look-up table
558547

559548
struct TextureData
560549
{
@@ -1056,7 +1045,7 @@ TextureData TextureToTextureData(Texture2D input, ref TextureFormat inputFormat)
10561045
// Copy input texture pixel data
10571046
Color[] colors = input.GetPixels();
10581047
TextureData res = new TextureData(input.width, input.height);
1059-
for(int x = 0; x < res.width; x++)
1048+
for (int x = 0; x < res.width; x++)
10601049
{
10611050
for (int y = 0; y < res.height; y++)
10621051
{
@@ -1130,10 +1119,10 @@ void SerializePrecomputedTextures(Texture2D input, ref TextureFormat inputFormat
11301119
}
11311120

11321121
private void Precomputations(
1133-
ref TextureData input, // input: example image
1134-
List<int> channels, // input: channels to process
1135-
ref TextureData Tinput, // output: T(input) image
1136-
ref TextureData invT, // output: T^{-1} look-up table
1122+
ref TextureData input, // input: example image
1123+
List<int> channels, // input: channels to process
1124+
ref TextureData Tinput, // output: T(input) image
1125+
ref TextureData invT, // output: T^{-1} look-up table
11371126
string inputName,
11381127
ref int stepCounter,
11391128
int totalSteps)
@@ -1195,7 +1184,7 @@ private void RescaleForDXTCompression(ref TextureData Tinput, ref Vector3 DXTSca
11951184
}
11961185
}
11971186
}
1198-
1187+
11991188
/*****************************************************************************/
12001189
/**************** Section 1.3.1 Target Gaussian distribution *****************/
12011190
/*****************************************************************************/
@@ -1260,7 +1249,7 @@ private float invCDF(float U, float mu, float sigma)
12601249
float x = sigma * Mathf.Sqrt(2.0f) * ErfInv(2.0f * U - 1.0f) + mu;
12611250
return x;
12621251
}
1263-
1252+
12641253
/*****************************************************************************/
12651254
/**** Section 1.3.2 Applying the histogram transformation T on the input *****/
12661255
/*****************************************************************************/
@@ -1298,7 +1287,7 @@ private void ComputeTinput(ref TextureData input, ref TextureData T_input, int c
12981287
T_input.SetColor(x, y, channel, G);
12991288
}
13001289
}
1301-
1290+
13021291
/*****************************************************************************/
13031292
/** Section 1.3.3 Precomputing the inverse histogram transformation T^{-1} ***/
13041293
/*****************************************************************************/
@@ -1329,7 +1318,7 @@ private void ComputeinvT(ref TextureData input, ref TextureData Tinv, int channe
13291318
Tinv.SetColor(i, 0, channel, I);
13301319
}
13311320
}
1332-
1321+
13331322
/*****************************************************************************/
13341323
/******** Section 1.4 Improvement: using a decorrelated color space **********/
13351324
/*****************************************************************************/
@@ -1355,7 +1344,7 @@ private void ComputeEigenVectors(ref TextureData input, Vector3[] eigenVectors)
13551344
GB += col.g * col.b;
13561345
}
13571346
}
1358-
1347+
13591348
R /= (float)(input.width * input.height);
13601349
G /= (float)(input.width * input.height);
13611350
B /= (float)(input.width * input.height);
@@ -1370,15 +1359,15 @@ private void ComputeEigenVectors(ref TextureData input, Vector3[] eigenVectors)
13701359
double[][] covarMat = new double[3][];
13711360
for (int i = 0; i < 3; i++)
13721361
covarMat[i] = new double[3];
1373-
covarMat[0][0] = RR - R* R;
1374-
covarMat[0][1] = RG - R* G;
1375-
covarMat[0][2] = RB - R* B;
1376-
covarMat[1][0] = RG - R* G;
1377-
covarMat[1][1] = GG - G* G;
1378-
covarMat[1][2] = GB - G* B;
1379-
covarMat[2][0] = RB - R* B;
1380-
covarMat[2][1] = GB - G* B;
1381-
covarMat[2][2] = BB - B* B;
1362+
covarMat[0][0] = RR - R * R;
1363+
covarMat[0][1] = RG - R * G;
1364+
covarMat[0][2] = RB - R * B;
1365+
covarMat[1][0] = RG - R * G;
1366+
covarMat[1][1] = GG - G * G;
1367+
covarMat[1][2] = GB - G * B;
1368+
covarMat[2][0] = RB - R * B;
1369+
covarMat[2][1] = GB - G * B;
1370+
covarMat[2][2] = BB - B * B;
13821371

13831372
// Find eigen values and vectors using Jacobi algorithm
13841373
double[][] eigenVectorsTemp = new double[3][];
@@ -1388,9 +1377,9 @@ private void ComputeEigenVectors(ref TextureData input, Vector3[] eigenVectors)
13881377
ComputeEigenValuesAndVectors(covarMat, eigenVectorsTemp, eigenValuesTemp);
13891378

13901379
// Set return values
1391-
eigenVectors[0] = new Vector3((float) eigenVectorsTemp[0][0], (float) eigenVectorsTemp[1][0], (float) eigenVectorsTemp[2][0]);
1392-
eigenVectors[1] = new Vector3((float) eigenVectorsTemp[0][1], (float) eigenVectorsTemp[1][1], (float) eigenVectorsTemp[2][1]);
1393-
eigenVectors[2] = new Vector3((float) eigenVectorsTemp[0][2], (float) eigenVectorsTemp[1][2], (float) eigenVectorsTemp[2][2]);
1380+
eigenVectors[0] = new Vector3((float)eigenVectorsTemp[0][0], (float)eigenVectorsTemp[1][0], (float)eigenVectorsTemp[2][0]);
1381+
eigenVectors[1] = new Vector3((float)eigenVectorsTemp[0][1], (float)eigenVectorsTemp[1][1], (float)eigenVectorsTemp[2][1]);
1382+
eigenVectors[2] = new Vector3((float)eigenVectorsTemp[0][2], (float)eigenVectorsTemp[1][2], (float)eigenVectorsTemp[2][2]);
13941383
}
13951384

13961385
// ----------------------------------------------------------------------------
@@ -1538,12 +1527,12 @@ private int ComputeEigenValuesAndVectors(double[][] A, double[][] Q, double[] w)
15381527

15391528
// Main function of Section 1.4
15401529
private void DecorrelateColorSpace(
1541-
ref TextureData input, // input: example image
1542-
ref TextureData input_decorrelated, // output: decorrelated input
1543-
ref Vector3 colorSpaceVector1, // output: color space vector1
1544-
ref Vector3 colorSpaceVector2, // output: color space vector2
1545-
ref Vector3 colorSpaceVector3, // output: color space vector3
1546-
ref Vector3 colorSpaceOrigin) // output: color space origin
1530+
ref TextureData input, // input: example image
1531+
ref TextureData input_decorrelated, // output: decorrelated input
1532+
ref Vector3 colorSpaceVector1, // output: color space vector1
1533+
ref Vector3 colorSpaceVector2, // output: color space vector2
1534+
ref Vector3 colorSpaceVector3, // output: color space vector3
1535+
ref Vector3 colorSpaceOrigin) // output: color space origin
15471536
{
15481537
// Compute the eigenvectors of the histogram
15491538
Vector3[] eigenvectors = new Vector3[3];
@@ -1752,6 +1741,5 @@ private bool InputIsSelected(int layerToCheck)
17521741
MaterialProperty emissionColorSpaceVector1 = null;
17531742
MaterialProperty emissionColorSpaceVector2 = null;
17541743
MaterialProperty emissionColorSpaceVector3 = null;
1755-
17561744
}
17571745
} // namespace UnityEditor

0 commit comments

Comments
 (0)