暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ParticleGUI.cs 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. using UnityEngine;
  2. using UnityEditorInternal;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5. using UnityEngine.Rendering;
  6. using UnityEngine.Rendering.Universal;
  7. namespace UnityEditor.Rendering.Universal.ShaderGUI
  8. {
  9. /// <summary>
  10. /// Editor script for the particle material inspector.
  11. /// </summary>
  12. public static class ParticleGUI
  13. {
  14. /// <summary>
  15. /// The available color modes.
  16. /// Controls how the Particle color and the Material color blend together.
  17. /// </summary>
  18. public enum ColorMode
  19. {
  20. /// <summary>
  21. /// Use this to select multiply mode.
  22. /// </summary>
  23. Multiply,
  24. /// <summary>
  25. /// Use this to select additive mode.
  26. /// </summary>
  27. Additive,
  28. /// <summary>
  29. /// Use this to select subtractive mode.
  30. /// </summary>
  31. Subtractive,
  32. /// <summary>
  33. /// Use this to select overlay mode.
  34. /// </summary>
  35. Overlay,
  36. /// <summary>
  37. /// Use this to select color mode.
  38. /// </summary>
  39. Color,
  40. /// <summary>
  41. /// Use this to select difference mode.
  42. /// </summary>
  43. Difference
  44. }
  45. /// <summary>
  46. /// Container for the text and tooltips used to display the shader.
  47. /// </summary>
  48. public static class Styles
  49. {
  50. /// <summary>
  51. /// The text and tooltip color mode.
  52. /// </summary>
  53. public static GUIContent colorMode = EditorGUIUtility.TrTextContent("Color Mode",
  54. "Controls how the Particle color and the Material color blend together.");
  55. /// <summary>
  56. /// The text and tooltip flip-book blending.
  57. /// </summary>
  58. public static GUIContent flipbookMode = EditorGUIUtility.TrTextContent("Flip-Book Blending",
  59. "Blends the frames in a flip-book together in a smooth animation.");
  60. /// <summary>
  61. /// The text and tooltip soft particles.
  62. /// </summary>
  63. public static GUIContent softParticlesEnabled = EditorGUIUtility.TrTextContent("Soft Particles",
  64. "Makes particles fade out when they get close to intersecting with the surface of other geometry in the depth buffer.");
  65. /// <summary>
  66. /// The text and tooltip soft particles surface fade.
  67. /// </summary>
  68. public static GUIContent softParticlesFadeText = EditorGUIUtility.TrTextContent("Surface Fade");
  69. /// <summary>
  70. /// The text and tooltip soft particles near fade distance.
  71. /// </summary>
  72. public static GUIContent softParticlesNearFadeDistanceText =
  73. EditorGUIUtility.TrTextContent("Near",
  74. "The distance from the other surface where the particle is completely transparent.");
  75. /// <summary>
  76. /// The text and tooltip soft particles far fade distance.
  77. /// </summary>
  78. public static GUIContent softParticlesFarFadeDistanceText =
  79. EditorGUIUtility.TrTextContent("Far",
  80. "The distance from the other surface where the particle is completely opaque.");
  81. /// <summary>
  82. /// The text and tooltip camera fading.
  83. /// </summary>
  84. public static GUIContent cameraFadingEnabled = EditorGUIUtility.TrTextContent("Camera Fading",
  85. "Makes particles fade out when they get close to the camera.");
  86. /// <summary>
  87. /// The text and tooltip camera fading distance.
  88. /// </summary>
  89. public static GUIContent cameraFadingDistanceText = EditorGUIUtility.TrTextContent("Distance");
  90. /// <summary>
  91. /// The text and tooltip camera fading near distance.
  92. /// </summary>
  93. public static GUIContent cameraNearFadeDistanceText =
  94. EditorGUIUtility.TrTextContent("Near",
  95. "The distance from the camera where the particle is completely transparent.");
  96. /// <summary>
  97. /// The text and tooltip camera fading far distance.
  98. /// </summary>
  99. public static GUIContent cameraFarFadeDistanceText =
  100. EditorGUIUtility.TrTextContent("Far", "The distance from the camera where the particle is completely opaque.");
  101. /// <summary>
  102. /// The text and tooltip distortion.
  103. /// </summary>
  104. public static GUIContent distortionEnabled = EditorGUIUtility.TrTextContent("Distortion",
  105. "Creates a distortion effect by making particles perform refraction with the objects drawn before them.");
  106. /// <summary>
  107. /// The text and tooltip distortion strength.
  108. /// </summary>
  109. public static GUIContent distortionStrength = EditorGUIUtility.TrTextContent("Strength",
  110. "Controls how much the Particle distorts the background. ");
  111. /// <summary>
  112. /// The text and tooltip distortion blend.
  113. /// </summary>
  114. public static GUIContent distortionBlend = EditorGUIUtility.TrTextContent("Blend",
  115. "Controls how visible the distortion effect is. At 0, there’s no visible distortion. At 1, only the distortion effect is visible, not the background.");
  116. /// <summary>
  117. /// The text and tooltip for vertex streams.
  118. /// </summary>
  119. public static GUIContent VertexStreams = EditorGUIUtility.TrTextContent("Vertex Streams",
  120. "List detailing the expected layout of data sent to the shader from the particle system.");
  121. /// <summary>
  122. /// The string for position vertex stream.
  123. /// </summary>
  124. public static string streamPositionText = "Position (POSITION.xyz)";
  125. /// <summary>
  126. /// The string for normal vertex stream.
  127. /// </summary>
  128. public static string streamNormalText = "Normal (NORMAL.xyz)";
  129. /// <summary>
  130. /// The string for color vertex stream.
  131. /// </summary>
  132. public static string streamColorText = "Color (COLOR.xyzw)";
  133. /// <summary>
  134. /// The string for color instanced vertex stream.
  135. /// </summary>
  136. public static string streamColorInstancedText = "Color (INSTANCED0.xyzw)";
  137. /// <summary>
  138. /// The string for UV vertex stream.
  139. /// </summary>
  140. public static string streamUVText = "UV (TEXCOORD0.xy)";
  141. /// <summary>
  142. /// The string for UV2 vertex stream.
  143. /// </summary>
  144. public static string streamUV2Text = "UV2 (TEXCOORD0.zw)";
  145. /// <summary>
  146. /// The string for AnimBlend Texcoord1 vertex stream.
  147. /// </summary>
  148. public static string streamAnimBlendText = "AnimBlend (TEXCOORD1.x)";
  149. /// <summary>
  150. /// The string for AnimBlend Instanced1 vertex stream.
  151. /// </summary>
  152. public static string streamAnimFrameText = "AnimFrame (INSTANCED1.x)";
  153. /// <summary>
  154. /// The string for tangent vertex stream.
  155. /// </summary>
  156. public static string streamTangentText = "Tangent (TANGENT.xyzw)";
  157. /// <summary>
  158. /// The text and tooltip for the vertex stream fix now GUI.
  159. /// </summary>
  160. public static GUIContent streamApplyToAllSystemsText = EditorGUIUtility.TrTextContent("Fix Now",
  161. "Apply the vertex stream layout to all Particle Systems using this material");
  162. /// <summary>
  163. /// The string for applying custom vertex streams from material.
  164. /// </summary>
  165. public static string undoApplyCustomVertexStreams = L10n.Tr("Apply custom vertex streams from material");
  166. /// <summary>
  167. /// The vertex stream icon.
  168. /// </summary>
  169. public static GUIStyle vertexStreamIcon = new GUIStyle();
  170. }
  171. private static ReorderableList vertexStreamList;
  172. /// <summary>
  173. /// Container for the properties used in the <c>ParticleGUI</c> editor script.
  174. /// </summary>
  175. public struct ParticleProperties
  176. {
  177. // Surface Option Props
  178. /// <summary>
  179. /// The MaterialProperty for color mode.
  180. /// </summary>
  181. public MaterialProperty colorMode;
  182. // Advanced Props
  183. /// <summary>
  184. /// The MaterialProperty for flip-book blending.
  185. /// </summary>
  186. public MaterialProperty flipbookMode;
  187. /// <summary>
  188. /// The MaterialProperty for soft particles enabled.
  189. /// </summary>
  190. public MaterialProperty softParticlesEnabled;
  191. /// <summary>
  192. /// The MaterialProperty for camera fading.
  193. /// </summary>
  194. public MaterialProperty cameraFadingEnabled;
  195. /// <summary>
  196. /// The MaterialProperty for distortion enabled.
  197. /// </summary>
  198. public MaterialProperty distortionEnabled;
  199. /// <summary>
  200. /// The MaterialProperty for soft particles near fade distance.
  201. /// </summary>
  202. public MaterialProperty softParticlesNearFadeDistance;
  203. /// <summary>
  204. /// The MaterialProperty for soft particles far fade distance.
  205. /// </summary>
  206. public MaterialProperty softParticlesFarFadeDistance;
  207. /// <summary>
  208. /// The MaterialProperty for camera fading near distance.
  209. /// </summary>
  210. public MaterialProperty cameraNearFadeDistance;
  211. /// <summary>
  212. /// The MaterialProperty for camera fading far distance.
  213. /// </summary>
  214. public MaterialProperty cameraFarFadeDistance;
  215. /// <summary>
  216. /// The MaterialProperty for distortion blend.
  217. /// </summary>
  218. public MaterialProperty distortionBlend;
  219. /// <summary>
  220. /// The MaterialProperty for distortion strength.
  221. /// </summary>
  222. public MaterialProperty distortionStrength;
  223. /// <summary>
  224. /// Constructor for the <c>ParticleProperties</c> container struct.
  225. /// </summary>
  226. /// <param name="properties"></param>
  227. public ParticleProperties(MaterialProperty[] properties)
  228. {
  229. // Surface Option Props
  230. colorMode = BaseShaderGUI.FindProperty("_ColorMode", properties, false);
  231. // Advanced Props
  232. flipbookMode = BaseShaderGUI.FindProperty("_FlipbookBlending", properties);
  233. softParticlesEnabled = BaseShaderGUI.FindProperty("_SoftParticlesEnabled", properties);
  234. cameraFadingEnabled = BaseShaderGUI.FindProperty("_CameraFadingEnabled", properties);
  235. distortionEnabled = BaseShaderGUI.FindProperty("_DistortionEnabled", properties, false);
  236. softParticlesNearFadeDistance = BaseShaderGUI.FindProperty("_SoftParticlesNearFadeDistance", properties);
  237. softParticlesFarFadeDistance = BaseShaderGUI.FindProperty("_SoftParticlesFarFadeDistance", properties);
  238. cameraNearFadeDistance = BaseShaderGUI.FindProperty("_CameraNearFadeDistance", properties);
  239. cameraFarFadeDistance = BaseShaderGUI.FindProperty("_CameraFarFadeDistance", properties);
  240. distortionBlend = BaseShaderGUI.FindProperty("_DistortionBlend", properties, false);
  241. distortionStrength = BaseShaderGUI.FindProperty("_DistortionStrength", properties, false);
  242. }
  243. }
  244. /// <summary>
  245. /// Sets up the material with correct keywords based on the color mode.
  246. /// </summary>
  247. /// <param name="material">The material to use.</param>
  248. public static void SetupMaterialWithColorMode(Material material)
  249. {
  250. var colorMode = (ColorMode)material.GetFloat("_ColorMode");
  251. switch (colorMode)
  252. {
  253. case ColorMode.Multiply:
  254. material.DisableKeyword("_COLOROVERLAY_ON");
  255. material.DisableKeyword("_COLORCOLOR_ON");
  256. material.DisableKeyword("_COLORADDSUBDIFF_ON");
  257. break;
  258. case ColorMode.Overlay:
  259. material.DisableKeyword("_COLORCOLOR_ON");
  260. material.DisableKeyword("_COLORADDSUBDIFF_ON");
  261. material.EnableKeyword("_COLOROVERLAY_ON");
  262. break;
  263. case ColorMode.Color:
  264. material.DisableKeyword("_COLOROVERLAY_ON");
  265. material.DisableKeyword("_COLORADDSUBDIFF_ON");
  266. material.EnableKeyword("_COLORCOLOR_ON");
  267. break;
  268. case ColorMode.Difference:
  269. material.DisableKeyword("_COLOROVERLAY_ON");
  270. material.DisableKeyword("_COLORCOLOR_ON");
  271. material.EnableKeyword("_COLORADDSUBDIFF_ON");
  272. material.SetVector("_BaseColorAddSubDiff", new Vector4(-1.0f, 1.0f, 0.0f, 0.0f));
  273. break;
  274. case ColorMode.Additive:
  275. material.DisableKeyword("_COLOROVERLAY_ON");
  276. material.DisableKeyword("_COLORCOLOR_ON");
  277. material.EnableKeyword("_COLORADDSUBDIFF_ON");
  278. material.SetVector("_BaseColorAddSubDiff", new Vector4(1.0f, 0.0f, 0.0f, 0.0f));
  279. break;
  280. case ColorMode.Subtractive:
  281. material.DisableKeyword("_COLOROVERLAY_ON");
  282. material.DisableKeyword("_COLORCOLOR_ON");
  283. material.EnableKeyword("_COLORADDSUBDIFF_ON");
  284. material.SetVector("_BaseColorAddSubDiff", new Vector4(-1.0f, 0.0f, 0.0f, 0.0f));
  285. break;
  286. }
  287. }
  288. /// <summary>
  289. /// Draws the fading options GUI.
  290. /// </summary>
  291. /// <param name="material">The material to use.</param>
  292. /// <param name="materialEditor">The material editor to use.</param>
  293. /// <param name="properties">The particle properties to use.</param>
  294. public static void FadingOptions(Material material, MaterialEditor materialEditor, ParticleProperties properties)
  295. {
  296. // Z write doesn't work with fading
  297. bool hasZWrite = (material.GetFloat("_ZWrite") > 0.0f);
  298. if (!hasZWrite)
  299. {
  300. // Soft Particles
  301. {
  302. materialEditor.ShaderProperty(properties.softParticlesEnabled, Styles.softParticlesEnabled);
  303. if (properties.softParticlesEnabled.floatValue >= 0.5f)
  304. {
  305. UniversalRenderPipelineAsset urpAsset = UniversalRenderPipeline.asset;
  306. if (urpAsset != null && !urpAsset.supportsCameraDepthTexture)
  307. {
  308. GUIStyle warnStyle = new GUIStyle(GUI.skin.label);
  309. warnStyle.fontStyle = FontStyle.BoldAndItalic;
  310. warnStyle.wordWrap = true;
  311. EditorGUILayout.HelpBox("Soft Particles require depth texture. Please enable \"Depth Texture\" in the Universal Render Pipeline settings.", MessageType.Warning);
  312. }
  313. EditorGUI.indentLevel++;
  314. BaseShaderGUI.TwoFloatSingleLine(Styles.softParticlesFadeText,
  315. properties.softParticlesNearFadeDistance,
  316. Styles.softParticlesNearFadeDistanceText,
  317. properties.softParticlesFarFadeDistance,
  318. Styles.softParticlesFarFadeDistanceText,
  319. materialEditor);
  320. EditorGUI.indentLevel--;
  321. }
  322. }
  323. // Camera Fading
  324. {
  325. materialEditor.ShaderProperty(properties.cameraFadingEnabled, Styles.cameraFadingEnabled);
  326. if (properties.cameraFadingEnabled.floatValue >= 0.5f)
  327. {
  328. EditorGUI.indentLevel++;
  329. BaseShaderGUI.TwoFloatSingleLine(Styles.cameraFadingDistanceText,
  330. properties.cameraNearFadeDistance,
  331. Styles.cameraNearFadeDistanceText,
  332. properties.cameraFarFadeDistance,
  333. Styles.cameraFarFadeDistanceText,
  334. materialEditor);
  335. EditorGUI.indentLevel--;
  336. }
  337. }
  338. // Distortion
  339. if (properties.distortionEnabled != null)
  340. {
  341. materialEditor.ShaderProperty(properties.distortionEnabled, Styles.distortionEnabled);
  342. if (properties.distortionEnabled.floatValue >= 0.5f)
  343. {
  344. EditorGUI.indentLevel++;
  345. materialEditor.ShaderProperty(properties.distortionStrength, Styles.distortionStrength);
  346. materialEditor.ShaderProperty(properties.distortionBlend, Styles.distortionBlend);
  347. EditorGUI.indentLevel--;
  348. }
  349. }
  350. EditorGUI.showMixedValue = false;
  351. }
  352. }
  353. /// <summary>
  354. /// Draws the vertex streams area.
  355. /// </summary>
  356. /// <param name="material">The material to use.</param>
  357. /// <param name="renderers">List of particle system renderers.</param>
  358. /// <param name="useLighting">Marks whether the renderers uses lighting or not.</param>
  359. public static void DoVertexStreamsArea(Material material, List<ParticleSystemRenderer> renderers, bool useLighting = false)
  360. {
  361. EditorGUILayout.Space();
  362. // Display list of streams required to make this shader work
  363. bool useNormalMap = false;
  364. bool useFlipbookBlending = (material.GetFloat("_FlipbookBlending") > 0.0f);
  365. if (material.HasProperty("_BumpMap"))
  366. useNormalMap = material.GetTexture("_BumpMap");
  367. bool useGPUInstancing = ShaderUtil.HasProceduralInstancing(material.shader);
  368. if (useGPUInstancing && renderers.Count > 0)
  369. {
  370. if (!renderers[0].enableGPUInstancing || renderers[0].renderMode != ParticleSystemRenderMode.Mesh)
  371. useGPUInstancing = false;
  372. }
  373. // Build the list of expected vertex streams
  374. List<ParticleSystemVertexStream> streams = new List<ParticleSystemVertexStream>();
  375. List<string> streamList = new List<string>();
  376. streams.Add(ParticleSystemVertexStream.Position);
  377. streamList.Add(Styles.streamPositionText);
  378. if (useLighting || useNormalMap)
  379. {
  380. streams.Add(ParticleSystemVertexStream.Normal);
  381. streamList.Add(Styles.streamNormalText);
  382. if (useNormalMap)
  383. {
  384. streams.Add(ParticleSystemVertexStream.Tangent);
  385. streamList.Add(Styles.streamTangentText);
  386. }
  387. }
  388. streams.Add(ParticleSystemVertexStream.Color);
  389. streamList.Add(useGPUInstancing ? Styles.streamColorInstancedText : Styles.streamColorText);
  390. streams.Add(ParticleSystemVertexStream.UV);
  391. streamList.Add(Styles.streamUVText);
  392. List<ParticleSystemVertexStream> instancedStreams = new List<ParticleSystemVertexStream>(streams);
  393. if (useGPUInstancing)
  394. {
  395. instancedStreams.Add(ParticleSystemVertexStream.AnimFrame);
  396. streamList.Add(Styles.streamAnimFrameText);
  397. }
  398. else if (useFlipbookBlending && !useGPUInstancing)
  399. {
  400. streams.Add(ParticleSystemVertexStream.UV2);
  401. streamList.Add(Styles.streamUV2Text);
  402. streams.Add(ParticleSystemVertexStream.AnimBlend);
  403. streamList.Add(Styles.streamAnimBlendText);
  404. }
  405. vertexStreamList = new ReorderableList(streamList, typeof(string), false, true, false, false);
  406. vertexStreamList.drawHeaderCallback = (Rect rect) =>
  407. {
  408. EditorGUI.LabelField(rect, Styles.VertexStreams);
  409. };
  410. vertexStreamList.DoLayoutList();
  411. // Display a warning if any renderers have incorrect vertex streams
  412. string Warnings = "";
  413. List<ParticleSystemVertexStream> rendererStreams = new List<ParticleSystemVertexStream>();
  414. foreach (ParticleSystemRenderer renderer in renderers)
  415. {
  416. renderer.GetActiveVertexStreams(rendererStreams);
  417. bool streamsValid;
  418. if (useGPUInstancing && renderer.renderMode == ParticleSystemRenderMode.Mesh && renderer.supportsMeshInstancing)
  419. streamsValid = CompareVertexStreams(rendererStreams, instancedStreams);
  420. else
  421. streamsValid = CompareVertexStreams(rendererStreams, streams);
  422. if (!streamsValid)
  423. Warnings += "-" + renderer.name + "\n";
  424. }
  425. if (!string.IsNullOrEmpty(Warnings))
  426. {
  427. EditorGUILayout.HelpBox(
  428. "The following Particle System Renderers are using this material with incorrect Vertex Streams:\n" +
  429. Warnings, MessageType.Error, true);
  430. // Set the streams on all systems using this material
  431. if (GUILayout.Button(Styles.streamApplyToAllSystemsText, EditorStyles.miniButton, GUILayout.ExpandWidth(true)))
  432. {
  433. Undo.RecordObjects(renderers.Where(r => r != null).ToArray(), Styles.undoApplyCustomVertexStreams);
  434. foreach (ParticleSystemRenderer renderer in renderers)
  435. {
  436. if (useGPUInstancing && renderer.renderMode == ParticleSystemRenderMode.Mesh && renderer.supportsMeshInstancing)
  437. renderer.SetActiveVertexStreams(instancedStreams);
  438. else
  439. renderer.SetActiveVertexStreams(streams);
  440. }
  441. }
  442. }
  443. }
  444. private static bool CompareVertexStreams(IEnumerable<ParticleSystemVertexStream> a, IEnumerable<ParticleSystemVertexStream> b)
  445. {
  446. var differenceA = a.Except(b);
  447. var differenceB = b.Except(a);
  448. var difference = differenceA.Union(differenceB).Distinct();
  449. if (!difference.Any())
  450. return true;
  451. // If normals are the only difference, ignore them, because the default particle streams include normals, to make it easy for users to switch between lit and unlit
  452. if (difference.Count() == 1)
  453. {
  454. if (difference.First() == ParticleSystemVertexStream.Normal)
  455. return true;
  456. }
  457. return false;
  458. }
  459. /// <summary>
  460. /// Sets up the keywords for the material and shader.
  461. /// </summary>
  462. /// <param name="material">The material to use.</param>
  463. public static void SetMaterialKeywords(Material material)
  464. {
  465. // Setup particle + material color blending
  466. SetupMaterialWithColorMode(material);
  467. // Is the material transparent, this is set in BaseShaderGUI
  468. bool isTransparent = material.GetTag("RenderType", false) == "Transparent";
  469. // Z write doesn't work with distortion/fading
  470. bool hasZWrite = (material.GetFloat("_ZWrite") > 0.0f);
  471. // Flipbook blending
  472. if (material.HasProperty("_FlipbookBlending"))
  473. {
  474. var useFlipbookBlending = (material.GetFloat("_FlipbookBlending") > 0.0f);
  475. CoreUtils.SetKeyword(material, "_FLIPBOOKBLENDING_ON", useFlipbookBlending);
  476. }
  477. // Soft particles
  478. var useSoftParticles = false;
  479. if (material.HasProperty("_SoftParticlesEnabled"))
  480. {
  481. useSoftParticles = (material.GetFloat("_SoftParticlesEnabled") > 0.0f && isTransparent);
  482. if (useSoftParticles)
  483. {
  484. var softParticlesNearFadeDistance = material.GetFloat("_SoftParticlesNearFadeDistance");
  485. var softParticlesFarFadeDistance = material.GetFloat("_SoftParticlesFarFadeDistance");
  486. // clamp values
  487. if (softParticlesNearFadeDistance < 0.0f)
  488. {
  489. softParticlesNearFadeDistance = 0.0f;
  490. material.SetFloat("_SoftParticlesNearFadeDistance", 0.0f);
  491. }
  492. if (softParticlesFarFadeDistance < 0.0f)
  493. {
  494. softParticlesFarFadeDistance = 0.0f;
  495. material.SetFloat("_SoftParticlesFarFadeDistance", 0.0f);
  496. }
  497. // set keywords
  498. material.SetVector("_SoftParticleFadeParams",
  499. new Vector4(softParticlesNearFadeDistance,
  500. 1.0f / (softParticlesFarFadeDistance - softParticlesNearFadeDistance), 0.0f, 0.0f));
  501. }
  502. else
  503. {
  504. material.SetVector("_SoftParticleFadeParams", new Vector4(0.0f, 0.0f, 0.0f, 0.0f));
  505. }
  506. CoreUtils.SetKeyword(material, "_SOFTPARTICLES_ON", useSoftParticles);
  507. }
  508. // Camera fading
  509. var useCameraFading = false;
  510. if (material.HasProperty("_CameraFadingEnabled") && isTransparent)
  511. {
  512. useCameraFading = (material.GetFloat("_CameraFadingEnabled") > 0.0f);
  513. if (useCameraFading)
  514. {
  515. var cameraNearFadeDistance = material.GetFloat("_CameraNearFadeDistance");
  516. var cameraFarFadeDistance = material.GetFloat("_CameraFarFadeDistance");
  517. // clamp values
  518. if (cameraNearFadeDistance < 0.0f)
  519. {
  520. cameraNearFadeDistance = 0.0f;
  521. material.SetFloat("_CameraNearFadeDistance", 0.0f);
  522. }
  523. if (cameraFarFadeDistance < 0.0f)
  524. {
  525. cameraFarFadeDistance = 0.0f;
  526. material.SetFloat("_CameraFarFadeDistance", 0.0f);
  527. }
  528. // set keywords
  529. material.SetVector("_CameraFadeParams",
  530. new Vector4(cameraNearFadeDistance, 1.0f / (cameraFarFadeDistance - cameraNearFadeDistance),
  531. 0.0f, 0.0f));
  532. }
  533. else
  534. {
  535. material.SetVector("_CameraFadeParams", new Vector4(0.0f, Mathf.Infinity, 0.0f, 0.0f));
  536. }
  537. }
  538. // Distortion
  539. if (material.HasProperty("_DistortionEnabled"))
  540. {
  541. var useDistortion = (material.GetFloat("_DistortionEnabled") > 0.0f) && isTransparent;
  542. CoreUtils.SetKeyword(material, "_DISTORTION_ON", useDistortion);
  543. if (useDistortion)
  544. material.SetFloat("_DistortionStrengthScaled", material.GetFloat("_DistortionStrength") * 0.1f);
  545. }
  546. var useFading = (useSoftParticles || useCameraFading) && !hasZWrite;
  547. CoreUtils.SetKeyword(material, "_FADING_ON", useFading);
  548. }
  549. }
  550. } // namespace UnityEditor