DevCam

Can switch to devcam while the game is running or paused and fly around checking shit out.

Here’s some undocumented code.

public class DevCamera : BaseCamera
{
    Angles LookAngles;
    Vector3 MoveInput;

    Vector3 TargetPos;
    Rotation TargetRot;


    bool PivotEnabled;
    Vector3 PivotPos;
    float PivotDist;

    float MoveSpeed;

    bool Overlays = true;

    /// <summary>
    /// On the camera becoming activated, snap to the current view position
    /// </summary>
    public override void Activated()
    {
        TargetPos = Camera.LastPos;
        TargetRot = Camera.LastRot;
        Pos = TargetPos;
        Rot = TargetRot;
        LookAngles = Rot.Angles();

        var pos = new Vector3( 100, 100 );
        var duration = 6.0f;

        DebugOverlay.ScreenText( pos, 0, Color.White, "  .                        ", duration );
        DebugOverlay.ScreenText( pos, 1, Color.White, ",-| ,-. .  , ,-. ,-. ,-,-. ", duration );
        DebugOverlay.ScreenText( pos, 2, Color.White, "| | |-' | /  |   ,-| | | | ", duration );
        DebugOverlay.ScreenText( pos, 3, Color.White, "`-^ `-' `'   `-' `-^ ' ' ' ", duration );

        DebugOverlay.ScreenText( pos, 5, Color.White, "  Free:  WSAD, Run/Crouch for speed", duration );
        DebugOverlay.ScreenText( pos, 6, Color.White, " Pivot:  Hold Walk (Default Alt) - forward + back to zoom in/out", duration );
        DebugOverlay.ScreenText( pos, 8, Color.White, "Reload:  Toggle Overlays", duration );
    }

    public override void Update()
    {
        var player = Player.Local;
        if ( player == null ) return;

        var tr = Trace.Ray( Pos, Pos + Rot.Forward * 4096 ).Run();

        // DebugOverlay.Box( tr.EndPos, Vector3.One * -1, Vector3.One, Color.Red );

        FieldOfView = 70;
        Viewer = null;

        if ( PivotEnabled )
        {
            PivotMove();
        }
        else
        {
            FreeMove();
        }


        if ( Overlays )
        {
            var normalRot = Rotation.LookAt( tr.Normal );
            DebugOverlay.Axis( tr.EndPos, normalRot, 3.0f );

            if ( tr.Entity != null && tr.Entity.NetworkIdent > 0 )
            {
                DebugOverlay.Text( tr.EndPos + Vector3.Up * 20, $"Entity: {tr.Entity} ({tr.Entity.EngineEntityName})\n" +
                                                                $" Index: {tr.Entity.NetworkIdent}\n" +
                                                                $"Health: {tr.Entity.Health}", Color.White );

                if ( tr.Entity is ModelEntity modelEnt )
                {
                    var bbox = modelEnt.OOBBox;
                    DebugOverlay.Box( tr.Entity.Pos, bbox.Mins, bbox.Maxs, Color.Green );
                }
            }
        }
    }

    public override void BuildInput( ClientInput input )
    {
        LookAngles += input.AnalogLook;
        LookAngles.roll = 0;
        MoveInput = input.AnalogMove;

        MoveSpeed = 1;
        if ( input.Down( InputButton.Run ) ) MoveSpeed = 5;
        if ( input.Down( InputButton.Duck ) ) MoveSpeed = 0.2f;

        if ( input.Pressed( InputButton.Walk ) )
        {
            var tr = Trace.Ray( Pos, Pos + Rot.Forward * 4096 ).Run();
            PivotPos = tr.EndPos;
            PivotDist = Vector3.DistanceBetween( tr.EndPos, Pos );


        }

        PivotEnabled = input.Down( InputButton.Walk );

        if ( input.Pressed( InputButton.Reload ) ) Overlays = !Overlays;

        input.Clear();
        input.StopProcessing = true;
    }

