Skip to content

Commit

Permalink
fix: Fix for baking smoothness to albedo alpha. (#900)
Browse files Browse the repository at this point in the history
* Fix for baking smoothness to albedo alpha.

* Changing to not use HDR
  • Loading branch information
schinkowski authored Apr 5, 2023
1 parent c345cf8 commit 16b29b9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
16 changes: 8 additions & 8 deletions Runtime/Scripts/BaseMeshSync.Textures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions Runtime/Scripts/Extensions/MaterialExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
4 changes: 3 additions & 1 deletion Runtime/Shaders/ComputeShaderHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using UnityEngine;
using UnityEngine.Experimental.Rendering;

namespace Unity.MeshSync {
internal class ComputeShaderHelper {
Expand Down Expand Up @@ -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
};

Expand Down
28 changes: 23 additions & 5 deletions Runtime/Shaders/MapsBaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 16b29b9

Please sign in to comment.