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