mattatz / unity-voxel

Mesh voxelization for Unity.
MIT License
1.4k stars 151 forks source link

UV Mapping is wrong. Found part of the problem. #10

Open thecrazy opened 4 years ago

thecrazy commented 4 years ago

Been trying to fix it for hours, could use a hand figuring out what's going on.

When testing with the Unity Cube only the forward (Z+) and back (Z-) faces get textured and additionally, the texture on the forward face is inverted.

In VoxelMesh..CalculatePlane(), to fix the inverted face, I tried changing (for the forward face only): uvs.Add(voxel.uv); to uvs.Add(new Vector2(voxel.uv.x, voxel.uv.y * -1)

It fixes that face but I'm not sure its working right on a sphere. As for the Top, Bottom, Left and Right sphere they are filled with colours taken from the edge of the front and back face and I haven't made much progress to fix them.

Here is an example of what I mean. All sides should show the same texture. Also, notice the tiny rim around the texture which seems to indicate some slight scaling issue. (Notice on this image, the forward face is still inverted on the Y axis (right cube)) image

Now, if we inspect the UV generated for a sphere it gives some insight on the problem:

Here is a screenshot of what a normal UV mapping looks like for the unity sphere (no voxel) image

This is what the UV of the voxelized sphere look like. Notice how the spherical distribution seems to be there "somewhat" but all sides have their voxels pointing to the same set of uv. image

This is what the UV of the sphere looks like with the modification mentioned above. Notice how a 2nd set of faces now appears under UV Map? Looks like a bunch of faces are mapped over each other, possibly offset and some inverted. image

Some help sorting this out would be greatly appreciated!

UPDATE After looking into it a little more, the uv problem is not just an offset / inverted issue. Some are but some are just plainly miss placed. Not sure what to do about this. Any one else interested in getting this thing working?

UPDATE 2 This morning I had a WTF moment when all of a sudden the mapping was done almost perfectly! Turns out I modified GPUdemo.cs, so I could switch between CPU and GPU voxelizing on the fly and I had turned off GPU without knowing. I'm going to investigate the differences between the GPU and CPU code next.