lgsvl / simulator

A ROS/ROS2 Multi-robot Simulator for Autonomous Vehicles
Other
2.26k stars 778 forks source link

Change camera view and zoom on loading #1781

Closed MartinaB85 closed 2 years ago

MartinaB85 commented 2 years ago

Good afternoon,

I'm currently using the SVL 2021.1 version and I would need to set the scene when loading with the simulator camera following the ego-vehicle, as it is doing by default, but with less zoom and, if possible, higher than the actual position. And every time the scene is reloaded I would need to keep this values, once adjusted.

Which scripts should I change in order to obtain this behavior?

Many thanks,

Martina

EricBoiseLGSVL commented 2 years ago

You need to adjust SimulatorCameraController code. That is where position and rotation logic lies. It is based on BoundsZ. This is calculated on the vehicle on start. Then build a new binary and your changes will stay.

MartinaB85 commented 2 years ago

Yeah, but which parameters should I adjust? Changing ZoomInput and Camera.Elevation does not do anything when launching the editor, even putting the parameters after if (SystemInfo.operatingSystemFamily == OperatingSystemFamily.Linux && Application.isEditor)

EricBoiseLGSVL commented 2 years ago

SimulatorCameraController.cs

public void SetFollowCameraState(GameObject target)
    {
        SimulatorManager.Instance?.UIManager?.ResetCinematicAlpha();
        Debug.Assert(target != null);
        transform.SetParent(SimulatorManager.Instance?.CameraManager.transform);
        CurrentCameraState = CameraStateType.Follow;
        targetObject = target.transform;
        lastTargetPosition = targetObject.transform.position;

        var controller = target.GetComponent<IAgentController>();
        if (controller != null)
        {
            BoundsY = controller.Bounds.size.y;
            BoundsZ = controller.Bounds.size.z;
            BoundsZ = BoundsZ < 1f ? 1f : BoundsZ;
            offset = new Vector3(0f, BoundsY, -BoundsZ);
            cinematicOffset = new Vector3(0f, BoundsY * 3f, 0f);
        }
        transform.position = targetObject.position;
        transform.rotation = targetObject.rotation;
        thisCamera.transform.localRotation = Quaternion.identity;
        thisCamera.transform.localPosition = Vector3.zero;
        thisCamera.transform.localPosition = thisCamera.transform.InverseTransformPoint(targetObject.position) + offset;
        defaultFollow = true;
        targetTiltFree = transform.eulerAngles.x;
        targetLookFree = transform.eulerAngles.y;
        SimulatorManager.Instance.UIManager?.SetCameraButtonState();
    }

This is slightly different because it is latest simulator code but you can see inthisCamera.transform.localPosition = thisCamera.transform.InverseTransformPoint(targetObject.position) + offset; the offset places the camera relative to the target position. Then the offset is used in CameraManager.cs to increase or decrease the distance to the focus. So add a value to the offest or adjust the offset itself.

MartinaB85 commented 2 years ago

Thank you very much for your help.! While here, in order to not open another issue..I would need to make a camera sensor visible (only this sensor), at the beginning, when playing the simulator..which script should I adjust to obtain this behavior?

Thank you very much,

Martina

EricBoiseLGSVL commented 2 years ago

No problem, glad to help. It's fine to open a new issue. It helps users search when they have a similar problem. You want to make the simulator camera snap to the camera sensor position? If so, you need to change simulator code.

This is a great idea and we might implement something like this. Thanks for the post.

MartinaB85 commented 2 years ago

Jejej, you got the idea from this..! Awesome.! :) No, I would just need to make the back camera sensor visible when playing, in order to see what happens behind the ego vehicle. This is usually done by making it visible in the eye menu of the game, while playing. But I want to make it visible starting from the beginning of the simulation.

EricBoiseLGSVL commented 2 years ago

Ah, I see. You can change the color camera sensor to enable in the Visualizer init method, like so image

