diff --git a/Runtime/Scripts/BaseMeshSync.Textures.cs b/Runtime/Scripts/BaseMeshSync.Textures.cs index 4a8dd63fb..342a542c4 100644 --- a/Runtime/Scripts/BaseMeshSync.Textures.cs +++ b/Runtime/Scripts/BaseMeshSync.Textures.cs @@ -118,15 +118,15 @@ private bool SetSerializedTextureForMaterial(Material mat, int textureNameID) { textureNameID == MeshSyncConstants._MaskMap) { mat.EnableKeyword(MeshSyncConstants._METALLICGLOSSMAP); mat.EnableKeyword(MeshSyncConstants._METALLICSPECGLOSSMAP); + } - TextureImporter importer = (TextureImporter)AssetImporter.GetAtPath(savePath); - if (importer != null) { - importer.sRGBTexture = false; - importer.alphaIsTransparency = false; - importer.ignorePngGamma = true; - importer.SaveAndReimport(); - AssetDatabase.Refresh(); - } + TextureImporter importer = (TextureImporter)AssetImporter.GetAtPath(savePath); + if (importer != null) { + importer.sRGBTexture = false; + importer.alphaIsTransparency = false; + importer.ignorePngGamma = true; + importer.SaveAndReimport(); + AssetDatabase.Refresh(); } return true; diff --git a/Runtime/Scripts/Extensions/MaterialExtensions.cs b/Runtime/Scripts/Extensions/MaterialExtensions.cs index 2d460ea83..8470bf010 100644 --- a/Runtime/Scripts/Extensions/MaterialExtensions.cs +++ b/Runtime/Scripts/Extensions/MaterialExtensions.cs @@ -19,6 +19,11 @@ public static void SetTextureAndReleaseExistingRenderTextures(this Material mat, existingRenderTexture.Release(); mat.SetTexture(nameID, texture); + + // Ensure main texture is set if the name is not _MainTex! + if (nameID == MeshSyncConstants._MainTex) { + mat.mainTexture = texture; + } } } } \ No newline at end of file diff --git a/Runtime/Shaders/ComputeShaderHelper.cs b/Runtime/Shaders/ComputeShaderHelper.cs index 794685887..47a953be9 100644 --- a/Runtime/Shaders/ComputeShaderHelper.cs +++ b/Runtime/Shaders/ComputeShaderHelper.cs @@ -1,5 +1,6 @@ using System; using UnityEngine; +using UnityEngine.Experimental.Rendering; namespace Unity.MeshSync { internal class ComputeShaderHelper { @@ -29,7 +30,8 @@ public RenderTexture RenderToTexture(Texture existingTexture) { renderTarget.height != maxTextureSize.y) { if (renderTarget != null) renderTarget.Release(); - renderTarget = new RenderTexture(maxTextureSize.x, maxTextureSize.y, 32) { + // We don't want sRGB here! + renderTarget = new RenderTexture(maxTextureSize.x, maxTextureSize.y, 32, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear) { enableRandomWrite = true }; diff --git a/Runtime/Shaders/MapsBaker.cs b/Runtime/Shaders/MapsBaker.cs index da08f4add..e2c2dda98 100644 --- a/Runtime/Shaders/MapsBaker.cs +++ b/Runtime/Shaders/MapsBaker.cs @@ -60,13 +60,31 @@ private static bool FindTexture(int nameID, // If there is no such texture, create one with the given fallback value: - float value = fallbackPropertyValue; - if (materialProperties.TryGetValue(fallbackPropertyNameID, out IMaterialPropertyData fallbackMaterialProperty)) - value = fallbackMaterialProperty.floatValue; + Color value = new Color( + fallbackPropertyValue, + fallbackPropertyValue, + fallbackPropertyValue, + fallbackPropertyValue); + + if (materialProperties.TryGetValue(fallbackPropertyNameID, + out IMaterialPropertyData fallbackMaterialProperty)) { + switch (fallbackMaterialProperty.type) { + case IMaterialPropertyData.Type.Float: + float v = fallbackMaterialProperty.floatValue; + value = new Color(v, v, v, v); + break; + case IMaterialPropertyData.Type.Vector: + value = fallbackMaterialProperty.vectorValue; + break; + default: + Debug.LogError($"Unsupported fallback property type: {fallbackMaterialProperty.type}!"); + break; + } + } const int dim = 8; - Color[] pixels = Enumerable.Repeat(new Color(value, value, value, value), dim * dim).ToArray(); + Color[] pixels = Enumerable.Repeat(value, dim * dim).ToArray(); disposableTexture = new Texture2DDisposable(new Texture2D(dim, dim, UnityEngine.TextureFormat.RFloat, false, true)); @@ -176,7 +194,7 @@ private static void BakeSmoothness(Material destMat, // Bake to albedo alpha channelName = MeshSyncConstants._MainTex; texturesExist |= - FindTexture(MeshSyncConstants._MainTex, textureHolders, materialProperties, 0, 0, + FindTexture(MeshSyncConstants._MainTex, textureHolders, materialProperties, MeshSyncConstants._Color, 0, out rgbTexture); } else {