| | 1 | | using System; |
| | 2 | | using DCL; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace NFTShape_Internal |
| | 6 | | { |
| | 7 | | public class NFTShapeHQImageConfig |
| | 8 | | { |
| | 9 | | public string name; |
| | 10 | | public string imageUrl; |
| | 11 | | public Transform transform; |
| | 12 | | public Collider collider; |
| | 13 | | public INFTAsset asset; |
| | 14 | | } |
| | 15 | |
|
| | 16 | | public class NFTShapeHQImageHandler : IDisposable |
| | 17 | | { |
| | 18 | | public static bool VERBOSE = false; |
| | 19 | |
|
| | 20 | | readonly NFTShapeHQImageConfig hqImageConfig; |
| | 21 | | readonly INFTAsset asset; |
| | 22 | | readonly Camera camera; |
| | 23 | | readonly Transform nftControllerT; |
| | 24 | |
|
| | 25 | | bool isPlayerNear; |
| | 26 | | bool isCameraInFront; |
| | 27 | | bool isPlayerLooking; |
| | 28 | |
|
| | 29 | | public static NFTShapeHQImageHandler Create(NFTShapeHQImageConfig config) |
| | 30 | | { |
| 9 | 31 | | if (config.asset == null || !config.collider || !config.transform) |
| | 32 | | { |
| 9 | 33 | | return null; |
| | 34 | | } |
| | 35 | | /** |
| | 36 | | * We dont have HQ images now. |
| | 37 | | * OpenSEA returns always the best image, there are no thumbnails images. |
| | 38 | | * BUT we keep this function, so when we add a transform image worker so we can resize them, |
| | 39 | | * we can use this mechanism again. |
| | 40 | | */ |
| | 41 | | return null; // new NFTShapeHQImageHandler(config); |
| | 42 | | } |
| | 43 | |
|
| | 44 | | public void Dispose() |
| | 45 | | { |
| 0 | 46 | | CommonScriptableObjects.playerUnityPosition.OnChange -= OnPlayerPositionChanged; |
| 0 | 47 | | asset.Dispose(); |
| 0 | 48 | | } |
| | 49 | |
|
| | 50 | | public void Update() |
| | 51 | | { |
| 0 | 52 | | if (!hqImageConfig.collider) |
| 0 | 53 | | return; |
| | 54 | |
|
| 0 | 55 | | if (!isPlayerNear) |
| 0 | 56 | | return; |
| | 57 | |
|
| 0 | 58 | | var config = DataStore.i.Get<DataStore_NFTShape>(); |
| | 59 | |
|
| 0 | 60 | | isCameraInFront = camera == null || |
| | 61 | | Vector3.Dot(nftControllerT.forward, |
| | 62 | | nftControllerT.position - camera.transform.position) |
| | 63 | | > config.hqImgInFrontDotProdMinValue; |
| | 64 | |
|
| 0 | 65 | | if (VERBOSE) |
| | 66 | | { |
| 0 | 67 | | Debug.Log($"Camera is in front of {hqImageConfig.name}? {isCameraInFront}"); |
| | 68 | | } |
| | 69 | |
|
| 0 | 70 | | if (!isCameraInFront) |
| | 71 | | { |
| 0 | 72 | | RestorePreviewTextureIfInHQ(); |
| 0 | 73 | | return; |
| | 74 | | } |
| | 75 | |
|
| 0 | 76 | | isPlayerLooking = camera == null || |
| | 77 | | Vector3.Dot(nftControllerT.forward, camera.transform.forward) >= |
| | 78 | | config.hqImgFacingDotProdMinValue; |
| | 79 | |
|
| 0 | 80 | | if (VERBOSE) |
| | 81 | | { |
| 0 | 82 | | Debug.Log($"Player is looking at {hqImageConfig.name}? {isPlayerLooking}"); |
| | 83 | | } |
| | 84 | |
|
| 0 | 85 | | if (isPlayerLooking) |
| | 86 | | { |
| 0 | 87 | | FetchHQTexture(); |
| | 88 | | } |
| | 89 | | else |
| | 90 | | { |
| 0 | 91 | | RestorePreviewTextureIfInHQ(); |
| | 92 | | } |
| 0 | 93 | | } |
| | 94 | |
|
| 0 | 95 | | private NFTShapeHQImageHandler(NFTShapeHQImageConfig hqImageConfig) |
| | 96 | | { |
| 0 | 97 | | this.hqImageConfig = hqImageConfig; |
| 0 | 98 | | this.asset = hqImageConfig.asset; |
| | 99 | |
|
| 0 | 100 | | camera = Camera.main; |
| 0 | 101 | | nftControllerT = hqImageConfig.transform; |
| | 102 | |
|
| 0 | 103 | | CommonScriptableObjects.playerUnityPosition.OnChange += OnPlayerPositionChanged; |
| 0 | 104 | | OnPlayerPositionChanged(CommonScriptableObjects.playerUnityPosition, Vector3.zero); |
| 0 | 105 | | } |
| | 106 | |
|
| | 107 | | private void OnPlayerPositionChanged(Vector3 current, Vector3 prev) |
| | 108 | | { |
| 0 | 109 | | isPlayerNear = false; |
| | 110 | |
|
| 0 | 111 | | if (!hqImageConfig.transform || !hqImageConfig.collider) |
| 0 | 112 | | return; |
| | 113 | |
|
| 0 | 114 | | var config = DataStore.i.Get<DataStore_NFTShape>(); |
| | 115 | |
|
| 0 | 116 | | isPlayerNear = ((current - hqImageConfig.collider.ClosestPoint(current)).sqrMagnitude |
| | 117 | | <= (config.hqImgMinDistance * |
| | 118 | | config.hqImgMinDistance)); |
| | 119 | |
|
| 0 | 120 | | if (!isPlayerNear) |
| | 121 | | { |
| 0 | 122 | | RestorePreviewTextureIfInHQ(); |
| | 123 | | } |
| | 124 | |
|
| 0 | 125 | | if (VERBOSE) |
| | 126 | | { |
| 0 | 127 | | Debug.Log($"Player position relative to {hqImageConfig.name} is near? {isPlayerNear}"); |
| | 128 | | } |
| 0 | 129 | | } |
| | 130 | |
|
| | 131 | | private void FetchHQTexture() |
| | 132 | | { |
| 0 | 133 | | if (asset.isHQ) |
| 0 | 134 | | return; |
| | 135 | |
|
| 0 | 136 | | Action debugSuccess = null; |
| 0 | 137 | | Action<Exception> debugFail = null; |
| | 138 | |
|
| 0 | 139 | | if (VERBOSE) |
| | 140 | | { |
| 0 | 141 | | debugSuccess = () => Debug.Log($"Success: Fetch {hqImageConfig.name} HQ image"); |
| 0 | 142 | | debugFail = error => Debug.Log($"Fail: Fetch {hqImageConfig.name} HQ image, Exception: {error}"); |
| | 143 | | } |
| | 144 | |
|
| | 145 | | // TODO(Brian): Asset is not supposed to fetch. Move this fetching mechanism to this class or elsewhere. |
| 0 | 146 | | asset.FetchAndSetHQAsset(hqImageConfig.imageUrl, debugSuccess, debugFail); |
| | 147 | |
|
| 0 | 148 | | if (VERBOSE) |
| | 149 | | { |
| 0 | 150 | | Debug.Log($"Fetch {hqImageConfig.name} HQ image"); |
| | 151 | | } |
| 0 | 152 | | } |
| | 153 | |
|
| | 154 | | private void RestorePreviewTexture() |
| | 155 | | { |
| 0 | 156 | | asset.RestorePreviewAsset(); |
| 0 | 157 | | if (VERBOSE) |
| | 158 | | { |
| 0 | 159 | | Debug.Log($"Restore {hqImageConfig.name} preview image"); |
| | 160 | | } |
| 0 | 161 | | } |
| | 162 | |
|
| | 163 | | private void RestorePreviewTextureIfInHQ() |
| | 164 | | { |
| 0 | 165 | | if (!asset.isHQ) |
| 0 | 166 | | return; |
| | 167 | |
|
| 0 | 168 | | RestorePreviewTexture(); |
| 0 | 169 | | } |
| | 170 | | } |
| | 171 | | } |