0
\$\begingroup\$

I'm new to shader programming, so I decided to use Unity's shader graph for that.

I have 256x256 texture atlas(like minecraft) with 4 16x16 block textures aligned on X axis

I can successfully assign texture depending on material ID (which is stored in UV.z) to the first block, but I've big issues with trying to make this work correctly for other blocks in the row(or in the 3 rows, cause 3 dimensions).

First block gets textured correctly, but other blocks(even if they are same material) get textures from other part of the atlas. For some reason texture won't repeat itself. (I assume reason is I need to do it myself somehow)

By current optimization, Vertices and UVs are shared between blocks only if they are of the same direction and same material ID.

My shader graph:

enter image description here

Atlas:

enter image description here

As seen in the result, even though all blocks are same material_ID, only the first one gets atlas aligned properly. Others don't.

How would I make this work with other blocks too using shader graph?

PS: How UVs are assigned:

  public static Vector2 ProjectPositionToUV(Vector3 position, Direction normal)
    {
        switch (normal)
        {
            case Direction.Up:
            case Direction.Down:
                return new Vector2(position.x, position.z);
            case Direction.East:
            case Direction.West:
                return new Vector2(position.z, position.y);
            case Direction.North:
            case Direction.South:
                return new Vector2(position.x, position.y);
        }

        // Fallback for an invalid input direction.
        return Vector2.zero;
    }

And

 uv.z = data.cubeType;
\$\endgroup\$
  • \$\begingroup\$ Looks like you forgot to wrap your UVs in your shader so they cross and re-cross the same small square within your atlas. \$\endgroup\$ – DMGregory Mar 8 at 16:00
  • \$\begingroup\$ @DMGregory Thanks for reply. Does script need to pass any additional data to shader or this can be done from shader with already available vertex data? \$\endgroup\$ – Nick Mar 8 at 16:47
  • \$\begingroup\$ That's up to your implementation. \$\endgroup\$ – DMGregory Mar 8 at 17:49
1
\$\begingroup\$

Eventually, I've resolved my problem using Texture Arrays(using UV.z coordinate as material_ID).

My shader:

enter image description here

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.