Convai Integration - PlayCanvas Plugin Guide for seamless integration.
After the addition of Convai web-sdk-cdn to url section, ConvaiClient class will be available to the browser directly.
Add all the scripts bellow to your Character entity.
Replace the empty "" with you API key and Character ID.
The ConvaiNpc
script is responsible for handling the interaction between the user and a virtual character powered by the Convai AI.
The script initializes the Convai client by providing an API key and character ID. It sets up necessary callbacks to handle various events, such as errors, user queries, and audio responses from the Convai service.
The initializeConvaiClient
function is the entry point for setting up the Convai client. It creates a new instance of the ConvaiClient
and configures it with the provided API key, character ID, and other settings like enabling audio and facial data.
The script handles user input through two methods: text input via a form and voice input using the "T" key. For voice input, the handleKeyDown
and handleKeyUp
functions are used to detect when the "T" key is pressed and released, respectively. When the "T" key is pressed, the script starts recording audio and sends it to the Convai service for processing.
The ConvaiNpc.prototype.initialize
function is called once per entity and sets up the Convai client. It also registers callbacks for handling audio playback events, updating the isTalking
and conversationActive
flags accordingly.
The ConvaiNpc.prototype.handleAnimation
function updates the character's animation based on the isTalking
state, allowing for synchronized lip movements and facial expressions.
The PlayerAnimationHandler
script is responsible for controlling the animations of a player character based on certain conditions, such as velocity or other factors.
The script defines three attributes:
blendTime
: This attribute controls the blend time between animations, which determines how smoothly the transition between animations occurs. The default value is set to 0.2.
velMin
: This attribute represents the minimum velocity required to trigger a specific animation. The default value is set to 10.
velMax
: This attribute represents the maximum velocity required to trigger a specific animation. The default value is set to 50.
These attributes can be adjusted in the editor or through code to fine-tune the animation behavior for the player character.
The initialize
function is called when the script is initialized. In this implementation, it plays the 'Idle' animation with the specified blend time (this.blendTime
). This animation will be played when the player character is not moving or when the velocity is outside the range defined by velMin
and velMax
.
The script is designed to be extended further to handle different animation states based on the player character's velocity or other conditions. For example, you could add additional functions or logic to check the player's velocity and play different animations (e.g., 'Walk', 'Run') based on the velocity range defined by velMin
and velMax
.
By utilizing this script, you can easily manage and transition between different animations for the player character, providing a more immersive and realistic experience in your game or application.
The Lipsync
script is responsible for animating the character's mouth and facial expressions based on the received viseme data. Visemes are the key mouth shapes and facial positions used to represent speech sounds. The script applies morph target animations to the character's head and teeth components to achieve realistic lip-syncing effects.
The script works by accessing the visemeData
array, which contains the viseme weights for each frame of the animation. It then applies these weights to the corresponding morph targets on the head and teeth components. The runVisemeData
function handles this process by looping through the viseme weights and setting the morph target weights accordingly.
The script keeps track of the current viseme frame using the currentVisemeFrame
variable and a timer variable. This ensures that the viseme animations are synchronized with the audio playback. When the viseme data has finished playing, the zeroMorphs
function is called to reset all morph target weights to zero, effectively resetting the character's facial expression.
The HeadTracking
script is responsible for controlling the rotation of a character's head and eyes based on the position of the camera (representing the user's viewpoint). The script achieves this by calculating the angle between the forward vector of the head component and the forward vector of the camera. If this angle is within a specified threshold (45 degrees in this case), the head and eyes are rotated to look towards the camera's position.
Add all the above scripts to your playcanvas project and attach convaiNPC, lipsync, Headtracking to the convi (your model) component.