Tested and this is the result: FYI UI will be off and state not updated but it works. You should also run it and position the display once. image

Just make the change and build a new binary since the code change is in simulator core.

MartinaB85 commented 2 years ago

Thank you very much !

MartinaB85 commented 2 years ago

Good afternoon, the line added to visualize the camera on start (vis.gameObject.SetActive(sensor.name == "Color Camera Sensor");) in AddVisualizer function in UIManager.cs is working only for a while then the camera disappears. But, regardless, it is always giving me the null reference error as the code added is referencing to the camera at the same time the sensors are added to vis (see the Debug of Vis.Sensor in the attached picture).

Is there any other place where I can put the same kind of line vis.gameObject.SetActive(sensor.name == "Color Camera Sensor" but after the creation of Vis.Sensor ? I tried different things but it doesn't work.

Many thanks,

Martina Vis_Sensor_Debug

EricBoiseLGSVL commented 2 years ago

What do you mean "is working only for a while then the camera disappears"? Add visualizer is only run after a sensor is created. What camera are you destroying/referencing? Did you add this code after the visualizer is instantiated? Sorry, I am not clear on what the issue is.

MartinaB85 commented 2 years ago

I just added a Color Camera Sensor to the ego vehicle in the web interface, with the name "Color Camera Sensor", and changed the Y, Z and pitch values. Then added your code to the UIManager with the matching name, i.e. "Color Camera Sensor". When running the simulation I always get the NullReferenceException error, but the sensor is visible. If I keep using the same code, same everything for let's say two weeks ( I use the simulator almost every day) at some point the sensor is not visible anymore. So that's why I am asking if this behaviour could be caused by this Null ReferenceException or if you are doing perhaps updates in the sensors sometimes?

EricBoiseLGSVL commented 2 years ago

No updates to sensors since 2021.3 release. Is this the name once it is loaded? Are you missing a case or space? Can you debug in add visualizer what sensors are loaded, name, etc? Are you sure the camera is null or is it something else?

This can happen. New code added, removed, new machines can show issues that wasn't there before.

MartinaB85 commented 2 years ago

Yes, the code in UIManager is: vis.gameObject.SetActive(sensor.name == "Color Camera Sensor");
and the name of the camera is the same as you can see in the first picture. In the second picture you can see the sensors that are loaded and the error that I always get is in the CameraSensorBase.cs script inside the function public override void OnVisualize(Visualizer visualizer) at this line: visualizer.UpdateRenderTexture(FinalRenderTarget.UiTexture, SensorCamera.aspect);

Don't you get the same error when adding your line of code to UIManager?

CameraSensor Vis_Sensor_Debug

EricBoiseLGSVL commented 2 years ago

No this was tested locally before and does not give the null ref. That is how I was able to show the image of it working above. I mention in the above solution that state is off, is this what you are asking about? Are you getting the null after you close the visualizer and then restart?

MartinaB85 commented 2 years ago

Ah, I missed that piece of info. What does it mean that UI is off? How do you switch the state off?

EricBoiseLGSVL commented 2 years ago

UI state is off. You are hacking the object state, If you want to be able to toggle off and then on again on next run or have more functionality, you will need to make sure that the UIManager sets the state active so all UI elements are aligned.

EricBoiseLGSVL commented 2 years ago

You can clear playerprefs so you go back to the original state before you toggled it off, see Unity documentation on this or have a check in the UIManager for state of the window on load

MartinaB85 commented 2 years ago

Sorry but I do not understand. At the beginning, stating from your code all the visualizers are off except for the Camera that I need to be on. Firstly, I do not understand why is giving the null reference as the Camera sensor is created and it apperas in the list, and secondly why the UIManager state needs to be off ???!

EricBoiseLGSVL commented 2 years ago

Please look to the UIManager code to see how state is handled. You are activating the panel while the state says otherwise. Does this make sense?