feat(math): add MathUtils class with remapping functions for various data types

This commit is contained in:
Данил Ярош 2025-11-27 17:02:29 +03:00
parent 0718c7805b
commit 9bcf2cfd16
28 changed files with 49079 additions and 327 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -913,7 +913,7 @@ Animator:
m_Enabled: 1
m_Avatar: {fileID: 0}
m_Controller: {fileID: 9100000, guid: 55e5b6f3f7baeab4d8dece0d2d9e57da, type: 2}
m_CullingMode: 0
m_CullingMode: 2
m_UpdateMode: 0
m_ApplyRootMotion: 0
m_LinearVelocityBlending: 0

View File

@ -146,6 +146,10 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 95000010530816694, guid: ce611ebabf8eb0f4683c660f008c8b88, type: 3}
propertyPath: m_CullingMode
value: 2
objectReference: {fileID: 0}
- target: {fileID: 2340304884233875183, guid: ce611ebabf8eb0f4683c660f008c8b88, type: 3}
propertyPath: m_Enabled
value: 1

View File

@ -1582,7 +1582,7 @@ Animator:
m_Enabled: 1
m_Avatar: {fileID: 0}
m_Controller: {fileID: 9100000, guid: 55e5b6f3f7baeab4d8dece0d2d9e57da, type: 2}
m_CullingMode: 0
m_CullingMode: 2
m_UpdateMode: 0
m_ApplyRootMotion: 0
m_LinearVelocityBlending: 0

View File

@ -294,7 +294,7 @@ Animator:
m_Enabled: 1
m_Avatar: {fileID: 0}
m_Controller: {fileID: 9100000, guid: e9c3c84c4e749924aa857dae9aa1573c, type: 2}
m_CullingMode: 0
m_CullingMode: 2
m_UpdateMode: 0
m_ApplyRootMotion: 0
m_LinearVelocityBlending: 0

View File

@ -158,7 +158,7 @@ BoxCollider2D:
m_UsedByEffector: 0
m_CompositeOperation: 0
m_CompositeOrder: 0
m_Offset: {x: 1, y: 1}
m_Offset: {x: 0.9999962, y: 1.7734919}
m_SpriteTilingProperty:
border: {x: 0, y: 0, z: 0, w: 0}
pivot: {x: 0, y: 0}
@ -168,7 +168,7 @@ BoxCollider2D:
drawMode: 0
adaptiveTiling: 0
m_AutoTiling: 0
m_Size: {x: 2, y: 2}
m_Size: {x: 2.8071213, y: 3.5469837}
m_EdgeRadius: 0
--- !u!114 &2823314259245713668
MonoBehaviour:

View File

@ -8,6 +8,14 @@ PrefabInstance:
serializedVersion: 3
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: 2823314259245713668, guid: 1c7455363049dbc4996e240f0bcdceff, type: 3}
propertyPath: activateSfx.pitch
value: 1
objectReference: {fileID: 0}
- target: {fileID: 2823314259245713668, guid: 1c7455363049dbc4996e240f0bcdceff, type: 3}
propertyPath: activateSfx.volume
value: 1
objectReference: {fileID: 0}
- target: {fileID: 2861349915552106200, guid: 1c7455363049dbc4996e240f0bcdceff, type: 3}
propertyPath: m_IsActive
value: 0
@ -52,6 +60,22 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5574401725797638114, guid: 1c7455363049dbc4996e240f0bcdceff, type: 3}
propertyPath: m_Size.x
value: 3.2561624
objectReference: {fileID: 0}
- target: {fileID: 5574401725797638114, guid: 1c7455363049dbc4996e240f0bcdceff, type: 3}
propertyPath: m_Size.y
value: 3.996025
objectReference: {fileID: 0}
- target: {fileID: 5574401725797638114, guid: 1c7455363049dbc4996e240f0bcdceff, type: 3}
propertyPath: m_Offset.x
value: 0.9839591
objectReference: {fileID: 0}
- target: {fileID: 5574401725797638114, guid: 1c7455363049dbc4996e240f0bcdceff, type: 3}
propertyPath: m_Offset.y
value: 1.9980125
objectReference: {fileID: 0}
- target: {fileID: 6994018639452157032, guid: 1c7455363049dbc4996e240f0bcdceff, type: 3}
propertyPath: m_Name
value: Checkpoint_CP

