ProFlareBatch.cs 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558
  1. //This asset was uploaded by https://unityassetcollection.com
  2. /// ProFlares - v1.08 - Copyright 2014-2015 All rights reserved - ProFlares.com
  3. /// <summary>
  4. /// ProFlareBatch.cs
  5. /// Processes all the ProFlares in a scene, converts them into geometry that can be rendered.
  6. /// </summary>
  7. using UnityEngine;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. [System.Serializable]
  11. public class FlareData {
  12. public ProFlare flare;
  13. public FlareOcclusion occlusion;
  14. }
  15. [System.Serializable]
  16. public class FlarePixelOcclusion {
  17. public RenderTexture renderTexture;
  18. //public Texture2D texture2D = new Texture2D(1, 1, TextureFormat.RGBAHalf, false);
  19. public Texture2D texture2D;
  20. public Rect rect = new Rect(0, 0, 1, 1);
  21. public Color pixelColor = new Color();
  22. public Camera pixelCamera = new Camera ();
  23. public Transform pixelCameraTransform;
  24. public bool pixelOcclusionPingPong;
  25. }
  26. [System.Serializable]
  27. public class FlareOcclusion {
  28. public bool occluded = false;
  29. public float occlusionScale = 1;
  30. public float occlusionBrigtness = 1;
  31. public float occlusionTarget = 1;
  32. public CullingState _CullingState;
  33. public float CullTimer = 0;
  34. public float cullFader = 1;
  35. public enum CullingState{
  36. Visible,
  37. CullCountDown,
  38. CanCull,
  39. Culled,
  40. NeverCull
  41. }
  42. public FlarePixelOcclusion flarePixelOcclusion;
  43. }
  44. [ExecuteInEditMode]
  45. [RequireComponent (typeof (MeshRenderer))]
  46. [RequireComponent (typeof (MeshFilter))]
  47. public class ProFlareBatch : MonoBehaviour {
  48. public enum Mode{
  49. Standard = 0,
  50. SingleCamera = 1
  51. //VR = 2
  52. }
  53. public bool debugMessages = true;
  54. public Mode mode = Mode.Standard;
  55. public ProFlareAtlas _atlas;
  56. //List of flares
  57. //public List<ProFlare> Flares = new List<ProFlare>();
  58. public List<FlareData> FlaresList = new List<FlareData>();
  59. //List of FlareElements
  60. public List<ProFlareElement> FlareElements = new List<ProFlareElement>();
  61. public ProFlareElement[] FlareElementsArray;
  62. public Camera GameCamera;
  63. public Transform GameCameraTrans;
  64. //Camera that the flare geometry will be rendered from.
  65. public Camera FlareCamera;
  66. public Transform FlareCameraTrans;
  67. //Cached Components
  68. public MeshFilter meshFilter;
  69. public Transform thisTransform;
  70. public MeshRenderer meshRender;
  71. public float zPos = 10;
  72. //Multiple meshes used for double buffering technique
  73. private Mesh bufferMesh;
  74. private Mesh meshA;
  75. private Mesh meshB;
  76. //Ping pong value for double Buffering
  77. bool PingPong;
  78. //Material used for the Flares, this is automatically created.
  79. public Material mat;
  80. //Geometry Arrays
  81. Vector3[] vertices;
  82. Vector2[] uv;
  83. Color32[] colors;
  84. int[] triangles;
  85. //public FlareOcclusion[] FlareOcclusionData;
  86. //Debug Propertys
  87. public bool useBrightnessThreshold = true;
  88. public int BrightnessThreshold = 1;
  89. public bool overdrawDebug;
  90. //When set to true the Flarebatches' geomerty will be rebuilt.
  91. public bool dirty = false;
  92. public bool useCulling = true;
  93. public int cullFlaresAfterTime = 5;
  94. public int cullFlaresAfterCount = 5;
  95. public bool culledFlaresNowVisiable = false;
  96. private float reshowCulledFlaresTimer = 0;
  97. public float reshowCulledFlaresAfter = 0.3f;
  98. //HelperTransform used for FlarePositions calculations.
  99. public Transform helperTransform;
  100. public bool showAllConnectedFlares;
  101. public bool VR_Mode = false;
  102. public float VR_Depth = 0.2f;
  103. public bool SingleCamera_Mode = false;
  104. public bool linearColorSpace = false;
  105. void Reset(){
  106. if(helperTransform == null)
  107. CreateHelperTransform();
  108. if (linearColorSpace){
  109. mat = new Material(Shader.Find("ProFlares/Textured Flare Shader Linear"));
  110. }else{
  111. mat = new Material(Shader.Find("ProFlares/Textured Flare Shader Gamma"));
  112. }
  113. if(meshFilter == null)
  114. meshFilter = GetComponent<MeshFilter>();
  115. if(meshFilter == null)
  116. meshFilter = gameObject.AddComponent<MeshFilter>();
  117. meshRender = gameObject.GetComponent<MeshRenderer>();
  118. if(meshRender == null)
  119. meshRender = gameObject.AddComponent<MeshRenderer>();
  120. if(FlareCamera == null){
  121. FlareCamera = transform.root.GetComponentInChildren<Camera>();
  122. }
  123. meshRender.material = mat;
  124. SetupMeshes();
  125. dirty = true;
  126. }
  127. void Awake(){
  128. PI_Div180 = Mathf.PI / 180;
  129. Div180_PI = 180 / Mathf.PI;
  130. ProFlare[] flares = GameObject.FindObjectsOfType(typeof(ProFlare)) as ProFlare[];
  131. for(int i = 0; i < flares.Length; i++){
  132. if(flares[i]._Atlas == _atlas)
  133. this.AddFlare(flares[i]);
  134. }
  135. for (int F = 0; F < FlaresList.Count; F++) {
  136. if (FlaresList [F].flare == null)
  137. continue;
  138. ProFlare flare_ = FlaresList [F].flare;
  139. if (Application.isPlaying) {
  140. FlareOcclusion data = FlaresList[F].occlusion;
  141. if(flare_.pixelBasedOcclusion)
  142. setupPixelBasedOcclusion (data, flare_);
  143. }else{
  144. //if(flare_.sunMode)
  145. // setupSunMode(flare_);
  146. }
  147. }
  148. }
  149. void Start () {
  150. //Turn off overdraw debug mode.
  151. if(Application.isPlaying){
  152. overdrawDebug = false;
  153. //dirty = true;
  154. }
  155. if(GameCamera == null){
  156. GameObject GameCameraGo = GameObject.FindWithTag("MainCamera");
  157. if(GameCameraGo)
  158. if(GameCameraGo.GetComponent<Camera>())
  159. GameCamera = GameCameraGo.GetComponent<Camera>();
  160. //# if(GameCameraGo)
  161. // if(GameCameraGo.camera)
  162. //ss GameCamera = GameCameraGo.camera;
  163. }
  164. if(GameCamera)
  165. GameCameraTrans = GameCamera.transform;
  166. //Make sure we have the transform cached
  167. thisTransform = transform;
  168. SetupMeshes();
  169. }
  170. void CreateMat(){
  171. // Debug.LogError("CreateMat");
  172. if (linearColorSpace){
  173. mat = new Material(Shader.Find("ProFlares/Textured Flare Shader Linear"));
  174. }else{
  175. mat = new Material(Shader.Find("ProFlares/Textured Flare Shader Gamma"));
  176. }
  177. meshRender.material = mat;
  178. if(_atlas)
  179. if(_atlas.texture)
  180. mat.mainTexture = _atlas.texture;
  181. }
  182. //Call when you switch your main render.
  183. public void SwitchCamera(Camera newCamera){
  184. GameCamera = newCamera;
  185. GameCameraTrans = newCamera.transform;
  186. //Update Occlusion data on changing camera new in v1.03
  187. //FixedUpdate ();
  188. for (int F = 0; F < FlaresList.Count; F++) {
  189. if (FlaresList [F].occlusion != null) {
  190. FlareOcclusion data = FlaresList [F].occlusion;
  191. if (data.occluded)
  192. FlaresList [F].occlusion.occlusionTarget = 0;
  193. if (FlaresList[F].flare.pixelBasedOcclusion) {
  194. data.flarePixelOcclusion.pixelCamera.transform.parent = newCamera.transform;
  195. data.flarePixelOcclusion.pixelCamera.transform.position = Vector3.zero;
  196. }
  197. }
  198. }
  199. }
  200. void OnDestroy(){
  201. //Remove the helper transform.
  202. if(Application.isPlaying){
  203. Destroy(helperTransform.gameObject);
  204. Destroy(mat);
  205. }
  206. else{
  207. DestroyImmediate(helperTransform.gameObject);
  208. DestroyImmediate(mat);
  209. }
  210. }
  211. public void RemoveFlare(ProFlare _flare){
  212. bool found = false;
  213. FlareData targetFlare = null;
  214. for(int i = 0; i < FlaresList.Count; i++){
  215. if(_flare == FlaresList[i].flare){
  216. targetFlare = FlaresList[i];
  217. found = true;
  218. break;
  219. }
  220. }
  221. if(found)
  222. FlaresList.Remove(targetFlare);
  223. }
  224. //Called from ProFlare.cs
  225. //First checks if the flare is already in the list, if not adds it and rebuils the Flarebatch Geometry
  226. public void AddFlare(ProFlare _flare){
  227. bool found = false;
  228. if (_flare.thisGameObject.scene != gameObject.scene){
  229. return;
  230. }
  231. for(int i = 0; i < FlaresList.Count; i++){
  232. if(_flare == FlaresList[i].flare){
  233. found = true;
  234. break;
  235. }
  236. }
  237. if(!found){
  238. FlareData FlareData_ = new FlareData();
  239. FlareData_.flare = _flare;
  240. FlareOcclusion data = new FlareOcclusion();
  241. if(_flare.neverCull)
  242. data._CullingState = FlareOcclusion.CullingState.NeverCull;
  243. FlareData_.occlusion = data;
  244. FlaresList.Add(FlareData_);
  245. if(_flare.pixelBasedOcclusion){
  246. /*
  247. data.flarePixelOcclusion = new FlarePixelOcclusion();
  248. GameObject cameraGO = new GameObject("ProFlarePixelCamera");
  249. cameraGO.layer = 8;
  250. Camera camera = cameraGO.AddComponent<Camera>();
  251. cameraGO.transform.parent = GameCameraTrans;
  252. camera.fieldOfView = 1;
  253. cameraGO.transform.localPosition = Vector3.zero;
  254. data.flarePixelOcclusion.pixelCamera = camera;
  255. data.flarePixelOcclusion.pixelCameraTransform = cameraGO.transform;
  256. data.flarePixelOcclusion.pixelCamera.clearFlags = CameraClearFlags.SolidColor;
  257. data.flarePixelOcclusion.pixelCamera.backgroundColor = new Color(0,0,0,0);
  258. data.flarePixelOcclusion.renderTexture = new RenderTexture(2,2,0,RenderTextureFormat.ARGBHalf);
  259. data.flarePixelOcclusion.rect = new Rect(0, 0, 1, 1);
  260. data.flarePixelOcclusion.texture2D = new Texture2D(2, 2, TextureFormat.RGBAHalf, false);
  261. camera.targetTexture = data.flarePixelOcclusion.renderTexture;
  262. */
  263. setupPixelBasedOcclusion(data,_flare);
  264. }
  265. dirty = true;
  266. }
  267. }
  268. void setupPixelBasedOcclusion(FlareOcclusion data,ProFlare flare){
  269. Debug.Log("setupPixelBasedOcclusion");
  270. data.flarePixelOcclusion = new FlarePixelOcclusion();
  271. GameObject cameraGO = new GameObject("ProFlarePixelCamera");
  272. cameraGO.layer = 8;
  273. Camera camera = cameraGO.AddComponent<Camera>();
  274. cameraGO.transform.parent = GameCameraTrans;
  275. camera.fieldOfView = 1;
  276. cameraGO.transform.localPosition = Vector3.zero;
  277. data.flarePixelOcclusion.pixelCamera = camera;
  278. data.flarePixelOcclusion.pixelCameraTransform = cameraGO.transform;
  279. data.flarePixelOcclusion.pixelCamera.clearFlags = CameraClearFlags.SolidColor;
  280. data.flarePixelOcclusion.pixelCamera.backgroundColor = new Color(0,0,0,0);
  281. data.flarePixelOcclusion.renderTexture = new RenderTexture(1,1,0,RenderTextureFormat.ARGB32);
  282. data.flarePixelOcclusion.rect = new Rect(0, 0, 1, 1);
  283. data.flarePixelOcclusion.texture2D = new Texture2D(1, 1, TextureFormat.ARGB32, false);
  284. camera.cullingMask = flare.pixelLayerMask;
  285. camera.enabled = false;
  286. camera.targetTexture = data.flarePixelOcclusion.renderTexture;
  287. }
  288. void CreateHelperTransform(){
  289. GameObject HelpTransformGo = new GameObject("_HelperTransform");
  290. helperTransform = HelpTransformGo.transform;
  291. helperTransform.parent = transform;
  292. helperTransform.localScale = Vector3.one;
  293. helperTransform.localPosition = Vector3.zero;
  294. }
  295. void Update(){
  296. Update2 ();
  297. if(thisTransform)
  298. thisTransform.localPosition = Vector3.forward*zPos;
  299. //Checks if you have deleted the helper transform. If its missing recreate it.....
  300. if(helperTransform == null)
  301. CreateHelperTransform();
  302. if(meshRender){
  303. if(meshRender.sharedMaterial == null)
  304. CreateMat();
  305. }else
  306. meshRender = gameObject.GetComponent<MeshRenderer>();
  307. bool meshMissing = false;
  308. //if(meshA == null){
  309. // meshMissing = true;
  310. // meshA = SetupSingleMesh();
  311. // meshA.name = "MeshA";
  312. // meshA.MarkDynamic();
  313. //}
  314. //if(meshB == null){
  315. // meshMissing = true;
  316. // meshB = SetupSingleMesh();
  317. // meshB.name = "MeshB";
  318. // meshB.MarkDynamic();
  319. //}
  320. if(meshMissing)
  321. if(_atlas != null)
  322. SetupMeshes();
  323. if(dirty)
  324. ReBuildGeometry();
  325. }
  326. //Late update
  327. void LateUpdate () {
  328. if(_atlas == null)
  329. return;
  330. UpdateFlares();
  331. }
  332. public void UpdateFlares(){
  333. bufferMesh = PingPong ? meshA : meshB;
  334. PingPong = !PingPong;
  335. UpdateGeometry();
  336. //Profiler.BeginSample("Set Arrays");
  337. if(bufferMesh != null){
  338. bufferMesh.vertices = vertices;
  339. bufferMesh.colors32 = colors;
  340. meshFilter.sharedMesh = bufferMesh;
  341. }
  342. //Profiler.EndSample();
  343. }
  344. public void ForceRefresh(){
  345. FlaresList.Clear();
  346. ProFlare[] flares = GameObject.FindObjectsOfType(typeof(ProFlare)) as ProFlare[];
  347. for(int i = 0; i < flares.Length; i++){
  348. if(flares[i]._Atlas == _atlas)
  349. this.AddFlare(flares[i]);
  350. }
  351. dirty = true;
  352. }
  353. void ReBuildGeometry(){
  354. #if UNITY_EDITOR
  355. //See when the geometry is built, try and avoid triggering this in the middle of your game.
  356. //This can be triggered by the new culling system, if your only using a few flares you may want to turn it off as the speed increase from culling will be more limited.
  357. if(debugMessages)
  358. Debug.Log("ProFlares - Rebuilding Geometry : "+ gameObject.name,gameObject);
  359. #endif
  360. FlareElements.Clear();
  361. int flareElementsCount = 0;
  362. bool missingFlare = false;
  363. for(int i = 0; i < FlaresList.Count; i++){
  364. if(FlaresList[i].flare == null){
  365. missingFlare = true;
  366. break;
  367. }
  368. for(int i2 = 0; i2 < FlaresList[i].flare.Elements.Count; i2++){
  369. // FlareElements.Add(Flares[i].Elements[i2]);
  370. if(FlaresList[i].occlusion._CullingState == FlareOcclusion.CullingState.CanCull){
  371. FlaresList[i].occlusion._CullingState = FlareOcclusion.CullingState.Culled;
  372. FlaresList[i].occlusion.cullFader = 0;
  373. }
  374. if(FlaresList[i].occlusion._CullingState != FlareOcclusion.CullingState.Culled){
  375. flareElementsCount++;
  376. }
  377. }
  378. }
  379. FlareElementsArray = new ProFlareElement[flareElementsCount];
  380. flareElementsCount = 0;
  381. if(!missingFlare)
  382. for(int i = 0; i < FlaresList.Count; i++){
  383. for(int i2 = 0; i2 < FlaresList[i].flare.Elements.Count; i2++){
  384. if(FlaresList[i].occlusion._CullingState != FlareOcclusion.CullingState.Culled){
  385. FlareElementsArray[flareElementsCount] = (FlaresList[i].flare.Elements[i2]);
  386. flareElementsCount++;
  387. }
  388. }
  389. }
  390. if(missingFlare){
  391. ForceRefresh();
  392. ReBuildGeometry();
  393. missingFlare = false;
  394. }
  395. meshA = null;
  396. meshB = null;
  397. bufferMesh = null;
  398. SetupMeshes();
  399. dirty = false;
  400. }
  401. void SetupMeshes()
  402. {
  403. if(_atlas == null)
  404. return;
  405. if(FlareElementsArray == null)
  406. return;
  407. meshA = new Mesh();
  408. meshB = new Mesh();
  409. int vertSize = 0;
  410. int uvSize = 0;
  411. int colSize = 0;
  412. int triCount = 0;
  413. //Calculate how big each array needs to be based on the Flares
  414. for(int i = 0; i < FlareElementsArray.Length; i++){
  415. switch(FlareElementsArray[i].type){
  416. case(ProFlareElement.Type.Single):
  417. {
  418. vertSize = vertSize+4;
  419. uvSize = uvSize+4;
  420. colSize = colSize+4;
  421. triCount = triCount+6;
  422. }
  423. break;
  424. case(ProFlareElement.Type.Multi):
  425. {
  426. int subCount = FlareElementsArray[i].subElements.Count;
  427. vertSize = vertSize+(4*subCount);
  428. uvSize = uvSize+(4*subCount);
  429. colSize = colSize+(4*subCount);
  430. triCount = triCount+(6*subCount);
  431. }
  432. break;
  433. }
  434. }
  435. //Create Built in arrays
  436. vertices = new Vector3[vertSize];
  437. uv = new Vector2[uvSize];
  438. colors = new Color32[colSize];
  439. triangles = new int[triCount];
  440. //Set Inital valuse for each vertex
  441. for(int i = 0; i < vertices.Length/4; i++){
  442. int extra = i * 4;
  443. vertices[0+extra] = new Vector3(1,1,0); //((Vector3.right)+(Vector3.up));
  444. vertices[1+extra] = new Vector3(1,-1,0);//((Vector3.right)+(Vector3.down));
  445. vertices[2+extra] = new Vector3(-1,1,0);//((Vector3.left)+(Vector3.up));
  446. vertices[3+extra] = new Vector3(-1,-1,0);//((Vector3.left)+(Vector3.down));
  447. }
  448. //Set UV coordinates for each vertex, this only needs to be done in the mesh rebuild.
  449. int count = 0;
  450. for(int i = 0; i < FlareElementsArray.Length; i++){
  451. switch(FlareElementsArray[i].type){
  452. case(ProFlareElement.Type.Single):
  453. {
  454. int extra = (count) * 4;
  455. Rect final = _atlas.elementsList[FlareElementsArray[i].elementTextureID].UV;
  456. uv[0+extra] = new Vector2(final.xMax,final.yMax);
  457. uv[1+extra] = new Vector2(final.xMax,final.yMin);
  458. uv[2+extra] = new Vector2(final.xMin,final.yMax);
  459. uv[3+extra] = new Vector2(final.xMin,final.yMin);
  460. count++;
  461. }break;
  462. case(ProFlareElement.Type.Multi):
  463. {
  464. for(int i2 = 0; i2 < FlareElementsArray[i].subElements.Count; i2++){
  465. int extra2 = (count+i2) * 4;
  466. Rect final = _atlas.elementsList[FlareElementsArray[i].elementTextureID].UV;
  467. uv[0+extra2] = new Vector2(final.xMax,final.yMax);
  468. uv[1+extra2] = new Vector2(final.xMax,final.yMin);
  469. uv[2+extra2] = new Vector2(final.xMin,final.yMax);
  470. uv[3+extra2] = new Vector2(final.xMin,final.yMin);
  471. }
  472. count = count+FlareElementsArray[i].subElements.Count;
  473. }break;
  474. }
  475. }
  476. Color32 newColor = new Color32(255,255,255,255);
  477. //Set inital vertex colors.
  478. for(int i = 0; i < colors.Length/4; i++){
  479. int extra = i * 4;
  480. colors[0+extra] = newColor;
  481. colors[1+extra] = newColor;
  482. colors[2+extra] = newColor;
  483. colors[3+extra] = newColor;
  484. }
  485. //Set triangle array, this is only done in the mesh rebuild.
  486. for(int i = 0; i < triangles.Length/6; i++){
  487. int extra = i * 4;
  488. int extra2 = i * 6;
  489. triangles[0+extra2] = 0+extra;
  490. triangles[1+extra2] = 1+extra;
  491. triangles[2+extra2] = 2+extra;
  492. triangles[3+extra2] = 2+extra;
  493. triangles[4+extra2] = 1+extra;
  494. triangles[5+extra2] = 3+extra;
  495. }
  496. meshA.vertices = vertices;
  497. meshA.uv = uv;
  498. meshA.triangles = triangles;
  499. meshA.colors32 = colors;
  500. meshA.bounds = new Bounds(Vector3.zero,Vector3.one*1000);
  501. meshB.vertices = vertices;
  502. meshB.uv = uv;
  503. meshB.triangles = triangles;
  504. meshB.colors32 = colors;
  505. meshB.bounds = new Bounds(Vector3.zero,Vector3.one*1000);
  506. }
  507. Vector3[] verts;
  508. Vector2 _scale;
  509. Color32 _color;
  510. float PI_Div180;
  511. float Div180_PI;
  512. int visibleFlares = 0;
  513. /*
  514. public Vector3 RotatePoint(Vector3 p, float d,float ct,float st) {
  515. //float r = d * (Mathf.PI / 180);
  516. //float ct = Mathf.Cos(r);
  517. //float st = Mathf.Sin(r);
  518. float x = (ct * p.x - st * p.y);
  519. float y = (st * p.x + ct * p.y);
  520. return new Vector3(x,y,0);
  521. }*/
  522. void Update2(){
  523. if (!dirty) {
  524. for (int F = 0; F < FlaresList.Count; F++) {
  525. if (FlaresList [F].flare == null)
  526. continue;
  527. ProFlare flare_ = FlaresList [F].flare;
  528. FlareOcclusion data = FlaresList [F].occlusion;
  529. float distanceRay = -1;
  530. if (flare_.isVisible) {
  531. if(flare_.DisabledPlayMode)
  532. continue;
  533. if (flare_.RaycastPhysics) {
  534. data.occluded = true;
  535. RaycastHit hit;
  536. Vector3 direction = GameCameraTrans.position - flare_.thisTransform.position;
  537. if (flare_.sunMode) {
  538. direction = GameCameraTrans.position - flare_.sunModeTransformB.position;
  539. }
  540. distanceRay = Vector3.Distance (GameCameraTrans.position, flare_.thisTransform.position);
  541. Ray ray = new Ray (flare_.thisTransform.position, direction);
  542. if (Physics.Raycast (ray, out hit, distanceRay, flare_.mask)) {
  543. data.occluded = true;
  544. data.occlusionTarget = 0;
  545. #if UNITY_EDITOR
  546. Debug.DrawRay (flare_.thisTransform.position, direction, Color.red);
  547. flare_.OccludingObject = hit.collider.gameObject;
  548. #endif
  549. } else {
  550. data.occluded = false;
  551. data.occlusionTarget = 1;
  552. #if UNITY_EDITOR
  553. flare_.OccludingObject = null;
  554. Debug.DrawRay (flare_.thisTransform.position, direction);
  555. #endif
  556. }
  557. }
  558. if (flare_.pixelBasedOcclusion && Application.isPlaying) {
  559. FlarePixelOcclusion flarePixelOcclusion = data.flarePixelOcclusion;
  560. if(!flarePixelOcclusion.pixelOcclusionPingPong){
  561. if (distanceRay < 0) {
  562. distanceRay = Vector3.Distance (GameCameraTrans.position, flare_.thisTransform.position);
  563. }
  564. if (flare_.sunMode) {
  565. flarePixelOcclusion.pixelCameraTransform.LookAt (flare_.sunModeTransformB);
  566. distanceRay = GameCamera.farClipPlane;
  567. }
  568. else {
  569. if(flarePixelOcclusion.pixelCameraTransform != null)
  570. flarePixelOcclusion.pixelCameraTransform.LookAt (flare_.thisTransform);
  571. }
  572. flarePixelOcclusion.pixelCamera.farClipPlane = distanceRay;
  573. RenderTexture.active = flarePixelOcclusion.renderTexture;
  574. flarePixelOcclusion.pixelCamera.Render ();
  575. flarePixelOcclusion.texture2D.ReadPixels (flarePixelOcclusion.rect, 0, 0);
  576. flarePixelOcclusion.texture2D.Apply ();
  577. flarePixelOcclusion.pixelOcclusionPingPong = true;
  578. }else{
  579. Color pix = Color.white;
  580. pix = flarePixelOcclusion.texture2D.GetPixel(0, 0);
  581. float pixA = pix.a;
  582. data.occlusionTarget = Mathf.Abs(1 - Mathf.Clamp01(pixA));
  583. if (data.occlusionTarget < 0.01){
  584. data.occluded = false;
  585. }else if (pix.a < 1.0){
  586. data.occluded = false;
  587. }else{
  588. data.occluded = true;
  589. }
  590. if (data.occlusionTarget > 1)
  591. data.occlusionTarget = 1;
  592. if (data.occlusionTarget < 0)
  593. data.occlusionTarget = 0;
  594. flarePixelOcclusion.pixelOcclusionPingPong = false;
  595. }
  596. //Debug.Log (pix);
  597. }
  598. }
  599. }
  600. }
  601. }
  602. void UpdateGeometry(){
  603. if(GameCamera == null){
  604. meshRender.enabled = false;
  605. return;
  606. }
  607. meshRender.enabled = true;
  608. //Profiler.BeginSample("Update Pos");
  609. visibleFlares = 0;
  610. int canCullFlares = 0;
  611. for(int F = 0; F < FlaresList.Count; F++){
  612. //Profiler.BeginSample("Lens Pos");
  613. ProFlare flare_ = FlaresList[F].flare;
  614. FlareOcclusion data = FlaresList[F].occlusion;
  615. if(flare_ == null)
  616. continue;
  617. if ((flare_.sunMode)&&(flare_.sunModeTransformA!=null)&&(flare_.sunModeTransformB!=null)){
  618. flare_.sunModeTransformA.position = GameCameraTrans.position;
  619. flare_.LensPosition = GameCamera.WorldToViewportPoint(flare_.sunModeTransformB.position);
  620. flare_.sunModeTransformA.rotation = flare_.thisTransform.rotation;
  621. }else
  622. flare_.LensPosition = GameCamera.WorldToViewportPoint(flare_.thisTransform.position);
  623. Vector3 LensPosition = flare_.LensPosition;
  624. bool isVisible = (LensPosition.z > 0f && LensPosition.x+flare_.OffScreenFadeDist > 0f && LensPosition.x-flare_.OffScreenFadeDist < 1f && LensPosition.y+flare_.OffScreenFadeDist > 0f && LensPosition.y-flare_.OffScreenFadeDist < 1f);
  625. flare_.isVisible = isVisible;
  626. //Profiler.EndSample();
  627. //Profiler.BeginSample("offScreen Fading");
  628. //Off Screen fading
  629. float offScreenFade = 1;
  630. if(!(LensPosition.x > 0f && LensPosition.x < 1f && LensPosition.y > 0f && LensPosition.y < 1f)){
  631. float offScreenNorm = 1f/flare_.OffScreenFadeDist;
  632. float xPos = 0;
  633. float yPos = 0;
  634. if(!(LensPosition.x > 0f && LensPosition.x < 1f))
  635. xPos = LensPosition.x > 0.5f ? LensPosition.x-1f : Mathf.Abs(LensPosition.x);
  636. if(!(LensPosition.y > 0f && LensPosition.y < 1f))
  637. yPos = LensPosition.y > 0.5f ? LensPosition.y-1f : Mathf.Abs(LensPosition.y);
  638. offScreenFade = Mathf.Clamp01( offScreenFade - (Mathf.Max(xPos,yPos))*offScreenNorm);
  639. }
  640. //Profiler.EndSample();
  641. //Profiler.BeginSample("Dynamic Triggering");
  642. //Dynamic Triggering Center
  643. float centerBoost = 0;
  644. if(LensPosition.x > 0.5f-flare_.DynamicCenterRange && LensPosition.x < 0.5f+flare_.DynamicCenterRange && LensPosition.y > 0.5f-flare_.DynamicCenterRange && LensPosition.y < 0.5f+flare_.DynamicCenterRange){
  645. if(flare_.DynamicCenterRange > 0){
  646. float centerBoostNorm = 1/(flare_.DynamicCenterRange);
  647. centerBoost = 1-Mathf.Max(Mathf.Abs(LensPosition.x-0.5f),Mathf.Abs(LensPosition.y-0.5f))*centerBoostNorm;
  648. }
  649. }
  650. //Dynamic Triggering Edge
  651. float DynamicEdgeAmount = 0;
  652. bool isInEdgeZone1 = (
  653. LensPosition.x > 0f+flare_.DynamicEdgeBias+(flare_.DynamicEdgeRange) &&
  654. LensPosition.x < 1f-flare_.DynamicEdgeBias-(flare_.DynamicEdgeRange) &&
  655. LensPosition.y > 0f+flare_.DynamicEdgeBias+(flare_.DynamicEdgeRange) &&
  656. LensPosition.y < 1f-flare_.DynamicEdgeBias-(flare_.DynamicEdgeRange)
  657. );
  658. bool isInEdgeZone2 = (
  659. LensPosition.x+(flare_.DynamicEdgeRange) > 0f+flare_.DynamicEdgeBias &&
  660. LensPosition.x-(flare_.DynamicEdgeRange) < 1f-flare_.DynamicEdgeBias &&
  661. LensPosition.y+(flare_.DynamicEdgeRange) > 0f+flare_.DynamicEdgeBias &&
  662. LensPosition.y-(flare_.DynamicEdgeRange) < 1f-flare_.DynamicEdgeBias
  663. );
  664. if(!isInEdgeZone1&&isInEdgeZone2){
  665. float DynamicEdgeNormalizeValue = 1/(flare_.DynamicEdgeRange);
  666. float DynamicEdgeX = 0;
  667. float DynamicEdgeY = 0;
  668. bool isInEdgeZoneX1 = (LensPosition.x > 0f+flare_.DynamicEdgeBias+(flare_.DynamicEdgeRange) && LensPosition.x < 1f-flare_.DynamicEdgeBias-(flare_.DynamicEdgeRange));
  669. bool isInEdgeZoneX2 = (LensPosition.x+(flare_.DynamicEdgeRange) > 0f+flare_.DynamicEdgeBias && LensPosition.x-(flare_.DynamicEdgeRange) < 1f-flare_.DynamicEdgeBias);
  670. bool isInEdgeZoneY1 = (LensPosition.y > 0f+flare_.DynamicEdgeBias+(flare_.DynamicEdgeRange) && LensPosition.y < 1f-flare_.DynamicEdgeBias-(flare_.DynamicEdgeRange));
  671. bool isInEdgeZoneY2 = (LensPosition.y+(flare_.DynamicEdgeRange) > 0f+flare_.DynamicEdgeBias && LensPosition.y-(flare_.DynamicEdgeRange) < 1f-flare_.DynamicEdgeBias);
  672. if(!isInEdgeZoneX1&&isInEdgeZoneX2){
  673. DynamicEdgeX = LensPosition.x > 0.5f ? (LensPosition.x - 1 +flare_.DynamicEdgeBias) + (flare_.DynamicEdgeRange) : Mathf.Abs(LensPosition.x -flare_.DynamicEdgeBias - (flare_.DynamicEdgeRange));
  674. DynamicEdgeX = (DynamicEdgeX*DynamicEdgeNormalizeValue)*0.5f;
  675. }
  676. if(!isInEdgeZoneY1&&isInEdgeZoneY2){
  677. DynamicEdgeY = LensPosition.y > 0.5f ? (LensPosition.y - 1 + flare_.DynamicEdgeBias) + (flare_.DynamicEdgeRange) : Mathf.Abs(LensPosition.y-flare_.DynamicEdgeBias - (flare_.DynamicEdgeRange));
  678. DynamicEdgeY = (DynamicEdgeY*DynamicEdgeNormalizeValue)*0.5f;
  679. }
  680. DynamicEdgeAmount = Mathf.Max(DynamicEdgeX,DynamicEdgeY);
  681. }
  682. DynamicEdgeAmount = flare_.DynamicEdgeCurve.Evaluate(DynamicEdgeAmount);
  683. //Profiler.EndSample();
  684. //Profiler.BeginSample("Angle Fall Off");
  685. /*
  686. float AngleFallOff = 1;
  687. if(flare_.UseAngleLimit){
  688. Vector3 playerAngle = flare_.thisTransform.forward;
  689. Vector3 cameraAngle = GameCameraTrans.forward;
  690. float horizDiffAngle = Vector3.Angle(cameraAngle, playerAngle);
  691. horizDiffAngle = Mathf.Abs( Mathf.Clamp(180-horizDiffAngle,-flare_.maxAngle,flare_.maxAngle));
  692. AngleFallOff = 1f-(horizDiffAngle*(1f/(flare_.maxAngle*0.5f)));
  693. if(flare_.UseAngleCurve)
  694. AngleFallOff = flare_.AngleCurve.Evaluate(AngleFallOff);
  695. }*/
  696. float AngleFallOff = 1;
  697. if(flare_.UseAngleLimit){
  698. Vector3 playerAngle = flare_.thisTransform.forward;
  699. //Vector3 cameraAngle = GameCameraTrans.forward;
  700. //the direct vector from flare point to camera point
  701. Vector3 camToPoint = GameCameraTrans.position - flare_.thisTransform.position;
  702. float horizDiffAngle = Vector3.Angle(playerAngle, camToPoint);
  703. horizDiffAngle = Mathf.Abs( Mathf.Clamp(horizDiffAngle,-flare_.maxAngle,flare_.maxAngle));
  704. if (horizDiffAngle > flare_.maxAngle)
  705. AngleFallOff = 0;
  706. else
  707. {
  708. AngleFallOff = 1f - (horizDiffAngle * (1f / (flare_.maxAngle * 0.5f)));
  709. if (flare_.UseAngleCurve)
  710. AngleFallOff = flare_.AngleCurve.Evaluate(AngleFallOff);
  711. }
  712. }
  713. //Profiler.EndSample();
  714. //Profiler.BeginSample("Distance Check");
  715. float distanceFalloff = 1f;
  716. if(flare_.useMaxDistance){
  717. Vector3 heading = flare_.thisTransform.position - GameCameraTrans.position;
  718. float distance = Vector3.Dot(heading, GameCameraTrans.forward);
  719. float distanceNormalised = 1f-(distance/flare_.GlobalMaxDistance);
  720. distanceFalloff = 1f*distanceNormalised;
  721. if(distanceFalloff < 0.001f)
  722. flare_.isVisible = false;
  723. }
  724. //Profiler.EndSample();
  725. //Profiler.BeginSample("Check Occlusion Data");
  726. if(!dirty)
  727. if(data != null){
  728. data.occlusionScale = Mathf.Lerp(data.occlusionScale,data.occlusionTarget,Time.deltaTime*flare_.occlusionScaleSpeed);
  729. data.occlusionBrigtness = Mathf.Lerp(data.occlusionBrigtness,data.occlusionTarget,Time.deltaTime*flare_.occlusionBrightnessSpeed);
  730. }
  731. //if(data.occluded){
  732. //data.occlusionScale = Mathf.Lerp(data.occlusionScale,data.occlusionTarget,Time.deltaTime*16);
  733. //}else{
  734. //data.occlusionScale = Mathf.Lerp(data.occlusionScale,data.occlusionTarget,Time.deltaTime*16);
  735. //}
  736. //Profiler.EndSample();
  737. //Profiler.BeginSample("Final Lens Pos Set");
  738. // Debug.Log("Visible = "+Flares[F].isVisible+" | offScreenFade "+offScreenFade);
  739. // Debug.Log("distanceFalloff "+distanceFalloff);
  740. if(!flare_.isVisible)
  741. offScreenFade = 0;
  742. float tempScale = 1;
  743. if(FlareCamera)
  744. helperTransform.position = FlareCamera.ViewportToWorldPoint(LensPosition);
  745. LensPosition = helperTransform.localPosition;
  746. //Profiler.EndSample();
  747. if((!VR_Mode) && (!SingleCamera_Mode))
  748. LensPosition.z = 0f;
  749. float finalAlpha;
  750. //Profiler.BeginSample("Elements Loo[");
  751. for(int i = 0; i < flare_.Elements.Count; i++){
  752. ProFlareElement _element = flare_.Elements[i];
  753. Color GlobalColor = flare_.GlobalTintColor;
  754. if(isVisible)
  755. switch(_element.type){
  756. case(ProFlareElement.Type.Single):
  757. //Do the color stuff.
  758. _element.ElementFinalColor.r = (_element.ElementTint.r * GlobalColor.r);
  759. _element.ElementFinalColor.g = (_element.ElementTint.g * GlobalColor.g);
  760. _element.ElementFinalColor.b = (_element.ElementTint.b * GlobalColor.b);
  761. finalAlpha = _element.ElementTint.a * GlobalColor.a;
  762. if(flare_.useDynamicEdgeBoost){
  763. if(_element.OverrideDynamicEdgeBrightness)
  764. finalAlpha = finalAlpha + (_element.DynamicEdgeBrightnessOverride*DynamicEdgeAmount);
  765. else
  766. finalAlpha = finalAlpha + (flare_.DynamicEdgeBrightness*DynamicEdgeAmount);
  767. }
  768. if(flare_.useDynamicCenterBoost){
  769. if(_element.OverrideDynamicCenterBrightness)
  770. finalAlpha += (_element.DynamicCenterBrightnessOverride*centerBoost);
  771. else
  772. finalAlpha += (flare_.DynamicCenterBrightness*centerBoost);
  773. }
  774. if(flare_.UseAngleBrightness)
  775. finalAlpha *= AngleFallOff;
  776. if(flare_.useDistanceFade)
  777. finalAlpha *= distanceFalloff;
  778. finalAlpha *= data.occlusionBrigtness;
  779. finalAlpha *= data.cullFader;
  780. finalAlpha *= offScreenFade;
  781. _element.ElementFinalColor.a = finalAlpha;
  782. break;
  783. case(ProFlareElement.Type.Multi):
  784. //Profiler.BeginSample("Color Mutli Loop");
  785. for(int i2 = 0; i2 < _element.subElements.Count; i2++){
  786. //Do the color stuff.
  787. SubElement _subElement = _element.subElements[i2];
  788. _subElement.colorFinal.r = (_subElement.color.r * GlobalColor.r);
  789. _subElement.colorFinal.g = (_subElement.color.g * GlobalColor.g);
  790. _subElement.colorFinal.b = (_subElement.color.b * GlobalColor.b);
  791. finalAlpha = _subElement.color.a * GlobalColor.a;
  792. if(flare_.useDynamicEdgeBoost){
  793. if(_element.OverrideDynamicEdgeBrightness)
  794. finalAlpha = finalAlpha + (_element.DynamicEdgeBrightnessOverride*DynamicEdgeAmount);
  795. else
  796. finalAlpha = finalAlpha + (flare_.DynamicEdgeBrightness*DynamicEdgeAmount);
  797. }
  798. if(flare_.useDynamicCenterBoost){
  799. if(_element.OverrideDynamicCenterBrightness)
  800. finalAlpha += (_element.DynamicCenterBrightnessOverride*centerBoost);
  801. else
  802. finalAlpha += (flare_.DynamicCenterBrightness*centerBoost);
  803. }
  804. if(flare_.UseAngleBrightness)
  805. finalAlpha *= AngleFallOff;
  806. if(flare_.useDistanceFade)
  807. finalAlpha *= distanceFalloff;
  808. finalAlpha *= data.occlusionBrigtness;
  809. finalAlpha *= data.cullFader;
  810. finalAlpha *= offScreenFade;
  811. _subElement.colorFinal.a = finalAlpha;
  812. }
  813. //Profiler.EndSample();
  814. break;
  815. }
  816. else{
  817. switch(_element.type){
  818. case(ProFlareElement.Type.Single):
  819. // _element.ElementFinalColor = Color.black;
  820. tempScale = 0;
  821. break;
  822. case(ProFlareElement.Type.Multi):
  823. // for(int i2 = 0; i2 < _element.subElements.Count; i2++){
  824. // _element.subElements[i2].colorFinal = Color.black;
  825. // }
  826. tempScale = 0;
  827. break;
  828. }
  829. }
  830. //Element Scale
  831. float finalScale = tempScale;
  832. if(flare_.useDynamicEdgeBoost){
  833. if(_element.OverrideDynamicEdgeBoost)
  834. finalScale = finalScale+((DynamicEdgeAmount)*_element.DynamicEdgeBoostOverride);
  835. else
  836. finalScale = finalScale+((DynamicEdgeAmount)*flare_.DynamicEdgeBoost);
  837. }
  838. if(flare_.useDynamicCenterBoost){
  839. if(_element.OverrideDynamicCenterBoost)
  840. finalScale = finalScale+(_element.DynamicCenterBoostOverride*centerBoost);
  841. else
  842. finalScale = finalScale+(flare_.DynamicCenterBoost*centerBoost);
  843. }
  844. if(finalScale < 0) finalScale = 0;
  845. if(flare_.UseAngleScale)
  846. finalScale *= AngleFallOff;
  847. if(flare_.useDistanceScale)
  848. finalScale *= distanceFalloff;
  849. finalScale *= data.occlusionScale;
  850. if(!_element.Visible)
  851. finalScale = 0;
  852. if(!isVisible)
  853. finalScale = 0;
  854. _element.ScaleFinal = finalScale;
  855. float angles = 0;
  856. //Apply final screen position.
  857. if(isVisible)
  858. switch(_element.type){
  859. case(ProFlareElement.Type.Single):{
  860. Vector3 pos = LensPosition*-_element.position;
  861. //Debug.Log(pos.magnitude);
  862. float zpos = LensPosition.z;
  863. if(VR_Mode){
  864. float flarePos = (_element.position*-1)-1;
  865. zpos = LensPosition.z *((flarePos*VR_Depth)+1);
  866. }
  867. Vector3 newVect = new Vector3(
  868. Mathf.Lerp(pos.x,LensPosition.x,_element.Anamorphic.x),
  869. Mathf.Lerp(pos.y,LensPosition.y,_element.Anamorphic.y),
  870. zpos
  871. );
  872. newVect = newVect + _element.OffsetPostion;
  873. _element.OffsetPosition = newVect;
  874. if(_element.rotateToFlare){
  875. angles = (Div180_PI)*(Mathf.Atan2(LensPosition.y - _element.OffsetPosition.y,LensPosition.x - _element.OffsetPosition.x));
  876. }
  877. }break;
  878. case(ProFlareElement.Type.Multi):{
  879. //Profiler.BeginSample("Scale Mutli Loop");
  880. for(int i2 = 0; i2 < _element.subElements.Count; i2++){
  881. SubElement _subElement = _element.subElements[i2];
  882. if(_element.useRangeOffset){
  883. Vector3 posM = LensPosition*-_subElement.position;
  884. //Vector3 posM = new Vector3(
  885. // LensPosition.x*-_subElement.position+(_subElement.offset.x*_subElement.random*0.5f),
  886. // LensPosition.y*-_subElement.position+(_subElement.offset.y*_subElement.random*0.5f),
  887. // LensPosition.z*-_subElement.position
  888. // );
  889. float zpos = LensPosition.z;
  890. if(VR_Mode){
  891. float flarePos = (_subElement.position*-1)-1;
  892. zpos = LensPosition.z *((flarePos*VR_Depth)+1);
  893. }
  894. Vector3 newVectM = new Vector3(
  895. Mathf.Lerp(posM.x,LensPosition.x,_element.Anamorphic.x),
  896. Mathf.Lerp(posM.y,LensPosition.y,_element.Anamorphic.y),
  897. zpos
  898. );
  899. newVectM = newVectM + _element.OffsetPostion;
  900. _subElement.offset = newVectM;
  901. if(_element.rotateToFlare)
  902. _subElement.angle = (Div180_PI)*(Mathf.Atan2(LensPosition.y - _subElement.offset.y,LensPosition.x - _subElement.offset.x));
  903. }
  904. else
  905. _subElement.offset = LensPosition*-_element.position;
  906. }
  907. //Profiler.EndSample();
  908. }break;
  909. }
  910. //Apply final element angle.
  911. //float angles = 0;
  912. //if(_element.rotateToFlare){
  913. // angles = (Div180_PI)*(Mathf.Atan2(LensPosition.y - 0,LensPosition.x - 0));
  914. //}
  915. angles = angles + (LensPosition.x*_element.rotationSpeed);
  916. angles = angles + (LensPosition.y*_element.rotationSpeed);
  917. angles = angles + (Time.time*_element.rotationOverTime);
  918. _element.FinalAngle = (_element.angle)+angles;
  919. }
  920. //Profiler.EndSample();
  921. if((!flare_.neverCull)&&useCulling){
  922. FlareOcclusion.CullingState _CullingState = data._CullingState;
  923. if(flare_.isVisible){
  924. visibleFlares++;
  925. if(data.occluded){
  926. if(_CullingState == FlareOcclusion.CullingState.Visible){
  927. //Debug.Log("Culled via Occlusion");
  928. data.CullTimer = cullFlaresAfterTime;
  929. _CullingState = FlareOcclusion.CullingState.CullCountDown;
  930. }
  931. }else{
  932. if(_CullingState == FlareOcclusion.CullingState.Culled){
  933. //Debug.Log("Re show not occluded");
  934. culledFlaresNowVisiable = true;
  935. }
  936. _CullingState = FlareOcclusion.CullingState.Visible;
  937. }
  938. }else{
  939. if(_CullingState == FlareOcclusion.CullingState.Visible){
  940. //Debug.Log("Culled via Not Vis");
  941. data.CullTimer = cullFlaresAfterTime;
  942. _CullingState = FlareOcclusion.CullingState.CullCountDown;
  943. }
  944. }
  945. switch(_CullingState){
  946. case(FlareOcclusion.CullingState.Visible):{
  947. }break;
  948. case(FlareOcclusion.CullingState.CullCountDown):{
  949. data.CullTimer = data.CullTimer-Time.deltaTime;
  950. if(data.CullTimer < 0)
  951. _CullingState = FlareOcclusion.CullingState.CanCull;
  952. }break;
  953. }
  954. if(_CullingState != FlareOcclusion.CullingState.Culled)
  955. data.cullFader = Mathf.Clamp01(data.cullFader+(Time.deltaTime));
  956. if(_CullingState == FlareOcclusion.CullingState.CanCull)
  957. canCullFlares++;
  958. data._CullingState = _CullingState;
  959. }
  960. reshowCulledFlaresTimer += Time.deltaTime;
  961. if(reshowCulledFlaresTimer > reshowCulledFlaresAfter){
  962. reshowCulledFlaresTimer = 0;
  963. if(culledFlaresNowVisiable){
  964. //Debug.Log("A culled flare has now become visiable");
  965. dirty = true;
  966. culledFlaresNowVisiable = false;
  967. }
  968. }
  969. if(!dirty)
  970. if(canCullFlares >= cullFlaresAfterCount){
  971. Debug.Log("Culling Flares");dirty = true;
  972. }
  973. }
  974. //Profiler.BeginSample("Rendering Bit");
  975. //Rendering Bit
  976. int count = 0;
  977. if(FlareElementsArray != null)
  978. for(int i = 0; i < FlareElementsArray.Length; i++){
  979. float scaleMulti = 1;
  980. ProFlare flare = FlareElementsArray[i].flare;
  981. if(flare.MultiplyScaleByTransformScale)
  982. scaleMulti = flare.thisTransform.localScale.x;
  983. switch(FlareElementsArray[i].type){
  984. case(ProFlareElement.Type.Single):
  985. {
  986. int extra = (count) * 4;
  987. //Check for DisabledPlayMode, then scale to zero. then skip over the rest of the calculations
  988. if(FlareElementsArray[i].flare.DisabledPlayMode){
  989. vertices[0+extra] = Vector3.zero;
  990. vertices[1+extra] = Vector3.zero;
  991. vertices[2+extra] = Vector3.zero;
  992. vertices[3+extra] = Vector3.zero;
  993. }
  994. _scale = (((FlareElementsArray[i].size*FlareElementsArray[i].Scale*0.01f)*flare.GlobalScale)*FlareElementsArray[i].ScaleFinal)*scaleMulti;
  995. //Avoid Negative scaling
  996. if((_scale.x < 0)||(_scale.y < 0))
  997. _scale = Vector3.zero;
  998. Vector3 offset = FlareElementsArray[i].OffsetPosition;
  999. float angle = FlareElementsArray[i].FinalAngle;
  1000. _color = FlareElementsArray[i].ElementFinalColor;
  1001. if(useBrightnessThreshold){
  1002. if(_color.a < BrightnessThreshold){
  1003. _scale = Vector2.zero;
  1004. }else if(_color.r+_color.g+_color.b < BrightnessThreshold){
  1005. _scale = Vector2.zero;
  1006. }
  1007. }
  1008. if(overdrawDebug)
  1009. _color = new Color32(20,20,20,100);
  1010. if(!FlareElementsArray[i].flare.DisabledPlayMode){
  1011. //Precalculate some of the RotatePoint function to avoid repeat math.
  1012. float r = angle * (PI_Div180);
  1013. float ct = Mathf.Cos(r);
  1014. float st = Mathf.Sin(r);
  1015. vertices[0+extra] = new Vector3((ct * (1*_scale.x) - st * (1*_scale.y)),(st * (1*_scale.x) + ct * (1*_scale.y)),0)+offset;
  1016. vertices[1+extra] = new Vector3((ct * (1*_scale.x) - st * (-1*_scale.y)),(st * (1*_scale.x) + ct * (-1*_scale.y)),0)+offset;
  1017. vertices[2+extra] = new Vector3((ct * (-1*_scale.x) - st * (1*_scale.y)),(st * (-1*_scale.x) + ct * (1*_scale.y)),0)+offset;
  1018. vertices[3+extra] = new Vector3((ct * (-1*_scale.x) - st * (-1*_scale.y)),(st * (-1*_scale.x) + ct * (-1*_scale.y)),0)+offset;
  1019. }
  1020. Color32 _color32 = _color;
  1021. colors[0+extra] = _color32;
  1022. colors[1+extra] = _color32;
  1023. colors[2+extra] = _color32;
  1024. colors[3+extra] = _color32;
  1025. count++;
  1026. }
  1027. break;
  1028. case(ProFlareElement.Type.Multi):
  1029. {
  1030. for(int i2 = 0; i2 < FlareElementsArray[i].subElements.Count; i2++){
  1031. int extra2 = (count+i2) * 4;
  1032. //Check for DisabledPlayMode, then scale to zero. then skip over the rest of the calculations
  1033. if(FlareElementsArray[i].flare.DisabledPlayMode){
  1034. vertices[0+extra2] = Vector3.zero;
  1035. vertices[1+extra2] = Vector3.zero;
  1036. vertices[2+extra2] = Vector3.zero;
  1037. vertices[3+extra2] = Vector3.zero;
  1038. continue;
  1039. }
  1040. ////Profiler.BeginSample("Calc Scale");
  1041. _scale = (((FlareElementsArray[i].size*FlareElementsArray[i].Scale*0.01f)*FlareElementsArray[i].flare.GlobalScale)*FlareElementsArray[i].subElements[i2].scale)*FlareElementsArray[i].ScaleFinal;
  1042. //Avoid Negative scaling
  1043. _scale = _scale*scaleMulti;
  1044. if((_scale.x < 0)||(_scale.y < 0))
  1045. _scale = Vector3.zero;
  1046. ////Profiler.EndSample();
  1047. ////Profiler.BeginSample("Calc Extras");
  1048. Vector3 offset = FlareElementsArray[i].subElements[i2].offset;
  1049. float angle = FlareElementsArray[i].FinalAngle;
  1050. //angle = 0;
  1051. angle += FlareElementsArray[i].subElements[i2].angle;
  1052. _color = FlareElementsArray[i].subElements[i2].colorFinal;
  1053. if(useBrightnessThreshold){
  1054. if(_color.a < BrightnessThreshold){
  1055. _scale = Vector2.zero;
  1056. }else if(_color.r+_color.g+_color.b < BrightnessThreshold){
  1057. _scale = Vector2.zero;
  1058. }
  1059. }
  1060. if(overdrawDebug)
  1061. _color = new Color32(20,20,20,100);
  1062. // //Profiler.EndSample();
  1063. ////Profiler.BeginSample("Set Pos and Rot");
  1064. if(!FlareElementsArray[i].flare.DisabledPlayMode){
  1065. //Precalculate some of the RotatePoint function to avoid repeat math.
  1066. float r = angle * (PI_Div180);
  1067. float ct = Mathf.Cos(r);
  1068. float st = Mathf.Sin(r);
  1069. vertices[0+extra2] = new Vector3((ct * (1*_scale.x) - st * (1*_scale.y)),(st * (1*_scale.x) + ct * (1*_scale.y)),0)+offset;
  1070. vertices[1+extra2] = new Vector3((ct * (1*_scale.x) - st * (-1*_scale.y)),(st * (1*_scale.x) + ct * (-1*_scale.y)),0)+offset;
  1071. vertices[2+extra2] = new Vector3((ct * (-1*_scale.x) - st * (1*_scale.y)),(st * (-1*_scale.x) + ct * (1*_scale.y)),0)+offset;
  1072. vertices[3+extra2] = new Vector3((ct * (-1*_scale.x) - st * (-1*_scale.y)),(st * (-1*_scale.x) + ct * (-1*_scale.y)),0)+offset;
  1073. }
  1074. ////Profiler.EndSample();
  1075. ////Profiler.BeginSample("SetColors");
  1076. Color32 _color32 = _color;
  1077. colors[0+extra2] = _color32;
  1078. colors[1+extra2] = _color32;
  1079. colors[2+extra2] = _color32;
  1080. colors[3+extra2] = _color32;
  1081. ////Profiler.EndSample();
  1082. }
  1083. count = count+FlareElementsArray[i].subElements.Count;
  1084. }
  1085. break;
  1086. }
  1087. }
  1088. //Profiler.EndSample();
  1089. // Debug.Log("updare Done");
  1090. }
  1091. }