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