# W.I.P. Rigging

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.24),
vec2(0.03,0.69),
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"
m:setColors(color(255))

r = Rig(m, cubeverts)
anim()
end

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)
end

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

-- This sets the line thickness
strokeWidth(5)

camera(3, 3, -5, 0, 0, 0)
perspective(45)
r:update()

-- 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()
m:draw()
end

--# 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)
end

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)
end
self.m.vertices = self.v
end

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

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.

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.

Hmm… Good point. I’ll upload it.

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

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

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.