Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by unity_slcKZ-2Fhcmm0Q · Dec 21, 2020 at 06:33 PM · scripting problemmeshscripting beginnerarduino

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.
         }
     }



Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Llama_w_2Ls · Dec 21, 2020 at 09:20 PM 0
Share

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

avatar image unity_slcKZ-2Fhcmm0Q · Dec 29, 2020 at 11:10 PM 0
Share

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.

avatar image Llama_w_2Ls unity_slcKZ-2Fhcmm0Q · Dec 30, 2020 at 10:29 AM 0
Share

Wow! That makes your life a lot easier then. See the answer below for constructing the mesh:

avatar image toddisarockstar · Jan 03, 2021 at 04:00 AM 1
Share

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

avatar image toddisarockstar · Jan 03, 2021 at 04:18 AM 0
Share

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.

avatar image unity_slcKZ-2Fhcmm0Q toddisarockstar · Jan 03, 2021 at 04:10 PM 0
Share

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.

avatar image unity_slcKZ-2Fhcmm0Q toddisarockstar · Jan 03, 2021 at 04:18 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_slcKZ-2Fhcmm0Q · Jan 02, 2021 at 08:19 PM 0
Share

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!

avatar image Llama_w_2Ls unity_slcKZ-2Fhcmm0Q · Jan 02, 2021 at 08:51 PM 0
Share

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


PS: I'm glad there are people like you in this world. Thank you so much!

This means a lot to me. Thanks

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

223 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to detect when two colliding objects create a corner? 1 Answer

Can't increase my score after OnTriggerEnter occurs. 1 Answer

Terrain and character navigation 0 Answers

How to add force if spherecast is true and how to set direction of sphere 0 Answers

Why is my job running slowly the first few frames then speeding up? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges