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