VolumeFog.cs 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. #if UNITY_EDITOR
  5. using UnityEditor;
  6. using UnityEditor.SceneManagement;
  7. #endif
  8. using UnityEngine;
  9. using Object = UnityEngine.Object;
  10. namespace FXV
  11. {
  12. [ExecuteInEditMode]
  13. public class VolumeFog : MonoBehaviour
  14. {
  15. static bool XR_ENABLED = false;
  16. public static void SetEnabledXR(bool enabled)
  17. {
  18. XR_ENABLED = enabled;
  19. }
  20. public enum FogType
  21. {
  22. ViewAligned = 0,
  23. SphericalPos = 1,
  24. SphericalDist = 2,
  25. InvertedSpherical = 8,
  26. InvertedSphericalXHeight = 11,
  27. BoxPos = 3,
  28. BoxDist = 4,
  29. BoxExperimental = 5,
  30. Height = 6,
  31. HeightXBox = 7,
  32. HeightXView = 9,
  33. BoxXView = 10,
  34. };
  35. public enum FogClipping
  36. {
  37. None = 0,
  38. ClipToSkybox = 1,
  39. ClipToBounds = 2
  40. }
  41. public enum FogBlendMode
  42. {
  43. AlphaBlend,
  44. Add
  45. }
  46. public enum FogMeshType
  47. {
  48. Default = 0,
  49. Custom
  50. }
  51. public enum FogFallof
  52. {
  53. Linear = 0,
  54. Smoothed,
  55. Exp,
  56. Exp2
  57. }
  58. public enum FogFunction
  59. {
  60. Default = 0,
  61. Alternative
  62. }
  63. public enum FogRenderMode
  64. {
  65. Simplified = 0,
  66. Default
  67. }
  68. [SerializeField] FogType fogType = FogType.ViewAligned;
  69. [SerializeField] float fogMin = 0.0f;
  70. [SerializeField] float fogMax = 1.0f;
  71. [SerializeField] float _fogDepth = 1.0f;
  72. public float FogMin
  73. {
  74. get { return fogMin; }
  75. set
  76. {
  77. fogMin = value;
  78. _fogDepth = fogMax - fogMin;
  79. isPropsDirty = true;
  80. }
  81. }
  82. public float FogMax
  83. {
  84. get { return fogMax; }
  85. set
  86. {
  87. fogMax = value;
  88. _fogDepth = fogMax - fogMin;
  89. isPropsDirty = true;
  90. }
  91. }
  92. public float FogDepth
  93. {
  94. get { return fogMax - fogMin; }
  95. set
  96. {
  97. _fogDepth = value;
  98. fogMin = fogMax - value;
  99. isPropsDirty = true;
  100. }
  101. }
  102. [SerializeField] float secFogMin = 0.0f;
  103. [SerializeField] float secFogMax = 1.0f;
  104. [SerializeField] Vector3 worldSize = Vector3.one;
  105. [SerializeField] FogFallof fogFalloffType = FogFallof.Linear;
  106. [SerializeField, Range(0.5f, 15.0f)] float fogFalloffCurve = 1.0f;
  107. //[SerializeField]
  108. //int renderQueue = 3100;
  109. [SerializeField, FXV.Internal.fxvSortingLayerAttribute]
  110. string sortingLayer = "Default";
  111. [SerializeField] int sortingOrder = 0;
  112. [SerializeField] uint renderingLayerMask = 0xffffffff;
  113. [SerializeField] Color fogColor = Color.white;
  114. [SerializeField] FogBlendMode blendType = FogBlendMode.AlphaBlend;
  115. [SerializeField] FogMeshType fogMeshType = FogMeshType.Default;
  116. [SerializeField] Mesh customMesh = null;
  117. [SerializeField] FogClipping fogClipping = FogClipping.None;
  118. [SerializeField] FogRenderMode renderMode = FogRenderMode.Default;
  119. [SerializeField] bool affectedByLights = false;
  120. [SerializeField, Range(0.1f, 2.0f)] float lightScatteringFactor = 1.0f;
  121. [SerializeField, Range(0.0f, 1.0f)] float lightReflectivity = 1.0f;
  122. [SerializeField, Range(0.0f, 1.0f)] float lightTransmission = 0.5f;
  123. VolumeFogGroup fogGroup = null;
  124. Renderer myRenderer = null;
  125. MeshFilter myFilter = null;
  126. MaterialPropertyBlock props = null;
  127. bool isPropsDirty = true;
  128. Mesh plane = null;
  129. Mesh renderMesh = null;
  130. bool renderMeshIsProcedural = false;
  131. Bounds rendererLocalBounds;
  132. Transform myTransform;
  133. Vector3 myPosition;
  134. Vector3 boundsOffset = Vector3.zero;
  135. float globalScaleFactor = 1.0f;
  136. private void Start()
  137. {
  138. SetupComponents();
  139. if (myTransform == null)
  140. {
  141. myTransform = transform;
  142. myPosition = transform.position;
  143. }
  144. PrepareFogObject();
  145. myRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
  146. Material mat = GetCachedMaterial(this);
  147. if (myRenderer.sharedMaterial != mat)
  148. {
  149. myRenderer.sharedMaterial = mat;
  150. }
  151. rendererLocalBounds = myRenderer.localBounds;
  152. Camera.onPostRender += CameraFinishedRendering;
  153. }
  154. void SetupComponents()
  155. {
  156. if (myFilter == null)
  157. {
  158. myFilter = GetComponent<MeshFilter>();
  159. if (myFilter == null)
  160. {
  161. myFilter = gameObject.AddComponent<MeshFilter>();
  162. }
  163. myFilter.hideFlags = HideFlags.HideInInspector;
  164. }
  165. if (myRenderer == null)
  166. {
  167. myRenderer = GetComponent<Renderer>();
  168. if (myRenderer == null)
  169. {
  170. myRenderer = gameObject.AddComponent<MeshRenderer>();
  171. }
  172. myRenderer.hideFlags = HideFlags.HideInInspector;
  173. }
  174. }
  175. internal void DestroyAsset(Object assetObject)
  176. {
  177. #if UNITY_EDITOR
  178. if (!Application.isPlaying)
  179. {
  180. DestroyImmediate(assetObject);
  181. }
  182. else
  183. #endif
  184. {
  185. Destroy(assetObject);
  186. }
  187. }
  188. internal void PrepareFogObject()
  189. {
  190. if (IsUsingDepthWorkflow())
  191. {
  192. FogDepth = _fogDepth;
  193. }
  194. else
  195. {
  196. FogDepth = fogMax - fogMin;
  197. }
  198. if (fogGroup != null)
  199. {
  200. VolumeFogGroup parentGroup = GetComponentInParent<VolumeFogGroup>();
  201. if (parentGroup == null)
  202. {
  203. TryUnegisterFromGroup();
  204. }
  205. else if (parentGroup != fogGroup)
  206. {
  207. TryUnegisterFromGroup();
  208. TryRegisterInGroup();
  209. }
  210. }
  211. SetupComponents();
  212. Material mat = GetCachedMaterial(this);
  213. if (myRenderer.sharedMaterial != mat)
  214. {
  215. myRenderer.sharedMaterial = mat;
  216. }
  217. if (myTransform == null)
  218. {
  219. myTransform = transform;
  220. }
  221. myPosition = myTransform.position;
  222. UpdateScale();
  223. if (props == null)
  224. {
  225. CreateMaterialProps();
  226. }
  227. else
  228. {
  229. UpdateMaterialProps();
  230. }
  231. #if UNITY_EDITOR
  232. UnityEditor.EditorApplication.delayCall += _DelayedValidate;
  233. #else
  234. _DelayedValidate();
  235. #endif
  236. }
  237. private void OnValidate()
  238. {
  239. PrepareFogObject();
  240. UpdateScale();
  241. }
  242. internal void _DelayedValidate()
  243. {
  244. UpdateRenderMesh();
  245. }
  246. public bool IsCustomMesh()
  247. {
  248. return fogMeshType == FogMeshType.Custom;
  249. }
  250. public bool IsUsingDepthWorkflow()
  251. {
  252. return (fogType == FogType.InvertedSpherical) || fogType == FogType.InvertedSphericalXHeight;
  253. }
  254. public bool IsSphereShape()
  255. {
  256. return (fogType == FogType.SphericalPos || fogType == FogType.SphericalDist);
  257. }
  258. public bool IsBoxShape()
  259. {
  260. return !IsSphereShape();
  261. }
  262. public bool IsUsingSecondaryParams()
  263. {
  264. return fogType == FogType.HeightXBox || fogType == FogType.HeightXView || fogType == FogType.BoxXView ||
  265. fogType == FogType.InvertedSphericalXHeight;
  266. }
  267. internal void UpdateRenderMesh()
  268. {
  269. if (this == null)
  270. {
  271. return;
  272. }
  273. if (myFilter == null)
  274. {
  275. myFilter = GetComponent<MeshFilter>();
  276. }
  277. if (renderMesh != null && renderMeshIsProcedural)
  278. {
  279. DestroyAsset(renderMesh);
  280. renderMeshIsProcedural = false;
  281. }
  282. if (IsCustomMesh())
  283. {
  284. myFilter.sharedMesh = customMesh;
  285. renderMesh = customMesh;
  286. worldSize = myRenderer.localBounds.size;
  287. boundsOffset = myRenderer.localBounds.center;
  288. UpdateMaterialProps();
  289. }
  290. else
  291. {
  292. if (IsSphereShape())
  293. {
  294. renderMesh = Resources.GetBuiltinResource<Mesh>("New-Sphere.fbx");
  295. myFilter.sharedMesh = renderMesh;
  296. }
  297. else if (IsBoxShape())
  298. {
  299. renderMesh = Resources.GetBuiltinResource<Mesh>("Cube.fbx");
  300. myFilter.sharedMesh = renderMesh;
  301. }
  302. else
  303. {
  304. InitializeBoxRenderMesh();
  305. myFilter.sharedMesh = renderMesh;
  306. }
  307. boundsOffset = Vector3.zero;
  308. }
  309. rendererLocalBounds = myRenderer.localBounds;
  310. }
  311. internal void UpdateGlobalScaleFactor()
  312. {
  313. Vector3 parentScale = (transform.parent != null) ? transform.parent.lossyScale : Vector3.one;
  314. float maxScale = Mathf.Max(Mathf.Max(parentScale.x, parentScale.y), parentScale.z);
  315. float minScale = Mathf.Min(Mathf.Min(parentScale.x, parentScale.y), parentScale.z);
  316. globalScaleFactor = (minScale + (maxScale - minScale) * 0.5f);
  317. }
  318. internal void UpdateScale()
  319. {
  320. UpdateGlobalScaleFactor();
  321. if (IsCustomMesh())
  322. {
  323. transform.localScale = Vector3.one;
  324. // lock scale for custom mesh
  325. }
  326. else
  327. {
  328. if (IsSphereShape())
  329. {
  330. transform.localScale = Vector3.one * Mathf.Max(fogMin, fogMax) * 2.0f;
  331. }
  332. else if (IsBoxShape())
  333. {
  334. transform.localScale = worldSize;
  335. }
  336. else
  337. {
  338. transform.localScale = Vector3.one;
  339. }
  340. }
  341. }
  342. internal void UpdateTransformScaleChanges()
  343. {
  344. if (IsCustomMesh())
  345. {
  346. transform.localScale = Vector3.one;
  347. // lock scale for custom mesh
  348. }
  349. else
  350. {
  351. if (IsSphereShape())
  352. {
  353. float currentScale = Mathf.Max(fogMin, fogMax) * 2.0f;
  354. bool changed = false;
  355. float r = fogMin / fogMax;
  356. if (transform.localScale.x != currentScale)
  357. {
  358. fogMax = transform.localScale.x * 0.5f;
  359. transform.localScale = Vector3.one * transform.localScale.x;
  360. changed = true;
  361. }
  362. if (transform.localScale.y != currentScale)
  363. {
  364. fogMax = transform.localScale.y * 0.5f;
  365. transform.localScale = Vector3.one * transform.localScale.y;
  366. changed = true;
  367. }
  368. if (transform.localScale.z != currentScale)
  369. {
  370. fogMax = transform.localScale.z * 0.5f;
  371. transform.localScale = Vector3.one * transform.localScale.z;
  372. changed = true;
  373. }
  374. if (changed)
  375. {
  376. fogMin = r * fogMax;
  377. UpdateGlobalScaleFactor();
  378. UpdateMaterialProps();
  379. }
  380. }
  381. else if (IsBoxShape())
  382. {
  383. if (worldSize != transform.localScale)
  384. {
  385. worldSize = transform.localScale;
  386. UpdateGlobalScaleFactor();
  387. UpdateMaterialProps();
  388. }
  389. }
  390. else
  391. {
  392. transform.localScale = Vector3.one;
  393. }
  394. }
  395. }
  396. internal void CreateMaterialProps()
  397. {
  398. props = new MaterialPropertyBlock();
  399. UpdateMaterialProps();
  400. }
  401. internal void UpdateMaterialProps()
  402. {
  403. props.SetColor("_Color", GetFogColor());
  404. props.SetVector("_WorldSize", worldSize * globalScaleFactor);
  405. if (IsCustomMesh())
  406. {
  407. props.SetVector("_LocalSize", worldSize); //worldSize stores local bounds size for custom mesh
  408. props.SetVector("_LocalOffset", boundsOffset);
  409. }
  410. props.SetFloat("_FogMin", fogMin * globalScaleFactor);
  411. props.SetFloat("_FogMax", fogMax * globalScaleFactor);
  412. props.SetFloat("_FogFalloff", GetFogFalloffParam());
  413. if (IsUsingSecondaryParams())
  414. {
  415. props.SetFloat("_SecFogMin", secFogMin * globalScaleFactor);
  416. props.SetFloat("_SecFogMax", secFogMax * globalScaleFactor);
  417. }
  418. if (IsAffectedByLights())
  419. {
  420. props.SetFloat("_LightScatteringFactor", GetLightScatteringFactor());
  421. props.SetFloat("_LightReflectivity", GetLightReflectivity());
  422. props.SetFloat("_LightTransmission", GetLightTransmission());
  423. }
  424. isPropsDirty = true;
  425. }
  426. internal void TryRegisterInGroup()
  427. {
  428. if (fogGroup == null)
  429. {
  430. fogGroup = GetComponentInParent<VolumeFogGroup>();
  431. if (fogGroup)
  432. {
  433. fogGroup.RegisterFogObject(this);
  434. }
  435. }
  436. }
  437. internal void TryUnegisterFromGroup()
  438. {
  439. if (fogGroup)
  440. {
  441. fogGroup.UnregisterFogObject(this);
  442. fogGroup = null;
  443. }
  444. }
  445. private void OnEnable()
  446. {
  447. SetupComponents();
  448. TryRegisterInGroup();
  449. myRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
  450. Camera.onPostRender -= CameraFinishedRendering;
  451. Camera.onPostRender += CameraFinishedRendering;
  452. if (props == null)
  453. {
  454. CreateMaterialProps();
  455. }
  456. else
  457. {
  458. UpdateMaterialProps();
  459. }
  460. }
  461. private void OnDisable()
  462. {
  463. TryUnegisterFromGroup();
  464. Camera.onPostRender -= CameraFinishedRendering;
  465. }
  466. void OnDestroy()
  467. {
  468. if (plane)
  469. {
  470. DestroyAsset(plane);
  471. plane = null;
  472. }
  473. if (renderMesh != null && renderMeshIsProcedural)
  474. {
  475. DestroyAsset(renderMesh);
  476. renderMeshIsProcedural = false;
  477. }
  478. if (fogGroup)
  479. {
  480. fogGroup.UnregisterFogObject(this);
  481. fogGroup = null;
  482. }
  483. props = null;
  484. myRenderer = null;
  485. Camera.onPostRender -= CameraFinishedRendering;
  486. }
  487. public void OnDidApplyAnimationProperties()
  488. {
  489. if (props != null)
  490. {
  491. UpdateMaterialProps();
  492. }
  493. }
  494. private void OnTransformParentChanged()
  495. {
  496. if (props == null)
  497. {
  498. return;
  499. }
  500. UpdateScale();
  501. UpdateMaterialProps();
  502. }
  503. private void Update()
  504. {
  505. }
  506. internal bool RenderCameraPlane(Camera camera)
  507. {
  508. if (IsSphereShape())
  509. {
  510. //TODO: optimize this
  511. float nearOffset = camera.nearClipPlane / Mathf.Cos(camera.fieldOfView * 0.5f * Mathf.Deg2Rad);
  512. float radius = Mathf.Max(fogMin, fogMax) * globalScaleFactor + nearOffset;
  513. Vector3 distVec = transform.position - camera.transform.position;
  514. if (distVec.sqrMagnitude < radius * radius)
  515. {
  516. return true;
  517. }
  518. }
  519. else
  520. {
  521. Bounds b = myRenderer.bounds;
  522. b.extents += Vector3.one *
  523. (camera.nearClipPlane / Mathf.Cos(camera.fieldOfView * 0.5f * Mathf.Deg2Rad));
  524. return b.Contains(camera.transform.position);
  525. }
  526. return false;
  527. }
  528. private void LateUpdate()
  529. {
  530. UpdateParams();
  531. }
  532. private void OnWillRenderObject()
  533. {
  534. try
  535. {
  536. UpdateParams();
  537. if (!Application.isPlaying)
  538. {
  539. UpdateTransformScaleChanges();
  540. }
  541. Camera camera = VolumeFogManager.instance.GetCurrentRenderingCamera();
  542. if (camera)
  543. {
  544. if ((camera.depthTextureMode & DepthTextureMode.Depth) == 0)
  545. {
  546. camera.depthTextureMode |= DepthTextureMode.Depth;
  547. }
  548. if (RenderCameraPlane(camera))
  549. {
  550. if (props == null)
  551. {
  552. CreateMaterialProps();
  553. }
  554. if (plane == null)
  555. {
  556. InitializeCameraMesh();
  557. }
  558. UpdateMesh(camera);
  559. myFilter.sharedMesh = plane;
  560. }
  561. else
  562. {
  563. myFilter.sharedMesh = renderMesh;
  564. }
  565. }
  566. }
  567. catch (Exception e)
  568. {
  569. Debug.LogError(e);
  570. }
  571. }
  572. private void CameraFinishedRendering(Camera cam)
  573. {
  574. }
  575. void InitializeBoxRenderMesh()
  576. {
  577. Vector3 extents = worldSize * 0.5f;
  578. renderMesh = new Mesh();
  579. renderMesh.name = "ProceduralBox_" + worldSize.x + "_" + worldSize.y + "_" + worldSize.z;
  580. Vector3[] c = new Vector3[8];
  581. c[0] = new Vector3(-extents.x, -extents.y, extents.z);
  582. c[1] = new Vector3(extents.x, -extents.y, extents.z);
  583. c[2] = new Vector3(extents.x, -extents.y, -extents.z);
  584. c[3] = new Vector3(-extents.x, -extents.y, -extents.z);
  585. c[4] = new Vector3(-extents.x, extents.y, extents.z);
  586. c[5] = new Vector3(extents.x, extents.y, extents.z);
  587. c[6] = new Vector3(extents.x, extents.y, -extents.z);
  588. c[7] = new Vector3(-extents.x, extents.y, -extents.z);
  589. Vector3[] vertices = new Vector3[24]
  590. {
  591. c[0], c[1], c[2], c[3], // Bottom
  592. c[7], c[4], c[0], c[3], // Left
  593. c[4], c[5], c[1], c[0], // Front
  594. c[6], c[7], c[3], c[2], // Back
  595. c[5], c[6], c[2], c[1], // Right
  596. c[7], c[6], c[5], c[4] // Top
  597. };
  598. renderMesh.vertices = vertices;
  599. int[] tris = new int[]
  600. {
  601. 3, 1, 0, 3, 2, 1, // Bottom
  602. 7, 5, 4, 7, 6, 5, // Left
  603. 11, 9, 8, 11, 10, 9, // Front
  604. 15, 13, 12, 15, 14, 13, // Back
  605. 19, 17, 16, 19, 18, 17, // Right
  606. 23, 21, 20, 23, 22, 21, // Top
  607. };
  608. renderMesh.triangles = tris;
  609. Vector3 up = Vector3.up;
  610. Vector3 down = Vector3.down;
  611. Vector3 forward = Vector3.forward;
  612. Vector3 back = Vector3.back;
  613. Vector3 left = Vector3.left;
  614. Vector3 right = Vector3.right;
  615. Vector3[] normals = new Vector3[]
  616. {
  617. down, down, down, down, // Bottom
  618. left, left, left, left, // Left
  619. forward, forward, forward, forward, // Front
  620. back, back, back, back, // Back
  621. right, right, right, right, // Right
  622. up, up, up, up // Top
  623. };
  624. renderMesh.normals = normals;
  625. Vector2 uv00 = new Vector2(0f, 0f);
  626. Vector2 uv10 = new Vector2(1f, 0f);
  627. Vector2 uv01 = new Vector2(0f, 1f);
  628. Vector2 uv11 = new Vector2(1f, 1f);
  629. Vector2[] uv = new Vector2[]
  630. {
  631. uv11, uv01, uv00, uv10, // Bottom
  632. uv11, uv01, uv00, uv10, // Left
  633. uv11, uv01, uv00, uv10, // Front
  634. uv11, uv01, uv00, uv10, // Back
  635. uv11, uv01, uv00, uv10, // Right
  636. uv11, uv01, uv00, uv10 // Top
  637. };
  638. renderMesh.uv = uv;
  639. renderMesh.RecalculateBounds();
  640. renderMesh.Optimize();
  641. renderMesh.MarkDynamic();
  642. renderMeshIsProcedural = true;
  643. }
  644. void UpdateBoxRenderMesh()
  645. {
  646. Vector3 extents = worldSize * 0.5f;
  647. Vector3[] c = new Vector3[8];
  648. c[0] = new Vector3(-extents.x, -extents.y, extents.z);
  649. c[1] = new Vector3(extents.x, -extents.y, extents.z);
  650. c[2] = new Vector3(extents.x, -extents.y, -extents.z);
  651. c[3] = new Vector3(-extents.x, -extents.y, -extents.z);
  652. c[4] = new Vector3(-extents.x, extents.y, extents.z);
  653. c[5] = new Vector3(extents.x, extents.y, extents.z);
  654. c[6] = new Vector3(extents.x, extents.y, -extents.z);
  655. c[7] = new Vector3(-extents.x, extents.y, -extents.z);
  656. Vector3[] vertices = new Vector3[24]
  657. {
  658. c[0], c[1], c[2], c[3], // Bottom
  659. c[7], c[4], c[0], c[3], // Left
  660. c[4], c[5], c[1], c[0], // Front
  661. c[6], c[7], c[3], c[2], // Back
  662. c[5], c[6], c[2], c[1], // Right
  663. c[7], c[6], c[5], c[4] // Top
  664. };
  665. renderMesh.vertices = vertices;
  666. }
  667. void InitializeCameraMesh()
  668. {
  669. plane = new Mesh();
  670. Vector3[] vertices = new Vector3[4]
  671. {
  672. new Vector3(-1.0f, -1.0f, 0),
  673. new Vector3(1.0f, -1.0f, 0),
  674. new Vector3(-1.0f, 1.0f, 0),
  675. new Vector3(1.0f, 1.0f, 0)
  676. };
  677. plane.vertices = vertices;
  678. int[] tris = new int[6]
  679. {
  680. // lower left triangle
  681. 0, 2, 1,
  682. // upper right triangle
  683. 2, 3, 1
  684. };
  685. plane.triangles = tris;
  686. Vector3[] normals = new Vector3[4]
  687. {
  688. -Vector3.forward,
  689. -Vector3.forward,
  690. -Vector3.forward,
  691. -Vector3.forward
  692. };
  693. plane.normals = normals;
  694. Vector2[] uv = new Vector2[4]
  695. {
  696. new Vector2(0, 0),
  697. new Vector2(1, 0),
  698. new Vector2(0, 1),
  699. new Vector2(1, 1)
  700. };
  701. plane.uv = uv;
  702. plane.bounds = rendererLocalBounds;
  703. plane.MarkDynamic();
  704. }
  705. void UpdateMesh(Camera camera)
  706. {
  707. float zOffset = 0.001f;
  708. Vector3[] vertices = null;
  709. float stereoSeparationX = (camera.stereoEnabled || XR_ENABLED) ? 3.0f : 0.2f;
  710. float stereoSeparationY = (camera.stereoEnabled || XR_ENABLED) ? 1.0f : 0.2f;
  711. vertices = new Vector3[4]
  712. {
  713. camera.ViewportToWorldPoint(new Vector3(-stereoSeparationX, -stereoSeparationY,
  714. camera.nearClipPlane + zOffset)),
  715. camera.ViewportToWorldPoint(new Vector3(1.0f + stereoSeparationX, -stereoSeparationY,
  716. camera.nearClipPlane + zOffset)),
  717. camera.ViewportToWorldPoint(new Vector3(-stereoSeparationX, 1.0f + stereoSeparationY,
  718. camera.nearClipPlane + zOffset)),
  719. camera.ViewportToWorldPoint(new Vector3(1.0f + stereoSeparationX, 1.0f + stereoSeparationY,
  720. camera.nearClipPlane + zOffset))
  721. };
  722. vertices[0] = transform.InverseTransformPoint(vertices[0]);
  723. vertices[1] = transform.InverseTransformPoint(vertices[1]);
  724. vertices[2] = transform.InverseTransformPoint(vertices[2]);
  725. vertices[3] = transform.InverseTransformPoint(vertices[3]);
  726. plane.vertices = vertices;
  727. plane.bounds = rendererLocalBounds;
  728. }
  729. public void SetFogColor(Color color)
  730. {
  731. if (fogGroup != null && fogGroup.controlsColor)
  732. {
  733. Debug.LogWarning(
  734. "Setting color for fog object that is controlled by group - this will not take effect");
  735. }
  736. fogColor = color;
  737. props.SetColor("_Color", GetFogColor());
  738. isPropsDirty = true;
  739. }
  740. public FogType GetFogType()
  741. {
  742. return fogType;
  743. }
  744. public float GetFogFalloffParam()
  745. {
  746. return fogGroup != null && fogGroup.controlsFalloffParam
  747. ? Mathf.Clamp(fogFalloffCurve * fogGroup.falloffParamMultiplier, 0.5f, 15.0f)
  748. : fogFalloffCurve;
  749. }
  750. public Vector3 GetFogBoxSize()
  751. {
  752. return worldSize;
  753. }
  754. public void SetFogBoxSize(Vector3 size)
  755. {
  756. worldSize = size;
  757. }
  758. public Color GetFogColor()
  759. {
  760. return fogGroup != null && fogGroup.controlsColor ? fogGroup.fogColor : fogColor;
  761. }
  762. public VolumeFogGroup GetParentFogGroup()
  763. {
  764. return fogGroup;
  765. }
  766. public void SetAffectedByLights(bool affected)
  767. {
  768. if (fogGroup != null && fogGroup.controlsLighting)
  769. {
  770. Debug.LogWarning(
  771. "Setting affected by lights for fog object that is controlled by group - this will not take effect");
  772. }
  773. affectedByLights = affected;
  774. Material mat = GetCachedMaterial(this);
  775. if (myRenderer.sharedMaterial != mat)
  776. {
  777. myRenderer.sharedMaterial = mat;
  778. }
  779. UpdateMaterialProps();
  780. }
  781. public bool IsAffectedByLights()
  782. {
  783. return fogGroup != null && fogGroup.controlsLighting ? fogGroup.affectedByLights : affectedByLights;
  784. }
  785. public float GetLightScatteringFactor()
  786. {
  787. return fogGroup != null && fogGroup.controlsLighting
  788. ? fogGroup.lightScatteringFactor
  789. : lightScatteringFactor;
  790. }
  791. public float GetLightReflectivity()
  792. {
  793. return fogGroup != null && fogGroup.controlsLighting ? fogGroup.lightReflectivity : lightReflectivity;
  794. }
  795. public float GetLightTransmission()
  796. {
  797. return fogGroup != null && fogGroup.controlsLighting ? fogGroup.lightTransmission : lightTransmission;
  798. }
  799. void UpdateParams()
  800. {
  801. if (isPropsDirty)
  802. {
  803. myRenderer.SetPropertyBlock(props);
  804. myRenderer.sortingLayerName = sortingLayer;
  805. myRenderer.sortingOrder = sortingOrder;
  806. myRenderer.renderingLayerMask = renderingLayerMask;
  807. isPropsDirty = false;
  808. }
  809. }
  810. internal string GetMaterialName()
  811. {
  812. string ret = "VF";
  813. ret += IsAffectedByLights() ? "Lit" : "Unlit";
  814. ret += blendType.ToString();
  815. ret += fogType.ToString();
  816. ret += fogFalloffType.ToString();
  817. ret += renderMode.ToString();
  818. ret += fogMeshType.ToString();
  819. ret += fogClipping.ToString();
  820. if (Internal.fxvFogAssetConfig.ActiveRenderPipeline == Internal.fxvFogAssetConfig.Pipeline.URP)
  821. {
  822. ret += "URP";
  823. }
  824. return ret;
  825. }
  826. internal static Dictionary<string, Material> cachedMaterials = new Dictionary<string, Material>();
  827. internal static Material GetCachedMaterial(VolumeFog fogObject)
  828. {
  829. string materialKey = fogObject.GetMaterialName();
  830. Material mat = null;
  831. if (cachedMaterials.TryGetValue(materialKey, out mat))
  832. {
  833. if (mat == null)
  834. {
  835. mat = CreateMaterial(fogObject);
  836. if (mat)
  837. {
  838. cachedMaterials[materialKey] = mat;
  839. }
  840. }
  841. return mat;
  842. }
  843. string cachePath = "MaterialsCache/" + materialKey;
  844. mat = Resources.Load(cachePath) as Material;
  845. if (mat == null)
  846. {
  847. mat = CreateMaterial(fogObject);
  848. if (mat)
  849. {
  850. #if UNITY_EDITOR
  851. UnityEditor.EditorApplication.delayCall += () =>
  852. {
  853. fogObject._DelayedSaveToCache(materialKey, mat);
  854. };
  855. #endif
  856. cachedMaterials[materialKey] = mat;
  857. }
  858. }
  859. else
  860. {
  861. SetupMaterialKeywords(mat, fogObject);
  862. cachedMaterials[materialKey] = mat;
  863. }
  864. return mat;
  865. }
  866. #if UNITY_EDITOR
  867. internal void _DelayedSaveToCache(string materialKey, Material mat)
  868. {
  869. if (mat == null)
  870. {
  871. Debug.Log("Trying to save null material for key " + materialKey);
  872. return;
  873. }
  874. string saveFolder = Internal.fxvFogAssetConfig.AssetPath + "/Resources/MaterialsCache/";
  875. if (!System.IO.Directory.Exists(saveFolder))
  876. {
  877. System.IO.Directory.CreateDirectory(saveFolder);
  878. }
  879. UnityEditor.AssetDatabase.CreateAsset(mat, saveFolder + materialKey + ".mat");
  880. UnityEditor.AssetDatabase.SaveAssets();
  881. UnityEditor.AssetDatabase.Refresh();
  882. }
  883. #endif
  884. internal static void SetupMaterialKeywords(Material mat, VolumeFog fogObject)
  885. {
  886. mat.SetFloat("_FogType", (float)fogObject.fogType);
  887. var types = System.Enum.GetNames(typeof(VolumeFog.FogType));
  888. foreach (string name in types)
  889. {
  890. mat.DisableKeyword("FXV_FOGTYPE_" + name.ToUpper());
  891. }
  892. mat.EnableKeyword("FXV_FOGTYPE_" + fogObject.fogType.ToString().ToUpper());
  893. mat.SetFloat("_FogFalloffType", (float)fogObject.fogFalloffType);
  894. if (fogObject.fogFalloffType == VolumeFog.FogFallof.Linear)
  895. {
  896. mat.EnableKeyword("FXV_LINEAR_FALLOFF");
  897. }
  898. else if (fogObject.fogFalloffType == VolumeFog.FogFallof.Smoothed)
  899. {
  900. mat.EnableKeyword("FXV_SMOOTHED_FALLOFF");
  901. }
  902. else if (fogObject.fogFalloffType == VolumeFog.FogFallof.Exp)
  903. {
  904. mat.EnableKeyword("FXV_EXP_FALLOFF");
  905. }
  906. else if (fogObject.fogFalloffType == VolumeFog.FogFallof.Exp2)
  907. {
  908. mat.EnableKeyword("FXV_EXP2_FALLOFF");
  909. }
  910. mat.SetFloat("_BlendMode", (float)fogObject.blendType);
  911. if (fogObject.blendType == VolumeFog.FogBlendMode.AlphaBlend)
  912. {
  913. mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
  914. mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  915. mat.SetInt("_ZWrite", 0);
  916. }
  917. else if (fogObject.blendType == VolumeFog.FogBlendMode.Add)
  918. {
  919. mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
  920. mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.One);
  921. mat.SetInt("_ZWrite", 0);
  922. }
  923. mat.SetFloat("_InAirEnabled", (float)fogObject.renderMode);
  924. if (fogObject.renderMode == FogRenderMode.Default)
  925. {
  926. mat.EnableKeyword("FXV_IN_AIR_FOG");
  927. }
  928. mat.SetFloat("_FogMeshType", (float)fogObject.fogMeshType);
  929. if (fogObject.fogMeshType == FogMeshType.Custom)
  930. {
  931. mat.EnableKeyword("FXV_FOG_CUSTOM_MESH");
  932. }
  933. mat.SetFloat("_FogClipping", (float)fogObject.fogClipping);
  934. if (fogObject.fogClipping == FogClipping.ClipToSkybox)
  935. {
  936. mat.EnableKeyword("FXV_FOG_CLIP_SKYBOX");
  937. }
  938. else if (fogObject.fogClipping == FogClipping.ClipToBounds)
  939. {
  940. mat.EnableKeyword("FXV_FOG_CLIP_BOUNDS");
  941. }
  942. mat.enableInstancing = true;
  943. }
  944. internal static Material CreateMaterial(VolumeFog fogObject)
  945. {
  946. Material mat = null;
  947. string shaderVersion = "";
  948. if (Internal.fxvFogAssetConfig.ActiveRenderPipeline == Internal.fxvFogAssetConfig.Pipeline.URP)
  949. {
  950. shaderVersion += "URP";
  951. }
  952. if (fogObject.IsAffectedByLights())
  953. {
  954. Shader shader = Shader.Find("FXV/FXVVolumeFogLit" + shaderVersion);
  955. if (!shader) //this might happen when render pipeline specific package is not imported
  956. {
  957. return null;
  958. }
  959. mat = new Material(shader);
  960. }
  961. else
  962. {
  963. mat = new Material(Shader.Find("FXV/FXVVolumeFog"));
  964. }
  965. SetupMaterialKeywords(mat, fogObject);
  966. return mat;
  967. }
  968. #if UNITY_EDITOR
  969. public static void SetFogType(VolumeFog fogObject, FogType type)
  970. {
  971. fogObject.fogType = type;
  972. fogObject.PrepareFogObject();
  973. }
  974. public static void UpdateFogType(VolumeFog fogObject)
  975. {
  976. fogObject.UpdateRenderMesh();
  977. fogObject.UpdateScale();
  978. }
  979. public static Material SetupFogMaterial(VolumeFog fogObject)
  980. {
  981. Material mat = GetCachedMaterial(fogObject);
  982. Renderer r = fogObject.GetComponent<Renderer>();
  983. if (r.sharedMaterial != mat)
  984. {
  985. r.sharedMaterial = mat;
  986. }
  987. return mat;
  988. }
  989. private void OnDrawGizmosSelected()
  990. {
  991. Gizmos.color = Color.red;
  992. if (IsBoxShape())
  993. {
  994. Gizmos.DrawWireCube(transform.position + boundsOffset, worldSize);
  995. }
  996. else
  997. {
  998. Gizmos.DrawWireSphere(transform.position + boundsOffset, fogMax);
  999. }
  1000. }
  1001. #endif
  1002. }
  1003. }