Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Deform Script (early stage)

Discussion in 'Made With Unity' started by •BTM, Jun 20, 2010.

  1. •BTM

    •BTM

    Joined:
    Jun 6, 2010
    Posts:
    108
    The script is available at the end of this post. Attach it to whatever object you want to be deformable.

    Until the release of Unity 3, I've stopped any work on this project as I'll most likely change it when Unity 3 is released, to be based on the new cloth/softbody feature instead. I'm releasing this simple, completely stable script now, which doesn't have that many features but could still be useful.

    Release Notes:
    File is attached to this post. Deforming meshes is fairly fast, but updating convexhulls is not. If you want to deform a lot of simple objects, it may be better not to update collision.

    (click image for a demo, older version but works the same way)


    current features:

    min force bellow which collisions are ignored

    multiplier the deformation value is the force of the collision * this.

    deform radius the radius of the deformation from a collision point.

    max deform 0 = n/a. this sets the maximum distance from it's original position that a vertex can move.

    bounce back speed 0 = no bounce back. this will make the object's mesh go back to it's original state after a while. pretty useful for giving an illusion of softbodies.

    bounce back sleep if all verts in a mesh are within this range from their original positions, bounce back will not be called until the mesh is deformed again. This means any number of objects can have bounce back in a scene without affecting the frame rate much at all, as long as they aren't being deformed at the time.

    on collision true will mean that this object deforms when it collides.

    update collider if this is enabled, and if the object has a mesh collider, the collider will be updated along with the mesh when it is deformed. Only works with the same mesh, not proxy meshes. Works with convexhulls, but updating convexhulls seems to be fairly slow.

    update collider on bounce if this is enabled, and if the object has a mesh collider, the collider will be updated along with the mesh as it bounces back. Only works with the same mesh, not proxy meshes. Works with convexhulls, but updating convexhulls seems to be fairly slow. This was separate from update collider originally so I could test it, but can be used separately to tweak for performance or other reasons.

    on call true will mean that other scripts can tell this object to deform a certain way by sending a point and a vector to represent a collision point and force. this is what a gun or other non-collider would use to tell it to deform. when telling the object to deform from another object, use whatevertheobjectisyouwanttodeform.Deform.Deform(somepointinworld,someforceinworld);
     

    Attached Files:

  2. Alex Mat

    Alex Mat

    Joined:
    May 14, 2010
    Posts:
    177
    Great work! :wink:

    Btw, the only colliders you can use are mesh collider?
    And can you use proxy mesh colliders? (simplified version of rendered mesh)

    (A (really temporary) solution for the "chizel" is to tell that the mesh.bounds can't be bigger thant in the original, only smaller.)

    Is it going to be free?

    Looking forward to a next release.

    Regards
    - Alex
     
  3. •BTM

    •BTM

    Joined:
    Jun 6, 2010
    Posts:
    108
    The mesh of objects can be deformed as long as it has any collider. Wherever a collision occurs on the collider is where things will be deformed from. For instance, the sphere and capsule in that demo use the sphere and capsule colliders. Only the visible mesh is changed, while the collider stays the same on both. As for the cube and the cylinder, they use convex mesh colliders, and because of that I could set their colliders to update along with the rendered mesh.

    The reason I mentioned mesh colliders, is that the script can change them on collision. If you set update collider to true and dent a mesh, and if the collider uses the same mesh (regardless of if it's convex or not), it will update it using
    Code (csharp):
    1. GetComponent(MeshCollider).sharedMesh = mesh;
    The rendered mesh will be deformed, but if you want an unrelated mesh collider to be edited as well, it can't yet. I might try to add that some time.

    I don't think that would work the way I'm thinking.

    hahahaha… yes. :) I'm not a professional, and I certainly don't think I've ever written anything worth selling.

    Right now it's really simple, and most of the complex stuff I might add won't be too hard for me. I've dealt with geometry transformation a bit before (mostly a year ago when I first learned to code and wrote some sculpting tools for sketchup). The only issue is, that there aren't as many tools for editing meshes in unity (there's no triangle class, so there's no triangle.vertices, or triangle.normal, which would help a lot)
     
  4. GeneralGrant

    GeneralGrant

    Joined:
    Jun 10, 2010
    Posts:
    977
    This will look amazing when it's all complete.
     
  5. •BTM

    •BTM

    Joined:
    Jun 6, 2010
    Posts:
    108
    so… I changed the bounce back from update to fixed update, not sure if it would make any difference but it might help a tiny bit with collisions failing.

    Still, the main issue with collisions is that if a collider is deformed so that another object is now inside of it before they got a chance to collide, then the objects' collision won't be recognized. I'm not sure if I can fix this at all; it may not be possible.

    I'm pretty sure I found the reason it slowed to 3 fps if you let everything sit for a while. Basically, the bounce back will move each vert back toward it's original position after a while, using deltaTime*speed. because of this, the vector it changes by will get smaller and smaller, but will always be > 0. when you have verts being moved by vectors of length 0.0000000000000000000001 several times a second, it isn't a good thing. So, to fix it, I implemented a little cap variable. When the object is deformed, it's set to true. If all the verts are within 0.001 of their original position, it's set to false and the script won't affect them until another deformation is made. Basically, when they poof back into shape, the script stops doing anything until it deforms again. I'm going to make the variable of how far from their original pos they have to be before the cap kicks in user-defined, probably in an advanced setting category when I build the custom inspector.
     
  6. •BTM

    •BTM

    Joined:
    Jun 6, 2010
    Posts:
    108
    Okay, so after some tests it looks like Update is much faster than FixedUpdate for bounce back. I should have guessed so, it makes sense. Will update the demo again soon.

    (edit) : updated the demo.
     
  7. •BTM

    •BTM

    Joined:
    Jun 6, 2010
    Posts:
    108
    I managed to get it quite a bit faster. Right now the only part I would consider 'slow' would be updating the collision mesh, and even that's not bad. (I didn't update the demo, I'll make a new one some time.)
    This is a screencap of the unity car tutorial, with me driving through a group of 80-100 red cubes with (a bit too much) deformation and bounceback, each with 150 verts, which didn't affect the framerate at all. Because I set them to use box colliders their collisions don't match the visible mesh exactly, but it's barely noticeable.

    I'm still having trouble with mesh.triangles, they're a pain; so are submeshes. Does anyone know where I can find more info on them? The docs don't seem to have much, unless I'm overlooking something.

    Edit: What do you guys get for framerates in the demo? I usually get about 49.
     

    Attached Files:

  8. Vert

    Vert

    Joined:
    Mar 23, 2010
    Posts:
    1,093
    In the demo I got 40-60 FPS on my 1GB GT240 C2D 2.13Ghz 4GB ram pc. Fantastic work. I love how you made the deformations be able to bounce back to create a softbody. Brilliant!
     
  9. •BTM

    •BTM

    Joined:
    Jun 6, 2010
    Posts:
    108
    I've posted a version of the script without any issues, but lacking some features I've been working on (which don't work too well yet). I'm abandoning this until Unity 3 is released, because I imagine it could be much more realistic and faster using the new cloth/softbody feature, and I might as well not waste time coding something that will be of no use in a month :wink:
     

    Attached Files:

  10. GeneralGrant

    GeneralGrant

    Joined:
    Jun 10, 2010
    Posts:
    977
    JELLO!


    These would look really cool if applied to, say, a large trash can. And then when you shoot it, it will apply the decal to the point of impact and bend the trash can making it look like something hit it.
     
  11. Vinícius Sanctus

    Vinícius Sanctus

    Joined:
    Dec 14, 2009
    Posts:
    282
    Thx for this sharing. Stable, light and cool as hell.
     
  12. FreelandCJV

    FreelandCJV

    Joined:
    Nov 12, 2009
    Posts:
    221
    Would this be a suitable piece of code for softbody integration?

    EDIT: This looks amazing btw. :D
     
  13. tatelax

    tatelax

    Joined:
    Feb 4, 2010
    Posts:
    1,147
    Wow this is the best mesh deformation I have seen! Hopefully I can use it.
     
  14. theitalianjob

    theitalianjob

    Joined:
    Sep 23, 2010
    Posts:
    19
    But it is applicable to the car body? (unity car tutorial)
     
  15. •BTM

    •BTM

    Joined:
    Jun 6, 2010
    Posts:
    108
    It can't do that right now, because it only works on objects that have both the mesh and collider components in one game object. (I didn't add many features)

    Still, it wouldn't be too hard to modify it to work with a mesh you assign from another object. Just make a new variable to assign the mesh you want to deform and change some of the code around. If you wanted to deform the separate collision geometry however, you would have to modify the code quite a bit more.
     
  16. tatelax

    tatelax

    Joined:
    Feb 4, 2010
    Posts:
    1,147
  17. theitalianjob

    theitalianjob

    Joined:
    Sep 23, 2010
    Posts:
    19
    BUt if I apply the collider directly on car "body" and I remove the external collider it work?
     
  18. skylerkingdev

    skylerkingdev

    Joined:
    Sep 10, 2010
    Posts:
    36
    Could this be used on objects that are children of the main object somehow? Like if I only wanted the vehicles side mirrors to smash or front and rear bumpers? I can only seem to get it to work on objects that have no children.
     
  19. rockysam888

    rockysam888

    Joined:
    Jul 28, 2009
    Posts:
    650
    Thanks for sharing.
     
  20. newlife

    newlife

    Joined:
    Jan 20, 2010
    Posts:
    837
  21. KillerSneak

    KillerSneak

    Joined:
    Jan 9, 2011
    Posts:
    38
    This is awesome, I hope you will pickup the project soon as I would like this to work with Unity 3.1 (i tested it and it seems to work fine?) with the features you had planned.
     
  22. granada

    granada

    Joined:
    Oct 31, 2010
    Posts:
    135
    The fracture demo is great :D.

    dave
     
  23. DanjelRicci

    DanjelRicci

    Joined:
    Mar 8, 2010
    Posts:
    192
    This is extremely awesome!
    I'm trying to compile it for Xcode, but I get some errors...

    line 42 BCE0019: 'sharedMesh' is not a member of 'UnityEngine.Component'
    line 64 BCE0019: 'sharedMesh' is not a member of 'UnityEngine.Component'
    line 79 BCE0019: 'sharedMesh' is not a member of 'UnityEngine.Component'
    line 13 BCE0019: 'mesh' is not a member of 'UnityEngine.Component'
    line 18 BCE0019: 'mesh' is not a member of 'UnityEngine.Component'


    The problem is, I don't get these errors when using #pragma strict.

    Can you help me please?
     
  24. dee

    dee

    Joined:
    Jun 4, 2010
    Posts:
    36
    Cool stuff. Thanks for sharing :)
     
  25. Reignoflife

    Reignoflife

    Joined:
    Nov 7, 2010
    Posts:
    10
    Wow this is really nice. With this you could make some GTA 4 type vehicle damage. Keep up the good work.
     
  26. DanjelRicci

    DanjelRicci

    Joined:
    Mar 8, 2010
    Posts:
    192
    Never mind, I managed to solve it. Thanks!
     
  27. austint30

    austint30

    Joined:
    Jul 1, 2013
    Posts:
    70
    When I put this script into the back bumper of my car, it does not work. When I hit a tree, nothing happens! The back bumper is a child and it is a separate mesh. It has a mesh collider, a mesh filter with the bumper mesh and the script!

    Does it not work with childs?

    Here is a pic.

    $Deform Script Problem.png
     
    Last edited: Jul 24, 2013
  28. Play_Edu

    Play_Edu

    Joined:
    Jun 10, 2012
    Posts:
    719
    thanks for making this scrips.great work
     
  29. ytkimirti

    ytkimirti

    Joined:
    Mar 21, 2015
    Posts:
    6

    Did you add a rigidbody?
    You need to add your script to work "rigidbody"
     
  30. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,615
    Hello necromancer! Lol his post is about two years old now, so hopefully they figured it out by now. I admire your need to help the community though, keep it up.
     
  31. ytkimirti

    ytkimirti

    Joined:
    Mar 21, 2015
    Posts:
    6
    Sory...I didn't recognize "old post":D
     
  32. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,615
    It is all good, better to see someone posting trying to help on an old post than the mountain of trash that we see on a daily basis here. Like I said - I do appreciate that you were trying to help someone!
     
  33. Jor02

    Jor02

    Joined:
    Jan 13, 2017
    Posts:
    1
    why is this project dead???
     
unityunity