Unity3D Mesh.bindposes

//The Bind Pose Matrices allow a raw vertex of the mesh (in local coordinates) to be transformed into world-space
// and then to each bone's local coordinate space, after which each bone's animation can be applied to the vertex in question
// (under the influence of the weighting value). The mesh's bind pose takes the vertex from local space to world-space,
// and then each bone's inverse bind pose takes the mesh vertex from world-space to the local space of that bone.
// Once in the bone's local space, the bone's current animated transformation matrix is used to transform (deform)
// the vertex's location. After all the bone influences have been taken into account, the vertex ends up in world-space
// in its final deformed location. In other words, these bind pose matrices relate the location of a vertex in its local mesh space
// to the same location of the vertex relative to each bones' local coordinate systems.
 
// The mesh's Bind Pose Matrix takes its vertices into world-space, to the location at the time of binding,
// and each bones' Bind Pose Matrix takes the bones from local space to world-space at the time of binding.
 
function Start ()
{
gameObject.AddComponent(SkinnedMeshRenderer);
var renderer : SkinnedMeshRenderer = GetComponent(SkinnedMeshRenderer);
 
// Build basic mesh
var mesh : Mesh = new Mesh ();
 
mesh.vertices = [   Vector3(-1, 0, 0),
                    Vector3(1, 0, 0),
                    Vector3(-1, 5, 0),
                    Vector3(1, 5, 0)
                ];
               
mesh.uv =       [    Vector2(0, 0),
                     Vector2(1, 0),
                     Vector2(0, 1),
                     Vector2(1, 1)
                ];
           
mesh.triangles = [   0, 1, 2,
                     1, 3, 2,
                     2, 1, 0,
                     2, 3, 1
                 ];
               
mesh.RecalculateNormals();
 
// Assign mesh to mesh filter  renderer
 
renderer.material = new Material (Shader.Find(" Diffuse"));
 
// BoneWeight[4] : 4 = vertices 0 to 3
// weights[0] : first (0) vertice
// boneIndex0 : 0 = first bone
// weight0 = 1 : 1 = how much influence this bone has on the vertice
 
var weights = new BoneWeight[4];
 
weights[0].boneIndex0 = 0;
weights[0].weight0 = 1;
 
weights[1].boneIndex0 = 0;
weights[1].weight0 = 1;
 
weights[2].boneIndex0 = 0;
weights[2].weight0 = 1;
 
weights[3].boneIndex0 = 0;
weights[3].weight0 = 1;
 
mesh.boneWeights = weights;
 
// Create 1 Bone Transform and 1 Bind pose
 
var bones : Transform[] = new Transform[1];
var bindPoses : Matrix4x4[] = new Matrix4x4[1];
 
// Create a new gameObject
 
bones[0] = new GameObject ("Lower").transform;
 
// Make this gameObject's transform the parent of the bone
 
bones[0].parent = transform;
 
// Set the position/rotation of the bone
 
bones[0].localRotation = Quaternion.identity;
bones[0].localPosition = Vector3.zero;
 
// bones[0] is a Transform mapped to world space. We map it to the local space of its parent,
// which is the transform "bones[0].worldToLocalMatrix" and afterwards
// we map it again in world space, keeping the relation child - parent with "* transform.localToWorldMatrix;",
// thus allowing us 1. to move/rotate/scale it freely in space but also
//                            2. make all move/rotate/scaling operations on its parent affect it too
 
bindPoses[0] = bones[0].worldToLocalMatrix * transform.localToWorldMatrix;
 
// Apply bindPoses to the mesh
 
mesh.bindposes = bindPoses;
 
// Assign bones and bind poses to the SkinnedMeshRenderer
 
renderer.bones = bones;
renderer.sharedMesh = mesh;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值