Skip to content

Commit

Permalink
Fix distance node with non-pot textures
Browse files Browse the repository at this point in the history
  • Loading branch information
alelievr committed Jan 29, 2025
1 parent 0c12da8 commit 5d93b71
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ protected override bool ProcessNode(CommandBuffer cmd)
UpdateTempRenderTexture(ref output);

cmd.SetComputeFloatParam(computeShader, "_Threshold", threshold);
cmd.SetComputeVectorParam(computeShader, "_Size", new Vector4(output.width, 1.0f / output.width));
cmd.SetComputeVectorParam(computeShader, "_Size", new Vector4(output.width, output.height, output.volumeDepth));
cmd.SetComputeFloatParam(computeShader, "_Distance", distance / 100.0f);
cmd.SetComputeIntParam(computeShader, "_ThresholdMode", (int)thresholdMode);
cmd.SetComputeIntParam(computeShader, "_DistanceMode", (int)distanceMode);
Expand All @@ -128,7 +128,7 @@ protected override bool ProcessNode(CommandBuffer cmd)
MixtureUtils.SetTextureWithDimension(cmd, computeShader, fillUvKernel, "_Output", output);
MixtureUtils.SetTextureWithDimension(cmd, computeShader, fillUvKernel, "_FinalOutput", rt);
cmd.SetComputeIntParam(computeShader, "_DistanceMode", (int)distanceMode);
cmd.SetComputeFloatParam(computeShader, "_InputScaleFactor", (float)input.width / (float)output.width);
cmd.SetComputeVectorParam(computeShader, "_InputScaleFactor", new Vector3(input.width / (float)output.width, input.height / (float)output.height, TextureUtils.GetSliceCount(input) / (float)TextureUtils.GetSliceCount(output)));
DispatchCompute(cmd, fillUvKernel, output.width, output.height, output.volumeDepth);

int maxLevels = (int)Mathf.Log(input.width, 2);
Expand All @@ -144,7 +144,8 @@ protected override bool ProcessNode(CommandBuffer cmd)
TextureUtils.CopyTexture(cmd, rt, output);
}

cmd.SetComputeFloatParam(computeShader, "_InputScaleFactor", (float)input.width / (float)output.width);

cmd.SetComputeVectorParam(computeShader, "_InputScaleFactor", new Vector3(input.width / (float)output.width, input.height / (float)output.height, TextureUtils.GetSliceCount(input) / (float)TextureUtils.GetSliceCount(output)));
cmd.SetComputeIntParam(computeShader, "_DistanceMode", (int)distanceMode);
MixtureUtils.SetTextureWithDimension(cmd, computeShader, finalPassKernel, "_Input", input);
MixtureUtils.SetTextureWithDimension(cmd, computeShader, finalPassKernel, "_Output", rt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
#include "Packages/com.alelievr.mixture/Runtime/Shaders/MixtureComputeUtils.hlsl"

float2 _Size;
float _InputScaleFactor;
float3 _Size;
float3 _InputScaleFactor;

TEXTURE_X(_Input);
RWTEXTURE_X(float4, _Output);
Expand Down Expand Up @@ -48,7 +48,7 @@ void FillUVMap(uint3 id : SV_DispatchThreadID)
float4 input = LoadInput(id);

if (PassThreshold(input))
WriteOutput(id, float4(id / _Size.x, 1));
WriteOutput(id, float4(id / _Size, 1));
else
WriteOutput(id, 0); // mark UV as invalid with w = 0

Expand Down Expand Up @@ -130,17 +130,17 @@ void JumpFlooding(uint3 id : SV_DispatchThreadID)
float3 o = id + (Is2D() ? float3(offset2D[i], 0) * _Offset : offset3D[i] * _Offset);

// Make the fetch repeat:
o %= _Size.x;
o %= _Size;
o.x += (o.x < 0) ? _Size.x : 0;
o.y += (o.y < 0) ? _Size.x : 0;
o.z += (o.z < 0) ? _Size.x : 0;
o.y += (o.y < 0) ? _Size.y : 0;
o.z += (o.z < 0) ? _Size.z : 0;
float4 n1 = LoadOutput(o);

// Discard invalid samples
if (n1.w < 0.5)
continue;

float3 uv = id / _Size.x;
float3 uv = id / _Size;
if (Distance(uv - n1.xyz) < Distance(uv - nearest.xyz))
nearest = n1;
}
Expand All @@ -152,7 +152,7 @@ void JumpFlooding(uint3 id : SV_DispatchThreadID)
void FinalPass(uint3 id : SV_DispatchThreadID)
{
float4 input = LoadInput(id);
float3 defaultUV = id / _Size.x;
float3 defaultUV = id / _Size;
float3 uv = defaultUV;
float fadeFactor = 1;
float4 uvValue = LoadOutput(id);
Expand Down
2 changes: 1 addition & 1 deletion ProjectSettings/PackageManagerSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ MonoBehaviour:
m_SeeAllPackageVersions: 0
m_DismissPreviewPackagesInUse: 0
oneTimeWarningShown: 1
oneTimeDeprecatedPopUpShown: 0
oneTimeDeprecatedPopUpShown: 1
m_Registries:
- m_Id: main
m_Name:
Expand Down

0 comments on commit 5d93b71

Please sign in to comment.