Files
sbox-public/engine/Sandbox.Tools/Assets/ResourceCompile/SoundCompiler.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

32 lines
1.0 KiB
C#

using Sandbox.Resources;
namespace Editor;
/// <summary>
/// Sounds are compiled by the native sound compiler, but we get first look at the
/// compile here. We read the lipsync visemes authored in the sound editor from the
/// sound's .meta and inject them into the compiled resource as a custom block, then
/// return false so the native compiler runs as usual - our block rides along into
/// the final file. The engine reads it back out via <see cref="SoundFile.Visemes"/>.
/// </summary>
[Expose]
[ResourceIdentity( "wav" )]
[ResourceIdentity( "mp3" )]
[ResourceIdentity( "ogg" )]
[ResourceIdentity( "flac" )]
public class SoundResourceCompiler : ResourceCompiler
{
protected override Task<bool> Compile()
{
var visemes = Context.ReadMeta<List<VisemeFrame>>( "visemes" );
if ( visemes is not null && visemes.Count > 0 )
{
Context.WriteBlockJson( SoundFile.VisemeBlockName, visemes );
}
// We never compile the audio ourselves - returning false hands the
// compile to the native sound compiler
return Task.FromResult( false );
}
}