C# Set UV on Triangle mesh using WorldSpace

Hello. I’ve made a C# script that generates triangle mesh with 3 Vector3 points.

and 3 points keep changing everytime the triangle mesh pops up.

triangle = new GameObject ("triangle", typeof(MeshFilter), typeof(MeshRenderer));
  triangle.transform.position = new Vector3(0,0,-0.1f);
  vertices = new Vector3[]{pointA, pointB, pointC};
  triangles = new int[] {1,0,2};
  Vector2[] uv = {new Vector2(0,0)
      ,new Vector2(1,0)
      ,new Vector2(0,1)
      };

  mesh = new Mesh();
  mesh.vertices = vertices;
  mesh.triangles = triangles;
  mesh.uv = uv;
  meshFilter = triangle.GetComponent<MeshFilter>();
  meshRenderer = triangle.GetComponent<MeshRenderer>();
  meshRenderer.material = matTriangle;
  meshFilter.mesh = mesh;

I tried to put meterial on this triangle dynamically but the texture looks twisted.

how can I get a nice flat image like it’s stuck in the background and masked with triangle? (like triangle shaped spot light)

the answers said that I should set UV in WorldSpace but I have no idea with WorldUV… :frowning:

I’ve been searching for WorldUV for weeks but nothing was like my project.

someone please be my hero… thank you for reading by the way!

43602-01.jpg
[43603-02.jpg |43603]

Use code like this and use tiling on texture:

triangle = new GameObject ("triangle", typeof(MeshFilter), typeof(MeshRenderer));
   triangle.transform.position = new Vector3(0,0,-0.1f);
   vertices = new Vector3[]{pointA, pointB, pointC};
   triangles = new int[] {1,0,2};
   Vector2[] uv = {
       new Vector2(Mathf.Repeat(pointA.x,1),Mathf.Repeat(pointA.y,1))
       ,new Vector2(Mathf.Repeat(pointB.x,1),Mathf.Repeat(pointB.y,1))
       ,new Vector2(Mathf.Repeat(pointC.x,1),Mathf.Repeat(pointC.y,1))
       };
 
   mesh = new Mesh();
   mesh.vertices = vertices;
   mesh.triangles = triangles;
   mesh.uv = uv;
   meshFilter = triangle.GetComponent<MeshFilter>();
   meshRenderer = triangle.GetComponent<MeshRenderer>();
   meshRenderer.material = matTriangle;
   meshFilter.mesh = mesh;

UVs are given in a scale from 0 to 1.

(0,0) is the bottom left of your texture, (1, 1) the top right. if you pass in higher values, it depends on what wrap mode you use. if you clamp you will get black (i think? dont quote me on that), if you wrap the texture will start repeating (1.5 will be the same as 0.5)

now to your triangle. if you want to have it appear as if the texture would be fixed in worldspace, you first need to know the min and max values your points can be.

ill give you an example for the x coordinate. for simplicity we assume the minimum x any point can have is 0, while the max is 100.
now you get 3 random points, lets assume their xs lie on 5, 20, and 60.
since we know the max extends is 100, we only need to divide the world-position by the max extends and get a factor between 0 and 1 describing “how much” the point extends along x. conincidentially (not really id say its intentional) this factor is where the point lies on the UV.

so for the concrete xs in my example: for 5 we get 5/100=0.05, for 20 we get 0.2 and 60 is 0.6 and thats all there is to it.

and careful, its extends in worldspace, not the maximum value, nor the dimension of the texture itself. i only took 0 and 100 as values, if the minimum x any point can have is 20 and max 80, you divide by 60 instead.