| | 1 | | using System; |
| | 2 | | using DCL.Helpers.NFT; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace NFTShape_Internal |
| | 6 | | { |
| | 7 | | public class NFTShapeHQImageConfig |
| | 8 | | { |
| | 9 | | public NFTInfo nftInfo; |
| | 10 | | public NFTShapeConfig nftConfig; |
| | 11 | | public NFTShapeLoaderController controller; |
| | 12 | | public INFTAsset asset; |
| | 13 | | } |
| | 14 | |
|
| | 15 | | public class NFTShapeHQImageHandler : IDisposable |
| | 16 | | { |
| | 17 | | readonly NFTShapeHQImageConfig config; |
| | 18 | | readonly INFTAsset asset; |
| | 19 | | readonly Camera camera; |
| | 20 | | readonly Transform nftControllerT; |
| | 21 | |
|
| | 22 | | bool isPlayerNear; |
| | 23 | | bool isCameraInFront; |
| | 24 | | bool isPlayerLooking; |
| | 25 | |
|
| | 26 | | public static NFTShapeHQImageHandler Create(NFTShapeHQImageConfig config) |
| | 27 | | { |
| 13 | 28 | | if (config.asset == null || config.controller == null) |
| | 29 | | { |
| 2 | 30 | | return null; |
| | 31 | | } |
| | 32 | |
|
| 11 | 33 | | return new NFTShapeHQImageHandler(config); |
| | 34 | | } |
| | 35 | |
|
| | 36 | | public void Dispose() |
| | 37 | | { |
| 7 | 38 | | CommonScriptableObjects.playerUnityPosition.OnChange -= OnPlayerPositionChanged; |
| 7 | 39 | | asset.Dispose(); |
| 7 | 40 | | } |
| | 41 | |
|
| | 42 | | public void Update() |
| | 43 | | { |
| 25 | 44 | | if (config.controller.collider is null) |
| 0 | 45 | | return; |
| | 46 | |
|
| 25 | 47 | | if (!isPlayerNear) |
| 18 | 48 | | return; |
| | 49 | |
|
| 7 | 50 | | isCameraInFront = camera == null || |
| | 51 | | Vector3.Dot(nftControllerT.forward, |
| | 52 | | nftControllerT.position - camera.transform.position) |
| | 53 | | > config.nftConfig.hqImgInFrontDotProdMinValue; |
| | 54 | |
|
| 7 | 55 | | if (config.nftConfig.verbose) |
| | 56 | | { |
| 0 | 57 | | Debug.Log($"Camera is in front of {config.nftInfo.name}? {isCameraInFront}"); |
| | 58 | | } |
| | 59 | |
|
| 7 | 60 | | if (!isCameraInFront) |
| | 61 | | { |
| 1 | 62 | | RestorePreviewTextureIfInHQ(); |
| 1 | 63 | | return; |
| | 64 | | } |
| | 65 | |
|
| 6 | 66 | | isPlayerLooking = camera == null || |
| | 67 | | Vector3.Dot(nftControllerT.forward, camera.transform.forward) >= |
| | 68 | | config.nftConfig.hqImgFacingDotProdMinValue; |
| | 69 | |
|
| 6 | 70 | | if (config.nftConfig.verbose) |
| | 71 | | { |
| 0 | 72 | | Debug.Log($"Player is looking at {config.nftInfo.name}? {isPlayerLooking}"); |
| | 73 | | } |
| | 74 | |
|
| 6 | 75 | | if (isPlayerLooking) |
| | 76 | | { |
| 5 | 77 | | FetchHQTexture(); |
| 5 | 78 | | } |
| | 79 | | else |
| | 80 | | { |
| 1 | 81 | | RestorePreviewTextureIfInHQ(); |
| | 82 | | } |
| 1 | 83 | | } |
| | 84 | |
|
| 11 | 85 | | private NFTShapeHQImageHandler(NFTShapeHQImageConfig config) |
| | 86 | | { |
| 11 | 87 | | this.config = config; |
| 11 | 88 | | this.asset = config.asset; |
| | 89 | |
|
| 11 | 90 | | camera = Camera.main; |
| 11 | 91 | | nftControllerT = config.controller.transform; |
| | 92 | |
|
| 11 | 93 | | CommonScriptableObjects.playerUnityPosition.OnChange += OnPlayerPositionChanged; |
| 11 | 94 | | OnPlayerPositionChanged(CommonScriptableObjects.playerUnityPosition, Vector3.zero); |
| 11 | 95 | | } |
| | 96 | |
|
| | 97 | | private void OnPlayerPositionChanged(Vector3 current, Vector3 prev) |
| | 98 | | { |
| 5632 | 99 | | isPlayerNear = false; |
| | 100 | |
|
| 5632 | 101 | | if (config.controller == null || config.controller.collider == null) |
| 5612 | 102 | | return; |
| | 103 | |
|
| 20 | 104 | | isPlayerNear = ((current - config.controller.collider.ClosestPoint(current)).sqrMagnitude |
| | 105 | | <= (config.nftConfig.hqImgMinDistance * config.nftConfig.hqImgMinDistance)); |
| | 106 | |
|
| 20 | 107 | | if (!isPlayerNear) |
| | 108 | | { |
| 15 | 109 | | RestorePreviewTextureIfInHQ(); |
| | 110 | | } |
| | 111 | |
|
| 20 | 112 | | if (config.nftConfig.verbose) |
| | 113 | | { |
| 0 | 114 | | Debug.Log($"Player position relative to {config.nftInfo.name} is near? {isPlayerNear}"); |
| | 115 | | } |
| 20 | 116 | | } |
| | 117 | |
|
| | 118 | | private void FetchHQTexture() |
| | 119 | | { |
| 5 | 120 | | if (asset.isHQ) |
| 0 | 121 | | return; |
| | 122 | |
|
| 5 | 123 | | string url = $"{config.nftInfo.imageUrl}=s{asset.hqResolution}"; |
| | 124 | |
|
| 5 | 125 | | Action debugSuccess = null; |
| 5 | 126 | | Action debugFail = null; |
| | 127 | |
|
| 5 | 128 | | if (config.nftConfig.verbose) |
| | 129 | | { |
| 0 | 130 | | debugSuccess = () => Debug.Log($"Success: Fetch {config.nftInfo.name} HQ image"); |
| 0 | 131 | | debugFail = () => Debug.Log($"Fail: Fetch {config.nftInfo.name} HQ image"); |
| | 132 | | } |
| | 133 | |
|
| 5 | 134 | | asset.FetchAndSetHQAsset(url, debugSuccess, debugFail); |
| | 135 | |
|
| 5 | 136 | | if (config.nftConfig.verbose) |
| | 137 | | { |
| 0 | 138 | | Debug.Log($"Fetch {config.nftInfo.name} HQ image"); |
| | 139 | | } |
| 5 | 140 | | } |
| | 141 | |
|
| | 142 | | private void RestorePreviewTexture() |
| | 143 | | { |
| 4 | 144 | | asset.RestorePreviewAsset(); |
| 4 | 145 | | if (config.nftConfig.verbose) |
| | 146 | | { |
| 0 | 147 | | Debug.Log($"Restore {config.nftInfo.name} preview image"); |
| | 148 | | } |
| 4 | 149 | | } |
| | 150 | |
|
| | 151 | | private void RestorePreviewTextureIfInHQ() |
| | 152 | | { |
| 17 | 153 | | if (!asset.isHQ) |
| 13 | 154 | | return; |
| | 155 | |
|
| 4 | 156 | | RestorePreviewTexture(); |
| 4 | 157 | | } |
| | 158 | | } |
| | 159 | | } |