| | 1 | | using UnityEngine; |
| | 2 | |
|
| | 3 | | namespace DCL |
| | 4 | | { |
| | 5 | | public static class AvatarRendererHelpers |
| | 6 | | { |
| 1 | 7 | | private static readonly int IMPOSTOR_TEXTURE_PROPERTY = Shader.PropertyToID("_BaseMap"); |
| 1 | 8 | | private static readonly int IMPOSTOR_TEXTURE_COLOR_PROPERTY = Shader.PropertyToID("_BaseColor"); |
| | 9 | |
|
| | 10 | | // Manually tweaked values |
| 1 | 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 | |
|
| | 17 | | // 2048x2048 atlas with 8 512x1024 snapshot-sprites |
| | 18 | | private const int GENERIC_IMPOSTORS_ATLAS_COLUMNS = 4; |
| | 19 | | private const int GENERIC_IMPOSTORS_ATLAS_ROWS = 2; |
| | 20 | |
|
| 1 | 21 | | private static Texture2D genericImpostorsTexture = Resources.Load<Texture2D>("Textures/avatar-impostors-atlas"); |
| 1 | 22 | | private static Texture2D transparentPixelTexture = Resources.Load<Texture2D>("Textures/transparent-pixel"); |
| | 23 | |
|
| | 24 | | public static void RandomizeAndApplyGenericImpostor(Mesh impostorMesh, Material impostorMaterial) |
| | 25 | | { |
| 0 | 26 | | SetImpostorTexture(genericImpostorsTexture, impostorMesh, impostorMaterial); |
| | 27 | |
|
| 0 | 28 | | Vector2 spriteSize = new Vector2(1f / GENERIC_IMPOSTORS_ATLAS_COLUMNS, 1f / GENERIC_IMPOSTORS_ATLAS_ROWS); |
| 0 | 29 | | float randomUVXPos = Random.Range(0, GENERIC_IMPOSTORS_ATLAS_COLUMNS) * spriteSize.x; |
| 0 | 30 | | float randomUVYPos = Random.Range(0, GENERIC_IMPOSTORS_ATLAS_ROWS) * spriteSize.y; |
| | 31 | |
|
| 0 | 32 | | Vector2[] uvs = new Vector2[4]; // Quads have only 4 vertices |
| 0 | 33 | | uvs[0].Set(randomUVXPos, randomUVYPos); |
| 0 | 34 | | uvs[1].Set(randomUVXPos + spriteSize.x, randomUVYPos); |
| 0 | 35 | | uvs[2].Set(randomUVXPos, randomUVYPos + spriteSize.y); |
| 0 | 36 | | uvs[3].Set(randomUVXPos + spriteSize.x, randomUVYPos + spriteSize.y); |
| 0 | 37 | | impostorMesh.uv = uvs; |
| 0 | 38 | | } |
| | 39 | |
|
| | 40 | | private static void ResetImpostorMeshUVs(Mesh impostorMesh) |
| | 41 | | { |
| 1327 | 42 | | Vector2[] uvs = new Vector2[4]; // Quads have only 4 vertices |
| 1327 | 43 | | uvs[0].Set(0, 0); |
| 1327 | 44 | | uvs[1].Set(1, 0); |
| 1327 | 45 | | uvs[2].Set(0, 1); |
| 1327 | 46 | | uvs[3].Set(1, 1); |
| 1327 | 47 | | impostorMesh.uv = uvs; |
| 1327 | 48 | | } |
| | 49 | |
|
| | 50 | | public static void SetImpostorTexture(Texture2D impostorTexture, Mesh impostorMesh, Material impostorMaterial) |
| | 51 | | { |
| 1327 | 52 | | if (TextureUtils.IsQuestionMarkPNG(impostorTexture)) |
| 0 | 53 | | return; |
| | 54 | |
|
| 1327 | 55 | | ResetImpostorMeshUVs(impostorMesh); |
| | 56 | |
|
| 1327 | 57 | | impostorMaterial.SetTexture(IMPOSTOR_TEXTURE_PROPERTY, impostorTexture); |
| 1327 | 58 | | } |
| | 59 | |
|
| | 60 | | /// <summary> |
| | 61 | | /// Sets impostor texture tint color ignoring the alpha channel |
| | 62 | | /// </summary> |
| | 63 | | public static void SetImpostorTintColor(Material impostorMaterial, Color newColor) |
| | 64 | | { |
| 0 | 65 | | newColor.a = impostorMaterial.GetColor(IMPOSTOR_TEXTURE_COLOR_PROPERTY).a; |
| 0 | 66 | | impostorMaterial.SetColor(IMPOSTOR_TEXTURE_COLOR_PROPERTY, newColor); |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | public static Color CalculateImpostorTint(float distanceToMainPlayer) |
| | 70 | | { |
| 0 | 71 | | float initialStep = Mathf.Max(IMPOSTOR_TINT_MIN_DISTANCE, distanceToMainPlayer); |
| 0 | 72 | | float tintStep = Mathf.InverseLerp(IMPOSTOR_TINT_MIN_DISTANCE, IMPOSTOR_TINT_MAX_DISTANCE, initialStep); |
| 0 | 73 | | float tintValue = Mathf.Lerp(IMPOSTOR_TINT_NEAREST_BLACKNESS, IMPOSTOR_TINT_FAREST_BLACKNESS, tintStep); |
| 0 | 74 | | Color newColor = Color.Lerp(Color.white, Color.black, tintValue); |
| | 75 | |
|
| 0 | 76 | | return newColor; |
| | 77 | | } |
| | 78 | |
|
| 2654 | 79 | | public static void ResetImpostor(Mesh impostorMesh, Material impostorMaterial) { SetImpostorTexture(transparentP |
| | 80 | | } |
| | 81 | | } |