Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion unity-renderer/Assets/DCLPlugins/ECS7/ECS7Plugin.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"GUID:7b4a5d0cf82e544daa512b41aec35b00",
"GUID:acb0f5633d7c47aba96ee6b61c872d94",
"GUID:1e10040ab6604cbe8ad0921047d49e65",
"GUID:ac62e852826a4b36aeb22931dad73edb"
"GUID:ac62e852826a4b36aeb22931dad73edb",
"GUID:f51ebe6a0ceec4240a699833d6309b23"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
5 changes: 3 additions & 2 deletions unity-renderer/Assets/DCLPlugins/ECS7/ECS7Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using DCL.ECSRuntime;
using RPC.Context;
using System.Collections.Generic;
using UnityEngine;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is a leftover?


namespace DCL.ECS7
{
Expand Down Expand Up @@ -68,8 +69,8 @@ public ECS7Plugin()
new WrappedComponentPool<IWrappedComponent<PBEngineInfo>>(MAX_EXPECTED_SCENES, () => new ProtobufWrappedComponent<PBEngineInfo>(new PBEngineInfo())),
new WrappedComponentPool<IWrappedComponent<PBUiCanvasInformation>>(MAX_EXPECTED_SCENES, () => new ProtobufWrappedComponent<PBUiCanvasInformation>(new PBUiCanvasInformation())),
new WrappedComponentPool<IWrappedComponent<PBPointerEventsResult>>(MAX_EXPECTED_SCENES * 10, () => new ProtobufWrappedComponent<PBPointerEventsResult>(new PBPointerEventsResult())),
new WrappedComponentPool<IWrappedComponent<PBTweenState>>(MAX_EXPECTED_SCENES * 10, () => new ProtobufWrappedComponent<PBTweenState>(new PBTweenState()))
);
new WrappedComponentPool<IWrappedComponent<PBTweenState>>(MAX_EXPECTED_SCENES * 10, () => new ProtobufWrappedComponent<PBTweenState>(new PBTweenState())),
new WrappedComponentPool<IWrappedComponent<PBAudioSource>>(MAX_EXPECTED_SCENES * 10, () => new ProtobufWrappedComponent<PBAudioSource>(new PBAudioSource())));

systemsController = new ECSSystemsController(systemsContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"GUID:760a1d365aad58044916992b072cf2a6",
"GUID:140f4a13272cd0842ab22d3faca39af2",
"GUID:9cccce9925d3495d8a5e4fa5b25f54a5",
"GUID:710b2089e5df4140b0d3e30fbc20d9bd"
"GUID:710b2089e5df4140b0d3e30fbc20d9bd",
"GUID:f51ebe6a0ceec4240a699833d6309b23"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System;

using Cysharp.Threading.Tasks;
using System;
using DCL.Controllers;
using DCL.ECS7.InternalComponents;
using DCL.ECSRuntime;
using DCL.Helpers;
using DCL.Models;
using DCL.SettingsCommon;
using System.Collections.Generic;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using Cysharp.Threading.Tasks; and using System.Collections.Generic; are not needed, maybe a left over?

using UnityEngine;
using AudioSettings = DCL.SettingsCommon.AudioSettings;

Expand Down Expand Up @@ -60,10 +63,7 @@ public void OnComponentCreated(IParcelScene scene, IDCLEntity entity)
dataStore.virtualAudioMixer.sceneSFXVolume.OnChange += OnVirtualAudioMixerChangedValue;
sceneNumber.OnChange += OnCurrentSceneChanged;

audioSourceInternalComponent.PutFor(scene, entity, new InternalAudioSource()
{
audioSource = this.audioSource
});


sbcInternalComponent.RegisterOnSceneBoundsStateChangeCallback(scene, entity, OnSceneBoundsStateChange);

Expand Down Expand Up @@ -149,17 +149,13 @@ private void ApplyCurrentModel()
audioSource.spatialBlend = 1;
audioSource.dopplerLevel = 0.1f;

if (model.Playing != audioSource.isPlaying)
{
if (audioSource.isPlaying)
audioSource.Stop();
else if (isAudioClipReady)
audioSource.Play();
}
else if (audioSource.isPlaying)
{
if (model.Playing == audioSource.isPlaying)
return;

if (!model.Playing && audioSource.isPlaying)
audioSource.Stop();
}
if (model.Playing && !audioSource.isPlaying && isAudioClipReady)
audioSource.Play();
}

