Skip to content

Commit 8f91852

Browse files
committed
Fix a bug to sample uv.
1 parent a134759 commit 8f91852

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

Assets/Packages/Voxelizer/Scripts/GPUVoxelizer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class GPUVoxelizer {
1313

1414
protected const string kVolumeKernelKey = "Volume", kSurfaceKernelKey = "Surface", kTextureKernelKey = "BuildTexture3D";
1515
protected const string kStartKey = "_Start", kEndKey = "_End", kSizeKey = "_Size";
16-
protected const string kUnitKey = "_Unit", kHalfUnitKey = "_HalfUnit";
16+
protected const string kUnitKey = "_Unit", kInvUnitKey = "_InvUnit", kHalfUnitKey = "_HalfUnit";
1717
protected const string kWidthKey = "_Width", kHeightKey = "_Height", kDepthKey = "_Depth";
1818
protected const string kTriCountKey = "_TrianglesCount";
1919
protected const string kVertBufferKey = "_VertBuffer", kUVBufferKey = "_UVBuffer", kTriBufferKey = "_TriBuffer";
@@ -73,6 +73,7 @@ public static GPUVoxelData Voxelize(ComputeShader voxelizer, Mesh mesh, int coun
7373
voxelizer.SetVector(kSizeKey, bounds.size);
7474

7575
voxelizer.SetFloat(kUnitKey, unit);
76+
voxelizer.SetFloat(kInvUnitKey, 1f / unit);
7677
voxelizer.SetFloat(kHalfUnitKey, unit * 0.5f);
7778
voxelizer.SetInt(kWidthKey, w);
7879
voxelizer.SetInt(kHeightKey, h);

Assets/Packages/Voxelizer/Shaders/Voxelizer.compute

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
CBUFFER_START(VoxelParams)
1111

1212
float3 _Start, _End, _Size;
13-
float _Unit, _HalfUnit;
13+
float _Unit, _InvUnit, _HalfUnit;
1414

1515
int _Width, _Height, _Depth;
1616

@@ -88,7 +88,7 @@ void voxelize_surface(int x, int y)
8888
float u, v;
8989
float dist = tri_hit(ori, dir, v0, v1, v2, u, v);
9090
if(dist >= 0.0) {
91-
int z = dist / _Unit;
91+
int z = dist * _InvUnit;
9292
uint vid = get_voxel_index(x, y, z);
9393
Voxel voxel;
9494

@@ -97,7 +97,7 @@ void voxelize_surface(int x, int y)
9797
float2 uv2 = _UVBuffer[c];
9898

9999
voxel.position = get_voxel_position(x, y, z);
100-
voxel.uv = lerp(uv0, uv1, u) + lerp(uv0, uv2, v);
100+
voxel.uv = u * uv1 + v * uv2 + (1.0 - u - v) * uv0;
101101
voxel.flag = true;
102102
_VoxelBuffer[vid] = voxel;
103103
}

0 commit comments

Comments
 (0)