294
weights
squared
distance
.
er than
two-ste
p
structur
e
mesh. T
followi
n
1. Loo
p
2. Fin
d
3. Rec
u
tanc
e
and
4. Tri
m
the
l
5. Nor
m
wei
g
Figure 1
7
sented w
e
inset). B
r
b
one, wh
are given re
l
distance
b
ec
a
.
) In some ca
s
n
, which is n
o
p
process en
s
e
, and thus ar
e
o recapitulat
e
n
g steps:
p
through all
d
the closest
b
u
rsively find
e
threshold
fo
add them to
t
m
the list to o
n
l
ist might ha
v
m
alize the b
o
g
ht for that v
e
7
.5. Weight d
i
e
ight distributi
r
ighter colors
ereas complet
e
l
ative to thei
r
a
use they giv
e
s
es, the num
b
o
rmal if fewe
r
s
ures that sp
a
e
physiologic
e
, the autom
a
the vertices.
b
one for each
all the bone
for
that verte
x
t
he assignme
n
n
ly keep the
v
e fewer than
o
ne’s distanc
e
e
rtex.
i
stribution for
a
on is for the r
i
on the mesh
e
ly dark parts
o
17.Plac
e
r
distance. (I
t
e
b
etter visu
a
b
er of bones
w
r
bones are l
o
a
tially close
b
ally unrelate
d
a
tic weight
a
vertex.
s parents an
d
x
. Only expl
o
nt
list.
n closest bo
n
n entries.
e
between ze
r
a
nontrivial c
a
i
ght femoral b
o
surface indic
a
o
f the surface
r
e
holdersbey
o
t
is advisabl
e
a
l results tha
n
w
ithin the th
r
o
cated near t
h
b
ones that a
r
d
, do not sha
r
a
ssignment a
l
d children t
h
o
re those tha
t
n
es to the ve
r
r
o and one a
n
a
se in an anim
a
o
ne of the skel
a
te a greater i
n
r
epresent no in
o
ndStaticAr
t
e
to use the
c
n
the direct E
r
eshold migh
t
h
e vertex. T
h
r
e far in the
r
e weights all
l
gori
t
hm req
u
h
at are below
t
haven’t bee
n
r
tex. Don’t f
o
n
d use it as t
h
a
tion skeleton.
l
eton (highligh
t
n
fluence of t
h
fluence.
t
Replaceme
n
c
ubed or
uclidean
t
be low-
h
is whole
skeletal
over the
u
ires the
the dis-
n
visited
o
rget that
h
e bone’s
The p
r
e-
ted in the
h
e middle
n
t
17.2PreachingbyExample:TheArticulatedPlaceholderModel 295
The automatic weight assignment algorithm is illustrated in Listing 17.1. Af-
ter the algorithm has been run on the mesh, the vertex weight assignment is com-
plete. The obtained result is a weight map for our articulated placeholder. The
weight map is far from perfect but entirely sufficient for a placeholder asset. As a
reference, Figure 17.5 shows the weight distribution for the vertices affected by a
given bone.
map<float, BoneAssignement> VertexAssignement(vec3 vertex,
const vector<Bone *>& bones, float threshold,
int maxSupportedBones)
{
map<int, float> assignationMap;
float nearestDist = 0.0F;
// We start by finding the nearest bone. The NearestDist argument
// is returned by the FindNearestBone function and returns the
// nearest squared distance.
Bone *nearestBone = FindNearestBone(bones, vertex, nearestDist);
AssignationMap[NearestDist].bone = nearestBone;
AssignationMap[NearestDist].dist = nearestDist;
nearestBone->SetVisited(true);
// We recursively search through the nearest bone's parents
// and children.
AssignRecur(vertex, nearestBone, threshold, assignationMap);
// We trim the obtained list to maxSupportedBones and normalize
// the squared distances.
assignationMap.trim(maxSupportedBones);
float distSum = 0.0F;
for (map<int, float>::iterator it = assignationMap.begin();
it != assignationMap.end(); ++it)
{
distSum += it->Dist;
}
296 17.PlaceholdersbeyondStaticArtReplacement
for (map<int, float>::iterator it = assignationMap.begin();
it != assignationMap.end(); ++it)
{
it->dist /= distSum;
}
return (assignationMap);
}
AssignRecur(vec3 vertex, const Bone *bone, float threshold,
map<float, BoneAssignement>& assignationMap)
{
// Go through all the children of the bone and get those that
// haven't been visited and are lower than the threshold.
for (int i = 0; i < bone->ChildrenCount(); ++i)
{
float dist = distance(bone->Child[i], vertex);
if (!bone->Child[i]->Visited() && dist < threshold)
{
assignationMap[dist].bone = bone->Child[i];
assignationMap[dist].dist = dist;
bone->Child[i]->SetVisited(true);
AssignRecur(vertex, bone->Child[i], threshold,
assignationMap);
}
}
float dist = distance(bone->Parent(), vertex);
if (!bone->Parent()->Visited() && dist < threshold)
{
assignationMap[dist].bone = bone->Parent();
assignationMap[dist].dist = dist;
bone->Parent()->SetVisited(true);
AssignRecur(vertex, bone->Parent(), threshold, assignationMap);
}
}
Listing 17.1. Implementation of the weight assignment algorithm.
17.2PreachingbyExample:TheArticulatedPlaceholderModel 297
Skinning
At this point in the process, we now know which bone affects a particular vertex
and to what extent it affects it. The only thing left to do is to actually grab the
bones’ matrices and apply them to our mesh’s vertices in order to transform the
mesh and animate it. The act of deforming a mesh to fit on a given skeleton’s
animation is called skinning. Multiple skinning techniques exist in the industry,
the most popular being linear-blend skinning [Kavan and Žára 2003] and spheri-
cal-blend skinning [Kavan and Žára 2005]. Both of the techniques have been im-
plemented with a programmable vertex shader in the demo code on the website,
but only the linear-blend skinning technique is explained in this section. Spheri-
cal-blend skinning requires some more advanced mathematics and could be the
subject of a whole gem by itself. However, keep in mind that if you can afford it,
spherical-blend skinning often provides better visual quality than does linear-
blend skinning. Again, also note that if you are already operating in a 3D devel-
opment environment, skinning techniques are almost certainly already available
and reusing them is preferable, as stated in our list of desired placeholder fea-
tures.
Linear-blend skinning is a very simple and widely popular technique that has
been in use since the Jurassic era of computer animation. While it has some visi-
ble rendering artifacts, it has proven to be a very efficient and robust technique
that adapts very well to a wide array of graphics hardware. The details given here
apply to programmable hardware but can be easily adapted for nonprogrammable
GPUs where the same work can be entirely performed on the CPU.
The idea behind linear-blend skinning is to linearly blend the transformation
matrices. This amounts to multiplying every bone’s matrix by its weight for a
given vertex and then summing the multiplied matrices together. The whole pro-
cess can be expressed with the equation
iijjj
j
wM
vv
, (17.8)
where
i
v
is the i-th untransformed vertex of the mesh in its bind pose,
i
v
is the
transformed vertex after it has been skinned to the skeleton,
j
M
is the transfor-
mation matrix of the j-th bone, and
ij
w
is the weight of bone j when applied to
vertex i. (In the case where only the n closest bones are kept, you can view all the
ij
w
as being set to zero except for the n closest ones.)
Implementing linear-blend skinning on programmable rendering hardware
remains equally straightforward and can be completely done in the vertex shader
stage of the pipeline. Before looking at the code, and to ensure that the imple-
298
Figure 1
here wit
h
ders hav
e
single bo
n
mentati
o
to be li
m
four in
p
The
vertex p
bone tra
n
nates ar
e
more in
p
a unifor
m
of the a
n
rendere
d
ton can
dimensi
o
for each
tary mat
r
influenc
e
ing-poi
n
weights
7.6. Bone co
u
h
a color map
o
e
four bone in
f
n
e influence.
C
o
n remains si
m
m
ited to fou
r
.
p
ractice, as s
h
standard inp
u
osition (attri
b
n
sformation
m
e
n’t required
p
uts dedicate
d
m
array of 4
n
imation ske
l
d
. The size o
f
have (40 i
s
o
nal integer
v
vertex. If th
e
r
ix identifier
s
e
on the fina
l
nt
vector attri
b
stored in this
n
t per vertex
r
o
n a humanoi
d
f
luences, the
d
C
olors in betw
e
m
ple, the nu
m
(The bone c
own in Figur
u
t values for
b
ute), the m
o
m
atrices (uni
f
if you only
d
to skinning
4
matrices t
h
l
eton’s bone
s
f
the array de
t
s
usually a
g
v
ector attribu
t
e
influencing
s
and weight
s
l
vertex posi
t
b
ute storing t
h
vector must
17.Plac
e
r
arely goes ab
o
d
mesh. The b
r
d
ark spots on t
h
e
en have two
o
m
ber of bone
s
ount affectin
g
e 17.6.)
the linear-
b
l
e
o
del-view-pr
o
f
or
m
s). (The
want to perf
o
have to be a
d
h
a
t
contains
t
s
(the
j
M
ma
t
t
ermines ho
w
g
ood numbe
t
e that encod
e
bone count
i
s
can be set t
o
t
ion. The last
h
e weight val
u
be in the sa
m
e
holdersbey
o
ove fou
r
. The
r
ighter spots n
e
h
e legs and th
e
o
r three bone i
n
s
that can aff
e
g a vertex ra
r
e
nd skinning
o
jection matr
i
normal vect
o
fo
rm linea
r
-
bl
d
ded to the s
h
t
he transfor
m
t
rices) at the
w
many bone
s
e
r). The sec
o
e
s the IDs of
t
i
s lower than
o
zero, which
input is a f
o
l
ues for the f
o
m
e order as th
e
o
ndStaticAr
t
bone count is
ear the neck a
n
e abdomen ha
v
n
fluences.
e
ct a single v
e
r
ely goes hi
g
vertex shad
e
i
x (uniform),
o
r and textur
e
l
end skinnin
g
h
ader. The fi
r
m
ation matrix
animated p
o
s
the animati
o
o
nd input is
t
he four clos
e
four, the su
p
ensures they
o
u
r
-dimensio
n
o
ur closest bo
e correspond
i
t
Replaceme
n
depicted
n
d shoul-
v
e only a
e
rtex has
g
her than
e
r are the
and the
e
coordi-
g
.) Three
r
st one is
for each
o
se to be
o
n skele-
a four-
e
st bones
p
plemen-
have no
n
al float-
nes. The
i
ng bone
n
t
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.2.240