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-685] Feature/avatar preview element #181

Merged
merged 5 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &1740648930196985801
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1740648930196985805}
- component: {fileID: 1740648930196985800}
- component: {fileID: 1740648930196985807}
- component: {fileID: 1740648930196985804}
m_Layer: 0
m_Name: Avatar Preview Element
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1740648930196985805
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1740648930196985801}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1740648930196985800
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1740648930196985801}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 198f21f00ad242b47bee2a7073994bea, type: 3}
m_Name:
m_EditorClassIdentifier:
cameraTransform: {fileID: 0}
faceViewPoint: {x: 0, y: 1.66, z: 0.85}
bodyViewPoint: {x: 0, y: 1.4, z: 2.6}
defaultDuration: 1
--- !u!114 &1740648930196985807
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1740648930196985801}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 740b98666dec44e5bb949b172ef49d6a, type: 3}
m_Name:
m_EditorClassIdentifier:
avatar: {fileID: 0}
speed: 50
rememberLastRotation: 1
--- !u!114 &1740648930196985804
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1740648930196985801}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c3cb903a634eb8c48a7050b2566ff30f, type: 3}
m_Name:
m_EditorClassIdentifier:

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 79 additions & 0 deletions Runtime/AvatarCreator/Utils/CameraFocuser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using ReadyPlayerMe.Core;
using UnityEngine;

namespace ReadyPlayerMe.AvatarCreator
{
public class CameraFocuser : MonoBehaviour
{
private const string TAG = nameof(CameraFocuser);

[SerializeField] private Transform cameraTransform;
[SerializeField] private Vector3 faceViewPoint = new Vector3(0f, 1.66f, 0.85f);
[SerializeField] private Vector3 bodyViewPoint = new Vector3(0f, 1.4f, 2.6f);
[SerializeField] private float defaultDuration = 1f;

private Vector3 targetPosition;
private Vector3 startPosition;
private float transitionTime;
private float duration;
private bool isTransitioning;

private void Awake()
{
if (cameraTransform == null)
{
SDKLogger.LogWarning(TAG, $"Avatar object not set for {gameObject.name}.");
}
}

private void LateUpdate()
{
if (!isTransitioning) return;
transitionTime += Time.deltaTime;
if (transitionTime < duration)
{
var t = Mathf.SmoothStep(0.0f, 1.0f, transitionTime / duration);
cameraTransform.position = Vector3.Lerp(startPosition, targetPosition, t);
}
else
{
cameraTransform.position = targetPosition;
isTransitioning = false;
}
}

public void FocusOnFace()
{
StartTransition(faceViewPoint, defaultDuration);
}

public void FocusOnBody()
{
StartTransition(bodyViewPoint, defaultDuration);
}

public void StopTransition()
{
isTransitioning = false;
}

private void StartTransition(Vector3 newTargetLocalPosition, float transitionDuration)
{
startPosition = cameraTransform.position;
targetPosition = transform.TransformPoint(newTargetLocalPosition);
duration = transitionDuration;
transitionTime = 0f;
isTransitioning = true;
}

private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawSphere(transform.position + faceViewPoint, 0.1f);

Gizmos.color = Color.blue;
Gizmos.DrawSphere(transform.position + bodyViewPoint, 0.1f);
}
}
}
11 changes: 11 additions & 0 deletions Runtime/AvatarCreator/Utils/CameraFocuser.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 0 additions & 46 deletions Runtime/AvatarCreator/Utils/Rotation/AvatarMouseRotation.cs

This file was deleted.

47 changes: 47 additions & 0 deletions Runtime/AvatarCreator/Utils/Rotation/AvatarRotator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using ReadyPlayerMe.Core;
using UnityEngine;

namespace ReadyPlayerMe.AvatarCreator
{
public class AvatarRotator : MonoBehaviour
{
private const string TAG = nameof(AvatarRotator);

[SerializeField] private GameObject avatar;
[SerializeField] private float speed = 50;
[SerializeField] private bool rememberLastRotation = true;

private IAvatarRotatorInput avatarRotatorInput;
private Quaternion lastRotation;

private void Awake()
{
avatarRotatorInput = GetComponent<IAvatarRotatorInput>();
if (avatar == null)
{
SDKLogger.LogWarning(TAG, $"Avatar object not set for {gameObject.name}.");
}
}

public void SetAvatar(GameObject newAvatar)
{
avatar = newAvatar;
if (rememberLastRotation && avatar != null)
{
avatar.transform.rotation = lastRotation;
}
}

private void Update()
{
if (avatar == null || avatarRotatorInput == null || !avatarRotatorInput.IsInputDetected())
{
return;
}

var rotationAmount = avatarRotatorInput.GetRotationAmount();
avatar.transform.Rotate(Vector3.up, rotationAmount * Time.deltaTime * speed);
lastRotation = avatar.transform.rotation;
}
}
}
23 changes: 23 additions & 0 deletions Runtime/AvatarCreator/Utils/Rotation/IAvatarRotatorInput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace ReadyPlayerMe.AvatarCreator
{
/// <summary>
/// Defines an interface for handling rotation input.
/// This interface provides methods to detect rotation input and retrieve the corresponding rotation amount.
/// Implementers of this interface can define custom logic for how rotation input is detected and calculated.
/// </summary>
public interface IAvatarRotatorInput
{
/// <summary>
/// Determines if rotation input is currently being detected.
/// </summary>
/// <returns>A boolean indicating whether rotation input is active.</returns>
bool IsInputDetected();

/// <summary>
/// Calculates and returns the amount of rotation based on the input.
/// This method should provide the rotation value to apply based on the detected input.
/// </summary>
/// <returns>The amount of rotation as a float.</returns>
float GetRotationAmount();
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions Runtime/AvatarCreator/Utils/Rotation/IMouseInput.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Runtime/AvatarCreator/Utils/Rotation/IMouseInput.cs.meta

This file was deleted.

22 changes: 0 additions & 22 deletions Runtime/AvatarCreator/Utils/Rotation/MouseInput.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Runtime/AvatarCreator/Utils/Rotation/MouseInput.cs.meta

This file was deleted.

33 changes: 33 additions & 0 deletions Runtime/AvatarCreator/Utils/Rotation/MouseRotationHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using UnityEngine;

namespace ReadyPlayerMe.AvatarCreator
{
public class MouseRotationHandler : MonoBehaviour, IAvatarRotatorInput
{
private const int MOUSE_BUTTON_INDEX = 0;
private float lastPosX;
private bool rotate;

public bool IsInputDetected()
{
if (Input.GetMouseButtonDown(MOUSE_BUTTON_INDEX))
{
lastPosX = Input.mousePosition.x;
rotate = true;
}
else if (Input.GetMouseButtonUp(MOUSE_BUTTON_INDEX))
{
rotate = false;
}

return rotate;
}

public float GetRotationAmount()
{
var rotationAmount = lastPosX - Input.mousePosition.x;
lastPosX = Input.mousePosition.x;
return rotationAmount;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading