This commit is contained in:
CortexCore
2024-11-13 17:47:45 +08:00
parent c4af12acd7
commit 416e3322db
208 changed files with 2591757 additions and 1497 deletions

View File

@@ -0,0 +1,98 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace NGS.AdvancedCullingSystem.Tutorial
{
public class CameraController : MonoBehaviour
{
[SerializeField]
private float mouseSensitivity = 5f;
[SerializeField]
private float movementSpeed = 5f;
[SerializeField]
private float _height = 5f;
private float _xRotation = 0f;
private float _currentHeight;
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
_currentHeight = transform.parent.position.y;
}
private void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * 100 * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * 100 * Time.deltaTime;
mouseX = Mathf.Clamp(mouseX, -10, 10);
mouseY = Mathf.Clamp(mouseY, -10, 10);
Rotate(mouseX, mouseY);
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 movement = CalculateMovement(horizontal, vertical);
Vector3 position = transform.parent.position;
_currentHeight = Mathf.Lerp(_currentHeight, GetHeight(), movementSpeed * Time.deltaTime);
position += movement * movementSpeed * Time.deltaTime;
position.y = _currentHeight;
MoveToPoint(position);
}
private void Rotate(float mouseX, float mouseY)
{
_xRotation -= mouseY;
_xRotation = Mathf.Clamp(_xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(_xRotation, 0f, 0f);
transform.parent.Rotate(Vector3.up * mouseX);
}
private Vector3 CalculateMovement(float horizontal, float vertical)
{
Vector3 movement = transform.right * horizontal + transform.forward * vertical;
movement.y = 0f;
return movement.normalized;
}
private void MoveToPoint(Vector3 targetPoint)
{
Vector3 start = transform.parent.position;
start.y = targetPoint.y;
Ray ray = new Ray(start, targetPoint - start);
if (Physics.Raycast(ray, out RaycastHit hit, 3f))
{
return;
}
transform.parent.position = targetPoint;
}
private float GetHeight()
{
Ray ray = new Ray(transform.parent.position + Vector3.up * 20, -transform.parent.up);
if (Physics.Raycast(ray, out RaycastHit hit))
{
return hit.point.y + _height;
}
return _height;
}
}
}

View File

@@ -0,0 +1,31 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace NGS.AdvancedCullingSystem.Tutorial
{
public class DisabledRendererGizmo : MonoBehaviour
{
private Renderer _target;
private void Awake()
{
_target = GetComponent<Renderer>();
if (_target == null)
enabled = false;
}
private void OnDrawGizmos()
{
if (_target == null)
return;
if (_target.enabled)
return;
Gizmos.color = Color.red;
Gizmos.DrawWireCube(_target.bounds.center, _target.bounds.size);
}
}
}

View File

@@ -0,0 +1,33 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using NGS.AdvancedCullingSystem.Static;
namespace NGS.AdvancedCullingSystem.Tutorial
{
public class DisabledTargetGizmo : MonoBehaviour
{
private CullingTarget _target;
private void Awake()
{
_target = GetComponent<CullingTarget>();
if (_target == null)
enabled = false;
}
private void OnDrawGizmos()
{
if (_target == null)
return;
if (_target.enabled)
return;
Gizmos.color = Color.red;
Gizmos.DrawWireCube(_target.Bounds.center, _target.Bounds.size);
}
}
}

View File

@@ -0,0 +1,47 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace NGS.AdvancedCullingSystem.Tutorial
{
public class Spawner : MonoBehaviour
{
[SerializeField]
private GameObject _prefab;
[SerializeField]
private int _maxInstances = 2000;
[SerializeField]
private float _delay = 0.02f;
private int _counter;
private float _timer;
private void Update()
{
_timer += Time.deltaTime;
if (_timer > _delay)
{
Spawn();
_timer = 0;
_counter++;
if (_counter > _maxInstances)
enabled = false;
}
}
private void Spawn()
{
GameObject instance = Instantiate(_prefab);
instance.transform.position = transform.position;
instance.transform.rotation = Random.rotation;
instance.transform.localScale = Vector3.one * Random.Range(1, 2.5f);
}
}
}