- Home /
How to create a mesh with coordinates given from an Arduino?
Hello guys, I would like to build a Laserscanner with Arduino, which gives me the coordinates of the scanned object. Then I need to create the Mesh/Modell for this particular object (This isn't the problem; Hardware already works fine). In the script down below you can find a simulation of this process. So my question is: How can I create a mesh/modell in Unity with those coordinates as vertices of the mesh/modell? It would be really nice if someone could help me, because this project is very important to me.
Arduino code: (sorry it's in german)
int abstand = 0;
int rotXY = 0;
int rotZ = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
for (int i = 0; i < 200; i++) { //200 = Steps für eine Umdrehung (Verdoppel und Vervierfachbar) i = Sensor Hoch und Runter
for (int j = 0; j < 200; j++) {
abstand = random(0, 1023); //0-1023 entspricht 4-30 cm j = Drehteller
rotXY = j;
rotZ = i;
String erg = "";
erg.concat(abstand);
erg.concat("|");
erg.concat(rotXY);
erg.concat("|");
erg.concat(rotZ);
Serial.println(erg);
delay(100);
}
}
delay(5000);
}
Unity code: (sorry it's in Spanish)
SerialPort stream = new SerialPort("COM3", 9600);
public string receivedstring;
public GameObject carrito;
public Vector3 rot;
public Vector3 rot2;
public string[] datos;
public string[] datos_recibidos;
void Start()
{
stream.Open(); //Open the Serial Stream.
}
void Update()
{
receivedstring = stream.ReadLine(); //Read the information
stream.BaseStream.Flush(); //Clear the serial information so we assure we get new information.
string[] datos = receivedstring.Split(','); //My arduino script returns a 3 part value (IE: 12,30,18)
if (datos[0] != "" && datos[1] != "" && datos[2] != "") //Check if all values are recieved
{
datos_recibidos [0] = datos[0];
datos_recibidos[1] = datos[1];
datos_recibidos[2] = datos[2];
//Read the information and put it in a vector3
//Take the vector3 and apply it to the object this script is applied.
stream.BaseStream.Flush(); //Clear the serial information so we assure we get new information.
}
}
Unfortunately, with just the coordinates, you can't construct a mesh. You will also need the triangle indicies which are essentially the triangles drawn to construct the mesh.
For example, to construct a square face, you need four vertices but 6 triangle indicies, which refer to the points in the order they are drawn. To draw a square face, you need to draw two triangles: one that connects the top-right most point to the bottom-right point to the top-left point, and one that connects the bottom-left to the top-left to the bottom-right points. This draws a square face. With just the points with no order, you can't reconstruct the mesh.
Do the coordinates given by the arduino give the order the points are used to draw the mesh? @unity_slcKZ-2Fhcmm0Q
First of all, thank you for your answer. I'm sorry for my late answer, I didn't have a lot of time because of Christmas and stuff. But my here my short answer: Yes it can.
Wow! That makes your life a lot easier then. See the answer below for constructing the mesh:
I love arduino and it sounds like you have a great project idea. the true answer depends on how you are scanning your object and the order your arduino is returning points. to create a mesh you need an array of vertices which is sounds like you have. Your scan is pry in a grid pattern. faces are actually rendered in triangles ... not squares. so you need another array that points to which vertices define each triangle. your triangle array will pry be bigger than your vertices array and share some indexes. ( the order of the three points in the triangle define wether to render the front or back of the face). Lastly, you need a UV array. this defines how the texture displays on each triangle. (you might be able to get a modeling program to do this part for you).
here is a good example to get you started: link text
post the output that Arduino is giving you! how are you scanning? are you spinning the object on a motor with your lasers/sensors side mounded? if you post your data output and give me an idea of what order/direction you are grabbing points I would love to help you with this! I have some time tonight and tomorrow.
Hello, thank you for your interest in my project. The problem is that I don't know the output of the Arduino. $$anonymous$$y friend is working with me and the sensor we are using is at his home. But my Inspiration came from this website: https://www.electronoobs.com/eng_arduino_tut30.php Our project is pretty similar to the project on this website. We are using another sensor but it's very similar overall.
What I have already is: -Transferring the data from Arduino to Unity -Using it and convert it from a string to a float
$$anonymous$$y friend is working on the creation of the order of the points. Another user said we need that to make the script easier.
Thanks in advance
Answer by Llama_w_2Ls · Dec 30, 2020 at 10:32 AM
You can set the mesh of a gameobject by changing the mesh property of its mesh filter. First you would have to construct the mesh using the Mesh
class, and then assign its corresponding points and order of points (i.e triangle indicies). Like so:
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class CreateMesh : MonoBehaviour
{
public Vector3[] Points;
public int[] TriangleIndicies; // order of points
private void Start()
{
// Pseudocode for getting the points and the order
// (I assume you already handled this bit yourself)
Points = Arduino.GetPoints();
TriangleIndicies = Arduino.GetTriangleIndicies();
// Set the mesh of the gameobject to the arduino-constructed mesh
GetComponent<MeshFilter>().mesh = ConstructMesh();
}
public Mesh ConstructMesh()
{
Mesh mesh = new Mesh
{
vertices = Points,
triangles = TriangleIndicies
};
return mesh;
}
}
This can be put on any empty gameobject and should automatically add a mesh filter, mesh renderer, and draw the mesh given by the arduino data. Hope this works! @unity_slcKZ-2Fhcmm0Q
Hello @Llama_w_2Ls, Thank you for helping me out. But I have another question: The vertices have to be an array of those points but in my code, they are just simple coordinates. This is why I get the error: " Error CS0029 Cannot implicitly convert type 'UnityEngine.Vector3' to 'UnityEngine.Vector3[]' ". So how do I fix that? In other words: How can I "convert" those coordinates to an Array.
Thanks in advance
PS: I'm glad there are people like you in this world. Thank you so much!
You don't really need to use an array. You could use a list and for each set of coordinates you have, you can add it to the list. __Note: The 'List' type only exists in the namespace System.Collections.Generic, so make sure to add using System.Collections.Generic;
at the top of your script.
private List<Vector3> Coordinates = new List<Vector3>();
private void Start()
{
// Gets the coordinate from the arduino (Pseudocode)
Vector3 coordinate = Arduino.GetCoordinate();
Coordinates.Add(coordinate);
}
And then where you create the mesh, you can convert your list of coordinates into a Vector3 array, by writing Coordinates.ToArray()
, which returns an array of Vector3's. Hope you solve it from here. @unity_slcKZ-2Fhcmm0Q
This means a lot to me. ThanksPS: I'm glad there are people like you in this world. Thank you so much!