// Transforms BVHJoint according to the keyframe channel data, and recursively transforms its children private void TransformJoint(BVHJoint joint, Matrix4x4 parentTransform, float[] keyframe) { Matrix4x4 localRMatrix = Matrix4x4.identity; if (!joint.isEndSite) { Matrix4x4[] rMatrices = new Matrix4x4[3]; rMatrices[joint.rotationOrder.x] = MatrixUtils.RotateX(keyframe[joint.rotationChannels.x]); rMatrices[joint.rotationOrder.y] = MatrixUtils.RotateY(keyframe[joint.rotationChannels.y]); rMatrices[joint.rotationOrder.z] = MatrixUtils.RotateZ(keyframe[joint.rotationChannels.z]); localRMatrix = rMatrices[0] * rMatrices[1] * rMatrices[2]; } Matrix4x4 localTMatrix = MatrixUtils.Translate(joint.offset); Matrix4x4 localTRS = localTMatrix * localRMatrix; Matrix4x4 globalTransform = parentTransform * localTRS; MatrixUtils.ApplyTransform(joint.gameObject, globalTransform); foreach (BVHJoint childJoint in joint.children) { TransformJoint(childJoint, globalTransform, keyframe); } }
// Creates a GameObject representing a given BVHJoint and recursively creates GameObjects for it's child joints GameObject CreateJoint(BVHJoint joint, Vector3 parentPosition) { joint.gameObject = new GameObject(); joint.gameObject.name = joint.name; var translateMatrix = MatrixUtils.Translate(parentPosition + joint.offset); if (joint == data.rootJoint) { Debug.Log(translateMatrix + " " + joint.offset); } var scaleFactor = joint.name == "Head" ? new Vector3(8, 8, 8) : new Vector3(2, 2, 2); var scalingMatrix = MatrixUtils.Scale(scaleFactor); var newSphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); newSphere.transform.parent = joint.gameObject.transform; MatrixUtils.ApplyTransform(newSphere, scalingMatrix); MatrixUtils.ApplyTransform(joint.gameObject, translateMatrix); foreach (var child in joint.children) { var childJoint = CreateJoint(child, joint.gameObject.transform.position); var cylinder = CreateCylinderBetweenPoints(childJoint.gameObject.transform.position, joint.gameObject.transform.position, .5f); cylinder.transform.parent = joint.gameObject.transform; } return(joint.gameObject); }
// Transforms BVHJoint according to the keyframe channel data, and recursively transforms its children private void TransformJoint(BVHJoint joint, Matrix4x4 parentTransform, float[] keyframe) { Matrix4x4 t, m; if (joint == data.rootJoint) { Vector3 position = new Vector3(keyframe[joint.positionChannels[0]], keyframe[joint.positionChannels[1]], keyframe[joint.positionChannels[2]]); t = MatrixUtils.Translate(position); } else { t = MatrixUtils.Translate(joint.offset); } if (!joint.isEndSite) { Matrix4x4[] r_array = new Matrix4x4[3]; r_array[joint.rotationOrder[0]] = MatrixUtils.RotateX(keyframe[joint.rotationChannels[0]]); r_array[joint.rotationOrder[1]] = MatrixUtils.RotateY(keyframe[joint.rotationChannels[1]]); r_array[joint.rotationOrder[2]] = MatrixUtils.RotateZ(keyframe[joint.rotationChannels[2]]); Matrix4x4 r = r_array[0] * r_array[1] * r_array[2]; m = parentTransform * t * r; } else { m = parentTransform * t; } MatrixUtils.ApplyTransform(joint.gameObject, m); foreach (BVHJoint child in joint.children) { TransformJoint(child, m, keyframe); } }
// Creates a GameObject representing a given BVHJoint and recursively creates GameObjects for it's child joints GameObject CreateJoint(BVHJoint joint, Vector3 parentPosition) { joint.gameObject = new GameObject(joint.name); GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere.transform.parent = joint.gameObject.transform; Vector3 scale; if (joint.name == "Head") { scale = new Vector3(8, 8, 8); } else { scale = new Vector3(2, 2, 2); } MatrixUtils.ApplyTransform(sphere, MatrixUtils.Scale(scale)); Matrix4x4 t = MatrixUtils.Translate(parentPosition + joint.offset); MatrixUtils.ApplyTransform(joint.gameObject, t); foreach (BVHJoint child in joint.children) { GameObject childObject = CreateJoint(child, joint.gameObject.transform.position); GameObject bone = CreateCylinderBetweenPoints(joint.gameObject.transform.position, childObject.transform.position, 0.5f); bone.transform.parent = joint.gameObject.transform; } return(joint.gameObject); }
// Creates a Cylinder GameObject between two given points in 3D space GameObject CreateCylinderBetweenPoints(Vector3 p1, Vector3 p2, float diameter) { GameObject cylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder); Matrix4x4 T = MatrixUtils.Translate((p1 + p2) / 2); Matrix4x4 R = RotateTowardsVector(p2 - p1); Matrix4x4 S = MatrixUtils.Scale(new Vector3(diameter, Vector3.Distance(p1, p2) / 2, diameter)); MatrixUtils.ApplyTransform(cylinder, T * R * S); return(cylinder); }
// Creates a Cylinder GameObject between two given points in 3D space GameObject CreateCylinderBetweenPoints(Vector3 p1, Vector3 p2, float diameter) { // Your code here GameObject cylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder); Matrix4x4 t = MatrixUtils.Translate(p1 + (p2 - p1) / 2); Vector3 direction = p2 - p1; Matrix4x4 r = RotateTowardsVector(direction); float height = Vector3.Distance(p1, p2) / 2; Matrix4x4 s = MatrixUtils.Scale(new Vector3(diameter, height, diameter)); MatrixUtils.ApplyTransform(cylinder, t * r * s); return(cylinder); }
// Creates a Cylinder GameObject between two given points in 3D space GameObject CreateCylinderBetweenPoints(Vector3 p1, Vector3 p2, float diameter) { // Your code here var cylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder); var height = Vector3.Distance(p1, p2); // Calculate Scale, Rotation and Translation matrices var S = MatrixUtils.Scale(new Vector3(diameter, height / 2, diameter)); var R = RotateTowardsVector(p2 - p1); var T = MatrixUtils.Translate((p2 + p1) / 2); MatrixUtils.ApplyTransform(cylinder, T * R * S); return(cylinder); }
// Creates a Cylinder GameObject between two given points in 3D space GameObject CreateCylinderBetweenPoints(Vector3 p1, Vector3 p2, float diameter) { GameObject cylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder); Matrix4x4 t = MatrixUtils.Translate(Vector3.Lerp(p1, p2, 0.5f)); Matrix4x4 r = RotateTowardsVector(p2 - p1); float y_size = (p2 - p1).magnitude / 2; Vector3 scale = new Vector3(diameter, y_size, diameter); Matrix4x4 s = MatrixUtils.Scale(scale); MatrixUtils.ApplyTransform(cylinder, t * r * s); return(cylinder); }
// Transforms BVHJoint according to the keyframe channel data, and recursively transforms its children private void TransformJoint(BVHJoint joint, Matrix4x4 parentTransform, float[] keyframe) { Matrix4x4[] order = new Matrix4x4[3]; order[joint.rotationOrder.x] = MatrixUtils.RotateX(keyframe[joint.rotationChannels.x]);; order[joint.rotationOrder.y] = MatrixUtils.RotateY(keyframe[joint.rotationChannels.y]); order[joint.rotationOrder.z] = MatrixUtils.RotateZ(keyframe[joint.rotationChannels.z]); Matrix4x4 R = order[0] * order[1] * order[2]; Matrix4x4 T = MatrixUtils.Translate(joint.offset); Matrix4x4 M = parentTransform * T * R; MatrixUtils.ApplyTransform(joint.gameObject, M); foreach (BVHJoint child in joint.children) { TransformJoint(child, M, keyframe); } }
// Transforms BVHJoint according to the keyframe channel data, and recursively transforms its children private void TransformJoint(BVHJoint joint, Matrix4x4 parentTransform, float[] keyframe) { var S = MatrixUtils.Scale(Vector3.one); var rootPos = new Vector3(keyframe[joint.positionChannels.x], keyframe[joint.positionChannels.y], keyframe[joint.positionChannels.z]); var T = joint != data.rootJoint ? MatrixUtils.Translate(joint.offset) : MatrixUtils.Translate(rootPos); var R = !joint.isEndSite ? getRMatrix(joint, keyframe) : Matrix4x4.identity; var M = T * R * S; var newTransform = parentTransform * M; MatrixUtils.ApplyTransform(joint.gameObject, newTransform); foreach (var child in joint.children) { TransformJoint(child, newTransform, keyframe); } }
// Creates a GameObject representing a given BVHJoint and recursively creates GameObjects for it's child joints GameObject CreateJoint(BVHJoint joint, Vector3 parentPosition) { joint.gameObject = new GameObject(joint.name); GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); Vector3 scalingVec = (joint.name == "Head") ? new Vector3(8, 8, 8) : new Vector3(2, 2, 2); Matrix4x4 s = MatrixUtils.Scale(scalingVec); MatrixUtils.ApplyTransform(sphere, s); Matrix4x4 t = MatrixUtils.Translate(parentPosition + joint.offset); sphere.gameObject.transform.parent = joint.gameObject.transform; MatrixUtils.ApplyTransform(joint.gameObject, t); foreach (BVHJoint childJoint in joint.children) { GameObject createdChildJoint = CreateJoint(childJoint, joint.gameObject.transform.position); GameObject cylinder = CreateCylinderBetweenPoints(sphere.transform.position, createdChildJoint.transform.position, 1); cylinder.transform.parent = joint.gameObject.transform; } return(joint.gameObject); }