Forum rules - please read before posting.

Create CCTV camera effect

Hi ACers!

I was wondering if someone could help create a script to force a low framerate for AC's main camera. The idea is that it would capture everything but only render X frames. It's to create a jittery effect like forced stop motion or a security camera. I know you can use targetFrameRate but that's for the whole Unity build - I don't want it to affect UI and cursor, for instance.

I managed to use some of the code mentioned here, but only with a Unity camera - not with AC's Main Camera.

When I add the script to AC's main camera, it disables it on start which breaks everything.

Any pointers?

Comments

  • edited March 2023

    I'd second mikelortega's answer on that thread - using a RenderTexture and updating it every "X" frames would be my approach.

    The MainCamera is the one that'll need to be affected, since that's what performs all rendering. However, if you hook into the OnSwitchCamera custom event, you can turn the effect on and off based on which GameCamera is currently active.

    Here's an adaptation of the code in the other thread. Attach it to the MainCamera and assign the GameCamera you want to be active when the effect kicks in:

    using UnityEngine;
    using AC;
    
    [RequireComponent(typeof(Camera))]
    public class FrameSkipper : MonoBehaviour
    {
    
        [SerializeField] private int framesToSkip = 10;
        [SerializeField] private _Camera gameCamera;
        private RenderTexture savedTexture;
        private bool skipFrames;
    
        private void OnEnable () { EventManager.OnSwitchCamera += OnSwitchCamera; }
        private void OnDisable () { EventManager.OnSwitchCamera -= OnSwitchCamera; }
    
        private void OnSwitchCamera (_Camera fromCamera, _Camera toCamera, float transitionTime)
        {
            skipFrames = (toCamera == gameCamera);
        }
    
        private void OnRenderImage(RenderTexture source, RenderTexture destination)
        {
            if (savedTexture == null) savedTexture = Instantiate(source) as RenderTexture;
    
            if (!skipFrames || Time.frameCount % framesToSkip == 0)
            {
                Graphics.Blit(source, savedTexture);
            }
            Graphics.Blit(savedTexture, destination);
        }
    
        private void OnDestroy ()
        {
            if (savedTexture) Destroy (savedTexture);
        }
    
    }
    
  • edited March 2023

    Thanks so much, Chris, it works like a charm!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.