Files
sbox-public/engine/Sandbox.Engine/Scene/Components/Audio/LipSyncComponent.cs
Garry Newman 4fd85ce69d Add SkinnedModelRenderer.SpeakSound with baked viseme lipsync; remove dead native code (#5306)
// SkinnedModelRenderer - the only new entry point for speaking
SoundHandle SpeakSound( SoundEvent sound, GameObject mouth = null )
SoundHandle SpeakSound( SoundFile sound, float volume = 1, float pitch = 1, GameObject mouth = null )

// SoundFile - the authored lipsync track, read from the compiled vsnd
VisemeTrack Visemes { get; }   // null = no track (or not loaded yet, see IsValidForPlayback)

// VisemeTrack - visemes over time, authored in the sound editor
IReadOnlyList<VisemeFrame> Frames { get; }
float Duration { get; }
bool Sample( float time, Span<float> weights )

// VisemeFrame - one viseme span (struct: Viseme, StartTime, EndTime)

// Visemes - the standard 15-viseme table, OVR order
static IReadOnlyList<string> MorphNames { get; }
static int Count { get; }

// MorphCollection - the one shared viseme blend (sounds, voice chat, editor preview)
void ApplyVisemes( ReadOnlySpan<float> visemeWeights, float scale = 1, float smoothTime = 0 )

// ModelMorphs - per-model viseme weight table, cached managed-side (no interop on lookups)
float GetVisemeMorph( int viseme, int morph )
float GetVisemeMorph( string viseme, int morph )

// ResourceCompileContext - inject managed data into natively-compiled resources
void WriteBlock( string blockName, ReadOnlySpan<byte> data )   // four-char block names, eg "LIPS"
void WriteBlockJson( string blockName, object obj )
JsonObject ReadMeta()                                          // the asset's .meta, registers a compile dependency
T ReadMeta<T>( string key, T defaultValue = default )

// ResourceLoadContext - read blocks back when the resource loads (Resource.OnLoaded, engine-internal for now)
// public ref struct, only valid during the load callback
bool Exists( string blockName )
ReadOnlySpan<byte> ReadData( string blockName )
string ReadString( string blockName )
T ReadJson<T>( string blockName, T defaultValue = default )
2026-07-09 20:04:02 +01:00

49 lines
1.1 KiB
C#

namespace Sandbox;
/// <summary>
/// Drive a renderer's viseme morphs from a playing sound component. This is the
/// same lipsync <see cref="SkinnedModelRenderer.SpeakSound(SoundEvent, GameObject)"/>
/// uses, pointed at a sound component instead.
/// </summary>
[Expose]
[Category( "Audio" )]
[Title( "Lip Syncing" )]
[Icon( "emoji_emotions" )]
[Tint( EditorTint.Green )]
public sealed class LipSync : Component
{
[Property]
public BaseSoundComponent Sound { get; set; }
[Property]
public SkinnedModelRenderer Renderer { get; set; }
[Property, Title( "Scale" ), Group( "Morph" ), Range( 0, 5 )]
public float MorphScale { get; set; } = 1.5f;
[Property, Title( "Smoothing" ), Group( "Morph" ), Range( 0, 1 )]
public float MorphSmoothTime { get; set; } = 0.1f;
protected override void OnUpdate()
{
base.OnUpdate();
if ( !Renderer.IsValid() )
return;
Renderer.VoiceRealtimeScale = MorphScale;
Renderer.VoiceMorphSmoothTime = MorphSmoothTime;
Renderer.Voice = Sound.IsValid() ? Sound.SoundHandleInternal : null;
}
protected override void OnDisabled()
{
base.OnDisabled();
if ( Renderer.IsValid() )
{
Renderer.Voice = null;
}
}
}