Files
FueraDeEscala/Assets/Game Kit Controller/Scripts/Camera/Others/playerCullingSystem.cs

279 lines
7.5 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerCullingSystem : MonoBehaviour
{
2026-03-29 23:03:14 -07:00
[Header ("Main Settings")]
[Space]
2026-03-29 23:03:14 -07:00
public bool cullingSystemEnabled = true;
2026-03-29 23:03:14 -07:00
public float minDistanceToApplyShader = 1;
public float minCullingShaderValue = 0.2f;
public float slerpCullingSpeed = 1;
2026-03-29 23:03:14 -07:00
public float stippleSize;
2026-03-29 23:03:14 -07:00
public string transparentAmountName = "_TransparentAmount";
2026-03-29 23:03:14 -07:00
public bool setStippleSize;
public string stippleSizeName = "_StippleSize";
2026-03-29 23:03:14 -07:00
[Space]
[Header ("Player GameObject List")]
[Space]
2026-03-29 23:03:14 -07:00
public List<GameObject> playerGameObjectList = new List<GameObject> ();
public List<GameObject> playerGameObjectWithChildList = new List<GameObject> ();
2026-03-29 23:03:14 -07:00
[Space]
[Header ("Debug")]
[Space]
2026-03-29 23:03:14 -07:00
public bool pauseCullingState;
public float cullingAmountToApply;
public bool cullingActive;
public float currentDistance;
public float currentCullingValue;
public bool materialsChangedChecked;
2026-03-29 23:03:14 -07:00
[Space]
[Header ("Components")]
[Space]
2026-03-29 23:03:14 -07:00
public Shader cullingShader;
public Transform playerTransform;
public Transform cameraTransform;
public playerCamera mainPlayerCamera;
2026-03-29 23:03:14 -07:00
public List<Renderer> rendererParts = new List<Renderer> ();
public List<Shader> originalShader = new List<Shader> ();
public List<Material> materialList = new List<Material> ();
2026-03-29 23:03:14 -07:00
float previousCullingValue;
2026-03-29 23:03:14 -07:00
bool materialsStored;
2026-03-29 23:03:14 -07:00
int transparentAmountID;
2026-03-29 23:03:14 -07:00
int stippleSizeID;
2026-03-29 23:03:14 -07:00
float previousStippleSize = -1;
2026-03-29 23:03:14 -07:00
int materialsCount;
2026-03-29 23:03:14 -07:00
Material temporalMaterial;
2026-03-29 23:03:14 -07:00
void Start ()
{
if (rendererParts.Count == 0) {
storePlayerRenderer ();
}
2026-03-29 23:03:14 -07:00
transparentAmountID = Shader.PropertyToID (transparentAmountName);
2026-03-29 23:03:14 -07:00
stippleSizeID = Shader.PropertyToID (stippleSizeName);
}
2026-03-29 23:03:14 -07:00
void FixedUpdate ()
{
if (!cullingSystemEnabled) {
return;
}
2026-03-29 23:03:14 -07:00
currentDistance = GKC_Utils.distance (playerTransform.position, cameraTransform.position);
2026-03-29 23:03:14 -07:00
if (!pauseCullingState && currentDistance < minDistanceToApplyShader) {
cullingAmountToApply = currentDistance / minDistanceToApplyShader;
2026-03-29 23:03:14 -07:00
cullingActive = true;
} else {
cullingActive = false;
2026-03-29 23:03:14 -07:00
cullingAmountToApply = 1;
}
2026-03-29 23:03:14 -07:00
if (cullingActive) {
if (mainPlayerCamera.isFirstPersonActive ()) {
cullingActive = false;
} else {
if (mainPlayerCamera.isFullBodyAwarenessActive ()) {
cullingActive = false;
} else {
bool isCameraTypeFree = mainPlayerCamera.isCameraTypeFree ();
2026-03-29 23:03:14 -07:00
if (!isCameraTypeFree) {
cullingActive = false;
} else {
if (mainPlayerCamera.isPlayerAiming ()) {
cullingAmountToApply = 1;
}
}
}
}
}
2026-03-29 23:03:14 -07:00
if (materialsChangedChecked != cullingActive) {
materialsChangedChecked = cullingActive;
2026-03-29 23:03:14 -07:00
if (!materialsStored) {
getMaterialList ();
2026-03-29 23:03:14 -07:00
materialsStored = true;
}
2026-03-29 23:03:14 -07:00
materialsCount = materialList.Count;
2026-03-29 23:03:14 -07:00
for (int j = 0; j < materialsCount; j++) {
temporalMaterial = materialList [j];
2026-03-29 23:03:14 -07:00
if (cullingActive) {
temporalMaterial.shader = cullingShader;
2026-03-29 23:03:14 -07:00
if (setStippleSize) {
if (stippleSize != previousStippleSize) {
previousStippleSize = stippleSize;
2026-03-29 23:03:14 -07:00
temporalMaterial.SetFloat (stippleSizeID, stippleSize);
}
}
} else {
temporalMaterial.shader = originalShader [j];
}
}
}
2026-03-29 23:03:14 -07:00
currentCullingValue = Mathf.Lerp (currentCullingValue, cullingAmountToApply, Time.deltaTime * slerpCullingSpeed);
2026-03-29 23:03:14 -07:00
if (currentCullingValue <= 1) {
if (Mathf.Abs (currentCullingValue - previousCullingValue) > 0.01f) {
2026-03-29 23:03:14 -07:00
materialsCount = materialList.Count;
2026-03-29 23:03:14 -07:00
for (int j = 0; j < materialsCount; j++) {
materialList [j].SetFloat (transparentAmountID, currentCullingValue);
}
2026-03-29 23:03:14 -07:00
previousCullingValue = currentCullingValue;
}
}
}
2026-03-29 23:03:14 -07:00
public void setPauseCullingState (bool state)
{
pauseCullingState = state;
}
2026-03-29 23:03:14 -07:00
public void getMaterialList ()
{
if (materialList.Count == 0) {
int rendererPartsCount = rendererParts.Count;
2026-03-29 23:03:14 -07:00
for (int i = 0; i < rendererPartsCount; i++) {
Renderer currentRenderer = rendererParts [i];
2026-03-29 23:03:14 -07:00
if (currentRenderer != null) {
2026-03-29 23:03:14 -07:00
if (currentRenderer.material.shader != null) {
int materialsLength = currentRenderer.materials.Length;
2026-03-29 23:03:14 -07:00
for (int j = 0; j < materialsLength; j++) {
2026-03-29 23:03:14 -07:00
Material currentMaterial = currentRenderer.materials [j];
2026-03-29 23:03:14 -07:00
if (currentMaterial != null) {
2026-03-29 23:03:14 -07:00
materialList.Add (currentMaterial);
originalShader.Add (currentMaterial.shader);
}
}
}
}
}
}
}
public void clearRendererList ()
{
rendererParts.Clear ();
originalShader.Clear ();
}
public void setNewPlayerGameObject (GameObject newObject)
{
playerGameObjectList.Clear ();
playerGameObjectList.Add (newObject);
}
public void storePlayerRenderer ()
{
clearRendererList ();
for (int i = 0; i < playerGameObjectList.Count; i++) {
GameObject currentObject = playerGameObjectList [i];
if (currentObject != null) {
Renderer currentRenderer = currentObject.GetComponent<Renderer> ();
rendererParts.Add (currentRenderer);
}
}
for (int i = 0; i < playerGameObjectWithChildList.Count; i++) {
GameObject currentObject = playerGameObjectWithChildList [i];
if (currentObject != null) {
Component [] components = currentObject.GetComponentsInChildren (typeof (Renderer));
int componentsLength = components.Length;
for (int j = 0; j < componentsLength; j++) {
rendererParts.Add (components [j] as Renderer);
}
}
}
if (!Application.isPlaying) {
print ("Total amount of " + rendererParts.Count + " render found");
}
}
public void storePlayerRendererFromEditor ()
{
storePlayerRenderer ();
updateComponent ();
}
public void clearRendererListFromEditor ()
{
clearRendererList ();
updateComponent ();
}
public void setCullingSystemEnabledState (bool state)
{
cullingSystemEnabled = state;
}
public void setCullingSystemEnabledStateFromEditor (bool state)
{
setCullingSystemEnabledState (state);
updateComponent ();
}
public void updateComponent ()
{
GKC_Utils.updateComponent (this);
GKC_Utils.updateDirtyScene ("Update Player Culling System", gameObject);
}
}