| | 1 | | using UnityEngine; |
| | 2 | |
|
| | 3 | | namespace DCL |
| | 4 | | { |
| | 5 | | public static class AvatarRendererHelpers |
| | 6 | | { |
| 0 | 7 | | private static readonly int IMPOSTOR_TEXTURE_PROPERTY = Shader.PropertyToID("_BaseMap"); |
| 0 | 8 | | private static readonly int IMPOSTOR_TEXTURE_COLOR_PROPERTY = Shader.PropertyToID("_BaseColor"); |
| | 9 | |
|
| | 10 | | // Manually tweaked values |
| 0 | 11 | | public static readonly float IMPOSTOR_MOVEMENT_INTERPOLATION = 1.79f; |
| | 12 | | private const float IMPOSTOR_TINT_MIN_DISTANCE = 30f; |
| | 13 | | private const float IMPOSTOR_TINT_MAX_DISTANCE = 81.33f; |
| | 14 | | private const float IMPOSTOR_TINT_NEAREST_BLACKNESS = 0f; |
| | 15 | | private const float IMPOSTOR_TINT_FAREST_BLACKNESS = 0.74f; |
| | 16 | | private const float IMPOSTOR_ALPHA_NEAREST_VALUE = 1f; |
| | 17 | | private const float IMPOSTOR_ALPHA_FAREST_VALUE = 1f; |
| | 18 | |
|
| | 19 | | // 2048x2048 atlas with 8 512x1024 snapshot-sprites |
| | 20 | | private const int GENERIC_IMPOSTORS_ATLAS_COLUMNS = 4; |
| | 21 | | private const int GENERIC_IMPOSTORS_ATLAS_ROWS = 2; |
| | 22 | |
|
| | 23 | | public static void RandomizeAndApplyGenericImpostor(Mesh impostorMesh) |
| | 24 | | { |
| 0 | 25 | | Vector2 spriteSize = new Vector2(1f / GENERIC_IMPOSTORS_ATLAS_COLUMNS, 1f / GENERIC_IMPOSTORS_ATLAS_ROWS); |
| 0 | 26 | | float randomUVXPos = Random.Range(0, GENERIC_IMPOSTORS_ATLAS_COLUMNS) * spriteSize.x; |
| 0 | 27 | | float randomUVYPos = Random.Range(0, GENERIC_IMPOSTORS_ATLAS_ROWS) * spriteSize.y; |
| | 28 | |
|
| 0 | 29 | | Vector2[] uvs = new Vector2[4]; // Quads have only 4 vertices |
| 0 | 30 | | uvs[0].Set(randomUVXPos, randomUVYPos); |
| 0 | 31 | | uvs[1].Set(randomUVXPos + spriteSize.x, randomUVYPos); |
| 0 | 32 | | uvs[2].Set(randomUVXPos, randomUVYPos + spriteSize.y); |
| 0 | 33 | | uvs[3].Set(randomUVXPos + spriteSize.x, randomUVYPos + spriteSize.y); |
| 0 | 34 | | impostorMesh.uv = uvs; |
| 0 | 35 | | } |
| | 36 | |
|
| | 37 | | private static void ResetImpostorMeshUVs(Mesh impostorMesh) |
| | 38 | | { |
| 0 | 39 | | Vector2[] uvs = new Vector2[4]; // Quads have only 4 vertices |
| 0 | 40 | | uvs[0].Set(0, 0); |
| 0 | 41 | | uvs[1].Set(1, 0); |
| 0 | 42 | | uvs[2].Set(0, 1); |
| 0 | 43 | | uvs[3].Set(1, 1); |
| 0 | 44 | | impostorMesh.uv = uvs; |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | public static void SetImpostorTexture(Texture2D impostorTexture, Mesh impostorMesh, Material impostorMaterial) |
| | 48 | | { |
| 0 | 49 | | if (impostorTexture == null) |
| 0 | 50 | | return; |
| | 51 | |
|
| 0 | 52 | | ResetImpostorMeshUVs(impostorMesh); |
| | 53 | |
|
| 0 | 54 | | impostorMaterial.SetTexture(IMPOSTOR_TEXTURE_PROPERTY, impostorTexture); |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | public static void SetImpostorTintColor(Material impostorMaterial, Color newColor) |
| | 58 | | { |
| 0 | 59 | | if (impostorMaterial == null) |
| 0 | 60 | | return; |
| | 61 | |
|
| 0 | 62 | | impostorMaterial.SetColor(IMPOSTOR_TEXTURE_COLOR_PROPERTY, newColor); |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | public static Color CalculateImpostorTint(float distanceToMainPlayer) |
| | 66 | | { |
| 0 | 67 | | float initialStep = Mathf.Max(IMPOSTOR_TINT_MIN_DISTANCE, distanceToMainPlayer); |
| 0 | 68 | | float tintStep = Mathf.InverseLerp(IMPOSTOR_TINT_MIN_DISTANCE, IMPOSTOR_TINT_MAX_DISTANCE, initialStep); |
| 0 | 69 | | float tintValue = Mathf.Lerp(IMPOSTOR_TINT_NEAREST_BLACKNESS, IMPOSTOR_TINT_FAREST_BLACKNESS, tintStep); |
| 0 | 70 | | Color newColor = Color.Lerp(Color.white, Color.black, tintValue); |
| 0 | 71 | | newColor.a = Mathf.Lerp(IMPOSTOR_ALPHA_NEAREST_VALUE, IMPOSTOR_ALPHA_FAREST_VALUE, tintStep); |
| | 72 | |
|
| 0 | 73 | | return newColor; |
| | 74 | | } |
| | 75 | | } |
| | 76 | | } |