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

383 lines
12 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class setTransparentSurfacesSystem : MonoBehaviour
{
2026-03-29 23:03:14 -07:00
[Header ("Main Settings")]
[Space]
2026-03-29 23:03:14 -07:00
public bool checkSurfaceEnabled = true;
2026-03-29 23:03:14 -07:00
public Shader transparentShader;
public float alphaBlendSpeed = 5;
public float alphaTransparency = 0.2f;
2026-03-29 23:03:14 -07:00
public bool useAlphaColor = true;
public string transparentAmountName = "_TransparentAmount";
2026-03-29 23:03:14 -07:00
public string colorPropertyName = "_Color";
[Space]
2026-03-29 23:03:14 -07:00
[Header ("Debug")]
[Space]
2026-03-29 23:03:14 -07:00
public List<surfaceInfo> surfaceInfoList = new List<surfaceInfo> ();
public List<GameObject> surfaceGameObjectList = new List<GameObject> ();
2026-03-29 23:03:14 -07:00
surfaceInfo newSurfaceInfo;
materialInfo newMaterialInfo;
2026-03-29 23:03:14 -07:00
int transparentAmountID = -1;
int colorID = -1;
2026-03-29 23:03:14 -07:00
public const string mainManagerName = "Set Transparent Surfaces Manager";
2026-03-29 23:03:14 -07:00
public static string getMainManagerName ()
{
return mainManagerName;
}
2026-03-29 23:03:14 -07:00
private static setTransparentSurfacesSystem _setTransparentSurfacesSystemInstance;
2026-03-29 23:03:14 -07:00
public static setTransparentSurfacesSystem Instance { get { return _setTransparentSurfacesSystemInstance; } }
2026-03-29 23:03:14 -07:00
bool instanceInitialized;
2026-03-29 23:03:14 -07:00
public void getComponentInstance ()
{
if (instanceInitialized) {
return;
}
2026-03-29 23:03:14 -07:00
if (_setTransparentSurfacesSystemInstance != null && _setTransparentSurfacesSystemInstance != this) {
Destroy (this.gameObject);
2026-03-29 23:03:14 -07:00
return;
}
2026-03-29 23:03:14 -07:00
_setTransparentSurfacesSystemInstance = this;
2026-03-29 23:03:14 -07:00
instanceInitialized = true;
}
2026-03-29 23:03:14 -07:00
void Awake ()
{
getComponentInstance ();
}
2026-03-29 23:03:14 -07:00
public void addNewSurface (GameObject newSurface, Shader customShader)
{
if (!checkSurfaceEnabled) {
return;
}
2026-03-29 23:03:14 -07:00
newSurfaceInfo = new surfaceInfo ();
newSurfaceInfo.surfaceGameObject = newSurface;
2026-03-29 23:03:14 -07:00
Component [] components = newSurfaceInfo.surfaceGameObject.GetComponentsInChildren (typeof (Renderer));
2026-03-29 23:03:14 -07:00
int componentsLength = components.Length;
2026-03-29 23:03:14 -07:00
if (colorID == -1) {
colorID = Shader.PropertyToID (colorPropertyName);
}
for (int i = 0; i < componentsLength; i++) {
2026-03-29 23:03:14 -07:00
Renderer child = components [i] as Renderer;
2026-03-29 23:03:14 -07:00
if (child.material.shader != null) {
int materialsLength = child.materials.Length;
2026-03-29 23:03:14 -07:00
for (int j = 0; j < materialsLength; j++) {
Material currentMaterial = child.materials [j];
2026-03-29 23:03:14 -07:00
newMaterialInfo = new materialInfo ();
newMaterialInfo.surfaceMaterial = currentMaterial;
2026-03-29 23:03:14 -07:00
float colorValue = 1;
if (colorID != -1) {
2026-03-29 23:03:14 -07:00
if (currentMaterial.HasProperty (colorID)) {
Color alpha = currentMaterial.color;
2026-03-29 23:03:14 -07:00
colorValue = alpha.a;
}
}
2026-03-29 23:03:14 -07:00
newMaterialInfo.originalAlphaColor = colorValue;
2026-03-29 23:03:14 -07:00
newSurfaceInfo.materialInfoList.Add (newMaterialInfo);
newSurfaceInfo.originalShader.Add (currentMaterial.shader);
2026-03-29 23:03:14 -07:00
if (customShader != null) {
currentMaterial.shader = customShader;
2026-03-29 23:03:14 -07:00
newSurfaceInfo.customShader = customShader;
} else {
currentMaterial.shader = transparentShader;
2026-03-29 23:03:14 -07:00
newSurfaceInfo.customShader = transparentShader;
}
2026-03-29 23:03:14 -07:00
newSurfaceInfo.materialsAmount++;
}
}
}
2026-03-29 23:03:14 -07:00
setSurfaceTransparent (newSurfaceInfo);
2026-03-29 23:03:14 -07:00
surfaceInfoList.Add (newSurfaceInfo);
2026-03-29 23:03:14 -07:00
surfaceGameObjectList.Add (newSurfaceInfo.surfaceGameObject);
}
2026-03-29 23:03:14 -07:00
public bool listContainsSurface (GameObject surfaceToCheck)
{
if (!checkSurfaceEnabled) {
return false;
}
2026-03-29 23:03:14 -07:00
if (surfaceGameObjectList.Contains (surfaceToCheck)) {
return true;
}
2026-03-29 23:03:14 -07:00
return false;
}
2026-03-29 23:03:14 -07:00
public void setSurfaceToRegular (int surfaceIndex, bool removeSurfaceAtEnd)
{
if (!checkSurfaceEnabled) {
return;
}
2026-03-29 23:03:14 -07:00
surfaceInfo currentSurfaceToCheck = surfaceInfoList [surfaceIndex];
2026-03-29 23:03:14 -07:00
currentSurfaceToCheck.changingToOriginalActive = true;
currentSurfaceToCheck.currentMaterialsChangedAmount = 0;
currentSurfaceToCheck.changingToTransparentActive = false;
2026-03-29 23:03:14 -07:00
currentSurfaceToCheck.changingToOriginalTemporaly = !removeSurfaceAtEnd;
2026-03-29 23:03:14 -07:00
for (int j = 0; j < currentSurfaceToCheck.materialInfoList.Count; j++) {
setAlphaValue (currentSurfaceToCheck, j, currentSurfaceToCheck.materialInfoList [j].surfaceMaterial,
false, currentSurfaceToCheck.originalShader [j], currentSurfaceToCheck.materialInfoList [j].originalAlphaColor, removeSurfaceAtEnd);
}
}
2026-03-29 23:03:14 -07:00
public void checkSurfaceToSetTransparentAgain (GameObject surfaceToCheck)
{
if (!checkSurfaceEnabled) {
return;
}
2026-03-29 23:03:14 -07:00
int surfaceIndex = surfaceGameObjectList.IndexOf (surfaceToCheck);
2026-03-29 23:03:14 -07:00
if (surfaceIndex > -1) {
surfaceInfo currentSurfaceToCheck = surfaceInfoList [surfaceIndex];
2026-03-29 23:03:14 -07:00
if (currentSurfaceToCheck.changingToOriginalActive && !currentSurfaceToCheck.changingToOriginalTemporaly) {
// print ("changing to original, stopping");
setSurfaceTransparent (currentSurfaceToCheck);
}
}
}
2026-03-29 23:03:14 -07:00
public void setSurfaceTransparent (surfaceInfo currentSurfaceToCheck)
{
currentSurfaceToCheck.changingToOriginalActive = false;
currentSurfaceToCheck.currentMaterialsChangedAmount = 0;
currentSurfaceToCheck.changingToTransparentActive = true;
for (int j = 0; j < currentSurfaceToCheck.materialInfoList.Count; j++) {
setAlphaValue (currentSurfaceToCheck, j, currentSurfaceToCheck.materialInfoList [j].surfaceMaterial,
true, currentSurfaceToCheck.originalShader [j], currentSurfaceToCheck.materialInfoList [j].originalAlphaColor, false);
}
}
2026-03-29 23:03:14 -07:00
public void setAlphaValue (surfaceInfo currentSurfaceToCheck, int surfaceIndex, Material currentMaterial,
bool changingToTransparent, Shader originalShader, float originalAlphaColor, bool removeSurfaceAtEnd)
{
if (currentSurfaceToCheck.materialInfoList [surfaceIndex].alphaBlendCoroutine != null) {
StopCoroutine (currentSurfaceToCheck.materialInfoList [surfaceIndex].alphaBlendCoroutine);
}
2026-03-29 23:03:14 -07:00
currentSurfaceToCheck.materialInfoList [surfaceIndex].alphaBlendCoroutine =
StartCoroutine (setAlphaValueCoroutine (currentSurfaceToCheck, currentMaterial,
changingToTransparent, originalShader, originalAlphaColor, removeSurfaceAtEnd));
}
2026-03-29 23:03:14 -07:00
IEnumerator setAlphaValueCoroutine (surfaceInfo currentSurfaceToCheck, Material currentMaterial, bool changingToTransparent,
Shader originalShader, float originalAlphaColor, bool removeSurfaceAtEnd)
{
float targetValue = originalAlphaColor;
2026-03-29 23:03:14 -07:00
if (changingToTransparent) {
targetValue = alphaTransparency;
2026-03-29 23:03:14 -07:00
if (currentSurfaceToCheck.changingToOriginalTemporaly) {
currentMaterial.shader = currentSurfaceToCheck.customShader;
}
}
2026-03-29 23:03:14 -07:00
if (colorID == -1) {
colorID = Shader.PropertyToID (colorPropertyName);
}
2026-03-29 23:03:14 -07:00
if (useAlphaColor) {
if (currentMaterial.HasProperty (colorID)) {
Color alpha = currentMaterial.GetColor (colorID);
2026-03-29 23:03:14 -07:00
while (alpha.a != targetValue) {
alpha.a = Mathf.MoveTowards (alpha.a, targetValue, Time.deltaTime * alphaBlendSpeed);
2026-03-29 23:03:14 -07:00
currentMaterial.SetColor (colorID, alpha);
2026-03-29 23:03:14 -07:00
yield return null;
}
}
//Color alpha = currentMaterial.color;
2026-03-29 23:03:14 -07:00
//while (alpha.a != targetValue) {
// alpha.a = Mathf.MoveTowards (alpha.a, targetValue, Time.deltaTime * alphaBlendSpeed);
// currentMaterial.color = alpha;
2026-03-29 23:03:14 -07:00
// yield return null;
//}
} else {
if (transparentAmountID == -1) {
transparentAmountID = Shader.PropertyToID (transparentAmountName);
}
if (currentMaterial.HasProperty (transparentAmountID)) {
float currentTransparentAmount = currentMaterial.GetFloat (transparentAmountID);
while (currentTransparentAmount != targetValue) {
currentTransparentAmount = Mathf.MoveTowards (currentTransparentAmount, targetValue, Time.deltaTime * alphaBlendSpeed);
currentMaterial.SetFloat (transparentAmountID, currentTransparentAmount);
yield return null;
}
} else {
yield return null;
}
2026-03-29 23:03:14 -07:00
}
2026-03-29 23:03:14 -07:00
if (!changingToTransparent) {
currentMaterial.shader = originalShader;
}
2026-03-29 23:03:14 -07:00
if (currentSurfaceToCheck.changingToOriginalActive) {
currentSurfaceToCheck.currentMaterialsChangedAmount++;
2026-03-29 23:03:14 -07:00
if (removeSurfaceAtEnd) {
if (currentSurfaceToCheck.currentMaterialsChangedAmount == currentSurfaceToCheck.materialsAmount) {
surfaceInfoList.Remove (currentSurfaceToCheck);
2026-03-29 23:03:14 -07:00
surfaceGameObjectList.Remove (currentSurfaceToCheck.surfaceGameObject);
}
}
} else {
currentSurfaceToCheck.currentMaterialsChangedAmount++;
2026-03-29 23:03:14 -07:00
if (currentSurfaceToCheck.currentMaterialsChangedAmount == currentSurfaceToCheck.materialsAmount) {
currentSurfaceToCheck.changingToTransparentActive = false;
2026-03-29 23:03:14 -07:00
currentSurfaceToCheck.changingToOriginalTemporaly = false;
}
}
}
2026-03-29 23:03:14 -07:00
public void checkSurfacesToRemove ()
{
if (!checkSurfaceEnabled) {
return;
}
2026-03-29 23:03:14 -07:00
for (int i = 0; i < surfaceInfoList.Count; i++) {
surfaceInfo currentSurfaceToCheck = surfaceInfoList [i];
2026-03-29 23:03:14 -07:00
if (currentSurfaceToCheck.numberOfPlayersFound == 0) {
setSurfaceToRegular (i, true);
}
}
}
2026-03-29 23:03:14 -07:00
public void changeSurfacesToTransparentOrRegularTemporaly (int playerID, int surfaceIndex, bool setTransparentSurface)
{
if (surfaceInfoList [surfaceIndex].playerIDs.Contains (playerID)) {
if (setTransparentSurface) {
// print ("transparent");
setSurfaceTransparent (surfaceInfoList [surfaceIndex]);
} else {
// print ("regular");
setSurfaceToRegular (surfaceIndex, false);
}
}
}
2026-03-29 23:03:14 -07:00
public void addPlayerIDToSurface (int playerID, GameObject surfaceToCheck)
{
if (!checkSurfaceEnabled) {
return;
}
2026-03-29 23:03:14 -07:00
for (int i = 0; i < surfaceInfoList.Count; i++) {
if (surfaceInfoList [i].surfaceGameObject == surfaceToCheck) {
if (!surfaceInfoList [i].playerIDs.Contains (playerID)) {
surfaceInfoList [i].playerIDs.Add (playerID);
2026-03-29 23:03:14 -07:00
surfaceInfoList [i].numberOfPlayersFound++;
2026-03-29 23:03:14 -07:00
return;
}
}
}
}
2026-03-29 23:03:14 -07:00
public void removePlayerIDToSurface (int playerID, int surfaceIndex)
{
if (!checkSurfaceEnabled) {
return;
}
2026-03-29 23:03:14 -07:00
if (surfaceInfoList [surfaceIndex].playerIDs.Contains (playerID)) {
surfaceInfoList [surfaceIndex].playerIDs.Remove (playerID);
2026-03-29 23:03:14 -07:00
surfaceInfoList [surfaceIndex].numberOfPlayersFound--;
}
}
2026-03-29 23:03:14 -07:00
[System.Serializable]
public class surfaceInfo
{
public GameObject surfaceGameObject;
public List<Shader> originalShader = new List<Shader> ();
public List<materialInfo> materialInfoList = new List<materialInfo> ();
public List<int> playerIDs = new List<int> ();
public int numberOfPlayersFound;
2026-03-29 23:03:14 -07:00
public bool changingToTransparentActive;
public bool changingToOriginalActive;
public bool changingToOriginalTemporaly;
2026-03-29 23:03:14 -07:00
public int materialsAmount;
public int currentMaterialsChangedAmount;
2026-03-29 23:03:14 -07:00
public Shader customShader;
}
[System.Serializable]
public class materialInfo
{
public Material surfaceMaterial;
public Coroutine alphaBlendCoroutine;
public float originalAlphaColor;
}
}