View File

@ -245,9 +245,10 @@ MonoBehaviour:
sfxVolume: 1
alignRotationToVelocity: 1
owner: {fileID: 0}
heldLocalOffset: {x: 0, y: 0, z: 0}
heldScale: 1.5
heldLocalOffset: {x: -0.3, y: -0.35, z: 0}
heldScale: 1
ignoreOwnerTimeOnThrow: 0.1
hideBehindAttachPoint: 1
groundVelocity: 0.05
maxGroundSlideTime: 1
lockWhenLanded: 1

View File

@ -26,11 +26,11 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 3607988290522455547, guid: dc6d2a9c11b2eac42a960140407a33fb, type: 3}
propertyPath: m_LocalPosition.x
value: 0
value: 204
objectReference: {fileID: 0}
- target: {fileID: 3607988290522455547, guid: dc6d2a9c11b2eac42a960140407a33fb, type: 3}
propertyPath: m_LocalPosition.y
value: 0
value: -10
objectReference: {fileID: 0}
- target: {fileID: 3607988290522455547, guid: dc6d2a9c11b2eac42a960140407a33fb, type: 3}
propertyPath: m_LocalPosition.z
@ -104,7 +104,7 @@ MonoBehaviour:
stick: {fileID: 308684000310606812}
leverBase: {fileID: 1628010112478157667}
rotationAngle: -90
rotationSpeed: 5
rotationSpeed: 1
onActivate:
m_PersistentCalls:
m_Calls: []

View File

@ -11,7 +11,7 @@ GameObject:
- component: {fileID: 3486793746334011244}
- component: {fileID: 3758310539452708709}
m_Layer: 6
m_Name: PlatformTile
m_Name: Tile
m_TagString: Ground
m_Icon: {fileID: 0}
m_NavMeshLayer: 0

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: a444f3cf9e6dc344f925ae4cbe94dac7
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -129,7 +129,7 @@ MonoBehaviour:
lightColor: {x: 1, y: 1, z: 1}
- generateQRCode: 1
linkPrefix: https://wa.me/
suffix: "\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435!
suffix: "\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435!\\n
\u042F \u0432\u044B\u0438\u0433\u0440\u0430\u043B \u0432 \u0438\u0433\u0440\u0435!
\u0416\u0434\u0443 \u0441\u0432\u043E\u0439 \u043F\u0440\u0438\u0437!"
darkColor: {x: 0, y: 0.5, z: 0}

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 MiB

View File

