Let’s finish up the series by adding hands from the VR template into our project and hooking them up to the player pawn that we have created. This way we can even look into the setting up the input from the Oculus controllers in the next tutorial.
Importing the VR Hand Mesh and Animation:
The first thing that we will need to add to the project would be the skinned mesh and the animations for hands. You could use any custom skinned mesh that you might have created for this but I will use the mesh and animation that Epic ships with the VR template for this tutorial.
We will need to create a project from the VR template from either the Epic launcher or from the Unreal Engine editor. Once the project has been successfully created we open the project and migrate that we will need for the hands. The skinned mesh that is used for the hand controller is found in the folder: “Content\VirtualReality\Mannequin\Character\Mesh“.
Right click on the skinned mesh asset and select the “Asset Actions\Migrate…” option from the context menu. You should get the pop-up to select the content folder of the destination project. Browse and select the content folder of the tutorial project.
After the files are copied, you can close the VR template project and open the tutorial project if it is not already open. Inspecting the content browser window, we should find that the static mesh for the hand has been migrated to the project and the folder structure is the same as the VR template project. Now that we have the require art assets we can go ahead and change the code to have the skinned mesh instead of the cubes that we set-up in the previous tutorial.
Using the Skinned Mesh to Represent VR Motion Controllers:
Let’s start by editing the code that loads the static cube mesh to load the hand skinned mesh that we just imported from the template project. In the player pawn class that we created, change the function declaration in the header file from
UStaticMeshComponent* CreateHandMesh(UMotionControllerComponent* a_compParent, FName a_strDisplayName, FName a_nameHandType);
to
USkeletalMeshComponent* CreateHandMesh(UMotionControllerComponent* a_compParent, FName a_strDisplayName, FName a_nameHandType);
This way the function can load and set-up the skinned mesh for the hand. We will require the reference to the skinned mesh component to the kept in the class later on when we do the input management etc. We can safely ignore the returned reference for this tutorial. Here is the function that is changed from the static cube loading to the skeletal mesh:
USkeletalMeshComponent* AcVRPlayerPawn::CreateHandMesh(UMotionControllerComponent* a_compParent, FName a_strDisplayName, FName a_nameHandType) { USkeletalMeshComponent* refComponentHand = NULL; //Find the default cube that ships with the engine content static ConstructorHelpers::FObjectFinderHandMeshObject(TEXT("SkeletalMesh'/Game/VirtualReality/Mannequin/Character/Mesh/MannequinHand_Right.MannequinHand_Right'")); if (!HandMeshObject.Object) { UE_LOG(LogTemp, Error, TEXT("Could not load the default cube for hand mesh")); return NULL; } //create the mesh component refComponentHand = CreateDefaultSubobject(a_strDisplayName); //set the mesh to the component refComponentHand->SetSkeletalMesh(HandMeshObject.Object,true); //Set the defaults refComponentHand->SetAutoActivate(true); refComponentHand->SetVisibility(true); refComponentHand->SetHiddenInGame(false); FQuat qRotation = FQuat::Identity; FVector vec3Scale = FVector::OneVector; if (a_nameHandType == FXRMotionControllerBase::LeftHandSourceId) { qRotation = FQuat(FVector(1, 0, 0), FMath::DegreesToRadians(90)); vec3Scale = FVector(1, -1, 1); //a_refHand->SetVisibility(false); //a_refHand->SetHiddenInGame(true); } else { qRotation = FQuat(FVector(1, 0, 0), FMath::DegreesToRadians(270)); } refComponentHand->SetRelativeLocationAndRotation(FVector::ZeroVector, qRotation); refComponentHand->SetRelativeScale3D(vec3Scale); refComponentHand->SetupAttachment(a_compParent); return refComponentHand; }
Compile the code and open the blueprint player pawn in the editor. You should see that the cubes have been replaced by the hand skeletal meshes. At this point, you can preview the changes in the editor or even make a build and see that the hands move with the motion controllers. You should be able to see the output like in the video below.
In the next part, we will set up the animation blueprint for the hand and load that up. We will also receive input from the motion controller and use that to perform the grab animation.
2 thoughts on “Adding Hand models To VR Motion Controllers”