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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Create cloth / Skinned-Mesh entirely through code

Discussion in 'Scripting' started by DragonCoder, Dec 27, 2015.

  1. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,475
    Hello Comunity :)

    The project I've mentioned here a while ago is in the middle of the devolpement:
    http://forum.unity3d.com/threads/air-resistance-with-push-back-supported.338212/

    In fact I'm at the point of creating some eyecandy already (mostly because I may send in a video by next week).
    However, my code is instantiating many prefabs made of the mesh-renderer and a rigid body to form the bones for each of the wings.
    Now I need a way to actually show a wing membrane between those moving and shifting rigid bodies.
    First I thought of using simple 3D polygons.
    From what I've seen, I need to use entire meshes for them and modify the coordinates in every step (as attaching the points to the rigid bodies is not possible, right?).

    However, Unity seems to support an amazing engine for the so called "Cloth" that would actually be perfect for creating a beautiful wing-membrane which swings as if there were air.

    Now unfortunately I'm getting to my limits with coding. Since the rigid bodies the cloth needs to be attached to, are being reated by code at runtime, the cloth cannot be created in the editor.

    I barely know how to even start creating cloth in code. Does anyone happen to have a simple example (just two rigid bodies and creating a plane of cloth between them when starting) or something like that?
    Sadly I haven't found anything.

    Would be really great if someone could push me in the right direction :)

    Huge thanks in advance!
    Greetings - and Merry, late Christmas to everyone who celebrate(d) ^_^
     
  2. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,475
    Hope I'm not annoying with this doublepost... But does nobody have a suggestion? :(
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    I don't think you want to use Cloth for this. If I understand you correctly, you just want a wing mesh that deforms according to whatever the bones in it are doing.

    So, do exactly that: rig a mesh with bones, and just hook the bones up to your simulation. (In this context a bone is just a transform that is applied to a subset of the vertices in a mesh.)
     
  4. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,475
    Thank you very much or the answer!
    Interesting! I was not knowing about those bones in meshes yet (having used only the standard shapes so far).
    Yes, that would probably work for a flat wing membrane betweens the rigid bodies which make up the real bones of the wing.
    However, wouldn't a cloth look better?
    Mean it would result in the memberane to stretch and swing up and down with some inertia. That woldn't work with bones alone, would it?
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    A skinned mesh (i.e. mesh with bones) will certainly stretch, and it'll have as much inertia as the bones provide.

    My intuition is that the cloth approach is the way of pain, but by all means, give it a whirl if you can. (But no, I don't have any code handy for creating a cloth from code — I only played with it once or twice, years ago.)
     
  6. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,475
    Again thanks for answering but I have to bug here around again :(

    Well, I am trying the the Skinned Mesh way now but the results are not really.. visible yet. I cannot find any kind of example doing that in code. It always requires the use of external programs to model the meshes.
    However, that's not working in my case because everything where the mesh should be inbetween, is already being generated by code.

    Has nobody here around ever tried to do something like that before?
    I only need a stretchable plane between two rigid bodies.. nothing more...
     
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    In the approach I was suggesting, you *would* use an external program to model the mesh. You don't need to make up the wings at runtime; you only need to deform them, right? So, model some wings, rigged with as many bones as you like, and load it into Unity. Then all you have to do is shift/rotate the bones, and the mesh will deform accordingly.

    If you're on a Mac, I highly recommend Cheetah3D for this sort of thing. If you're not that fortunate, you can subject yourself to Blender. Or maybe you already have another modeling tool you're used to; there are many that would work for this.

    If you really want to generate your mesh at runtime, Unity has a section about it in the manual. But that's certainly not the approach I would recommend in this case.
     
  8. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,475
    Unfortunately not.
    Please give a quick look at my other thread which I have linked up there.
    My whole project resolves around an algorithm which will create and test thousands of different wing shapes and sizes. All data related to the wing is generated at runtime.
    So I do have to create this part from code too.

    Thanks for the suggestion of that part of the manual. Didn't come across that yet. Maybe it helps.
     
  9. kietus

    kietus

    Joined:
    Jun 4, 2013
    Posts:
    54
    Hello,

    Just to add info, i like that article about mesh creation :
    jayelinda.com/modelling-by-numbers-part-1a/
    Depending on number and size, mesh deformation at runtime will hurt performances. that seems complicated, i wish you good luck : )
     
  10. ChristmasEve

    ChristmasEve

    Joined:
    Apr 21, 2015
    Posts:
    44
    DragonCoder, have you found a solution to this yet? I can see that in January of this year, you were working with procedural skinned meshes but your results were not "visible" yet. That is exactly what I'm running into. I can set the verts, triangles, bones and their weights, materials, etc (and I calculated normals), but, in the end, the resulting GameObject with my skinned mesh is invisible! I did notice that it thinks my bounds are 0 x 0 x 0 and it's not recalculating to incorporate my sharedMesh. I'm not sure if that's the reason I can't see anything. I really need to be able to do this; I'm completely stuck. Maybe we can help each other?
    Thanks in advance :)
     
  11. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,475
    Oh sorry, I haven't looked in here for a while unfortunately :(

    Sadly I haven't found a solution for all that. Luckily it was not a crucial part for the project I was working on at that time.
    Hope you have been successful yourself :/
     
  12. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,496
  13. _Keith

    _Keith

    Joined:
    Nov 7, 2015
    Posts:
    27
    You can definitely create a mesh in code, and it looks like you can edit the Cloth constraints through code.

    However, I do not think you can change the mesh used by cloth at runtime.

    I've been trying to figure out how to get Cloth to work with a code generated mesh set on a Skinned Mesh Renderer, all it seems to do is crash the editor.

    Even trying to simply add a Cloth component will crash the editor.

    gameObject.AddComponent<Cloth>();

    You also get this error if you use don't set a mesh: "Cannot initialize Cloth: Unable to extract collision mesh"