campfire/Assets/Scripts/RenderScaler.cs

49 lines
1.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/**
* <summary>
* Blits a RenderTexture on the Camera in this GameObject.
* The RenderingCamera field controls which Camera's render target is blitted.
* The render target of RenderingCamera is scaled according to Options.RenderScale.
* </summary>
*/
[RequireComponent(typeof(Camera))]
public class RenderScaler : MonoBehaviour {
public Options Options;
public Camera RenderingCamera;
public float CurrentRenderScale = -1f;
// 100ms between texture recreation should lessen the gpu load a bit
// when moving the slider around, but still give realtime (enough) feedback.
public float RenderScaleChangeCooldown = 0.1f;
private RenderTexture CameraTarget;
private float LastChange = 0;
private void Start() {
UpdateRenderTexture();
}
private void Update() {
if (Time.unscaledTime - LastChange > RenderScaleChangeCooldown && CurrentRenderScale != Options.RenderScale) {
LastChange = Time.unscaledTime;
UpdateRenderTexture();
}
}
private void UpdateRenderTexture() {
CurrentRenderScale = Options.RenderScale;
RenderingCamera.targetTexture.Release();
RenderingCamera.targetTexture.width = (int)(Screen.width * CurrentRenderScale);
RenderingCamera.targetTexture.height = (int)(Screen.height * CurrentRenderScale);
RenderingCamera.targetTexture.Create();
}
private void OnRenderImage(RenderTexture src, RenderTexture dst) {
Graphics.Blit(RenderingCamera.targetTexture, dst);
}
}