-1

I created a Unity sphere and applied standard material with albedo texture. Now I'm trying to rotate mesh uvs (it looks like this is the simpliest way to rotate the texture)

Here is the code

using UnityEngine;

public class GameController : MonoBehaviour 
{
    public GameObject player;
    public float rotationValue;

    void Start () 
    {
        Mesh mesh = player.GetComponent<MeshFilter>().mesh;
        Vector2[] uvs = mesh.uv;

        mesh.uv = changeUvs(uvs);
    }

    private Vector2[] changeUvs (Vector2[] originalUvs)
    {
        for (int i = 0; i < originalUvs.Length; i++)
        {
            originalUvs[i].x = originalUvs[i].x + rotationValue;
            if (originalUvs[i].x > 1)
            {
                originalUvs[i].x = originalUvs[i].x - 1f;
            }
        }

        return originalUvs;
    }
}

This gives me this strange artifact. What am I doing wrong? Unity UV artifact

derHugo
  • 83,094
  • 9
  • 75
  • 115
Taras Kohut
  • 2,505
  • 3
  • 18
  • 42
  • Remove the condition in `changeUvs()`. Unity can support ranges outside [0,1], and what you do stretches the texture, as [0.95,1.05] will become [0.95,0.05] for example. – pleluron May 21 '17 at 16:03

1 Answers1

0

It can't be done the way you're trying to do it. Even if you go outside the [0,1] range as pleluron suggests there will be a line on the sphere where the texture interpolates from high to low, and you get the entire texture in a single band as you see now.

The way the original sphere solves the problem is by having a seam that is duplicated. One version has x 0 and the other one has x 1. You don't see it because the vertices are at the same location. If you want to solve the problem with uv trickery then the only option is to move the seam, which involves creating a new mesh.

Actually the simplest way to rotate the planet is by leaving the texture alone and just rotate the object! If this for some reason is not an option then go into the material and find the tiling and offset. If you're using the standard shader then make sure you use the top one, just below the emission checkbox. If you modify that X you get the effect you're trying to create with the script you posted.

Tubeliar
  • 836
  • 7
  • 20