    void FreeMove()
    {
        var mv = MoveInput.Normal * 300 * RealTime.Delta * Rot * MoveSpeed;

        TargetRot = Rotation.From( LookAngles );
        TargetPos += mv;

        Pos = Vector3.Lerp( Pos, TargetPos, 10 * RealTime.Delta );
        Rot = Rotation.Slerp( Rot, TargetRot, 40 * RealTime.Delta );
    }

    void PivotMove()
    {
        PivotDist -= MoveInput.x * RealTime.Delta * 100 * ( PivotDist / 50 );
        PivotDist = PivotDist.Clamp( 1, 1000 );

        TargetRot = Rotation.From( LookAngles );
        Rot = Rotation.Slerp( Rot, TargetRot, 40 * RealTime.Delta );

        TargetPos = PivotPos + Rot.Forward * -PivotDist;
        Pos = TargetPos;

        if ( Overlays )
        {
            var scale = Vector3.One * (2 + MathF.Sin( RealTime.Now * 10 ) * 0.3f);
            DebugOverlay.Box( PivotPos, scale * -1, scale, Color.Green );
        }
    }
}
25 Likes

What does the number mean in
DebugOverlay.ScreenText( pos, 0, Color.White, " . ", duration );

P.s: how i can pate code like in this post?

Probably the order of the lines ?

They’re the line numbers

    public static class DebugOverlay
    {
        public static void Axis( Vector3 pos, Rotation rot, float length = 10, float duration = 0, bool depthTest = true );
        public static void Box( float duration, Vector3 mins, Vector3 maxs, Color color, bool ignoreDepth = false );
        public static void Box( Vector3 mins, Vector3 maxs, Color color );
        public static void Box( Vector3 mins, Vector3 maxs );
        public static void Box( float duration, Vector3 position, Rotation rot, Vector3 mins, Vector3 maxs, Color color, bool ignoreDepth = false );
        public static void Box( float duration, Vector3 position, Vector3 mins, Vector3 maxs, Color color, bool ignoreDepth = false );
        public static void Box( Vector3 position, Vector3 mins, Vector3 maxs, Color color, bool ignoreDepth = false );
        public static void Line( Vector3 start, Vector3 end, Color color, float duration = 0, bool ignoreDepth = true );
        public static void Line( Vector3 start, Vector3 end, float duration = 0, bool ignoreDepth = true );
        public static void ScreenText( Vector2 pos, int line, Color color, string text, float duration = 0 );
        public static void ScreenText( Vector2 pos, string text, float duration = 0 );
        public static void ScreenText( string text, float duration = 0 );
        public static void ScreenText( int line, string text, float duration = 0 );
        public static void Text( Vector3 start, string text, Color color, float duration = 0, float maxDistance = 500 );
        public static void Text( Vector3 start, string text, float duration = 0 );
    }
3 Likes

i like the way you can attach yourself to props with what I assume is PivotMove would there be a way to add more information to the DevCam ourselves? maybe things like Size, WorldPos or Colour?

Well the entire devcam is just a simple class built onto BaseCamera. So you can just make anything you want. PivotMove is just a function defined in that class that changes how the camera rotates.

2 Likes

Like this:

```
<code>
```

this is great :D

Could you freeze time and move the devcam around?

There’s no reason you wouldn’t be able to freeze time and still move the camera.

Yeah you can totally pause the game and still move around. That was a big part of why I made it.

5 Likes

Did you manage to fix the AABB not covering the whole of the tub guy model? :slight_smile:

:grinning_face_with_smiling_eyes: that’s on my list this week

4 Likes

Awesome, got any more videos you can show of what you’re working on? :slight_smile: Also, is there any new shader stuff? You should make a “random stuff from development” topic and post things that don’t deserve their own thread :slight_smile:

Seems like there’s some real potential to do something like a photo mode if you can take a clientside snapshot of sorts and move around in it.

1 Like

Isn’t that just how demos work in all valve games?

In a sense, but as far as I know you can’t quickly pop in to take a picture from any angle and exit right back into to your uninterrupted multiplayer session.

does source 2 still retain anything akin to demos, btw?

Yeah, I tested it out just now and it seems to work fine.