Skip to content

Instantly share code, notes, and snippets.

@garrynewman
Created May 18, 2018 10:32
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save garrynewman/79157e593bf6e6b7d52c6829612dcdd7 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System;
public class ScreenshotCamera
{
static Vector3 FocusPoint;
static float Aperture = 20.0f;
RealTimeSince timeSinceMessage;
string MessageText;
void Message( string msg )
{
timeSinceMessage = 0;
MessageText = msg;
}
public ScreenshotCamera( Camera main )
{
FocusPoint = Player.Local.GetComponentInChildren<CartoonEyes>().transform.position;
Message( "Screenshot Camera (Press 2 to exit)" );
}
internal void Shutdown( Camera main )
{
var dof = Dof( main );
if ( dof != null )
{
dof.active = false;
}
}
internal void Update( Camera main )
{
Vector3 move = Vector3.zero;
if ( Input.GetKey( KeyCode.W ) ) move += Vector3.forward;
if ( Input.GetKey( KeyCode.S ) ) move -= Vector3.forward;
if ( Input.GetKey( KeyCode.A ) ) move -= Vector3.right;
if ( Input.GetKey( KeyCode.D ) ) move += Vector3.right;
if ( Input.GetKey( KeyCode.Space ) ) move += Vector3.up;
if ( Input.GetKey( KeyCode.LeftControl ) ) move -= Vector3.up;
move = main.transform.rotation * move.normalized * Time.unscaledDeltaTime;
var fovDelta = ((main.fieldOfView + 10.0f) / 200.0f);
if ( Input.GetKey( KeyCode.LeftShift ) ) move *= 20.0f;
if ( Input.GetKey( KeyCode.Q ) ) move /= 10.0f;
var x = Input.GetAxisRaw( "Mouse X" );
var y = Input.GetAxisRaw( "Mouse Y" );
var sensitivity = Mathf.Clamp( fovDelta * 2.0f, 0.001f, 0.5f ) * 2.0f;
x *= sensitivity;
y *= sensitivity;
if ( Input.GetKey( KeyCode.Mouse1 ) )
{
main.fieldOfView -= y;
main.fieldOfView = Mathf.Clamp( main.fieldOfView, 0.4f, 180.0f );
y = 0;
x = 0;
}
var newRot = main.transform.rotation * Quaternion.Euler( -y, x, 0 );
var newRotE = newRot.eulerAngles;
newRotE.z = 0;
main.transform.rotation = Quaternion.Euler( newRotE );
main.transform.position += move;
if ( Input.GetKeyDown( KeyCode.Mouse0 ) )
{
if ( !System.IO.Directory.Exists( "Screenshots" ) )
System.IO.Directory.CreateDirectory( "Screenshots" );
var fn = $"Screenshots/{DateTime.Now:ddMMMMyyyy-H-mm-ss}.png";
ScreenCapture.CaptureScreenshot( fn );
Debug.Log( $"Screenshot Saved To {fn}" );
}
UpdateDOF( main );
}
UnityEngine.Rendering.PostProcessing.DepthOfField Dof( Camera main )
{
var volume = main.GetComponent<UnityEngine.Rendering.PostProcessing.PostProcessVolume>();
UnityEngine.Rendering.PostProcessing.DepthOfField dof = null;
volume.profile.TryGetSettings<UnityEngine.Rendering.PostProcessing.DepthOfField>( out dof );
return dof;
}
void UpdateDOF( Camera main )
{
var dof = Dof( main );
if ( dof == null ) return;
dof.active = true;
if ( Input.GetKey( KeyCode.E ) )
{
RaycastHit hitInfo;
if ( Physics.Raycast( new Ray( main.transform.position, main.transform.forward ), out hitInfo, 1000.0f ) )
{
FocusPoint = hitInfo.point + main.transform.forward * 0.05f;
Message( $"Focused: {hitInfo.collider.gameObject.name}" );
}
else
{
FocusPoint = main.transform.position + main.transform.forward * 1000.0f;
Message( $"Focused: Nothing" );
}
}
if ( Input.GetKey( KeyCode.R ) )
{
Aperture += Time.unscaledDeltaTime * 8.0f;
Message( $"Aperture: {Aperture:0.00}" );
}
if ( Input.GetKey( KeyCode.F ) )
{
Aperture -= Time.unscaledDeltaTime * 8.0f;
Message( $"Aperture: {Aperture:0.00}" );
}
Aperture = Mathf.Clamp( Aperture, 0.05f, 32.0f );
dof.aperture.Override( Aperture );
dof.focalLength.Override( 120.0f );
dof.focusDistance.Override ( Vector3.Distance( main.transform.position, FocusPoint ) );
dof.kernelSize.Override( UnityEngine.Rendering.PostProcessing.KernelSize.VeryLarge );
}
public void OnGUI()
{
if ( timeSinceMessage < 1.0f )
{
var style = new GUIStyle( GUI.skin.label );
style.fontSize = 32;
style.normal.textColor = Color.white;
style.alignment = TextAnchor.UpperCenter;
GUI.Label( new Rect( 0, 32, Screen.width, Screen.height ), MessageText, style );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment