Search Unity

ECS Hybrid Renderer and GameObject conversion tool now available

Discussion in 'Graphics for ECS' started by rz_0lento, Jan 18, 2019.

  1. rz_0lento

    rz_0lento

    Joined:
    Oct 8, 2013
    Posts:
    2,361
    Noticed that the newest Entities package now has GameObject scene -> Entities conversion tool (you use it from code afaik) but also saw that Unity pushed out the new Hybrid Renderer shown at last Unite.

    I don't know how complete these things are and how you specifically use them. I did small test on the conversion tool and got my level objects to show on entity debugger but that's about it.

    Edit: Apparently Hybrid Renderer now shows up in Package Manager without manual manifest modification but I'll keep the snippet here just in case someone still needs it:
    Code (CSharp):
    1. {
    2.   "dependencies": {
    3.     "com.unity.rendering.hybrid": "0.0.1-preview.3",
    4.     "com.unity.entities": "0.0.12-preview.23",
    5.  
    6.     ...
    7.  
    8. }

    Edit #2: As mentioned later on this thread, ECS release notes clarify a lot of the changes here:
    https://github.com/Unity-Technologies/EntityComponentSystemSamples/blob/master/ReleaseNotes.md
     
    Last edited: Jan 18, 2019
  2. rz_0lento

    rz_0lento

    Joined:
    Oct 8, 2013
    Posts:
    2,361
    Here's a quick snippet I used to test convert currently active scene into ECS:
    Code (CSharp):
    1. var entityManager = World.Active.GetOrCreateManager<EntityManager>();
    2. var activeScene = SceneManager.GetActiveScene();
    3. GameObjectConversionUtility.ConvertScene(activeScene, entityManager.World);
    this is obviously not how you are supposed to use this though :D But if you actually have both Entities and Hybrid packages installed, this will basically copy the scene contents and turn them into Entities and relevant components + convert every meshrenderer into new RenderMesh component which in turn are used in the Hybrid Renderer automatically.

    Additionally, noticed that after GameObject conversion, your new entities now contain the actual name of the old GameObject, instead of just text "Entity x" in Entity Debugger. I dug around this a bit and noticed that you can now name your pure ECS entities as well like:
    Code (CSharp):
    1. entityManager.SetName(myEntity, "So this happened");
     
    Last edited: Apr 18, 2019
    tahabaig, Squize, NotaNaN and 3 others like this.
  3. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    I'm not sure how the GameObject converter wouldn't conflict with the current GameObjectEntity which already converts GameObjects to entities.
    Good catch, that's awesome. Yeah they now store a string pointer in the EntityData but only in Editor mode.
    It's so good to be able to name them now as it's been a real pain finding specific entities in the Debugger.
    I'm not sure why they haven't automatically added the name via the GameObjectEntity but you can add the following to a monobehaviour to automate it.
    Code (CSharp):
    1. void Awake()
    2. {
    3.     EntityManager em = World.Active.GetOrCreateManager<EntityManager>();
    4.     Entity entity = GetComponent<GameObjectEntity>().Entity;
    5.     em.SetName(entity, gameObject.name);
    6. }
     
    NotaNaN, Ofx360, 5argon and 2 others like this.
  4. rz_0lento

    rz_0lento

    Joined:
    Oct 8, 2013
    Posts:
    2,361
    I don't really know how they are really planning for all this to work but if you just convert using this tool, it will basically make a duplicate of these items so that the converted duplicates only exists in ECS world (while still keeping the old GameObject scene intact).

    Meaning that after conversion, you can remove the original gameobjects and have the converted counterparts still existing in pure ECS world (that is IF you actually have only functionality in the GameObjects that can be converted, so mainly meshes and hybrid ECS components etc). There's no link to between the original GameObject and converted Entity like with GameObjectEntity, hence this being a conversion. The confusing part here mainly is that the original GameObjects are not automatically removed after the process (you can still unload the original scene after conversion yourself of course).
     
    Last edited: Jan 18, 2019
    5argon likes this.
  5. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    Ah ok, that's a good idea for pure ecs gameobjects.
     
  6. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    857
    So does RenderMesh replace MeshInstanceRenderer? Is this HybridRenderSystem a rename of the existing system or are there more changes? I saw last night that the github for samples were pretty heavily updated, which is great but none of these things appeared to be in it.
     
  7. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
  8. rz_0lento

    rz_0lento

    Joined:
    Oct 8, 2013
    Posts:
    2,361
    Yeah, some things got moved around, MeshInstanceRenderer got renamed to RenderMesh and new things got added. Basically rendering related things got moved now to Hybrid Renderer package.

    As what comes to the github samples update, I think it was almost purely a documentation pass besides updating the boids sample from it.
     
  9. Held0fTheWelt

    Held0fTheWelt

    Joined:
    Sep 29, 2015
    Posts:
    173
    Which one was it ?
     
  10. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    857
  11. Held0fTheWelt

    Held0fTheWelt

    Joined:
    Sep 29, 2015
    Posts:
    173
    What i did lately was, writing a scriptable object, that is capable to readout a gameobjects components to setup entities with it, when starting the game.
    This gives me the capability to store different versions easily, getting around the gameobject sitting in my gamescene around, and just using the prefab or setupscene to review.
    I made a small editor script with a button, that i would run, when i wanted to dump the current state, and entity manager loaded an ordinary resource when spawning its entities.

    But this possibility here changes on how i wanted to do this, and i would think about it another time, because i think now, i more wanted to store the components directly into the scriptable component.

    But i am not ready with this right now.

    Currently i want to use this hybrid renderer and don't really get into a working example.

    What you showed me here doesn't help me get it run... yet...
     
  12. Held0fTheWelt

    Held0fTheWelt

    Joined:
    Sep 29, 2015
    Posts:
    173
    Oha.

    Taking away my current editor, it's working.

    This is a long way to use. If there is a better possibility than writing myself a complete wrapper representation, i would be grateful, but with this, i am currently able to load a very complex structure into ecs with loading it out of a wrapper.
    The files are absolutely alpha, i just played around a bit, and this is, where i am right now, but maybe it's of interest to someone !

    Feedback wanted
    And yourselves outputs wanted too !
     

    Attached Files:

  13. rz_0lento

    rz_0lento

    Joined:
    Oct 8, 2013
    Posts:
    2,361
    I don't know if everything is here yet but pretty sure Hybrid Renderer is at least part of this setup.

    But while looking at the full ECS playlist from same Unite, I saw they also did this talk (totally missed they had this):
    This actually explains the thing I was wondering about (how you'd do this in practice). Look into around 5 minute time mark where he starts to explain how they did this.

    For anyone interested on the whole ECS playlist itself, it's here: https://www.youtube.com/playlist?list=PLX2vGYjWbI0TOdkqr_pcYAK8BvEorwfS1

    Edit: saw in the new Entities package that their perf tests do things like:
    Code (CSharp):
    1. using Unity.Entities.Serialization;
    2.  
    3. ...
    4.  
    5. SerializeUtility.SerializeWorld(m_Manager, writer, out sharedData);
    6. SerializeUtility.DeserializeWorld(entityManager.BeginExclusiveEntityTransaction(), reader, 0);
    They also have a test that serializes 10k entities as json.
     
    Last edited: Jan 19, 2019
  14. jessejarvis

    jessejarvis

    Joined:
    Aug 9, 2013
    Posts:
    303
    I must be misunderstanding this. What exactly does it do? It doesn't convert the GameObject's MonoBehaviour scripts into ECS does it?
     
  15. Held0fTheWelt

    Held0fTheWelt

    Joined:
    Sep 29, 2015
    Posts:
    173
    The new Render System is working much faster.
    It gives you the capability to use it, even if you work with gameobjects right now.
    Probably you don't see your mesh any longer, when using the RenderMeshComponent, but it's faster - a lot.
    No! It doesn't convert the GameObject's MonoBehaviour to ECS.
    It even doesn't do it for the MeshRender or MeshFilter component.
    But using GameObjectEntity makes it possible to interact between both worlds.
    And using HybridRenderer makes it possible to use the new RenderSystem.

    Hope everything was right in these lines ...
     
  16. rz_0lento

    rz_0lento

    Joined:
    Oct 8, 2013
    Posts:
    2,361
    Actually the gameobject scene to entities -conversion tool does also convert your traditional MeshRenderer components to RenderMeshes (as long as you have both Entities and Hybrid Renderer packages installed).
     
    Held0fTheWelt likes this.
  17. softrare

    softrare

    Joined:
    Jun 12, 2011
    Posts:
    444
    Where is this conversion tool? Using version 2019.1.0f1 I feel really dumb for asking but I am looking for it for hours lol
     
  18. Held0fTheWelt

    Held0fTheWelt

    Joined:
    Sep 29, 2015
    Posts:
    173
    I would be careful, using this.
    It is no built in magic.
    Last time, i used it, it build a new entity component model out of what i built, if it knew, what it is, what in some cases doesn't, and that completely split my logic into two separate objects, both not usable as wanted.
    It does not magically snip its finger and there you have an entity component model matching your gameobject.
    But as you ask and may wanna try, what you can do with it.
    It's in

    Unity.Entities.Hybrid.GameObjectConversion

    Just open the package section in your project folder and surf for it.
     
  19. rz_0lento

    rz_0lento

    Joined:
    Oct 8, 2013
    Posts:
    2,361
    Ah, right now it's probably better to use the subscene and convert to entity workflows. This thread was made before those were available. Of course you can still manually convert your scenes but the new workflow is much nicer.
     
  20. softrare

    softrare

    Joined:
    Jun 12, 2011
    Posts:
    444
    Thanks guys! I programmed OOP the better part of my life, this change is certainly not very intuitive for me.

    I can't seem to be able to get a hold on it.

    devenv_pP9Hhyy0XP.png

    Do I need anything other than the Entities package from the package manager?

    And if I included this, is there a tutorial to follow on how to use this?

    I looked for that too, cannot find how to even start :( Is there a wizard or anything else in the Unity menu to get there?
     
  21. rz_0lento

    rz_0lento

    Joined:
    Oct 8, 2013
    Posts:
    2,361
    You also need Hybrid Rendering package if you also want to convert meshes etc to ECS world with this.

    Like with all ECS things so far, samples and tutorials are scarce. There are examples for the SubScene and ConvertToEntity though here:

    https://github.com/Unity-Technologies/EntityComponentSystemSamples

    Check those HelloECS samples, they all use these things now (convert to entity + one sample for subscene at least). For this threads conversion tool, it's really up to people themselves figure out how to use it. I don't think Unity recommends using the scripts manually atm as you can do most of the things on the subscene loading now.
     
  22. radiantboy

    radiantboy

    Joined:
    Nov 21, 2012
    Posts:
    1,633
    where's this tool?
     
  23. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    987
    I'm still reluctant to use this because I'm afraid it will break our game. So we have big prefabs that acts as the game agents/characters. I used GameObjectEntity before so I could access them via ECS. However, those other MonoBehaviour classes are also used in their own managers and whatnot.

    I don't quite understand the conversion. If I convert say this agent prefab, does the GameObject still live in the scene? If it is, is the converted entity and components a copy since they're not connected? If the converted GameObject is lost, does this mean I can no longer access such GameObject and components in some kind of manager?
     
  24. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    You have an option on the script to destroy or inject the converted game object after the conversation
     
    davenirline likes this.
  25. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    857
    you should really take a look at how conversion is used in the ecs/physics samples repository. it has many use cases that cover both keeping the gameobject around via inject, as well as destroying the original. I personally make use of gameobject characters that require Animators as well as attaching additional IComponentData/dynamicbuffers, and find it works better than the old proxy workflow.
     
    davenirline likes this.
  26. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    987
    Can you give a sample code for keeping the GameObject and have its converted entity as well? Just looked at the updated samples. None of them is doing this.

    Edit:
    I saw the Convert And Inject Game Object value for Conversion Mode in SpawnFromEntity sample. I still didn't see game objects when I selected this value.
     
    Last edited: Jun 2, 2019
  27. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    Create your IComponentData

    Screen Shot 2019-06-02 at 1.27.11 AM.png

    Add ConvertToEntityScript Screen Shot 2019-06-02 at 1.28.27 AM.png

    Implement IConverGameObjectToEntity in you MonoBehaviour

    Screen Shot 2019-06-02 at 1.29.43 AM.png

    Get data in System and do what you need to do with it
    Screen Shot 2019-06-02 at 1.31.11 AM.png

    Im just starring to learn this stuff so this could all be totally wrong . And I spelled radius wrong :( (just noticed)
     
  28. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    987
    Thanks. I figured it out. There's one thing that seems wrong. When I use Convert and Inject, there are two meshes that are being rendered. I'm guessing it's the one from MeshRenderer and the one handled by ECS. If this is the case, are you required to remove the MeshRenderer component then after converting?

    I also found that the entity's LocalToWorld does not automatically follow the values of game object's Transform. Does this need a custom system to copy from Transform to LocalToWorld?
     
  29. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    Sorry I have not got that far
     
  30. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    987
    So I did remove the MeshRenderer and MeshFilter components after converting. I also made this system to copy the position from the Transform:

    Code (CSharp):
    1. public class CubeCopyTransformSystem : ComponentSystem {
    2.     private EntityQuery query;
    3.  
    4.     protected override void OnCreate() {
    5.         base.OnCreate();
    6.         this.query = GetEntityQuery(typeof(CubeMonoBehaviour), typeof(Transform),
    7.             typeof(Translation));
    8.     }
    9.  
    10.     protected override void OnUpdate() {
    11.         NativeArray<Translation> destination = this.query.ToComponentDataArray<Translation>(Allocator.TempJob,
    12.             out JobHandle getDestinationHandle);
    13.        
    14.         CopyJob copyJob = new CopyJob() {
    15.             translations = destination
    16.         };
    17.         copyJob.Schedule(this.query.GetTransformAccessArray(), getDestinationHandle).Complete();
    18.         this.query.CopyFromComponentDataArray(destination); // Copy back into the entity values
    19.         destination.Dispose();
    20.     }
    21.    
    22.     private struct CopyJob : IJobParallelForTransform {
    23.         public NativeArray<Translation> translations;
    24.        
    25.         public void Execute(int index, TransformAccess transform) {
    26.             this.translations[index] = new Translation() {
    27.                 Value = transform.position
    28.             };
    29.         }
    30.     }
    31. }
    Copying the rotation and scale would be trivial. But is there a more elegant way?
     
  31. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    There is already CopyInitialTransformFromGameObject, CopyTransformToGameObject and CopyTransformFromGameObject in Unity.Transforms which take care of this via built in ecs transform systems.
     
  32. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    987
    I see. The usage is not very intuitive. My initial action is to add CopyTransformFromGameObjectProxy to the prefab, but this is the very thing that will be deprecated soon. I realized later that I have to add them in Convert().

    Copying of scale doesn't work, though, even if I explicitly add the Scale component. I added both
    CopyInitialTransformFromGameObject and
    CopyTransformFromGameObject.
     
  33. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    Yeah scale is not part of the hybrid transform copy system so you might have to write your own for that one. You can look at CopyTransformFromGameObjectSystem source.

    Just replicate existing pattern and create a CopyScaleFromGameObject component with a system to copy it.
    Keep in mind that there is a single float Scale component for uniform scale and also a float3 NonUniformScale component.
     
    davenirline likes this.
  34. crescinicharmaine

    crescinicharmaine

    Joined:
    Nov 16, 2018
    Posts:
    3
    Hello, I am trying to implement ECS with navmesh but I can't seem to access the navmeshagent component from the gameobject.
     
  35. crescinicharmaine

    crescinicharmaine

    Joined:
    Nov 16, 2018
    Posts:
    3
    nevermind. It is not supported.
     
  36. tahabaig

    tahabaig

    Joined:
    Jul 1, 2018
    Posts:
    3
    I convert all the mesh gameobjects to ECS but it is too much heavy on pc thread so how could i convert it using JOB SYSTEM