Files
Pal3.Unity/Assets/Scripts/Engine/Animation/CoreAnimation.cs
Jiaqi Liu d1b2c05eb4 Optimize resource loading and fix PAL3A script execution (#92)
* Optimize DDS texture creation and static POL meshes

Separate file decoding from Unity upload through format-neutral texture data
and explicit world, sprite and skybox policies. Validate DDS layouts, preserve
alpha coverage and use native BC uploads with software RGBA fallback.

Compact referenced POL vertices and batch nearby static opaque groups sharing
materials. Keep water and dynamic geometry separate, with source ranges retained
only in the Editor. Add DDS layout and POL index regression tests.

Validation: PAL3 and PAL3A each passed 24 Editor tests, eight combined Game and
ResourceViewer style regressions, 936 paired skybox comparisons, and both macOS
Player builds on Metal. Editor provenance and Core tests are excluded from Players.

* Add core protocol regression tests and copyright checks

Cover SCE command contracts, binary readers, resource layouts and full local CPK decompression for PAL3 and PAL3A. Fix CVD time rebasing, binary read bounds and short reads, and CPK/NAV validation.

Standardize first-party copyright headers, preserve correct creation years, and update console source generation. Validate both variants in Unity and verify all live CPK entries.

* Implement PAL3A command handlers and isolate script execution

Correct PAL3A parameter signatures and add handlers for fortune selection,
skybox sequences, actor facing and attacks, team and progression state,
camera constraints, and battle preparation flags.

Give script instances separate locals, advance grouped waits concurrently,
and pause PAL3A parents only for their directly decoded child calls. Cancel
stale interactions and save replay on reset, preserve finite action semantics,
and restore camera orientation after releasing horizontal constraints.

Persist supported state in v2 saves and add protocol, runtime, and progression
regressions. Full combat gameplay remains outside this change.

Validation: PAL3 274/274 and PAL3A 291/291 EditMode tests; 18 PAL3 and 22 PAL3A
runtime checks. All 77,889 PAL3A instruction boundaries match EXE signatures.

* Run CI only for pull requests and upgrade build actions
2026-11-13 09:28:31 +08:00

193 lines
7.1 KiB
C#

// ------------------------------------------------------------------------
// Copyright (c) 2021-2026, Jiaqi (0x7c13) Liu. All rights reserved.
// See LICENSE file in the project root for license information.
// ------------------------------------------------------------------------
namespace Engine.Animation
{
using System;
using System.Collections;
using System.Threading;
using Core.Abstraction;
using Pal3.Core.Utilities;
using Services;
using Quaternion = UnityEngine.Quaternion;
using Vector3 = UnityEngine.Vector3;
public enum AnimationCurveType
{
// OG game only has these two
Linear = 0,
Sine = 1,
// New ones added by me
Quadratic = 2,
Cubic = 3,
Exponential = 4,
EaseIn = 5,
EaseOut = 6,
}
/// <summary>
/// Provides helper functions to do Transform animations etc.
/// </summary>
public static class CoreAnimation
{
// Progress -> (0,1)
private static float GetInterpolationRatio(float progress, AnimationCurveType curveType)
{
return curveType switch
{
AnimationCurveType.Linear => progress,
AnimationCurveType.Sine => MathF.Sin(progress * (MathF.PI / 2)),
AnimationCurveType.Quadratic => MathF.Pow(progress, 2),
AnimationCurveType.Cubic => MathF.Pow(progress, 3),
AnimationCurveType.Exponential => MathF.Exp(progress) - 1,
AnimationCurveType.EaseIn => MathF.Pow(progress, 3),
AnimationCurveType.EaseOut => 1f - MathF.Pow(1 - progress, 3),
_ => progress
};
}
private static float Clamp01(float value)
{
if ((double) value < 0.0) return 0.0f;
return (double) value > 1.0 ? 1f : value;
}
private static float Lerp(float a, float b, float t) => a + (b - a) * Clamp01(t);
public static IEnumerator EnumerateValueAsync(float from,
float to,
float duration,
AnimationCurveType curveType,
Action<float> onValueChanged,
CancellationToken cancellationToken = default)
{
if (MathF.Abs(duration) < float.Epsilon)
{
onValueChanged?.Invoke(to);
yield break;
}
float timePast = 0f;
while (timePast < duration && !cancellationToken.IsCancellationRequested)
{
float newValue = Lerp(from, to, GetInterpolationRatio(timePast / duration, curveType));
onValueChanged?.Invoke(newValue);
timePast += GameTimeProvider.Instance.DeltaTime;
yield return null;
}
if (!cancellationToken.IsCancellationRequested)
{
onValueChanged?.Invoke(to);
}
yield return null;
}
public static IEnumerator MoveAsync(this ITransform target,
Vector3 toPosition,
float duration,
AnimationCurveType curveType = AnimationCurveType.Linear,
CancellationToken cancellationToken = default)
{
Vector3 oldPosition = target.Position;
float timePast = 0f;
while (timePast < duration && !target.IsNativeObjectDisposed && !cancellationToken.IsCancellationRequested)
{
target.Position = oldPosition + (toPosition - oldPosition) *
GetInterpolationRatio(timePast / duration, curveType);
timePast += GameTimeProvider.Instance.DeltaTime;
yield return null;
}
if (!target.IsNativeObjectDisposed && !cancellationToken.IsCancellationRequested) target.Position = toPosition;
yield return null;
}
public static IEnumerator ShakeAsync(this ITransform target,
float duration,
float xAxisAmplitude,
float yAxisAmplitude,
float zAxisAmplitude)
{
Vector3 originalPosition = target.LocalPosition;
while (duration > 0 && !target.IsNativeObjectDisposed)
{
(float x, float y, float z) = RandomGenerator.RandomPointInsideUnitSphere();
target.LocalPosition = originalPosition + new Vector3(
x * xAxisAmplitude / 2f,
y * yAxisAmplitude / 2f,
z * zAxisAmplitude / 2f);
duration -= GameTimeProvider.Instance.DeltaTime;
yield return null;
}
if (!target.IsNativeObjectDisposed) target.LocalPosition = originalPosition;
yield return null;
}
public static IEnumerator OrbitAroundCenterPointAsync(this ITransform target,
Quaternion toRotation,
Vector3 centerPoint,
float duration,
AnimationCurveType curveType,
float distanceDelta,
CancellationToken cancellationToken = default)
{
float distance = Vector3.Distance(target.Position, centerPoint);
Quaternion startRotation = target.Rotation;
float timePast = 0f;
while (timePast < duration && !target.IsNativeObjectDisposed && !cancellationToken.IsCancellationRequested)
{
Quaternion newRotation = Quaternion.Slerp(startRotation, toRotation,
GetInterpolationRatio(timePast / duration, curveType));
Vector3 direction = (newRotation * Vector3.forward).normalized;
Vector3 newPosition = centerPoint + direction * -(distance + (timePast / duration) * distanceDelta);
target.SetPositionAndRotation(newPosition, newRotation);
timePast += GameTimeProvider.Instance.DeltaTime;
yield return null;
}
if (!target.IsNativeObjectDisposed && !cancellationToken.IsCancellationRequested)
{
Vector3 newPosition = centerPoint + (toRotation * Vector3.forward).normalized * -(distance + distanceDelta);
target.SetPositionAndRotation(newPosition, toRotation);
}
yield return null;
}
public static IEnumerator RotateAsync(this ITransform target,
Quaternion toRotation,
float duration,
AnimationCurveType curveType,
CancellationToken cancellationToken = default)
{
Quaternion startRotation = target.Rotation;
float timePast = 0f;
while (timePast < duration && !target.IsNativeObjectDisposed && !cancellationToken.IsCancellationRequested)
{
Quaternion rotation = Quaternion.Slerp(startRotation, toRotation,
GetInterpolationRatio(timePast / duration, curveType));
target.Rotation = rotation;
timePast += GameTimeProvider.Instance.DeltaTime;
yield return null;
}
if (!target.IsNativeObjectDisposed && !cancellationToken.IsCancellationRequested) target.Rotation = toRotation;
yield return null;
}
}
}