- Home /
Question by
johannesneff · Jul 11, 2020 at 10:47 AM ·
meshscripting beginnerverticestrianglesuvs
Procedural Mesh : draw labels with triangles and uvs to display a image at specific vertices
Hi - c# beginner here - Im using a text file to generate the height in an array of vertices of an procedural mesh. Now I want to draw little labels at specific points (for example at each point which has a "T" Value) - these labels should be drawn with a new set of vertices (triangles) and uvs (for both sides) so you can see the picture on each side of the rect. How can I setup the new Vertice and triangle array + the uv mapping to achieve this? Little bit confusing right now
Heres my code so far:
using System.Collections; using System.Collections.Generic; using UnityEngine;
[RequireComponent(typeof(MeshFilter))] public class TMesh01 : MonoBehaviour {
Mesh mesh;
Vector3[] vertices;
int[] triangles;
public Material Tshader;
public int xSize = 10;
public int zSize = 10;
TextAsset textFile;
// public float scale = 20f;
// Start is called before the first frame update
public void start()
{
Debug.Log("Resources.Load: ");
Debug.Log(fileName);
textFile = Resources.Load<TextAsset>("text");
Debug.Log(textFile.text);
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
CreateShape();
UpdateMesh();
}
void CreateShape()
{
Mesh mesh = GetComponent<MeshFilter>().mesh;
string basen = textFile.text;
zSize = 32;
xSize = Mathf.CeilToInt(basen.Length / 32f);
vertices = new Vector3[(xSize + 2) * (zSize + 2)];
int valueA = 1;
int valueC = 1;
int valueG = 1;
int valueT = 1;
var CubeColor = Color.gray;
int lastCharValue = 0;
for (int x = 0; x < xSize + 2; x++)
{
for (int z = 0; z < zSize + 2; z++)
{
float y = 0;
int index = x * (zSize + 2) + z;
int basenIndex = (x - 1) * zSize + (z - 1);
if (x > 0 && x < xSize + 2 && z > 0 && z < zSize + 1 && basenIndex < basen.Length)
{
char c = basen[basenIndex];
char lastChar = 'A';
if (basenIndex > 0) { lastChar = basen[basenIndex - 1]; }
if (c == 'A' && lastChar == 'A')
{
lastCharValue += valueA;
}
else if (c == 'C' && lastChar == 'C')
{
lastCharValue += valueC;
}
else if (c == 'G' && lastChar == 'G')
{
lastCharValue += valueG;
}
else if (c == 'T' && lastChar == 'T')
{
lastCharValue += valueT;
}
else
{
lastCharValue = 0;
}
y = lastCharValue;
}else{ //Debug.Log(x + " " + z);
}
//int radius = zSize / 2;
float winkel = ((x * 1.0f / xSize) * Mathf.PI * 2);
float _z = Mathf.Cos((winkel)) * (z + 30);
float _x = Mathf.Sin((winkel)) * (z + 30);
vertices[index] = new Vector3(_x, y, _z);
Vector3[] verticesT = new Vector3[]; 4 per label * 2 ?
Vector2[] uvsT = new Vector2[verticesT.Length];
triangles = new int[]; // 4 per label * 3 ?
int index = (x * (zSize + 2) + z);
verticesT[index] = new Vector3(_x + 0.1f, y, _z);
verticesT[index+1] = new Vector3(_x + 0.1f, y + 0.2f, _z);
verticesT[index+2] = new Vector3(_x - 0.1f, y + 0.2f, _z);
verticesT[index+3] = new Vector3(_x - 0.1f, y, _z);
verticesT[index+4] = new Vector3(_x + 0.1f, y, _z);
verticesT[index+5] = new Vector3(_x + 0.1f, y + 0.2f, _z);
verticesT[index+6] = new Vector3(_x - 0.1f, y + 0.2f, _z);
verticesT[index+7] = new Vector3(_x - 0.1f, y, _z);
uvsT[index] = new Vector2(1,0);
uvsT[index+1] = new Vector2(1,1);
uvsT[index+2] = new Vector2(0,1);
uvsT[index+3] = new Vector2(0,0);
uvsT[index+4] = new Vector2(1,0);
uvsT[index+5] = new Vector2(1,1);
uvsT[index+6] = new Vector2(0,1);
uvsT[index+7] = new Vector2(0,0);
//front
triangles[index + 0] = 0; //0
triangles[index + 1] = 1; //1
triangles[index + 2] = 2; //2
triangles[index + 3] = 2; //2
triangles[index + 4] = 3; //3
triangles[index + 5] = 0; //0
// back
triangles[index + 6] = 7; //7
triangles[index + 7] = 6; //6
triangles[index + 8] = 5; //5
triangles[index + 9] = 5; //5
triangles[index + 10] = 4; //4
triangles[index + 11] = 7; //7
}
}
}
void UpdateMesh()
{
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
}
}
Comment