| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using DCL.Models; |
| | 5 | | using UnityEngine; |
| | 6 | | using DCL.Components.Video.Plugin; |
| | 7 | | using DCL.Helpers; |
| | 8 | |
|
| | 9 | | namespace DCL.Components |
| | 10 | | { |
| | 11 | | public class DCLVideoTexture : DCLTexture |
| | 12 | | { |
| | 13 | | #if UNITY_EDITOR |
| 1 | 14 | | internal static bool isTest = true; |
| | 15 | | #else |
| | 16 | | internal static bool isTest = false; |
| | 17 | | #endif |
| | 18 | |
|
| | 19 | | private const float OUTOFSCENE_TEX_UPDATE_INTERVAL = 1.5f; |
| | 20 | |
|
| | 21 | | [System.Serializable] |
| | 22 | | new public class Model : BaseModel |
| | 23 | | { |
| | 24 | | public string videoClipId; |
| | 25 | | public bool playing = false; |
| 33 | 26 | | public float volume = 1f; |
| 33 | 27 | | public float playbackRate = 1f; |
| | 28 | | public bool loop = false; |
| 33 | 29 | | public float seek = -1; |
| | 30 | | public BabylonWrapMode wrap = BabylonWrapMode.CLAMP; |
| 33 | 31 | | public FilterMode samplingMode = FilterMode.Bilinear; |
| | 32 | |
|
| 11 | 33 | | public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); } |
| | 34 | | } |
| | 35 | |
|
| | 36 | | internal WebVideoPlayer texturePlayer; |
| | 37 | | private Coroutine texturePlayerUpdateRoutine; |
| | 38 | | private float baseVolume; |
| 11 | 39 | | private float distanceVolumeModifier = 1f; |
| | 40 | | private bool isPlayStateDirty = false; |
| | 41 | | internal bool isVisible = false; |
| | 42 | |
|
| 11 | 43 | | private bool isPlayerInScene = true; |
| 11 | 44 | | private float currUpdateIntervalTime = OUTOFSCENE_TEX_UPDATE_INTERVAL; |
| | 45 | |
|
| 11 | 46 | | internal Dictionary<string, MaterialInfo> attachedMaterials = new Dictionary<string, MaterialInfo>(); |
| | 47 | |
|
| 33 | 48 | | public DCLVideoTexture() { model = new Model(); } |
| | 49 | |
|
| | 50 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 51 | | { |
| 22 | 52 | | yield return new WaitUntil(() => CommonScriptableObjects.rendererState.Get()); |
| | 53 | |
|
| | 54 | | //If the scene creates and destroy the component before our renderer has been turned on bad things happen! |
| | 55 | | //TODO: Analyze if we can catch this upstream and stop the IEnumerator |
| 11 | 56 | | if (isDisposed) |
| 0 | 57 | | yield break; |
| | 58 | |
|
| 11 | 59 | | var model = (Model) newModel; |
| | 60 | |
|
| 11 | 61 | | unitySamplingMode = model.samplingMode; |
| | 62 | |
|
| 11 | 63 | | switch (model.wrap) |
| | 64 | | { |
| | 65 | | case BabylonWrapMode.CLAMP: |
| 11 | 66 | | unityWrap = TextureWrapMode.Clamp; |
| 11 | 67 | | break; |
| | 68 | | case BabylonWrapMode.WRAP: |
| 0 | 69 | | unityWrap = TextureWrapMode.Repeat; |
| 0 | 70 | | break; |
| | 71 | | case BabylonWrapMode.MIRROR: |
| 0 | 72 | | unityWrap = TextureWrapMode.Mirror; |
| | 73 | | break; |
| | 74 | | } |
| | 75 | |
|
| 11 | 76 | | if (texturePlayer == null) |
| | 77 | | { |
| 11 | 78 | | DCLVideoClip dclVideoClip = scene.GetSharedComponent(model.videoClipId) as DCLVideoClip; |
| | 79 | |
|
| 11 | 80 | | if (dclVideoClip == null) |
| | 81 | | { |
| 0 | 82 | | Debug.LogError("Wrong video clip type when playing VideoTexture!!"); |
| 0 | 83 | | yield break; |
| | 84 | | } |
| | 85 | |
|
| 11 | 86 | | string videoId = (!string.IsNullOrEmpty(scene.sceneData.id)) ? scene.sceneData.id + id : scene.GetHashCo |
| 11 | 87 | | texturePlayer = new WebVideoPlayer(videoId, dclVideoClip.GetUrl(), dclVideoClip.isStream); |
| 11 | 88 | | texturePlayerUpdateRoutine = CoroutineStarter.Start(VideoTextureUpdate()); |
| 11 | 89 | | CommonScriptableObjects.playerCoords.OnChange += OnPlayerCoordsChanged; |
| 11 | 90 | | CommonScriptableObjects.sceneID.OnChange += OnSceneIDChanged; |
| 11 | 91 | | scene.OnEntityRemoved += OnEntityRemoved; |
| 11 | 92 | | Settings.i.OnGeneralSettingsChanged += OnSettingsChanged; |
| | 93 | |
|
| 11 | 94 | | OnSceneIDChanged(CommonScriptableObjects.sceneID.Get(), null); |
| | 95 | | } |
| | 96 | |
|
| | 97 | | // NOTE: create texture for testing cause real texture will only be created on web platform |
| 11 | 98 | | if (isTest) |
| | 99 | | { |
| 11 | 100 | | if (texture == null) |
| 11 | 101 | | texture = new Texture2D(1, 1); |
| | 102 | | } |
| | 103 | |
|
| 11 | 104 | | if (texture == null) |
| | 105 | | { |
| 0 | 106 | | while (texturePlayer.texture == null && !texturePlayer.isError) |
| | 107 | | { |
| 0 | 108 | | yield return null; |
| | 109 | | } |
| | 110 | |
|
| 0 | 111 | | if (texturePlayer.isError) |
| | 112 | | { |
| 0 | 113 | | if (texturePlayerUpdateRoutine != null) |
| | 114 | | { |
| 0 | 115 | | CoroutineStarter.Stop(texturePlayerUpdateRoutine); |
| 0 | 116 | | texturePlayerUpdateRoutine = null; |
| | 117 | | } |
| | 118 | |
|
| 0 | 119 | | yield break; |
| | 120 | | } |
| | 121 | |
|
| 0 | 122 | | texture = texturePlayer.texture; |
| 0 | 123 | | isPlayStateDirty = true; |
| | 124 | | } |
| | 125 | |
|
| 11 | 126 | | if (texturePlayer != null) |
| | 127 | | { |
| 11 | 128 | | if (model.seek >= 0) |
| | 129 | | { |
| 0 | 130 | | texturePlayer.SetTime(model.seek); |
| 0 | 131 | | model.seek = -1; |
| | 132 | |
|
| | 133 | | // Applying seek is not immediate |
| 0 | 134 | | yield return null; |
| | 135 | | } |
| | 136 | |
|
| 11 | 137 | | if (model.playing) |
| 0 | 138 | | texturePlayer.Play(); |
| | 139 | | else |
| 11 | 140 | | texturePlayer.Pause(); |
| | 141 | |
|
| 11 | 142 | | if (baseVolume != model.volume) |
| | 143 | | { |
| 11 | 144 | | baseVolume = model.volume; |
| 11 | 145 | | UpdateVolume(); |
| | 146 | | } |
| | 147 | |
|
| 11 | 148 | | texturePlayer.SetPlaybackRate(model.playbackRate); |
| 11 | 149 | | texturePlayer.SetLoop(model.loop); |
| | 150 | | } |
| 11 | 151 | | } |
| | 152 | |
|
| 0 | 153 | | public float GetVolume() { return ((Model) model).volume; } |
| | 154 | |
|
| 0 | 155 | | private bool HasTexturePropertiesChanged() { return texture.wrapMode != unityWrap || texture.filterMode != unity |
| | 156 | |
|
| | 157 | | private void ApplyTextureProperties() |
| | 158 | | { |
| 0 | 159 | | texture.wrapMode = unityWrap; |
| 0 | 160 | | texture.filterMode = unitySamplingMode; |
| 0 | 161 | | texture.Compress(false); |
| 0 | 162 | | texture.Apply(unitySamplingMode != FilterMode.Point, true); |
| 0 | 163 | | } |
| | 164 | |
|
| | 165 | | private IEnumerator VideoTextureUpdate() |
| | 166 | | { |
| 46 | 167 | | while (true) |
| | 168 | | { |
| 57 | 169 | | if (isPlayStateDirty) |
| | 170 | | { |
| 20 | 171 | | CalculateVideoVolumeAndPlayStatus(); |
| 20 | 172 | | isPlayStateDirty = false; |
| | 173 | | } |
| | 174 | |
|
| 57 | 175 | | if (!isPlayerInScene && currUpdateIntervalTime < OUTOFSCENE_TEX_UPDATE_INTERVAL) |
| | 176 | | { |
| 0 | 177 | | currUpdateIntervalTime += Time.unscaledDeltaTime; |
| 0 | 178 | | } |
| 57 | 179 | | else if (texturePlayer != null && !isTest) |
| | 180 | | { |
| 0 | 181 | | currUpdateIntervalTime = 0; |
| 0 | 182 | | texturePlayer.UpdateWebVideoTexture(); |
| | 183 | | } |
| | 184 | |
|
| 57 | 185 | | yield return null; |
| | 186 | | } |
| | 187 | | } |
| | 188 | |
|
| | 189 | | private void CalculateVideoVolumeAndPlayStatus() |
| | 190 | | { |
| 20 | 191 | | isVisible = false; |
| 20 | 192 | | float minDistance = float.MaxValue; |
| 20 | 193 | | distanceVolumeModifier = 0; |
| | 194 | |
|
| 20 | 195 | | if (attachedMaterials.Count > 0) |
| | 196 | | { |
| 20 | 197 | | using (var iterator = attachedMaterials.GetEnumerator()) |
| | 198 | | { |
| 31 | 199 | | while (iterator.MoveNext()) |
| | 200 | | { |
| 20 | 201 | | var materialInfo = iterator.Current; |
| 20 | 202 | | if (materialInfo.Value.IsVisible()) |
| | 203 | | { |
| 9 | 204 | | isVisible = true; |
| 9 | 205 | | var entityDist = materialInfo.Value.GetClosestDistanceSqr(DCLCharacterController.i.transform |
| 9 | 206 | | if (entityDist < minDistance) |
| 9 | 207 | | minDistance = entityDist; |
| | 208 | | // NOTE: if current minDistance is enough for full volume then there is no need to keep iter |
| 9 | 209 | | if (minDistance <= DCL.Configuration.ParcelSettings.PARCEL_SIZE * DCL.Configuration.ParcelSe |
| 9 | 210 | | break; |
| | 211 | | } |
| | 212 | | } |
| 11 | 213 | | } |
| | 214 | | } |
| | 215 | |
|
| 20 | 216 | | if (isVisible) |
| | 217 | | { |
| | 218 | | const float maxDistanceBlockForSound = 6; |
| 9 | 219 | | float sqrParcelDistance = DCL.Configuration.ParcelSettings.PARCEL_SIZE * DCL.Configuration.ParcelSetting |
| 9 | 220 | | distanceVolumeModifier = 1 - Mathf.Clamp01(Mathf.FloorToInt(minDistance / sqrParcelDistance) / maxDistan |
| | 221 | | } |
| | 222 | |
|
| 20 | 223 | | if (texturePlayer != null) |
| | 224 | | { |
| 20 | 225 | | texturePlayer.visible = isVisible; |
| | 226 | | } |
| | 227 | |
|
| 20 | 228 | | UpdateVolume(); |
| 20 | 229 | | } |
| | 230 | |
|
| | 231 | | private void UpdateVolume() |
| | 232 | | { |
| 31 | 233 | | if (texturePlayer == null) |
| 0 | 234 | | return; |
| | 235 | |
|
| 31 | 236 | | float targetVolume = 0f; |
| | 237 | |
|
| 31 | 238 | | if (CommonScriptableObjects.rendererState.Get() && IsPlayerInSameSceneAsComponent((CommonScriptableObjects.s |
| 24 | 239 | | targetVolume = baseVolume * distanceVolumeModifier * Settings.i.generalSettings.sfxVolume; |
| | 240 | |
|
| 31 | 241 | | texturePlayer.SetVolume(targetVolume); |
| 31 | 242 | | } |
| | 243 | |
|
| | 244 | | private bool IsPlayerInSameSceneAsComponent(string currentSceneId) |
| | 245 | | { |
| 44 | 246 | | if (scene == null) |
| 0 | 247 | | return false; |
| 44 | 248 | | if (string.IsNullOrEmpty(currentSceneId)) |
| 0 | 249 | | return false; |
| | 250 | |
|
| 44 | 251 | | return (scene.sceneData.id == currentSceneId) || (scene is GlobalScene globalScene && globalScene.isPortable |
| | 252 | | } |
| | 253 | |
|
| 8 | 254 | | private void OnPlayerCoordsChanged(Vector2Int coords, Vector2Int prevCoords) { isPlayStateDirty = true; } |
| | 255 | |
|
| 26 | 256 | | private void OnSceneIDChanged(string current, string previous) { isPlayerInScene = IsPlayerInSameSceneAsComponen |
| | 257 | |
|
| | 258 | | public override void AttachTo(PBRMaterial material) |
| | 259 | | { |
| 0 | 260 | | base.AttachTo(material); |
| 0 | 261 | | AttachToMaterial(material); |
| 0 | 262 | | } |
| | 263 | |
|
| | 264 | | public override void DetachFrom(PBRMaterial material) |
| | 265 | | { |
| 0 | 266 | | base.DetachFrom(material); |
| 0 | 267 | | DetachFromMaterial(material); |
| 0 | 268 | | } |
| | 269 | |
|
| | 270 | | public override void AttachTo(BasicMaterial material) |
| | 271 | | { |
| 10 | 272 | | base.AttachTo(material); |
| 10 | 273 | | AttachToMaterial(material); |
| 10 | 274 | | } |
| | 275 | |
|
| | 276 | | public override void DetachFrom(BasicMaterial material) |
| | 277 | | { |
| 11 | 278 | | base.DetachFrom(material); |
| 11 | 279 | | DetachFromMaterial(material); |
| 11 | 280 | | } |
| | 281 | |
|
| | 282 | | private void AttachToMaterial(BaseDisposable baseDisposable) |
| | 283 | | { |
| 10 | 284 | | if (!attachedMaterials.ContainsKey(baseDisposable.id)) |
| | 285 | | { |
| 10 | 286 | | attachedMaterials.Add(baseDisposable.id, new MaterialComponent(baseDisposable)); |
| 10 | 287 | | baseDisposable.OnAttach += OnEntityAttachedMaterial; |
| 10 | 288 | | baseDisposable.OnDetach += OnEntityDetachedMaterial; |
| 10 | 289 | | isPlayStateDirty = true; |
| | 290 | |
|
| 10 | 291 | | if (baseDisposable.attachedEntities.Count > 0) |
| | 292 | | { |
| 8 | 293 | | using (var iterator = baseDisposable.attachedEntities.GetEnumerator()) |
| | 294 | | { |
| 16 | 295 | | while (iterator.MoveNext()) |
| | 296 | | { |
| 8 | 297 | | var entity = iterator.Current; |
| 8 | 298 | | entity.OnShapeUpdated -= OnEntityShapeUpdated; |
| 8 | 299 | | entity.OnShapeUpdated += OnEntityShapeUpdated; |
| | 300 | | } |
| 8 | 301 | | } |
| | 302 | | } |
| | 303 | | } |
| 10 | 304 | | } |
| | 305 | |
|
| | 306 | | private void DetachFromMaterial(BaseDisposable baseDisposable) |
| | 307 | | { |
| 11 | 308 | | if (attachedMaterials.ContainsKey(baseDisposable.id)) |
| | 309 | | { |
| 10 | 310 | | attachedMaterials.Remove(baseDisposable.id); |
| 10 | 311 | | baseDisposable.OnAttach -= OnEntityAttachedMaterial; |
| 10 | 312 | | baseDisposable.OnDetach -= OnEntityDetachedMaterial; |
| 10 | 313 | | isPlayStateDirty = true; |
| | 314 | | } |
| 11 | 315 | | } |
| | 316 | |
|
| | 317 | | // TODO: we will need an event for visibility change on UI for supporting video |
| | 318 | | public override void AttachTo(UIImage image) |
| | 319 | | { |
| 0 | 320 | | if (!attachedMaterials.ContainsKey(image.id)) |
| | 321 | | { |
| 0 | 322 | | attachedMaterials.Add(image.id, new UIShapeComponent(image)); |
| 0 | 323 | | isPlayStateDirty = true; |
| | 324 | | } |
| 0 | 325 | | } |
| | 326 | |
|
| | 327 | | public override void DetachFrom(UIImage image) |
| | 328 | | { |
| 0 | 329 | | if (attachedMaterials.ContainsKey(image.id)) |
| | 330 | | { |
| 0 | 331 | | attachedMaterials.Remove(image.id); |
| 0 | 332 | | isPlayStateDirty = true; |
| | 333 | | } |
| 0 | 334 | | } |
| | 335 | |
|
| 2 | 336 | | void OnEntityRemoved(IDCLEntity entity) { isPlayStateDirty = true; } |
| | 337 | |
|
| 0 | 338 | | void OnSettingsChanged(SettingsData.GeneralSettings settings) { UpdateVolume(); } |
| | 339 | |
|
| | 340 | | public override void Dispose() |
| | 341 | | { |
| 12 | 342 | | Settings.i.OnGeneralSettingsChanged -= OnSettingsChanged; |
| 12 | 343 | | CommonScriptableObjects.playerCoords.OnChange -= OnPlayerCoordsChanged; |
| 12 | 344 | | CommonScriptableObjects.sceneID.OnChange -= OnSceneIDChanged; |
| 12 | 345 | | if (scene != null) |
| 12 | 346 | | scene.OnEntityRemoved -= OnEntityRemoved; |
| 12 | 347 | | if (texturePlayerUpdateRoutine != null) |
| | 348 | | { |
| 11 | 349 | | CoroutineStarter.Stop(texturePlayerUpdateRoutine); |
| 11 | 350 | | texturePlayerUpdateRoutine = null; |
| | 351 | | } |
| | 352 | |
|
| 12 | 353 | | if (texturePlayer != null) |
| | 354 | | { |
| 11 | 355 | | texturePlayer.Dispose(); |
| 11 | 356 | | texturePlayer = null; |
| | 357 | | } |
| | 358 | |
|
| 12 | 359 | | Utils.SafeDestroy(texture); |
| 12 | 360 | | base.Dispose(); |
| 12 | 361 | | } |
| | 362 | |
|
| 0 | 363 | | private void OnEntityAttachedMaterial(IDCLEntity entity) { entity.OnShapeUpdated += OnEntityShapeUpdated; } |
| | 364 | |
|
| | 365 | | private void OnEntityDetachedMaterial(IDCLEntity entity) |
| | 366 | | { |
| 1 | 367 | | if (texturePlayer != null) |
| 1 | 368 | | texturePlayer.Pause(); |
| | 369 | |
|
| 1 | 370 | | entity.OnShapeUpdated -= OnEntityShapeUpdated; |
| 1 | 371 | | } |
| | 372 | |
|
| 28 | 373 | | private void OnEntityShapeUpdated(IDCLEntity entity) { isPlayStateDirty = true; } |
| | 374 | |
|
| | 375 | | internal interface MaterialInfo |
| | 376 | | { |
| | 377 | | float GetClosestDistanceSqr(Vector3 fromPosition); |
| | 378 | | bool IsVisible(); |
| | 379 | | } |
| | 380 | |
|
| | 381 | | struct MaterialComponent : MaterialInfo |
| | 382 | | { |
| | 383 | | BaseDisposable component; |
| | 384 | |
|
| | 385 | | float MaterialInfo.GetClosestDistanceSqr(Vector3 fromPosition) |
| | 386 | | { |
| 9 | 387 | | float dist = int.MaxValue; |
| 9 | 388 | | if (component.attachedEntities.Count > 0) |
| | 389 | | { |
| 9 | 390 | | using (var iterator = component.attachedEntities.GetEnumerator()) |
| | 391 | | { |
| 18 | 392 | | while (iterator.MoveNext()) |
| | 393 | | { |
| 9 | 394 | | var entity = iterator.Current; |
| 9 | 395 | | if (IsEntityVisible(entity)) |
| | 396 | | { |
| 9 | 397 | | var entityDist = (entity.meshRootGameObject.transform.position - fromPosition).sqrMagnit |
| 9 | 398 | | if (entityDist < dist) |
| 9 | 399 | | dist = entityDist; |
| | 400 | | } |
| | 401 | | } |
| 9 | 402 | | } |
| | 403 | | } |
| | 404 | |
|
| 9 | 405 | | return dist; |
| | 406 | | } |
| | 407 | |
|
| | 408 | | bool MaterialInfo.IsVisible() |
| | 409 | | { |
| 20 | 410 | | if (component.attachedEntities.Count > 0) |
| | 411 | | { |
| 18 | 412 | | using (var iterator = component.attachedEntities.GetEnumerator()) |
| | 413 | | { |
| 27 | 414 | | while (iterator.MoveNext()) |
| | 415 | | { |
| 18 | 416 | | if (IsEntityVisible(iterator.Current)) |
| | 417 | | { |
| 9 | 418 | | return true; |
| | 419 | | } |
| | 420 | | } |
| 9 | 421 | | } |
| | 422 | | } |
| | 423 | |
|
| 11 | 424 | | return false; |
| 9 | 425 | | } |
| | 426 | |
|
| | 427 | | bool IsEntityVisible(IDCLEntity entity) |
| | 428 | | { |
| 27 | 429 | | if (entity.meshesInfo == null) |
| 0 | 430 | | return false; |
| 27 | 431 | | if (entity.meshesInfo.currentShape == null) |
| 7 | 432 | | return false; |
| 20 | 433 | | return entity.meshesInfo.currentShape.IsVisible(); |
| | 434 | | } |
| | 435 | |
|
| 0 | 436 | | public MaterialComponent(BaseDisposable component) { this.component = component; } |
| | 437 | | } |
| | 438 | |
|
| | 439 | | struct UIShapeComponent : MaterialInfo |
| | 440 | | { |
| | 441 | | UIShape shape; |
| | 442 | |
|
| 0 | 443 | | float MaterialInfo.GetClosestDistanceSqr(Vector3 fromPosition) { return 0; } |
| | 444 | |
|
| | 445 | | bool MaterialInfo.IsVisible() |
| | 446 | | { |
| 0 | 447 | | if (!((UIShape.Model) shape.GetModel()).visible) |
| 0 | 448 | | return false; |
| 0 | 449 | | return IsParentVisible(shape); |
| | 450 | | } |
| | 451 | |
|
| | 452 | | bool IsParentVisible(UIShape shape) |
| | 453 | | { |
| 0 | 454 | | UIShape parent = shape.parentUIComponent; |
| 0 | 455 | | if (parent == null) |
| 0 | 456 | | return true; |
| 0 | 457 | | if (parent.referencesContainer.canvasGroup.alpha == 0) |
| | 458 | | { |
| 0 | 459 | | return false; |
| | 460 | | } |
| | 461 | |
|
| 0 | 462 | | return IsParentVisible(parent); |
| | 463 | | } |
| | 464 | |
|
| 0 | 465 | | public UIShapeComponent(UIShape image) { shape = image; } |
| | 466 | | } |
| | 467 | | } |
| | 468 | | } |