live2D (sdk가 6.5 미지원으로 sprite로 대응)
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
/**
|
||||
* Copyright(c) Live2D Inc. All rights reserved.
|
||||
*
|
||||
* Use of this source code is governed by the Live2D Open Software license
|
||||
* that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
|
||||
*/
|
||||
|
||||
|
||||
using Live2D.Cubism.Core;
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
namespace Live2D.Cubism.Framework.HarmonicMotion
|
||||
{
|
||||
/// <summary>
|
||||
/// Controller for <see cref="CubismHarmonicMotionParameter"/>s.
|
||||
/// </summary>
|
||||
public sealed class CubismHarmonicMotionController : MonoBehaviour, ICubismUpdatable
|
||||
{
|
||||
/// <summary>
|
||||
/// Default number of channels.
|
||||
/// </summary>
|
||||
private const int DefaultChannelCount = 1;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Blend mode.
|
||||
/// </summary>
|
||||
[SerializeField]
|
||||
public CubismParameterBlendMode BlendMode = CubismParameterBlendMode.Additive;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The timescales for each channel.
|
||||
/// </summary>
|
||||
[SerializeField]
|
||||
public float[] ChannelTimescales;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Sources.
|
||||
/// </summary>
|
||||
private CubismHarmonicMotionParameter[] Sources { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Destinations.
|
||||
/// </summary>
|
||||
private CubismParameter[] Destinations { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Model has update controller component.
|
||||
/// </summary>
|
||||
[HideInInspector]
|
||||
public bool HasUpdateController { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Refreshes the controller. Call this method after adding and/or removing <see cref="CubismHarmonicMotionParameter"/>.
|
||||
/// </summary>
|
||||
public void Refresh()
|
||||
{
|
||||
var model = this.FindCubismModel();
|
||||
|
||||
|
||||
// Catch sources and destinations.
|
||||
Sources = model
|
||||
.Parameters
|
||||
.GetComponentsMany<CubismHarmonicMotionParameter>();
|
||||
Destinations = new CubismParameter[Sources.Length];
|
||||
|
||||
|
||||
for (var i = 0; i < Sources.Length; ++i)
|
||||
{
|
||||
Destinations[i] = Sources[i].GetComponent<CubismParameter>();
|
||||
}
|
||||
|
||||
// Get cubism update controller.
|
||||
HasUpdateController = (GetComponent<CubismUpdateController>() != null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by cubism update controller. Order to invoke OnLateUpdate.
|
||||
/// </summary>
|
||||
public int ExecutionOrder
|
||||
{
|
||||
get { return CubismUpdateExecutionOrder.CubismHarmonicMotionController; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by cubism update controller. Needs to invoke OnLateUpdate on Editing.
|
||||
/// </summary>
|
||||
public bool NeedsUpdateOnEditing
|
||||
{
|
||||
get { return false; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by cubism update controller. Updates controller.
|
||||
/// </summary>
|
||||
public void OnLateUpdate()
|
||||
{
|
||||
// Return if it is not valid or there's nothing to update.
|
||||
if (!enabled || Sources == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Update sources and destinations.
|
||||
for (var i = 0; i < Sources.Length; ++i)
|
||||
{
|
||||
Sources[i].Play(ChannelTimescales);
|
||||
|
||||
|
||||
Destinations[i].BlendToValue(BlendMode, Sources[i].Evaluate());
|
||||
}
|
||||
}
|
||||
|
||||
#region Unity Events Handling
|
||||
|
||||
/// <summary>
|
||||
/// Called by Unity. Makes sure cache is initialized.
|
||||
/// </summary>
|
||||
private void Start()
|
||||
{
|
||||
// Initialize cache.
|
||||
Refresh();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Called by Unity. Updates controller.
|
||||
/// </summary>
|
||||
private void LateUpdate()
|
||||
{
|
||||
if (!HasUpdateController)
|
||||
{
|
||||
OnLateUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Called by Unity. Resets channels.
|
||||
/// </summary>
|
||||
private void Reset()
|
||||
{
|
||||
// Reset/Initialize channel timescales.
|
||||
ChannelTimescales = new float[DefaultChannelCount];
|
||||
|
||||
|
||||
for (var s = 0; s < DefaultChannelCount; ++s)
|
||||
{
|
||||
ChannelTimescales[s] = 1f;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7fdbd2f0b5a84544698d697356f8fea0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Copyright(c) Live2D Inc. All rights reserved.
|
||||
*
|
||||
* Use of this source code is governed by the Live2D Open Software license
|
||||
* that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
|
||||
*/
|
||||
|
||||
|
||||
namespace Live2D.Cubism.Framework.HarmonicMotion
|
||||
{
|
||||
/// <summary>
|
||||
/// Determines the direction of a harmonic motion from its origin.
|
||||
/// </summary>
|
||||
public enum CubismHarmonicMotionDirection
|
||||
{
|
||||
/// <summary>
|
||||
/// Motion to the left of the origin.
|
||||
/// </summary>
|
||||
Left,
|
||||
|
||||
/// <summary>
|
||||
/// Motion to the right of the origin.
|
||||
/// </summary>
|
||||
Right,
|
||||
|
||||
/// <summary>
|
||||
/// Centric left-right motion around the origin.
|
||||
/// </summary>
|
||||
Centric,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0bbb87e5c2985894483413158770026a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,218 @@
|
||||
/**
|
||||
* Copyright(c) Live2D Inc. All rights reserved.
|
||||
*
|
||||
* Use of this source code is governed by the Live2D Open Software license
|
||||
* that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
|
||||
*/
|
||||
|
||||
|
||||
using Live2D.Cubism.Core;
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
namespace Live2D.Cubism.Framework.HarmonicMotion
|
||||
{
|
||||
/// <summary>
|
||||
/// Holds data for controlling the output of simple harmonic motions.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This type of motion can be very useful for faking breathing, for example.
|
||||
/// </remarks>
|
||||
public sealed class CubismHarmonicMotionParameter : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// Timescale channel.
|
||||
/// </summary>
|
||||
[SerializeField]
|
||||
public int Channel;
|
||||
|
||||
/// <summary>
|
||||
/// Motion direction.
|
||||
/// </summary>
|
||||
[SerializeField]
|
||||
public CubismHarmonicMotionDirection Direction;
|
||||
|
||||
/// <summary>
|
||||
/// Normalized origin of motion.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The actual origin used for evaluating the motion depends limits of the <see cref="CubismParameter"/>.
|
||||
/// </remarks>
|
||||
[SerializeField, Range(0f, 1f)]
|
||||
public float NormalizedOrigin = 0.5f;
|
||||
|
||||
/// <summary>
|
||||
/// Normalized range of motion.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The actual origin used for evaluating the motion depends limits of the <see cref="CubismParameter"/>.
|
||||
/// </remarks>
|
||||
[SerializeField, Range(0f, 1f)]
|
||||
public float NormalizedRange = 0.5f;
|
||||
|
||||
/// <summary>
|
||||
/// Duration of one motion cycle in seconds.
|
||||
/// </summary>
|
||||
[SerializeField, Range(0.01f, 10f)]
|
||||
public float Duration = 3f;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// <see langword="true"/> if <see langword="this"/> is initialized.
|
||||
/// </summary>
|
||||
private bool IsInitialized
|
||||
{
|
||||
get { return Mathf.Abs(ValueRange) >= Mathf.Epsilon; }
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes instance.
|
||||
/// </summary>
|
||||
private void Initialize()
|
||||
{
|
||||
// Initialize value fields.
|
||||
var parameter = GetComponent<CubismParameter>();
|
||||
|
||||
|
||||
MaximumValue = parameter.MaximumValue;
|
||||
MinimumValue = parameter.MinimumValue;
|
||||
ValueRange = MaximumValue - MinimumValue;
|
||||
}
|
||||
|
||||
#region Interface for Controller
|
||||
|
||||
/// <summary>
|
||||
/// Cached <see cref="CubismParameter.MaximumValue"/>.
|
||||
/// </summary>
|
||||
private float MaximumValue { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Cached <see cref="CubismParameter.MinimumValue"/>.
|
||||
/// </summary>
|
||||
private float MinimumValue { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Range of <see cref="MaximumValue"/> and <see cref="MinimumValue"/>.
|
||||
/// </summary>
|
||||
private float ValueRange { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Current time.
|
||||
/// </summary>
|
||||
private float T { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Proceeds time.
|
||||
/// </summary>
|
||||
/// <param name="channelTimescales"></param>
|
||||
internal void Play(float[] channelTimescales)
|
||||
{
|
||||
T += (Time.deltaTime * channelTimescales[Channel]);
|
||||
|
||||
|
||||
// Make sure time stays within duration.
|
||||
while (T > Duration)
|
||||
{
|
||||
T -= Duration;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Evaluates the parameter.
|
||||
/// </summary>
|
||||
/// <returns>Parameter value.</returns>
|
||||
internal float Evaluate()
|
||||
{
|
||||
// Lazily initialize.
|
||||
if (!IsInitialized)
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
|
||||
// Restore origin and range.
|
||||
var origin = MinimumValue + (NormalizedOrigin * ValueRange);
|
||||
var range = NormalizedRange * ValueRange;
|
||||
|
||||
|
||||
// Clamp the range so that it stays within the limits.
|
||||
Clamp(ref origin, ref range);
|
||||
|
||||
|
||||
// Return result.
|
||||
return origin + (range * Mathf.Sin(T * (2 * Mathf.PI) / Duration));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Helper Methods
|
||||
|
||||
/// <summary>
|
||||
/// Clamp origin and range based on <see cref="Direction"/>.
|
||||
/// </summary>
|
||||
/// <param name="origin">Origin to clamp.</param>
|
||||
/// <param name="range">Range to clamp.</param>
|
||||
private void Clamp(ref float origin, ref float range)
|
||||
{
|
||||
switch (Direction)
|
||||
{
|
||||
case CubismHarmonicMotionDirection.Left:
|
||||
{
|
||||
if ((origin - range) >= MinimumValue)
|
||||
{
|
||||
range /= 2;
|
||||
origin -= range;
|
||||
}
|
||||
else
|
||||
{
|
||||
range = (origin - MinimumValue) / 2f;
|
||||
origin = MinimumValue + range;
|
||||
NormalizedRange = (range * 2f)/ValueRange;
|
||||
}
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
case CubismHarmonicMotionDirection.Right:
|
||||
{
|
||||
if ((origin + range) <= MaximumValue)
|
||||
{
|
||||
range /= 2f;
|
||||
origin += range;
|
||||
}
|
||||
else
|
||||
{
|
||||
range = (MaximumValue - origin) / 2f;
|
||||
origin = MaximumValue - range;
|
||||
NormalizedRange = (range * 2f)/ValueRange;
|
||||
}
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Clamp both range and NormalizedRange.
|
||||
if ((origin - range) < MinimumValue)
|
||||
{
|
||||
range = origin - MinimumValue;
|
||||
NormalizedRange = range / ValueRange;
|
||||
}
|
||||
else if ((origin + range) > MaximumValue)
|
||||
{
|
||||
range = MaximumValue - origin;
|
||||
NormalizedRange = range / ValueRange;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: aef61b2310c3336498fc218c4dffb25e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user