Skip to content

Commit

Permalink
[SDK-588] Update render api (#147)
Browse files Browse the repository at this point in the history
## [SDK-588](https://ready-player-me.atlassian.net/browse/SDK-588)

## Description

- Updated render api 
- Updated samples and tests
  • Loading branch information
rYuuk authored and HarrisonHough committed Nov 29, 2023
1 parent 8f02c60 commit f47abc5
Show file tree
Hide file tree
Showing 17 changed files with 1,384 additions and 1,185 deletions.
22 changes: 3 additions & 19 deletions Runtime/AvatarRenderLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,13 @@ public class AvatarRenderLoader
/// the <see cref="AvatarRenderLoader.OnCompleted" /> event.
/// </summary>
/// <param name="url">The url to the avatars .glb file.</param>
/// <param name="renderScene">The <see cref="AvatarRenderScene" /> to use for the avatar render.</param>
/// <param name="blendShapeMeshes">
/// The name of the <see cref="SkinnedMeshRenderer" /> that contains the blendshapes you
/// want to set.
/// </param>
/// <param name="renderBlendShapes">A map of blendshape names and values that you want to set.</param>
/// <param name="renderSettings">Settings for render.</param>
public async void LoadRender(
string url,
AvatarRenderScene renderScene,
string[] blendShapeMeshes = null,
Dictionary<string, float> renderBlendShapes = null
)
AvatarRenderSettings renderSettings)
{
var renderSettings = new AvatarRenderSettings
{
Model = url,
Scene = renderScene,
BlendShapeMeshes = blendShapeMeshes,
BlendShapes = renderBlendShapes
};

var context = new AvatarContext();
context.Url = renderSettings.Model;
context.Url = url;
context.RenderSettings = renderSettings;

executor = new OperationExecutor<AvatarContext>(new IOperation<AvatarContext>[]
Expand Down
69 changes: 57 additions & 12 deletions Runtime/Data/AvatarRenderSettings.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,79 @@
using System.Collections.Generic;
using System.Globalization;
using UnityEngine;

namespace ReadyPlayerMe.Core
{
[System.Serializable]
public struct BlendShape
{
public string Name;
public float Value;
public BlendShape(string name, float value)
{
Name = name;
Value = value;
}
}

/// <summary>
/// This structure holds all the data required a request to the Avatar Render API.
/// </summary>
public struct AvatarRenderSettings
[System.Serializable]
public class AvatarRenderSettings
{
public string Model;
public AvatarRenderScene Scene;
public string[] BlendShapeMeshes;
public Dictionary<string, float> BlendShapes;
public Expression Expression = Expression.None;
public RenderPose Pose = RenderPose.Relaxed;
public RenderCamera Camera = RenderCamera.Portrait;
public int Quality = 100;
public int Size = 800;
public Color Background = Color.white;
public bool IsTransparent;
public List<BlendShape> BlendShapes;

public string GetParametersAsString()
{
BlendShapes ??= new Dictionary<string, float>();
var queryBuilder = new QueryBuilder();
queryBuilder.AddKeyValue(AvatarAPIParameters.RENDER_SCENE, Scene.GetSceneNameAsString());
foreach (KeyValuePair<string, float> blendShape in BlendShapes)
if (Expression != Expression.None)
{
foreach (var blendShapeMesh in BlendShapeMeshes)
queryBuilder.AddKeyValue(nameof(Core.Expression).ToCamelCase(), Expression.ToString().ToCamelCase());
}

queryBuilder.AddKeyValue(nameof(Pose).ToCamelCase(), RenderSettingsHelper.RenderPoseMap[Pose]);

if (BlendShapes != null)
{
foreach (var blendShape in BlendShapes)
{
string key = $"{AvatarAPIParameters.RENDER_BLEND_SHAPES}[{blendShapeMesh}][{blendShape.Key}]";
string value = blendShape.Value.ToString(CultureInfo.InvariantCulture);
var key = $"{AvatarAPIParameters.RENDER_BLEND_SHAPES}[{blendShape.Name}]";
var value = blendShape.Value.ToString(CultureInfo.InvariantCulture);
queryBuilder.AddKeyValue(key, value);
}
}


queryBuilder.AddKeyValue(nameof(Camera).ToCamelCase(), Camera.ToString().ToLower());

// TODO Quality is only supported by jpg. Find better approach for this.
// if (Quality == 0)
// {
// Quality = 100;
// }
// queryBuilder.AddKeyValue(nameof(Quality).ToCamelCase(), Quality.ToString());

if (Size == 0)
{
Size = 800;
}

queryBuilder.AddKeyValue(nameof(Size).ToCamelCase(), Size.ToString());

if (!IsTransparent)
{
queryBuilder.AddKeyValue(nameof(Background).ToCamelCase(), RenderSettingsHelper.FloatToRGBString(Background));
}

return queryBuilder.Query;
}

}
}
36 changes: 24 additions & 12 deletions Runtime/Data/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,6 @@ public enum MeshType
TeethMesh
}

/// <summary>
/// This enumeration describes the different render scene options.
/// </summary>
public enum AvatarRenderScene
{
[Description("Upper body render")] FullbodyPortrait,
[Description("Upper body render")] HalfbodyPortrait,
[Description("Upper body render with transparent background")] FullbodyPortraitTransparent,
[Description("Upper body render with transparent background")] HalfbodyPortraitTransparent,
[Description("Posed full body render with transparent background")] FullBodyPostureTransparent
}

/// <summary>
/// This enumeration describes the pose options for the avatar skeleton.
/// </summary>
Expand Down Expand Up @@ -111,5 +99,29 @@ public enum FailureType
AvatarRenderError,
OperationCancelled
}

public enum Expression
{
None,
Happy,
Lol,
Sad,
Scared,
Rage,
}

public enum RenderPose
{
Relaxed,
PowerStance,
Standing,
ThumbsUp,
}

public enum RenderCamera
{
Portrait,
FullBody
}

}
21 changes: 0 additions & 21 deletions Runtime/Utils/RenderSceneHelper.cs

This file was deleted.

31 changes: 31 additions & 0 deletions Runtime/Utils/RenderSettingsHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Collections.Generic;
using UnityEngine;

namespace ReadyPlayerMe.Core
{
public static class RenderSettingsHelper
{
public static string ToCamelCase(this string str)
{
if (string.IsNullOrEmpty(str))
return string.Empty;

var letters = str.ToCharArray();
letters[0] = char.ToLower(letters[0]);
return new string(letters);
}

public static Dictionary<RenderPose, string> RenderPoseMap = new Dictionary<RenderPose, string>()
{
{ RenderPose.PowerStance, "power-stance" },
{ RenderPose.Relaxed, "relaxed" },
{ RenderPose.Standing, "standing" },
{ RenderPose.ThumbsUp, "thumbs-up" }
};

public static string FloatToRGBString(Color color)
{
return $"{(int) (color.r * 255)},{(int) (color.g * 255)},{(int) (color.b * 255)}";
}
}
}
File renamed without changes.

This file was deleted.

This file was deleted.

Loading

0 comments on commit f47abc5

Please sign in to comment.