226 lines
5.1 KiB
C#
226 lines
5.1 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class SEGIDemo : MonoBehaviour
|
|
{
|
|
private SEGI segi;
|
|
|
|
public GameObject throwObject;
|
|
|
|
private List<GameObject> thrownObjects = new List<GameObject>();
|
|
|
|
public LayerMask grabMask;
|
|
|
|
public Text voxelResolution;
|
|
|
|
public Text reflections;
|
|
|
|
public Text cones;
|
|
|
|
public Text coneTraceSteps;
|
|
|
|
public Text infiniteBounces;
|
|
|
|
public Text gi;
|
|
|
|
public Text fpsCounter;
|
|
|
|
public Text spawnedObjects;
|
|
|
|
private Transform heldObject;
|
|
|
|
private Transform heldObjectParent;
|
|
|
|
public SEGIPreset low;
|
|
|
|
public SEGIPreset medium;
|
|
|
|
public SEGIPreset high;
|
|
|
|
public SEGIPreset ultra;
|
|
|
|
private float fps;
|
|
|
|
private float prevfps;
|
|
|
|
private int spawnedObjectsCounter;
|
|
|
|
public GameObject infoOverlay;
|
|
|
|
public float ambientWhenNoGi = 1f;
|
|
|
|
private void Start()
|
|
{
|
|
segi = GetComponent<SEGI>();
|
|
}
|
|
|
|
private void UpdateUIText()
|
|
{
|
|
voxelResolution.text = "Voxel Resolution: " + ((segi.voxelResolution == SEGI.VoxelResolution.low) ? "128" : "256");
|
|
reflections.text = "Reflections: " + (segi.doReflections ? "On" : "Off");
|
|
cones.text = "Cones: " + segi.cones;
|
|
coneTraceSteps.text = "Cone Trace Steps: " + segi.coneTraceSteps;
|
|
infiniteBounces.text = "Infinite Bounces: " + (segi.infiniteBounces ? "On" : "Off");
|
|
gi.text = "GI: " + (segi.enabled ? "On" : "Off");
|
|
fps = Mathf.Lerp(fps, Mathf.Lerp(1f / Time.deltaTime, prevfps, 0.5f), 3f * Time.deltaTime);
|
|
fpsCounter.text = "FPS: " + Mathf.RoundToInt(fps);
|
|
spawnedObjects.text = "Spawned Objects: " + spawnedObjectsCounter;
|
|
prevfps = 1f / Time.deltaTime;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
UpdateUIText();
|
|
if (Input.GetKeyDown(KeyCode.G))
|
|
{
|
|
if (segi.enabled)
|
|
{
|
|
segi.enabled = false;
|
|
gi.text = "GI: Off";
|
|
AddBadAmbient();
|
|
}
|
|
else
|
|
{
|
|
segi.enabled = true;
|
|
gi.text = "GI: On";
|
|
RemoveBadAmbient();
|
|
}
|
|
}
|
|
if (Input.GetKeyDown(KeyCode.I))
|
|
{
|
|
segi.infiniteBounces = !segi.infiniteBounces;
|
|
infiniteBounces.text = (segi.infiniteBounces ? "Infinite Bounces: On" : "Infinite Bounces: Off");
|
|
}
|
|
if (Input.GetKeyDown(KeyCode.R))
|
|
{
|
|
if (segi.voxelResolution == SEGI.VoxelResolution.high)
|
|
{
|
|
segi.voxelResolution = SEGI.VoxelResolution.low;
|
|
voxelResolution.text = "Voxel Resolution: 128";
|
|
}
|
|
else
|
|
{
|
|
segi.voxelResolution = SEGI.VoxelResolution.high;
|
|
voxelResolution.text = "Voxel Resolution: 256";
|
|
}
|
|
}
|
|
if (Input.GetKeyDown(KeyCode.Alpha1))
|
|
{
|
|
segi.ApplyPreset(low);
|
|
}
|
|
if (Input.GetKeyDown(KeyCode.Alpha2))
|
|
{
|
|
segi.ApplyPreset(medium);
|
|
}
|
|
if (Input.GetKeyDown(KeyCode.Alpha3))
|
|
{
|
|
segi.ApplyPreset(high);
|
|
}
|
|
if (Input.GetKeyDown(KeyCode.Alpha4))
|
|
{
|
|
segi.ApplyPreset(ultra);
|
|
}
|
|
if (Input.GetKeyDown(KeyCode.Q) || Input.GetKeyDown(KeyCode.R))
|
|
{
|
|
GameObject gameObject = Object.Instantiate(throwObject, base.transform.position + base.transform.forward * 4f, base.transform.rotation);
|
|
gameObject.GetComponent<Rigidbody>().AddForce(base.transform.forward * 1000f);
|
|
thrownObjects.Add(gameObject);
|
|
spawnedObjectsCounter++;
|
|
}
|
|
if (Input.GetKeyDown(KeyCode.C))
|
|
{
|
|
foreach (GameObject thrownObject in thrownObjects)
|
|
{
|
|
Object.Destroy(thrownObject);
|
|
}
|
|
thrownObjects.Clear();
|
|
spawnedObjectsCounter = 0;
|
|
}
|
|
if (Input.GetKeyDown(KeyCode.E))
|
|
{
|
|
if (heldObject == null)
|
|
{
|
|
if (Physics.Raycast(new Ray(base.transform.position, base.transform.forward), out var hitInfo, 50f, grabMask))
|
|
{
|
|
heldObject = hitInfo.transform;
|
|
heldObjectParent = heldObject.parent;
|
|
heldObject.SetParent(base.transform);
|
|
Debug.Log("Grabbed " + hitInfo.transform.gameObject.name);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (heldObjectParent != null)
|
|
{
|
|
heldObject.SetParent(heldObjectParent);
|
|
}
|
|
else
|
|
{
|
|
heldObject.SetParent(null);
|
|
}
|
|
heldObject = null;
|
|
}
|
|
}
|
|
if (Input.GetKey(KeyCode.PageUp))
|
|
{
|
|
segi.sun.intensity += 1f * Time.deltaTime;
|
|
}
|
|
if (Input.GetKey(KeyCode.PageDown))
|
|
{
|
|
segi.sun.intensity -= 1f * Time.deltaTime;
|
|
}
|
|
if (Input.GetKey(KeyCode.Home))
|
|
{
|
|
segi.softSunlight += 1f * Time.deltaTime;
|
|
segi.softSunlight = Mathf.Max(0f, segi.softSunlight);
|
|
}
|
|
if (Input.GetKey(KeyCode.End))
|
|
{
|
|
segi.softSunlight -= 1f * Time.deltaTime;
|
|
segi.softSunlight = Mathf.Max(0f, segi.softSunlight);
|
|
}
|
|
if (Input.GetKey(KeyCode.RightArrow))
|
|
{
|
|
segi.sun.transform.RotateAround(segi.sun.transform.position, Vector3.up, 30f * Time.deltaTime);
|
|
}
|
|
if (Input.GetKey(KeyCode.LeftArrow))
|
|
{
|
|
segi.sun.transform.RotateAround(segi.sun.transform.position, Vector3.down, 30f * Time.deltaTime);
|
|
}
|
|
if (Input.GetKey(KeyCode.UpArrow))
|
|
{
|
|
segi.sun.transform.Rotate(Vector3.right * 30f * Time.deltaTime);
|
|
}
|
|
if (Input.GetKey(KeyCode.DownArrow))
|
|
{
|
|
segi.sun.transform.Rotate(Vector3.left * 30f * Time.deltaTime);
|
|
}
|
|
if (Input.GetKeyDown(KeyCode.H))
|
|
{
|
|
if (infoOverlay.activeSelf)
|
|
{
|
|
infoOverlay.SetActive(value: false);
|
|
}
|
|
else
|
|
{
|
|
infoOverlay.SetActive(value: true);
|
|
}
|
|
}
|
|
if (Input.GetKeyDown(KeyCode.Escape))
|
|
{
|
|
Application.Quit();
|
|
}
|
|
}
|
|
|
|
private void RemoveBadAmbient()
|
|
{
|
|
RenderSettings.ambientIntensity = 0f;
|
|
}
|
|
|
|
private void AddBadAmbient()
|
|
{
|
|
RenderSettings.ambientIntensity = ambientWhenNoGi;
|
|
}
|
|
}
|