title: How do I calculate the right UV's for this mesh?
body: <p>I am currently working on a project with UE4 and wrote a heightmap-generator and combined him with a delaunay-triangulator. Then I have to read the triangles/vertices from an output-file. But I have a problem with the UV's. Every vertex wants to get a U and a V variable defined, but I have no clue how to calculate them. Basically, the texture I put on the final mesh should look like one big texture. The only thing I got working (while just playing with the values in the range of 0-1) is that every triangle has a texture displayed, but not aligned to all the others, so it looks like a nuclear power plant-explosion.</p>
  
  <p>Here is the code where I read all vertices from two files and specify the UV's  sort of randomly.</p>
  
  <pre><code>inStream &gt;&gt; vertexCount &gt;&gt; buffer &gt;&gt; buffer; // first element is the number of elements // skip second and third
  
      TArray&lt;FProceduralMeshTriangle&gt; triangles;
      for (int i = 0; i &lt; vertexCount; i++){
          FProceduralMeshTriangle triangle;
          FProceduralMeshVertex v0, v1, v2;
          int i1, i2, i3;
          inStream &gt;&gt; buffer &gt;&gt; i1 &gt;&gt; i2 &gt;&gt; i3;
          i1--;
          i2--;
          i3--;
          v0.Position.X = nodes[i1].X;
          v0.Position.Y = nodes[i1].Y;
          v0.Position.Z = vectorPoints[nodes[i1].X][nodes[i1].Y].first;
          //v0.Position.Z = 5;
          v0.U = 0.0;
          v0.V = 0.5;
  
          v1.Position.X = nodes[i2].X;
          v1.Position.Y = nodes[i2].Y;
          v1.Position.Z = vectorPoints[nodes[i2].X][nodes[i2].Y].first;
          v1.U = 0.5;
          v1.V = 0.5;
  
          v2.Position.X = nodes[i3].X;
          v2.Position.Y = nodes[i3].Y;
          v2.Position.Z = vectorPoints[nodes[i3].X][nodes[i3].Y].first;
          v2.U = 0.0;
          v2.V = 0.0;
  
          triangle.Vertex0 = v0;
          triangle.Vertex1 = v1;
          triangle.Vertex2 = v2;
          triangles.Add(triangle);
      }
  </code></pre>
  
  <p>Thanks for your help!</p>
  
  <p>Wireframe:
  <img src="https://i.stack.imgur.com/OmMO6.png" alt="Wireframe"></p>
  
  <p>With material specified:
  <img src="https://i.stack.imgur.com/yI4FM.jpg" alt="With Material"></p>
  
  <p>Edit:</p>
  
  <p>Okay, I added the following code: </p>
  
  <pre><code>double range = (lowestX - highestX)*-1;
      double offset = 0 - lowestX;
  
      for (int i = 0; i &lt; triangles.Num(); i++){
          FProceduralMeshVertex vT;
          vT = triangles[i].Vertex0;
          vT.U = (vT.Position.X + offset) / range;
          vT.V = 1 - vT.U;
          triangles[i].Vertex0 = vT;
  
          vT = triangles[i].Vertex1;
          vT.U = (vT.Position.X + offset) / range;
          vT.V = 1 - vT.U;
          triangles[i].Vertex1 = vT;
  
          vT = triangles[i].Vertex2;
          vT.U = (vT.Position.X + offset) / range;
          vT.V = 1 - vT.U;
          triangles[i].Vertex2 = vT;
      }
  </code></pre>
  
  <p>Now it doesnt look like a nuclear accident anymore, but the texture is stretched about the whole mesh, instead of repeating and it is still slighly distorted..</p>