Created
August 18, 2023 21:15
-
-
Save farism/eef91150524aeb4fa0b9d5a9aeb8c7a7 to your computer and use it in GitHub Desktop.
Viewcone
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class Viewcone : MonoBehaviour | |
{ | |
static readonly int alertScaleID = Shader.PropertyToID("_AlertScale"); | |
static readonly int farViewDistanceID = Shader.PropertyToID("_FarViewDistance"); | |
static readonly int isAttackingID = Shader.PropertyToID("_IsAttacking"); | |
static readonly int nearViewDistanceID = Shader.PropertyToID("_NearViewDistance"); | |
static readonly int originYID = Shader.PropertyToID("_OriginY"); | |
static readonly int viewAngleID = Shader.PropertyToID("_ViewAngle"); | |
static readonly int viewDepthTexturedID = Shader.PropertyToID("_ViewDepthTexture"); | |
static readonly int viewSpaceMatrixID = Shader.PropertyToID("_ViewSpaceMatrix"); | |
public Enemy enemy; // TODO get rid of this ref | |
[SerializeField] Camera cam; | |
[SerializeField] Transform container; | |
[SerializeField] GameObject mesh; | |
int storiesAbove { get; set; } = 1; | |
int storiesBelow { get; set; } = 2; | |
Material material; | |
public void SetEnemy(Enemy e, bool force = false) | |
{ | |
if (enemy && !force) | |
{ | |
return; | |
} | |
enemy = e; | |
gameObject.SetActive(true); | |
} | |
public void ClearEnemy(Enemy e) | |
{ | |
if (enemy == e) | |
{ | |
enemy = null; | |
gameObject.SetActive(false); | |
} | |
} | |
void Awake() | |
{ | |
cam.targetTexture = new RenderTexture(cam.pixelWidth, cam.pixelHeight, 32, RenderTextureFormat.Depth); // TODO adjust to 24bit depth buffer for perf? | |
cam.aspect = 1; | |
var meshRenderer = mesh.GetComponent<MeshRenderer>(); | |
material = meshRenderer.material; // generate a copy of the material | |
material.SetTexture(viewDepthTexturedID, cam.targetTexture); | |
meshRenderer.material = material; | |
} | |
void Start() | |
{ | |
gameObject.SetActive(false); | |
} | |
void Update() | |
{ | |
if (!enemy) | |
{ | |
return; | |
} | |
container.localRotation = Quaternion.Euler(0, enemy.viewconeRotation, 0); | |
transform.position = enemy.transform.position; | |
transform.rotation = enemy.transform.rotation; | |
cam.fieldOfView = enemy.viewconeFov; | |
cam.farClipPlane = enemy.viewconeFarRange; | |
var meshSize = enemy.viewconeFarRange * 2; | |
var meshHeight = (storiesAbove + storiesBelow) * 3; | |
var meshOffset = -(meshHeight / 2) + (storiesAbove * 3); | |
mesh.transform.localScale = new Vector3(meshSize, meshHeight, meshSize); | |
mesh.transform.localPosition = new Vector3(0, meshOffset, 0); | |
// material.SetFloat(isAttackingID, enemy.isAttacking == 1 ? 1 : 0); | |
material.SetFloat(alertScaleID, enemy.viewconeAlertScale * 0.5f); | |
material.SetFloat(viewAngleID, enemy.viewconeFov); | |
material.SetFloat(nearViewDistanceID, enemy.viewconeNearRange); | |
material.SetFloat(farViewDistanceID, enemy.viewconeFarRange); | |
material.SetFloat(originYID, transform.position.y); | |
material.SetMatrix(viewSpaceMatrixID, cam.projectionMatrix * cam.worldToCameraMatrix); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment