Solved Procedural mesh creation (Lua)

Discussion in 'Mod Support' started by aljowen, Nov 21, 2018.

  1. aljowen

    aljowen
    Expand Collapse

    Joined:
    Oct 21, 2012
    Messages:
    1,677
    I have read that BeamNG supports procedural mesh creation using Lua. Presumably where vertices and faces can be created manually using Lua?

    Has anyone tried this?
    Does anyone have any idea how it works? or any hints on how to get started?

    Thanks,
    aljowen
     
    #1 aljowen, Nov 21, 2018
    Last edited: Nov 21, 2018
  2. fufsgfen

    fufsgfen
    Expand Collapse

    Joined:
    Jan 10, 2017
    Messages:
    6,782
    Are you planning on doing some puzzles that require some adventuring with a vehicle?

    I would guess that it is something related to this, that creates meshes procedural way, but might not be what you are after:
    upload_2018-11-21_16-26-58.png

    How to use lua to make those happen, that sadly I have no idea, but maybe that gives idea where from files to look about it. Maybe one can even use it for other purposes?
     
  3. aljowen

    aljowen
    Expand Collapse

    Joined:
    Oct 21, 2012
    Messages:
    1,677
    So, long story short:
    On the repo, I have an adjustable spring. I am currently working on an adjustable ramp. The problem being that the ramp is so adjustable, it is not possible to simply stretch a pre made mesh onto it. So I am left with two options:
    1. Upload it as it is without a mesh, using the dubug drawing system to display the shape of it. Of course this is translucent, and showing all the beams etc.
    2. Create a Lua script that can convert all the nodes into vertices, then connect vertices to form faces. Creating a perfectly shaped solid looking mesh.

    So its not so much a case of placing objects procedurally, more a case of creating the objects mesh itself.



    I did find the following line of code:
    Code:
    -- and the collision triangles if there are any
        obj.debugDrawProxy:drawColTris(0, color(0,0,0,150), color(0,100,0,50), color(100,0,0,50), 1, color(0,0,255,255))
    However, copying that into the vehicles Lua file and changing the colour values causes and error. Which is a shame, since a 1 line solution would have been nice xD

    But changing the colours in the skeleton.lua file that it came from, does have an effect. So presumably the skeleton.lua file is overriding it or has something that makes it work there.
     
    #3 aljowen, Nov 21, 2018
    Last edited: Nov 21, 2018
  4. fufsgfen

    fufsgfen
    Expand Collapse

    Joined:
    Jan 10, 2017
    Messages:
    6,782
    If you comment out whole flexbody section including flexbody word, you can have beam and node drawing without need to enable debug drawing.
    upload_2018-11-21_16-44-55.png

    However that might not be good enough, I got quite poor performance with that for some reason, might be something I have done wrong.

    Solid color is easier than textures in terms of stretching, but if even that is not enough, then there might be quite bit of tinkering needed.


    Does it ever be as simple as single line? :D
     
  5. TStabbert

    TStabbert
    Expand Collapse
    BeamNG Team

    Joined:
    Nov 28, 2016
    Messages:
    65
    Hey aljowen,
    The Track Builder does make heavy use of the procedural mesh. To create a mesh with this, you need 4 things:
    • Vertices, as a list points looking like {{x=1, y=2, z=3}, {x=-1,y=-2,z=-3} ... }
    • UV-Mapping, as a list of texture coordinates, looking like {{u=0, v=0}, {u=1,v=1} ... }
    • Normals, as a list of normalized vectors, looking like {{x=1, y=0, z=0}, {x=0,y=1,z=0} ... }
    • Faces, as a list of vertex-normal-uv-groups, where three of them make one triangle, looking like {{v=0, n=0, u=0}, {v=1,n=1,u=1} ... }
    You can also specify a material for the mesh. One procedural mesh object can furthermore have multiple level of details (LOD), and multiple submeshes (so you can have different materials for every submesh). To get you started, I attached a script file that creates a simple cube, with comments. I find the whole of procedural meshes quite tricky to get my head around some times, but if you have further questions, feel free to ask here to write me a PM :). It helps a lot to have some paper and graph out the mesh you want to create.

    Do you already have some plans what you want to do with this?
     

    Attached Files:

    • Like Like x 3
  6. aljowen

    aljowen
    Expand Collapse

    Joined:
    Oct 21, 2012
    Messages:
    1,677
    You're an absolute legend. Just what I was looking for. Thanks :)

    Yeah, I have a fully adjustable ramp :
    upload_2018-11-21_15-1-12.png
    upload_2018-11-21_15-1-47.png
    The variables change the positions of the nodes. The issue being that stretching meshes as I have done in the past wouldn't work on this since it has multiple axis of non-linear adjustments. So I needed a way to create a mesh that fits it, since the debug mesh isn't very pretty for the end user.
     
    • Like Like x 2
  7. TStabbert

    TStabbert
    Expand Collapse
    BeamNG Team

    Joined:
    Nov 28, 2016
    Messages:
    65
    Thats a really nice idea! I was thinking of something similar for the Track Builder, it does have those static ramps already, but customizable ones would be really nice. How do you currently create this mesh? I like that you can twist the ramp as well, but i think for higher twists you could use higher resolution on the mesh.
    Also it looks like the ramp is basically a vehicle, so it can deform when you land on it? Something like this is sadly not possible with the procMesh, though you can change the groundModel of the material to something like dirt or sand, which might help, but i'm not sure.
     
    • Like Like x 2
  8. aljowen

    aljowen
    Expand Collapse

    Joined:
    Oct 21, 2012
    Messages:
    1,677
    Yeah, it is a vehicle. The aim being that the player can put it anywhere they want. The spring value can be lowered, and the damping values raised, allowing it to absorb landing impacts and so on.

    Assuming I can get the vehicles variable data (or node data) through the vehicle Lua, and assuming vehicle Lua can create a mesh for the vehicle, it should work right?


    --- Post updated ---
    I can get variable data out of the vehicle Lua system by using
    Code:
    dump(v.data.variables)
    So I know I can get all the data needed to create the mesh, it is simply a matter of whether making meshes works I guess.
     
    • Like Like x 1
  9. TStabbert

    TStabbert
    Expand Collapse
    BeamNG Team

    Joined:
    Nov 28, 2016
    Messages:
    65
    Theoretically yes, but practically it might me quite tricky. Some pointers:
    • The bare minimum you need is the vertices (preferably ordered in some way which is known to you).
    • Normals can be calculated from that, using some vector math (cross product), but it would be nice if you got them from the vehicle too (if thats possible? Im not really into vehicle coding)
    • If you dont care so much about UV, you can simply set all u/v coordinates to 0/0, for testing.
    • If the structure of the mesh is always the same (number of vertices, how the faces are constructed etc.), it will be easier to simple hard-code the faces list.

    Although, if possible, i suggest you dont go through the vehicle lua, but simply code it all with lua. Aim for a function similar to the cube function, where you can put in the parameters for the height, width, etc. I mean, you probably have the math/procedure to create the vertices somewhere already, right?
     
    • Like Like x 1
    • Informative Informative x 1
  10. aljowen

    aljowen
    Expand Collapse

    Joined:
    Oct 21, 2012
    Messages:
    1,677
    For anyone interested:

    Turns out the reason "obj.debugDrawProxy:drawColTris" wasn't working, is indeed because the game draws over it. If you try running it on updateGFX, you can kinda get the effect, sorta :p

    Warning: Strobing



    If only life was that simple xD
    --- Post updated ---




    Solution (kinda)

    Ok, so I have essentially cheesed it, and its not perfect, but it works.



    By assigning the vehicle a flexbody that doesn't exist, the game stops trying to draw the debug mesh. Hence it no longer overrides my debug mesh rendering in a colour of my choosing.


    In theory, performance should be no worse than drawing the debug mesh already was.

    So I am calling that a win
     
    • Like Like x 2
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice