Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

ECS Camera

Discussion in 'Data Oriented Technology Stack' started by Kender, Mar 28, 2019.

  1. Kender

    Kender

    Joined:
    Nov 16, 2012
    Posts:
    275
    Is there ECS camera coming anytime soon? I am interested in manual render to target texture with the camera's new transform manipulations, since I work on a portals solution. Since GameObjectEntity got deprected, I see no performant way to keep camera entities bound to their Unity components nor GameObjects.
     
    deus0 likes this.
  2. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    404
    CopyTransformTo/FromGameObject is still there. Adding the camera's Transform (using AddComponentObject) and CopyTransformTo/FromGameObject component is still working... if that is what you wanted.
     
    Kender likes this.
  3. Kender

    Kender

    Joined:
    Nov 16, 2012
    Posts:
    275
    Thank you. Though I'd love to have some less hacky solution with access to Camera as well. I just can't wait to move my code to Pure ECS :)
     
    deus0, GliderGuy and starikcetin like this.
  4. andynayrb

    andynayrb

    Joined:
    Feb 28, 2019
    Posts:
    25
    Sorry for the potentially obvious question. I can get the camera right on the entity, but is there a way to off set the camera using this method? In other words, a way to have the camera behind and slightly above your entities position, third person point of view?
     
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    7,289
    Leaving camera as game object, allows to use for example cinemachine. And other features.

    I don't think there is anything critical for camera, to be 'must' on ECS.
     
  6. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    404
    You can add any Transform Component to any Entity and let the transformation get synced between ECS and 'managed' in one direction or the other.

    Eg you can add an empty proxy gameobject representing you player, sync the players transform to that gameobject using CopyTranformToGameObject and then use cinemachine on the actual camera to follow this synced gameobject.
     
    Antypodish likes this.
  7. Kender

    Kender

    Joined:
    Nov 16, 2012
    Posts:
    275
    There is though. I have many cameras for many portals, and I also need to have some ECS way to track the renderer's visibility
     
    Nyanpas and Antypodish like this.
  8. deus0

    deus0

    Joined:
    May 12, 2015
    Posts:
    33
    Hey Kender.
    I was just working on this. You just have to move all your code (camera functions) to ECS and use the monobehaviour camera for rendering. You can even disable that monobehaviour camera and render it in a system. I would also love a camera ECS component. *mildly obsessed with optimization*
    The camera.render function still takes a lot of time (1ish milliseconds). So optimization of what to draw on what portals to draw is key.

    For portal visibility from camera, its not perfect as it doesn't test for occlusion (when there is portals behind a wall), but i found:
    // http://wiki.unity3d.com/index.php/IsVisibleFrom
    Code (CSharp):
    1.  
    2.     public static bool VisibleFromCamera(Bounds bounds, Camera camera)
    3.     {
    4.         Plane[] frustumPlanes = GeometryUtility.CalculateFrustumPlanes(camera);
    5.         return GeometryUtility.TestPlanesAABB(frustumPlanes, bounds);
    6.     }
    7.  
     
    Nyanpas likes this.
  9. wg-siggig

    wg-siggig

    Joined:
    Mar 17, 2020
    Posts:
    36
    Camera as a hybrid component should already work now in 0.7 and higher. Try moving your camera GO to a subscene or mark it as Convert To Entity, and then see if there isn't a Camera component on it?
     
  10. JakHussain

    JakHussain

    Joined:
    Oct 20, 2016
    Posts:
    299
    @wg-siggig what's the advantage of having the camera as a hybrid component? what does this allow me to do?
     
    Nyanpas likes this.
  11. wg-siggig

    wg-siggig

    Joined:
    Mar 17, 2020
    Posts:
    36
    It allows you to manipulate it from ECS code since you access it like any other entity component.
     
  12. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    277
    Just to give an example of what @wg-siggig is talking about, you can add your camera/transform to an entity using
    AddComponentObject
    in a conversion script. You have to set your ConvertToEntity to "ConvertAndInjectGameObject". Then you can create a dedicated system to sync the entity position to the transform. This way you can manipulate the transform of your camera and the entity position will move with it. You could of course put all this in a single file if you wanted to reduce the clutter a bit.
     
    KwahuNashoba and JakHussain like this.
  13. Kender

    Kender

    Joined:
    Nov 16, 2012
    Posts:
    275
  14. wg-siggig

    wg-siggig

    Joined:
    Mar 17, 2020
    Posts:
    36
    Actually, since 0.7 you can mark Lights and Cameras (and some other things, I don't remember the full list) to convert and the conversion flow handles it for you. No need to manually add the hybrid components yourself.
     
    florianhanke, Sarkahn and JakHussain like this.
  15. YurySedyakin

    YurySedyakin

    Joined:
    Jul 25, 2018
    Posts:
    47
    I have tried that - it works for me if I add ConvertToEntity on the camera game object. But when I move the camera into a subscene it doesn't work. I.e. there is an entity with the companion game object but my game view says "No cameras rendering".
     
  16. JakHussain

    JakHussain

    Joined:
    Oct 20, 2016
    Posts:
    299
    PSA regarding the roadmap live stream, Unity have announced more details on DOTS cinemachine
     
    deus0 likes this.
  17. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    277
    You're right, ConvertAndInject will automatically attach all the monobehaviours on that gameobject to the entity - my mistake!
     
    wg-siggig likes this.
  18. wg-siggig

    wg-siggig

    Joined:
    Mar 17, 2020
    Posts:
    36
    That is odd, it worked fine for me when I tested this last week. Maybe something is messing with the conversion, did you leave the convert behavior on after moving it to the subscene?
     
  19. Grimreaper358

    Grimreaper358

    Joined:
    Apr 8, 2013
    Posts:
    702
    DOTS Cinemachine info in the Roadmap live stream was limited and showed nothing new. You can get a lot more information and see the demo shown there in more detail from the Cinemachine 3.0 talk a few months back.

    It's timestamped to the DOTS part but if you want to see the rest you can. It also includes an early version of the Visual Scripting that was Monobehavior based before it was scrapped and now being built for DOTS.
     
    Last edited: Mar 25, 2020
    JakHussain and wg-siggig like this.
  20. YurySedyakin

    YurySedyakin

    Joined:
    Jul 25, 2018
    Posts:
    47
    Well, here's the simpliest possible experiment in 30 seconds:
    - create new scene
    - create a cube
    - add ConvertToEntity to the cube and the main camera
    - try running it - the cube renders
    - now remove ConverToEntity and move both the cube and the camera into a subscene
    - try running - nothing renders
    Don't know what I can do beyond this point - I believe it means I can't have cameras in a subscene (which is a shame, because I'd like to)
     
  21. wg-siggig

    wg-siggig

    Joined:
    Mar 17, 2020
    Posts:
    36
    I did this just now and it worked just fine. What Unity version are you on? I just tried this on the 2020.1 beta, but remember doing this on 2019.3 recently too.
     
  22. YurySedyakin

    YurySedyakin

    Joined:
    Jul 25, 2018
    Posts:
    47
    Originally I tried 2019.3.6f1, but I just tried 2020.1.0b3 - nothing is rendering. What am I doing wrong?
    Literally:
    - 2020.1.0b3 new project
    - Install Entities 0.8.0, Hybrid renderer 0.4.0
    - create cube
    - create new empty subscene
    - move Main Camera and the Cube into the subscene
    Result: the moment I uncheck the checkbox of the subscene my game view says "No cameras rendering" and that is what I see when I hit play.
     
    Last edited: Mar 27, 2020
  23. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    277
    As far as I know you can't have a camera in a subscene yet, since a subscene's default behaviour is to just destroy all gameobjects after doing entity conversion, and theres no ecs version of a camera yet.

    Apparently some time soon they will change this so that for cameras and lights it will create "HybridComponents" instead of just destroying the gameobjects if they are in a subscene.

    Perhaps you added the camera to a subscene but left the subscene open for editing? This would prevent it from being destroyed.
     
    YurySedyakin likes this.
  24. wg-siggig

    wg-siggig

    Joined:
    Mar 17, 2020
    Posts:
    36
    As I already mentioned here above, that change is already in the released packages since 0.7 afaik?

    But you both are right, when keeping the subscene open everything works (including play mode). But when you close the subscene it stops working. Sounds like a bug to me?
     
  25. YurySedyakin

    YurySedyakin

    Joined:
    Jul 25, 2018
    Posts:
    47
    Here's what we have in the latest Hybrid Renderer docs https://docs.unity3d.com/Packages/c...brid@0.4/manual/index.html#hybrid-renderer-v2:
    Hybrid Entities: Subscene support for Light, Camera and other managed components
    I read this as we should be able to have a camera in a subscene. Now if only we could figure out the correct setup for that...
     
    Sarkahn likes this.
  26. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    277
    Sorry, I thought you were talking about "Convert and inject", my mistake! I took a look at the source code, it looks like the relevant change is in the Hybrid Renderer package like Yury said - from "HybridEntitiesConversion.cs":

    Code (CSharp):
    1.     public class CameraConversionSystem : GameObjectConversionSystem
    2.     {
    3.         protected override void OnUpdate()
    4.         {
    5.             Entities.ForEach((Camera camera) =>
    6.             {
    7.                 AddHybridComponent(camera);
    8.             });
    9.         }
    10.     }
    It does the same for lights, sprites, particles and VisualEffects. It does look like it's bugged right now though, when I tried adding my camera to a subscene I immediately got an exception:

    Unity_KTYV0IpSrP.png

    It also creates a disabled clone of the Camera, which I suppose is meant to be the hidden companion object used in HybridComponents.
     
  27. deus0

    deus0

    Joined:
    May 12, 2015
    Posts:
    33
    Any news on the ECS camera component? #dotsforlife #ecs :)
     
  28. SenseEater

    SenseEater

    Joined:
    Nov 28, 2014
    Posts:
    68
    IIRC it has been always like this. the conversion doesn't happen till you are out of edit mode. in Open subscene mode, everything is normal Game-object representation. serialization to native binary data happens once the subscene has been closed. this seems intentional by design since without this we can't have the existing editor tooling operate on the entities in edit time.
     
  29. wg-siggig

    wg-siggig

    Joined:
    Mar 17, 2020
    Posts:
    36
    I see your point, but in this case the feature is incomplete or buggy. There is already a
    CameraConversionSystem in the codebase.
     
  30. YurySedyakin

    YurySedyakin

    Joined:
    Jul 25, 2018
    Posts:
    47
    There seems to be some work going on in hybrid renderer regarding cameras. Just updated to the latest hybrid renderer 0.5.0-preview.6 and now there is a bunch of #ifdef's around hybrid camera conversion. You need HYBRID_ENTITIES_CAMERA_CONVERSION for the conversion to happen at all, and for HDRP and URP the conversion seems to add some "additional camera data" which is separate for HDRP and URP, but both still seem to be hybrid and not "pure" DOTS cameras. Anyone interested - look in HybridEntitiesConversion.cs in hybrid renderer package.

    Just tried, and it still doesn't work in subscenes, which is a shame. Does anyone know if hybrid components aka companion objects are supposed to work with subscenes at all?
     
    Last edited: May 7, 2020
  31. wolfson_rebel

    wolfson_rebel

    Joined:
    Mar 7, 2020
    Posts:
    3
    Thank you! That helped a lot!
     
  32. wolfson_rebel

    wolfson_rebel

    Joined:
    Mar 7, 2020
    Posts:
    3
    Ok, Guys... I am a few month in unity in general and in DOTS in particular... May be it is quite obvious for everybody here, but I did figured out a workaround for the situation with "No camera rendering" when converting camera to entity.

    I was experiencing this issue being updated to the latest versions of DOTS packages:

    upload_2020-5-26_10-52-19.png

    And with this configuration I got this every time I hit play:

    upload_2020-5-26_10-53-58.png

    So, if you switch method of game object conversion to entity from destroying

    upload_2020-5-26_10-55-7.png

    to injection

    upload_2020-5-26_10-55-44.png

    camera is back! That is workaround, obviously... Destroying workflow should also work as expected. Again, It could be completely obvious for everybody here, but for someone like me - here is the temp solution...
     
    alvinwan and bobbaluba like this.
  33. francois85

    francois85

    Joined:
    Aug 11, 2015
    Posts:
    1,289
    I had a similar issue add HYBRID_ENTITIES_CAMERA_CONVERSION to scripting defined symbols fixed my issues.
     

    Attached Files:

    lclemens likes this.
  34. wolfson_rebel

    wolfson_rebel

    Joined:
    Mar 7, 2020
    Posts:
    3
    Oh, great! Thank you! Did it make work "convert and destroy" workflow?
     
  35. francois85

    francois85

    Joined:
    Aug 11, 2015
    Posts:
    1,289
    I believe it should but have not tested it
     
unityunity