@ -0,0 +1,195 @@
fileFormatVersion: 2
guid: 7acf9de40869d934c9fabcf97dfe90b5
TextureImporter:
internalIDToNameTable:
- first:
213: -1322564667193956839
second: "\u043B\u0435\u0441 1_0"
- first:
213: -8940008131569111771
second: "\u043B\u0435\u0441 1_1"
externalObjects: {}
serializedVersion: 13
mipmaps:
mipMapMode: 0
enableMipMap: 0
sRGBTexture: 1
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
flipGreenChannel: 0
isReadable: 0
streamingMipmaps: 0
streamingMipmapsPriority: 0
vTOnly: 0
ignoreMipmapLimit: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: 1
aniso: 1
mipBias: 0
wrapU: 1
wrapV: 1
wrapW: 1
nPOTScale: 0
lightmap: 0
compressionQuality: 50
spriteMode: 2
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spritePixelsToUnits: 100
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spriteGenerateFallbackPhysicsShape: 1
alphaUsage: 1
alphaIsTransparency: 1
spriteTessellationDetail: -1
textureType: 8
textureShape: 1
singleChannelComponent: 0
flipbookRows: 1
flipbookColumns: 1
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
ignorePngGamma: 0
applyGammaDecoding: 0
swizzle: 50462976
cookieLightType: 0
platformSettings:
- serializedVersion: 4
buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
ignorePlatformSupport: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 4
buildTarget: Standalone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
ignorePlatformSupport: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 4
buildTarget: WebGL
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
ignorePlatformSupport: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 4
buildTarget: WindowsStoreApps
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
ignorePlatformSupport: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
spriteSheet:
serializedVersion: 2
sprites:
- serializedVersion: 2
name: "\u043B\u0435\u0441 1_0"
rect:
serializedVersion: 2
x: 0
y: 0
width: 4000
height: 1930
alignment: 0
pivot: {x: 0, y: 0}
border: {x: 0, y: 0, z: 0, w: 0}
customData:
outline: []
physicsShape: []
tessellationDetail: -1
bones: []
spriteID: 916eb63417e45ade0800000000000000
internalID: -1322564667193956839
vertices: []
indices:
edges: []
weights: []
- serializedVersion: 2
name: "\u043B\u0435\u0441 1_1"
rect:
serializedVersion: 2
x: 3751
y: 1921
width: 20
height: 6
alignment: 0
pivot: {x: 0, y: 0}
border: {x: 0, y: 0, z: 0, w: 0}
customData:
outline: []
physicsShape: []
tessellationDetail: -1
bones: []
spriteID: 52d28b478f5bee380800000000000000
internalID: -8940008131569111771
vertices: []
indices:
edges: []
weights: []
outline: []
customData:
physicsShape: []
bones: []
spriteID:
internalID: 0
vertices: []
indices:
edges: []
weights: []
secondaryTextures: []
spriteCustomMetadata:
entries: []
nameFileIdTable:
"\u043B\u0435\u0441 1_0": -1322564667193956839
"\u043B\u0435\u0441 1_1": -8940008131569111771
mipmapLimitGroupName:
pSDRemoveMatte: 0
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 MiB

View File

@ -0,0 +1,169 @@
fileFormatVersion: 2
guid: 343644e5ca6f0a1459ff6c77736f3659
TextureImporter:
internalIDToNameTable:
- first:
213: -1243276751104078171
second: "\u043D\u0435\u0431\u043E 1_0"
externalObjects: {}
serializedVersion: 13
mipmaps:
mipMapMode: 0
enableMipMap: 0
sRGBTexture: 1
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
flipGreenChannel: 0
isReadable: 0
streamingMipmaps: 0
streamingMipmapsPriority: 0
vTOnly: 0
ignoreMipmapLimit: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: 1
aniso: 1
mipBias: 0
wrapU: 1
wrapV: 1
wrapW: 1
nPOTScale: 0
lightmap: 0
compressionQuality: 50
spriteMode: 2
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spritePixelsToUnits: 100
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spriteGenerateFallbackPhysicsShape: 1
alphaUsage: 1
alphaIsTransparency: 1
spriteTessellationDetail: -1
textureType: 8
textureShape: 1
singleChannelComponent: 0
flipbookRows: 1
flipbookColumns: 1
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
ignorePngGamma: 0
applyGammaDecoding: 0
swizzle: 50462976
cookieLightType: 0
platformSettings:
- serializedVersion: 4
buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
ignorePlatformSupport: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 4
buildTarget: Standalone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
ignorePlatformSupport: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 4
buildTarget: WebGL
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
ignorePlatformSupport: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 4
buildTarget: WindowsStoreApps
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
ignorePlatformSupport: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
spriteSheet:
serializedVersion: 2
sprites:
- serializedVersion: 2
name: "\u043D\u0435\u0431\u043E 1_0"
rect:
serializedVersion: 2
x: 0
y: 0
width: 4000
height: 2048
alignment: 0
pivot: {x: 0, y: 0}
border: {x: 0, y: 0, z: 0, w: 0}
customData:
outline: []
physicsShape: []
tessellationDetail: -1
bones: []
spriteID: 5ae80fcc16efebee0800000000000000
internalID: -1243276751104078171
vertices: []
indices:
edges: []
weights: []
outline: []
customData:
physicsShape: []
bones: []
spriteID:
internalID: 0
vertices: []
indices:
edges: []
weights: []
secondaryTextures: []
spriteCustomMetadata:
entries: []
nameFileIdTable:
"\u043D\u0435\u0431\u043E 1_0": -1243276751104078171
mipmapLimitGroupName:
pSDRemoveMatte: 0
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
using UnityEngine;
using RR.Common.Interfaces;
using RR.Common.Utilities;
using RR.Common.Utility;
using RR.Game2D.Core.Managers;
using RR.Game2D.Behaviour.ScriptableObjects;

