Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

W.I.P. Rigging

edited November 2013 in Code Sharing Posts: 1,976

So, I came up with an idea for 3D rigging. Here it is:

--# Main
-- SkyRig

-- Use this function to perform your initial setup
function setup()
    print("Hello World!")

    -- all the unique vertices that make up a cube
    local vertices = {
      vec3(-0.5, -0.5,  0.5), -- Left  bottom front
      vec3( 0.5, -0.5,  0.5), -- Right bottom front
      vec3( 0.5,  0.5,  0.5), -- Right top    front
      vec3(-0.5,  0.5,  0.5), -- Left  top    front
      vec3(-0.5, -0.5, -0.5), -- Left  bottom back
      vec3( 0.5, -0.5, -0.5), -- Right bottom back
      vec3( 0.5,  0.5, -0.5), -- Right top    back
      vec3(-0.5,  0.5, -0.5), -- Left  top    back

    -- now construct a cube out of the vertices above
    local cubeverts = {
      -- Front
      vertices[1], vertices[2], vertices[3],
      vertices[1], vertices[3], vertices[4],
      -- Right
      vertices[2], vertices[6], vertices[7],
      vertices[2], vertices[7], vertices[3],
      -- Back
      vertices[6], vertices[5], vertices[8],
      vertices[6], vertices[8], vertices[7],
      -- Left
      vertices[5], vertices[1], vertices[4],
      vertices[5], vertices[4], vertices[8],
      -- Top
      vertices[4], vertices[3], vertices[7],
      vertices[4], vertices[7], vertices[8],
      -- Bottom
      vertices[5], vertices[6], vertices[2],
      vertices[5], vertices[2], vertices[1],

    -- all the unique texture positions needed
    local texvertices = { vec2(0.03,0.24),
                          vec2(0.97,0.69) }

    -- apply the texture coordinates to each triangle
    local cubetexCoords = {
      -- Front
      texvertices[1], texvertices[2], texvertices[4],
      texvertices[1], texvertices[4], texvertices[3],
      -- Right
      texvertices[1], texvertices[2], texvertices[4],
      texvertices[1], texvertices[4], texvertices[3],
      -- Back
      texvertices[1], texvertices[2], texvertices[4],
      texvertices[1], texvertices[4], texvertices[3],
      -- Left
      texvertices[1], texvertices[2], texvertices[4],
      texvertices[1], texvertices[4], texvertices[3],
      -- Top
      texvertices[1], texvertices[2], texvertices[4],
      texvertices[1], texvertices[4], texvertices[3],
      -- Bottom
      texvertices[1], texvertices[2], texvertices[4],
      texvertices[1], texvertices[4], texvertices[3],

    m = mesh()
    m.vertices = cubeverts
    m.texCoords = cubetexCoords
    m.texture = "Planet Cute:Grass Block"

    r = Rig(m, cubeverts)

function anim()
    local t1 = tween(2, r.motion, {x = 2, y = -1, z = -1.5}, tween.easing.bounceOut)
    local t2 = tween(2, r.motion, {x = -1, y = 0.5, z = 0.5}, tween.easing.quadInOut)
    local t3 = tween(2, r.motion, {x = 0, y = -0.5, z = 0}, tween.easing.bounceOut)
    local t4 = tween(2, r.motion, {x = 0, y = 0.5, z = 0}, tween.easing.bounceIn, anim)
    tween.sequence(t1, t2, t3, t4)

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness

    -- Do your drawing here
    camera(3, 3, -5, 0, 0, 0)

    -- Motion delta debug print
    --print(vec3(r.mot.x - r.lastMot.x, r.mot.y - r.lastMot.y, r.mot.z - r.lastMot.z))

    m = r:getMesh()

--# Rig
Rig = class()

function Rig:init(m, v)
    self.m = m
    self.v = v
    self.lastMot = {x = 0, y = 0, z = 0}
    self.mot = table.copy(self.lastMot)
    self.motion = table.copy(self.mot)

function Rig:update()
    self.lastMot = table.copy(self.mot)
    self.mot = table.copy(self.motion)
    --print(self.mot.x, self.lastMot.x)
    local dx, dy, dz = self.mot.x - self.lastMot.x, self.mot.y - self.lastMot.y, self.mot.z - self.lastMot.z
    for k, v in pairs(self.v) do
        self.v[k] = vec3(v.x + dx, v.y + dy, v.z + dz)
    self.m.vertices = self.v

function Rig:getMesh()
    return self.m
--# Util
function table.copy(t)
    local tbl = {}
    for k, v in pairs(t) do
        tbl[k] = v
    return tbl

(Long-press on Add New Project)

It simply changes the vertices of the mesh to move with the Tweens. The W.I.P. part: changing part of a mesh. I attempted to add "vertex sets" which just keep track of the starts and ends of a vertex's vertices, but kinda failed, as nothing moved. That's the part I need help with. Any help would be greatly appreciated. Thanks in advance!

Note: The code above actually works and moves the cube without translating or moving the camera.


  • edited November 2013 Posts: 2,820

    Is it on CC? Edit: Nope. Downloading manually the old way... CC has made my standards so high. Going back a month or two before it was created would be a disaster for me. LOL.

  • Posts: 1,976

    Hmm... Good point. I'll upload it.

    I thought, though, that CC was to be used for finished projects?

  • Posts: 355

    Right Now in my iPhone. But the code looks great =D> . Animation (tween) in 3D ? Great

  • Posts: 1,976

    Using Tweens, it changes the vertices of the mesh to move it. To make it practical, I need help with making part of the mesh edited, and part still or moving differently.

  • Interesting stuff... for a cube like in your example this is fine, the issue with rigging is if your mesh gets richer (ie lots of vertices) then pushing the vertex buffer to the gpu every frame becomes very expensive, very quickly.

    This is why I played with the idea of a vertex shader based rigging a while ago. The idea of that is you have a mesh with many vertices, and the vertices are bound to a simplified skeleton (of maybe 10 joints). That way you only have to push information of the 10 joints to the shader each frame and it can determine the vertex locations in the vertex shader.

    Also, when your mesh becomes more complex you need to think about the dependancy between joints, eg for an arm, if you move the shoulder, or the elbow, or the wrist, then all of those move the hand, and if you move all 3 they move the hand in combination.

  • Posts: 355

    @SkyTheCoder. just try it, pretty interesting how to apply tweens, and the ease with which you did. because the code looks pretty good and understandable!

    btw. @Zoyt. CC is used to place most of all longest projects. and of course finished

  • @Luismi apologies if I came across as discoraging, playing in this area is really fun and dynamically deformable 3d objects is a fun space to play. Continueing on @SkyTheCoder 's current path is a good experiment and way to learn about thinking about these kind of problems. The main thing I wanted to highlight is that modifying vertices in Lua and pushing them to the shader each frame is not a scalable solution, so what he's currently doing is a great toy and way to learn, but it's not likely to form a real solution for some of the sorts of things you would want to do in a full scale app.

    I played with a couple of things in this area and performance killed it pretty quickly when I got to a reasonable number (100+) of vertices.

  • Posts: 355

    @spacemonkey don't worry :)

Sign In or Register to comment.