Skip to content

Commit

Permalink
Merge pull request #30 from soupday/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
soupday authored Mar 11, 2022
2 parents b61292d + 568a3c5 commit 024f19b
Show file tree
Hide file tree
Showing 16 changed files with 1,728 additions and 367 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
Changelog
=========

### v 1.2.0
- Blender Tools to Unity pipeline implemented.
- Using compute shaders for baking Unity packed textures from base Blender textures.
- Eye Occlusion and Tearline shaders updated for Blender model space.
- Diffuse Color modifier correctly converted from linear to sRGB color space.

### v 1.1.1
- Amplify Shaders added.
- Amplify Baked material shaders added.
Expand Down
7 changes: 6 additions & 1 deletion Editor/CharacterInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ public QuickJSON JsonData
return jsonData;
}
}

public void Refresh()
{
if (jsonData != null) jsonData = Util.GetJsonData(jsonPath);
}

public BaseGeneration Generation
{
Expand All @@ -178,7 +183,7 @@ public BaseGeneration Generation

return generation;
}
}
}

public void Release()
{
Expand Down
63 changes: 63 additions & 0 deletions Editor/Compute/RLBakeShader.compute
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

// Kernels
//
#pragma kernel RLDiffuseAlpha
#pragma kernel RLHDRPMask
#pragma kernel RLURPMetallicGloss
#pragma kernel RLGradient
//
#pragma kernel RLDiffuseBlend
#pragma kernel RLMask
#pragma kernel RLDetailMask
Expand Down Expand Up @@ -80,6 +85,11 @@ float sssNormalSoften;
TEX2D(Mask);
TEX2D(MetallicGloss);
TEX2D(AO);
TEX2D(Metallic);
TEX2D(Roughness);
TEX2D(Alpha);
TEX2D(MicroNormalMask);
TEX2D(SmoothnessLUT);

TEX2D(CavityAO);
float smoothnessMin, smoothnessMax, smoothnessPower;
Expand Down Expand Up @@ -346,6 +356,17 @@ float invLerp(float a, float b, float v)
return (v - a) / (b - a);
}

[numthreads(1, 1, 1)]
void RLGradient(uint3 id : SV_DispatchThreadID)
{
int w, h;
Result.GetDimensions(w, h);
//float v = (id.x / float(w - 1)) + (id.y / float(h - 1));
float v = (id.x / float(w - 1));

Result[id.xy] = float4(v, v, v, 1);
}

// Generic Bake kernels
//
[numthreads(1, 1, 1)]
Expand Down Expand Up @@ -377,6 +398,48 @@ void RLMask(uint3 id : SV_DispatchThreadID)
Result[id.xy] = packed;
}

[numthreads(1, 1, 1)]
void RLDiffuseAlpha(uint3 id : SV_DispatchThreadID)
{
float2 uv = GetUV(id.xy);

float4 diffuse = SAMPLE(Diffuse, uv);
float alpha = SAMPLE(Alpha, uv).g;
float4 packed = float4(diffuse.rgb, alpha);

Result[id.xy] = LinearTosRGB(packed);
}

[numthreads(1, 1, 1)]
void RLHDRPMask(uint3 id : SV_DispatchThreadID)
{
float2 uv = GetUV(id.xy);

float metallic = SAMPLE(Metallic, uv).g;
float ao = SAMPLE(AO, uv).g;
float roughness = SAMPLE(Roughness, uv).g;
roughness = (roughness * 255.0 / 256.0) + (0.5 / 256.0);
float smoothness = SAMPLE(SmoothnessLUT, float2(roughness, roughness)).g;
float detailMask = SAMPLE(MicroNormalMask, uv).g;
float4 packed = float4(metallic, ao, detailMask, smoothness);

Result[id.xy] = packed;
}

[numthreads(1, 1, 1)]
void RLURPMetallicGloss(uint3 id : SV_DispatchThreadID)
{
float2 uv = GetUV(id.xy);

float metallic = SAMPLE(Metallic, uv).g;
float roughness = SAMPLE(Roughness, uv).g;
roughness = (roughness * 255.0 / 256.0) + (0.5 / 256.0);
float smoothness = SAMPLE(SmoothnessLUT, float2(roughness, roughness)).g;
float4 packed = float4(metallic, metallic, metallic, smoothness);

Result[id.xy] = packed;
}

[numthreads(1, 1, 1)]
void RLNormalBlend(uint3 id : SV_DispatchThreadID)
{
Expand Down
144 changes: 132 additions & 12 deletions Editor/ComputeBake.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,23 @@ private static Vector2Int GetMaxSize(Texture2D a, Texture2D b, Texture2D c)

private static Vector2Int GetMaxSize(Texture2D a, Texture2D b, Texture2D c, Texture2D d)
{
Vector2Int max = new Vector2Int(a.width, a.height);
if (b.width > max.x) max.x = b.width;
if (b.height > max.y) max.y = b.height;
if (c.width > max.x) max.x = c.width;
if (c.height > max.y) max.y = c.height;
if (d.width > max.x) max.x = d.width;
if (d.height > max.y) max.y = d.height;
Vector2Int max = new Vector2Int(MIN_SIZE, MIN_SIZE);
if (a) max = new Vector2Int(a.width, a.height);
if (b)
{
if (b.width > max.x) max.x = b.width;
if (b.height > max.y) max.y = b.height;
}
if (c)
{
if (c.width > max.x) max.x = c.width;
if (c.height > max.y) max.y = c.height;
}
if (d)
{
if (d.width > max.x) max.x = d.width;
if (d.height > max.y) max.y = d.height;
}
if (max.x > MAX_SIZE) max.x = MAX_SIZE;
if (max.y > MAX_SIZE) max.y = MAX_SIZE;
return max;
Expand Down Expand Up @@ -230,6 +240,15 @@ private Texture2D CheckMask(Texture2D tex)
return Texture2D.whiteTexture;
}

/// <summary>
/// Checks that the texture exists, if not returns a gray texture.
/// </summary>
private Texture2D CheckGray(Texture2D tex)
{
if (tex) return tex;
return Texture2D.grayTexture;
}

/// <summary>
/// Checks that the texture exists, if not returns a blank black texture.
/// </summary>
Expand Down Expand Up @@ -1380,6 +1399,7 @@ private Material BakeEyeOcclusionMaterial(Material mat, string sourceName)
float expandLower = mat.GetFloatIf("_ExpandLower");
float expandInner = mat.GetFloatIf("_ExpandInner");
float expandOuter = mat.GetFloatIf("_ExpandOuter");
float expandScale = mat.GetFloatIf("_ExpandScale");

Texture2D bakedBaseMap = null;
Texture2D bakedMaskMap = null;
Expand All @@ -1404,15 +1424,115 @@ private Material BakeEyeOcclusionMaterial(Material mat, string sourceName)
sourceName, Pipeline.GetTemplateMaterial(MaterialType.EyeOcclusion,
MaterialQuality.Baked, characterInfo));

result.SetFloat("_ExpandOut", expandOut);
result.SetFloat("_ExpandUpper", expandUpper);
result.SetFloat("_ExpandLower", expandLower);
result.SetFloat("_ExpandInner", expandInner);
result.SetFloat("_ExpandOuter", expandOuter);
result.SetFloatIf("_ExpandOut", expandOut);
result.SetFloatIf("_ExpandUpper", expandUpper);
result.SetFloatIf("_ExpandLower", expandLower);
result.SetFloatIf("_ExpandInner", expandInner);
result.SetFloatIf("_ExpandOuter", expandOuter);
result.SetFloatIf("_ExpandScale", expandScale);

return result;
}

//

