using UnityEngine; namespace Artngame.SKYMASTER { public class ParticleShadowsSM : MonoBehaviour { private Color Start_color; public Transform Ref_light; public ParticleSystem p2; private ParticleSystem.Particle[] ParticleList; public bool affectAlpha; public bool Debug_on; public float Shadow_factor = 0.5f; public float Shadow_max_dist = 1000f; public Color ShadowColor = new Color(0.5f, 0.5f, 0.5f, 0.5f); private int counter; private void Start() { Start_color = p2.main.startColor.color; } private void Update() { if (p2 == null) { Debug.Log("Please insert a particle system"); return; } ParticleSystem particleSystem = p2; ParticleList = new ParticleSystem.Particle[particleSystem.particleCount]; particleSystem.GetParticles(ParticleList); for (int i = 0; i < ParticleList.Length; i++) { Vector3 direction = ParticleList[i].position - Ref_light.position; Ray ray = new Ray(Ref_light.position, direction); ray = new Ray(ParticleList[i].position, -Ref_light.forward); if (Debug_on) { Debug.DrawRay(ParticleList[i].position, -Ref_light.forward); Debug.DrawLine(ParticleList[i].position, Ref_light.position); } RaycastHit hitInfo = default(RaycastHit); if (Physics.Raycast(ray.origin, ray.direction, out hitInfo, Shadow_max_dist)) { if (affectAlpha) { ParticleList[i].startColor = ShadowColor * Shadow_factor; } else { float a = (int)ParticleList[i].GetCurrentColor(particleSystem).a; ParticleList[i].startColor = ShadowColor * Shadow_factor; ParticleList[i].startColor = new Color((int)ParticleList[i].startColor.r, (int)ParticleList[i].startColor.g, (int)ParticleList[i].startColor.b, a); } if (Debug_on) { Debug.DrawLine(ParticleList[i].position, hitInfo.point, Color.red, 5f); } } else if (affectAlpha) { ParticleList[i].startColor = Start_color; } else { float a2 = (int)ParticleList[i].GetCurrentColor(particleSystem).a; ParticleList[i].startColor = Start_color; ParticleList[i].startColor = new Color((int)ParticleList[i].startColor.r, (int)ParticleList[i].startColor.g, (int)ParticleList[i].startColor.b, a2); } counter++; if (counter > ParticleList.Length) { counter = 0; } } p2.SetParticles(ParticleList, particleSystem.particleCount); } } }