Skip to content

Commit 16b29b9

Browse files
authored
fix: Fix for baking smoothness to albedo alpha. (#900)
* Fix for baking smoothness to albedo alpha. * Changing to not use HDR
1 parent c345cf8 commit 16b29b9

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

Runtime/Scripts/BaseMeshSync.Textures.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,15 @@ private bool SetSerializedTextureForMaterial(Material mat, int textureNameID) {
118118
textureNameID == MeshSyncConstants._MaskMap) {
119119
mat.EnableKeyword(MeshSyncConstants._METALLICGLOSSMAP);
120120
mat.EnableKeyword(MeshSyncConstants._METALLICSPECGLOSSMAP);
121+
}
121122

122-
TextureImporter importer = (TextureImporter)AssetImporter.GetAtPath(savePath);
123-
if (importer != null) {
124-
importer.sRGBTexture = false;
125-
importer.alphaIsTransparency = false;
126-
importer.ignorePngGamma = true;
127-
importer.SaveAndReimport();
128-
AssetDatabase.Refresh();
129-
}
123+
TextureImporter importer = (TextureImporter)AssetImporter.GetAtPath(savePath);
124+
if (importer != null) {
125+
importer.sRGBTexture = false;
126+
importer.alphaIsTransparency = false;
127+
importer.ignorePngGamma = true;
128+
importer.SaveAndReimport();
129+
AssetDatabase.Refresh();
130130
}
131131

132132
return true;

Runtime/Scripts/Extensions/MaterialExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public static void SetTextureAndReleaseExistingRenderTextures(this Material mat,
1919
existingRenderTexture.Release();
2020

2121
mat.SetTexture(nameID, texture);
22+
23+
// Ensure main texture is set if the name is not _MainTex!
24+
if (nameID == MeshSyncConstants._MainTex) {
25+
mat.mainTexture = texture;
26+
}
2227
}
2328
}
2429
}

Runtime/Shaders/ComputeShaderHelper.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using UnityEngine;
3+
using UnityEngine.Experimental.Rendering;
34

45
namespace Unity.MeshSync {
56
internal class ComputeShaderHelper {
@@ -29,7 +30,8 @@ public RenderTexture RenderToTexture(Texture existingTexture) {
2930
renderTarget.height != maxTextureSize.y) {
3031
if (renderTarget != null) renderTarget.Release();
3132

32-
renderTarget = new RenderTexture(maxTextureSize.x, maxTextureSize.y, 32) {
33+
// We don't want sRGB here!
34+
renderTarget = new RenderTexture(maxTextureSize.x, maxTextureSize.y, 32, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear) {
3335
enableRandomWrite = true
3436
};
3537

Runtime/Shaders/MapsBaker.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,31 @@ private static bool FindTexture(int nameID,
6060

6161
// If there is no such texture, create one with the given fallback value:
6262

63-
float value = fallbackPropertyValue;
64-
if (materialProperties.TryGetValue(fallbackPropertyNameID, out IMaterialPropertyData fallbackMaterialProperty))
65-
value = fallbackMaterialProperty.floatValue;
63+
Color value = new Color(
64+
fallbackPropertyValue,
65+
fallbackPropertyValue,
66+
fallbackPropertyValue,
67+
fallbackPropertyValue);
68+
69+
if (materialProperties.TryGetValue(fallbackPropertyNameID,
70+
out IMaterialPropertyData fallbackMaterialProperty)) {
71+
switch (fallbackMaterialProperty.type) {
72+
case IMaterialPropertyData.Type.Float:
73+
float v = fallbackMaterialProperty.floatValue;
74+
value = new Color(v, v, v, v);
75+
break;
76+
case IMaterialPropertyData.Type.Vector:
77+
value = fallbackMaterialProperty.vectorValue;
78+
break;
79+
default:
80+
Debug.LogError($"Unsupported fallback property type: {fallbackMaterialProperty.type}!");
81+
break;
82+
}
83+
}
6684

6785
const int dim = 8;
6886

69-
Color[] pixels = Enumerable.Repeat(new Color(value, value, value, value), dim * dim).ToArray();
87+
Color[] pixels = Enumerable.Repeat(value, dim * dim).ToArray();
7088

7189
disposableTexture =
7290
new Texture2DDisposable(new Texture2D(dim, dim, UnityEngine.TextureFormat.RFloat, false, true));
@@ -176,7 +194,7 @@ private static void BakeSmoothness(Material destMat,
176194
// Bake to albedo alpha
177195
channelName = MeshSyncConstants._MainTex;
178196
texturesExist |=
179-
FindTexture(MeshSyncConstants._MainTex, textureHolders, materialProperties, 0, 0,
197+
FindTexture(MeshSyncConstants._MainTex, textureHolders, materialProperties, MeshSyncConstants._Color, 0,
180198
out rgbTexture);
181199
}
182200
else {

0 commit comments

Comments
 (0)