0

In my shader, I calculate uv by multiplying vertex.position to some matrix. This looks fine if I dont move my object, but once I move my object or scale the object, the vertex position will change and uv will be wrong.

If there is a way for shader to store parameters, like this, so that I can only calculate uv at the first time, and then I just use that uv value.

if( ifCalculated==false)
{
   ifCalculated=true;
   uv=CalculateUV();
   temp = uv;
}
else
{
  uv = temp;
}
ywj7931
  • 135
  • 1
  • 3
  • 9

1 Answers1

0

UV coordinates are stored in mesh for each vertex. You can update them from code, not from shader:

Vector2[] CalculateUV(Vector3[] vertices);

...

if(!ifCalculated)
{
    var mesh = GetComponent<MeshFilter>().mesh;
    mesh.uv = CalculateUV(mesh.vertices);
    ifCalculated = true;
}
Azever
  • 1
  • 1
  • But I used texture2dArray, in shader, I iterate the texture2dArray and find the right uv.z( array index), it seems Mesh class doesnt have texture2dArray, the closet I find is uv,uv2,uv3,uv4 parameter, but I need more than 4 textures – ywj7931 Oct 15 '16 at 17:52