Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Generate Mesh

Discussion in 'Scripting' started by Deivydas4, Aug 16, 2023.

  1. Deivydas4

    Deivydas4

    Joined:
    Oct 28, 2021
    Posts:
    16
    Is it possible to generate a 3D shape from text file with coordinates. Like the shape could be anything from cube to cylinder and etc. But the question is if I could generate a non-standard 3D shape in Unity?

    Thank you for your answers.
     
  2. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,453
    Yes, look at the example here at the bottom:
    https://docs.unity3d.com/ScriptReference/Mesh.html

    Note however that you will need to learn a few things about how the list of vertices and list of triangles are corelated. For example the order of the vertices listed in the triangles list matters because GPUs only draw one "face" of a triangle by default.

    There is also an advanced mesh API (mentioned on the same page) which supports the burst compiler. That is only worth pursuing if you need to change vertices very often at runtime and not just for loading, unless maybe for meshes that have hundred thousands of vertices.
     
    Deivydas4 likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,494
    You mean a custom or standardized text based mesh format? When you write or find a loader for that format, yes, no problem. Over here I shared my own mesh format which was just a json file. The question over there was about extruding a 2d sprite. However at the bottom of my answer I also shared my custom file loader as well as the "mesh" file. I just wanted to include the links to the relevant files. However the forum suddenly popped up an "access denied" and I couldn't post this answer. So I had to remove my links.

    (If the forum rejects posts with 4-5 links, my IMGUI crash course would have been impossible as it contained over 55 links... )
     
    Deivydas4 likes this.
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,494
    In case you meant some sort of "custom scene file" which could just instantiate from a preselected pool of shapes, that's of course also possible, but you have to provide that pool of shapes. If you want to support some kind of parametric shapes you have to implement them yourself. For example a parametric box or sphere may be defined by just the center, orientation and a Vector3 to specify the scale / size in each dimension. A sphere may also want some kind of faces count / sector count to define the granularity of the mesh. Shapes which are not primitive shapes would need to be created as a usual triangle or quad mesh, face by face like my json mesh loader does it.

    ps: that turtle mesh was created "by hand" and was based on a grid of 16 since that's what minecraft did as well. That's why I included a matrix that scales everything down by 1/16 (==0.0625). That simplifies the specification of the vertex positions as I can use whole numbers. The same is used for texture coordinates where I use "pixel coordinates" and they are divided by the textures width / height accordingly.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,558
    Unity's built-in mesh creation stuff is SUPER easy to use.

    Don't combine the problem of reading and parsing your text data file with generating the mesh.

    They are two separate processes. Parsing something is just C# stuff. You should probably look at Bunny's format above if you need a starting point, or else look at any one of the ten billion examples for reading the text-based OBJ/MTL Lightwave format.

    I have a few dozen different examples in my MakeGeo project.

    MakeGeo is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/makegeo

    https://github.com/kurtdekker/makegeo

    https://gitlab.com/kurtdekker/makegeo

    https://sourceforge.net/p/makegeo

    Here's a super-simple "Monopoly" house exported to Lightwave OBJ file... paste it into a file named house1.obj and drop it into Unity and see for yourself.

    # Blender v2.91.0 OBJ File: ''
    # www.blender.org
    mtllib house1.mtl
    o Cube
    v 1.000000 1.000000 -1.000000
    v 1.000000 -1.000000 -1.000000
    v 1.000000 1.000000 1.000000
    v 1.000000 -1.000000 1.000000
    v -1.000000 1.000000 -1.000000
    v -1.000000 -1.000000 -1.000000
    v -1.000000 1.000000 1.000000
    v -1.000000 -1.000000 1.000000
    v 0.000000 2.000000 1.000000
    v 0.000000 2.000000 -1.000000
    vt 0.750000 0.500000
    vt 0.875000 0.500000
    vt 0.875000 0.750000
    vt 0.750000 0.750000
    vt 0.375000 0.750000
    vt 0.625000 0.750000
    vt 0.625000 0.875000
    vt 0.625000 1.000000
    vt 0.375000 1.000000
    vt 0.375000 0.000000
    vt 0.625000 0.000000
    vt 0.625000 0.250000
    vt 0.375000 0.250000
    vt 0.125000 0.500000
    vt 0.375000 0.500000
    vt 0.125000 0.750000
    vt 0.625000 0.500000
    vt 0.625000 0.375000
    vn -0.7071 0.7071 0.0000
    vn 0.0000 0.0000 1.0000
    vn -1.0000 0.0000 0.0000
    vn 0.0000 -1.0000 0.0000
    vn 1.0000 0.0000 0.0000
    vn 0.0000 0.0000 -1.0000
    vn 0.7071 0.7071 0.0000
    usemtl Material
    s off
    f 10/1/1 5/2/1 7/3/1 9/4/1
    f 4/5/2 3/6/2 9/7/2 7/8/2 8/9/2
    f 8/10/3 7/11/3 5/12/3 6/13/3
    f 6/14/4 2/15/4 4/5/4 8/16/4
    f 2/15/5 1/17/5 3/6/5 4/5/5
    f 6/13/6 5/12/6 10/18/6 1/17/6 2/15/6
    f 1/17/7 10/1/7 9/4/7 3/6/7[/code]
     
  6. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,453
    Question however is where is that mesh data from?
    I mean ideally you should use an existing format like fbx for which exporters in many languages exist as well as importers for Unity.
     
  7. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    If you just want simple shapes there is a Unity Package, it think "Mesh Maker" was it's name. It's pretty simple to use, and to setup UVs and add new materials.

    But as was mentioned, and what I do, is create the object in Blender. Blender, or other modelling programs, offer everything to your hearts content, and even wildest dreams.

    One thing I needed one time, was an inverted normal sphere. So the material showed on the inside, and not the outside, took like 1 minute in Blender(and yes I'm still a modelling noob).
     
    Kurt-Dekker likes this.
  8. Deivydas4

    Deivydas4

    Joined:
    Oct 28, 2021
    Posts:
    16
    The idea is that there will be a RC car with sensor which will send coordinates and corrosion level to me, for that I think I will use SignalR. The question was if the scanned object is non-standard is it possible to generate a shape in Unity using mesh generation.
    https://ibb.co/QKrFKLG

    This is just a simple example, I get coordinates from simple .txt. file and set vertices to sphere primitive with colors.
    So I thinking it's possible to draw a 3D shape from vertices, like in the picture.
     
  9. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,453
    A fascinating usecase!
    Note that as far as you describe this, your txt does not actually contain a full mesh information but only the vertices, aka the points.
    That means it is missing what points actually form the triangles which are needed to define the surface of the mesh!

    You will need to do some serious programming here to generate this triangle-list, depending on your constraints. This is entirely independent of Unity. Unity's only concerned once you have the triangle-list which you simply asign to the mesh.

    Will your object be guaranteed convex? In that case you can approach this with a standard hull algorithm. However as soon as you have concave objects, there is no absolute definition for what the hull of a set of points in 3D can be.
    There are algorithms that make some educated guesses.
    E.g. you might already get a good aproximation if you simply assign the nearest 2 points to each point to be a triangle and then apply some hole-filling algorithm.

    Alternatively you might want to come up with your own algorithm (or research if there's some professional solution) because you have the additional information that the order of the points matter because a robot has physically moved that route.

    P.s. this thread may be helpful, especially the "Marching cube" algorithm it mentions: https://gis.stackexchange.com/quest...oncave-hull-for-a-cloud-of-points-in-3d-space
    You might find a usable implementation of that on Github in C# or even Unity.

    P.P.s. once you do have the triangle list, you can nicely set the corrosion value as a vertex color for each vertex (mesh.colors). Pair this with a shader which does use the vertex colors (the default doesn't) and you have a nice visualization without needing to paint a texture.
     
    Last edited: Aug 18, 2023
  10. Deivydas4

    Deivydas4

    Joined:
    Oct 28, 2021
    Posts:
    16
    Thank you!
     
  11. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    That's another good point, with creating the mesh in Unity, you'd have to make sure the UVS are set right, especially if it wasn't just a solid color. Not to mention the vertices wouldn't smooth out, so it would always have sharp edges. It personally just sounds like a ton of work.

    I'm not fully sure how or what you plan to implement, but if just simple shapes with solid colors, then yup should be pretty easy. I personally made a created mesh which is basically a quad(4 tris vs 2 tris), to give a better look with a homemade terrain(sharp not smooth). And just creating the mesh is easily 100 lines, but modifying it isn't that bad, maybe 15 lines?

    I've seen a bunch of geometry games doing some crazy stuff, so if that's what you're aiming for, then all the power to ya!
     
    Deivydas4 likes this.
  12. Deivydas4

    Deivydas4

    Joined:
    Oct 28, 2021
    Posts:
    16
    Well my company works with ports, so basically it will be normal shapes, like rectangular, cylinders maybe spheres. But there is change maybe there will be non-standard shape. That's why I asked is it possible to generate a non-standard mesh. :D

    Thank you! :)
     
  13. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728