Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK-588] Update render api #147

Merged
merged 4 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)}";
}
}
}

This file was deleted.

This file was deleted.

Loading