View File

@ -9,7 +9,7 @@ namespace RR.Game2D.Entities.Platforms
[SerializeField] private float vanishDuration = 0.5f;
[SerializeField] private float reappearDelay = 2f;
[SerializeField] private float reappearDuration = 0.5f;
[SerializeField, Min(0f)] private float vanishStartDelay = 0.2f;
[SerializeField, Min(0f)] private float vanishStartDelay = 1f;
private CompositeCollider2D _compositeCollider;
private Rigidbody2D _rigidbody;

View File

@ -0,0 +1,93 @@
using System;
using System.Collections;
using RR.Common.Utility;
using UnityEngine;
namespace RR.Game2D.Entities
{
public class TransparentForeground : MonoBehaviour
{
[SerializeField] private float fadeDuration = 0.25f;
[SerializeField, Range(0, 255)] private float targetAlpha = 100f;
private const float STARTING_ALPHA = 255f;
private Collider2D _collider;
private SpriteRenderer[] _spriteRenderers;
private void Awake()
{
_collider = GetComponent<Collider2D>();
if (!_collider.isTrigger) _collider.isTrigger = true;
_spriteRenderers = GetComponentsInChildren<SpriteRenderer>();
if (_collider == null)
Debug.LogWarning($"[{nameof(TransparentForeground)}] Collider2D не найден на {gameObject.name}.");
if (_spriteRenderers == null || _spriteRenderers.Length == 0)
Debug.LogWarning($"[{nameof(TransparentForeground)}] SpriteRenderer'ы не найдены на {gameObject.name} и его дочерних объектах.");
if (_spriteRenderers == null) return;
float initialAlpha = MathUtils.Remap(STARTING_ALPHA, 0f, 255f, 0f, 1f);
foreach (SpriteRenderer spriteRenderer in _spriteRenderers)
{
if (!Mathf.Approximately(spriteRenderer.color.a, initialAlpha))
{
spriteRenderer.color = new Color(spriteRenderer.color.r, spriteRenderer.color.g, spriteRenderer.color.b,
initialAlpha);
}
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (!other.CompareTag("Player")) return;
StopAllCoroutines();
StartCoroutine(Fade(targetAlpha));
}
private void OnTriggerExit2D(Collider2D other)
{
if (!other.CompareTag("Player")) return;
StopAllCoroutines();
StartCoroutine(Fade(STARTING_ALPHA));
}
private IEnumerator Fade(float targetFadeAlpha)
{
float elapsed = 0f;
float targetAlphaNormalized = MathUtils.Remap(targetFadeAlpha, 0f, 255f, 0f, 1f);
// Сохраняем начальные цвета всех спрайтов
Color[] initialColors = new Color[_spriteRenderers.Length];
Color[] targetColors = new Color[_spriteRenderers.Length];
for (int i = 0; i < _spriteRenderers.Length; i++)
{
initialColors[i] = _spriteRenderers[i].color;
targetColors[i] = new Color(initialColors[i].r, initialColors[i].g, initialColors[i].b, targetAlphaNormalized);
}
// Интерполируем все спрайты одновременно
while (elapsed < fadeDuration)
{
elapsed += Time.deltaTime;
float t = elapsed / fadeDuration;
for (int i = 0; i < _spriteRenderers.Length; i++)
{
_spriteRenderers[i].color = Color.Lerp(initialColors[i], targetColors[i], t);
}
yield return null;
}
// Устанавливаем финальные значения
for (int i = 0; i < _spriteRenderers.Length; i++)
{
_spriteRenderers[i].color = targetColors[i];
}
}
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: c0171453a75a9e04494fe951c542410e

View File

@ -111,9 +111,9 @@ namespace RR.Common.Managers
private AudioSource CreateChildAudioSource(string childName, bool loop, AudioMixerGroup output)
{
var go = new GameObject(childName);
GameObject go = new GameObject(childName);
go.transform.SetParent(transform, false);
var src = go.AddComponent<AudioSource>();
AudioSource src = go.AddComponent<AudioSource>();
src.loop = loop;
src.playOnAwake = false;
src.spatialBlend = 0f;
@ -126,21 +126,21 @@ namespace RR.Common.Managers
{
if (sfxPoolParent == null)
{
var t = transform.Find("SFX Pool");
Transform t = transform.Find("SFX Pool");
if (t != null) sfxPoolParent = t;
}
if (sfxPoolParent == null)
{
var go = new GameObject("SFX Pool");
GameObject go = new GameObject("SFX Pool");
sfxPoolParent = go.transform;
sfxPoolParent.SetParent(transform, false);
}
// Если уже есть дети-аудиоисточники — используем их как пул.
sfxPool.Clear();
var existing = sfxPoolParent.GetComponentsInChildren<AudioSource>(includeInactive: true);
foreach (var src in existing)
AudioSource[] existing = sfxPoolParent.GetComponentsInChildren<AudioSource>(includeInactive: true);
foreach (AudioSource src in existing)
{
SetupSfxSource(src);
sfxPool.Add(src);
@ -156,9 +156,9 @@ namespace RR.Common.Managers
private AudioSource CreateSfxSource(string sourceName)
{
var go = new GameObject(sourceName);
GameObject go = new GameObject(sourceName);
go.transform.SetParent(sfxPoolParent, false);
var src = go.AddComponent<AudioSource>();
AudioSource src = go.AddComponent<AudioSource>();
SetupSfxSource(src);
return src;
}
@ -352,7 +352,7 @@ namespace RR.Common.Managers
musicFadeCoroutine = null;
}
private IEnumerator Fade(AudioSource src, float from, float to, float duration)
private static IEnumerator Fade(AudioSource src, float from, float to, float duration)
{
float t = 0f;
src.volume = from;
@ -375,15 +375,13 @@ namespace RR.Common.Managers
for (int i = 0; i < sfxPool.Count; i++)
{
int idx = (sfxRoundRobinIndex + i) % sfxPool.Count;
if (!sfxPool[idx].isPlaying)
{
sfxRoundRobinIndex = (idx + 1) % sfxPool.Count;
return sfxPool[idx];
}
if (sfxPool[idx].isPlaying) continue;
sfxRoundRobinIndex = (idx + 1) % sfxPool.Count;
return sfxPool[idx];
}
// Если все заняты — берём по round-robin (steal)
var src = sfxPool[sfxRoundRobinIndex];
AudioSource src = sfxPool[sfxRoundRobinIndex];
sfxRoundRobinIndex = (sfxRoundRobinIndex + 1) % sfxPool.Count;
return src;
}
@ -407,7 +405,7 @@ namespace RR.Common.Managers
src.Play();
}
float DbFrom01_Log(float t, float minDb = -80f, float maxDb = 20f)
private static float DbFrom01_Log(float t, float minDb = -80f, float maxDb = 20f)
{
t = Mathf.Clamp01(t);
float minAmp = Mathf.Pow(10f, minDb / 20f); // 10^(-80/20) = 0.0001

View File

@ -0,0 +1,94 @@
using UnityEngine;
namespace RR.Common.Utility
{
public static class MathUtils
{
/// <summary>
/// Линейно переносит значение из диапазона [fromMin, fromMax] в диапазон [toMin, toMax].
/// </summary>
/// <param name="value">Исходное значение</param>
/// <param name="fromMin">Минимум исходного диапазона</param>
/// <param name="fromMax">Максимум исходного диапазона</param>
/// <param name="toMin">Минимум целевого диапазона</param>
/// <param name="toMax">Максимум целевого диапазона</param>
/// <returns>Преобразованное значение</returns>
public static float Remap(float value, float fromMin, float fromMax, float toMin, float toMax)
{
// Нормализуем значение в диапазон [0, 1]
float t = (value - fromMin) / (fromMax - fromMin);
// Переносим в целевой диапазон
return Mathf.Lerp(toMin, toMax, t);
}
/// <summary>
/// Линейно переносит значение из диапазона [from] в диапазон [to].
/// </summary>
/// <param name="value">Исходное значение</param>
/// <param name="from">Исходный диапазон (x - минимум, y - максимум)</param>
/// <param name="to">Целевой диапазон (x - минимум, y - максимум)</param>
/// <returns>Преобразованное значение</returns>
public static float Remap(float value, Vector2 from, Vector2 to)
{
float t = (value - from.x) / (from.y - from.x);
return Mathf.Lerp(to.x, to.y, t);
}
/// <summary>
/// Линейно переносит значение из диапазона [fromMin, fromMax] в диапазон [toMin, toMax].
/// </summary>
/// <param name="value">Исходное значение</param>
/// <param name="fromMin">Минимум исходного диапазона</param>
/// <param name="fromMax">Максимум исходного диапазона</param>
/// <param name="toMin">Минимум целевого диапазона</param>
/// <param name="toMax">Максимум целевого диапазона</param>
/// <returns>Преобразованное значение (округленное)</returns>
public static int Remap(int value, int fromMin, int fromMax, int toMin, int toMax)
{
float t = (float)(value - fromMin) / (fromMax - fromMin);
return Mathf.RoundToInt(Mathf.Lerp(toMin, toMax, t));
}
/// <summary>
/// Линейно переносит значение из диапазона [from] в диапазон [to].
/// </summary>
/// <param name="value">Исходное значение</param>
/// <param name="from">Исходный диапазон (x - минимум, y - максимум)</param>
/// <param name="to">Целевой диапазон (x - минимум, y - максимум)</param>
/// <returns>Преобразованное значение (округленное)</returns>
public static int Remap(int value, Vector2Int from, Vector2Int to)
{
float t = (float)(value - from.x) / (from.y - from.x);
return Mathf.RoundToInt(Mathf.Lerp(to.x, to.y, t));
}
/// <summary>
/// Линейно переносит значение из диапазона [fromMin, fromMax] в диапазон [toMin, toMax].
/// </summary>
/// <param name="value">Исходное значение</param>
/// <param name="fromMin">Минимум исходного диапазона</param>
/// <param name="fromMax">Максимум исходного диапазона</param>
/// <param name="toMin">Минимум целевого диапазона</param>
/// <param name="toMax">Максимум целевого диапазона</param>
/// <returns>Преобразованное значение</returns>
public static double Remap(double value, double fromMin, double fromMax, double toMin, double toMax)
{
double t = (value - fromMin) / (fromMax - fromMin);
return toMin + (toMax - toMin) * t;
}
/// <summary>
/// Линейно переносит значение из диапазона [from] в диапазон [to].
/// </summary>
/// <param name="value">Исходное значение</param>
/// <param name="from">Исходный диапазон (x - минимум, y - максимум)</param>
/// <param name="to">Целевой диапазон (x - минимум, y - максимум)</param>
/// <returns>Преобразованное значение</returns>
public static double Remap(double value, Vector2 from, Vector2 to)
{
double t = (value - from.x) / (from.y - from.x);
return to.x + (to.y - to.x) * t;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0a04388923b9489da48d16ad42749d60
timeCreated: 1764226775

View File

@ -6,7 +6,7 @@ using UnityEditor;
#endif
namespace RR.Common.Utilities
namespace RR.Common.Utility
{
public class ReadOnlyInInspectorAttribute : PropertyAttribute { }