private void ApplyLoadedAudioClip(AudioClip clip)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ public InternalRenderers(IList<Renderer> renderers)
public struct InternalAudioSource : IInternalComponent
{
public bool dirty { get; set; }
public bool isPlaying;
public bool loop;
public float volume;
public float pitch;
public string audioClipUrl;
public float speed;
public float decay;
public AudioSource audioSource;
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "ECS7Plugin.Systems.AudioSource",
"rootNamespace": "",
"references": [
"GUID:9cccce9925d3495d8a5e4fa5b25f54a5",
"GUID:9239602a072d4e52b5f1cae20ab2b459",
"GUID:0a17d39e1dc14c038aa3c6483a185df0",
"GUID:37d2c04574c1d480d8c817e2a7c578e7",
"GUID:97d8897529779cb49bebd400c7f402a6",
"GUID:ac62e852826a4b36aeb22931dad73edb",
"GUID:1e10040ab6604cbe8ad0921047d49e65",
"GUID:a881b57670b1d2747a6d7f9e32b63230",
"GUID:3c7b57a14671040bd8c549056adc04f5",
"GUID:acb0f5633d7c47aba96ee6b61c872d94"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using DCL.ECS7;
using DCL.ECS7.ComponentWrapper.Generic;
using DCL.ECS7.InternalComponents;
using DCL.ECSComponents;
using DCL.ECSRuntime;
using System.Collections.Generic;
using UnityEngine;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove UnityEngine and DCL.ECSRuntime 👌


namespace ECSSystems.AudioSourceSystem
{
public class ECSAudioSourceSystem
{
private readonly IInternalECSComponent<InternalAudioSource> audioSourceComponent;
private readonly IReadOnlyDictionary<int, ComponentWriter> componentsWriter;
private readonly WrappedComponentPool<IWrappedComponent<PBAudioSource>> audioSourcePool;

public ECSAudioSourceSystem(
IInternalECSComponent<InternalAudioSource> audioSourceComponent,
IReadOnlyDictionary<int, ComponentWriter> componentsWriter,
WrappedComponentPool<IWrappedComponent<PBAudioSource>> audioSourcePool)
{
this.audioSourceComponent = audioSourceComponent;
this.componentsWriter = componentsWriter;
this.audioSourcePool = audioSourcePool;
}

public void Update()
{
var audioSourceComponentList = audioSourceComponent.GetForAll();

// Loop through every audio source component
foreach (var component in audioSourceComponentList)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be worth it to switch from a foreach to a for loop to reduce allocations. What do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlejandroAlvarezMelucciDCL you are right! The IEnumerator call will allocate, good spot

{
var scene = component.key1;
var entity = component.key2;
var model = component.value.model;

var wrappedComponent = audioSourcePool.Get();
wrappedComponent.WrappedComponent.Model.Playing = model.audioSource.isPlaying;
wrappedComponent.WrappedComponent.Model.Loop = model.loop;
wrappedComponent.WrappedComponent.Model.Volume = model.volume;
wrappedComponent.WrappedComponent.Model.Pitch = model.pitch;
wrappedComponent.WrappedComponent.Model.AudioClipUrl ??= model.audioClipUrl;
wrappedComponent.WrappedComponent.Model.CurrentTime = model.audioSource.time;

if (componentsWriter.TryGetValue(scene.sceneData.sceneNumber, out var writer))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can re-order this check to the beginning to avoid doing all the above in case the componentsWriter doesn't have a writer for the given sceneNumber?

Copy link
Member

@pravusjif pravusjif Jun 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uhmmm it would be weird to have a scene without a components writer, to be honest

{
writer.Put(entity, ComponentID.AUDIO_SOURCE, wrappedComponent);
}
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"GUID:1e10040ab6604cbe8ad0921047d49e65",
"GUID:c76ce68567754cd0abab86915b7ba63e",
"GUID:ed6f26f965aa4a39b494b55b335924b9",
"GUID:e55bb9116cf54f04a57453fc981dc81d"
"GUID:e55bb9116cf54f04a57453fc981dc81d",
"GUID:cfa7a9aba4cb4fdfac7f5a9abfb2c2c8"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using DCL;
using ECSSystems.AnimationSystem;
using ECSSystems.AudioSourceSystem;
using ECSSystems.AvatarModifierAreaSystem;
using ECSSystems.BillboardSystem;
using ECSSystems.CameraSystem;
Expand Down Expand Up @@ -38,6 +39,7 @@ public class ECSSystemsController : IDisposable
private readonly ECSPlayerTransformSystem playerTransformSystem;
private readonly ECSSceneBoundsCheckerSystem sceneBoundsCheckerSystem;
private readonly ECSUiCanvasInformationSystem uiCanvasInformationSystem;
private readonly ECSAudioSourceSystem audioSourceSystem;
private readonly GameObject hoverCanvas;
private readonly GameObject scenesUi;
private readonly IWorldState worldState;
Expand Down Expand Up @@ -98,6 +100,11 @@ public ECSSystemsController(SystemsContext context)
context.ComponentWriters,
context.RaycastResultPool);

audioSourceSystem = new ECSAudioSourceSystem(
context.internalEcsComponents.audioSourceComponent,
context.ComponentWriters,
context.AudioSourcePool);

sceneBoundsCheckerSystem = new ECSSceneBoundsCheckerSystem(
DataStore.i.ecs7.scenes,
context.internalEcsComponents.sceneBoundsCheckComponent,
Expand Down Expand Up @@ -178,7 +185,8 @@ public ECSSystemsController(SystemsContext context)
billboardSystem.Update,
videoPlayerSystem.Update,
uiCanvasInformationSystem.Update,
animationSystem.Update
animationSystem.Update,
audioSourceSystem.Update
};

lateUpdateSystems = new ECS7System[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public record SystemsContext
public readonly WrappedComponentPool<IWrappedComponent<ECSTransform>> TransformPool;
public readonly WrappedComponentPool<IWrappedComponent<PBVideoEvent>> VideoEventPool;
public readonly WrappedComponentPool<IWrappedComponent<PBRaycastResult>> RaycastResultPool;
public readonly WrappedComponentPool<IWrappedComponent<PBAudioSource>> AudioSourcePool;
public readonly WrappedComponentPool<IWrappedComponent<PBGltfContainerLoadingState>> GltfContainerLoadingStatePool;
public readonly WrappedComponentPool<IWrappedComponent<PBEngineInfo>> EngineInfoPool;
public readonly WrappedComponentPool<IWrappedComponent<PBUiCanvasInformation>> UiCanvasInformationPool;
public readonly WrappedComponentPool<IWrappedComponent<PBPointerEventsResult>> PointerEventsResultPool;
public readonly WrappedComponentPool<IWrappedComponent<PBTweenState>> TweenStatePool;

public SystemsContext(
IReadOnlyDictionary<int, ComponentWriter> componentWriters,
public SystemsContext(IReadOnlyDictionary<int, ComponentWriter> componentWriters,
IInternalECSComponents internalEcsComponents,
IComponentGroups componentGroups,
ECSComponent<PBBillboard> billboards,
Expand All @@ -37,7 +37,8 @@ public SystemsContext(
WrappedComponentPool<IWrappedComponent<PBEngineInfo>> engineInfoPool,
WrappedComponentPool<IWrappedComponent<PBUiCanvasInformation>> uiCanvasInformationPool,
WrappedComponentPool<IWrappedComponent<PBPointerEventsResult>> pointerEventsResultPool,
WrappedComponentPool<IWrappedComponent<PBTweenState>> tweenStatePool)
WrappedComponentPool<IWrappedComponent<PBTweenState>> tweenStatePool,
WrappedComponentPool<IWrappedComponent<PBAudioSource>> audioSourcePool)
{
this.internalEcsComponents = internalEcsComponents;
this.componentGroups = componentGroups;
Expand All @@ -54,5 +55,6 @@ public SystemsContext(
UiCanvasInformationPool = uiCanvasInformationPool;
PointerEventsResultPool = pointerEventsResultPool;
TweenStatePool = tweenStatePool;
AudioSourcePool = audioSourcePool;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace DCL.Components
{

public class DCLAudioSource : BaseComponent, IOutOfSceneBoundariesHandler
{
[System.Serializable]
Expand Down
Loading