StandardSurface.cs 133 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEditor;
  7. using UnityEditorInternal;
  8. namespace AmplifyShaderEditor
  9. {
  10. public enum VertexMode
  11. {
  12. Relative,
  13. Absolute
  14. }
  15. public enum RenderPath
  16. {
  17. All,
  18. ForwardOnly,
  19. DeferredOnly
  20. }
  21. public enum StandardShaderLightModel
  22. {
  23. Standard,
  24. StandardSpecular,
  25. Lambert,
  26. BlinnPhong,
  27. Unlit,
  28. CustomLighting
  29. }
  30. public enum CullMode
  31. {
  32. Back,
  33. Front,
  34. Off
  35. }
  36. public enum AlphaMode
  37. {
  38. Opaque = 0,
  39. Masked = 1,
  40. Transparent = 2, // Transparent (alpha:fade)
  41. Translucent = 3,
  42. Premultiply = 4, // Alpha Premul (alpha:premul)
  43. Custom = 5,
  44. }
  45. public enum DisableBatching
  46. {
  47. True,
  48. False,
  49. LODFading
  50. }
  51. public enum RenderType
  52. {
  53. Opaque,
  54. Transparent,
  55. TransparentCutout,
  56. Background,
  57. Overlay,
  58. TreeOpaque,
  59. TreeTransparentCutout,
  60. TreeBillboard,
  61. Grass,
  62. GrassBillboard,
  63. Custom
  64. }
  65. public enum RenderQueue
  66. {
  67. Background,
  68. Geometry,
  69. AlphaTest,
  70. Transparent,
  71. Overlay
  72. }
  73. [Serializable]
  74. public class NodeCache
  75. {
  76. public int TargetNodeId = -1;
  77. public int TargetPortId = -1;
  78. public NodeCache( int targetNodeId, int targetPortId )
  79. {
  80. SetData( targetNodeId, targetPortId );
  81. }
  82. public void SetData( int targetNodeId, int targetPortId )
  83. {
  84. TargetNodeId = targetNodeId;
  85. TargetPortId = targetPortId;
  86. }
  87. public void Invalidate()
  88. {
  89. TargetNodeId = -1;
  90. TargetPortId = -1;
  91. }
  92. public bool IsValid
  93. {
  94. get { return ( TargetNodeId >= 0 ); }
  95. }
  96. public override string ToString()
  97. {
  98. return "TargetNodeId " + TargetNodeId + " TargetPortId " + TargetPortId;
  99. }
  100. }
  101. [Serializable]
  102. public class CacheNodeConnections
  103. {
  104. public Dictionary<string, List<NodeCache>> NodeCacheArray;
  105. public CacheNodeConnections()
  106. {
  107. NodeCacheArray = new Dictionary<string, List<NodeCache>>();
  108. }
  109. public void Add( string key, NodeCache value )
  110. {
  111. if( NodeCacheArray.ContainsKey( key ) )
  112. {
  113. NodeCacheArray[ key ].Add( value );
  114. }
  115. else
  116. {
  117. NodeCacheArray.Add( key, new List<NodeCache>() );
  118. NodeCacheArray[ key ].Add( value );
  119. }
  120. }
  121. public NodeCache Get( string key, int idx = 0 )
  122. {
  123. if( NodeCacheArray.ContainsKey( key ) )
  124. {
  125. if( idx < NodeCacheArray[ key ].Count )
  126. return NodeCacheArray[ key ][ idx ];
  127. }
  128. return null;
  129. }
  130. public List<NodeCache> GetList( string key )
  131. {
  132. if( NodeCacheArray.ContainsKey( key ) )
  133. {
  134. return NodeCacheArray[ key ];
  135. }
  136. return null;
  137. }
  138. public void Clear()
  139. {
  140. foreach( KeyValuePair<string, List<NodeCache>> kvp in NodeCacheArray )
  141. {
  142. kvp.Value.Clear();
  143. }
  144. NodeCacheArray.Clear();
  145. }
  146. }
  147. [Serializable]
  148. [NodeAttributes( "Standard Surface Output", "Master", "Surface shader generator output", null, KeyCode.None, false )]
  149. public sealed class StandardSurfaceOutputNode : MasterNode, ISerializationCallbackReceiver
  150. {
  151. private readonly static string[] VertexLitFunc = { "\t\tinline half4 LightingUnlit( SurfaceOutput s, half3 lightDir, half atten )",
  152. "\t\t{",
  153. "\t\t\treturn half4 ( 0, 0, 0, s.Alpha );",
  154. "\t\t}\n"};
  155. private readonly static string[] FadeModeOptions = { "Opaque", "Masked", "Transparent", "Translucent", "Alpha Premultipled", "Custom" };
  156. private const string VertexModeStr = "Vertex Output";
  157. private readonly static GUIContent RenderPathContent = new GUIContent( "Render Path", "Selects and generates passes for the supported rendering paths\nDefault: All" );
  158. private const string ShaderModelStr = "Shader Model";
  159. private readonly static GUIContent LightModelContent = new GUIContent( "Light Model", "Surface shader lighting model defines how the surface reflects light\nDefault: Standard" );
  160. private readonly static GUIContent ShaderLODContent = new GUIContent( "Shader LOD", "Shader LOD" );
  161. private readonly static GUIContent CullModeContent = new GUIContent( "Cull Mode", "Polygon culling mode prevents rendering of either back-facing or front-facing polygons to save performance, turn it off if you want to render both sides\nDefault: Back" );
  162. private const string ChromaticAberrationStr = "Chromatic Aberration";
  163. private const string DiscardStr = "Opacity Mask";
  164. private const string VertexDisplacementStr = "Local Vertex Offset";
  165. private const string VertexPositionStr = "Local Vertex Position";
  166. private const string VertexDataStr = "VertexData";
  167. private const string VertexNormalStr = "Local Vertex Normal";
  168. private const string VertexTangentStr = "Local Vertex Tangent";
  169. private const string CustomLightingStr = "Custom Lighting";
  170. private const string AlbedoStr = "Albedo";
  171. private const string NormalStr = "Normal";
  172. private const string EmissionStr = "Emission";
  173. private const string MetallicStr = "Metallic";
  174. private const string SmoothnessStr = "Smoothness";
  175. private const string OcclusionDataStr = "Occlusion";
  176. private const string OcclusionLabelStr = "Ambient Occlusion";
  177. private const string TransmissionStr = "Transmission";
  178. private const string TranslucencyStr = "Translucency";
  179. private const string RefractionStr = "Refraction";
  180. private const string AlphaStr = "Opacity";
  181. private const string AlphaDataStr = "Alpha";
  182. private const string DebugStr = "Debug";
  183. private const string SpecularStr = "Specular";
  184. private const string GlossStr = "Gloss";
  185. private const string CustomRenderTypeStr = "Custom Type";
  186. private readonly static GUIContent AlphaModeContent = new GUIContent( " Blend Mode", "Defines how the surface blends with the background\nDefault: Opaque" );
  187. private const string OpacityMaskClipValueStr = "Mask Clip Value";
  188. private readonly static GUIContent OpacityMaskClipValueContent = new GUIContent( "Mask Clip Value", "Default clip value to be compared with opacity alpha ( 0 = fully Opaque, 1 = fully Masked )\nDefault: 0.5" );
  189. private readonly static GUIContent CastShadowsContent = new GUIContent( "Cast Shadows", "Generates a shadow caster pass for vertex modifications and point lights in forward rendering\nDefault: ON" );
  190. private readonly static GUIContent ReceiveShadowsContent = new GUIContent( "Receive Shadows", "Untick it to disable shadow receiving, this includes self-shadowing (only for forward rendering) \nDefault: ON" );
  191. private readonly static GUIContent QueueIndexContent = new GUIContent( "Queue Index", "Value to offset the render queue, accepts both positive values to render later and negative values to render sooner\nDefault: 0" );
  192. private readonly static GUIContent RefractionLayerStr = new GUIContent( "Refraction Layer", "Use it to group or ungroup different refraction shaders into the same or different grabpass (only for forward rendering) \nDefault: 0" );
  193. private readonly static GUIContent AlphaToCoverageStr = new GUIContent( "Alpha To Coverage", "" );
  194. private readonly static GUIContent RenderQueueContent = new GUIContent( "Render Queue", "Base rendering queue index\n(Background = 1000, Geometry = 2000, AlphaTest = 2450, Transparent = 3000, Overlay = 4000)\nDefault: Geometry" );
  195. private readonly static GUIContent RenderTypeContent = new GUIContent( "Render Type", "Categorizes shaders into several predefined groups, usually to be used with screen shader effects\nDefault: Opaque" );
  196. private const string ShaderInputOrderStr = "Shader Input Order";
  197. [SerializeField]
  198. private BlendOpsHelper m_blendOpsHelper = new BlendOpsHelper();
  199. [SerializeField]
  200. private StencilBufferOpHelper m_stencilBufferHelper = new StencilBufferOpHelper();
  201. [SerializeField]
  202. private ZBufferOpHelper m_zBufferHelper = new ZBufferOpHelper();
  203. [SerializeField]
  204. private OutlineOpHelper m_outlineHelper = new OutlineOpHelper();
  205. [SerializeField]
  206. private TessellationOpHelper m_tessOpHelper = new TessellationOpHelper();
  207. [SerializeField]
  208. private ColorMaskHelper m_colorMaskHelper = new ColorMaskHelper();
  209. [SerializeField]
  210. private RenderingPlatformOpHelper m_renderingPlatformOpHelper = new RenderingPlatformOpHelper();
  211. [SerializeField]
  212. private RenderingOptionsOpHelper m_renderingOptionsOpHelper = new RenderingOptionsOpHelper();
  213. [SerializeField]
  214. private BillboardOpHelper m_billboardOpHelper = new BillboardOpHelper();
  215. [SerializeField]
  216. private FallbackPickerHelper m_fallbackHelper = null;
  217. [SerializeField]
  218. private TerrainDrawInstancedHelper m_drawInstancedHelper = new TerrainDrawInstancedHelper();
  219. //legacy
  220. [SerializeField]
  221. private AdditionalIncludesHelper m_additionalIncludes = new AdditionalIncludesHelper();
  222. //legacy
  223. [SerializeField]
  224. private AdditionalPragmasHelper m_additionalPragmas = new AdditionalPragmasHelper();
  225. //legacy
  226. [SerializeField]
  227. private AdditionalDefinesHelper m_additionalDefines = new AdditionalDefinesHelper();
  228. [SerializeField]
  229. private TemplateAdditionalDirectivesHelper m_additionalDirectives = new TemplateAdditionalDirectivesHelper( " Additional Directives" );
  230. [SerializeField]
  231. private AdditionalSurfaceOptionsHelper m_additionalSurfaceOptions = new AdditionalSurfaceOptionsHelper();
  232. [SerializeField]
  233. private UsePassHelper m_usePass;
  234. [SerializeField]
  235. private CustomTagsHelper m_customTagsHelper = new CustomTagsHelper();
  236. [SerializeField]
  237. private DependenciesHelper m_dependenciesHelper = new DependenciesHelper();
  238. [SerializeField]
  239. private StandardShaderLightModel m_currentLightModel;
  240. [SerializeField]
  241. private StandardShaderLightModel m_lastLightModel;
  242. [SerializeField]
  243. private CullMode m_cullMode = CullMode.Back;
  244. [SerializeField]
  245. private InlineProperty m_inlineCullMode = new InlineProperty();
  246. [SerializeField]
  247. private InlineProperty m_inlineChromaticAberration = new InlineProperty(0.1f);
  248. [SerializeField]
  249. private AlphaMode m_alphaMode = AlphaMode.Opaque;
  250. [SerializeField]
  251. private RenderType m_renderType = RenderType.Opaque;
  252. [SerializeField]
  253. private string m_customRenderType = string.Empty;
  254. [SerializeField]
  255. private RenderQueue m_renderQueue = RenderQueue.Geometry;
  256. [SerializeField]
  257. private RenderPath m_renderPath = RenderPath.All;
  258. [SerializeField]
  259. private VertexMode m_vertexMode = VertexMode.Relative;
  260. [SerializeField]
  261. private bool m_customBlendMode = false;
  262. [SerializeField]
  263. private float m_opacityMaskClipValue = 0.5f;
  264. [SerializeField]
  265. private InlineProperty m_inlineOpacityMaskClipValue = new InlineProperty();
  266. [SerializeField]
  267. private InlineProperty m_inlineAlphaToCoverage = new InlineProperty();
  268. [SerializeField]
  269. private int m_customLightingPortId = -1;
  270. [SerializeField]
  271. private int m_emissionPortId = -1;
  272. [SerializeField]
  273. private int m_discardPortId = -1;
  274. [SerializeField]
  275. private int m_opacityPortId = -1;
  276. [SerializeField]
  277. private int m_vertexPortId = -1;
  278. [SerializeField]
  279. private bool m_keepAlpha = true;
  280. [SerializeField]
  281. private bool m_castShadows = true;
  282. //[SerializeField]
  283. private bool m_customShadowCaster = false;
  284. [SerializeField]
  285. private bool m_receiveShadows = true;
  286. [SerializeField]
  287. private int m_queueOrder = 0;
  288. [SerializeField]
  289. private int m_grabOrder = 0;
  290. [SerializeField]
  291. private bool m_alphaToCoverage = false;
  292. private InputPort m_transmissionPort;
  293. private InputPort m_translucencyPort;
  294. private InputPort m_tessellationPort;
  295. private bool m_previousTranslucencyOn = false;
  296. private bool m_previousRefractionOn = false;
  297. [SerializeField]
  298. private CacheNodeConnections m_cacheNodeConnections = new CacheNodeConnections();
  299. private bool m_usingProSkin = false;
  300. private GUIStyle m_inspectorFoldoutStyle;
  301. private GUIStyle m_inspectorToolbarStyle;
  302. private GUIStyle m_inspectorTooldropdownStyle;
  303. private bool m_customBlendAvailable = false;
  304. private Color m_cachedColor = Color.white;
  305. private float m_titleOpacity = 0.5f;
  306. private float m_boxOpacity = 0.5f;
  307. private InputPort m_refractionPort;
  308. private InputPort m_normalPort;
  309. private GUIStyle m_inspectorDefaultStyle;
  310. [SerializeField]
  311. private ReordenatorNode m_specColorReorder = null;
  312. [SerializeField]
  313. private int m_specColorOrderIndex = -1;
  314. [SerializeField]
  315. private ReordenatorNode m_maskClipReorder = null;
  316. [SerializeField]
  317. private int m_maskClipOrderIndex = -1;
  318. [SerializeField]
  319. private ReordenatorNode m_translucencyReorder = null;
  320. [SerializeField]
  321. private int m_translucencyOrderIndex = -1;
  322. [SerializeField]
  323. private ReordenatorNode m_refractionReorder = null;
  324. [SerializeField]
  325. private int m_refractionOrderIndex = -1;
  326. [SerializeField]
  327. private ReordenatorNode m_tessellationReorder = null;
  328. [SerializeField]
  329. private int m_tessellationOrderIndex = -1;
  330. private bool m_previousTessellationOn = false;
  331. private bool m_initialize = true;
  332. private bool m_checkChanges = true;
  333. private bool m_lightModelChanged = true;
  334. private PropertyNode m_dummyProperty = null;
  335. protected override void CommonInit( int uniqueId )
  336. {
  337. m_currentLightModel = m_lastLightModel = StandardShaderLightModel.Standard;
  338. m_textLabelWidth = 120;
  339. m_autoDrawInternalPortData = false;
  340. base.CommonInit( uniqueId );
  341. m_zBufferHelper.ParentSurface = this;
  342. m_tessOpHelper.ParentSurface = this;
  343. m_customPrecision = true;
  344. }
  345. public override void OnEnable()
  346. {
  347. base.OnEnable();
  348. if( m_usePass == null )
  349. {
  350. m_usePass = ScriptableObject.CreateInstance<UsePassHelper>();
  351. m_usePass.Init( " Additional Use Passes" );
  352. }
  353. if( m_fallbackHelper == null )
  354. {
  355. m_fallbackHelper = ScriptableObject.CreateInstance<FallbackPickerHelper>();
  356. m_fallbackHelper.Init();
  357. }
  358. }
  359. public override void AddMasterPorts()
  360. {
  361. int vertexCorrection = 3;
  362. int index = vertexCorrection + 2;
  363. base.AddMasterPorts();
  364. switch( m_currentLightModel )
  365. {
  366. case StandardShaderLightModel.Standard:
  367. {
  368. AddInputPort( WirePortDataType.FLOAT3, false, AlbedoStr, vertexCorrection + 1, MasterNodePortCategory.Fragment, 0 );
  369. AddInputPort( WirePortDataType.FLOAT3, false, NormalStr, vertexCorrection + 0, MasterNodePortCategory.Fragment, 1 );
  370. m_normalPort = m_inputPorts[ m_inputPorts.Count - 1 ];
  371. AddInputPort( WirePortDataType.FLOAT3, false, EmissionStr, index++, MasterNodePortCategory.Fragment, 2 );
  372. AddInputPort( WirePortDataType.FLOAT, false, MetallicStr, index++, MasterNodePortCategory.Fragment, 3 );
  373. AddInputPort( WirePortDataType.FLOAT, false, SmoothnessStr, index++, MasterNodePortCategory.Fragment, 4 );
  374. AddInputPort( WirePortDataType.FLOAT, false, OcclusionLabelStr, OcclusionDataStr, index++, MasterNodePortCategory.Fragment, 5 );
  375. }
  376. break;
  377. case StandardShaderLightModel.StandardSpecular:
  378. {
  379. AddInputPort( WirePortDataType.FLOAT3, false, AlbedoStr, vertexCorrection + 1, MasterNodePortCategory.Fragment, 0 );
  380. AddInputPort( WirePortDataType.FLOAT3, false, NormalStr, vertexCorrection + 0, MasterNodePortCategory.Fragment, 1 );
  381. m_normalPort = m_inputPorts[ m_inputPorts.Count - 1 ];
  382. AddInputPort( WirePortDataType.FLOAT3, false, EmissionStr, index++, MasterNodePortCategory.Fragment, 2 );
  383. AddInputPort( WirePortDataType.FLOAT3, false, SpecularStr, index++, MasterNodePortCategory.Fragment, 3 );
  384. AddInputPort( WirePortDataType.FLOAT, false, SmoothnessStr, index++, MasterNodePortCategory.Fragment, 4 );
  385. AddInputPort( WirePortDataType.FLOAT, false, OcclusionLabelStr, OcclusionDataStr, index++, MasterNodePortCategory.Fragment, 5 );
  386. }
  387. break;
  388. case StandardShaderLightModel.CustomLighting:
  389. {
  390. AddInputPort( WirePortDataType.FLOAT3, false, AlbedoStr, vertexCorrection + 1, MasterNodePortCategory.Fragment, 0 );
  391. AddInputPort( WirePortDataType.FLOAT3, false, NormalStr, vertexCorrection + 0, MasterNodePortCategory.Fragment, 1 );
  392. m_normalPort = m_inputPorts[ m_inputPorts.Count - 1 ];
  393. m_inputPorts[ m_inputPorts.Count - 1 ].Locked = true;
  394. AddInputPort( WirePortDataType.FLOAT3, false, EmissionStr, index++, MasterNodePortCategory.Fragment, 2 );
  395. AddInputPort( WirePortDataType.FLOAT, false, SpecularStr, index++, MasterNodePortCategory.Fragment, 3 );
  396. m_inputPorts[ m_inputPorts.Count - 1 ].Locked = true;
  397. AddInputPort( WirePortDataType.FLOAT, false, GlossStr, index++, MasterNodePortCategory.Fragment, 4 );
  398. m_inputPorts[ m_inputPorts.Count - 1 ].Locked = true;
  399. }
  400. break;
  401. case StandardShaderLightModel.Unlit:
  402. {
  403. AddInputPort( WirePortDataType.FLOAT3, false, AlbedoStr, vertexCorrection + 1, MasterNodePortCategory.Fragment, 0 );
  404. m_inputPorts[ m_inputPorts.Count - 1 ].Locked = true;
  405. AddInputPort( WirePortDataType.FLOAT3, false, NormalStr, vertexCorrection + 0, MasterNodePortCategory.Fragment, 1 );
  406. m_normalPort = m_inputPorts[ m_inputPorts.Count - 1 ];
  407. m_inputPorts[ m_inputPorts.Count - 1 ].Locked = true;
  408. AddInputPort( WirePortDataType.FLOAT3, false, EmissionStr, index++, MasterNodePortCategory.Fragment, 2 );
  409. AddInputPort( WirePortDataType.FLOAT, false, SpecularStr, index++, MasterNodePortCategory.Fragment, 3 );
  410. m_inputPorts[ m_inputPorts.Count - 1 ].Locked = true;
  411. AddInputPort( WirePortDataType.FLOAT, false, GlossStr, index++, MasterNodePortCategory.Fragment, 4 );
  412. m_inputPorts[ m_inputPorts.Count - 1 ].Locked = true;
  413. }
  414. break;
  415. case StandardShaderLightModel.Lambert:
  416. {
  417. AddInputPort( WirePortDataType.FLOAT3, false, AlbedoStr, vertexCorrection + 1, MasterNodePortCategory.Fragment, 0 );
  418. AddInputPort( WirePortDataType.FLOAT3, false, NormalStr, vertexCorrection + 0, MasterNodePortCategory.Fragment, 1 );
  419. m_normalPort = m_inputPorts[ m_inputPorts.Count - 1 ];
  420. AddInputPort( WirePortDataType.FLOAT3, false, EmissionStr, index++, MasterNodePortCategory.Fragment, 2 );
  421. AddInputPort( WirePortDataType.FLOAT, false, SpecularStr, index++, MasterNodePortCategory.Fragment, 3 );
  422. AddInputPort( WirePortDataType.FLOAT, false, GlossStr, index++, MasterNodePortCategory.Fragment, 4 );
  423. }
  424. break;
  425. case StandardShaderLightModel.BlinnPhong:
  426. {
  427. AddInputPort( WirePortDataType.FLOAT3, false, AlbedoStr, vertexCorrection + 1, MasterNodePortCategory.Fragment, 0 );
  428. AddInputPort( WirePortDataType.FLOAT3, false, NormalStr, vertexCorrection + 0, MasterNodePortCategory.Fragment, 1 );
  429. m_normalPort = m_inputPorts[ m_inputPorts.Count - 1 ];
  430. AddInputPort( WirePortDataType.FLOAT3, false, EmissionStr, index++, MasterNodePortCategory.Fragment, 2 );
  431. AddInputPort( WirePortDataType.FLOAT, false, SpecularStr, index++, MasterNodePortCategory.Fragment, 3 );
  432. AddInputPort( WirePortDataType.FLOAT, false, GlossStr, index++, MasterNodePortCategory.Fragment, 4 );
  433. }
  434. break;
  435. }
  436. // instead of setting in the switch emission port is always at position 2;
  437. m_emissionPortId = 2;
  438. AddInputPort( WirePortDataType.FLOAT3, false, TransmissionStr, index++, MasterNodePortCategory.Fragment, 6 );
  439. m_transmissionPort = m_inputPorts[ m_inputPorts.Count - 1 ];
  440. m_inputPorts[ m_inputPorts.Count - 1 ].Locked = ( m_currentLightModel == StandardShaderLightModel.Standard ) || ( m_currentLightModel == StandardShaderLightModel.StandardSpecular ) ? false : true;
  441. AddInputPort( WirePortDataType.FLOAT3, false, TranslucencyStr, index++, MasterNodePortCategory.Fragment, 7 );
  442. m_translucencyPort = m_inputPorts[ m_inputPorts.Count - 1 ];
  443. m_inputPorts[ m_inputPorts.Count - 1 ].Locked = ( m_currentLightModel == StandardShaderLightModel.Standard ) || ( m_currentLightModel == StandardShaderLightModel.StandardSpecular ) ? false : true;
  444. AddInputPort( WirePortDataType.FLOAT, false, RefractionStr, index + 2, MasterNodePortCategory.Fragment, 8 );
  445. m_refractionPort = m_inputPorts[ m_inputPorts.Count - 1 ];
  446. m_inputPorts[ m_inputPorts.Count - 1 ].Locked = ( m_alphaMode == AlphaMode.Opaque || m_alphaMode == AlphaMode.Masked || m_currentLightModel == StandardShaderLightModel.Unlit || m_currentLightModel == StandardShaderLightModel.CustomLighting );
  447. AddInputPort( WirePortDataType.FLOAT, false, AlphaStr, index++, MasterNodePortCategory.Fragment, 9 );
  448. m_inputPorts[ m_inputPorts.Count - 1 ].DataName = AlphaDataStr;
  449. m_opacityPortId = m_inputPorts.Count - 1;
  450. m_inputPorts[ m_inputPorts.Count - 1 ].Locked = ( m_alphaMode == AlphaMode.Opaque || m_alphaMode == AlphaMode.Masked );
  451. AddInputPort( WirePortDataType.FLOAT, false, DiscardStr, index++, MasterNodePortCategory.Fragment, 10 );
  452. m_inputPorts[ m_inputPorts.Count - 1 ].Locked = ( m_alphaMode != AlphaMode.Masked && m_alphaMode != AlphaMode.Custom );
  453. m_discardPortId = m_inputPorts.Count - 1;
  454. // This is done to take the index + 2 from refraction port into account and not overlap indexes
  455. index++;
  456. AddInputPort( WirePortDataType.FLOAT3, false, CustomLightingStr, index++, MasterNodePortCategory.Fragment, 13 );
  457. m_inputPorts[ m_inputPorts.Count - 1 ].Locked = ( m_currentLightModel != StandardShaderLightModel.CustomLighting );
  458. m_inputPorts[ m_inputPorts.Count - 1 ].GenType = PortGenType.CustomLighting;
  459. m_customLightingPortId = m_inputPorts.Count - 1;
  460. ////////////////////////////////////////////////////////////////////////////////////////////////
  461. // Vertex functions - Adding ordex index in order to force these to be the last ones
  462. // Well now they have been moved to be the first ones so operations on vertex are to be taken into account
  463. // by dither, screen position and similar nodes
  464. ////////////////////////////////////////////////////////////////////////////////////////////////
  465. m_vertexPortId = m_inputPorts.Count;
  466. m_tessOpHelper.VertexOffsetIndexPort = m_vertexPortId;
  467. AddInputPort( WirePortDataType.FLOAT3, false, ( m_vertexMode == VertexMode.Relative ? VertexDisplacementStr : VertexPositionStr ), VertexDataStr, 0/*index++*/, MasterNodePortCategory.Vertex, 11 );
  468. AddInputPort( WirePortDataType.FLOAT3, false, VertexNormalStr, 1/*index++*/, MasterNodePortCategory.Vertex, 12 );
  469. AddInputPort( WirePortDataType.FLOAT4, false, VertexTangentStr, 2/*index++*/, MasterNodePortCategory.Vertex, 16 );
  470. //AddInputPort( WirePortDataType.FLOAT3, false, CustomLightModelStr, index++, MasterNodePortCategory.Fragment, 13 );
  471. //m_inputPorts[ m_inputPorts.Count - 1 ].Locked = true;// !(m_currentLightModel == StandardShaderLightModel.CustomLighting);
  472. AddInputPort( WirePortDataType.FLOAT4, false, TessellationOpHelper.TessellationPortStr, index++, MasterNodePortCategory.Tessellation, 14 );
  473. m_tessellationPort = m_inputPorts[ m_inputPorts.Count - 1 ];
  474. m_tessOpHelper.MasterNodeIndexPort = m_tessellationPort.PortId;
  475. ////////////////////////////////////////////////////////////////////////////////////
  476. AddInputPort( WirePortDataType.FLOAT3, false, DebugStr, index++, MasterNodePortCategory.Debug, 15 );
  477. for( int i = 0; i < m_inputPorts.Count; i++ )
  478. {
  479. m_inputPorts[ i ].CustomColor = Color.white;
  480. }
  481. m_sizeIsDirty = true;
  482. }
  483. public override void ForcePortType()
  484. {
  485. int portId = 0;
  486. switch( m_currentLightModel )
  487. {
  488. case StandardShaderLightModel.Standard:
  489. {
  490. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT3, false );
  491. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT3, false );
  492. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT3, false );
  493. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT, false );
  494. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT, false );
  495. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT, false );
  496. }
  497. break;
  498. case StandardShaderLightModel.StandardSpecular:
  499. {
  500. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT3, false );
  501. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT3, false );
  502. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT3, false );
  503. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT3, false );
  504. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT, false );
  505. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT, false );
  506. }
  507. break;
  508. case StandardShaderLightModel.CustomLighting:
  509. {
  510. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT3, false );
  511. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT3, false );
  512. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT3, false );
  513. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT3, false );
  514. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT, false );
  515. }
  516. break;
  517. case StandardShaderLightModel.Unlit:
  518. case StandardShaderLightModel.Lambert:
  519. {
  520. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT3, false );
  521. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT3, false );
  522. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT3, false );
  523. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT, false );
  524. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT, false );
  525. }
  526. break;
  527. case StandardShaderLightModel.BlinnPhong:
  528. {
  529. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT3, false );
  530. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT3, false );
  531. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT3, false );
  532. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT, false );
  533. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT, false );
  534. }
  535. break;
  536. }
  537. //Transmission
  538. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT3, false );
  539. //Translucency
  540. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT3, false );
  541. //Refraction
  542. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT, false );
  543. //Alpha
  544. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT, false );
  545. //Discard
  546. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT, false );
  547. //Custom Lighting
  548. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT3, false );
  549. //Vertex Offset
  550. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT3, false );
  551. //Vertex Normal
  552. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT3, false );
  553. //Vertex Tangents
  554. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT4, false );
  555. //Tessellation
  556. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT4, false );
  557. //Debug
  558. m_inputPorts[ portId++ ].ChangeType( WirePortDataType.FLOAT3, false );
  559. }
  560. public override void SetName( string name )
  561. {
  562. ShaderName = name;
  563. }
  564. public void DrawInspectorProperty()
  565. {
  566. if( m_inspectorDefaultStyle == null )
  567. {
  568. m_inspectorDefaultStyle = UIUtils.GetCustomStyle( CustomStyle.ResetToDefaultInspectorButton );
  569. }
  570. DrawCustomInspector( false );
  571. }
  572. private void RecursiveLog()
  573. {
  574. List<PropertyNode> nodes = UIUtils.PropertyNodesList();
  575. nodes.Sort( ( x, y ) => { return x.OrderIndex.CompareTo( y.OrderIndex ); } );
  576. for( int i = 0; i < nodes.Count; i++ )
  577. {
  578. if( ( nodes[ i ] is ReordenatorNode ) )
  579. ( nodes[ i ] as ReordenatorNode ).RecursiveLog();
  580. else
  581. Debug.Log( nodes[ i ].OrderIndex + " " + nodes[ i ].PropertyName );
  582. }
  583. }
  584. public void DrawGeneralOptions()
  585. {
  586. DrawShaderName();
  587. DrawCurrentShaderType();
  588. EditorGUI.BeginChangeCheck();
  589. m_currentLightModel = (StandardShaderLightModel)EditorGUILayoutEnumPopup( LightModelContent, m_currentLightModel );
  590. if( EditorGUI.EndChangeCheck() )
  591. {
  592. ContainerGraph.ChangedLightingModel = true;
  593. if( m_currentLightModel == StandardShaderLightModel.CustomLighting )
  594. {
  595. ContainerGraph.ParentWindow.CurrentNodeAvailability = NodeAvailability.CustomLighting;
  596. //ContainerGraph.CurrentCanvasMode = NodeAvailability.CustomLighting;
  597. }
  598. else
  599. {
  600. ContainerGraph.ParentWindow.CurrentNodeAvailability = NodeAvailability.SurfaceShader;
  601. //ContainerGraph.CurrentCanvasMode = NodeAvailability.SurfaceShader;
  602. }
  603. }
  604. m_shaderModelIdx = EditorGUILayoutPopup( ShaderModelStr, m_shaderModelIdx, ShaderModelTypeArr );
  605. EditorGUI.BeginChangeCheck();
  606. DrawPrecisionProperty( false );
  607. if( EditorGUI.EndChangeCheck() )
  608. ContainerGraph.CurrentPrecision = m_currentPrecisionType;
  609. //m_cullMode = (CullMode)EditorGUILayoutEnumPopup( CullModeContent, m_cullMode );
  610. UndoParentNode inst = this;
  611. m_inlineCullMode.CustomDrawer( ref inst, ( x ) => { m_cullMode = (CullMode)EditorGUILayoutEnumPopup( CullModeContent, m_cullMode ); }, CullModeContent.text );
  612. //m_inlineCullMode.Value = (int)m_cullMode;
  613. //m_inlineCullMode.EnumTypePopup( ref inst, CullModeContent.text, Enum.GetNames( typeof( CullMode ) ) );
  614. //m_cullMode = (CullMode) m_inlineCullMode.Value;
  615. m_renderPath = (RenderPath)EditorGUILayoutEnumPopup( RenderPathContent, m_renderPath );
  616. m_castShadows = EditorGUILayoutToggle( CastShadowsContent, m_castShadows );
  617. m_receiveShadows = EditorGUILayoutToggle( ReceiveShadowsContent, m_receiveShadows );
  618. DrawSamplingMacros();
  619. m_drawInstancedHelper.Draw( this );
  620. m_queueOrder = EditorGUILayoutIntField( QueueIndexContent, m_queueOrder );
  621. EditorGUI.BeginChangeCheck();
  622. m_vertexMode = (VertexMode)EditorGUILayoutEnumPopup( VertexModeStr, m_vertexMode );
  623. if( EditorGUI.EndChangeCheck() )
  624. {
  625. m_inputPorts[ m_vertexPortId ].Name = m_vertexMode == VertexMode.Relative ? VertexDisplacementStr : VertexPositionStr;
  626. m_sizeIsDirty = true;
  627. }
  628. ShaderLOD = Mathf.Clamp( EditorGUILayoutIntField( ShaderLODContent, ShaderLOD ), 0, Shader.globalMaximumLOD );
  629. ////m_lodCrossfade = EditorGUILayoutToggle( LODCrossfadeContent, m_lodCrossfade );
  630. m_fallbackHelper.Draw( this );
  631. DrawInspectorProperty();
  632. }
  633. public void ShowOpacityMaskValueUI()
  634. {
  635. EditorGUI.BeginChangeCheck();
  636. UndoParentNode inst = this;
  637. m_inlineOpacityMaskClipValue.CustomDrawer( ref inst, ( x ) => { m_opacityMaskClipValue = EditorGUILayoutFloatField( OpacityMaskClipValueContent, m_opacityMaskClipValue ); }, OpacityMaskClipValueContent.text );
  638. if( EditorGUI.EndChangeCheck() )
  639. {
  640. m_checkChanges = true;
  641. if( m_currentMaterial != null && m_currentMaterial.HasProperty( IOUtils.MaskClipValueName ) )
  642. {
  643. m_currentMaterial.SetFloat( IOUtils.MaskClipValueName, m_opacityMaskClipValue );
  644. }
  645. }
  646. }
  647. public override void DrawProperties()
  648. {
  649. if( m_inspectorFoldoutStyle == null || EditorGUIUtility.isProSkin != m_usingProSkin )
  650. m_inspectorFoldoutStyle = new GUIStyle( GUI.skin.GetStyle( "foldout" ) );
  651. if( m_inspectorToolbarStyle == null || EditorGUIUtility.isProSkin != m_usingProSkin )
  652. {
  653. m_inspectorToolbarStyle = new GUIStyle( GUI.skin.GetStyle( "toolbarbutton" ) )
  654. {
  655. fixedHeight = 20
  656. };
  657. }
  658. if( m_inspectorTooldropdownStyle == null || EditorGUIUtility.isProSkin != m_usingProSkin )
  659. {
  660. m_inspectorTooldropdownStyle = new GUIStyle( GUI.skin.GetStyle( "toolbardropdown" ) )
  661. {
  662. fixedHeight = 20
  663. };
  664. m_inspectorTooldropdownStyle.margin.bottom = 2;
  665. }
  666. if( EditorGUIUtility.isProSkin != m_usingProSkin )
  667. m_usingProSkin = EditorGUIUtility.isProSkin;
  668. base.DrawProperties();
  669. EditorGUILayout.BeginVertical();
  670. {
  671. EditorGUILayout.Separator();
  672. m_titleOpacity = 0.5f;
  673. m_boxOpacity = ( EditorGUIUtility.isProSkin ? 0.5f : 0.25f );
  674. m_cachedColor = GUI.color;
  675. // General
  676. bool generalIsVisible = ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedGeneralShaderOptions;
  677. NodeUtils.DrawPropertyGroup( ref generalIsVisible, GeneralFoldoutStr, DrawGeneralOptions );
  678. ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedGeneralShaderOptions = generalIsVisible;
  679. //Blend Mode
  680. GUI.color = new Color( m_cachedColor.r, m_cachedColor.g, m_cachedColor.b, m_titleOpacity );
  681. EditorGUILayout.BeginHorizontal( m_inspectorToolbarStyle );
  682. GUI.color = m_cachedColor;
  683. bool blendOptionsVisible = GUILayout.Toggle( ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedBlendOptions, AlphaModeContent, UIUtils.MenuItemToggleStyle, GUILayout.ExpandWidth( true ) );
  684. if( Event.current.button == Constants.FoldoutMouseId )
  685. {
  686. ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedBlendOptions = blendOptionsVisible;
  687. }
  688. if( !EditorGUIUtility.isProSkin )
  689. GUI.color = new Color( 0.25f, 0.25f, 0.25f, 1f );
  690. float boxSize = 60;
  691. switch( m_alphaMode )
  692. {
  693. case AlphaMode.Transparent:
  694. boxSize = 85;
  695. break;
  696. case AlphaMode.Translucent:
  697. boxSize = 80;
  698. break;
  699. case AlphaMode.Premultiply:
  700. boxSize = 120;
  701. break;
  702. }
  703. EditorGUI.BeginChangeCheck();
  704. m_alphaMode = (AlphaMode)EditorGUILayoutPopup( string.Empty, (int)m_alphaMode, FadeModeOptions, UIUtils.InspectorPopdropdownStyle, GUILayout.Width( boxSize ), GUILayout.Height( 19 ) );
  705. if( EditorGUI.EndChangeCheck() )
  706. {
  707. UpdateFromBlendMode();
  708. }
  709. GUI.color = m_cachedColor;
  710. EditorGUILayout.EndHorizontal();
  711. m_customBlendAvailable = ( m_alphaMode == AlphaMode.Custom || m_alphaMode == AlphaMode.Opaque );
  712. if( ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedBlendOptions )
  713. {
  714. GUI.color = new Color( m_cachedColor.r, m_cachedColor.g, m_cachedColor.b, m_boxOpacity );
  715. EditorGUILayout.BeginVertical( UIUtils.MenuItemBackgroundStyle );
  716. GUI.color = m_cachedColor;
  717. EditorGUI.indentLevel++;
  718. EditorGUILayout.Separator();
  719. EditorGUI.BeginChangeCheck();
  720. m_renderType = (RenderType)EditorGUILayoutEnumPopup( RenderTypeContent, m_renderType );
  721. if( m_renderType == RenderType.Custom )
  722. {
  723. EditorGUI.BeginChangeCheck();
  724. m_customRenderType = EditorGUILayoutTextField( CustomRenderTypeStr, m_customRenderType );
  725. if( EditorGUI.EndChangeCheck() )
  726. {
  727. m_customRenderType = UIUtils.RemoveInvalidCharacters( m_customRenderType );
  728. }
  729. }
  730. m_renderQueue = (RenderQueue)EditorGUILayoutEnumPopup( RenderQueueContent, m_renderQueue );
  731. if( EditorGUI.EndChangeCheck() )
  732. {
  733. if( m_renderType == RenderType.Opaque && m_renderQueue == RenderQueue.Geometry )
  734. m_alphaMode = AlphaMode.Opaque;
  735. else if( m_renderType == RenderType.TransparentCutout && m_renderQueue == RenderQueue.AlphaTest )
  736. m_alphaMode = AlphaMode.Masked;
  737. else if( m_renderType == RenderType.Transparent && m_renderQueue == RenderQueue.Transparent )
  738. m_alphaMode = AlphaMode.Transparent;
  739. else if( m_renderType == RenderType.Opaque && m_renderQueue == RenderQueue.Transparent )
  740. m_alphaMode = AlphaMode.Translucent;
  741. else
  742. m_alphaMode = AlphaMode.Custom;
  743. UpdateFromBlendMode();
  744. }
  745. bool bufferedEnabled = GUI.enabled;
  746. GUI.enabled = ( m_alphaMode == AlphaMode.Masked || m_alphaMode == AlphaMode.Custom );
  747. m_inputPorts[ m_discardPortId ].Locked = !GUI.enabled;
  748. ShowOpacityMaskValueUI();
  749. GUI.enabled = bufferedEnabled;
  750. EditorGUI.BeginDisabledGroup( !( m_alphaMode == AlphaMode.Transparent || m_alphaMode == AlphaMode.Premultiply || m_alphaMode == AlphaMode.Translucent || m_alphaMode == AlphaMode.Custom ) );
  751. m_grabOrder = EditorGUILayoutIntField( RefractionLayerStr, m_grabOrder );
  752. float cachedLabelWidth = EditorGUIUtility.labelWidth;
  753. UndoParentNode inst = this;
  754. if( m_refractionPort.IsConnected )
  755. {
  756. EditorGUIUtility.labelWidth = 145;
  757. EditorGUI.BeginChangeCheck();
  758. m_inlineChromaticAberration.RangedFloatField( ref inst, ChromaticAberrationStr, 0.0f,0.3f );
  759. if( EditorGUI.EndChangeCheck() )
  760. {
  761. if( m_currentMaterial != null && m_currentMaterial.HasProperty( IOUtils.ChromaticAberrationProperty ) )
  762. {
  763. m_currentMaterial.SetFloat( IOUtils.ChromaticAberrationProperty, m_inlineChromaticAberration.FloatValue );
  764. }
  765. }
  766. }
  767. //EditorGUIUtility.labelWidth = 130;
  768. m_inlineAlphaToCoverage.CustomDrawer( ref inst, ( x ) => { m_alphaToCoverage = EditorGUILayoutToggle( AlphaToCoverageStr, m_alphaToCoverage ); }, AlphaToCoverageStr.text );
  769. //EditorGUIUtility.labelWidth = cachedLabelWidth;
  770. EditorGUI.EndDisabledGroup();
  771. EditorGUILayout.Separator();
  772. if( !m_customBlendAvailable )
  773. {
  774. EditorGUILayout.HelpBox( "Advanced options are only available for Custom blend modes", MessageType.Warning );
  775. }
  776. EditorGUI.BeginDisabledGroup( !m_customBlendAvailable );
  777. m_blendOpsHelper.Draw( this, m_customBlendAvailable );
  778. m_colorMaskHelper.Draw( this );
  779. EditorGUI.EndDisabledGroup();
  780. EditorGUILayout.Separator();
  781. EditorGUI.indentLevel--;
  782. EditorGUILayout.EndVertical();
  783. }
  784. m_stencilBufferHelper.Draw( this );
  785. m_tessOpHelper.Draw( this, m_inspectorToolbarStyle, m_currentMaterial, m_tessellationPort.IsConnected );
  786. m_outlineHelper.Draw( this, m_inspectorToolbarStyle, m_currentMaterial );
  787. m_billboardOpHelper.Draw( this );
  788. m_zBufferHelper.Draw( this, m_inspectorToolbarStyle, m_customBlendAvailable );
  789. m_renderingOptionsOpHelper.Draw( this );
  790. m_renderingPlatformOpHelper.Draw( this );
  791. //m_additionalDefines.Draw( this );
  792. //m_additionalIncludes.Draw( this );
  793. //m_additionalPragmas.Draw( this );
  794. m_additionalSurfaceOptions.Draw( this );
  795. m_usePass.Draw( this );
  796. m_additionalDirectives.Draw( this );
  797. m_customTagsHelper.Draw( this );
  798. m_dependenciesHelper.Draw( this );
  799. DrawMaterialInputs( m_inspectorToolbarStyle );
  800. }
  801. EditorGUILayout.EndVertical();
  802. }
  803. public override void OnNodeLogicUpdate( DrawInfo drawInfo )
  804. {
  805. base.OnNodeLogicUpdate( drawInfo );
  806. if( m_initialize )
  807. {
  808. m_initialize = false;
  809. if( m_dummyProperty == null )
  810. {
  811. m_dummyProperty = ScriptableObject.CreateInstance<PropertyNode>();
  812. m_dummyProperty.ContainerGraph = ContainerGraph;
  813. }
  814. }
  815. if( m_currentLightModel != m_lastLightModel )
  816. m_lightModelChanged = true;
  817. if( m_lightModelChanged )
  818. {
  819. m_lightModelChanged = false;
  820. if( m_currentLightModel == StandardShaderLightModel.BlinnPhong )
  821. {
  822. if( m_specColorReorder == null )
  823. {
  824. m_specColorReorder = ScriptableObject.CreateInstance<ReordenatorNode>();
  825. m_specColorReorder.ContainerGraph = ContainerGraph;
  826. m_specColorReorder.OrderIndex = m_specColorOrderIndex;
  827. m_specColorReorder.Init( "_SpecColor", "Specular Color", null );
  828. }
  829. UIUtils.RegisterPropertyNode( m_specColorReorder );
  830. }
  831. else
  832. {
  833. if( m_specColorReorder != null )
  834. UIUtils.UnregisterPropertyNode( m_specColorReorder );
  835. }
  836. if( m_currentLightModel == StandardShaderLightModel.CustomLighting && m_masterNodeCategory == 0 )
  837. ContainerGraph.CurrentCanvasMode = NodeAvailability.CustomLighting;
  838. else if( m_masterNodeCategory == 0 )
  839. ContainerGraph.CurrentCanvasMode = NodeAvailability.SurfaceShader;
  840. CacheCurrentSettings();
  841. m_lastLightModel = m_currentLightModel;
  842. DeleteAllInputConnections( true, true );
  843. AddMasterPorts();
  844. ConnectFromCache();
  845. }
  846. if( drawInfo.CurrentEventType != EventType.Layout )
  847. return;
  848. if( m_transmissionPort != null && m_transmissionPort.IsConnected && m_renderPath != RenderPath.ForwardOnly )
  849. {
  850. m_renderPath = RenderPath.ForwardOnly;
  851. UIUtils.ShowMessage( "Render Path changed to Forward Only since transmission only works in forward rendering" );
  852. }
  853. if( m_translucencyPort != null && m_translucencyPort.IsConnected && m_renderPath != RenderPath.ForwardOnly )
  854. {
  855. m_renderPath = RenderPath.ForwardOnly;
  856. UIUtils.ShowMessage( "Render Path changed to Forward Only since translucency only works in forward rendering" );
  857. }
  858. if( m_translucencyPort.IsConnected != m_previousTranslucencyOn )
  859. m_checkChanges = true;
  860. if( m_refractionPort.IsConnected != m_previousRefractionOn )
  861. m_checkChanges = true;
  862. if( ( m_tessOpHelper.EnableTesselation && !m_tessellationPort.IsConnected ) != m_previousTessellationOn )
  863. m_checkChanges = true;
  864. m_previousTranslucencyOn = m_translucencyPort.IsConnected;
  865. m_previousRefractionOn = m_refractionPort.IsConnected;
  866. m_previousTessellationOn = ( m_tessOpHelper.EnableTesselation && !m_tessellationPort.IsConnected );
  867. if( m_checkChanges )
  868. {
  869. if( m_translucencyPort.IsConnected )
  870. {
  871. if( m_translucencyReorder == null )
  872. {
  873. List<PropertyNode> translucencyList = new List<PropertyNode>();
  874. for( int i = 0; i < 7; i++ )
  875. {
  876. translucencyList.Add( m_dummyProperty );
  877. }
  878. m_translucencyReorder = ScriptableObject.CreateInstance<ReordenatorNode>();
  879. m_translucencyReorder.ContainerGraph = ContainerGraph;
  880. m_translucencyReorder.OrderIndex = m_translucencyOrderIndex;
  881. m_translucencyReorder.Init( "_TranslucencyGroup", "Translucency", translucencyList );
  882. }
  883. UIUtils.RegisterPropertyNode( m_translucencyReorder );
  884. }
  885. else
  886. {
  887. if( m_translucencyReorder != null )
  888. UIUtils.UnregisterPropertyNode( m_translucencyReorder );
  889. }
  890. if( m_refractionPort.IsConnected )
  891. {
  892. if( m_refractionReorder == null )
  893. {
  894. List<PropertyNode> refractionList = new List<PropertyNode>();
  895. for( int i = 0; i < 2; i++ )
  896. {
  897. refractionList.Add( m_dummyProperty );
  898. }
  899. m_refractionReorder = ScriptableObject.CreateInstance<ReordenatorNode>();
  900. m_refractionReorder.ContainerGraph = ContainerGraph;
  901. m_refractionReorder.OrderIndex = m_refractionOrderIndex;
  902. m_refractionReorder.Init( "_RefractionGroup", "Refraction", refractionList );
  903. }
  904. UIUtils.RegisterPropertyNode( m_refractionReorder );
  905. }
  906. else
  907. {
  908. if( m_refractionReorder != null )
  909. UIUtils.UnregisterPropertyNode( m_refractionReorder );
  910. }
  911. if( m_tessOpHelper.EnableTesselation && !m_tessellationPort.IsConnected )
  912. {
  913. if( m_tessellationReorder == null )
  914. {
  915. List<PropertyNode> tessellationList = new List<PropertyNode>();
  916. for( int i = 0; i < 4; i++ )
  917. {
  918. tessellationList.Add( m_dummyProperty );
  919. }
  920. m_tessellationReorder = ScriptableObject.CreateInstance<ReordenatorNode>();
  921. m_tessellationReorder.ContainerGraph = ContainerGraph;
  922. m_tessellationReorder.OrderIndex = m_tessellationOrderIndex;
  923. m_tessellationReorder.Init( "_TessellationGroup", "Tessellation", tessellationList );
  924. m_tessellationReorder.HeaderTitle = "Tesselation";
  925. }
  926. UIUtils.RegisterPropertyNode( m_tessellationReorder );
  927. }
  928. else
  929. {
  930. if( m_tessellationReorder != null )
  931. UIUtils.UnregisterPropertyNode( m_tessellationReorder );
  932. }
  933. if( m_inputPorts[ m_discardPortId ].Available && !m_inlineOpacityMaskClipValue.IsValid )
  934. {
  935. if( m_maskClipReorder == null )
  936. {
  937. // Create dragable clip material property
  938. m_maskClipReorder = ScriptableObject.CreateInstance<ReordenatorNode>();
  939. m_maskClipReorder.ContainerGraph = ContainerGraph;
  940. m_maskClipReorder.OrderIndex = m_maskClipOrderIndex;
  941. m_maskClipReorder.Init( "_Cutoff", "Mask Clip Value", null );
  942. }
  943. UIUtils.RegisterPropertyNode( m_maskClipReorder );
  944. }
  945. else
  946. {
  947. if( m_maskClipReorder != null )
  948. UIUtils.UnregisterPropertyNode( m_maskClipReorder );
  949. }
  950. m_checkChanges = false;
  951. }
  952. }
  953. public override void OnNodeRepaint( DrawInfo drawInfo )
  954. {
  955. base.OnNodeRepaint( drawInfo );
  956. if( m_containerGraph.IsInstancedShader || m_renderingOptionsOpHelper.ForceEnableInstancing )
  957. {
  958. DrawInstancedIcon( drawInfo );
  959. }
  960. }
  961. private void CacheCurrentSettings()
  962. {
  963. m_cacheNodeConnections.Clear();
  964. for( int portId = 0; portId < m_inputPorts.Count; portId++ )
  965. {
  966. if( m_inputPorts[ portId ].IsConnected )
  967. {
  968. WireReference connection = m_inputPorts[ portId ].GetConnection();
  969. m_cacheNodeConnections.Add( m_inputPorts[ portId ].Name, new NodeCache( connection.NodeId, connection.PortId ) );
  970. }
  971. }
  972. }
  973. private void ConnectFromCache()
  974. {
  975. for( int i = 0; i < m_inputPorts.Count; i++ )
  976. {
  977. NodeCache cache = m_cacheNodeConnections.Get( m_inputPorts[ i ].Name );
  978. if( cache != null )
  979. {
  980. UIUtils.SetConnection( UniqueId, m_inputPorts[ i ].PortId, cache.TargetNodeId, cache.TargetPortId );
  981. }
  982. }
  983. }
  984. public override void UpdateMaterial( Material mat )
  985. {
  986. base.UpdateMaterial( mat );
  987. if( m_alphaMode == AlphaMode.Masked || m_alphaMode == AlphaMode.Custom )
  988. {
  989. if( mat.HasProperty( IOUtils.MaskClipValueName ) )
  990. mat.SetFloat( IOUtils.MaskClipValueName, m_opacityMaskClipValue );
  991. }
  992. if( m_refractionPort.IsConnected && !m_inlineChromaticAberration.Active )
  993. {
  994. if( mat.HasProperty( IOUtils.ChromaticAberrationProperty ) )
  995. mat.SetFloat( IOUtils.ChromaticAberrationProperty, m_inlineChromaticAberration.FloatValue );
  996. }
  997. }
  998. public override void SetMaterialMode( Material mat, bool fetchMaterialValues )
  999. {
  1000. base.SetMaterialMode( mat, fetchMaterialValues );
  1001. if( m_alphaMode == AlphaMode.Masked || m_alphaMode == AlphaMode.Custom )
  1002. {
  1003. if( fetchMaterialValues && m_materialMode && mat.HasProperty( IOUtils.MaskClipValueName ) )
  1004. {
  1005. m_opacityMaskClipValue = mat.GetFloat( IOUtils.MaskClipValueName );
  1006. }
  1007. }
  1008. if( m_refractionPort.IsConnected && !m_inlineChromaticAberration.Active )
  1009. {
  1010. if( fetchMaterialValues && m_materialMode && mat.HasProperty( IOUtils.ChromaticAberrationProperty ) )
  1011. m_inlineChromaticAberration.FloatValue = mat.GetFloat( IOUtils.ChromaticAberrationProperty);
  1012. }
  1013. }
  1014. public override void ForceUpdateFromMaterial( Material material )
  1015. {
  1016. m_tessOpHelper.UpdateFromMaterial( material );
  1017. m_outlineHelper.UpdateFromMaterial( material );
  1018. if( m_alphaMode == AlphaMode.Masked || m_alphaMode == AlphaMode.Custom )
  1019. {
  1020. if( material.HasProperty( IOUtils.MaskClipValueName ) )
  1021. m_opacityMaskClipValue = material.GetFloat( IOUtils.MaskClipValueName );
  1022. }
  1023. if( m_refractionPort.IsConnected && !m_inlineChromaticAberration.Active )
  1024. {
  1025. if( material.HasProperty( IOUtils.ChromaticAberrationProperty ) )
  1026. m_inlineChromaticAberration.FloatValue = material.GetFloat( IOUtils.ChromaticAberrationProperty );
  1027. }
  1028. }
  1029. public override void UpdateMasterNodeMaterial( Material material )
  1030. {
  1031. m_currentMaterial = material;
  1032. UpdateMaterialEditor();
  1033. }
  1034. void UpdateMaterialEditor()
  1035. {
  1036. FireMaterialChangedEvt();
  1037. }
  1038. public string CreateInstructionsForVertexPort( InputPort port )
  1039. {
  1040. //Vertex displacement and per vertex custom data
  1041. WireReference connection = port.GetConnection();
  1042. ParentNode node = UIUtils.GetNode( connection.NodeId );
  1043. string vertexInstructions = node.GetValueFromOutputStr( connection.PortId, port.DataType, ref m_currentDataCollector, false );
  1044. if( m_currentDataCollector.DirtySpecialLocalVariables )
  1045. {
  1046. m_currentDataCollector.AddVertexInstruction( m_currentDataCollector.SpecialLocalVariables, UniqueId, false );
  1047. m_currentDataCollector.ClearSpecialLocalVariables();
  1048. }
  1049. if( m_currentDataCollector.DirtyVertexVariables )
  1050. {
  1051. m_currentDataCollector.AddVertexInstruction( m_currentDataCollector.VertexLocalVariables, UniqueId, false );
  1052. m_currentDataCollector.ClearVertexLocalVariables();
  1053. }
  1054. return vertexInstructions;
  1055. }
  1056. public void CreateInstructionsForPort( InputPort port, string portName, bool addCustomDelimiters = false, string customDelimiterIn = null, string customDelimiterOut = null, bool ignoreLocalVar = false, bool normalIsConnected = false , bool isDebugPort = false )
  1057. {
  1058. WireReference connection = port.GetConnection();
  1059. ParentNode node = UIUtils.GetNode( connection.NodeId );
  1060. string newInstruction = node.GetValueFromOutputStr( connection.PortId, port.DataType, ref m_currentDataCollector, ignoreLocalVar );
  1061. if( m_currentDataCollector.DirtySpecialLocalVariables )
  1062. {
  1063. m_currentDataCollector.AddInstructions( m_currentDataCollector.SpecialLocalVariables );
  1064. m_currentDataCollector.ClearSpecialLocalVariables();
  1065. }
  1066. if( m_currentDataCollector.DirtyVertexVariables )
  1067. {
  1068. m_currentDataCollector.AddVertexInstruction( m_currentDataCollector.VertexLocalVariables, port.NodeId, false );
  1069. m_currentDataCollector.ClearVertexLocalVariables();
  1070. }
  1071. if( m_currentDataCollector.ForceNormal && !normalIsConnected )
  1072. {
  1073. m_currentDataCollector.AddToStartInstructions( "\t\t\t" + Constants.OutputVarStr + ".Normal = float3(0,0,1);\n" );
  1074. m_currentDataCollector.DirtyNormal = true;
  1075. m_currentDataCollector.ForceNormal = false;
  1076. }
  1077. m_currentDataCollector.AddInstructions( addCustomDelimiters ? customDelimiterIn : ( "\t\t\t" + portName + " = " ) );
  1078. m_currentDataCollector.AddInstructions( newInstruction );
  1079. m_currentDataCollector.AddInstructions( addCustomDelimiters ? customDelimiterOut :((isDebugPort)?" + 1E-5;\n":";\n") );
  1080. }
  1081. public string CreateInstructionStringForPort( InputPort port, bool ignoreLocalVar = false )
  1082. {
  1083. WireReference connection = port.GetConnection();
  1084. ParentNode node = UIUtils.GetNode( connection.NodeId );
  1085. string newInstruction = node.GetValueFromOutputStr( connection.PortId, port.DataType, ref m_currentDataCollector, ignoreLocalVar );
  1086. if( m_currentDataCollector.DirtySpecialLocalVariables )
  1087. {
  1088. m_currentDataCollector.AddInstructions( m_currentDataCollector.SpecialLocalVariables );
  1089. m_currentDataCollector.ClearSpecialLocalVariables();
  1090. }
  1091. if( m_currentDataCollector.DirtyVertexVariables )
  1092. {
  1093. m_currentDataCollector.AddVertexInstruction( m_currentDataCollector.VertexLocalVariables, port.NodeId, false );
  1094. m_currentDataCollector.ClearVertexLocalVariables();
  1095. }
  1096. if( m_currentDataCollector.ForceNormal )
  1097. {
  1098. m_currentDataCollector.AddToStartInstructions( "\t\t\t" + Constants.OutputVarStr + ".Normal = float3(0,0,1);\n" );
  1099. m_currentDataCollector.DirtyNormal = true;
  1100. m_currentDataCollector.ForceNormal = false;
  1101. }
  1102. return newInstruction;
  1103. }
  1104. public override Shader Execute( string pathname, bool isFullPath )
  1105. {
  1106. ForcePortType();
  1107. ForceReordering();
  1108. UpdateFromBlendMode();
  1109. base.Execute( pathname, isFullPath );
  1110. RegisterStandaloneFuntions();
  1111. bool isInstancedShader = m_renderingOptionsOpHelper.ForceEnableInstancing || UIUtils.IsInstancedShader();
  1112. bool hasVirtualTexture = UIUtils.HasVirtualTexture();
  1113. bool hasTranslucency = false;
  1114. bool hasTransmission = false;
  1115. bool hasEmission = false;
  1116. bool hasOpacity = false;
  1117. bool hasOpacityMask = false;
  1118. bool hasRefraction = false;
  1119. //bool hasVertexOffset = false;
  1120. //bool hasCustomLightingAlpha = false;
  1121. bool hasCustomLightingMask = false;
  1122. string customLightingCode = string.Empty;
  1123. string customLightingAlphaCode = string.Empty;
  1124. string customLightingMaskCode = string.Empty;
  1125. string customLightingInstructions = string.Empty;
  1126. string refractionCode = string.Empty;
  1127. string refractionInstructions = string.Empty;
  1128. string refractionFix = string.Empty;
  1129. string aboveUsePasses = string.Empty;
  1130. string bellowUsePasses = string.Empty;
  1131. m_currentDataCollector.TesselationActive = m_tessOpHelper.EnableTesselation;
  1132. #if UNITY_IOS
  1133. if ( m_currentDataCollector.TesselationActive )
  1134. {
  1135. // On iOS custom app data must be used since fixed4 color from appdata_full generates an error on it when tessellation is active
  1136. m_currentDataCollector.ForceCustomAppDataUsage();
  1137. }
  1138. #endif
  1139. m_currentDataCollector.CurrentRenderPath = m_renderPath;
  1140. StandardShaderLightModel cachedLightModel = m_currentLightModel;
  1141. NodeAvailability cachedAvailability = ContainerGraph.CurrentCanvasMode;
  1142. bool debugIsUsingCustomLighting = false;
  1143. bool usingDebugPort = false;
  1144. if( m_inputPorts[ m_inputPorts.Count - 1 ].IsConnected )
  1145. {
  1146. usingDebugPort = true;
  1147. debugIsUsingCustomLighting = m_currentLightModel == StandardShaderLightModel.CustomLighting;
  1148. m_currentDataCollector.GenType = PortGenType.CustomLighting;
  1149. m_currentLightModel = StandardShaderLightModel.CustomLighting;
  1150. ContainerGraph.CurrentCanvasMode = NodeAvailability.CustomLighting;
  1151. }
  1152. // @diogo: Set ASE info
  1153. ASEPackageManagerHelper.SetASEVersionInfoOnDataCollector( ref m_currentDataCollector );
  1154. if ( isInstancedShader )
  1155. {
  1156. m_currentDataCollector.AddToPragmas( UniqueId, IOUtils.InstancedPropertiesHeader );
  1157. }
  1158. if( m_renderingOptionsOpHelper.SpecularHighlightToggle || m_renderingOptionsOpHelper.ReflectionsToggle )
  1159. m_currentDataCollector.AddToProperties( UniqueId, "[Header(Forward Rendering Options)]", 10001 );
  1160. if( m_renderingOptionsOpHelper.SpecularHighlightToggle )
  1161. {
  1162. m_currentDataCollector.AddToProperties( UniqueId, "[ToggleOff] _SpecularHighlights(\"Specular Highlights\", Float) = 1.0", 10002 );
  1163. m_currentDataCollector.AddToPragmas( UniqueId, "shader_feature _SPECULARHIGHLIGHTS_OFF" );
  1164. }
  1165. if( m_renderingOptionsOpHelper.ReflectionsToggle )
  1166. {
  1167. m_currentDataCollector.AddToProperties( UniqueId, "[ToggleOff] _GlossyReflections(\"Reflections\", Float) = 1.0", 10003 );
  1168. m_currentDataCollector.AddToPragmas( UniqueId, "shader_feature _GLOSSYREFLECTIONS_OFF" );
  1169. }
  1170. // See if each node is being used on frag and/or vert ports
  1171. SetupNodeCategories();
  1172. m_containerGraph.CheckPropertiesAutoRegister( ref m_currentDataCollector );
  1173. if( m_refractionPort.IsConnected || m_inputPorts[ m_inputPorts.Count - 1 ].IsConnected )
  1174. {
  1175. m_currentDataCollector.DirtyNormal = true;
  1176. m_currentDataCollector.ForceNormal = true;
  1177. }
  1178. //this.PropagateNodeData( nodeData );
  1179. string tags = "\"RenderType\" = \"{0}\" \"Queue\" = \"{1}\"";
  1180. string finalRenderType = ( m_renderType == RenderType.Custom && m_customRenderType.Length > 0 ) ? m_customRenderType : m_renderType.ToString();
  1181. tags = string.Format( tags, finalRenderType, ( m_renderQueue + ( ( m_queueOrder >= 0 ) ? "+" : string.Empty ) + m_queueOrder ) );
  1182. //if ( !m_customBlendMode )
  1183. {
  1184. if( m_alphaMode == AlphaMode.Transparent || m_alphaMode == AlphaMode.Premultiply )
  1185. {
  1186. //tags += " \"IgnoreProjector\" = \"True\"";
  1187. if( !m_renderingOptionsOpHelper.IgnoreProjectorValue )
  1188. {
  1189. Debug.Log( string.Format( "Setting Ignore Projector to True since it's requires by Blend Mode {0}.", m_alphaMode ) );
  1190. m_renderingOptionsOpHelper.IgnoreProjectorValue = true;
  1191. }
  1192. }
  1193. }
  1194. tags += m_renderingOptionsOpHelper.IgnoreProjectorTag;
  1195. tags += m_renderingOptionsOpHelper.ForceNoShadowCastingTag;
  1196. tags += m_renderingOptionsOpHelper.DisableBatchingTag;
  1197. //add virtual texture support
  1198. if( hasVirtualTexture )
  1199. {
  1200. tags += " \"Amplify\" = \"True\" ";
  1201. }
  1202. //tags = "Tags{ " + tags + " }";
  1203. string outputStruct = "";
  1204. switch( m_currentLightModel )
  1205. {
  1206. case StandardShaderLightModel.CustomLighting: outputStruct = "SurfaceOutputCustomLightingCustom"; break;
  1207. case StandardShaderLightModel.Standard: outputStruct = "SurfaceOutputStandard"; break;
  1208. case StandardShaderLightModel.StandardSpecular: outputStruct = "SurfaceOutputStandardSpecular"; break;
  1209. case StandardShaderLightModel.Unlit:
  1210. case StandardShaderLightModel.Lambert:
  1211. case StandardShaderLightModel.BlinnPhong: outputStruct = "SurfaceOutput"; break;
  1212. }
  1213. if( m_currentLightModel == StandardShaderLightModel.CustomLighting )
  1214. {
  1215. m_currentDataCollector.AddToIncludes( UniqueId, Constants.UnityPBSLightingLib );
  1216. m_currentDataCollector.ChangeCustomInputHeader( m_currentLightModel.ToString() + Constants.CustomLightStructStr );
  1217. m_currentDataCollector.AddToCustomInput( UniqueId, "half3 Albedo", true );
  1218. m_currentDataCollector.AddToCustomInput( UniqueId, "half3 Normal", true );
  1219. m_currentDataCollector.AddToCustomInput( UniqueId, "half3 Emission", true );
  1220. m_currentDataCollector.AddToCustomInput( UniqueId, "half Metallic", true );
  1221. m_currentDataCollector.AddToCustomInput( UniqueId, "half Smoothness", true );
  1222. m_currentDataCollector.AddToCustomInput( UniqueId, "half Occlusion", true );
  1223. m_currentDataCollector.AddToCustomInput( UniqueId, "half Alpha", true );
  1224. m_currentDataCollector.AddToCustomInput( UniqueId, "Input SurfInput", true );
  1225. m_currentDataCollector.AddToCustomInput( UniqueId, "UnityGIInput GIData", true );
  1226. }
  1227. //Terrain Draw Instanced
  1228. if( m_drawInstancedHelper.Enabled )
  1229. {
  1230. if( !m_currentDataCollector.DirtyPerVertexData )
  1231. {
  1232. m_currentDataCollector.OpenPerVertexHeader( !m_tessOpHelper.EnableTesselation );
  1233. }
  1234. m_drawInstancedHelper.UpdateDataCollectorForStandard( ref m_currentDataCollector );
  1235. }
  1236. // Need to sort before creating local vars so they can inspect the normal port correctly
  1237. SortedList<int, InputPort> sortedPorts = new SortedList<int, InputPort>();
  1238. for( int i = 0; i < m_inputPorts.Count; i++ )
  1239. {
  1240. sortedPorts.Add( m_inputPorts[ i ].OrderId, m_inputPorts[ i ] );
  1241. }
  1242. //This must be set before node code generation since it will be used by Outline node
  1243. m_currentDataCollector.SurfaceCustomShadowCaster = CustomShadowCaster;
  1244. bool normalIsConnected = m_normalPort.IsConnected;
  1245. m_tessOpHelper.Reset();
  1246. if( m_inputPorts[ m_inputPorts.Count - 1 ].IsConnected )
  1247. {
  1248. //Debug Port active
  1249. InputPort debugPort = m_inputPorts[ m_inputPorts.Count - 1 ];
  1250. m_currentDataCollector.PortCategory = debugPort.Category;
  1251. if( debugIsUsingCustomLighting )
  1252. {
  1253. m_currentDataCollector.UsingCustomOutput = true;
  1254. WireReference connection = m_inputPorts[ m_inputPorts.Count - 1 ].GetConnection();
  1255. ParentNode node = UIUtils.GetNode( connection.NodeId );
  1256. customLightingCode = node.GetValueFromOutputStr( connection.PortId, WirePortDataType.FLOAT3, ref m_currentDataCollector, false );
  1257. customLightingInstructions = m_currentDataCollector.CustomOutput;
  1258. if( m_currentDataCollector.ForceNormal )
  1259. {
  1260. m_currentDataCollector.AddToStartInstructions( "\t\t\t" + Constants.OutputVarStr + ".Normal = float3(0,0,1);\n" );
  1261. m_currentDataCollector.DirtyNormal = true;
  1262. m_currentDataCollector.ForceNormal = false;
  1263. }
  1264. if( m_currentDataCollector.DirtyVertexVariables )
  1265. {
  1266. m_currentDataCollector.AddVertexInstruction( m_currentDataCollector.VertexLocalVariables, UniqueId, false );
  1267. m_currentDataCollector.ClearVertexLocalVariables();
  1268. }
  1269. m_currentDataCollector.UsingCustomOutput = false;
  1270. }
  1271. else
  1272. {
  1273. CreateInstructionsForPort( debugPort, Constants.OutputVarStr + ".Emission", false, null, null, false, false,true );
  1274. }
  1275. }
  1276. else
  1277. {
  1278. MasterNodePortCategory currentCategory = sortedPorts[ 0 ].Category;
  1279. //Collect data from standard nodes
  1280. for( int i = 0; i < sortedPorts.Count; i++ )
  1281. {
  1282. // prepare ports for custom lighting
  1283. m_currentDataCollector.GenType = sortedPorts[ i ].GenType;
  1284. if( m_currentLightModel == StandardShaderLightModel.CustomLighting && sortedPorts[ i ].Name.Equals( AlphaStr ) )
  1285. ContainerGraph.ResetNodesLocalVariablesIfNot( MasterNodePortCategory.Vertex );
  1286. if( sortedPorts[ i ].IsConnected )
  1287. {
  1288. m_currentDataCollector.PortCategory = sortedPorts[ i ].Category;
  1289. if( sortedPorts[ i ].Name.Equals( NormalStr ) )// Normal Map is Connected
  1290. {
  1291. m_currentDataCollector.DirtyNormal = true;
  1292. }
  1293. if( sortedPorts[ i ].Name.Equals( TranslucencyStr ) )
  1294. {
  1295. hasTranslucency = true;
  1296. }
  1297. if( sortedPorts[ i ].Name.Equals( TransmissionStr ) )
  1298. {
  1299. hasTransmission = true;
  1300. }
  1301. if( sortedPorts[ i ].Name.Equals( EmissionStr ) )
  1302. {
  1303. hasEmission = true;
  1304. }
  1305. if( sortedPorts[ i ].Name.Equals( RefractionStr ) )
  1306. {
  1307. hasRefraction = true;
  1308. }
  1309. if( sortedPorts[ i ].Name.Equals( AlphaStr ) )
  1310. {
  1311. hasOpacity = true;
  1312. }
  1313. if( sortedPorts[ i ].Name.Equals( DiscardStr ) )
  1314. {
  1315. hasOpacityMask = true;
  1316. }
  1317. if( hasRefraction )
  1318. {
  1319. m_currentDataCollector.AddToInput( UniqueId, SurfaceInputs.SCREEN_POS );
  1320. m_currentDataCollector.AddToInput( UniqueId, SurfaceInputs.WORLD_POS );
  1321. //not necessary, just being safe
  1322. m_currentDataCollector.DirtyNormal = true;
  1323. m_currentDataCollector.ForceNormal = true;
  1324. if( m_grabOrder != 0 )
  1325. {
  1326. m_currentDataCollector.AddGrabPass( "RefractionGrab" + m_grabOrder );
  1327. m_currentDataCollector.AddToUniforms( UniqueId, "uniform sampler2D RefractionGrab" + m_grabOrder + ";" );
  1328. }
  1329. else
  1330. {
  1331. m_currentDataCollector.AddGrabPass( "" );
  1332. m_currentDataCollector.AddToUniforms( UniqueId, "uniform sampler2D _GrabTexture;" );
  1333. }
  1334. if( !m_inlineChromaticAberration.Active )
  1335. {
  1336. m_currentDataCollector.AddToUniforms( UniqueId, "uniform float _ChromaticAberration;" );
  1337. m_currentDataCollector.AddToProperties( UniqueId, "[Header(Refraction)]", m_refractionReorder.OrderIndex );
  1338. m_currentDataCollector.AddToProperties( UniqueId, "_ChromaticAberration(\"Chromatic Aberration\", Range( 0 , 0.3)) = 0.1", m_refractionReorder.OrderIndex + 1 );
  1339. }
  1340. m_currentDataCollector.AddToPragmas( UniqueId, "multi_compile _ALPHAPREMULTIPLY_ON" );
  1341. }
  1342. if( hasTranslucency || hasTransmission )
  1343. {
  1344. //Translucency and Transmission Generation
  1345. //Add properties and uniforms
  1346. m_currentDataCollector.AddToIncludes( UniqueId, Constants.UnityPBSLightingLib );
  1347. if( hasTranslucency )
  1348. {
  1349. m_currentDataCollector.AddToProperties( UniqueId, "[Header(Translucency)]", m_translucencyReorder.OrderIndex );
  1350. m_currentDataCollector.AddToProperties( UniqueId, "_Translucency(\"Strength\", Range( 0 , 50)) = 1", m_translucencyReorder.OrderIndex + 1 );
  1351. m_currentDataCollector.AddToProperties( UniqueId, "_TransNormalDistortion(\"Normal Distortion\", Range( 0 , 1)) = 0.1", m_translucencyReorder.OrderIndex + 2 );
  1352. m_currentDataCollector.AddToProperties( UniqueId, "_TransScattering(\"Scaterring Falloff\", Range( 1 , 50)) = 2", m_translucencyReorder.OrderIndex + 3 );
  1353. m_currentDataCollector.AddToProperties( UniqueId, "_TransDirect(\"Direct\", Range( 0 , 1)) = 1", m_translucencyReorder.OrderIndex + 4 );
  1354. m_currentDataCollector.AddToProperties( UniqueId, "_TransAmbient(\"Ambient\", Range( 0 , 1)) = 0.2", m_translucencyReorder.OrderIndex + 5 );
  1355. m_currentDataCollector.AddToProperties( UniqueId, "_TransShadow(\"Shadow\", Range( 0 , 1)) = 0.9", m_translucencyReorder.OrderIndex + 6 );
  1356. m_currentDataCollector.AddToUniforms( UniqueId, "uniform half _Translucency;" );
  1357. m_currentDataCollector.AddToUniforms( UniqueId, "uniform half _TransNormalDistortion;" );
  1358. m_currentDataCollector.AddToUniforms( UniqueId, "uniform half _TransScattering;" );
  1359. m_currentDataCollector.AddToUniforms( UniqueId, "uniform half _TransDirect;" );
  1360. m_currentDataCollector.AddToUniforms( UniqueId, "uniform half _TransAmbient;" );
  1361. m_currentDataCollector.AddToUniforms( UniqueId, "uniform half _TransShadow;" );
  1362. }
  1363. //Add custom struct
  1364. switch( m_currentLightModel )
  1365. {
  1366. case StandardShaderLightModel.Standard:
  1367. case StandardShaderLightModel.StandardSpecular:
  1368. outputStruct = "SurfaceOutput" + m_currentLightModel.ToString() + Constants.CustomLightStructStr; break;
  1369. }
  1370. m_currentDataCollector.ChangeCustomInputHeader( m_currentLightModel.ToString() + Constants.CustomLightStructStr );
  1371. m_currentDataCollector.AddToCustomInput( UniqueId, "half3 Albedo", true );
  1372. m_currentDataCollector.AddToCustomInput( UniqueId, "half3 Normal", true );
  1373. m_currentDataCollector.AddToCustomInput( UniqueId, "half3 Emission", true );
  1374. switch( m_currentLightModel )
  1375. {
  1376. case StandardShaderLightModel.Standard:
  1377. m_currentDataCollector.AddToCustomInput( UniqueId, "half Metallic", true );
  1378. break;
  1379. case StandardShaderLightModel.StandardSpecular:
  1380. m_currentDataCollector.AddToCustomInput( UniqueId, "half3 Specular", true );
  1381. break;
  1382. }
  1383. m_currentDataCollector.AddToCustomInput( UniqueId, "half Smoothness", true );
  1384. m_currentDataCollector.AddToCustomInput( UniqueId, "half Occlusion", true );
  1385. m_currentDataCollector.AddToCustomInput( UniqueId, "half Alpha", true );
  1386. if( hasTranslucency )
  1387. m_currentDataCollector.AddToCustomInput( UniqueId, "half3 Translucency", true );
  1388. if( hasTransmission )
  1389. m_currentDataCollector.AddToCustomInput( UniqueId, "half3 Transmission", true );
  1390. }
  1391. if( sortedPorts[ i ].Name.Equals( DiscardStr ) )
  1392. {
  1393. //Discard Op Node
  1394. if( m_currentLightModel == StandardShaderLightModel.CustomLighting )
  1395. {
  1396. hasCustomLightingMask = true;
  1397. m_currentDataCollector.UsingCustomOutput = true;
  1398. m_currentDataCollector.GenType = PortGenType.CustomLighting;
  1399. WireReference connection = sortedPorts[ i ].GetConnection();
  1400. ParentNode node = UIUtils.GetNode( connection.NodeId );
  1401. customLightingMaskCode = node.GetValueFromOutputStr( connection.PortId, WirePortDataType.FLOAT, ref m_currentDataCollector, false );
  1402. customLightingMaskCode = "clip( " + customLightingMaskCode + " - " + m_inlineOpacityMaskClipValue.GetValueOrProperty( IOUtils.MaskClipValueName, false ) + " )";
  1403. customLightingInstructions = m_currentDataCollector.CustomOutput;
  1404. m_currentDataCollector.GenType = PortGenType.NonCustomLighting;
  1405. m_currentDataCollector.UsingCustomOutput = false;
  1406. continue;
  1407. }
  1408. else
  1409. {
  1410. string clipIn = "\t\t\tclip( ";
  1411. string clipOut = " - " + m_inlineOpacityMaskClipValue.GetValueOrProperty( IOUtils.MaskClipValueName, false ) + " );\n";
  1412. //if( ( m_alphaToCoverage || m_inlineAlphaToCoverage.Active ) && m_castShadows )
  1413. //{
  1414. // clipIn = "\t\t\t#if UNITY_PASS_SHADOWCASTER\n" + clipIn;
  1415. // clipOut = clipOut + "\t\t\t#endif\n";
  1416. //}
  1417. CreateInstructionsForPort( sortedPorts[ i ], Constants.OutputVarStr + "." + sortedPorts[ i ].DataName, true, clipIn, clipOut, false, normalIsConnected );
  1418. }
  1419. }
  1420. else if( sortedPorts[ i ].DataName.Equals( VertexDataStr ) )
  1421. {
  1422. string vertexInstructions = CreateInstructionsForVertexPort( sortedPorts[ i ] );
  1423. m_currentDataCollector.AddToVertexDisplacement( vertexInstructions, m_vertexMode );
  1424. }
  1425. else if( sortedPorts[ i ].DataName.Equals( VertexNormalStr ) )
  1426. {
  1427. string vertexInstructions = CreateInstructionsForVertexPort( sortedPorts[ i ] );
  1428. m_currentDataCollector.AddToVertexNormal( vertexInstructions );
  1429. }
  1430. else if ( sortedPorts[ i ].DataName.Equals( VertexTangentStr ) )
  1431. {
  1432. string vertexInstructions = CreateInstructionsForVertexPort( sortedPorts[ i ] );
  1433. m_currentDataCollector.AddToVertexTangent( vertexInstructions );
  1434. }
  1435. else if( m_tessOpHelper.IsTessellationPort( sortedPorts[ i ].PortId ) && sortedPorts[ i ].IsConnected /* && m_tessOpHelper.EnableTesselation*/)
  1436. {
  1437. //Vertex displacement and per vertex custom data
  1438. WireReference connection = sortedPorts[ i ].GetConnection();
  1439. ParentNode node = UIUtils.GetNode( connection.NodeId );
  1440. string vertexInstructions = node.GetValueFromOutputStr( connection.PortId, sortedPorts[ i ].DataType, ref m_currentDataCollector, false );
  1441. if( m_currentDataCollector.DirtySpecialLocalVariables )
  1442. {
  1443. m_tessOpHelper.AddAdditionalData( m_currentDataCollector.SpecialLocalVariables );
  1444. m_currentDataCollector.ClearSpecialLocalVariables();
  1445. }
  1446. if( m_currentDataCollector.DirtyVertexVariables )
  1447. {
  1448. m_tessOpHelper.AddAdditionalData( m_currentDataCollector.VertexLocalVariables );
  1449. m_currentDataCollector.ClearVertexLocalVariables();
  1450. }
  1451. m_tessOpHelper.AddCustomFunction( vertexInstructions );
  1452. }
  1453. else if( sortedPorts[ i ].Name.Equals( RefractionStr ) )
  1454. {
  1455. ContainerGraph.ResetNodesLocalVariables();
  1456. m_currentDataCollector.UsingCustomOutput = true;
  1457. refractionFix = " + 0.00001 * i.screenPos * i.worldPos";
  1458. m_currentDataCollector.AddInstructions( "\t\t\to.Normal = o.Normal" + refractionFix + ";\n" );
  1459. refractionCode = CreateInstructionStringForPort( sortedPorts[ i ], false );
  1460. refractionInstructions = m_currentDataCollector.CustomOutput;
  1461. m_currentDataCollector.UsingCustomOutput = false;
  1462. }
  1463. else if( sortedPorts[ i ].Name.Equals( CustomLightingStr ) )
  1464. {
  1465. m_currentDataCollector.UsingCustomOutput = true;
  1466. WireReference connection = sortedPorts[ i ].GetConnection();
  1467. ParentNode node = UIUtils.GetNode( connection.NodeId );
  1468. customLightingCode = node.GetValueFromOutputStr( connection.PortId, WirePortDataType.FLOAT3, ref m_currentDataCollector, false );
  1469. customLightingInstructions = m_currentDataCollector.CustomOutput;
  1470. if( m_currentDataCollector.ForceNormal )
  1471. {
  1472. m_currentDataCollector.AddToStartInstructions( "\t\t\t" + Constants.OutputVarStr + ".Normal = float3(0,0,1);\n" );
  1473. m_currentDataCollector.DirtyNormal = true;
  1474. m_currentDataCollector.ForceNormal = false;
  1475. }
  1476. if( m_currentDataCollector.DirtyVertexVariables )
  1477. {
  1478. m_currentDataCollector.AddVertexInstruction( m_currentDataCollector.VertexLocalVariables, UniqueId, false );
  1479. m_currentDataCollector.ClearVertexLocalVariables();
  1480. }
  1481. m_currentDataCollector.UsingCustomOutput = false;
  1482. }
  1483. else if( sortedPorts[ i ].Name.Equals( AlphaStr ) && m_currentLightModel == StandardShaderLightModel.CustomLighting )
  1484. {
  1485. m_currentDataCollector.UsingCustomOutput = true;
  1486. m_currentDataCollector.GenType = PortGenType.CustomLighting;
  1487. WireReference connection = sortedPorts[ i ].GetConnection();
  1488. ParentNode node = UIUtils.GetNode( connection.NodeId );
  1489. customLightingAlphaCode = node.GetValueFromOutputStr( connection.PortId, WirePortDataType.FLOAT, ref m_currentDataCollector, false );
  1490. customLightingInstructions = m_currentDataCollector.CustomOutput;
  1491. if( m_currentDataCollector.ForceNormal )
  1492. {
  1493. m_currentDataCollector.AddToStartInstructions( "\t\t\t" + Constants.OutputVarStr + ".Normal = float3(0,0,1);\n" );
  1494. m_currentDataCollector.DirtyNormal = true;
  1495. m_currentDataCollector.ForceNormal = false;
  1496. }
  1497. if( m_currentDataCollector.DirtyVertexVariables )
  1498. {
  1499. m_currentDataCollector.AddVertexInstruction( m_currentDataCollector.VertexLocalVariables, UniqueId, false );
  1500. m_currentDataCollector.ClearVertexLocalVariables();
  1501. }
  1502. m_currentDataCollector.GenType = PortGenType.NonCustomLighting;
  1503. m_currentDataCollector.UsingCustomOutput = false;
  1504. }
  1505. else
  1506. {
  1507. // Surface shader instruccions
  1508. // if working on normals and have normal dependent node then ignore local var generation
  1509. CreateInstructionsForPort( sortedPorts[ i ], Constants.OutputVarStr + "." + sortedPorts[ i ].DataName, false, null, null, false, normalIsConnected );
  1510. }
  1511. }
  1512. else if( sortedPorts[ i ].Name.Equals( AlphaStr ) )
  1513. {
  1514. if( m_currentLightModel != StandardShaderLightModel.CustomLighting )
  1515. {
  1516. m_currentDataCollector.AddInstructions( string.Format( "\t\t\t{0}.{1} = 1;\n", Constants.OutputVarStr, sortedPorts[ i ].DataName ) );
  1517. }
  1518. }
  1519. }
  1520. m_billboardOpHelper.FillDataCollectorWithInternalData( ref m_currentDataCollector );
  1521. }
  1522. m_customShadowCaster = CustomShadowCaster;
  1523. //if( !m_renderingOptionsOpHelper.UseDefaultShadowCaster &&
  1524. // ( ( m_castShadows && ( m_alphaToCoverage || m_inlineAlphaToCoverage.Active ) ) ||
  1525. // ( m_castShadows && hasOpacity ) ||
  1526. // ( m_castShadows && ( m_currentDataCollector.UsingWorldNormal || m_currentDataCollector.UsingWorldReflection || m_currentDataCollector.UsingViewDirection ) ) ||
  1527. // ( m_castShadows && m_inputPorts[ m_discardPortId ].Available && m_inputPorts[ m_discardPortId ].IsConnected && m_currentLightModel == StandardShaderLightModel.CustomLighting ) ))
  1528. // m_customShadowCaster = true;
  1529. //else
  1530. // m_customShadowCaster = false;
  1531. //m_customShadowCaster = true;
  1532. for( int i = 0; i < 4; i++ )
  1533. {
  1534. if( m_currentDataCollector.GetChannelUsage( i ) == TextureChannelUsage.Required )
  1535. {
  1536. string channelName = UIUtils.GetChannelName( i );
  1537. m_currentDataCollector.AddToProperties( -1, UIUtils.GetTex2DProperty( channelName, TexturePropertyValues.white ), -1 );
  1538. }
  1539. }
  1540. m_currentDataCollector.AddToProperties( -1, IOUtils.DefaultASEDirtyCheckProperty, 10000 );
  1541. if( m_inputPorts[ m_discardPortId ].Available && m_inputPorts[ m_discardPortId ].IsConnected )
  1542. {
  1543. if( m_inlineOpacityMaskClipValue.IsValid )
  1544. {
  1545. RangedFloatNode fnode = UIUtils.GetNode( m_inlineOpacityMaskClipValue.NodeId ) as RangedFloatNode;
  1546. if( fnode != null )
  1547. {
  1548. m_currentDataCollector.AddToProperties( fnode.UniqueId, fnode.GetPropertyValue(), fnode.OrderIndex );
  1549. m_currentDataCollector.AddToUniforms( fnode.UniqueId, fnode.GetUniformValue() );
  1550. }
  1551. else
  1552. {
  1553. IntNode inode = UIUtils.GetNode( m_inlineOpacityMaskClipValue.NodeId ) as IntNode;
  1554. m_currentDataCollector.AddToProperties( inode.UniqueId, inode.GetPropertyValue(), inode.OrderIndex );
  1555. m_currentDataCollector.AddToUniforms( inode.UniqueId, inode.GetUniformValue() );
  1556. }
  1557. }
  1558. else
  1559. {
  1560. m_currentDataCollector.AddToProperties( -1, string.Format( IOUtils.MaskClipValueProperty, OpacityMaskClipValueStr, m_opacityMaskClipValue ), ( m_maskClipReorder != null ) ? m_maskClipReorder.OrderIndex : -1 );
  1561. m_currentDataCollector.AddToUniforms( -1, string.Format( IOUtils.MaskClipValueUniform, m_opacityMaskClipValue ) );
  1562. }
  1563. }
  1564. if( !m_currentDataCollector.DirtyInputs )
  1565. m_currentDataCollector.AddToInput( UniqueId, "half filler", true );
  1566. if( m_currentLightModel == StandardShaderLightModel.BlinnPhong )
  1567. m_currentDataCollector.AddToProperties( -1, "_SpecColor(\"Specular Color\",Color)=(1,1,1,1)", m_specColorReorder.OrderIndex );
  1568. //Tesselation
  1569. if( m_tessOpHelper.EnableTesselation )
  1570. {
  1571. m_tessOpHelper.AddToDataCollector( ref m_currentDataCollector, m_tessellationReorder != null ? m_tessellationReorder.OrderIndex : -1 );
  1572. if( !m_currentDataCollector.DirtyPerVertexData )
  1573. {
  1574. m_currentDataCollector.OpenPerVertexHeader( false );
  1575. }
  1576. }
  1577. if( m_outlineHelper.EnableOutline || ( m_currentDataCollector.UsingCustomOutlineColor || m_currentDataCollector.CustomOutlineSelectedAlpha > 0 || m_currentDataCollector.UsingCustomOutlineWidth ) )
  1578. {
  1579. m_outlineHelper.AddToDataCollector( ref m_currentDataCollector );
  1580. }
  1581. //m_additionalIncludes.AddToDataCollector( ref m_currentDataCollector );
  1582. //m_additionalPragmas.AddToDataCollector( ref m_currentDataCollector );
  1583. //m_additionalDefines.AddToDataCollector( ref m_currentDataCollector );
  1584. m_additionalDirectives.AddAllToDataCollector( ref m_currentDataCollector );
  1585. //m_currentDataCollector.CloseInputs();
  1586. m_currentDataCollector.CloseCustomInputs();
  1587. m_currentDataCollector.CloseProperties();
  1588. m_currentDataCollector.ClosePerVertexHeader();
  1589. //build Shader Body
  1590. string ShaderBody = string.Empty;
  1591. OpenShaderBody( ref ShaderBody, m_shaderName );
  1592. {
  1593. //set properties
  1594. if( m_currentDataCollector.DirtyProperties )
  1595. {
  1596. ShaderBody += m_currentDataCollector.BuildPropertiesString();
  1597. }
  1598. //set subshader
  1599. OpenSubShaderBody( ref ShaderBody );
  1600. {
  1601. // Add extra depth pass
  1602. m_zBufferHelper.DrawExtraDepthPass( ref ShaderBody );
  1603. // Add optionalPasses
  1604. if( m_outlineHelper.EnableOutline || ( m_currentDataCollector.UsingCustomOutlineColor || m_currentDataCollector.CustomOutlineSelectedAlpha > 0 || m_currentDataCollector.UsingCustomOutlineWidth ) )
  1605. {
  1606. if( !usingDebugPort )
  1607. AddMultilineBody( ref ShaderBody, m_outlineHelper.OutlineFunctionBody( ref m_currentDataCollector, isInstancedShader, m_customShadowCaster, UIUtils.RemoveInvalidCharacters( ShaderName ), ( m_billboardOpHelper.IsBillboard && !usingDebugPort ? m_billboardOpHelper.GetInternalMultilineInstructions() : null ), ref m_tessOpHelper, ShaderModelTypeArr[ m_shaderModelIdx ], CurrentPrecisionType ) );
  1608. }
  1609. //Add SubShader tags
  1610. if( hasEmission )
  1611. {
  1612. tags += " \"IsEmissive\" = \"true\" ";
  1613. }
  1614. tags += m_customTagsHelper.GenerateCustomTags();
  1615. tags = "Tags{ " + tags + " }";
  1616. m_usePass.BuildUsePassInfo( m_currentDataCollector, ref aboveUsePasses, ref bellowUsePasses, "\t\t" );
  1617. if( !string.IsNullOrEmpty( aboveUsePasses ) )
  1618. {
  1619. ShaderBody += aboveUsePasses;
  1620. }
  1621. AddRenderTags( ref ShaderBody, tags );
  1622. AddShaderLOD( ref ShaderBody, ShaderLOD );
  1623. AddRenderState( ref ShaderBody, "Cull", m_inlineCullMode.GetValueOrProperty( m_cullMode.ToString() ) );
  1624. m_customBlendAvailable = ( m_alphaMode == AlphaMode.Custom || m_alphaMode == AlphaMode.Opaque );
  1625. if( ( m_zBufferHelper.IsActive && m_customBlendAvailable ) || m_outlineHelper.UsingZWrite || m_outlineHelper.UsingZTest )
  1626. {
  1627. ShaderBody += m_zBufferHelper.CreateDepthInfo( m_outlineHelper.UsingZWrite, m_outlineHelper.UsingZTest );
  1628. }
  1629. if( m_stencilBufferHelper.Active )
  1630. {
  1631. ShaderBody += m_stencilBufferHelper.CreateStencilOp( this );
  1632. }
  1633. if( m_blendOpsHelper.Active )
  1634. {
  1635. ShaderBody += m_blendOpsHelper.CreateBlendOps();
  1636. }
  1637. if( m_alphaToCoverage || m_inlineAlphaToCoverage.Active )
  1638. {
  1639. ShaderBody += "\t\tAlphaToMask "+ m_inlineAlphaToCoverage.GetValueOrProperty( "On" )+"\n";
  1640. }
  1641. // Build Color Mask
  1642. bool forceDefault = m_outlineHelper.ActiveColorMask;
  1643. m_colorMaskHelper.BuildColorMask( ref ShaderBody, m_customBlendAvailable, forceDefault );
  1644. //ShaderBody += "\t\tZWrite " + _zWriteMode + '\n';
  1645. //ShaderBody += "\t\tZTest " + _zTestMode + '\n';
  1646. //Add GrabPass
  1647. if( m_currentDataCollector.DirtyGrabPass )
  1648. {
  1649. ShaderBody += m_currentDataCollector.GrabPass;
  1650. }
  1651. // build optional parameters
  1652. string OptionalParameters = string.Empty;
  1653. // addword standard to custom lighting to accepts standard lighting models
  1654. string standardCustomLighting = string.Empty;
  1655. if( m_currentLightModel == StandardShaderLightModel.CustomLighting )
  1656. standardCustomLighting = "Standard";
  1657. //add cg program
  1658. if( m_customShadowCaster )
  1659. OpenCGInclude( ref ShaderBody );
  1660. else
  1661. OpenCGProgram( ref ShaderBody );
  1662. {
  1663. //Add Defines
  1664. if( m_currentDataCollector.DirtyDefines )
  1665. ShaderBody += m_currentDataCollector.Defines;
  1666. //Add Includes
  1667. if( m_customShadowCaster )
  1668. {
  1669. m_currentDataCollector.AddToIncludes( UniqueId, Constants.UnityPBSLightingLib );
  1670. m_currentDataCollector.AddToIncludes( UniqueId, "Lighting.cginc" );
  1671. }
  1672. if( m_currentDataCollector.DirtyIncludes )
  1673. ShaderBody += m_currentDataCollector.Includes;
  1674. //define as surface shader and specify lighting model
  1675. if( UIUtils.GetTextureArrayNodeAmount() > 0 && m_shaderModelIdx < 3 )
  1676. {
  1677. UIUtils.ShowMessage( "Automatically changing Shader Model to 3.5 since\nit's the minimum required by texture arrays." );
  1678. m_shaderModelIdx = 3;
  1679. }
  1680. // if tessellation is active then we need be at least using shader model 4.6
  1681. if( m_tessOpHelper.EnableTesselation && m_shaderModelIdx < 6 )
  1682. {
  1683. UIUtils.ShowMessage( "Automatically changing Shader Model to 4.6 since\nit's the minimum required by tessellation." );
  1684. m_shaderModelIdx = 6;
  1685. }
  1686. // if translucency is ON change render path
  1687. if( hasTranslucency && m_renderPath != RenderPath.ForwardOnly )
  1688. {
  1689. UIUtils.ShowMessage( "Automatically changing Render Path to Forward Only since\ntranslucency only works in forward rendering." );
  1690. m_renderPath = RenderPath.ForwardOnly;
  1691. }
  1692. // if outline is ON change render path
  1693. if( m_outlineHelper.EnableOutline && m_renderPath != RenderPath.ForwardOnly )
  1694. {
  1695. UIUtils.ShowMessage( "Automatically changing Render Path to Forward Only since\noutline only works in forward rendering." );
  1696. m_renderPath = RenderPath.ForwardOnly;
  1697. }
  1698. // if transmission is ON change render path
  1699. if( hasTransmission && m_renderPath != RenderPath.ForwardOnly )
  1700. {
  1701. UIUtils.ShowMessage( "Automatically changing Render Path to Forward Only since\ntransmission only works in forward rendering." );
  1702. m_renderPath = RenderPath.ForwardOnly;
  1703. }
  1704. // if refraction is ON change render path
  1705. if( hasRefraction && m_renderPath != RenderPath.ForwardOnly )
  1706. {
  1707. UIUtils.ShowMessage( "Automatically changing Render Path to Forward Only since\nrefraction only works in forward rendering." );
  1708. m_renderPath = RenderPath.ForwardOnly;
  1709. }
  1710. ShaderBody += string.Format( IOUtils.PragmaTargetHeader, ShaderModelTypeArr[ m_shaderModelIdx ] );
  1711. //Add pragmas (needs check to see if all pragmas work with custom shadow caster)
  1712. if( m_currentDataCollector.DirtyPragmas/* && !m_customShadowCaster */)
  1713. ShaderBody += m_currentDataCollector.Pragmas;
  1714. CheckSamplingMacrosFlag();
  1715. m_currentDataCollector.AddASEMacros();
  1716. if( m_currentDataCollector.DirtyAdditionalDirectives )
  1717. ShaderBody += m_currentDataCollector.StandardAdditionalDirectives;
  1718. //if ( !m_customBlendMode )
  1719. {
  1720. switch( m_alphaMode )
  1721. {
  1722. case AlphaMode.Opaque:
  1723. case AlphaMode.Masked: break;
  1724. case AlphaMode.Transparent:
  1725. {
  1726. OptionalParameters += "alpha:fade" + Constants.OptionalParametersSep;
  1727. }
  1728. break;
  1729. case AlphaMode.Premultiply:
  1730. {
  1731. OptionalParameters += "alpha:premul" + Constants.OptionalParametersSep;
  1732. }
  1733. break;
  1734. }
  1735. }
  1736. if( m_keepAlpha )
  1737. {
  1738. OptionalParameters += "keepalpha" + Constants.OptionalParametersSep;
  1739. }
  1740. if( hasRefraction )
  1741. {
  1742. OptionalParameters += "finalcolor:RefractionF" + Constants.OptionalParametersSep;
  1743. }
  1744. if( !m_customShadowCaster && m_castShadows )
  1745. {
  1746. OptionalParameters += "addshadow" + Constants.OptionalParametersSep;
  1747. }
  1748. if( m_castShadows )
  1749. {
  1750. OptionalParameters += "fullforwardshadows" + Constants.OptionalParametersSep;
  1751. }
  1752. if( !m_receiveShadows )
  1753. {
  1754. OptionalParameters += "noshadow" + Constants.OptionalParametersSep;
  1755. }
  1756. if( m_renderingOptionsOpHelper.IsOptionActive( " Add Pass" ) && usingDebugPort )
  1757. {
  1758. OptionalParameters += "noforwardadd" + Constants.OptionalParametersSep;
  1759. }
  1760. if( m_renderingOptionsOpHelper.ForceDisableInstancing )
  1761. {
  1762. OptionalParameters += "noinstancing" + Constants.OptionalParametersSep;
  1763. }
  1764. switch( m_renderPath )
  1765. {
  1766. case RenderPath.All: break;
  1767. case RenderPath.DeferredOnly: OptionalParameters += "exclude_path:forward" + Constants.OptionalParametersSep; break;
  1768. case RenderPath.ForwardOnly: OptionalParameters += "exclude_path:deferred" + Constants.OptionalParametersSep; break;
  1769. }
  1770. //Add code generation options
  1771. m_renderingOptionsOpHelper.Build( ref OptionalParameters );
  1772. if( !m_customShadowCaster )
  1773. {
  1774. string customLightSurface = string.Empty;
  1775. if( hasTranslucency || hasTransmission )
  1776. customLightSurface = "Custom";
  1777. m_renderingPlatformOpHelper.SetRenderingPlatforms( ref ShaderBody );
  1778. //Check if Custom Vertex is being used and add tag
  1779. if( m_currentDataCollector.DirtyPerVertexData )
  1780. OptionalParameters += "vertex:" + Constants.VertexDataFunc + Constants.OptionalParametersSep;
  1781. if( m_tessOpHelper.EnableTesselation && !usingDebugPort )
  1782. {
  1783. m_tessOpHelper.WriteToOptionalParams( ref OptionalParameters );
  1784. }
  1785. m_additionalSurfaceOptions.WriteToOptionalSurfaceOptions( ref OptionalParameters );
  1786. AddShaderPragma( ref ShaderBody, "surface surf " + standardCustomLighting + m_currentLightModel.ToString() + customLightSurface + Constants.OptionalParametersSep + OptionalParameters );
  1787. }
  1788. else
  1789. {
  1790. if( /*m_currentDataCollector.UsingWorldNormal ||*/ m_currentDataCollector.UsingInternalData )
  1791. {
  1792. ShaderBody += "\t\t#ifdef UNITY_PASS_SHADOWCASTER\n";
  1793. ShaderBody += "\t\t\t#undef INTERNAL_DATA\n";
  1794. ShaderBody += "\t\t\t#undef WorldReflectionVector\n";
  1795. ShaderBody += "\t\t\t#undef WorldNormalVector\n";
  1796. ShaderBody += "\t\t\t#define INTERNAL_DATA half3 internalSurfaceTtoW0; half3 internalSurfaceTtoW1; half3 internalSurfaceTtoW2;\n";
  1797. ShaderBody += "\t\t\t#define WorldReflectionVector(data,normal) reflect (data.worldRefl, half3(dot(data.internalSurfaceTtoW0,normal), dot(data.internalSurfaceTtoW1,normal), dot(data.internalSurfaceTtoW2,normal)))\n";
  1798. ShaderBody += "\t\t\t#define WorldNormalVector(data,normal) half3(dot(data.internalSurfaceTtoW0,normal), dot(data.internalSurfaceTtoW1,normal), dot(data.internalSurfaceTtoW2,normal))\n";
  1799. ShaderBody += "\t\t#endif\n";
  1800. }
  1801. }
  1802. if( m_currentDataCollector.UsingHigherSizeTexcoords )
  1803. {
  1804. ShaderBody += "\t\t#undef TRANSFORM_TEX\n";
  1805. ShaderBody += "\t\t#define TRANSFORM_TEX(tex,name) float4(tex.xy * name##_ST.xy + name##_ST.zw, tex.z, tex.w)\n";
  1806. }
  1807. if( m_currentDataCollector.DirtyAppData )
  1808. ShaderBody += m_currentDataCollector.CustomAppData;
  1809. // Add Input struct
  1810. if( m_currentDataCollector.DirtyInputs )
  1811. ShaderBody += "\t\t" + m_currentDataCollector.Inputs + "\t\t};" + "\n\n";
  1812. // Add Custom Lighting struct
  1813. if( m_currentDataCollector.DirtyCustomInput )
  1814. ShaderBody += m_currentDataCollector.CustomInput + "\n\n";
  1815. //Add Uniforms
  1816. if( m_currentDataCollector.DirtyUniforms )
  1817. ShaderBody += m_currentDataCollector.Uniforms + "\n";
  1818. // Add Array Derivatives Macros
  1819. //if( m_currentDataCollector.UsingArrayDerivatives )
  1820. //{
  1821. // ShaderBody += "\t\t#if defined(UNITY_COMPILER_HLSL2GLSL) || defined(SHADER_TARGET_SURFACE_ANALYSIS)\n";
  1822. // ShaderBody += "\t\t\t#define ASE_SAMPLE_TEX2DARRAY_GRAD(tex,coord,dx,dy) UNITY_SAMPLE_TEX2DARRAY (tex,coord)\n";
  1823. // ShaderBody += "\t\t#else\n";
  1824. // ShaderBody += "\t\t\t#define ASE_SAMPLE_TEX2DARRAY_GRAD(tex,coord,dx,dy) tex.SampleGrad (sampler##tex,coord,dx,dy)\n";
  1825. // ShaderBody += "\t\t#endif\n\n";
  1826. //}
  1827. //Add Instanced Properties
  1828. if( isInstancedShader && m_currentDataCollector.DirtyInstancedProperties )
  1829. {
  1830. m_currentDataCollector.SetupInstancePropertiesBlock( UIUtils.RemoveInvalidCharacters( ShaderName ) );
  1831. ShaderBody += m_currentDataCollector.InstancedProperties + "\n";
  1832. }
  1833. if( m_currentDataCollector.DirtyFunctions )
  1834. ShaderBody += m_currentDataCollector.Functions + "\n";
  1835. //Tesselation
  1836. if( m_tessOpHelper.EnableTesselation && !usingDebugPort )
  1837. {
  1838. ShaderBody += m_tessOpHelper.GetCurrentTessellationFunction( ref m_currentDataCollector ) + "\n";
  1839. }
  1840. //Add Custom Vertex Data
  1841. if( m_currentDataCollector.DirtyPerVertexData )
  1842. {
  1843. ShaderBody += m_currentDataCollector.VertexData;
  1844. }
  1845. if( m_currentLightModel == StandardShaderLightModel.Unlit )
  1846. {
  1847. for( int i = 0; i < VertexLitFunc.Length; i++ )
  1848. {
  1849. ShaderBody += VertexLitFunc[ i ] + "\n";
  1850. }
  1851. }
  1852. //Add custom lighting
  1853. if( m_currentLightModel == StandardShaderLightModel.CustomLighting )
  1854. {
  1855. ShaderBody += "\t\tinline half4 LightingStandard" + m_currentLightModel.ToString() + "( inout " + outputStruct + " " + Constants.CustomLightOutputVarStr + ", half3 viewDir, UnityGI gi )\n\t\t{\n";
  1856. ShaderBody += "\t\t\tUnityGIInput data = s.GIData;\n";
  1857. ShaderBody += "\t\t\tInput i = s.SurfInput;\n";
  1858. ShaderBody += "\t\t\thalf4 c = 0;\n";
  1859. if( m_currentDataCollector.UsingLightAttenuation )
  1860. {
  1861. ShaderBody += "\t\t\t#ifdef UNITY_PASS_FORWARDBASE\n";
  1862. ShaderBody += "\t\t\tfloat ase_lightAtten = data.atten;\n";
  1863. ShaderBody += "\t\t\tif( _LightColor0.a == 0)\n";
  1864. ShaderBody += "\t\t\tase_lightAtten = 0;\n";
  1865. ShaderBody += "\t\t\t#else\n";
  1866. ShaderBody += "\t\t\tfloat3 ase_lightAttenRGB = gi.light.color / ( ( _LightColor0.rgb ) + 0.000001 );\n";
  1867. ShaderBody += "\t\t\tfloat ase_lightAtten = max( max( ase_lightAttenRGB.r, ase_lightAttenRGB.g ), ase_lightAttenRGB.b );\n";
  1868. ShaderBody += "\t\t\t#endif\n";
  1869. ShaderBody += "\t\t\t#if defined(HANDLE_SHADOWS_BLENDING_IN_GI)\n";
  1870. ShaderBody += "\t\t\thalf bakedAtten = UnitySampleBakedOcclusion(data.lightmapUV.xy, data.worldPos);\n";
  1871. ShaderBody += "\t\t\tfloat zDist = dot(_WorldSpaceCameraPos - data.worldPos, UNITY_MATRIX_V[2].xyz);\n";
  1872. ShaderBody += "\t\t\tfloat fadeDist = UnityComputeShadowFadeDistance(data.worldPos, zDist);\n";
  1873. ShaderBody += "\t\t\tase_lightAtten = UnityMixRealtimeAndBakedShadows(data.atten, bakedAtten, UnityComputeShadowFade(fadeDist));\n";
  1874. ShaderBody += "\t\t\t#endif\n";
  1875. }
  1876. //if( m_currentDataCollector.dirtyc )
  1877. ShaderBody += customLightingInstructions;
  1878. ShaderBody += "\t\t\tc.rgb = " + ( !string.IsNullOrEmpty( customLightingCode ) ? customLightingCode : "0" ) + ";\n";
  1879. ShaderBody += "\t\t\tc.a = " + ( !string.IsNullOrEmpty( customLightingAlphaCode ) ? customLightingAlphaCode : "1" ) + ";\n";
  1880. if( m_alphaMode == AlphaMode.Premultiply || ( ( m_alphaMode == AlphaMode.Custom || m_alphaMode == AlphaMode.Opaque ) && m_blendOpsHelper.CurrentBlendRGB.IndexOf( "Premultiplied" ) > -1 ) )
  1881. ShaderBody += "\t\t\tc.rgb *= c.a;\n";
  1882. if( hasCustomLightingMask )
  1883. ShaderBody += "\t\t\t" + customLightingMaskCode + ";\n";
  1884. ShaderBody += "\t\t\treturn c;\n";
  1885. ShaderBody += "\t\t}\n\n";
  1886. //Add GI function
  1887. ShaderBody += "\t\tinline void LightingStandard" + m_currentLightModel.ToString() + "_GI( inout " + outputStruct + " " + Constants.CustomLightOutputVarStr + ", UnityGIInput data, inout UnityGI gi )\n\t\t{\n";
  1888. ShaderBody += "\t\t\ts.GIData = data;\n";
  1889. //ShaderBody += "\t\t\tUNITY_GI(gi, " + Constants.CustomLightOutputVarStr + ", data);\n";
  1890. ShaderBody += "\t\t}\n\n";
  1891. }
  1892. //Add custom lighting function
  1893. if( hasTranslucency || hasTransmission )
  1894. {
  1895. ShaderBody += "\t\tinline half4 Lighting" + m_currentLightModel.ToString() + Constants.CustomLightStructStr + "(" + outputStruct + " " + Constants.CustomLightOutputVarStr + ", half3 viewDir, UnityGI gi )\n\t\t{\n";
  1896. if( hasTranslucency )
  1897. {
  1898. //ShaderBody += "\t\t\t#if !DIRECTIONAL\n";
  1899. ShaderBody += "\t\t\t#if !defined(DIRECTIONAL)\n";
  1900. ShaderBody += "\t\t\tfloat3 lightAtten = gi.light.color;\n";
  1901. ShaderBody += "\t\t\t#else\n";
  1902. ShaderBody += "\t\t\tfloat3 lightAtten = lerp( _LightColor0.rgb, gi.light.color, _TransShadow );\n";
  1903. ShaderBody += "\t\t\t#endif\n";
  1904. ShaderBody += "\t\t\thalf3 lightDir = gi.light.dir + " + Constants.CustomLightOutputVarStr + ".Normal * _TransNormalDistortion;\n";
  1905. ShaderBody += "\t\t\thalf transVdotL = pow( saturate( dot( viewDir, -lightDir ) ), _TransScattering );\n";
  1906. ShaderBody += "\t\t\thalf3 translucency = lightAtten * (transVdotL * _TransDirect + gi.indirect.diffuse * _TransAmbient) * " + Constants.CustomLightOutputVarStr + ".Translucency;\n";
  1907. ShaderBody += "\t\t\thalf4 c = half4( " + Constants.CustomLightOutputVarStr + ".Albedo * translucency * _Translucency, 0 );\n\n";
  1908. }
  1909. if( hasTransmission )
  1910. {
  1911. ShaderBody += "\t\t\thalf3 transmission = max(0 , -dot(" + Constants.CustomLightOutputVarStr + ".Normal, gi.light.dir)) * gi.light.color * " + Constants.CustomLightOutputVarStr + ".Transmission;\n";
  1912. ShaderBody += "\t\t\thalf4 d = half4(" + Constants.CustomLightOutputVarStr + ".Albedo * transmission , 0);\n\n";
  1913. }
  1914. ShaderBody += "\t\t\tSurfaceOutput" + m_currentLightModel.ToString() + " r;\n";
  1915. ShaderBody += "\t\t\tr.Albedo = " + Constants.CustomLightOutputVarStr + ".Albedo;\n";
  1916. ShaderBody += "\t\t\tr.Normal = " + Constants.CustomLightOutputVarStr + ".Normal;\n";
  1917. ShaderBody += "\t\t\tr.Emission = " + Constants.CustomLightOutputVarStr + ".Emission;\n";
  1918. switch( m_currentLightModel )
  1919. {
  1920. case StandardShaderLightModel.Standard:
  1921. ShaderBody += "\t\t\tr.Metallic = " + Constants.CustomLightOutputVarStr + ".Metallic;\n";
  1922. break;
  1923. case StandardShaderLightModel.StandardSpecular:
  1924. ShaderBody += "\t\t\tr.Specular = " + Constants.CustomLightOutputVarStr + ".Specular;\n";
  1925. break;
  1926. }
  1927. ShaderBody += "\t\t\tr.Smoothness = " + Constants.CustomLightOutputVarStr + ".Smoothness;\n";
  1928. ShaderBody += "\t\t\tr.Occlusion = " + Constants.CustomLightOutputVarStr + ".Occlusion;\n";
  1929. ShaderBody += "\t\t\tr.Alpha = " + Constants.CustomLightOutputVarStr + ".Alpha;\n";
  1930. ShaderBody += "\t\t\treturn Lighting" + m_currentLightModel.ToString() + " (r, viewDir, gi)" + ( hasTranslucency ? " + c" : "" ) + ( hasTransmission ? " + d" : "" ) + ";\n";
  1931. ShaderBody += "\t\t}\n\n";
  1932. //Add GI function
  1933. ShaderBody += "\t\tinline void Lighting" + m_currentLightModel.ToString() + Constants.CustomLightStructStr + "_GI(" + outputStruct + " " + Constants.CustomLightOutputVarStr + ", UnityGIInput data, inout UnityGI gi )\n\t\t{\n";
  1934. ShaderBody += "\t\t\t#if defined(UNITY_PASS_DEFERRED) && UNITY_ENABLE_REFLECTION_BUFFERS\n";
  1935. ShaderBody += "\t\t\t\tgi = UnityGlobalIllumination(data, " + Constants.CustomLightOutputVarStr + ".Occlusion, " + Constants.CustomLightOutputVarStr + ".Normal);\n";
  1936. ShaderBody += "\t\t\t#else\n";
  1937. ShaderBody += "\t\t\t\tUNITY_GLOSSY_ENV_FROM_SURFACE( g, " + Constants.CustomLightOutputVarStr + ", data );\n";
  1938. ShaderBody += "\t\t\t\tgi = UnityGlobalIllumination( data, " + Constants.CustomLightOutputVarStr + ".Occlusion, " + Constants.CustomLightOutputVarStr + ".Normal, g );\n";
  1939. ShaderBody += "\t\t\t#endif\n";
  1940. //ShaderBody += "\t\t\tUNITY_GI(gi, " + Constants.CustomLightOutputVarStr + ", data);\n";
  1941. ShaderBody += "\t\t}\n\n";
  1942. }
  1943. if( hasRefraction )
  1944. {
  1945. ShaderBody += "\t\tinline float4 Refraction( Input " + Constants.InputVarStr + ", " + outputStruct + " " + Constants.OutputVarStr + ", float indexOfRefraction, float chomaticAberration ) {\n";
  1946. ShaderBody += "\t\t\tfloat3 worldNormal = " + Constants.OutputVarStr + ".Normal;\n";
  1947. ShaderBody += "\t\t\tfloat4 screenPos = " + Constants.InputVarStr + ".screenPos;\n";
  1948. ShaderBody += "\t\t\t#if UNITY_UV_STARTS_AT_TOP\n";
  1949. ShaderBody += "\t\t\t\tfloat scale = -1.0;\n";
  1950. ShaderBody += "\t\t\t#else\n";
  1951. ShaderBody += "\t\t\t\tfloat scale = 1.0;\n";
  1952. ShaderBody += "\t\t\t#endif\n";
  1953. ShaderBody += "\t\t\tfloat halfPosW = screenPos.w * 0.5;\n";
  1954. ShaderBody += "\t\t\tscreenPos.y = ( screenPos.y - halfPosW ) * _ProjectionParams.x * scale + halfPosW;\n";
  1955. ShaderBody += "\t\t\t#if SHADER_API_D3D9 || SHADER_API_D3D11\n";
  1956. ShaderBody += "\t\t\t\tscreenPos.w += 0.00000000001;\n";
  1957. ShaderBody += "\t\t\t#endif\n";
  1958. ShaderBody += "\t\t\tfloat2 projScreenPos = ( screenPos / screenPos.w ).xy;\n";
  1959. ShaderBody += "\t\t\tfloat3 worldViewDir = normalize( UnityWorldSpaceViewDir( " + Constants.InputVarStr + ".worldPos ) );\n";
  1960. ShaderBody += "\t\t\tfloat3 refractionOffset = ( indexOfRefraction - 1.0 ) * mul( UNITY_MATRIX_V, float4( worldNormal, 0.0 ) ) * ( 1.0 - dot( worldNormal, worldViewDir ) );\n";
  1961. ShaderBody += "\t\t\tfloat2 cameraRefraction = float2( refractionOffset.x, refractionOffset.y );\n";
  1962. string grabpass = "_GrabTexture";
  1963. if( m_grabOrder != 0 )
  1964. grabpass = "RefractionGrab" + m_grabOrder;
  1965. ShaderBody += "\t\t\tfloat4 redAlpha = tex2D( " + grabpass + ", ( projScreenPos + cameraRefraction ) );\n";
  1966. ShaderBody += "\t\t\tfloat green = tex2D( " + grabpass + ", ( projScreenPos + ( cameraRefraction * ( 1.0 - chomaticAberration ) ) ) ).g;\n";
  1967. ShaderBody += "\t\t\tfloat blue = tex2D( " + grabpass + ", ( projScreenPos + ( cameraRefraction * ( 1.0 + chomaticAberration ) ) ) ).b;\n";
  1968. ShaderBody += "\t\t\treturn float4( redAlpha.r, green, blue, redAlpha.a );\n";
  1969. ShaderBody += "\t\t}\n\n";
  1970. ShaderBody += "\t\tvoid RefractionF( Input " + Constants.InputVarStr + ", " + outputStruct + " " + Constants.OutputVarStr + ", inout half4 color )\n";
  1971. ShaderBody += "\t\t{\n";
  1972. ShaderBody += "\t\t\t#ifdef UNITY_PASS_FORWARDBASE\n";
  1973. ShaderBody += refractionInstructions;
  1974. if( m_inlineChromaticAberration.Active )
  1975. {
  1976. ShaderBody += "\t\t\tcolor.rgb = color.rgb + Refraction( " + Constants.InputVarStr + ", " + Constants.OutputVarStr + ", " + refractionCode + ", " + m_inlineChromaticAberration.GetValueOrProperty(false) + " ) * ( 1 - color.a );\n";
  1977. }
  1978. else
  1979. {
  1980. ShaderBody += "\t\t\tcolor.rgb = color.rgb + Refraction( " + Constants.InputVarStr + ", " + Constants.OutputVarStr + ", " + refractionCode + ", _ChromaticAberration ) * ( 1 - color.a );\n";
  1981. }
  1982. ShaderBody += "\t\t\tcolor.a = 1;\n";
  1983. ShaderBody += "\t\t\t#endif\n";
  1984. ShaderBody += "\t\t}\n\n";
  1985. }
  1986. //Add Surface Shader body
  1987. ShaderBody += "\t\tvoid surf( Input " + Constants.InputVarStr + " , inout " + outputStruct + " " + Constants.OutputVarStr + " )\n\t\t{\n";
  1988. {
  1989. // Pass input information to custom lighting function
  1990. if( m_currentLightModel == StandardShaderLightModel.CustomLighting )
  1991. ShaderBody += "\t\t\t" + Constants.OutputVarStr + ".SurfInput = " + Constants.InputVarStr + ";\n";
  1992. //add local vars
  1993. if( m_currentDataCollector.DirtyLocalVariables )
  1994. ShaderBody += m_currentDataCollector.LocalVariables;
  1995. //add nodes ops
  1996. if( m_currentDataCollector.DirtyInstructions )
  1997. ShaderBody += m_currentDataCollector.Instructions;
  1998. }
  1999. ShaderBody += "\t\t}\n";
  2000. }
  2001. CloseCGProgram( ref ShaderBody );
  2002. //Add custom Shadow Caster
  2003. if( m_customShadowCaster )
  2004. {
  2005. OpenCGProgram( ref ShaderBody );
  2006. string customLightSurface = hasTranslucency || hasTransmission ? "Custom" : "";
  2007. m_renderingPlatformOpHelper.SetRenderingPlatforms( ref ShaderBody );
  2008. //Check if Custom Vertex is being used and add tag
  2009. if( m_currentDataCollector.DirtyPerVertexData )
  2010. OptionalParameters += "vertex:" + Constants.VertexDataFunc + Constants.OptionalParametersSep;
  2011. if( m_tessOpHelper.EnableTesselation && !usingDebugPort )
  2012. {
  2013. m_tessOpHelper.WriteToOptionalParams( ref OptionalParameters );
  2014. }
  2015. //if ( hasRefraction )
  2016. // ShaderBody += "\t\t#pragma multi_compile _ALPHAPREMULTIPLY_ON\n";
  2017. m_additionalSurfaceOptions.WriteToOptionalSurfaceOptions( ref OptionalParameters );
  2018. AddShaderPragma( ref ShaderBody, "surface surf " + standardCustomLighting + m_currentLightModel.ToString() + customLightSurface + Constants.OptionalParametersSep + OptionalParameters );
  2019. CloseCGProgram( ref ShaderBody );
  2020. ShaderBody += "\t\tPass\n";
  2021. ShaderBody += "\t\t{\n";
  2022. ShaderBody += "\t\t\tName \"ShadowCaster\"\n";
  2023. ShaderBody += "\t\t\tTags{ \"LightMode\" = \"ShadowCaster\" }\n";
  2024. ShaderBody += "\t\t\tZWrite On\n";
  2025. if( m_alphaToCoverage || m_inlineAlphaToCoverage.Active )
  2026. ShaderBody += "\t\t\tAlphaToMask Off\n";
  2027. ShaderBody += "\t\t\tCGPROGRAM\n";
  2028. ShaderBody += "\t\t\t#pragma vertex vert\n";
  2029. ShaderBody += "\t\t\t#pragma fragment frag\n";
  2030. ShaderBody += "\t\t\t#pragma target " + ShaderModelTypeArr[ m_shaderModelIdx ] + "\n";
  2031. //ShaderBody += "\t\t\t#pragma multi_compile_instancing\n";
  2032. ShaderBody += "\t\t\t#pragma multi_compile_shadowcaster\n";
  2033. ShaderBody += "\t\t\t#pragma multi_compile UNITY_PASS_SHADOWCASTER\n";
  2034. ShaderBody += "\t\t\t#pragma skip_variants FOG_LINEAR FOG_EXP FOG_EXP2\n";
  2035. ShaderBody += "\t\t\t#include \"HLSLSupport.cginc\"\n";
  2036. //Preventing WebGL to throw error Duplicate system value semantic definition: input semantic 'SV_POSITION' and input semantic 'VPOS'
  2037. ShaderBody += "\t\t\t#if ( SHADER_API_D3D11 || SHADER_API_GLCORE || SHADER_API_GLES || SHADER_API_GLES3 || SHADER_API_METAL || SHADER_API_VULKAN )\n";
  2038. ShaderBody += "\t\t\t\t#define CAN_SKIP_VPOS\n";
  2039. ShaderBody += "\t\t\t#endif\n";
  2040. ShaderBody += "\t\t\t#include \"UnityCG.cginc\"\n";
  2041. ShaderBody += "\t\t\t#include \"Lighting.cginc\"\n";
  2042. ShaderBody += "\t\t\t#include \"UnityPBSLighting.cginc\"\n";
  2043. if( !( ( m_alphaToCoverage || m_inlineAlphaToCoverage.Active ) && hasOpacity && hasOpacityMask ) )
  2044. if( hasOpacity )
  2045. ShaderBody += "\t\t\tsampler3D _DitherMaskLOD;\n";
  2046. //ShaderBody += "\t\t\tsampler3D _DitherMaskLOD;\n";
  2047. ShaderBody += "\t\t\tstruct v2f\n";
  2048. ShaderBody += "\t\t\t{\n";
  2049. ShaderBody += "\t\t\t\tV2F_SHADOW_CASTER;\n";
  2050. int texcoordIndex = 1;
  2051. for( int i = 0; i < m_currentDataCollector.PackSlotsList.Count; i++ )
  2052. {
  2053. int size = 4 - m_currentDataCollector.PackSlotsList[ i ];
  2054. if( size > 0 )
  2055. {
  2056. ShaderBody += "\t\t\t\tfloat" + size + " customPack" + ( i + 1 ) + " : TEXCOORD" + ( i + 1 ) + ";\n";
  2057. }
  2058. texcoordIndex++;
  2059. }
  2060. if( !m_currentDataCollector.UsingInternalData )
  2061. ShaderBody += "\t\t\t\tfloat3 worldPos : TEXCOORD" + ( texcoordIndex++ ) + ";\n";
  2062. if( m_currentDataCollector.UsingScreenPos )
  2063. ShaderBody += "\t\t\t\tfloat4 screenPos : TEXCOORD" + ( texcoordIndex++ ) + ";\n";
  2064. if( /*m_currentDataCollector.UsingWorldNormal || m_currentDataCollector.UsingWorldPosition ||*/ m_currentDataCollector.UsingInternalData || m_currentDataCollector.DirtyNormal )
  2065. {
  2066. ShaderBody += "\t\t\t\tfloat4 tSpace0 : TEXCOORD" + ( texcoordIndex++ ) + ";\n";
  2067. ShaderBody += "\t\t\t\tfloat4 tSpace1 : TEXCOORD" + ( texcoordIndex++ ) + ";\n";
  2068. ShaderBody += "\t\t\t\tfloat4 tSpace2 : TEXCOORD" + ( texcoordIndex++ ) + ";\n";
  2069. }
  2070. else if( !m_currentDataCollector.UsingInternalData && m_currentDataCollector.UsingWorldNormal )
  2071. {
  2072. ShaderBody += "\t\t\t\tfloat3 worldNormal : TEXCOORD" + ( texcoordIndex++ ) + ";\n";
  2073. }
  2074. if( m_currentDataCollector.UsingVertexColor )
  2075. ShaderBody += "\t\t\t\thalf4 color : COLOR0;\n";
  2076. ShaderBody += "\t\t\t\tUNITY_VERTEX_INPUT_INSTANCE_ID\n";
  2077. ShaderBody += "\t\t\t\tUNITY_VERTEX_OUTPUT_STEREO\n";
  2078. ShaderBody += "\t\t\t};\n";
  2079. ShaderBody += "\t\t\tv2f vert( " + m_currentDataCollector.SurfaceVertexStructure + " v )\n";
  2080. ShaderBody += "\t\t\t{\n";
  2081. ShaderBody += "\t\t\t\tv2f o;\n";
  2082. ShaderBody += "\t\t\t\tUNITY_SETUP_INSTANCE_ID( v );\n";
  2083. ShaderBody += "\t\t\t\tUNITY_INITIALIZE_OUTPUT( v2f, o );\n";
  2084. ShaderBody += "\t\t\t\tUNITY_INITIALIZE_VERTEX_OUTPUT_STEREO( o );\n";
  2085. ShaderBody += "\t\t\t\tUNITY_TRANSFER_INSTANCE_ID( v, o );\n";
  2086. if( m_currentDataCollector.DirtyPerVertexData || m_currentDataCollector.CustomShadowCoordsList.Count > 0 )
  2087. ShaderBody += "\t\t\t\tInput customInputData;\n";
  2088. if( m_currentDataCollector.DirtyPerVertexData )
  2089. {
  2090. ShaderBody += "\t\t\t\tvertexDataFunc( v" + ( m_currentDataCollector.TesselationActive ? "" : ", customInputData" ) + " );\n";
  2091. }
  2092. ShaderBody += "\t\t\t\tfloat3 worldPos = mul( unity_ObjectToWorld, v.vertex ).xyz;\n";
  2093. ShaderBody += "\t\t\t\thalf3 worldNormal = UnityObjectToWorldNormal( v.normal );\n";
  2094. if( m_currentDataCollector.UsingInternalData || m_currentDataCollector.DirtyNormal )
  2095. {
  2096. ShaderBody += "\t\t\t\thalf3 worldTangent = UnityObjectToWorldDir( v.tangent.xyz );\n";
  2097. ShaderBody += "\t\t\t\thalf tangentSign = v.tangent.w * unity_WorldTransformParams.w;\n";
  2098. ShaderBody += "\t\t\t\thalf3 worldBinormal = cross( worldNormal, worldTangent ) * tangentSign;\n";
  2099. ShaderBody += "\t\t\t\to.tSpace0 = float4( worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x );\n";
  2100. ShaderBody += "\t\t\t\to.tSpace1 = float4( worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y );\n";
  2101. ShaderBody += "\t\t\t\to.tSpace2 = float4( worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z );\n";
  2102. }
  2103. else if( !m_currentDataCollector.UsingInternalData && m_currentDataCollector.UsingWorldNormal )
  2104. {
  2105. ShaderBody += "\t\t\t\to.worldNormal = worldNormal;\n";
  2106. }
  2107. for( int i = 0; i < m_currentDataCollector.CustomShadowCoordsList.Count; i++ )
  2108. {
  2109. int size = UIUtils.GetChannelsAmount( m_currentDataCollector.CustomShadowCoordsList[ i ].DataType );
  2110. string channels = string.Empty;
  2111. for( int j = 0; j < size; j++ )
  2112. {
  2113. channels += Convert.ToChar( 120 + m_currentDataCollector.CustomShadowCoordsList[ i ].TextureIndex + j );
  2114. }
  2115. channels = channels.Replace( '{', 'w' );
  2116. ShaderBody += "\t\t\t\to.customPack" + ( m_currentDataCollector.CustomShadowCoordsList[ i ].TextureSlot + 1 ) + "." + channels + " = customInputData." + m_currentDataCollector.CustomShadowCoordsList[ i ].CoordName + ";\n";
  2117. //TODO: TEMPORARY SOLUTION, this needs to go somewhere else, there's no need for these comparisons
  2118. if( m_currentDataCollector.CustomShadowCoordsList[ i ].CoordName.StartsWith( "uv_" ) )
  2119. {
  2120. ShaderBody += "\t\t\t\to.customPack" + ( m_currentDataCollector.CustomShadowCoordsList[ i ].TextureSlot + 1 ) + "." + channels + " = v.texcoord;\n";
  2121. }
  2122. else if( m_currentDataCollector.CustomShadowCoordsList[ i ].CoordName.StartsWith( "uv2_" ) )
  2123. {
  2124. ShaderBody += "\t\t\t\to.customPack" + ( m_currentDataCollector.CustomShadowCoordsList[ i ].TextureSlot + 1 ) + "." + channels + " = v.texcoord1;\n";
  2125. }
  2126. else if( m_currentDataCollector.CustomShadowCoordsList[ i ].CoordName.StartsWith( "uv3_" ) )
  2127. {
  2128. ShaderBody += "\t\t\t\to.customPack" + ( m_currentDataCollector.CustomShadowCoordsList[ i ].TextureSlot + 1 ) + "." + channels + " = v.texcoord2;\n";
  2129. }
  2130. else if( m_currentDataCollector.CustomShadowCoordsList[ i ].CoordName.StartsWith( "uv4_" ) )
  2131. {
  2132. ShaderBody += "\t\t\t\to.customPack" + ( m_currentDataCollector.CustomShadowCoordsList[ i ].TextureSlot + 1 ) + "." + channels + " = v.texcoord3;\n";
  2133. }
  2134. }
  2135. if( !m_currentDataCollector.UsingInternalData )
  2136. ShaderBody += "\t\t\t\to.worldPos = worldPos;\n";
  2137. ShaderBody += "\t\t\t\tTRANSFER_SHADOW_CASTER_NORMALOFFSET( o )\n";
  2138. if( m_currentDataCollector.UsingScreenPos )
  2139. ShaderBody += "\t\t\t\to.screenPos = ComputeScreenPos( o.pos );\n";
  2140. if( m_currentDataCollector.UsingVertexColor )
  2141. ShaderBody += "\t\t\t\to.color = v.color;\n";
  2142. ShaderBody += "\t\t\t\treturn o;\n";
  2143. ShaderBody += "\t\t\t}\n";
  2144. ShaderBody += "\t\t\thalf4 frag( v2f IN\n";
  2145. ShaderBody += "\t\t\t#if !defined( CAN_SKIP_VPOS )\n";
  2146. ShaderBody += "\t\t\t, UNITY_VPOS_TYPE vpos : VPOS\n";
  2147. ShaderBody += "\t\t\t#endif\n";
  2148. ShaderBody += "\t\t\t) : SV_Target\n";
  2149. ShaderBody += "\t\t\t{\n";
  2150. ShaderBody += "\t\t\t\tUNITY_SETUP_INSTANCE_ID( IN );\n";
  2151. ShaderBody += "\t\t\t\tInput surfIN;\n";
  2152. ShaderBody += "\t\t\t\tUNITY_INITIALIZE_OUTPUT( Input, surfIN );\n";
  2153. for( int i = 0; i < m_currentDataCollector.CustomShadowCoordsList.Count; i++ )
  2154. {
  2155. int size = UIUtils.GetChannelsAmount( m_currentDataCollector.CustomShadowCoordsList[ i ].DataType );
  2156. string channels = string.Empty;
  2157. for( int j = 0; j < size; j++ )
  2158. {
  2159. channels += Convert.ToChar( 120 + m_currentDataCollector.CustomShadowCoordsList[ i ].TextureIndex + j );
  2160. }
  2161. channels = channels.Replace( '{', 'w' );
  2162. ShaderBody += "\t\t\t\tsurfIN." + m_currentDataCollector.CustomShadowCoordsList[ i ].CoordName + " = IN.customPack" + ( m_currentDataCollector.CustomShadowCoordsList[ i ].TextureSlot + 1 ) + "." + channels + ";\n";
  2163. }
  2164. if( m_currentDataCollector.UsingInternalData )
  2165. ShaderBody += "\t\t\t\tfloat3 worldPos = float3( IN.tSpace0.w, IN.tSpace1.w, IN.tSpace2.w );\n";
  2166. else
  2167. ShaderBody += "\t\t\t\tfloat3 worldPos = IN.worldPos;\n";
  2168. ShaderBody += "\t\t\t\thalf3 worldViewDir = normalize( UnityWorldSpaceViewDir( worldPos ) );\n";
  2169. if( m_currentDataCollector.UsingViewDirection && !m_currentDataCollector.DirtyNormal )
  2170. ShaderBody += "\t\t\t\tsurfIN.viewDir = worldViewDir;\n";
  2171. else if( m_currentDataCollector.UsingViewDirection )
  2172. ShaderBody += "\t\t\t\tsurfIN.viewDir = IN.tSpace0.xyz * worldViewDir.x + IN.tSpace1.xyz * worldViewDir.y + IN.tSpace2.xyz * worldViewDir.z;\n";
  2173. if( m_currentDataCollector.UsingWorldPosition )
  2174. ShaderBody += "\t\t\t\tsurfIN.worldPos = worldPos;\n";
  2175. if( m_currentDataCollector.UsingWorldNormal && m_currentDataCollector.UsingInternalData )
  2176. ShaderBody += "\t\t\t\tsurfIN.worldNormal = float3( IN.tSpace0.z, IN.tSpace1.z, IN.tSpace2.z );\n";
  2177. else if( !m_currentDataCollector.UsingInternalData && m_currentDataCollector.UsingWorldNormal )
  2178. ShaderBody += "\t\t\t\tsurfIN.worldNormal = IN.worldNormal;\n";
  2179. if( m_currentDataCollector.UsingWorldReflection )
  2180. ShaderBody += "\t\t\t\tsurfIN.worldRefl = -worldViewDir;\n";
  2181. if( m_currentDataCollector.UsingInternalData )
  2182. {
  2183. ShaderBody += "\t\t\t\tsurfIN.internalSurfaceTtoW0 = IN.tSpace0.xyz;\n";
  2184. ShaderBody += "\t\t\t\tsurfIN.internalSurfaceTtoW1 = IN.tSpace1.xyz;\n";
  2185. ShaderBody += "\t\t\t\tsurfIN.internalSurfaceTtoW2 = IN.tSpace2.xyz;\n";
  2186. }
  2187. if( m_currentDataCollector.UsingScreenPos )
  2188. ShaderBody += "\t\t\t\tsurfIN.screenPos = IN.screenPos;\n";
  2189. if( m_currentDataCollector.UsingVertexColor )
  2190. ShaderBody += "\t\t\t\tsurfIN.vertexColor = IN.color;\n";
  2191. ShaderBody += "\t\t\t\t" + outputStruct + " o;\n";
  2192. ShaderBody += "\t\t\t\tUNITY_INITIALIZE_OUTPUT( " + outputStruct + ", o )\n";
  2193. ShaderBody += "\t\t\t\tsurf( surfIN, o );\n";
  2194. if( ( hasOpacity || hasOpacityMask ) && m_currentLightModel == StandardShaderLightModel.CustomLighting )
  2195. {
  2196. ShaderBody += "\t\t\t\tUnityGI gi;\n";
  2197. ShaderBody += "\t\t\t\tUNITY_INITIALIZE_OUTPUT( UnityGI, gi );\n";
  2198. ShaderBody += "\t\t\t\to.Alpha = LightingStandardCustomLighting( o, worldViewDir, gi ).a;\n";
  2199. }
  2200. ShaderBody += "\t\t\t\t#if defined( CAN_SKIP_VPOS )\n";
  2201. ShaderBody += "\t\t\t\tfloat2 vpos = IN.pos;\n";
  2202. ShaderBody += "\t\t\t\t#endif\n";
  2203. /*if( ( ( m_alphaToCoverage || m_inlineAlphaToCoverage.Active ) && hasOpacity && m_inputPorts[ m_discardPortId ].IsConnected ) )
  2204. {
  2205. }
  2206. else*/ if(!( ( m_alphaToCoverage || m_inlineAlphaToCoverage.Active ) && hasOpacity && m_inputPorts[ m_discardPortId ].IsConnected ) && hasOpacity )
  2207. {
  2208. ShaderBody += "\t\t\t\thalf alphaRef = tex3D( _DitherMaskLOD, float3( vpos.xy * 0.25, o.Alpha * 0.9375 ) ).a;\n";
  2209. ShaderBody += "\t\t\t\tclip( alphaRef - 0.01 );\n";
  2210. }
  2211. ShaderBody += "\t\t\t\tSHADOW_CASTER_FRAGMENT( IN )\n";
  2212. ShaderBody += "\t\t\t}\n";
  2213. ShaderBody += "\t\t\tENDCG\n";
  2214. ShaderBody += "\t\t}\n";
  2215. }
  2216. }
  2217. if( !string.IsNullOrEmpty( bellowUsePasses ) )
  2218. {
  2219. ShaderBody += bellowUsePasses;
  2220. }
  2221. CloseSubShaderBody( ref ShaderBody );
  2222. if( m_dependenciesHelper.HasDependencies )
  2223. {
  2224. ShaderBody += m_dependenciesHelper.GenerateDependencies();
  2225. }
  2226. if( m_fallbackHelper.Active )
  2227. {
  2228. ShaderBody += m_fallbackHelper.TabbedFallbackShader;
  2229. }
  2230. else if( m_castShadows || m_receiveShadows )
  2231. {
  2232. AddShaderProperty( ref ShaderBody, "Fallback", "Diffuse" );
  2233. }
  2234. if( !string.IsNullOrEmpty( m_customInspectorName ) )
  2235. {
  2236. AddShaderProperty( ref ShaderBody, "CustomEditor", m_customInspectorName );
  2237. }
  2238. }
  2239. CloseShaderBody( ref ShaderBody );
  2240. if( usingDebugPort )
  2241. {
  2242. m_currentLightModel = cachedLightModel;
  2243. ContainerGraph.CurrentCanvasMode = cachedAvailability;
  2244. }
  2245. // Generate Graph info
  2246. ShaderBody += ContainerGraph.ParentWindow.GenerateGraphInfo();
  2247. //TODO: Remove current SaveDebugShader and uncomment SaveToDisk as soon as pathname is editable
  2248. if( !String.IsNullOrEmpty( pathname ) )
  2249. {
  2250. IOUtils.StartSaveThread( ShaderBody, ( isFullPath ? pathname : ( IOUtils.dataPath + pathname ) ) );
  2251. }
  2252. else
  2253. {
  2254. IOUtils.StartSaveThread( ShaderBody, Application.dataPath + "/AmplifyShaderEditor/Samples/Shaders/" + m_shaderName + ".shader" );
  2255. }
  2256. // Load new shader into material
  2257. if( CurrentShader == null )
  2258. {
  2259. AssetDatabase.Refresh( ImportAssetOptions.ForceUpdate );
  2260. CurrentShader = Shader.Find( ShaderName );
  2261. }
  2262. //else
  2263. //{
  2264. // // need to always get asset datapath because a user can change and asset location from the project window
  2265. // AssetDatabase.ImportAsset( AssetDatabase.GetAssetPath( m_currentShader ) );
  2266. // //ShaderUtil.UpdateShaderAsset( m_currentShader, ShaderBody );
  2267. //}
  2268. if( m_currentShader != null )
  2269. {
  2270. m_currentDataCollector.UpdateShaderImporter( ref m_currentShader );
  2271. if( m_currentMaterial != null )
  2272. {
  2273. if( m_currentShader != m_currentMaterial.shader )
  2274. m_currentMaterial.shader = m_currentShader;
  2275. if ( isInstancedShader )
  2276. {
  2277. m_currentMaterial.enableInstancing = true;
  2278. }
  2279. m_currentDataCollector.UpdateMaterialOnPropertyNodes( m_currentMaterial );
  2280. UpdateMaterialEditor();
  2281. // need to always get asset datapath because a user can change and asset location from the project window
  2282. //AssetDatabase.ImportAsset( AssetDatabase.GetAssetPath( m_currentMaterial ) );
  2283. }
  2284. }
  2285. m_currentDataCollector.Destroy();
  2286. m_currentDataCollector = null;
  2287. return m_currentShader;
  2288. }
  2289. public override void UpdateFromShader( Shader newShader )
  2290. {
  2291. if( m_currentMaterial != null && m_currentMaterial.shader != newShader )
  2292. {
  2293. m_currentMaterial.shader = newShader;
  2294. }
  2295. CurrentShader = newShader;
  2296. }
  2297. public override void Destroy()
  2298. {
  2299. base.Destroy();
  2300. if( m_dummyProperty != null )
  2301. {
  2302. m_dummyProperty.Destroy();
  2303. GameObject.DestroyImmediate( m_dummyProperty );
  2304. m_dummyProperty = null;
  2305. }
  2306. m_drawInstancedHelper = null;
  2307. m_translucencyPort = null;
  2308. m_transmissionPort = null;
  2309. m_refractionPort = null;
  2310. m_normalPort = null;
  2311. m_renderingOptionsOpHelper.Destroy();
  2312. m_renderingOptionsOpHelper = null;
  2313. m_additionalIncludes.Destroy();
  2314. m_additionalIncludes = null;
  2315. m_additionalPragmas.Destroy();
  2316. m_additionalPragmas = null;
  2317. m_additionalDefines.Destroy();
  2318. m_additionalDefines = null;
  2319. m_additionalSurfaceOptions.Destroy();
  2320. m_additionalSurfaceOptions = null;
  2321. m_additionalDirectives.Destroy();
  2322. m_additionalDirectives = null;
  2323. m_customTagsHelper.Destroy();
  2324. m_customTagsHelper = null;
  2325. m_dependenciesHelper.Destroy();
  2326. m_dependenciesHelper = null;
  2327. m_renderingPlatformOpHelper = null;
  2328. m_inspectorDefaultStyle = null;
  2329. m_inspectorFoldoutStyle = null;
  2330. m_zBufferHelper = null;
  2331. m_stencilBufferHelper = null;
  2332. m_blendOpsHelper = null;
  2333. m_tessOpHelper.Destroy();
  2334. m_tessOpHelper = null;
  2335. m_outlineHelper.Destroy();
  2336. m_outlineHelper = null;
  2337. m_colorMaskHelper.Destroy();
  2338. m_colorMaskHelper = null;
  2339. m_billboardOpHelper = null;
  2340. m_fallbackHelper.Destroy();
  2341. GameObject.DestroyImmediate( m_fallbackHelper );
  2342. m_fallbackHelper = null;
  2343. m_usePass.Destroy();
  2344. GameObject.DestroyImmediate( m_usePass );
  2345. m_usePass = null;
  2346. }
  2347. public override int VersionConvertInputPortId( int portId )
  2348. {
  2349. int newPort = portId;
  2350. //added translucency input after occlusion
  2351. if( UIUtils.CurrentShaderVersion() <= 2404 )
  2352. {
  2353. switch( m_currentLightModel )
  2354. {
  2355. case StandardShaderLightModel.Standard:
  2356. case StandardShaderLightModel.StandardSpecular:
  2357. if( portId >= 6 )
  2358. newPort += 1;
  2359. break;
  2360. case StandardShaderLightModel.CustomLighting:
  2361. case StandardShaderLightModel.Unlit:
  2362. case StandardShaderLightModel.Lambert:
  2363. case StandardShaderLightModel.BlinnPhong:
  2364. if( portId >= 5 )
  2365. newPort += 1;
  2366. break;
  2367. }
  2368. }
  2369. portId = newPort;
  2370. //added transmission input after occlusion
  2371. if( UIUtils.CurrentShaderVersion() < 2407 )
  2372. {
  2373. switch( m_currentLightModel )
  2374. {
  2375. case StandardShaderLightModel.Standard:
  2376. case StandardShaderLightModel.StandardSpecular:
  2377. if( portId >= 6 )
  2378. newPort += 1;
  2379. break;
  2380. case StandardShaderLightModel.CustomLighting:
  2381. case StandardShaderLightModel.Unlit:
  2382. case StandardShaderLightModel.Lambert:
  2383. case StandardShaderLightModel.BlinnPhong:
  2384. if( portId >= 5 )
  2385. newPort += 1;
  2386. break;
  2387. }
  2388. }
  2389. portId = newPort;
  2390. //added tessellation ports
  2391. if( UIUtils.CurrentShaderVersion() < 3002 )
  2392. {
  2393. switch( m_currentLightModel )
  2394. {
  2395. case StandardShaderLightModel.Standard:
  2396. case StandardShaderLightModel.StandardSpecular:
  2397. if( portId >= 13 )
  2398. newPort += 1;
  2399. break;
  2400. case StandardShaderLightModel.CustomLighting:
  2401. case StandardShaderLightModel.Unlit:
  2402. case StandardShaderLightModel.Lambert:
  2403. case StandardShaderLightModel.BlinnPhong:
  2404. if( portId >= 10 )
  2405. newPort += 1;
  2406. break;
  2407. }
  2408. }
  2409. portId = newPort;
  2410. //added refraction after translucency
  2411. if( UIUtils.CurrentShaderVersion() < 3204 )
  2412. {
  2413. switch( m_currentLightModel )
  2414. {
  2415. case StandardShaderLightModel.Standard:
  2416. case StandardShaderLightModel.StandardSpecular:
  2417. if( portId >= 8 )
  2418. newPort += 1;
  2419. break;
  2420. case StandardShaderLightModel.CustomLighting:
  2421. case StandardShaderLightModel.Unlit:
  2422. case StandardShaderLightModel.Lambert:
  2423. case StandardShaderLightModel.BlinnPhong:
  2424. if( portId >= 7 )
  2425. newPort += 1;
  2426. break;
  2427. }
  2428. }
  2429. portId = newPort;
  2430. //removed custom lighting port
  2431. //if ( UIUtils.CurrentShaderVersion() < 10003 ) //runs everytime because this system is only used after 5000 version
  2432. {
  2433. switch( m_currentLightModel )
  2434. {
  2435. case StandardShaderLightModel.Standard:
  2436. case StandardShaderLightModel.StandardSpecular:
  2437. if( portId >= 13 )
  2438. newPort -= 1;
  2439. break;
  2440. case StandardShaderLightModel.CustomLighting:
  2441. case StandardShaderLightModel.Unlit:
  2442. case StandardShaderLightModel.Lambert:
  2443. case StandardShaderLightModel.BlinnPhong:
  2444. if( portId >= 12 )
  2445. newPort -= 1;
  2446. break;
  2447. }
  2448. }
  2449. portId = newPort;
  2450. //if( UIUtils.CurrentShaderVersion() < 13802 ) //runs everytime because this system is only used after 5000 version
  2451. {
  2452. switch( m_currentLightModel )
  2453. {
  2454. case StandardShaderLightModel.Standard:
  2455. case StandardShaderLightModel.StandardSpecular:
  2456. if( portId >= 11 )
  2457. newPort += 1;
  2458. break;
  2459. case StandardShaderLightModel.CustomLighting:
  2460. case StandardShaderLightModel.Unlit:
  2461. case StandardShaderLightModel.Lambert:
  2462. case StandardShaderLightModel.BlinnPhong:
  2463. if( portId >= 10 )
  2464. newPort += 1;
  2465. break;
  2466. }
  2467. }
  2468. portId = newPort;
  2469. return newPort;
  2470. }
  2471. public override void ReadFromString( ref string[] nodeParams )
  2472. {
  2473. try
  2474. {
  2475. base.ReadFromString( ref nodeParams );
  2476. m_currentLightModel = (StandardShaderLightModel)Enum.Parse( typeof( StandardShaderLightModel ), GetCurrentParam( ref nodeParams ) );
  2477. if( CurrentMasterNodeCategory == AvailableShaderTypes.SurfaceShader && m_currentLightModel == StandardShaderLightModel.CustomLighting )
  2478. {
  2479. ContainerGraph.CurrentCanvasMode = NodeAvailability.CustomLighting;
  2480. ContainerGraph.ParentWindow.CurrentNodeAvailability = NodeAvailability.CustomLighting;
  2481. }
  2482. else if( CurrentMasterNodeCategory == AvailableShaderTypes.SurfaceShader )
  2483. {
  2484. ContainerGraph.CurrentCanvasMode = NodeAvailability.SurfaceShader;
  2485. ContainerGraph.ParentWindow.CurrentNodeAvailability = NodeAvailability.SurfaceShader;
  2486. }
  2487. //if ( _shaderCategory.Length > 0 )
  2488. // _shaderCategory = UIUtils.RemoveInvalidCharacters( _shaderCategory );
  2489. ShaderName = GetCurrentParam( ref nodeParams );
  2490. if( m_shaderName.Length > 0 )
  2491. ShaderName = UIUtils.RemoveShaderInvalidCharacters( ShaderName );
  2492. m_renderingOptionsOpHelper.ReadFromString( ref m_currentReadParamIdx, ref nodeParams );
  2493. m_cullMode = (CullMode)Enum.Parse( typeof( CullMode ), GetCurrentParam( ref nodeParams ) );
  2494. m_zBufferHelper.ReadFromString( ref m_currentReadParamIdx, ref nodeParams );
  2495. string alphaMode = GetCurrentParam( ref nodeParams );
  2496. if( UIUtils.CurrentShaderVersion() < 4003 )
  2497. {
  2498. if( alphaMode.Equals( "Fade" ) )
  2499. {
  2500. alphaMode = "Transparent";
  2501. }
  2502. else if( alphaMode.Equals( "Transparent" ) )
  2503. {
  2504. alphaMode = "Premultiply";
  2505. }
  2506. }
  2507. m_alphaMode = (AlphaMode)Enum.Parse( typeof( AlphaMode ), alphaMode );
  2508. m_opacityMaskClipValue = Convert.ToSingle( GetCurrentParam( ref nodeParams ) );
  2509. m_keepAlpha = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  2510. m_keepAlpha = true;
  2511. m_castShadows = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  2512. m_queueOrder = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  2513. if( UIUtils.CurrentShaderVersion() > 11 )
  2514. {
  2515. m_customBlendMode = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  2516. m_renderType = (RenderType)Enum.Parse( typeof( RenderType ), GetCurrentParam( ref nodeParams ) );
  2517. if( UIUtils.CurrentShaderVersion() > 14305 )
  2518. {
  2519. m_customRenderType = GetCurrentParam( ref nodeParams );
  2520. }
  2521. m_renderQueue = (RenderQueue)Enum.Parse( typeof( RenderQueue ), GetCurrentParam( ref nodeParams ) );
  2522. }
  2523. if( UIUtils.CurrentShaderVersion() > 2402 )
  2524. {
  2525. m_renderPath = (RenderPath)Enum.Parse( typeof( RenderPath ), GetCurrentParam( ref nodeParams ) );
  2526. }
  2527. if( UIUtils.CurrentShaderVersion() > 2405 )
  2528. {
  2529. m_renderingPlatformOpHelper.ReadFromString( ref m_currentReadParamIdx, ref nodeParams );
  2530. }
  2531. if( UIUtils.CurrentShaderVersion() > 2500 )
  2532. {
  2533. m_colorMaskHelper.ReadFromString( ref m_currentReadParamIdx, ref nodeParams );
  2534. }
  2535. if( UIUtils.CurrentShaderVersion() > 2501 )
  2536. {
  2537. m_stencilBufferHelper.ReadFromString( ref m_currentReadParamIdx, ref nodeParams );
  2538. }
  2539. if( UIUtils.CurrentShaderVersion() > 2504 )
  2540. {
  2541. m_tessOpHelper.ReadFromString( ref m_currentReadParamIdx, ref nodeParams );
  2542. }
  2543. if( UIUtils.CurrentShaderVersion() > 2505 )
  2544. {
  2545. m_receiveShadows = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  2546. }
  2547. if( UIUtils.CurrentShaderVersion() > 3202 )
  2548. {
  2549. m_blendOpsHelper.ReadFromString( ref m_currentReadParamIdx, ref nodeParams );
  2550. }
  2551. if( UIUtils.CurrentShaderVersion() > 3203 )
  2552. {
  2553. m_grabOrder = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  2554. }
  2555. if( UIUtils.CurrentShaderVersion() > 5003 )
  2556. {
  2557. m_outlineHelper.ReadFromString( ref m_currentReadParamIdx, ref nodeParams );
  2558. }
  2559. if( UIUtils.CurrentShaderVersion() > 5110 )
  2560. {
  2561. m_billboardOpHelper.ReadFromString( ref m_currentReadParamIdx, ref nodeParams );
  2562. }
  2563. if( UIUtils.CurrentShaderVersion() > 6101 )
  2564. {
  2565. m_vertexMode = (VertexMode)Enum.Parse( typeof( VertexMode ), GetCurrentParam( ref nodeParams ) );
  2566. }
  2567. if( UIUtils.CurrentShaderVersion() > 6102 )
  2568. {
  2569. ShaderLOD = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  2570. m_fallbackHelper.ReadFromString( ref m_currentReadParamIdx, ref nodeParams );
  2571. }
  2572. if( UIUtils.CurrentShaderVersion() > 7102 )
  2573. {
  2574. m_maskClipOrderIndex = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  2575. m_translucencyOrderIndex = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  2576. m_refractionOrderIndex = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  2577. m_tessellationOrderIndex = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  2578. }
  2579. if( UIUtils.CurrentShaderVersion() > 10010 && UIUtils.CurrentShaderVersion() < 15312 )
  2580. {
  2581. m_additionalIncludes.ReadFromString( ref m_currentReadParamIdx, ref nodeParams );
  2582. }
  2583. if( UIUtils.CurrentShaderVersion() > 11006 )
  2584. {
  2585. m_customTagsHelper.ReadFromString( ref m_currentReadParamIdx, ref nodeParams );
  2586. }
  2587. if( UIUtils.CurrentShaderVersion() > 13102 && UIUtils.CurrentShaderVersion() < 15312 )
  2588. {
  2589. m_additionalPragmas.ReadFromString( ref m_currentReadParamIdx, ref nodeParams );
  2590. }
  2591. if( UIUtils.CurrentShaderVersion() > 13205 )
  2592. {
  2593. m_alphaToCoverage = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  2594. }
  2595. if( UIUtils.CurrentShaderVersion() > 13903 )
  2596. {
  2597. m_dependenciesHelper.ReadFromString( ref m_currentReadParamIdx, ref nodeParams );
  2598. }
  2599. if( UIUtils.CurrentShaderVersion() > 14005 && UIUtils.CurrentShaderVersion() < 15312 )
  2600. {
  2601. m_additionalDefines.ReadFromString( ref m_currentReadParamIdx, ref nodeParams );
  2602. }
  2603. if( UIUtils.CurrentShaderVersion() > 14501 )
  2604. {
  2605. m_inlineCullMode.ReadFromString( ref m_currentReadParamIdx, ref nodeParams );
  2606. }
  2607. if( UIUtils.CurrentShaderVersion() > 14502 )
  2608. {
  2609. m_specColorOrderIndex = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  2610. }
  2611. if( UIUtils.CurrentShaderVersion() > 15204 )
  2612. {
  2613. m_inlineOpacityMaskClipValue.ReadFromString( ref m_currentReadParamIdx, ref nodeParams );
  2614. }
  2615. if( UIUtils.CurrentShaderVersion() > 15311 )
  2616. {
  2617. m_additionalDirectives.ReadFromString( ref m_currentReadParamIdx, ref nodeParams );
  2618. m_additionalSurfaceOptions.ReadFromString( ref m_currentReadParamIdx, ref nodeParams );
  2619. }
  2620. else
  2621. {
  2622. m_additionalDirectives.AddItems( AdditionalLineType.Define, m_additionalDefines.DefineList );
  2623. m_additionalDirectives.AddItems( AdditionalLineType.Include, m_additionalIncludes.IncludeList );
  2624. m_additionalDirectives.AddItems( AdditionalLineType.Pragma, m_additionalPragmas.PragmaList );
  2625. }
  2626. if( UIUtils.CurrentShaderVersion() > 15402 )
  2627. {
  2628. m_usePass.ReadFromString( ref m_currentReadParamIdx, ref nodeParams );
  2629. }
  2630. if( UIUtils.CurrentShaderVersion() > 16203 )
  2631. {
  2632. m_drawInstancedHelper.ReadFromString( ref m_currentReadParamIdx, ref nodeParams );
  2633. }
  2634. if( UIUtils.CurrentShaderVersion() > 16204 )
  2635. m_inlineChromaticAberration.ReadFromString( ref m_currentReadParamIdx, ref nodeParams , false );
  2636. if( UIUtils.CurrentShaderVersion() > 16207 )
  2637. m_inlineAlphaToCoverage.ReadFromString( ref m_currentReadParamIdx, ref nodeParams );
  2638. if( UIUtils.CurrentShaderVersion() > 18302 )
  2639. SamplingMacros = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  2640. else
  2641. SamplingMacros = false;
  2642. m_lightModelChanged = true;
  2643. m_lastLightModel = m_currentLightModel;
  2644. DeleteAllInputConnections( true );
  2645. AddMasterPorts();
  2646. UpdateFromBlendMode();
  2647. m_customBlendMode = TestCustomBlendMode();
  2648. ContainerGraph.CurrentPrecision = m_currentPrecisionType;
  2649. }
  2650. catch( Exception e )
  2651. {
  2652. Debug.Log( e );
  2653. }
  2654. }
  2655. public override void RefreshExternalReferences()
  2656. {
  2657. base.RefreshExternalReferences();
  2658. // change port connection from emission to the new custom lighting port
  2659. if( m_currentLightModel == StandardShaderLightModel.CustomLighting && m_inputPorts[ m_emissionPortId ].IsConnected && UIUtils.CurrentShaderVersion() < 13802 )
  2660. {
  2661. OutputPort port = m_inputPorts[ m_emissionPortId ].GetOutputConnection( 0 );
  2662. m_inputPorts[ m_emissionPortId ].FullDeleteConnections();
  2663. UIUtils.SetConnection( m_inputPorts[ m_customLightingPortId ].NodeId, m_inputPorts[ m_customLightingPortId ].PortId, port.NodeId, port.PortId );
  2664. }
  2665. }
  2666. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  2667. {
  2668. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  2669. IOUtils.AddFieldValueToString( ref nodeInfo, m_currentLightModel );
  2670. IOUtils.AddFieldValueToString( ref nodeInfo, m_shaderName );
  2671. m_renderingOptionsOpHelper.WriteToString( ref nodeInfo );
  2672. IOUtils.AddFieldValueToString( ref nodeInfo, m_cullMode );
  2673. m_zBufferHelper.WriteToString( ref nodeInfo );
  2674. IOUtils.AddFieldValueToString( ref nodeInfo, m_alphaMode );
  2675. IOUtils.AddFieldValueToString( ref nodeInfo, m_opacityMaskClipValue );
  2676. IOUtils.AddFieldValueToString( ref nodeInfo, m_keepAlpha );
  2677. IOUtils.AddFieldValueToString( ref nodeInfo, m_castShadows );
  2678. IOUtils.AddFieldValueToString( ref nodeInfo, m_queueOrder );
  2679. IOUtils.AddFieldValueToString( ref nodeInfo, m_customBlendMode );
  2680. IOUtils.AddFieldValueToString( ref nodeInfo, m_renderType );
  2681. IOUtils.AddFieldValueToString( ref nodeInfo, m_customRenderType );
  2682. IOUtils.AddFieldValueToString( ref nodeInfo, m_renderQueue );
  2683. IOUtils.AddFieldValueToString( ref nodeInfo, m_renderPath );
  2684. m_renderingPlatformOpHelper.WriteToString( ref nodeInfo );
  2685. m_colorMaskHelper.WriteToString( ref nodeInfo );
  2686. m_stencilBufferHelper.WriteToString( ref nodeInfo );
  2687. m_tessOpHelper.WriteToString( ref nodeInfo );
  2688. IOUtils.AddFieldValueToString( ref nodeInfo, m_receiveShadows );
  2689. m_blendOpsHelper.WriteToString( ref nodeInfo );
  2690. IOUtils.AddFieldValueToString( ref nodeInfo, m_grabOrder );
  2691. m_outlineHelper.WriteToString( ref nodeInfo );
  2692. m_billboardOpHelper.WriteToString( ref nodeInfo );
  2693. IOUtils.AddFieldValueToString( ref nodeInfo, m_vertexMode );
  2694. IOUtils.AddFieldValueToString( ref nodeInfo, ShaderLOD );
  2695. m_fallbackHelper.WriteToString( ref nodeInfo );
  2696. IOUtils.AddFieldValueToString( ref nodeInfo, ( m_maskClipReorder != null ) ? m_maskClipReorder.OrderIndex : -1 );
  2697. IOUtils.AddFieldValueToString( ref nodeInfo, ( m_translucencyReorder != null ) ? m_translucencyReorder.OrderIndex : -1 );
  2698. IOUtils.AddFieldValueToString( ref nodeInfo, ( m_refractionReorder != null ) ? m_refractionReorder.OrderIndex : -1 );
  2699. IOUtils.AddFieldValueToString( ref nodeInfo, ( m_tessellationReorder != null ) ? m_tessellationReorder.OrderIndex : -1 );
  2700. //m_additionalIncludes.WriteToString( ref nodeInfo );
  2701. m_customTagsHelper.WriteToString( ref nodeInfo );
  2702. //m_additionalPragmas.WriteToString( ref nodeInfo );
  2703. IOUtils.AddFieldValueToString( ref nodeInfo, m_alphaToCoverage );
  2704. m_dependenciesHelper.WriteToString( ref nodeInfo );
  2705. //m_additionalDefines.WriteToString( ref nodeInfo );
  2706. m_inlineCullMode.WriteToString( ref nodeInfo );
  2707. IOUtils.AddFieldValueToString( ref nodeInfo, ( m_specColorReorder != null ) ? m_specColorReorder.OrderIndex : -1 );
  2708. m_inlineOpacityMaskClipValue.WriteToString( ref nodeInfo );
  2709. m_additionalDirectives.WriteToString( ref nodeInfo );
  2710. m_additionalSurfaceOptions.WriteToString( ref nodeInfo );
  2711. m_usePass.WriteToString( ref nodeInfo );
  2712. m_drawInstancedHelper.WriteToString( ref nodeInfo );
  2713. m_inlineChromaticAberration.WriteToString( ref nodeInfo );
  2714. m_inlineAlphaToCoverage.WriteToString( ref nodeInfo );
  2715. IOUtils.AddFieldValueToString( ref nodeInfo, m_samplingMacros );
  2716. }
  2717. private bool TestCustomBlendMode()
  2718. {
  2719. switch( m_alphaMode )
  2720. {
  2721. case AlphaMode.Opaque:
  2722. {
  2723. if( m_renderType == RenderType.Opaque && m_renderQueue == RenderQueue.Geometry )
  2724. return false;
  2725. }
  2726. break;
  2727. case AlphaMode.Masked:
  2728. {
  2729. if( m_renderType == RenderType.TransparentCutout && m_renderQueue == RenderQueue.AlphaTest )
  2730. return false;
  2731. }
  2732. break;
  2733. case AlphaMode.Transparent:
  2734. case AlphaMode.Premultiply:
  2735. {
  2736. if( m_renderType == RenderType.Transparent && m_renderQueue == RenderQueue.Transparent )
  2737. return false;
  2738. }
  2739. break;
  2740. case AlphaMode.Translucent:
  2741. {
  2742. if( m_renderType == RenderType.Opaque && m_renderQueue == RenderQueue.Transparent )
  2743. return false;
  2744. }
  2745. break;
  2746. }
  2747. return true;
  2748. }
  2749. private void UpdateFromBlendMode()
  2750. {
  2751. m_checkChanges = true;
  2752. bool lockRefractionPort = false;
  2753. if( m_currentLightModel == StandardShaderLightModel.Unlit || m_currentLightModel == StandardShaderLightModel.CustomLighting )
  2754. {
  2755. lockRefractionPort = true;
  2756. }
  2757. switch( m_alphaMode )
  2758. {
  2759. case AlphaMode.Opaque:
  2760. {
  2761. m_renderType = RenderType.Opaque;
  2762. m_renderQueue = RenderQueue.Geometry;
  2763. m_keepAlpha = true;
  2764. m_refractionPort.Locked = true;
  2765. m_inputPorts[ m_opacityPortId ].Locked = true;
  2766. m_inputPorts[ m_discardPortId ].Locked = true;
  2767. }
  2768. break;
  2769. case AlphaMode.Masked:
  2770. {
  2771. m_renderType = RenderType.TransparentCutout;
  2772. m_renderQueue = RenderQueue.AlphaTest;
  2773. m_keepAlpha = true;
  2774. m_refractionPort.Locked = true;
  2775. m_inputPorts[ m_opacityPortId ].Locked = true;
  2776. m_inputPorts[ m_discardPortId ].Locked = false;
  2777. }
  2778. break;
  2779. case AlphaMode.Transparent:
  2780. case AlphaMode.Premultiply:
  2781. {
  2782. m_renderType = RenderType.Transparent;
  2783. m_renderQueue = RenderQueue.Transparent;
  2784. m_refractionPort.Locked = false || lockRefractionPort;
  2785. m_inputPorts[ m_opacityPortId ].Locked = false;
  2786. m_inputPorts[ m_discardPortId ].Locked = true;
  2787. }
  2788. break;
  2789. case AlphaMode.Translucent:
  2790. {
  2791. m_renderType = RenderType.Opaque;
  2792. m_renderQueue = RenderQueue.Transparent;
  2793. m_refractionPort.Locked = false || lockRefractionPort;
  2794. m_inputPorts[ m_opacityPortId ].Locked = false;
  2795. m_inputPorts[ m_discardPortId ].Locked = true;
  2796. }
  2797. break;
  2798. case AlphaMode.Custom:
  2799. {
  2800. m_refractionPort.Locked = false || lockRefractionPort;
  2801. m_inputPorts[ m_opacityPortId ].Locked = false;
  2802. m_inputPorts[ m_discardPortId ].Locked = false;
  2803. }
  2804. break;
  2805. }
  2806. m_blendOpsHelper.SetBlendOpsFromBlendMode( m_alphaMode, ( m_alphaMode == AlphaMode.Custom || m_alphaMode == AlphaMode.Opaque ) );
  2807. }
  2808. public bool CastShadows { get { return m_castShadows; } }
  2809. public StandardShaderLightModel CurrentLightingModel { get { return m_currentLightModel; } }
  2810. public CullMode CurrentCullMode { get { return m_cullMode; } }
  2811. //public AdditionalIncludesHelper AdditionalIncludes { get { return m_additionalIncludes; } set { m_additionalIncludes = value; } }
  2812. //public AdditionalPragmasHelper AdditionalPragmas { get { return m_additionalPragmas; } set { m_additionalPragmas = value; } }
  2813. //public AdditionalDefinesHelper AdditionalDefines { get { return m_additionalDefines; } set { m_additionalDefines = value; } }
  2814. public TemplateAdditionalDirectivesHelper AdditionalDirectives { get { return m_additionalDirectives; } }
  2815. public OutlineOpHelper OutlineHelper { get { return m_outlineHelper; } }
  2816. public float OpacityMaskClipValue { get { return m_opacityMaskClipValue; } }
  2817. public InlineProperty InlineOpacityMaskClipValue { get { return m_inlineOpacityMaskClipValue; } set { m_inlineOpacityMaskClipValue = value; } }
  2818. public bool CustomShadowCaster
  2819. {
  2820. get
  2821. {
  2822. bool hasOpacity = m_inputPorts[ m_opacityPortId ].IsConnected;
  2823. return
  2824. ( !m_renderingOptionsOpHelper.UseDefaultShadowCaster &&
  2825. ( ( m_castShadows && ( m_alphaToCoverage || m_inlineAlphaToCoverage.Active ) ) ||
  2826. ( m_castShadows && hasOpacity ) ||
  2827. ( m_castShadows && ( m_currentDataCollector.UsingWorldNormal || m_currentDataCollector.UsingWorldReflection || m_currentDataCollector.UsingViewDirection ) ) ||
  2828. ( m_castShadows && m_inputPorts[ m_discardPortId ].Available && m_inputPorts[ m_discardPortId ].IsConnected && m_currentLightModel == StandardShaderLightModel.CustomLighting ) ) );
  2829. }
  2830. }
  2831. public override AvailableShaderTypes CurrentMasterNodeCategory { get { return AvailableShaderTypes.SurfaceShader; } }
  2832. }
  2833. }