• Register
Forum Thread
  Posts  
Mesh fading. CapMinMax and CapMin error. (Forums : Coding & Scripting : Mesh fading. CapMinMax and CapMin error.) Locked
Thread Options
May 22 2016 Anchor

So this script is one of four posted on this site to create a mesh fade system without using layers. This is the only one I am having an issue with. I get these three errors.

Assets/Editor/MeshFade.cs(121,64): error CS1061: Type `float' does not contain a definition for `CapMinMax' and no extension method `CapMinMax' of type `float' could be found (are you missing a using directive or an assembly reference?)

Assets/Editor/MeshFade.cs(122,53): error CS1061: Type `float' does not contain a definition for `CapMin' and no extension method `CapMin' of type `float' could be found (are you missing a using directive or an assembly reference?)

Assets/Editor/MeshFade.cs(179,51): error CS1061: Type `float' does not contain a definition for `CapMin' and no extension method `CapMin' of type `float' could be found (are you missing a using directive or an assembly reference?)

I am hoping someone might be able to help me figure this out as the original author seems to be MIA. Any assistance on this would be greatly appreciated. Thank you.



using System;
using UnityEngine;
using deVoid.Extensions;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace deVoid
{
	public class MeshFade : MonoBehaviour
	{
		public string FadeGroup = "Default";

		public GameObject[] Colliders;
		private Collider[] _colliders;

//		/// <summary>
//		/// Gets or sets the sync context.
//		/// </summary>
//		/// <value>The sync context used by this component to make sure that it will not be faded in if the parent MeshFade component is currently fading or is already faded out.</value>
//		public MeshFadeSync SyncContext { get; set; }

		public Renderer[] Renderers;
		public GameObject[] ShadowCasters;

		public event Action<MeshFade> FadingOut;
		public event Action<MeshFade> FadingIn;

		public bool IsFadingOut { get; private set; }
		public bool IsFadingIn { get; private set; }

		public bool IsFadedIn { get { return CurrentFadeAmount >= 1; } }
		public bool IsFadedOut { get { return CurrentFadeAmount <= TargetFadeAmount; } }

		public float TargetFadeAmount { get; private set; }
		public float CurrentFadeAmount { get; private set; }
		public float FadeOutSpeed { get; private set; }
		public float FadeInSpeed { get; private set; }

		public Shader FadeShader { get; private set; }

		private float _fadeVelocity;
		private bool _stopFadeOut = false;
		private bool _stopFadeIn = false;

		private readonly Dictionary<Renderer, Shader> _originalShaders = new Dictionary<Renderer, Shader>{};

		private void Start ()
		{		
			StartCoroutine(StartCR());
		}
		
		private IEnumerator StartCR()
		{
			while(!ManagerMeshFade.Instance.Ready)
			{
#if _DEBUG
				Debug.Log("MeshFade.StartCR > Waiting for MeshFadeManager to finish loading.");
#endif
				yield return new WaitForEndOfFrame(); 
			}

			if(Renderers == null || Renderers.Any())
			{
				Debug.LogWarning("MeshFade.Start > Renderers are null or empty.");
				yield return 0;
			}
			
			if(Colliders == null || Colliders.Length == 0)
			{
				Debug.LogWarning("MeshFade.Start > Colliders are null or empty.");
				yield return 0;
			}

			RefreshColliders();

			// Gather shaders for swap collection
			foreach(var renderer in Renderers)
			{
				// We need to keep a list of original shaders keyed by renderer. This will let us swap out the shaders after fade in / out operations.
				if(!_originalShaders.ContainsKey(renderer))
				{ 
					var shader = Shader.Find(renderer.material.shader.name);

					Debug.Log(shader);

					_originalShaders.Add(renderer, shader); 
				}
			}

			// Register this instance with the manager
			ManagerMeshFade.Instance.Register(_colliders, this);

			// Disable all shadow casters
			if(ShadowCasters != null &amp;&amp; ShadowCasters.Length > 0)
			{
				foreach(var sc in ShadowCasters)
				{ sc.SetActive(false); }
			}

			// Init
			CurrentFadeAmount = 1;
		}

		public void FadeOut(float targetFadeOutAmount, float fadeOutSpeed, Shader shader)
		{
			if(shader == null)
			{
				Debug.LogError("MeshFade.FadeOut > Shader is null.");
				return;
			}

			// Ignore call if we are already fading out 
			if(IsFadingOut || IsFadedOut)
			{ return; }

			// Stop fade in if in progress
			StopFadeIn();

			FadeShader = shader;
			TargetFadeAmount = targetFadeOutAmount.CapMinMax(0F, 1F);
			FadeOutSpeed = fadeOutSpeed.CapMin(0F);

			// Set flags
			_stopFadeOut = false;
			IsFadingOut = true;

			// Show shadow casters
			foreach(var sc in ShadowCasters)
			{ sc.SetActive(true); }

			// Fade out meshes
			StartCoroutine(FadeOut_CR());

			// Notify listners
			if(FadingOut != null)
			{ FadingOut(this); }
		}

		private IEnumerator FadeOut_CR()
		{
			while(!_stopFadeOut)
			{
				if(CurrentFadeAmount <= TargetFadeAmount)
				{ 
					StopFadeOut();
					break; 
				}

				CurrentFadeAmount = Mathf.SmoothDamp(CurrentFadeAmount, TargetFadeAmount, ref _fadeVelocity, FadeOutSpeed);

				foreach(var renderer in Renderers)
				{
					if(renderer.material.shader.name != FadeShader.name)
					{ renderer.material.shader = FadeShader; }

					var color = renderer.material.color;
					renderer.material.color = new Color(color.r, color.g, color.b, CurrentFadeAmount);
				}

				yield return 0;
			}
		}

		public void FadeIn()
		{
			FadeIn(FadeInSpeed);
		}

		public void FadeIn(float fadeInSpeed)
		{
			// Ignore call if we are fading in already
			if(IsFadingIn || IsFadedIn)
			{ return; }

			// Stop fade out if in progress
			StopFadeOut();

			FadeInSpeed = fadeInSpeed.CapMin(0);
			
			// Set flags
			_stopFadeIn = false;
			IsFadingIn = true;

			StartCoroutine(FadeIn_CR());

			// Notify listners
			if(FadingIn != null)
			{ FadingIn(this); }

		}

		private IEnumerator FadeIn_CR()
		{
			while(!_stopFadeIn)
			{
				if(CurrentFadeAmount >= 1F)
				{ 
					StopFadeIn();
					break; 
				}

				CurrentFadeAmount = Mathf.SmoothDamp(CurrentFadeAmount, 1.1F, ref _fadeVelocity, FadeInSpeed);
				
				// Fade renderer material
				foreach(var renderer in Renderers)
				{
					if(renderer.material.shader.name != FadeShader.name)
					{ renderer.material.shader = FadeShader; }
					
					var color = renderer.material.color;
					renderer.material.color = new Color(color.r, color.g, color.b, CurrentFadeAmount);
				}
				
				yield return 0;
			}

			// Swap fade shaders with originals
			foreach(var renderer in Renderers)
			{
				var originalShader = (Shader)null;
				_originalShaders.TryGetValue(renderer, out originalShader);

				if(originalShader == null)
				{ 
					Debug.LogError("MeshFade.FadeIn_CR > OriginalShader is null.");
					continue;
				}

				renderer.material.shader = originalShader;
			}

			// Hide shadow casters
			foreach(var sc in ShadowCasters)
			{ sc.SetActive(false); }
		}

		public void StopFadeIn()
		{
			IsFadingIn = false;
			_stopFadeIn = true;

			_fadeVelocity = 1F;
			CurrentFadeAmount = 1F;
		}

		public void StopFadeOut()
		{
			IsFadingOut = false;
			_stopFadeOut = true;

			_fadeVelocity = 0F;
			CurrentFadeAmount = 0F;
		}

		/// <summary>
		/// Refreshes the internal colliders collection. Call this method every time the Colliders field is updated at runtime.
		/// </summary>
		public void RefreshColliders()
		{
			_colliders = Colliders.GetComponentsInHierarchy<Collider>().ToArray();
		}

	}
}
Reply to thread
click to sign in and post

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.