public Texture2D BakeGradientMap(string folder, string name)
{
Vector2Int maxSize = new Vector2Int(256, 256);
ComputeBakeTexture bakeTarget =
new ComputeBakeTexture(maxSize, folder, name, Importer.FLAG_ALPHA_DATA);

ComputeShader bakeShader = Util.FindComputeShader(COMPUTE_SHADER);
if (bakeShader)
{
int kernel = bakeShader.FindKernel("RLGradient");
bakeTarget.Create(bakeShader, kernel);
bakeShader.Dispatch(kernel, bakeTarget.width, bakeTarget.height, 1);
return bakeTarget.SaveAndReimport();
}

return null;
}

public Texture2D BakeBlenderDiffuseAlphaMap(Texture2D diffuse, Texture2D alpha, string folder, string name)
{
Vector2Int maxSize = GetMaxSize(diffuse, alpha);
ComputeBakeTexture bakeTarget =
new ComputeBakeTexture(maxSize, folder, name, Importer.FLAG_SRGB);

ComputeShader bakeShader = Util.FindComputeShader(COMPUTE_SHADER);
if (bakeShader)
{
diffuse = CheckDiffuse(diffuse);
alpha = CheckMask(alpha);

int kernel = bakeShader.FindKernel("RLDiffuseAlpha");
bakeTarget.Create(bakeShader, kernel);
bakeShader.SetTexture(kernel, "Diffuse", diffuse);
bakeShader.SetTexture(kernel, "Alpha", alpha);
bakeShader.Dispatch(kernel, bakeTarget.width, bakeTarget.height, 1);
return bakeTarget.SaveAndReimport();
}

return null;
}

public Texture2D BakeBlenderHDRPMaskMap(Texture2D metallic, Texture2D ao,
Texture2D microNormalMask, Texture2D roughness,
Texture2D smoothnessLUT,
string folder, string name)
{
Vector2Int maxSize = GetMaxSize(metallic, ao, roughness, microNormalMask);
ComputeBakeTexture bakeTarget =
new ComputeBakeTexture(maxSize, folder, name, Importer.FLAG_ALPHA_DATA);

ComputeShader bakeShader = Util.FindComputeShader(COMPUTE_SHADER);
if (bakeShader)
{
metallic = CheckBlank(metallic);
ao = CheckMask(ao);
roughness = CheckGray(roughness);
microNormalMask = CheckMask(microNormalMask);

int kernel = bakeShader.FindKernel("RLHDRPMask");
bakeTarget.Create(bakeShader, kernel);
bakeShader.SetTexture(kernel, "Metallic", metallic);
bakeShader.SetTexture(kernel, "AO", ao);
bakeShader.SetTexture(kernel, "Roughness", roughness);
bakeShader.SetTexture(kernel, "MicroNormalMask", microNormalMask);
bakeShader.SetTexture(kernel, "SmoothnessLUT", smoothnessLUT);
bakeShader.Dispatch(kernel, bakeTarget.width, bakeTarget.height, 1);
return bakeTarget.SaveAndReimport();
}

return null;
}

public Texture2D BakeBlenderMetallicGlossMap(Texture2D metallic, Texture2D roughness,
Texture2D smoothnessLUT,
string folder, string name)
{
Vector2Int maxSize = GetMaxSize(metallic, roughness);
ComputeBakeTexture bakeTarget =
new ComputeBakeTexture(maxSize, folder, name, Importer.FLAG_ALPHA_DATA);

ComputeShader bakeShader = Util.FindComputeShader(COMPUTE_SHADER);
if (bakeShader)
{
metallic = CheckBlank(metallic);
roughness = CheckGray(roughness);

int kernel = bakeShader.FindKernel("RLURPMetallicGloss");
bakeTarget.Create(bakeShader, kernel);
bakeShader.SetTexture(kernel, "Metallic", metallic);
bakeShader.SetTexture(kernel, "Roughness", roughness);
bakeShader.SetTexture(kernel, "SmoothnessLUT", smoothnessLUT);
bakeShader.Dispatch(kernel, bakeTarget.width, bakeTarget.height, 1);
return bakeTarget.SaveAndReimport();
}

return null;
}

// Bake Maps

Expand Down
Loading

0 comments on commit 024f19b

Please sign in to comment.