Search Unity

Little Big Planet style Mesh Editing at Runtime

Discussion in 'Scripting' started by Trevir, Dec 1, 2015.

  1. Trevir

    Trevir

    Joined:
    Dec 1, 2015
    Posts:
    15
    (Solution at end of thread)

    Hello! I am ***BRAND NEW*** to Unity, and I need help making a script.

    I recently began a project and I need help. What I need is a way to create a script (javascript) that takes a mesh and basically enables you to sculpt it across the X and Y axis, but not the Z axis... This mesh would be a "square" brush shape where you can only "paint" along one axis at a time. (Controlled with mouse) Refer to this:

    Any help is greatly appreciated.
     
    Last edited: Feb 1, 2019
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Well, you're likely to get a lot more help if you use C# instead of JavaScript. JavaScript (especially Unity's almost-but-not-quite JavaScript) is... well, icky, and we don't want to get our hands icky.

    Second, I think you need to ask a more specific question. The video is showing some pretty advanced stuff; do you really need to do all of that? And since you've already found the Mesh class — which is indeed what you need to do this — what have you done with it, and where exactly are you stuck?
     
  3. Trevir

    Trevir

    Joined:
    Dec 1, 2015
    Posts:
    15
    I am ***brand new**** to it, so I don't understand where to place the triangles. If there is one script to just create a box, and I looked at it, it may help. My other idea is to create a box for the length the cursor travels along the x or y, then when the cursor moves along another axis it just creates a box for the length of that direction then combines all the objects together. The problem there is I wouldn't be able to cut away at those boxes.
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    OK, so it sounds like you really do need to do the full boolean operations shown in the video.

    The trouble is, that's hard. The proper solution involves a fair bit of math; you have to compute lots of line-line intersections (for example, finding where the eraser cursor intersects the existing geometry), and some fairly sophisticated data structures to keep track of what is attached to what. If you're having trouble seeing how to arrange the triangles to make a box, I suspect your math skills are not up to this task. Here is a reference. It would take me probably somewhere around 4 hours of concentrated work to produce; it's (unfortunately) not something I can just tell you how to do.

    So let's see. Are there other ways of approaching it?

    Maybe you don't actually need a mesh. Could you use PixelSurface instead? This would make it trivial to draw and erase as shown in the video. If you need a more sophisticated texture, you could maybe use the PixelSurface texture as a mask onto something else.
     
  5. Trevir

    Trevir

    Joined:
    Dec 1, 2015
    Posts:
    15
    I looked at that a bit. My project is in 3D, but I'm only drawing across a 2D plane. I also have another idea. Instead of spreading or painting, could I just create a cube with four draggable corners to manipulate its shape, and then add new draggable vertecies to create new shapes? Again, this would be across X and Y but not Z...
     
    Last edited: Dec 2, 2015
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yeah, the fact that you're only drawing in a 2D plane is why I think PixelSurface might be the far easier solution. (It works fine in a 3D environment, as a 2D surface of course.) Of course that's my asset, so you might reasonably suspect that I'm biased. ;)

    OK, so draggable vertices. Yes, that's easier, if you can guarantee that the user won't drag them in such a way as to make the edges cross. Once edges can cross, you need a more robust triangulation solution. I do have one of those; see this blog post. It's not exactly easy either, but it is doable.
     
  7. Trevir

    Trevir

    Joined:
    Dec 1, 2015
    Posts:
    15
    I'ts fine if they cross. It is not a huge worry, and hole support is not needed. Also, looked into it some more, and now I know how to make a mesh with a script. :)
     
    JoeStrout likes this.
  8. Trevir

    Trevir

    Joined:
    Dec 1, 2015
    Posts:
    15
    So... Ive been looking around and I found a tool called BooleanRT. It is a real time CSG tool and it looks like the closest thing to what I need. The problem is... it isn't on the asset store anymore and I don't know where to find it... Any chance you know of a real time Boolean Operations script?
     
  9. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    No, general boolean mesh operations are Hard with a capital "H". There have been a few attempts to make such a thing for Unity, but as far as I know, none of them were successfully polished up and made reliable.

    I have it on my list of possible Asset Store products to make one day, but the trouble is, it's a huge amount of work and I don't know that there is a big enough market for it to justify that kind of time.

    I'd still really love to understand why PixelSurface (or some other texture-based solution) isn't the ideal solution for your problem here. Why do you need a mesh rather than a texture? Isn't the result exactly the same, as far as the user can tell?
     
  10. Trevir

    Trevir

    Joined:
    Dec 1, 2015
    Posts:
    15
    Pixel Surface looks great, but I'm not sure if:
    A: it has depth (can draw 3D objects on the z axis, since what I'm making is 2.5D).
    B: it has collisions. (Player needs to move around and collide with it)
    C: it only supports pixel art or can do high definition textures.

    I would be ok with using it if it had A and B and supported just a texture and not a material, but it looks like it doesn't support depth. What I am trying to recreate is a game called Little Big Planet (to some basic extent). If you look up some videos you'll get the jist of it. What I'm trying to recreate is its ability to create "objects" by placing material and painting it like a brush on paper. These objects turn into physical objects that can be interacted with. Most of the recreated game is done but I still need to get this "brush drawing" thing down...
     
  11. Trevir

    Trevir

    Joined:
    Dec 1, 2015
    Posts:
    15
     
  12. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Well why didn't you say so! LBP is one of my favorite games. :D

    OK, so it's easy enough to support collisions and arbitrarily high resolution (B and C), but you're right, it doesn't do depth (A).

    HOWEVER, it does suggest a path to a possible solution. You could draw in a PixelSurface, or even just a big 2D array of bools, and then do a marching squares algorithm to find the border contours. These would be 2D lines, oriented properly with an "inside" and "outside". So then you would just extrude these in the Z direction to give them some depth, and have meshes you can render and interact with.

    Of course, if you're using physics (like LBP does), you really want to stick to convex mesh colliders for the environment as much as possible. The approach described above, on its own, wouldn't give you convex meshes in general; they could be any crazy shape. So you might want an additional processing step that breaks the 2D contours into rectangles, and then builds a mesh for each, at which point you could use a simple BoxCollider — and that's great, because box colliders are crazy fast (much better than convex meshes, even).

    I'm starting to see how all this could come together, now that I have a clearer idea of what you're trying to do. If you still feel like you're in over your head and need to bring in some hired help, PM me and I bet we can work something out.
     
  13. erich202

    erich202

    Joined:
    Sep 24, 2016
    Posts:
    38
    Any progress on this? I really want to be able to flip a mountain to carve a hole into a cube. Anyway since you're a JS guy you may find this interesting since it was converted from JS: https://github.com/karl-/pb_CSG
     
  14. Trevir

    Trevir

    Joined:
    Dec 1, 2015
    Posts:
    15
    Stumbling on this thread again 4 years later, I decided to finally find a solution for fun, and I did!
    Pairing the Clipper library for 2D boolean operations along with a C# port of the Poly2Tri library yields an incredibly robust mesh editing system like the one 14-year-old-me so desperately wanted for his LittleBigPlanet fan-game.

    Bellow is an example of what can be accomplished


    Well that took long enough! ;)
     
    JoeStrout likes this.
  15. Trevir

    Trevir

    Joined:
    Dec 1, 2015
    Posts:
    15
    one thing led to another and I've come full circle:
     
    Last edited: Feb 16, 2020
    JoeStrout likes this.