4.3 KiB
| title | description |
|---|---|
| Photo-to-Avatar generation | Explains how to use AvatarCreationService AvatarGeneration APIs to prompt users to generate Avatars from photos in experience. |
You can create an experience that allows players to generate a fully functional avatar character using a photo and a text prompt.
This process involves the following steps:
- Request an AvatarGeneration session
- Prompt the user to take a selfie and generate a 2D preview image of the avatar
- Generate the full Avatar character using
Class.HumanoidDescription
Request an AvatarGeneration session
To start a photo-to-avatar generation, call Class.AvatarCreationService:RequestAvatarGenerationSessionAsync() from the server to request a session for the player. A session provides a Class.Player with a certain number of Avatar previews and Avatar generations.
As it may take some time for a session to become available, Class.AvatarCreationService:RequestAvatarGenerationSessionAsync() returns a Datatype.RBXScriptConnection and a waitTime in seconds. The waitTime can be used to provide the Class.Player with information on how long it will take them to be able to start their generations.
Once a session is ready, the callback is invoked with a Dictionary of information about the session. The Dictionary includes:
SessionId— passed as an argument when callingClass.AvatarCreationService:GenerateAvatar2DPreviewAsyncandClass.AvatarCreationService:GenerateAvatarAsyncAllowed2DGenerations— the number of 2D preview generations allowed in a sessionAllowed3DGenerations— the number of 3D avatar generations allowed in a sessionSessionTime— the maximum time for a session in seconds
Prompt selfie and generate 2D preview
After a session is started, prompt the user to take a selfie and get the fileId string by calling the Class.AvatarCreationService:PromptSelectAvatarGenerationImageAsync method on the Server. This fileId will be passed to the Class.AvatarCreationService:GenerateAvatar2DPreviewAsync method.
To create a 2D preview image of the Avatar call the following methods:
Class.AvatarCreationService:GenerateAvatar2DPreviewAsyncon the ServerClass.AvatarCreationService:LoadAvatar2DPreviewAsyncon the Client
The Class.AvatarCreationService:GenerateAvatar2DPreviewAsync takes the SessionId, fileId and an optional text prompt as input to generate a 2D Avatar preview. This method returns a previewId.
This previewId should be sent to the client and can then be used to retrieve the preview as an Class.EditableImage.
If the user is not satisfied with the generated preview this workflow can be repeated.
Generate the 3D avatar
Once the user is satisfied with the preview, you can generate the complete 3D avatar character.
To generate an avatar character:
- Call
Class.AvatarCreationService:GenerateAvatarAsyncon the Server with aDictionarycontaining the SessionId and PreviewId.- This method call returns a
stringgenerationId.
- This method call returns a
- Retrieve the generated avatar data as a
Class.HumanoidDescriptionusing theClass.AvatarCreationService:LoadGeneratedAvatarAsyncmethod on both the experience server and the client. - You can use the
Players.CreateHumanoidModelFromDescriptionmethod to create an avatar model from theClass.HumanoidDescriptionto display to theClass.Player.
Mesh and texture assets will be provided as Class.EditableMesh and Class.EditableImage objects, respectively, to allow continued editing of the generated avatar. Edits should be made on both the experience server and the client copy to keep them in sync for publish.
Publish
If the user is satisfied with the resulting avatar it can be published using the Class.AvatarCreationService:PromptCreateAvatarAsync method.
For more details on see in-experience creation.