【译】glTF教程:简单变形对象(十七)
2019-02-23
一个简单的变形对象
从2.0版开始,glTF支持网格的“变形目标”定义。变形目标存储特定网格属性的位移或差异。在运行时,这些差异可能会添加到原始网格,以不同的权重,以动画部分的网格。这通常用于角色动画中,例如,编码虚拟角色的不同面部表情。
下面是一个最小的例子,它显示了一个带有两个变形目标的网格。这里将总结这些新元素,下一节将解释变形目标的更广泛概念及其在运行时的应用。
{
"scenes":[
{
"nodes":[
0
]
}
],
"nodes":[
{
"mesh":0
}
],
"meshes":[
{
"primitives":[
{
"attributes":{
"POSITION":1
},
"targets":[
{
"POSITION":2
},
{
"POSITION":3
}
],
"indices":0
}
],
"weights":[
1.0,
0.5
]
}
],
"animations":[
{
"samplers":[
{
"input":4,
"interpolation":"LINEAR",
"output":5
}
],
"channels":[
{
"sampler":0,
"target":{
"node":0,
"path":"weights"
}
}
]
}
],
"buffers":[
{
"uri":"data:application/gltf-buffer;base64,AAABAAIAAAAAAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAAA/AAAAPwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIC/AACAPwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIA/AACAPwAAAAA=",
"byteLength":116
},
{
"uri":"data:application/gltf-buffer;base64,AAAAAAAAgD8AAABAAABAQAAAgEAAAAAAAAAAAAAAAAAAAIA/AACAPwAAgD8AAIA/AAAAAAAAAAAAAAAA",
"byteLength":60
}
],
"bufferViews":[
{
"buffer":0,
"byteOffset":0,
"byteLength":6,
"target":34963
},
{
"buffer":0,
"byteOffset":8,
"byteLength":108,
"byteStride":12,
"target":34962
},
{
"buffer":1,
"byteOffset":0,
"byteLength":20
},
{
"buffer":1,
"byteOffset":20,
"byteLength":40
}
],
"accessors":[
{
"bufferView":0,
"byteOffset":0,
"componentType":5123,
"count":3,
"type":"SCALAR",
"max":[
2
],
"min":[
0
]
},
{
"bufferView":1,
"byteOffset":0,
"componentType":5126,
"count":3,
"type":"VEC3",
"max":[
1.0,
0.5,
0.0
],
"min":[
0.0,
0.0,
0.0
]
},
{
"bufferView":1,
"byteOffset":36,
"componentType":5126,
"count":3,
"type":"VEC3",
"max":[
0.0,
1.0,
0.0
],
"min":[
-1.0,
0.0,
0.0
]
},
{
"bufferView":1,
"byteOffset":72,
"componentType":5126,
"count":3,
"type":"VEC3",
"max":[
1.0,
1.0,
0.0
],
"min":[
0.0,
0.0,
0.0
]
},
{
"bufferView":2,
"byteOffset":0,
"componentType":5126,
"count":5,
"type":"SCALAR",
"max":[
4.0
],
"min":[
0.0
]
},
{
"bufferView":3,
"byteOffset":0,
"componentType":5126,
"count":10,
"type":"SCALAR",
"max":[
1.0
],
"min":[
0.0
]
}
],
"asset":{
"version":"2.0"
}
}
该资产包含一个动画,用于在单个三角形的不同变形目标之间进行插值。此资产的屏幕截图如图21a所示。
这个资产的大部分元素已经在前几节中解释过了:它包含一个“场景”,只有一个“节点”和一个“网格”。有两个“buffer”对象,一个存储几何数据,另一个存储用于“animation”的数据,还有几个“bufferView”和“accessor”对象提供对这些数据的访问。
为了定义变形目标而添加的新元素包含在“网格”和“动画”中:
"meshes":[
{
"primitives":[
{
"attributes":{
"POSITION":1
},
"targets":[
{
"POSITION":2
},
{
"POSITION":3
}
],
"indices":0
}
],
"weights":[
0.5,
0.5
]
}
],
mesh.primitive
包含一个morph targets
。每个变形目标都是一个将属性名映射到accessor
对象的字典。在本例中,有两个变形目标,它们都将"POSITION"
属性映射到包含变形顶点位置的访问器。网格还包含一个weights
数组,它定义了每个变形目标对最终渲染网格的贡献。这些权重也是 channel.target
。资产中包含的animation
的目标:
"animations":[
{
"samplers":[
{
"input":4,
"interpolation":"LINEAR",
"output":5
}
],
"channels":[
{
"sampler":0,
"target":{
"node":0,
"path":"weights"
}
}
]
}
],
这意味着动画将修改target.node
引用的网格的weights
。将动画应用于这些权重的结果,以及最终渲染网格的计算,将在下一节中详细解释变形对象。