ProFlare.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. //This asset was uploaded by https://unityassetcollection.com
  2. /// ProFlares - v1.08 - Copyright 2014-2015 All rights reserved - ProFlares.com
  3. /// <summary>
  4. /// ProFlare.cs
  5. /// Holds the definition of a flare and all of its features.
  6. /// </summary>
  7. using UnityEngine;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. [System.Serializable]
  11. public class SubElement{
  12. public Color color = Color.white;
  13. public Color colorFinal = Color.white;
  14. public float position = 0;
  15. public Vector3 offset = Vector2.zero;
  16. public float angle = 0;
  17. public float scale = 0;
  18. public float random = 0.5f;
  19. public float random2= 0.5f;
  20. public float RandomScaleSeed= 0.5f;
  21. public float RandomColorSeedR = 0.5f;
  22. public float RandomColorSeedG = 0.5f;
  23. public float RandomColorSeedB = 0.5f;
  24. public float RandomColorSeedA = 0.5f;
  25. }
  26. [System.Serializable]
  27. public class ProFlareElement{
  28. public bool Editing;
  29. public bool Visible = true;
  30. //Element's texture index inside the texture atlas.
  31. public int elementTextureID;
  32. //Elements Sprite name from the texture atlas, this isn't checked at runtime. Its only used to help stop flares breaking when the atlas changes.
  33. public string SpriteName;
  34. public ProFlare flare;
  35. public ProFlareAtlas flareAtlas;
  36. public float Brightness = 1;
  37. public float Scale = 1;
  38. public float ScaleRandom = 0;
  39. public float ScaleFinal = 1;
  40. public Vector4 RandomColorAmount = Vector4.zero;
  41. //Element OffSet Properties
  42. public float position;
  43. public bool useRangeOffset = false;
  44. public float SubElementPositionRange_Min = -1f;
  45. public float SubElementPositionRange_Max = 1f;
  46. public float SubElementAngleRange_Min = -180f;
  47. public float SubElementAngleRange_Max = 180f;
  48. public Vector3 OffsetPosition;
  49. public Vector3 Anamorphic = Vector3.zero;
  50. public Vector3 OffsetPostion = Vector3.zero;
  51. //Element Rotation Properties
  52. public float angle = 0;
  53. public float FinalAngle = 0; //Final angle used by FlareBatch
  54. public bool useRandomAngle;
  55. public bool useStarRotation;
  56. public float AngleRandom_Min;
  57. public float AngleRandom_Max;
  58. public bool OrientToSource;
  59. public bool rotateToFlare;
  60. public float rotationSpeed;
  61. public float rotationOverTime;
  62. //Colour Properties,
  63. public bool useColorRange;
  64. public Color ElementFinalColor;
  65. public Color ElementTint = new Color(1,1,1,0.33f);
  66. public Color SubElementColor_Start = Color.white;
  67. public Color SubElementColor_End = Color.white;
  68. //Scale Curve
  69. public bool useScaleCurve;
  70. public AnimationCurve ScaleCurve = new AnimationCurve(new Keyframe(0, 0.1f), new Keyframe(0.5f, 1.0f), new Keyframe(1.0f, 0.1f));
  71. //Override Properties
  72. public bool OverrideDynamicEdgeBoost;
  73. public float DynamicEdgeBoostOverride = 1f;
  74. public bool OverrideDynamicCenterBoost;
  75. public float DynamicCenterBoostOverride = 1f;
  76. public bool OverrideDynamicEdgeBrightness;
  77. public float DynamicEdgeBrightnessOverride = 0.4f;
  78. public bool OverrideDynamicCenterBrightness;
  79. public float DynamicCenterBrightnessOverride = 0.4f;
  80. //public SubElement[] subElements;
  81. public List<SubElement> subElements = new List<SubElement>();
  82. public Vector2 size = Vector2.one;
  83. public enum Type{
  84. Single,
  85. Multi
  86. }
  87. #if UNITY_EDITOR
  88. public bool EditDynamicTriggering;
  89. public bool EditOcclusion;
  90. public bool ElementSetting;
  91. public bool OffsetSetting;
  92. public bool ColorSetting;
  93. public bool ScaleSetting;
  94. public bool RotationSetting;
  95. public bool OverrideSetting;
  96. public Texture2D colorTexture;
  97. public bool colorTextureDirty;
  98. #endif
  99. public Type type;
  100. }
  101. [ExecuteInEditMode]
  102. public class ProFlare : MonoBehaviour {
  103. //Required External Objects
  104. public ProFlareAtlas _Atlas;
  105. public ProFlareBatch[] FlareBatches = new ProFlareBatch[0];
  106. public bool EditingAtlas;
  107. public bool isVisible = true;
  108. //
  109. public List<ProFlareElement> Elements = new List<ProFlareElement>();
  110. //Cached Components
  111. public Transform thisTransform;
  112. public GameObject thisGameObject;
  113. //Screen space Flare Position
  114. public Vector3 LensPosition;
  115. //Global Settings
  116. public bool EditGlobals;
  117. public float GlobalScale = 100;
  118. public bool MultiplyScaleByTransformScale = false;
  119. public float GlobalBrightness = 1;
  120. public Color GlobalTintColor = Color.white;
  121. //Distance Fall off
  122. public bool useMaxDistance = true;
  123. public bool useDistanceScale = true;
  124. public bool useDistanceFade = true;
  125. public float GlobalMaxDistance = 150f;
  126. //Angle Culling Properties
  127. public bool UseAngleLimit;
  128. public float maxAngle = 90f;
  129. public bool UseAngleScale;
  130. public bool UseAngleBrightness = true;
  131. public bool UseAngleCurve;
  132. public AnimationCurve AngleCurve = new AnimationCurve(new Keyframe(0, 0f), new Keyframe(1, 1.0f));
  133. //Occlusion Properties
  134. public LayerMask mask = 1;
  135. public bool RaycastPhysics;
  136. public bool Occluded = false;
  137. public float OccludeScale = 1;
  138. public bool pixelBasedOcclusion = false;
  139. // public LayerMask pixelMask = 1>>8;
  140. public LayerMask pixelLayerMask = ~(1<<8);
  141. public float occlusionScaleSpeed = 4;
  142. public float occlusionBrightnessSpeed = 32;
  143. #if UNITY_EDITOR
  144. public GameObject OccludingObject;
  145. #endif
  146. //Editor Inspector Toggles.
  147. #if UNITY_EDITOR
  148. public bool EditDynamicTriggering;
  149. public bool EditOcclusion;
  150. public bool IoSetting;
  151. #endif
  152. public float OffScreenFadeDist = 0.2f;
  153. //Dynamic Edge Properties
  154. public bool useDynamicEdgeBoost = false;
  155. public float DynamicEdgeBoost = 1f;
  156. public float DynamicEdgeBrightness = 0.1f;
  157. public float DynamicEdgeRange = 0.3f;
  158. public float DynamicEdgeBias = -0.1f;
  159. public AnimationCurve DynamicEdgeCurve = new AnimationCurve(new Keyframe(0, 0f), new Keyframe(0.5f, 1), new Keyframe(1, 0f));
  160. //Dynamic Center Properties
  161. public bool useDynamicCenterBoost = false;
  162. public float DynamicCenterBoost = 1f;
  163. public float DynamicCenterBrightness = 0.2f;
  164. public float DynamicCenterRange = 0.3f;
  165. public float DynamicCenterBias = 0.0f;
  166. public bool neverCull;
  167. public bool sunMode;
  168. public Transform sunModeTransformA;
  169. public Transform sunModeTransformB;
  170. void Awake(){
  171. DisabledPlayMode = false;
  172. Initialised = false;
  173. //Cache Transform.
  174. thisTransform = transform;
  175. thisGameObject = gameObject;
  176. }
  177. void Start(){
  178. //Cache Transform.
  179. //thisTransform = transform;
  180. if((_Atlas != null)&&(FlareBatches.Length == 0)){
  181. PopulateFlareBatches();
  182. }
  183. if(!Initialised){
  184. Init();
  185. }
  186. //Make Sure All elements reference the correct atlas.
  187. for(int i = 0; i < Elements.Count; i++){
  188. Elements[i].flareAtlas = _Atlas;
  189. Elements[i].flare = this;
  190. }
  191. if (sunMode){
  192. if ((sunModeTransformA == null)||(sunModeTransformB == null)) {
  193. setupSunMode();
  194. }
  195. }
  196. }
  197. void PopulateFlareBatches(){
  198. //Atlas is set but Flare is not try and find a FlareBatch with the same atlas;
  199. ProFlareBatch[] flareBatchs = GameObject.FindObjectsOfType(typeof(ProFlareBatch)) as ProFlareBatch[];
  200. int matchCount = 0;
  201. foreach (ProFlareBatch flareBatch in flareBatchs){
  202. if (flareBatch._atlas == _Atlas){
  203. matchCount++;
  204. }
  205. }
  206. FlareBatches = new ProFlareBatch[matchCount];
  207. int count = 0;
  208. foreach (ProFlareBatch flareBatch in flareBatchs){
  209. if (flareBatch._atlas == _Atlas){
  210. FlareBatches[count] = flareBatch;
  211. count++;
  212. }
  213. }
  214. }
  215. bool Initialised;
  216. void Init(){
  217. if(thisTransform == null)
  218. thisTransform = transform;
  219. if(_Atlas == null)
  220. return;
  221. PopulateFlareBatches();
  222. //Make Sure All elements reference the correct atlas.
  223. for(int i = 0; i < Elements.Count; i++){
  224. Elements[i].flareAtlas = _Atlas;
  225. }
  226. //All Conditions met, add flare to flare batch.
  227. for(int i = 0; i < FlareBatches.Length; i++){
  228. if(FlareBatches[i] != null)
  229. if(FlareBatches[i]._atlas == _Atlas)
  230. FlareBatches[i].AddFlare(this);
  231. }
  232. Initialised = true;
  233. }
  234. public void ReInitialise(){
  235. Initialised = false;
  236. Init();
  237. }
  238. /// <summary>
  239. /// Rebuilding the FlareBatch geometry is more expensive than just updating it, and can cause a small hickup in the frame
  240. /// While in play mode, we avoid doing a full rebuild by using a 'soft' enable/disable. If Flare isn't in the FlareBatches list it will always be added.
  241. /// </summary>
  242. public bool DisabledPlayMode;
  243. void OnEnable(){
  244. if(Application.isPlaying && DisabledPlayMode){
  245. DisabledPlayMode = false;
  246. }else{
  247. if(_Atlas)
  248. for(int i = 0; i < FlareBatches.Length; i++){
  249. if(FlareBatches[i] != null){
  250. FlareBatches[i].dirty = true;
  251. }else{
  252. Initialised = false;
  253. }
  254. }
  255. Init();
  256. }
  257. }
  258. void OnDisable(){
  259. if(Application.isPlaying){
  260. DisabledPlayMode = true;
  261. }else{
  262. for(int i = 0; i < FlareBatches.Length; i++){
  263. if(FlareBatches[i] != null){
  264. FlareBatches[i].RemoveFlare(this);// .Flares.Remove(this);
  265. FlareBatches[i].dirty = true;
  266. }else{
  267. Initialised = false;
  268. }
  269. }
  270. }
  271. }
  272. public void setupSunMode(){
  273. GameObject ProFlareSunTransformGO_pos = new GameObject (name + "_SunTransform_Pos");
  274. sunModeTransformA = ProFlareSunTransformGO_pos.transform;
  275. GameObject ProFlareSunTransformGO_rot = new GameObject(name+"_SunTransform_Rot");
  276. sunModeTransformB = ProFlareSunTransformGO_rot.transform;
  277. sunModeTransformB.parent = sunModeTransformA;
  278. sunModeTransformB.localPosition = Vector3.back;
  279. }
  280. /// <summary>
  281. /// If Flare is destroyed, it will be removed from the FlaresBatch List.
  282. /// </summary>
  283. void OnDestroy(){
  284. for(int i = 0; i < FlareBatches.Length; i++){
  285. if(FlareBatches[i] != null){
  286. // FlareBatches[i].Flares.Remove(this);
  287. FlareBatches[i].RemoveFlare(this);// .Flares.Remove(this);
  288. FlareBatches[i].dirty = true;
  289. }else{
  290. Initialised = false;
  291. }
  292. }
  293. if (sunMode) {
  294. if (Application.isPlaying) {
  295. if(sunModeTransformA != null)
  296. Destroy (sunModeTransformA.gameObject);
  297. if(sunModeTransformB != null)
  298. Destroy (sunModeTransformB.gameObject);
  299. } else {
  300. if(sunModeTransformA != null)
  301. DestroyImmediate (sunModeTransformA.gameObject);
  302. if(sunModeTransformB != null)
  303. DestroyImmediate (sunModeTransformB.gameObject);
  304. }
  305. }
  306. }
  307. /// <summary>
  308. /// Helper piece of code that only runs while in the editor.
  309. /// It checks the state of the flare against its atlas.
  310. /// </summary>
  311. #if UNITY_EDITOR
  312. void LateUpdate(){
  313. for(int i = 0; i < Elements.Count; i++){
  314. if(Elements[i].SpriteName == ""){
  315. Elements[i].SpriteName = _Atlas.elementsList[Elements[i].elementTextureID].name;
  316. }
  317. }
  318. for(int i = 0; i < Elements.Count; i++){
  319. bool error = false;
  320. if(!_Atlas)
  321. continue;
  322. if(Elements[i].elementTextureID >= _Atlas.elementsList.Count){
  323. Debug.LogWarning("ProFlares - Elements ID No longer exists");
  324. error = true;
  325. }
  326. if(!error)
  327. if(Elements[i].SpriteName != _Atlas.elementsList[Elements[i].elementTextureID].name){
  328. Debug.LogWarning("Pro Flares - Where did my elements go? - Lets try and find it. -" + Elements[i].SpriteName,gameObject);
  329. error = true;
  330. }
  331. if(error){
  332. bool Found = false;
  333. for(int i2 = 0; i2 < _Atlas.elementsList.Count; i2++){
  334. if(Elements[i].SpriteName == _Atlas.elementsList[i2].name){
  335. Debug.LogWarning("Pro Flares - Ah its ok i Found it = "+Elements[i].SpriteName);
  336. Found = true;
  337. Elements[i].elementTextureID = i2;
  338. for(int f = 0; f < FlareBatches.Length; f++){
  339. FlareBatches[f].dirty = true;
  340. }
  341. }
  342. }
  343. if(!Found){
  344. Debug.LogError("Pro Flares - NOOOO.... It must of been deleted, resetting to first in list. :( -" +Elements[i].SpriteName,gameObject);
  345. Elements[i].elementTextureID = 0;
  346. Elements[i].SpriteName = _Atlas.elementsList[0].name;
  347. for(int f = 0; f < FlareBatches.Length; f++){
  348. FlareBatches[f].dirty = true;
  349. }
  350. }
  351. }
  352. }
  353. }
  354. #endif
  355. void Update () {
  356. if(!Initialised){
  357. Init();
  358. }
  359. }
  360. void OnDrawGizmos() {
  361. Gizmos.color = GlobalTintColor;
  362. Gizmos.DrawSphere(transform.position,0.1f);
  363. //Gizmos.DrawIcon(transform.position,"ProFlaresGizmo.tga",true);
  364. }
  365. }