TemplateDataCollector.cs 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using UnityEngine;
  4. using System.Collections.Generic;
  5. using System.Text.RegularExpressions;
  6. using System;
  7. namespace AmplifyShaderEditor
  8. {
  9. public enum NormalizeType
  10. {
  11. Off,
  12. Regular,
  13. Safe
  14. }
  15. public enum DepthMode
  16. {
  17. DepthLinearEye,
  18. DepthLinear01,
  19. DepthEye,
  20. Depth01,
  21. }
  22. public class InterpDataHelper
  23. {
  24. public string VarName;
  25. public WirePortDataType VarType;
  26. public bool IsSingleComponent;
  27. public bool SetAtCompileTime;
  28. public InterpDataHelper( WirePortDataType varType, string varName, bool isSingleComponent = true , bool setAtCompileTime = false )
  29. {
  30. VarName = varName;
  31. VarType = varType;
  32. IsSingleComponent = isSingleComponent;
  33. SetAtCompileTime = setAtCompileTime;
  34. }
  35. }
  36. public class TemplateCustomData
  37. {
  38. public WirePortDataType DataType;
  39. public string Name;
  40. public bool IsVertex;
  41. public bool IsFragment;
  42. public TemplateCustomData( string name, WirePortDataType dataType )
  43. {
  44. name = Name;
  45. DataType = dataType;
  46. IsVertex = false;
  47. IsFragment = false;
  48. }
  49. }
  50. public class TemplateInputParameters
  51. {
  52. public WirePortDataType Type;
  53. public string Name;
  54. public string Declaration;
  55. public TemplateSemantics Semantic;
  56. public TemplateInputParameters( WirePortDataType type, PrecisionType precision, string name, TemplateSemantics semantic, string custom = null )
  57. {
  58. Type = type;
  59. Name = name;
  60. Semantic = semantic;
  61. Declaration = string.Format( "{0} {1} : {2}", UIUtils.PrecisionWirePortToCgType( precision, type ), Name, Semantic );
  62. if( !string.IsNullOrEmpty( custom ) )
  63. Declaration = custom;
  64. }
  65. }
  66. public class TemplateDataCollector
  67. {
  68. private const int MaxUV = 8;
  69. private int[] m_UVUsage = { 0, 0, 0, 0, 0, 0, 0, 0 };
  70. private int m_multipassSubshaderIdx = 0;
  71. private int m_multipassPassIdx = 0;
  72. private TemplateMultiPass m_currentTemplate;
  73. private TemplateSRPType m_currentSRPType = TemplateSRPType.BiRP;
  74. private Dictionary<string, TemplateCustomData> m_customInterpolatedData;
  75. private Dictionary<string, TemplateVertexData> m_registeredVertexData;
  76. private Dictionary<TemplateInfoOnSematics, InterpDataHelper> m_availableFragData;
  77. private Dictionary<TemplateInfoOnSematics, InterpDataHelper> m_availableVertData;
  78. private TemplateInterpData m_interpolatorData;
  79. private Dictionary<TemplateSemantics, TemplateVertexData> m_vertexDataDict;
  80. private TemplateData m_currentTemplateData;
  81. private MasterNodeDataCollector m_currentDataCollector;
  82. public Dictionary<TemplateSemantics, TemplateInputParameters> m_vertexInputParams;
  83. public Dictionary<TemplateSemantics, TemplateInputParameters> m_fragmentInputParams;
  84. private Dictionary<TemplateInfoOnSematics, TemplateLocalVarData> m_specialVertexLocalVars;
  85. private Dictionary<TemplateInfoOnSematics, TemplateLocalVarData> m_specialFragmentLocalVars;
  86. private List<PropertyDataCollector> m_lateDirectivesList = new List<PropertyDataCollector>();
  87. private Dictionary<string, PropertyDataCollector> m_lateDirectivesDict = new Dictionary<string, PropertyDataCollector>();
  88. private List<PropertyDataCollector> m_srpBatcherPropertiesList = new List<PropertyDataCollector>();
  89. private List<PropertyDataCollector> m_fullSrpBatcherPropertiesList = new List<PropertyDataCollector>();
  90. private Dictionary<string, PropertyDataCollector> m_srpBatcherPropertiesDict = new Dictionary<string, PropertyDataCollector>();
  91. public void CopySRPPropertiesFromDataCollector( int nodeId, TemplateDataCollector dataCollector )
  92. {
  93. for( int i = 0; i < dataCollector.SrpBatcherPropertiesList.Count; i++ )
  94. {
  95. AddSRPBatcherProperty( nodeId, dataCollector.SrpBatcherPropertiesList[ i ].PropertyName );
  96. }
  97. }
  98. public void AddSRPBatcherProperty( int nodeID, string property )
  99. {
  100. if( !m_srpBatcherPropertiesDict.ContainsKey( property ) )
  101. {
  102. PropertyDataCollector newValue = new PropertyDataCollector( nodeID, property );
  103. m_srpBatcherPropertiesDict.Add( property, newValue );
  104. m_srpBatcherPropertiesList.Add( newValue );
  105. }
  106. }
  107. public void SetUVUsage( int uv, WirePortDataType type )
  108. {
  109. if( uv >= 0 && uv < MaxUV )
  110. {
  111. m_UVUsage[ uv ] = Mathf.Max( m_UVUsage[ uv ], TemplateHelperFunctions.DataTypeChannelUsage[ type ] );
  112. }
  113. }
  114. public void SetUVUsage( int uv, int size )
  115. {
  116. if( uv >= 0 && uv < MaxUV )
  117. {
  118. m_UVUsage[ uv ] = Mathf.Max( m_UVUsage[ uv ], size );
  119. }
  120. }
  121. public void CloseLateDirectives()
  122. {
  123. if( m_lateDirectivesList.Count > 0 )
  124. {
  125. m_lateDirectivesList.Add( new PropertyDataCollector( -1, string.Empty ) );
  126. }
  127. }
  128. public void AddHDLightInfo()
  129. {
  130. }
  131. public void AddLateDirective( AdditionalLineType type, string value )
  132. {
  133. if( !m_lateDirectivesDict.ContainsKey( value ) )
  134. {
  135. string formattedValue = string.Empty;
  136. switch( type )
  137. {
  138. case AdditionalLineType.Include: formattedValue = string.Format( Constants.IncludeFormat, value ); break;
  139. case AdditionalLineType.Define: formattedValue = string.Format( Constants.DefineFormat, value ); break;
  140. case AdditionalLineType.Pragma: formattedValue = string.Format( Constants.PragmaFormat, value ); break;
  141. case AdditionalLineType.Custom: formattedValue = value; break;
  142. }
  143. PropertyDataCollector property = new PropertyDataCollector( -1, formattedValue );
  144. m_lateDirectivesDict.Add( value, property );
  145. m_lateDirectivesList.Add( property );
  146. }
  147. }
  148. public void SetMultipassInfo( TemplateMultiPass currentTemplate, int subShaderIdx, int passIdx, TemplateSRPType currentSRPType )
  149. {
  150. m_currentTemplate = currentTemplate;
  151. m_multipassSubshaderIdx = subShaderIdx;
  152. m_multipassPassIdx = passIdx;
  153. m_currentSRPType = currentSRPType;
  154. }
  155. public bool HasDirective( AdditionalLineType type, string value )
  156. {
  157. switch( type )
  158. {
  159. case AdditionalLineType.Include:
  160. {
  161. return m_currentTemplate.SubShaders[ m_multipassSubshaderIdx ].Modules.IncludePragmaContainer.HasInclude( value ) ||
  162. m_currentTemplate.SubShaders[ m_multipassSubshaderIdx ].Passes[ m_multipassPassIdx ].Modules.IncludePragmaContainer.HasInclude( value );
  163. }
  164. case AdditionalLineType.Define:
  165. {
  166. return m_currentTemplate.SubShaders[ m_multipassSubshaderIdx ].Modules.IncludePragmaContainer.HasDefine( value ) ||
  167. m_currentTemplate.SubShaders[ m_multipassSubshaderIdx ].Passes[ m_multipassPassIdx ].Modules.IncludePragmaContainer.HasDefine( value );
  168. }
  169. case AdditionalLineType.Pragma:
  170. {
  171. return m_currentTemplate.SubShaders[ m_multipassSubshaderIdx ].Modules.IncludePragmaContainer.HasPragma( value ) ||
  172. m_currentTemplate.SubShaders[ m_multipassSubshaderIdx ].Passes[ m_multipassPassIdx ].Modules.IncludePragmaContainer.HasPragma( value );
  173. }
  174. }
  175. return false;
  176. }
  177. public void FillSpecialVariables( TemplatePass currentPass )
  178. {
  179. m_specialVertexLocalVars = new Dictionary<TemplateInfoOnSematics, TemplateLocalVarData>();
  180. m_specialFragmentLocalVars = new Dictionary<TemplateInfoOnSematics, TemplateLocalVarData>();
  181. int localVarAmount = currentPass.LocalVarsList.Count;
  182. for( int i = 0; i < localVarAmount; i++ )
  183. {
  184. if( currentPass.LocalVarsList[ i ].IsSpecialVar )
  185. {
  186. if( currentPass.LocalVarsList[ i ].Category == MasterNodePortCategory.Vertex )
  187. {
  188. m_specialVertexLocalVars.Add( currentPass.LocalVarsList[ i ].SpecialVarType, currentPass.LocalVarsList[ i ] );
  189. }
  190. else
  191. {
  192. m_specialFragmentLocalVars.Add( currentPass.LocalVarsList[ i ].SpecialVarType, currentPass.LocalVarsList[ i ] );
  193. }
  194. }
  195. }
  196. }
  197. public void BuildFromTemplateData( MasterNodeDataCollector dataCollector, TemplateData templateData )
  198. {
  199. m_registeredVertexData = new Dictionary<string, TemplateVertexData>();
  200. m_customInterpolatedData = new Dictionary<string, TemplateCustomData>();
  201. m_currentDataCollector = dataCollector;
  202. m_currentTemplateData = templateData;
  203. m_vertexDataDict = new Dictionary<TemplateSemantics, TemplateVertexData>();
  204. if( templateData.VertexDataList != null )
  205. {
  206. for( int i = 0; i < templateData.VertexDataList.Count; i++ )
  207. {
  208. m_vertexDataDict.Add( templateData.VertexDataList[ i ].Semantics, new TemplateVertexData( templateData.VertexDataList[ i ] ) );
  209. }
  210. }
  211. m_availableFragData = new Dictionary<TemplateInfoOnSematics, InterpDataHelper>();
  212. if( templateData.InterpolatorData != null && templateData.FragmentFunctionData != null )
  213. {
  214. m_interpolatorData = new TemplateInterpData( templateData.InterpolatorData );
  215. int fragCount = templateData.InterpolatorData.Interpolators.Count;
  216. for( int i = 0; i < fragCount; i++ )
  217. {
  218. string varName = string.Empty;
  219. if( templateData.InterpolatorData.Interpolators[ i ].ExcludeStructPrefix )
  220. {
  221. varName = templateData.InterpolatorData.Interpolators[ i ].VarName;
  222. }
  223. else if( templateData.InterpolatorData.Interpolators[ i ].IsSingleComponent )
  224. {
  225. varName = string.Format( TemplateHelperFunctions.TemplateVarFormat,
  226. templateData.FragmentFunctionData.InVarName,
  227. templateData.InterpolatorData.Interpolators[ i ].VarNameWithSwizzle );
  228. }
  229. else
  230. {
  231. varName = string.Format( templateData.InterpolatorData.Interpolators[ i ].VarNameWithSwizzle, templateData.FragmentFunctionData.InVarName );
  232. }
  233. m_availableFragData.Add( templateData.InterpolatorData.Interpolators[ i ].DataInfo,
  234. new InterpDataHelper( templateData.InterpolatorData.Interpolators[ i ].SwizzleType,
  235. varName,
  236. templateData.InterpolatorData.Interpolators[ i ].IsSingleComponent ) );
  237. }
  238. }
  239. m_availableVertData = new Dictionary<TemplateInfoOnSematics, InterpDataHelper>();
  240. if( templateData.VertexFunctionData != null && templateData.VertexDataList != null )
  241. {
  242. int vertCount = templateData.VertexDataList.Count;
  243. for( int i = 0; i < vertCount; i++ )
  244. {
  245. string varName = string.Empty;
  246. if( templateData.VertexDataList[ i ].ExcludeStructPrefix )
  247. {
  248. varName = templateData.VertexDataList[ i ].VarName;
  249. }
  250. else
  251. {
  252. varName = string.Format( TemplateHelperFunctions.TemplateVarFormat, templateData.VertexFunctionData.InVarName, templateData.VertexDataList[ i ].VarNameWithSwizzle );
  253. }
  254. m_availableVertData.Add( templateData.VertexDataList[ i ].DataInfo,
  255. new InterpDataHelper( templateData.VertexDataList[ i ].SwizzleType,
  256. varName,
  257. templateData.VertexDataList[ i ].IsSingleComponent ) );
  258. }
  259. }
  260. }
  261. public void RegisterFragInputParams( WirePortDataType type, PrecisionType precision, string name, TemplateSemantics semantic, string custom )
  262. {
  263. if( m_fragmentInputParams == null )
  264. m_fragmentInputParams = new Dictionary<TemplateSemantics, TemplateInputParameters>();
  265. m_fragmentInputParams.Add( semantic, new TemplateInputParameters( type, precision, name, semantic, custom ) );
  266. }
  267. public void RegisterFragInputParams( WirePortDataType type, PrecisionType precision, string name, TemplateSemantics semantic )
  268. {
  269. if( m_fragmentInputParams == null )
  270. m_fragmentInputParams = new Dictionary<TemplateSemantics, TemplateInputParameters>();
  271. m_fragmentInputParams.Add( semantic, new TemplateInputParameters( type, precision, name, semantic ) );
  272. }
  273. public void RegisterVertexInputParams( WirePortDataType type , PrecisionType precision , string name , TemplateSemantics semantic, string custom )
  274. {
  275. if( m_vertexInputParams == null )
  276. m_vertexInputParams = new Dictionary<TemplateSemantics , TemplateInputParameters>();
  277. m_vertexInputParams.Add( semantic , new TemplateInputParameters( type , precision , name , semantic, custom ) );
  278. }
  279. public void RegisterVertexInputParams( WirePortDataType type, PrecisionType precision, string name, TemplateSemantics semantic )
  280. {
  281. if( m_vertexInputParams == null )
  282. m_vertexInputParams = new Dictionary<TemplateSemantics, TemplateInputParameters>();
  283. m_vertexInputParams.Add( semantic, new TemplateInputParameters( type, precision, name, semantic ) );
  284. }
  285. public string GetInstanceId()
  286. {
  287. var precision = PrecisionType.Float;
  288. bool useMasterNodeCategory = true;
  289. MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment;
  290. m_currentDataCollector.AddToDirectives( "#define INSTANCEID_SEMANTIC SV_InstanceID" );
  291. if ( HasInfo( TemplateInfoOnSematics.INSTANCEID, useMasterNodeCategory, customCategory ) )
  292. {
  293. InterpDataHelper info = GetInfo( TemplateInfoOnSematics.INSTANCEID, useMasterNodeCategory, customCategory );
  294. return info.VarName;
  295. }
  296. else
  297. {
  298. MasterNodePortCategory category = useMasterNodeCategory ? m_currentDataCollector.PortCategory : customCategory;
  299. string name = TemplateHelperFunctions.SemanticsDefaultName[ TemplateSemantics.SV_InstanceID ];
  300. string varName = RegisterInfoOnSemantic( category, TemplateInfoOnSematics.INSTANCEID, TemplateSemantics.INSTANCEID_SEMANTIC, name, WirePortDataType.UINT, precision, true );
  301. return varName;
  302. }
  303. }
  304. public string GetVertexId()
  305. {
  306. var precision = PrecisionType.Float;
  307. bool useMasterNodeCategory = true;
  308. MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment;
  309. m_currentDataCollector.AddToDirectives( "#define VERTEXID_SEMANTIC SV_VertexID" );
  310. WirePortDataType type = WirePortDataType.UINT;
  311. if( HasInfo( TemplateInfoOnSematics.VERTEXID, useMasterNodeCategory, customCategory ) )
  312. {
  313. InterpDataHelper info = GetInfo( TemplateInfoOnSematics.VERTEXID, useMasterNodeCategory, customCategory );
  314. return TemplateHelperFunctions.AutoSwizzleData( info.VarName, info.VarType, type, true );
  315. }
  316. else
  317. {
  318. MasterNodePortCategory portCategory = useMasterNodeCategory ? m_currentDataCollector.PortCategory : customCategory;
  319. string name = TemplateHelperFunctions.SemanticsDefaultName[ TemplateSemantics.SV_VertexID ];
  320. return RegisterInfoOnSemantic( portCategory, TemplateInfoOnSematics.VERTEXID, TemplateSemantics.VERTEXID_SEMANTIC, name, WirePortDataType.UINT, precision, true );
  321. }
  322. }
  323. public string GetPrimitiveId()
  324. {
  325. if ( m_fragmentInputParams != null && m_fragmentInputParams.ContainsKey( TemplateSemantics.SV_PrimitiveID ) )
  326. return m_fragmentInputParams[ TemplateSemantics.SV_PrimitiveID ].Name;
  327. string name = TemplateHelperFunctions.SemanticsDefaultName[ TemplateSemantics.SV_PrimitiveID ];
  328. RegisterFragInputParams( WirePortDataType.UINT, PrecisionType.Half, name, TemplateSemantics.SV_PrimitiveID );
  329. return m_fragmentInputParams[ TemplateSemantics.SV_PrimitiveID ].Name;
  330. }
  331. public string GetURPMainLight( int uniqueId, string shadowCoords = null )
  332. {
  333. if( string.IsNullOrEmpty( shadowCoords ) )
  334. {
  335. shadowCoords = GetShadowCoords( uniqueId );
  336. }
  337. m_currentDataCollector.AddLocalVariable( uniqueId , string.Format( "Light ase_mainLight = GetMainLight( {0} );",shadowCoords) );
  338. return "ase_mainLight";
  339. }
  340. public string GetVFace( int uniqueId )
  341. {
  342. if( IsSRP )
  343. {
  344. string result = string.Empty;
  345. if( GetCustomInterpolatedData( TemplateInfoOnSematics.VFACE, WirePortDataType.FLOAT, PrecisionType.Float, ref result, true, MasterNodePortCategory.Fragment ) )
  346. {
  347. m_currentDataCollector.AddToDirectives( "#if !defined(ASE_NEED_CULLFACE)" );
  348. m_currentDataCollector.AddToDirectives( "#define ASE_NEED_CULLFACE 1" );
  349. m_currentDataCollector.AddToDirectives( "#endif //ASE_NEED_CULLFACE" );
  350. return result;
  351. }
  352. else
  353. {
  354. if( m_fragmentInputParams != null && m_fragmentInputParams.ContainsKey( TemplateSemantics.SV_IsFrontFacing ) )
  355. return m_fragmentInputParams[ TemplateSemantics.SV_IsFrontFacing ].Name;
  356. string custom = "bool "+ TemplateHelperFunctions.SemanticsDefaultName[ TemplateSemantics.SV_IsFrontFacing ] + " : SV_IsFrontFace";
  357. RegisterFragInputParams( WirePortDataType.FLOAT, PrecisionType.Half, TemplateHelperFunctions.SemanticsDefaultName[ TemplateSemantics.SV_IsFrontFacing ], TemplateSemantics.SV_IsFrontFacing, custom );
  358. return m_fragmentInputParams[ TemplateSemantics.SV_IsFrontFacing ].Name;
  359. }
  360. }
  361. else
  362. {
  363. //if( m_fragmentInputParams != null && m_fragmentInputParams.ContainsKey( TemplateSemantics.VFACE ) )
  364. // return m_fragmentInputParams[ TemplateSemantics.VFACE ].Name;
  365. //RegisterFragInputParams( WirePortDataType.FLOAT, PrecisionType.Half, TemplateHelperFunctions.SemanticsDefaultName[ TemplateSemantics.VFACE ], TemplateSemantics.VFACE );
  366. //return m_fragmentInputParams[ TemplateSemantics.VFACE ].Name;
  367. if( m_fragmentInputParams != null && m_fragmentInputParams.ContainsKey( TemplateSemantics.SV_IsFrontFacing ) )
  368. return m_fragmentInputParams[ TemplateSemantics.SV_IsFrontFacing ].Name;
  369. string custom = "bool " + TemplateHelperFunctions.SemanticsDefaultName[ TemplateSemantics.SV_IsFrontFacing ] + " : SV_IsFrontFace";
  370. RegisterFragInputParams( WirePortDataType.FLOAT , PrecisionType.Half , TemplateHelperFunctions.SemanticsDefaultName[ TemplateSemantics.SV_IsFrontFacing ] , TemplateSemantics.SV_IsFrontFacing , custom );
  371. return m_fragmentInputParams[ TemplateSemantics.SV_IsFrontFacing ].Name;
  372. }
  373. }
  374. public string GetShadowCoords( int uniqueId, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  375. {
  376. // overriding precision
  377. var precision = PrecisionType.Float;
  378. string worldPos = GetWorldPos( false, m_currentDataCollector.PortCategory );
  379. string result = string.Empty;
  380. if( GetCustomInterpolatedData( TemplateInfoOnSematics.SHADOWCOORDS, WirePortDataType.FLOAT4, precision, ref result, useMasterNodeCategory, customCategory ) )
  381. {
  382. return result;
  383. }
  384. string varName = GeneratorUtils.ShadowCoordsStr;
  385. if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
  386. return varName;
  387. string shadowCoordsValue = string.Format( "TransformWorldToShadowCoord( {0} )", worldPos );
  388. if( m_currentDataCollector.PortCategory == MasterNodePortCategory.Fragment )
  389. {
  390. worldPos = GetWorldPos( false, MasterNodePortCategory.Vertex );
  391. m_currentDataCollector.AddLocalVariable( uniqueId, "#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR) //la" );
  392. RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT4, precision, string.Format( "TransformWorldToShadowCoord( {0} )", worldPos ), false, MasterNodePortCategory.Fragment );
  393. m_currentDataCollector.AddLocalVariable( uniqueId, "#elif defined(MAIN_LIGHT_CALCULATE_SHADOWS) //la" );
  394. m_currentDataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT4, varName, shadowCoordsValue );
  395. m_currentDataCollector.AddLocalVariable( uniqueId, "#else //la" );
  396. m_currentDataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT4, varName, "0" );
  397. m_currentDataCollector.AddLocalVariable( uniqueId, "#endif //la" );
  398. } else
  399. {
  400. m_currentDataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT4, varName, shadowCoordsValue );
  401. }
  402. return varName;
  403. }
  404. public bool HasUV( int uvChannel )
  405. {
  406. return ( m_currentDataCollector.PortCategory == MasterNodePortCategory.Fragment ) ? m_availableFragData.ContainsKey( TemplateHelperFunctions.IntToUVChannelInfo[ uvChannel ] ) : m_availableVertData.ContainsKey( TemplateHelperFunctions.IntToUVChannelInfo[ uvChannel ] );
  407. }
  408. public string GetUVName( int uvChannel, WirePortDataType dataType = WirePortDataType.FLOAT2 )
  409. {
  410. InterpDataHelper info = ( m_currentDataCollector.PortCategory == MasterNodePortCategory.Fragment ) ? m_availableFragData[ TemplateHelperFunctions.IntToUVChannelInfo[ uvChannel ] ] : m_availableVertData[ TemplateHelperFunctions.IntToUVChannelInfo[ uvChannel ] ];
  411. if( dataType != info.VarType )
  412. return info.VarName + UIUtils.GetAutoSwizzle( dataType );
  413. else
  414. return info.VarName;
  415. }
  416. public string GetTextureCoord( int uvChannel, string propertyName, int uniqueId, PrecisionType precisionType )
  417. {
  418. bool isVertex = ( m_currentDataCollector.PortCategory == MasterNodePortCategory.Vertex || m_currentDataCollector.PortCategory == MasterNodePortCategory.Tessellation );
  419. string uvChannelName = string.Empty;
  420. string propertyHelperVar = propertyName + "_ST";
  421. m_currentDataCollector.AddToUniforms( uniqueId, "float4", propertyHelperVar, IsSRP );
  422. string uvName = string.Empty;
  423. string result = string.Empty;
  424. if( GetCustomInterpolatedData( TemplateHelperFunctions.IntToUVChannelInfo[ uvChannel ], WirePortDataType.FLOAT2, PrecisionType.Float, ref result, false, m_currentDataCollector.PortCategory ) )
  425. {
  426. uvName = result;
  427. }
  428. else
  429. if( m_currentDataCollector.TemplateDataCollectorInstance.HasUV( uvChannel ) )
  430. {
  431. uvName = m_currentDataCollector.TemplateDataCollectorInstance.GetUVName( uvChannel );
  432. }
  433. else
  434. {
  435. uvName = m_currentDataCollector.TemplateDataCollectorInstance.RegisterUV( uvChannel );
  436. }
  437. uvChannelName = "uv" + propertyName;
  438. if( isVertex )
  439. {
  440. string value = string.Format( Constants.TilingOffsetFormat, uvName, propertyHelperVar + ".xy", propertyHelperVar + ".zw" );
  441. string lodLevel = "0";
  442. value = "float4( " + value + ", 0 , " + lodLevel + " )";
  443. m_currentDataCollector.AddLocalVariable( uniqueId, precisionType, WirePortDataType.FLOAT4, uvChannelName, value );
  444. }
  445. else
  446. {
  447. m_currentDataCollector.AddLocalVariable( uniqueId, precisionType, WirePortDataType.FLOAT2, uvChannelName, string.Format( Constants.TilingOffsetFormat, uvName, propertyHelperVar + ".xy", propertyHelperVar + ".zw" ) );
  448. }
  449. return uvChannelName;
  450. }
  451. public string GenerateAutoUVs( int uvChannel, WirePortDataType size = WirePortDataType.FLOAT2 )
  452. {
  453. string uvName = string.Empty;
  454. string result = string.Empty;
  455. if( GetCustomInterpolatedData( TemplateHelperFunctions.IntToUVChannelInfo[ uvChannel ], size, PrecisionType.Float, ref result, false, m_currentDataCollector.PortCategory ) )
  456. {
  457. uvName = result;
  458. }
  459. else
  460. if( HasUV( uvChannel ) )
  461. {
  462. uvName = GetUVName( uvChannel, size );
  463. }
  464. else
  465. {
  466. uvName = RegisterUV( uvChannel, size );
  467. }
  468. return uvName;
  469. }
  470. public string GetUV( int uvChannel, MasterNodePortCategory category = MasterNodePortCategory.Fragment, WirePortDataType size = WirePortDataType.FLOAT4 )
  471. {
  472. if( !HasUV( uvChannel ) )
  473. {
  474. RegisterUV( uvChannel, size );
  475. }
  476. InterpDataHelper info = ( category == MasterNodePortCategory.Fragment ) ? m_availableFragData[ TemplateHelperFunctions.IntToUVChannelInfo[ uvChannel ] ] : m_availableVertData[ TemplateHelperFunctions.IntToUVChannelInfo[ uvChannel ] ];
  477. return info.VarName;
  478. }
  479. public InterpDataHelper GetUVInfo( int uvChannel )
  480. {
  481. return ( m_currentDataCollector.PortCategory == MasterNodePortCategory.Fragment ) ? m_availableFragData[ TemplateHelperFunctions.IntToUVChannelInfo[ uvChannel ] ] : m_availableVertData[ TemplateHelperFunctions.IntToUVChannelInfo[ uvChannel ] ];
  482. }
  483. public string RegisterUV( int UVChannel, WirePortDataType size = WirePortDataType.FLOAT2 )
  484. {
  485. int channelsSize = TemplateHelperFunctions.DataTypeChannelUsage[ size ];
  486. WirePortDataType originalSize = size;
  487. if( m_UVUsage[ UVChannel ] > channelsSize )
  488. {
  489. size = TemplateHelperFunctions.ChannelToDataType[ m_UVUsage[ UVChannel ] ];
  490. }
  491. if( m_currentDataCollector.PortCategory == MasterNodePortCategory.Vertex )
  492. {
  493. TemplateSemantics semantic = TemplateHelperFunctions.IntToSemantic[ UVChannel ];
  494. if( m_vertexDataDict.ContainsKey( semantic ) )
  495. {
  496. return m_vertexDataDict[ semantic ].VarName;
  497. }
  498. string varName = TemplateHelperFunctions.BaseInterpolatorName + ( ( UVChannel > 0 ) ? UVChannel.ToString() : string.Empty );
  499. m_availableVertData.Add( TemplateHelperFunctions.IntToUVChannelInfo[ UVChannel ],
  500. new InterpDataHelper( WirePortDataType.FLOAT4,
  501. string.Format( TemplateHelperFunctions.TemplateVarFormat,
  502. m_currentTemplateData.VertexFunctionData.InVarName,
  503. varName ) ) );
  504. m_currentDataCollector.AddToVertexInput(
  505. string.Format( TemplateHelperFunctions.TexFullSemantic,
  506. varName,
  507. semantic ) );
  508. RegisterOnVertexData( semantic, size, varName );
  509. string finalVarName = m_availableVertData[ TemplateHelperFunctions.IntToUVChannelInfo[ UVChannel ] ].VarName;
  510. switch( size )
  511. {
  512. case WirePortDataType.FLOAT:
  513. case WirePortDataType.INT:
  514. case WirePortDataType.UINT:
  515. finalVarName += ".x";
  516. break;
  517. case WirePortDataType.FLOAT2:
  518. finalVarName += ".xy";
  519. break;
  520. case WirePortDataType.FLOAT3:
  521. finalVarName += ".xyz";
  522. break;
  523. case WirePortDataType.UINT4:
  524. case WirePortDataType.FLOAT4:
  525. case WirePortDataType.COLOR:
  526. case WirePortDataType.FLOAT3x3:
  527. case WirePortDataType.FLOAT4x4:
  528. case WirePortDataType.SAMPLER1D:
  529. case WirePortDataType.SAMPLER2D:
  530. case WirePortDataType.SAMPLER3D:
  531. case WirePortDataType.SAMPLERCUBE:
  532. case WirePortDataType.SAMPLER2DARRAY:
  533. case WirePortDataType.SAMPLERSTATE:
  534. case WirePortDataType.OBJECT:
  535. default:
  536. break;
  537. }
  538. return finalVarName;
  539. }
  540. else
  541. {
  542. //search if the correct vertex data is set ...
  543. TemplateInfoOnSematics info = TemplateHelperFunctions.IntToInfo[ UVChannel ];
  544. TemplateSemantics vertexSemantics = TemplateSemantics.NONE;
  545. foreach( KeyValuePair<TemplateSemantics, TemplateVertexData> kvp in m_vertexDataDict )
  546. {
  547. if( kvp.Value.DataInfo == info )
  548. {
  549. vertexSemantics = kvp.Key;
  550. break;
  551. }
  552. }
  553. // if not, add vertex data and create interpolator
  554. if( vertexSemantics == TemplateSemantics.NONE )
  555. {
  556. vertexSemantics = TemplateHelperFunctions.IntToSemantic[ UVChannel ];
  557. if( !m_vertexDataDict.ContainsKey( vertexSemantics ) )
  558. {
  559. string varName = TemplateHelperFunctions.BaseInterpolatorName + ( ( UVChannel > 0 ) ? UVChannel.ToString() : string.Empty );
  560. m_availableVertData.Add( TemplateHelperFunctions.IntToUVChannelInfo[ UVChannel ],
  561. new InterpDataHelper( WirePortDataType.FLOAT4,
  562. string.Format( TemplateHelperFunctions.TemplateVarFormat,
  563. m_currentTemplateData.VertexFunctionData.InVarName,
  564. varName ) ) );
  565. m_currentDataCollector.AddToVertexInput(
  566. string.Format( TemplateHelperFunctions.TexFullSemantic,
  567. varName,
  568. vertexSemantics ) );
  569. RegisterOnVertexData( vertexSemantics, size, varName );
  570. }
  571. }
  572. // either way create interpolator
  573. TemplateVertexData availableInterp = RequestNewInterpolator( size, false );
  574. if( availableInterp != null )
  575. {
  576. bool isPosition = vertexSemantics == TemplateSemantics.POSITION || vertexSemantics == TemplateSemantics.POSITION;
  577. string interpVarName = m_currentTemplateData.VertexFunctionData.OutVarName + "." + availableInterp.VarNameWithSwizzle;
  578. InterpDataHelper vertInfo = m_availableVertData[ TemplateHelperFunctions.IntToUVChannelInfo[ UVChannel ] ];
  579. string interpDecl = string.Format( TemplateHelperFunctions.TemplateVariableDecl, interpVarName, TemplateHelperFunctions.AutoSwizzleData( vertInfo.VarName, vertInfo.VarType, size , isPosition ) );
  580. m_currentDataCollector.AddToVertexInterpolatorsDecl( interpDecl );
  581. string finalVarName = m_currentTemplateData.FragmentFunctionData.InVarName + "." + availableInterp.VarNameWithSwizzle;
  582. m_availableFragData.Add( TemplateHelperFunctions.IntToUVChannelInfo[ UVChannel ], new InterpDataHelper( size, finalVarName ) );
  583. if( size != originalSize )
  584. {
  585. //finalVarName = m_currentTemplateData.FragmentFunctionData.InVarName + "." + availableInterp.VarName + UIUtils.GetAutoSwizzle( originalSize );
  586. finalVarName = m_availableFragData[ TemplateHelperFunctions.IntToUVChannelInfo[ UVChannel ] ].VarName + UIUtils.GetAutoSwizzle( originalSize );
  587. }
  588. return finalVarName;
  589. }
  590. }
  591. return string.Empty;
  592. }
  593. ////////////////////////////////////////////////////////////////////////////////////////////////
  594. bool IsSemanticUsedOnInterpolator( TemplateSemantics semantics )
  595. {
  596. for( int i = 0; i < m_interpolatorData.Interpolators.Count; i++ )
  597. {
  598. if( m_interpolatorData.Interpolators[ i ].Semantics == semantics )
  599. {
  600. return true;
  601. }
  602. }
  603. return false;
  604. }
  605. public bool HasInfo( TemplateInfoOnSematics info, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  606. {
  607. MasterNodePortCategory category = useMasterNodeCategory ? m_currentDataCollector.PortCategory : customCategory;
  608. return ( category == MasterNodePortCategory.Fragment ) ? m_availableFragData.ContainsKey( info ) : m_availableVertData.ContainsKey( info );
  609. }
  610. public InterpDataHelper GetInfo( TemplateInfoOnSematics info, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  611. {
  612. MasterNodePortCategory category = useMasterNodeCategory ? m_currentDataCollector.PortCategory : customCategory;
  613. if( category == MasterNodePortCategory.Fragment )
  614. {
  615. if( !m_availableFragData[ info ].SetAtCompileTime )
  616. {
  617. string defineValue = string.Empty;
  618. if( TemplateHelperFunctions.InfoToDefineFrag.TryGetValue( info, out defineValue ) )
  619. m_currentDataCollector.AddToDefines( -1, defineValue );
  620. }
  621. return m_availableFragData[ info ];
  622. }
  623. else
  624. {
  625. if( !m_availableVertData[ info ].SetAtCompileTime )
  626. {
  627. string defineValue = string.Empty;
  628. if( TemplateHelperFunctions.InfoToDefineVertex.TryGetValue( info, out defineValue ) )
  629. m_currentDataCollector.AddToDefines( -1, defineValue );
  630. }
  631. return m_availableVertData[ info ];
  632. }
  633. }
  634. public string RegisterInfoOnSemantic( TemplateInfoOnSematics info, TemplateSemantics semantic, string name, WirePortDataType dataType, PrecisionType precisionType, bool requestNewInterpolator, string dataName = null )
  635. {
  636. return RegisterInfoOnSemantic( m_currentDataCollector.PortCategory, info, semantic, name, dataType, precisionType, requestNewInterpolator, dataName );
  637. }
  638. // This should only be used to semantics outside the text coord set
  639. public string RegisterInfoOnSemantic( MasterNodePortCategory portCategory, TemplateInfoOnSematics info, TemplateSemantics semantic, string name, WirePortDataType dataType, PrecisionType precisionType, bool requestNewInterpolator, string dataName = null )
  640. {
  641. if( portCategory == MasterNodePortCategory.Vertex )
  642. {
  643. if( m_vertexDataDict.ContainsKey( semantic ) )
  644. {
  645. return m_vertexDataDict[ semantic ].VarName;
  646. }
  647. m_availableVertData.Add( info,
  648. new InterpDataHelper( dataType,
  649. string.Format( TemplateHelperFunctions.TemplateVarFormat,
  650. m_currentTemplateData.VertexFunctionData.InVarName,
  651. name ),true,true ) );
  652. string vertInputVarType = UIUtils.PrecisionWirePortToCgType( precisionType, dataType );
  653. m_currentDataCollector.AddToVertexInput(
  654. string.Format( TemplateHelperFunctions.InterpFullSemantic,
  655. vertInputVarType,
  656. name,
  657. semantic ) );
  658. RegisterOnVertexData( semantic, dataType, name );
  659. return m_availableVertData[ info ].VarName;
  660. }
  661. else
  662. {
  663. //search if the correct vertex data is set ...
  664. TemplateSemantics vertexSemantics = TemplateSemantics.NONE;
  665. foreach( KeyValuePair<TemplateSemantics, TemplateVertexData> kvp in m_vertexDataDict )
  666. {
  667. if( kvp.Value.DataInfo == info )
  668. {
  669. vertexSemantics = kvp.Key;
  670. break;
  671. }
  672. }
  673. // if not, add vertex data and create interpolator
  674. if( vertexSemantics == TemplateSemantics.NONE )
  675. {
  676. vertexSemantics = semantic;
  677. if( !m_vertexDataDict.ContainsKey( vertexSemantics ) )
  678. {
  679. m_availableVertData.Add( info,
  680. new InterpDataHelper( dataType,
  681. string.Format( TemplateHelperFunctions.TemplateVarFormat,
  682. m_currentTemplateData.VertexFunctionData.InVarName,
  683. name ),true,true ) );
  684. string vertInputVarType = UIUtils.PrecisionWirePortToCgType( precisionType, dataType );
  685. m_currentDataCollector.AddToVertexInput(
  686. string.Format( TemplateHelperFunctions.InterpFullSemantic,
  687. vertInputVarType,
  688. name,
  689. vertexSemantics ) );
  690. RegisterOnVertexData( vertexSemantics, dataType, name );
  691. }
  692. }
  693. // either way create interpolator
  694. TemplateVertexData availableInterp = null;
  695. if( requestNewInterpolator || IsSemanticUsedOnInterpolator( semantic ) )
  696. {
  697. availableInterp = RequestNewInterpolator( dataType, false, dataName );
  698. }
  699. else
  700. {
  701. availableInterp = RegisterOnInterpolator( semantic, dataType, dataName );
  702. }
  703. if( availableInterp != null )
  704. {
  705. bool isPosition = vertexSemantics == TemplateSemantics.POSITION || vertexSemantics == TemplateSemantics.POSITION;
  706. string interpVarName = m_currentTemplateData.VertexFunctionData.OutVarName + "." + availableInterp.VarNameWithSwizzle;
  707. string interpDecl = string.Format( TemplateHelperFunctions.TemplateVariableDecl, interpVarName, TemplateHelperFunctions.AutoSwizzleData( m_availableVertData[ info ].VarName, m_availableVertData[ info ].VarType, dataType, isPosition ) );
  708. m_currentDataCollector.AddToVertexInterpolatorsDecl( interpDecl );
  709. string finalVarName = m_currentTemplateData.FragmentFunctionData.InVarName + "." + availableInterp.VarNameWithSwizzle;
  710. m_availableFragData.Add( info, new InterpDataHelper( dataType, finalVarName ) );
  711. return finalVarName;
  712. }
  713. }
  714. return string.Empty;
  715. }
  716. TemplateVertexData RegisterOnInterpolator( TemplateSemantics semantics, WirePortDataType dataType, string vertexDataName = null )
  717. {
  718. if( vertexDataName == null )
  719. {
  720. if( TemplateHelperFunctions.SemanticsDefaultName.ContainsKey( semantics ) )
  721. {
  722. vertexDataName = TemplateHelperFunctions.SemanticsDefaultName[ semantics ];
  723. }
  724. else
  725. {
  726. vertexDataName = string.Empty;
  727. Debug.LogError( "No valid name given to vertex data" );
  728. }
  729. }
  730. TemplateVertexData data = new TemplateVertexData( semantics, dataType, vertexDataName );
  731. m_interpolatorData.Interpolators.Add( data );
  732. string interpolator = string.Format( TemplateHelperFunctions.InterpFullSemantic, UIUtils.WirePortToCgType( dataType ), data.VarName, data.Semantics );
  733. m_currentDataCollector.AddToInterpolators( interpolator );
  734. return data;
  735. }
  736. public void RegisterOnVertexData( TemplateSemantics semantics, WirePortDataType dataType, string varName )
  737. {
  738. m_vertexDataDict.Add( semantics, new TemplateVertexData( semantics, dataType, varName ) );
  739. }
  740. public TemplateVertexData RequestMacroInterpolator( string varName )
  741. {
  742. if( varName != null && m_registeredVertexData.ContainsKey( varName ) )
  743. {
  744. return m_registeredVertexData[ varName ];
  745. }
  746. for( int i = 0; i < m_interpolatorData.AvailableInterpolators.Count; i++ )
  747. {
  748. if( !m_interpolatorData.AvailableInterpolators[ i ].IsFull )
  749. {
  750. TemplateVertexData data = m_interpolatorData.AvailableInterpolators[ i ].RequestChannels( WirePortDataType.FLOAT4, false, varName );
  751. if( data != null )
  752. {
  753. if( !m_registeredVertexData.ContainsKey( data.VarName ) )
  754. {
  755. m_registeredVertexData.Add( data.VarName, data );
  756. }
  757. if( m_interpolatorData.AvailableInterpolators[ i ].Usage == 1 )
  758. {
  759. string interpolator = string.Format( TemplateHelperFunctions.InterpMacro, varName, TemplateHelperFunctions.SemanticToInt[ data.Semantics ] );
  760. m_currentDataCollector.AddToInterpolators( interpolator );
  761. }
  762. return data;
  763. }
  764. }
  765. }
  766. return null;
  767. }
  768. public bool HasRawInterpolatorOfName( string name )
  769. {
  770. return m_interpolatorData.HasRawInterpolatorOfName( name );
  771. }
  772. public TemplateVertexData RequestNewInterpolator( WirePortDataType dataType, bool isColor, string varName = null , bool noInterpolationFlag = false, bool sampleFlag = false )
  773. {
  774. if( varName != null && m_registeredVertexData.ContainsKey( varName ) )
  775. {
  776. return m_registeredVertexData[ varName ];
  777. }
  778. for( int i = 0; i < m_interpolatorData.AvailableInterpolators.Count; i++ )
  779. {
  780. if( !m_interpolatorData.AvailableInterpolators[ i ].IsFull )
  781. {
  782. if( m_interpolatorData.AvailableInterpolators[ i ].Usage != 0 &&
  783. (m_interpolatorData.AvailableInterpolators[ i ].NoInterpolation != noInterpolationFlag ||
  784. m_interpolatorData.AvailableInterpolators[ i ].Sample != sampleFlag ))
  785. continue;
  786. TemplateVertexData data = m_interpolatorData.AvailableInterpolators[ i ].RequestChannels( dataType, isColor, varName );
  787. if( data != null )
  788. {
  789. if( !m_registeredVertexData.ContainsKey( data.VarName ) )
  790. {
  791. m_registeredVertexData.Add( data.VarName, data );
  792. }
  793. if( m_interpolatorData.AvailableInterpolators[ i ].Usage == 1 )
  794. {
  795. m_interpolatorData.AvailableInterpolators[ i ].NoInterpolation = noInterpolationFlag;
  796. m_interpolatorData.AvailableInterpolators[ i ].Sample = sampleFlag;
  797. // First time using this interpolator, so we need to register it
  798. string interpolator = string.Format( TemplateHelperFunctions.TexFullSemantic,
  799. data.VarName, data.Semantics );
  800. if( noInterpolationFlag )
  801. interpolator = "nointerpolation " + interpolator;
  802. if( sampleFlag)
  803. interpolator = "sample " + interpolator;
  804. m_currentDataCollector.AddToInterpolators( interpolator );
  805. }
  806. return data;
  807. }
  808. }
  809. }
  810. // This area is reached if max available interpolators from shader model is reached
  811. // Nevertheless, we register all new interpolators to that list so no imediate compilation errors are thrown
  812. // A warning message is then thrown to warn the user about this
  813. int newInterpId = 1 + TemplateHelperFunctions.SemanticToInt[ m_interpolatorData.AvailableInterpolators[ m_interpolatorData.AvailableInterpolators.Count - 1 ].Semantic ];
  814. if( TemplateHelperFunctions.IntToSemantic.ContainsKey( newInterpId ) )
  815. {
  816. TemplateInterpElement item = new TemplateInterpElement( TemplateHelperFunctions.IntToSemantic[ newInterpId ] );
  817. m_interpolatorData.AvailableInterpolators.Add( item );
  818. TemplateVertexData data = item.RequestChannels( dataType, isColor, varName );
  819. if( data != null )
  820. {
  821. if( !m_registeredVertexData.ContainsKey( data.VarName ) )
  822. {
  823. m_registeredVertexData.Add( data.VarName, data );
  824. }
  825. if( item.Usage == 1 )
  826. {
  827. string interpolator = string.Format( TemplateHelperFunctions.TexFullSemantic, data.VarName, data.Semantics );
  828. m_currentDataCollector.AddToInterpolators( interpolator );
  829. }
  830. return data;
  831. }
  832. }
  833. UIUtils.ShowMessage( "Maximum amount of interpolators exceeded", MessageSeverity.Error );
  834. return null;
  835. }
  836. // Unused channels in interpolators must be set to something so the compiler doesn't generate warnings
  837. public List<string> GetInterpUnusedChannels()
  838. {
  839. List<string> resetInstrucctions = new List<string>();
  840. if( m_interpolatorData != null )
  841. {
  842. for( int i = 0; i < m_interpolatorData.AvailableInterpolators.Count; i++ )
  843. {
  844. if( m_interpolatorData.AvailableInterpolators[ i ].Usage > 0 && !m_interpolatorData.AvailableInterpolators[ i ].IsFull )
  845. {
  846. string channels = string.Empty;
  847. bool[] availableChannels = m_interpolatorData.AvailableInterpolators[ i ].AvailableChannels;
  848. for( int j = 0; j < availableChannels.Length; j++ )
  849. {
  850. if( availableChannels[ j ] )
  851. {
  852. channels += TemplateHelperFunctions.VectorSwizzle[ j ];
  853. }
  854. }
  855. resetInstrucctions.Add( string.Format( "{0}.{1}.{2} = 0;", m_currentTemplateData.VertexFunctionData.OutVarName, m_interpolatorData.AvailableInterpolators[ i ].Name, channels ) );
  856. }
  857. }
  858. }
  859. if( resetInstrucctions.Count > 0 )
  860. {
  861. resetInstrucctions.Insert( 0, "\n//setting value to unused interpolator channels and avoid initialization warnings" );
  862. }
  863. return resetInstrucctions;
  864. }
  865. public bool ContainsSpecialLocalFragVar( TemplateInfoOnSematics info, WirePortDataType type, ref string result )
  866. {
  867. if( m_specialFragmentLocalVars.ContainsKey( info ) )
  868. {
  869. result = m_specialFragmentLocalVars[ info ].LocalVarName;
  870. if( m_specialFragmentLocalVars[ info ].DataType != type )
  871. {
  872. result = TemplateHelperFunctions.AutoSwizzleData( result, m_specialFragmentLocalVars[ info ].DataType, type, false );
  873. }
  874. return true;
  875. }
  876. return false;
  877. }
  878. public bool GetCustomInterpolatedData( TemplateInfoOnSematics info, WirePortDataType type, PrecisionType precisionType, ref string result, bool useMasterNodeCategory, MasterNodePortCategory customCategory )
  879. {
  880. bool isPosition = info == TemplateInfoOnSematics.POSITION ||
  881. info == TemplateInfoOnSematics.CLIP_POS ||
  882. info == TemplateInfoOnSematics.SCREEN_POSITION ||
  883. info == TemplateInfoOnSematics.SCREEN_POSITION_NORMALIZED ||
  884. info == TemplateInfoOnSematics.WORLD_POSITION ||
  885. info == TemplateInfoOnSematics.RELATIVE_WORLD_POS ||
  886. info == TemplateInfoOnSematics.OBJECT_POSITION ||
  887. info == TemplateInfoOnSematics.VIEW_POSITION;
  888. MasterNodePortCategory category = useMasterNodeCategory ? m_currentDataCollector.PortCategory : customCategory;
  889. if( category == MasterNodePortCategory.Vertex )
  890. {
  891. if( m_specialVertexLocalVars.ContainsKey( info ) )
  892. {
  893. result = m_specialVertexLocalVars[ info ].LocalVarName;
  894. if( m_specialVertexLocalVars[ info ].DataType != type )
  895. {
  896. result = TemplateHelperFunctions.AutoSwizzleData( result, m_specialVertexLocalVars[ info ].DataType, type , isPosition );
  897. }
  898. string defineValue = string.Empty;
  899. if( TemplateHelperFunctions.InfoToDefineVertex.TryGetValue( info, out defineValue ) )
  900. m_currentDataCollector.AddToDefines( -1, defineValue );
  901. return true;
  902. }
  903. }
  904. if( category == MasterNodePortCategory.Fragment )
  905. {
  906. if( m_specialFragmentLocalVars.ContainsKey( info ) )
  907. {
  908. result = m_specialFragmentLocalVars[ info ].LocalVarName;
  909. if( m_specialFragmentLocalVars[ info ].DataType != type )
  910. {
  911. result = TemplateHelperFunctions.AutoSwizzleData( result, m_specialFragmentLocalVars[ info ].DataType, type, isPosition );
  912. }
  913. string defineValue = string.Empty;
  914. if( TemplateHelperFunctions.InfoToDefineFrag.TryGetValue( info, out defineValue ))
  915. m_currentDataCollector.AddToDefines( -1, defineValue );
  916. return true;
  917. }
  918. if( m_availableFragData.ContainsKey( info ) )
  919. {
  920. if( m_availableFragData[ info ].IsSingleComponent )
  921. {
  922. result = m_availableFragData[ info ].VarName;
  923. if( m_availableFragData[ info ].VarType != type )
  924. {
  925. result = TemplateHelperFunctions.AutoSwizzleData( result, m_availableFragData[ info ].VarType, type, isPosition );
  926. }
  927. return true;
  928. }
  929. else if( TemplateHelperFunctions.InfoToLocalVar.ContainsKey( info ) && TemplateHelperFunctions.InfoToWirePortType.ContainsKey( info ) )
  930. {
  931. result = TemplateHelperFunctions.InfoToLocalVar[ info ];
  932. m_currentDataCollector.AddLocalVariable( -1, precisionType, TemplateHelperFunctions.InfoToWirePortType[ info ], result, m_availableFragData[ info ].VarName );
  933. return true;
  934. }
  935. }
  936. }
  937. return false;
  938. }
  939. public WirePortDataType GetVertexPositionDataType()
  940. {
  941. InterpDataHelper info = GetInfo( TemplateInfoOnSematics.POSITION , false, MasterNodePortCategory.Vertex);
  942. return info.VarType;
  943. }
  944. public string GetVertexPosition( WirePortDataType type, PrecisionType precisionType, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  945. {
  946. PrecisionType precision = PrecisionType.Float;
  947. string result = string.Empty;
  948. if ( GetCustomInterpolatedData( TemplateInfoOnSematics.OBJECT_POSITION, WirePortDataType.FLOAT3, precision, ref result, useMasterNodeCategory, customCategory ) )
  949. {
  950. return result;
  951. }
  952. if ( HasInfo( TemplateInfoOnSematics.POSITION, useMasterNodeCategory, customCategory ) )
  953. {
  954. InterpDataHelper info = GetInfo( TemplateInfoOnSematics.POSITION, useMasterNodeCategory, customCategory );
  955. if( type != WirePortDataType.OBJECT && type != info.VarType )
  956. return TemplateHelperFunctions.AutoSwizzleData( info.VarName, info.VarType, type,true );
  957. else
  958. return info.VarName;
  959. }
  960. else
  961. {
  962. MasterNodePortCategory portCategory = useMasterNodeCategory ? m_currentDataCollector.PortCategory : customCategory;
  963. string name = "ase_vertex_pos";
  964. string varName = RegisterInfoOnSemantic( portCategory, TemplateInfoOnSematics.POSITION, TemplateSemantics.POSITION, name, WirePortDataType.FLOAT4, precisionType, true );
  965. if( type != WirePortDataType.OBJECT && type != WirePortDataType.FLOAT4 )
  966. return TemplateHelperFunctions.AutoSwizzleData( varName, WirePortDataType.FLOAT4, type,true );
  967. else
  968. return varName;
  969. }
  970. }
  971. private const string InstancingLibStandard = "UnityInstancing.cginc";
  972. private const string InstancingLibSRP = "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl";
  973. public void SetupInstancing()
  974. {
  975. if( !HasInfo( TemplateInfoOnSematics.INSTANCE_ID ) )
  976. {
  977. m_currentDataCollector.AddToPragmas( -1, IOUtils.InstancedPropertiesHeader );
  978. m_currentDataCollector.AddToIncludes( -1, IsSRP ? InstancingLibSRP : InstancingLibStandard );
  979. m_currentDataCollector.AddToVertexInput( Constants.InstanceIdMacro );
  980. m_currentDataCollector.AddToInterpolators( Constants.InstanceIdMacro );
  981. m_currentDataCollector.AddToLocalVariables( MasterNodePortCategory.Vertex, -1, string.Format( "UNITY_SETUP_INSTANCE_ID( {0} );", m_currentTemplateData.VertexFunctionData.InVarName ) );
  982. m_currentDataCollector.AddToLocalVariables( MasterNodePortCategory.Vertex, -1, string.Format( "UNITY_TRANSFER_INSTANCE_ID( {0}, {1} );", m_currentTemplateData.VertexFunctionData.InVarName, m_currentTemplateData.VertexFunctionData.OutVarName ) );
  983. m_currentDataCollector.AddToLocalVariables( MasterNodePortCategory.Fragment, -1, string.Format( "UNITY_SETUP_INSTANCE_ID( {0} );", m_currentTemplateData.FragmentFunctionData.InVarName ) );
  984. }
  985. }
  986. public string GetVertexColor( PrecisionType precisionType )
  987. {
  988. if( HasInfo( TemplateInfoOnSematics.COLOR ) )
  989. {
  990. return GetInfo( TemplateInfoOnSematics.COLOR ).VarName;
  991. }
  992. else
  993. {
  994. string name = "ase_color";
  995. return RegisterInfoOnSemantic( TemplateInfoOnSematics.COLOR, TemplateSemantics.COLOR, name, WirePortDataType.FLOAT4, precisionType, false );
  996. }
  997. }
  998. public string GetVertexNormal( PrecisionType precisionType, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  999. {
  1000. if( HasInfo( TemplateInfoOnSematics.NORMAL, useMasterNodeCategory, customCategory ) )
  1001. {
  1002. InterpDataHelper info = GetInfo( TemplateInfoOnSematics.NORMAL, useMasterNodeCategory, customCategory );
  1003. return TemplateHelperFunctions.AutoSwizzleData( info.VarName, info.VarType, WirePortDataType.FLOAT3 , false);
  1004. }
  1005. else
  1006. {
  1007. MasterNodePortCategory category = useMasterNodeCategory ? m_currentDataCollector.PortCategory : customCategory;
  1008. string name = "ase_normal";
  1009. return RegisterInfoOnSemantic( category, TemplateInfoOnSematics.NORMAL, TemplateSemantics.NORMAL, name, WirePortDataType.FLOAT3, precisionType, false );
  1010. }
  1011. }
  1012. public string GetWorldNormal( PrecisionType precisionType = PrecisionType.Float, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment, bool normalize = false )
  1013. {
  1014. string result = string.Empty;
  1015. if( GetCustomInterpolatedData( TemplateInfoOnSematics.WORLD_NORMAL, WirePortDataType.FLOAT3, precisionType, ref result, useMasterNodeCategory, customCategory ) )
  1016. {
  1017. if( normalize )
  1018. return string.Format( "normalize( {0} )", result );
  1019. else
  1020. return result;
  1021. }
  1022. string varName = normalize ? "normalizeWorldNormal" : GeneratorUtils.WorldNormalStr;
  1023. if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
  1024. return varName;
  1025. string worldNormalValue = string.Empty;
  1026. if( !GetCustomInterpolatedData( TemplateInfoOnSematics.WORLD_NORMAL, WirePortDataType.FLOAT3, precisionType, ref worldNormalValue, false, MasterNodePortCategory.Vertex ) )
  1027. {
  1028. string vertexNormal = GetVertexNormal( precisionType, false, MasterNodePortCategory.Vertex );
  1029. string formatStr = string.Empty;
  1030. if( IsSRP )
  1031. formatStr = "TransformObjectToWorldNormal( {0} )";
  1032. else
  1033. formatStr = "UnityObjectToWorldNormal( {0} )";
  1034. worldNormalValue = string.Format( formatStr, vertexNormal );
  1035. }
  1036. if( normalize )
  1037. worldNormalValue = string.Format( "normalize( {0} )", worldNormalValue );
  1038. RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT3, precisionType, worldNormalValue, useMasterNodeCategory, customCategory );
  1039. return varName;
  1040. }
  1041. public string GetWorldNormal( int uniqueId, PrecisionType precisionType, string normal, string outputId )
  1042. {
  1043. string tanToWorld0 = string.Empty;
  1044. string tanToWorld1 = string.Empty;
  1045. string tanToWorld2 = string.Empty;
  1046. GetWorldTangentTf( precisionType, out tanToWorld0, out tanToWorld1, out tanToWorld2, true );
  1047. string tanNormal = "tanNormal" + outputId;
  1048. m_currentDataCollector.AddLocalVariable( uniqueId, "float3 " + tanNormal + " = " + normal + ";" );
  1049. return string.Format( "float3( dot( {1}, {0} ), dot( {2}, {0} ), dot( {3}, {0} ) )", tanNormal, tanToWorld0, tanToWorld1, tanToWorld2 );
  1050. }
  1051. public string GetVertexTangent( WirePortDataType type, PrecisionType precisionType, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1052. {
  1053. if( HasInfo( TemplateInfoOnSematics.TANGENT, useMasterNodeCategory, customCategory ) )
  1054. {
  1055. InterpDataHelper info = GetInfo( TemplateInfoOnSematics.TANGENT, useMasterNodeCategory, customCategory );
  1056. if( type != WirePortDataType.OBJECT && type != info.VarType )
  1057. return TemplateHelperFunctions.AutoSwizzleData( info.VarName, info.VarType, type , false);
  1058. else
  1059. return info.VarName;
  1060. }
  1061. else
  1062. {
  1063. MasterNodePortCategory category = useMasterNodeCategory ? m_currentDataCollector.PortCategory : customCategory;
  1064. string name = "ase_tangent";
  1065. string varName = RegisterInfoOnSemantic( category, TemplateInfoOnSematics.TANGENT, TemplateSemantics.TANGENT, name, WirePortDataType.FLOAT4, precisionType, false );
  1066. if( type != WirePortDataType.OBJECT && type != WirePortDataType.FLOAT4 )
  1067. return TemplateHelperFunctions.AutoSwizzleData( varName, WirePortDataType.FLOAT4, type , false );
  1068. else
  1069. return varName;
  1070. }
  1071. }
  1072. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1073. public string GetBlendWeights( bool useMasterNodeCategory = true , MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1074. {
  1075. if( HasInfo( TemplateInfoOnSematics.BLENDWEIGHTS , useMasterNodeCategory , customCategory ) )
  1076. {
  1077. InterpDataHelper info = GetInfo( TemplateInfoOnSematics.BLENDWEIGHTS , useMasterNodeCategory , customCategory );
  1078. return info.VarName;
  1079. }
  1080. else
  1081. {
  1082. MasterNodePortCategory category = useMasterNodeCategory ? m_currentDataCollector.PortCategory : customCategory;
  1083. string name = GeneratorUtils.VertexBlendWeightsStr;
  1084. string varName = RegisterInfoOnSemantic( category , TemplateInfoOnSematics.BLENDWEIGHTS , TemplateSemantics.BLENDWEIGHTS , name , WirePortDataType.FLOAT4 ,PrecisionType.Float , false , name );
  1085. return varName;
  1086. }
  1087. }
  1088. public string GetBlendIndices( bool useMasterNodeCategory = true , MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1089. {
  1090. if( HasInfo( TemplateInfoOnSematics.BLENDINDICES , useMasterNodeCategory , customCategory ) )
  1091. {
  1092. InterpDataHelper info = GetInfo( TemplateInfoOnSematics.BLENDINDICES , useMasterNodeCategory , customCategory );
  1093. return info.VarName;
  1094. }
  1095. else
  1096. {
  1097. MasterNodePortCategory category = useMasterNodeCategory ? m_currentDataCollector.PortCategory : customCategory;
  1098. string name = GeneratorUtils.VertexBlendIndicesStr;
  1099. string varName = RegisterInfoOnSemantic( category , TemplateInfoOnSematics.BLENDINDICES , TemplateSemantics.BLENDINDICES , name , WirePortDataType.UINT4 , PrecisionType.Float , false , name );
  1100. return varName;
  1101. }
  1102. }
  1103. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1104. public string GetVertexBitangent( PrecisionType precisionType, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1105. {
  1106. string varName = GeneratorUtils.VertexBitangentStr;
  1107. if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
  1108. return varName;
  1109. string tangentValue = GetVertexTangent( WirePortDataType.FLOAT4, precisionType, false, MasterNodePortCategory.Vertex );
  1110. string normalValue = GetVertexNormal( precisionType, false, MasterNodePortCategory.Vertex );
  1111. string bitangentValue = string.Format( "cross( {0}, {1}.xyz ) * {1}.w * ( unity_WorldTransformParams.w >= 0.0 ? 1.0 : -1.0 )", normalValue, tangentValue );
  1112. RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT3, precisionType, bitangentValue, useMasterNodeCategory, customCategory );
  1113. return varName;
  1114. }
  1115. public string GetWorldTangent( PrecisionType precisionType, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1116. {
  1117. string result = string.Empty;
  1118. if( GetCustomInterpolatedData( TemplateInfoOnSematics.WORLD_TANGENT, WirePortDataType.FLOAT3, precisionType, ref result, useMasterNodeCategory, customCategory ) )
  1119. {
  1120. return result;
  1121. }
  1122. string varName = GeneratorUtils.WorldTangentStr;
  1123. if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
  1124. return varName;
  1125. string worldTangentValue = string.Empty;
  1126. if( !GetCustomInterpolatedData( TemplateInfoOnSematics.WORLD_TANGENT, WirePortDataType.FLOAT3, precisionType, ref worldTangentValue, false, MasterNodePortCategory.Vertex ) )
  1127. {
  1128. string vertexTangent = GetVertexTangent( WirePortDataType.FLOAT4, precisionType, false, MasterNodePortCategory.Vertex );
  1129. string formatStr = string.Empty;
  1130. if( IsSRP )
  1131. formatStr = "TransformObjectToWorldDir( {0}.xyz )";
  1132. else
  1133. formatStr = "UnityObjectToWorldDir( {0} )";
  1134. worldTangentValue = string.Format( formatStr, vertexTangent );
  1135. }
  1136. RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT3, precisionType, worldTangentValue, useMasterNodeCategory, customCategory );
  1137. return varName;
  1138. }
  1139. public string GetTangentSign( PrecisionType precisionType, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1140. {
  1141. string varName = GeneratorUtils.VertexTangentSignStr;
  1142. if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
  1143. return varName;
  1144. string tangentValue = GetVertexTangent( WirePortDataType.FLOAT4, precisionType, false, MasterNodePortCategory.Vertex );
  1145. string tangentSignValue = string.Format( "{0}.w * ( unity_WorldTransformParams.w >= 0.0 ? 1.0 : -1.0 )", tangentValue );
  1146. RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT, precisionType, tangentSignValue, useMasterNodeCategory, customCategory );
  1147. return varName;
  1148. }
  1149. public string GetWorldBinormal( PrecisionType precisionType, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1150. {
  1151. string result = string.Empty;
  1152. if( GetCustomInterpolatedData( TemplateInfoOnSematics.WORLD_BITANGENT, WirePortDataType.FLOAT3, precisionType, ref result, useMasterNodeCategory, customCategory ) )
  1153. {
  1154. return result;
  1155. }
  1156. string varName = GeneratorUtils.WorldBitangentStr;
  1157. if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
  1158. return varName;
  1159. string worldBinormal = string.Empty;
  1160. if( !GetCustomInterpolatedData( TemplateInfoOnSematics.WORLD_BITANGENT, WirePortDataType.FLOAT3, precisionType, ref worldBinormal, false, MasterNodePortCategory.Vertex ) )
  1161. {
  1162. string worldNormal = GetWorldNormal( precisionType, false, MasterNodePortCategory.Vertex );
  1163. string worldtangent = GetWorldTangent( precisionType, false, MasterNodePortCategory.Vertex );
  1164. string tangentSign = GetTangentSign( precisionType, false, MasterNodePortCategory.Vertex );
  1165. worldBinormal = string.Format( "cross( {0}, {1} ) * {2}", worldNormal, worldtangent, tangentSign );
  1166. }
  1167. RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT3, PrecisionType.Float, worldBinormal, useMasterNodeCategory, customCategory );
  1168. return varName;
  1169. }
  1170. public string GetWorldReflection( PrecisionType precisionType, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment, bool normalize = false )
  1171. {
  1172. string varName = GeneratorUtils.WorldReflectionStr;//UIUtils.GetInputValueFromType( SurfaceInputs.WORLD_REFL );
  1173. if( normalize )
  1174. varName = "normalized" + varName;
  1175. if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
  1176. return varName;
  1177. string worldNormal = GetWorldNormal( precisionType );
  1178. string worldViewDir = GetViewDir();
  1179. string worldRefl = string.Format( "reflect( -{0}, {1} )", worldViewDir, worldNormal );
  1180. if( normalize )
  1181. worldRefl = string.Format( "normalize( {0} )", worldRefl );
  1182. m_currentDataCollector.AddLocalVariable( -1, precisionType, WirePortDataType.FLOAT3, varName, worldRefl );
  1183. return varName;
  1184. }
  1185. public string GetWorldReflection( PrecisionType precisionType, string normal )
  1186. {
  1187. string tanToWorld0 = string.Empty;
  1188. string tanToWorld1 = string.Empty;
  1189. string tanToWorld2 = string.Empty;
  1190. GetWorldTangentTf( precisionType, out tanToWorld0, out tanToWorld1, out tanToWorld2 );
  1191. string worldRefl = GetViewDir();
  1192. return string.Format( "reflect( -{0}, float3( dot( {2}, {1} ), dot( {3}, {1} ), dot( {4}, {1} ) ) )", worldRefl, normal, tanToWorld0, tanToWorld1, tanToWorld2 );
  1193. }
  1194. public string GetLightAtten( int uniqueId, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1195. {
  1196. //string result = string.Empty;
  1197. //if( GetCustomInterpolatedData( TemplateInfoOnSematics.WORLD_POSITION, PrecisionType.Float, ref result, useMasterNodeCategory, customCategory ) )
  1198. //{
  1199. // return result;
  1200. //}
  1201. //string varName = GeneratorUtils.WorldPositionStr;//UIUtils.GetInputValueFromType( SurfaceInputs.WORLD_POS );
  1202. //if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
  1203. // return varName;
  1204. //if( !m_availableVertData.ContainsKey( TemplateInfoOnSematics.POSITION ) )
  1205. //{
  1206. // UIUtils.ShowMessage( "Attempting to access inexisting vertex position to calculate world pos" );
  1207. // return "fixed3(0,0,0)";
  1208. //}
  1209. //string vertexPos = m_availableVertData[ TemplateInfoOnSematics.POSITION ].VarName;
  1210. //string worldPosConversion = string.Format( "mul(unity_ObjectToWorld, {0}).xyz", vertexPos );
  1211. //RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT3, PrecisionType.Float, worldPosConversion, useMasterNodeCategory, customCategory );
  1212. //return varName;
  1213. m_currentDataCollector.AddToIncludes( uniqueId, Constants.UnityAutoLightLib );
  1214. m_currentDataCollector.AddToDefines( uniqueId, "ASE_SHADOWS 1" );
  1215. RequestMacroInterpolator( "UNITY_SHADOW_COORDS" );
  1216. //string vOutName = CurrentTemplateData.VertexFunctionData.OutVarName;
  1217. string fInName = CurrentTemplateData.FragmentFunctionData.InVarName;
  1218. string worldPos = GetWorldPos();
  1219. m_currentDataCollector.AddLocalVariable( uniqueId, "UNITY_LIGHT_ATTENUATION( ase_atten, " + fInName + ", " + worldPos + " )" );
  1220. return "ase_atten";
  1221. }
  1222. public string GenerateRotationIndependentObjectScale( ref MasterNodeDataCollector dataCollector, int uniqueId )
  1223. {
  1224. string worldToObjectMatrix = ( m_currentSRPType == TemplateSRPType.BiRP ) ? "unity_WorldToObject" : "GetWorldToObjectMatrix()";
  1225. string value = string.Format( "( 1.0 / float3( length( {0}[ 0 ].xyz ), length( {0}[ 1 ].xyz ), length( {0}[ 2 ].xyz ) ) )", worldToObjectMatrix );
  1226. dataCollector.AddLocalVariable( uniqueId, PrecisionType.Float, WirePortDataType.FLOAT3, GeneratorUtils.ParentObjectScaleStr, value );
  1227. return GeneratorUtils.ParentObjectScaleStr;
  1228. }
  1229. public string GenerateObjectScale( ref MasterNodeDataCollector dataCollector, int uniqueId )
  1230. {
  1231. string worldToObjectMatrix = ( m_currentSRPType == TemplateSRPType.BiRP ) ? "unity_ObjectToWorld" : "GetObjectToWorldMatrix()";
  1232. string value = string.Format( "float3( length( {0}[ 0 ].xyz ), length( {0}[ 1 ].xyz ), length( {0}[ 2 ].xyz ) )", worldToObjectMatrix );
  1233. dataCollector.AddLocalVariable( uniqueId, PrecisionType.Float, WirePortDataType.FLOAT3, GeneratorUtils.ObjectScaleStr, value );
  1234. return GeneratorUtils.ObjectScaleStr;
  1235. }
  1236. public string GenerateObjectPosition( ref MasterNodeDataCollector dataCollector, int uniqueId )
  1237. {
  1238. string value = string.Empty;
  1239. if ( m_currentSRPType != TemplateSRPType.BiRP )
  1240. {
  1241. value = "GetAbsolutePositionWS( UNITY_MATRIX_M._m03_m13_m23 )";
  1242. }
  1243. else
  1244. {
  1245. value = "UNITY_MATRIX_M._m03_m13_m23";
  1246. }
  1247. dataCollector.AddLocalVariable( uniqueId, PrecisionType.Float, WirePortDataType.FLOAT3, GeneratorUtils.ObjectPositionStr, value );
  1248. return GeneratorUtils.ObjectPositionStr;
  1249. }
  1250. public string GenerateObjectBoundsMin( ref MasterNodeDataCollector dataCollector, int uniqueId )
  1251. {
  1252. string value = string.Empty;
  1253. if ( m_currentSRPType != TemplateSRPType.BiRP && ( ASEPackageManagerHelper.CurrentHDRPBaseline >= ASESRPBaseline.ASE_SRP_14 ||
  1254. ASEPackageManagerHelper.CurrentURPBaseline >= ASESRPBaseline.ASE_SRP_14 ) )
  1255. {
  1256. value = "unity_RendererBounds_Min.xyz";
  1257. }
  1258. else
  1259. {
  1260. value = "0"; // not supported
  1261. }
  1262. dataCollector.AddLocalVariable( uniqueId, PrecisionType.Float, WirePortDataType.FLOAT3, GeneratorUtils.ObjectBoundsMinStr, value );
  1263. return GeneratorUtils.ObjectBoundsMinStr;
  1264. }
  1265. public string GenerateObjectBoundsMax( ref MasterNodeDataCollector dataCollector, int uniqueId )
  1266. {
  1267. string value = string.Empty;
  1268. if ( m_currentSRPType != TemplateSRPType.BiRP && ( ASEPackageManagerHelper.CurrentHDRPBaseline >= ASESRPBaseline.ASE_SRP_14 ||
  1269. ASEPackageManagerHelper.CurrentURPBaseline >= ASESRPBaseline.ASE_SRP_14 ) )
  1270. {
  1271. value = "unity_RendererBounds_Max.xyz";
  1272. }
  1273. else
  1274. {
  1275. value = "0"; // not supported
  1276. }
  1277. dataCollector.AddLocalVariable( uniqueId, PrecisionType.Float, WirePortDataType.FLOAT3, GeneratorUtils.ObjectBoundsMaxStr, value );
  1278. return GeneratorUtils.ObjectBoundsMaxStr;
  1279. }
  1280. public string GenerateObjectBoundsSize( ref MasterNodeDataCollector dataCollector, int uniqueId )
  1281. {
  1282. string value = string.Empty;
  1283. if ( m_currentSRPType != TemplateSRPType.BiRP && ( ASEPackageManagerHelper.CurrentHDRPBaseline >= ASESRPBaseline.ASE_SRP_14 ||
  1284. ASEPackageManagerHelper.CurrentURPBaseline >= ASESRPBaseline.ASE_SRP_14 ) )
  1285. {
  1286. value = "( unity_RendererBounds_Max.xyz - unity_RendererBounds_Min.xyz )";
  1287. }
  1288. else
  1289. {
  1290. value = "0"; // not supported
  1291. }
  1292. dataCollector.AddLocalVariable( uniqueId, PrecisionType.Float, WirePortDataType.FLOAT3, GeneratorUtils.ObjectBoundsSizeStr, value );
  1293. return GeneratorUtils.ObjectBoundsSizeStr;
  1294. }
  1295. public string GetPosition( PositionNode.Space space, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1296. {
  1297. // overriding precision
  1298. var precision = PrecisionType.Float;
  1299. string result;
  1300. if ( space == PositionNode.Space.Object )
  1301. {
  1302. result = GetVertexPosition( WirePortDataType.FLOAT3, precision, useMasterNodeCategory, customCategory );
  1303. }
  1304. else if ( space == PositionNode.Space.RelativeWorld )
  1305. {
  1306. result = GetRelativeWorldPos( useMasterNodeCategory, customCategory );
  1307. }
  1308. else if ( space == PositionNode.Space.View )
  1309. {
  1310. result = GetViewPos( useMasterNodeCategory, customCategory );
  1311. }
  1312. else // PositionNode.Space.World
  1313. {
  1314. result = GetWorldPos( useMasterNodeCategory, customCategory );
  1315. }
  1316. return result;
  1317. }
  1318. public string GetWorldPos( bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1319. {
  1320. // overriding precision
  1321. var precision = PrecisionType.Float;
  1322. string result = string.Empty;
  1323. if( GetCustomInterpolatedData( TemplateInfoOnSematics.WORLD_POSITION, WirePortDataType.FLOAT3, precision, ref result, useMasterNodeCategory, customCategory ) )
  1324. {
  1325. return result;
  1326. }
  1327. else if( m_currentSRPType == TemplateSRPType.HDRP )
  1328. {
  1329. if( GetCustomInterpolatedData( TemplateInfoOnSematics.RELATIVE_WORLD_POS, WirePortDataType.FLOAT3, precision, ref result, useMasterNodeCategory, customCategory ) )
  1330. {
  1331. string worldPosVarName = GeneratorUtils.WorldPositionStr;
  1332. string relWorldPosConversion = string.Format( "GetAbsolutePositionWS( {0} )", result );
  1333. m_currentDataCollector.AddLocalVariable( -1, precision, WirePortDataType.FLOAT3, worldPosVarName, relWorldPosConversion );
  1334. return worldPosVarName;
  1335. }
  1336. }
  1337. string varName = GeneratorUtils.WorldPositionStr;//UIUtils.GetInputValueFromType( SurfaceInputs.WORLD_POS );
  1338. if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
  1339. return varName;
  1340. if( !m_availableVertData.ContainsKey( TemplateInfoOnSematics.POSITION ) )
  1341. {
  1342. UIUtils.ShowMessage( "Attempting to access inexisting vertex position to calculate world pos" );
  1343. return "half3(0,0,0)";
  1344. }
  1345. string vertexPos = m_availableVertData[ TemplateInfoOnSematics.POSITION ].VarName;
  1346. string worldPosConversion = string.Empty;
  1347. //Check if world pos already defined in the vertex body
  1348. if( !GetCustomInterpolatedData( TemplateInfoOnSematics.WORLD_POSITION, WirePortDataType.FLOAT3, precision, ref worldPosConversion, false, MasterNodePortCategory.Vertex ) )
  1349. {
  1350. if( m_currentSRPType == TemplateSRPType.HDRP )
  1351. {
  1352. worldPosConversion = string.Format( "GetAbsolutePositionWS( TransformObjectToWorld( ( {0} ).xyz ) )", vertexPos );
  1353. }
  1354. else if( m_currentSRPType == TemplateSRPType.URP )
  1355. {
  1356. worldPosConversion = string.Format( "TransformObjectToWorld( ( {0} ).xyz )", vertexPos );
  1357. }
  1358. else
  1359. {
  1360. worldPosConversion = string.Format( "mul( unity_ObjectToWorld, float4( ( {0} ).xyz, 1 ) ).xyz", vertexPos );
  1361. }
  1362. }
  1363. RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT3, precision, worldPosConversion, useMasterNodeCategory, customCategory );
  1364. return varName;
  1365. }
  1366. public string GetRelativeWorldPos( bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1367. {
  1368. PrecisionType precision = PrecisionType.Float;
  1369. string result = string.Empty;
  1370. if ( GetCustomInterpolatedData( TemplateInfoOnSematics.RELATIVE_WORLD_POS, WirePortDataType.FLOAT3, precision, ref result, useMasterNodeCategory, customCategory ) )
  1371. {
  1372. return result;
  1373. }
  1374. if ( !m_currentDataCollector.HasLocalVariableByName( GeneratorUtils.RelativeWorldPositionStr ) )
  1375. {
  1376. string positionOS = GetVertexPosition( WirePortDataType.FLOAT4, precision, false, MasterNodePortCategory.Vertex );
  1377. string positionCS = GetClipPos( false, MasterNodePortCategory.Vertex );
  1378. string invProjMatrix = GeneratorUtils.GenerateInverseProjection( ref m_currentDataCollector, -1, precision, false, MasterNodePortCategory.Vertex );
  1379. if ( !m_currentDataCollector.HasLocalVariableByName( GeneratorUtils.ViewHPositionStr ) )
  1380. {
  1381. m_currentDataCollector.AddToVertexLocalVariables( -1, precision, WirePortDataType.FLOAT4, GeneratorUtils.ViewHPositionStr,
  1382. string.Format( "mul( {0}, {1} )", invProjMatrix, GeneratorUtils.ClipPositionStr ) );
  1383. }
  1384. m_currentDataCollector.AddToVertexLocalVariables( -1, precision, WirePortDataType.FLOAT3, GeneratorUtils.RelativeWorldPositionStr,
  1385. string.Format( "mul( ( float3x3 )UNITY_MATRIX_I_V, {0}.xyz / {0}.w )", GeneratorUtils.ViewHPositionStr ) );
  1386. RegisterCustomInterpolatedData( GeneratorUtils.RelativeWorldPositionStr, WirePortDataType.FLOAT3, precision, GeneratorUtils.RelativeWorldPositionStr, useMasterNodeCategory, customCategory );
  1387. }
  1388. return GeneratorUtils.RelativeWorldPositionStr;
  1389. }
  1390. public string GetViewPos( bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1391. {
  1392. PrecisionType precision = PrecisionType.Float;
  1393. string result = string.Empty;
  1394. if ( GetCustomInterpolatedData( TemplateInfoOnSematics.VIEW_POSITION, WirePortDataType.FLOAT3, precision, ref result, useMasterNodeCategory, customCategory ) )
  1395. {
  1396. return result;
  1397. }
  1398. MasterNodePortCategory portCategory = useMasterNodeCategory ? m_currentDataCollector.PortCategory : customCategory;
  1399. if ( !m_currentDataCollector.HasLocalVariableByName( GeneratorUtils.ViewPositionStr ) )
  1400. {
  1401. if ( portCategory == MasterNodePortCategory.Vertex || portCategory == MasterNodePortCategory.Tessellation )
  1402. {
  1403. string positionOS = GetVertexPosition( WirePortDataType.FLOAT3, precision, useMasterNodeCategory, customCategory );
  1404. result = string.Format( IsSRP ? "TransformWorldToView( TransformObjectToWorld( {0} ) )" : "UnityObjectToViewPos( {0} )", positionOS );
  1405. }
  1406. else
  1407. {
  1408. string positionWS = GetWorldPos( useMasterNodeCategory, customCategory );
  1409. result = string.Format( IsSRP ? "TransformWorldToView( {0} )" : "UnityWorldToViewPos( {0} )", positionWS );
  1410. }
  1411. m_currentDataCollector.AddLocalVariable( -1, precision, WirePortDataType.FLOAT3, GeneratorUtils.ViewPositionStr, result );
  1412. }
  1413. return GeneratorUtils.ViewPositionStr;
  1414. }
  1415. public string GetClipPosForValue( string customVertexPos, string outputId, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1416. {
  1417. string varName = GeneratorUtils.ClipPositionStr + outputId;
  1418. if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
  1419. return varName;
  1420. if( !m_availableVertData.ContainsKey( TemplateInfoOnSematics.POSITION ) )
  1421. {
  1422. UIUtils.ShowMessage( "Attempting to access inexisting vertex position to calculate clip pos" );
  1423. return "half4(0,0,0,0)";
  1424. }
  1425. string formatStr = string.Empty;
  1426. switch( m_currentSRPType )
  1427. {
  1428. default:
  1429. case TemplateSRPType.BiRP:
  1430. formatStr = "UnityObjectToClipPos( {0} )";
  1431. break;
  1432. case TemplateSRPType.HDRP:
  1433. formatStr = "TransformWorldToHClip( TransformObjectToWorld( ( {0} ).xyz ) )";
  1434. break;
  1435. case TemplateSRPType.URP:
  1436. formatStr = "TransformObjectToHClip( ( {0} ).xyz )";
  1437. break;
  1438. }
  1439. string clipSpaceConversion = string.Format( formatStr, customVertexPos );
  1440. RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT4, PrecisionType.Float, clipSpaceConversion, useMasterNodeCategory, customCategory );
  1441. return varName;
  1442. }
  1443. public string GetClipPos( bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1444. {
  1445. string varName = GeneratorUtils.ClipPositionStr;// "clipPos";
  1446. if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
  1447. return varName;
  1448. if( !m_availableVertData.ContainsKey( TemplateInfoOnSematics.POSITION ) )
  1449. {
  1450. UIUtils.ShowMessage( "Attempting to access inexisting vertex position to calculate clip pos" );
  1451. return "half4(0,0,0,0)";
  1452. }
  1453. string vertexPos = m_availableVertData[ TemplateInfoOnSematics.POSITION ].VarName;
  1454. string formatStr = string.Empty;
  1455. switch( m_currentSRPType )
  1456. {
  1457. default:
  1458. case TemplateSRPType.BiRP:
  1459. formatStr = "UnityObjectToClipPos( {0} )";
  1460. break;
  1461. case TemplateSRPType.HDRP:
  1462. formatStr = "TransformWorldToHClip( TransformObjectToWorld( ( {0} ).xyz ) )";
  1463. break;
  1464. case TemplateSRPType.URP:
  1465. formatStr = "TransformObjectToHClip( ( {0} ).xyz )";
  1466. break;
  1467. }
  1468. string clipSpaceConversion = string.Format( formatStr, vertexPos );
  1469. RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT4, PrecisionType.Float, clipSpaceConversion, useMasterNodeCategory, customCategory );
  1470. return varName;
  1471. }
  1472. public string GetScreenPosForValue( PrecisionType precision, string customVertexPos, string outputId, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1473. {
  1474. // overriding precision
  1475. precision = PrecisionType.Float;
  1476. string varName = UIUtils.GetInputValueFromType( SurfaceInputs.SCREEN_POS ) + outputId;
  1477. if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
  1478. return varName;
  1479. string clipSpacePos = GetClipPosForValue( customVertexPos, outputId, false, MasterNodePortCategory.Vertex );
  1480. string screenPosConversion = string.Empty;
  1481. if( m_currentSRPType == TemplateSRPType.HDRP )
  1482. {
  1483. screenPosConversion = string.Format( "ComputeScreenPos( {0}, _ProjectionParams.x )", clipSpacePos );
  1484. }
  1485. else
  1486. {
  1487. screenPosConversion = string.Format( "ComputeScreenPos( {0} )", clipSpacePos );
  1488. }
  1489. RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT4, precision, screenPosConversion, useMasterNodeCategory, customCategory );
  1490. return varName;
  1491. }
  1492. public string GetScreenPosRaw( PrecisionType precision, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1493. {
  1494. // overriding precision
  1495. precision = PrecisionType.Float;
  1496. string result = string.Empty;
  1497. if ( GetCustomInterpolatedData( TemplateInfoOnSematics.SCREEN_POSITION, WirePortDataType.FLOAT4, precision, ref result, useMasterNodeCategory, customCategory ) )
  1498. {
  1499. return result;
  1500. }
  1501. string varName = UIUtils.GetInputValueFromType( SurfaceInputs.SCREEN_POS );
  1502. if ( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
  1503. {
  1504. return varName;
  1505. }
  1506. string clipSpacePos = GetClipPos( false, MasterNodePortCategory.Vertex );
  1507. string screenPosConversion = string.Empty;
  1508. if ( m_currentSRPType == TemplateSRPType.HDRP )
  1509. {
  1510. screenPosConversion = string.Format( "ComputeScreenPos( {0}, _ProjectionParams.x )", clipSpacePos );
  1511. }
  1512. else
  1513. {
  1514. screenPosConversion = string.Format( "ComputeScreenPos( {0} )", clipSpacePos );
  1515. }
  1516. RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT4, precision, screenPosConversion, useMasterNodeCategory, customCategory );
  1517. return varName;
  1518. }
  1519. public string GetScreenPosNormalized( PrecisionType precision, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1520. {
  1521. string result = string.Empty;
  1522. if( GetCustomInterpolatedData( TemplateInfoOnSematics.SCREEN_POSITION_NORMALIZED, WirePortDataType.FLOAT4, precision, ref result, useMasterNodeCategory, customCategory ) )
  1523. {
  1524. return result;
  1525. }
  1526. if ( !m_currentDataCollector.HasLocalVariableByName( GeneratorUtils.ScreenPosNormStr ) )
  1527. {
  1528. string screenPos = GetScreenPosRaw( precision, useMasterNodeCategory, customCategory );
  1529. string clipPlaneTestOp = string.Format( "{0}.z = ( UNITY_NEAR_CLIP_VALUE >= 0 ) ? {0}.z : {0}.z * 0.5 + 0.5;", GeneratorUtils.ScreenPosNormStr );
  1530. m_currentDataCollector.AddLocalVariable( -1, precision, WirePortDataType.FLOAT4, GeneratorUtils.ScreenPosNormStr, string.Format( GeneratorUtils.NormalizedScreenPosFormat, screenPos ) );
  1531. m_currentDataCollector.AddLocalVariable( -1, clipPlaneTestOp );
  1532. }
  1533. return GeneratorUtils.ScreenPosNormStr;
  1534. }
  1535. public string GetScreenPosCenter( PrecisionType precision, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1536. {
  1537. string screenPosNorm = GetScreenPosNormalized( precision, useMasterNodeCategory, customCategory );
  1538. if ( !m_currentDataCollector.HasLocalVariableByName( GeneratorUtils.ScreenPosCenterStr ) )
  1539. {
  1540. string value = string.Format( "float4( {0}.xy * 2 - 1, 0, 0 )", screenPosNorm );
  1541. m_currentDataCollector.AddLocalVariable( -1, precision, WirePortDataType.FLOAT4, GeneratorUtils.ScreenPosCenterStr, value );
  1542. }
  1543. return GeneratorUtils.ScreenPosCenterStr;
  1544. }
  1545. public string GetScreenPosTiled( PrecisionType precision, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1546. {
  1547. if ( !m_currentDataCollector.HasLocalVariableByName( GeneratorUtils.ScreenPosTiledStr ) )
  1548. {
  1549. string screenPosNorm = GetScreenPosNormalized( precision, useMasterNodeCategory, customCategory );
  1550. string value = string.Format( "frac( float4( ( {0}.x * 2 - 1 ) * _ScreenParams.x / _ScreenParams.y, {0}.y * 2 - 1, 0, 0 ) )", screenPosNorm );
  1551. m_currentDataCollector.AddLocalVariable( -1, precision, WirePortDataType.FLOAT4, GeneratorUtils.ScreenPosTiledStr, value );
  1552. }
  1553. return GeneratorUtils.ScreenPosTiledStr;
  1554. }
  1555. public string GetScreenPosPixel( PrecisionType precision, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1556. {
  1557. if ( !m_currentDataCollector.HasLocalVariableByName( GeneratorUtils.ScreenPosPixelStr ) )
  1558. {
  1559. string screenPosNorm = GetScreenPosNormalized( precision, useMasterNodeCategory, customCategory );
  1560. GeneratorUtils.GenerateScreenPosNormToPixelFunction( ref m_currentDataCollector );
  1561. m_currentDataCollector.AddLocalVariable( -1, precision, WirePortDataType.FLOAT4, GeneratorUtils.ScreenPosPixelStr, GeneratorUtils.GenerateScreenPosNormToPixelFunctionCall( screenPosNorm ) );
  1562. }
  1563. return GeneratorUtils.ScreenPosPixelStr;
  1564. }
  1565. public string GetSurfaceDepth( DepthMode depthMode, PrecisionType precision, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1566. {
  1567. string screenPos = GetScreenPosRaw( precision, useMasterNodeCategory, customCategory );
  1568. string instruction = string.Format( "{0}.z / {0}.w", screenPos );
  1569. string varName;
  1570. switch ( depthMode )
  1571. {
  1572. case DepthMode.DepthLinearEye:
  1573. {
  1574. instruction = GeneratorUtils.ApplyLinearDepthModifier( ref m_currentDataCollector, instruction, depthMode );
  1575. varName = GeneratorUtils.DepthLinearEyeStr;
  1576. break;
  1577. }
  1578. case DepthMode.DepthLinear01:
  1579. {
  1580. instruction = GeneratorUtils.ApplyLinearDepthModifier( ref m_currentDataCollector, instruction, depthMode );
  1581. varName = GeneratorUtils.DepthLinear01Str;
  1582. break;
  1583. }
  1584. case DepthMode.DepthEye:
  1585. {
  1586. instruction = string.Format( "( {0} ) * ( _ProjectionParams.z - _ProjectionParams.y )", instruction );
  1587. varName = GeneratorUtils.DepthEyeStr;
  1588. break;
  1589. }
  1590. case DepthMode.Depth01:
  1591. default:
  1592. {
  1593. varName = GeneratorUtils.DepthRawStr;
  1594. break;
  1595. }
  1596. }
  1597. m_currentDataCollector.AddLocalVariable( -1, precision, WirePortDataType.FLOAT, varName, instruction );
  1598. return varName;
  1599. }
  1600. public string GetViewVector( PrecisionType precisionType = PrecisionType.Float, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment, ViewSpace space = ViewSpace.World )
  1601. {
  1602. string varName;
  1603. switch ( space )
  1604. {
  1605. case ViewSpace.Tangent: varName = GeneratorUtils.TangentViewVectorStr; break;
  1606. case ViewSpace.Object: varName = GeneratorUtils.ObjectViewVectorStr; break;
  1607. case ViewSpace.View: varName = GeneratorUtils.ViewViewVectorStr; break;
  1608. case ViewSpace.World:
  1609. default: varName = GeneratorUtils.WorldViewVectorStr; break;
  1610. }
  1611. if ( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
  1612. {
  1613. return varName;
  1614. }
  1615. string worldPos = GetWorldPos();
  1616. string viewVectorWS = "( _WorldSpaceCameraPos.xyz - " + worldPos + " )";
  1617. string viewVector;
  1618. if ( space == ViewSpace.Tangent )
  1619. {
  1620. string tanToWorld0, tanToWorld1, tanToWorld2;
  1621. GetWorldTangentTf( precisionType, out tanToWorld0, out tanToWorld1, out tanToWorld2 );
  1622. viewVector = string.Format( " {0} * {3}.x + {1} * {3}.y + {2} * {3}.z", tanToWorld0, tanToWorld1, tanToWorld2, viewVectorWS );
  1623. }
  1624. else if ( space == ViewSpace.Object )
  1625. {
  1626. string worldToObjectMatrix = ( m_currentSRPType == TemplateSRPType.BiRP ) ? "unity_WorldToObject" : "GetWorldToObjectMatrix()";
  1627. viewVector = string.Format( "mul( ( float3x3 ){0}, {1} )", worldToObjectMatrix, viewVectorWS );
  1628. }
  1629. else if ( space == ViewSpace.View )
  1630. {
  1631. viewVector = string.Format( "mul( ( float3x3 )UNITY_MATRIX_V, {0} )", viewVectorWS );
  1632. }
  1633. else
  1634. {
  1635. viewVector = viewVectorWS;
  1636. }
  1637. m_currentDataCollector.AddLocalVariable( -1, precisionType, WirePortDataType.FLOAT3, varName, viewVector );
  1638. return varName;
  1639. }
  1640. public string GetViewDir( PrecisionType precisionType = PrecisionType.Float, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment, NormalizeType normalizeType = NormalizeType.Regular, ViewSpace space = ViewSpace.World )
  1641. {
  1642. if ( space == ViewSpace.World )
  1643. {
  1644. string result = string.Empty;
  1645. if ( GetCustomInterpolatedData( TemplateInfoOnSematics.WORLD_VIEW_DIR, WirePortDataType.FLOAT3, precisionType, ref result, useMasterNodeCategory, customCategory ) )
  1646. {
  1647. return result;
  1648. }
  1649. }
  1650. string varName;
  1651. switch ( space )
  1652. {
  1653. case ViewSpace.Tangent: varName = ( normalizeType == NormalizeType.Regular ) ? GeneratorUtils.TangentViewDirectionStr : GeneratorUtils.TangentViewDirectionSafeStr; break;
  1654. case ViewSpace.Object: varName = ( normalizeType == NormalizeType.Regular ) ? GeneratorUtils.ObjectViewDirectionStr : GeneratorUtils.ObjectViewDirectionSafeStr; break;
  1655. case ViewSpace.View: varName = ( normalizeType == NormalizeType.Regular ) ? GeneratorUtils.ViewViewDirectionStr : GeneratorUtils.ViewViewDirectionSafeStr; break;
  1656. case ViewSpace.World:
  1657. default: varName = ( normalizeType == NormalizeType.Regular ) ? GeneratorUtils.WorldViewDirectionStr : GeneratorUtils.WorldViewDirectionSafeStr; break;
  1658. }
  1659. if ( !m_currentDataCollector.HasLocalVariableByName( varName ) )
  1660. {
  1661. string viewVector = GetViewVector( precisionType, useMasterNodeCategory, customCategory, space );
  1662. if ( normalizeType == NormalizeType.Regular )
  1663. {
  1664. viewVector = "normalize( " + viewVector + " )";
  1665. }
  1666. else if ( normalizeType == NormalizeType.Safe )
  1667. {
  1668. viewVector = TemplateHelperFunctions.SafeNormalize( m_currentDataCollector, viewVector );
  1669. }
  1670. m_currentDataCollector.AddLocalVariable( -1, precisionType, WirePortDataType.FLOAT3, varName, viewVector );
  1671. }
  1672. return varName;
  1673. }
  1674. public void GetWorldTangentTf( PrecisionType precisionType, out string tanToWorld0, out string tanToWorld1, out string tanToWorld2, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1675. {
  1676. tanToWorld0 = "tanToWorld0";
  1677. tanToWorld1 = "tanToWorld1";
  1678. tanToWorld2 = "tanToWorld2";
  1679. if( HasCustomInterpolatedData( tanToWorld0, useMasterNodeCategory, customCategory ) ||
  1680. HasCustomInterpolatedData( tanToWorld1, useMasterNodeCategory, customCategory ) ||
  1681. HasCustomInterpolatedData( tanToWorld2, useMasterNodeCategory, customCategory ) )
  1682. return;
  1683. string worldTangent = GetWorldTangent( precisionType, useMasterNodeCategory, customCategory );
  1684. string worldNormal = GetWorldNormal( precisionType, useMasterNodeCategory, customCategory );
  1685. string worldBinormal = GetWorldBinormal( precisionType, useMasterNodeCategory, customCategory );
  1686. string tanToWorldVar0 = string.Format( "float3( {0}.x, {1}.x, {2}.x )", worldTangent, worldBinormal, worldNormal );
  1687. string tanToWorldVar1 = string.Format( "float3( {0}.y, {1}.y, {2}.y )", worldTangent, worldBinormal, worldNormal );
  1688. string tanToWorldVar2 = string.Format( "float3( {0}.z, {1}.z, {2}.z )", worldTangent, worldBinormal, worldNormal );
  1689. if( customCategory == MasterNodePortCategory.Vertex )
  1690. {
  1691. RegisterCustomInterpolatedData( tanToWorld0, WirePortDataType.FLOAT3, precisionType, tanToWorldVar0, useMasterNodeCategory, customCategory );
  1692. RegisterCustomInterpolatedData( tanToWorld1, WirePortDataType.FLOAT3, precisionType, tanToWorldVar1, useMasterNodeCategory, customCategory );
  1693. RegisterCustomInterpolatedData( tanToWorld2, WirePortDataType.FLOAT3, precisionType, tanToWorldVar2, useMasterNodeCategory, customCategory );
  1694. }
  1695. else
  1696. {
  1697. m_currentDataCollector.AddLocalVariable( -1, precisionType, WirePortDataType.FLOAT3, tanToWorld0, tanToWorldVar0 );
  1698. m_currentDataCollector.AddLocalVariable( -1, precisionType, WirePortDataType.FLOAT3, tanToWorld1, tanToWorldVar1 );
  1699. m_currentDataCollector.AddLocalVariable( -1, precisionType, WirePortDataType.FLOAT3, tanToWorld2, tanToWorldVar2 );
  1700. }
  1701. }
  1702. public string GetTangentToWorldMatrixFast( PrecisionType precisionType, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1703. {
  1704. string worldTangent = GetWorldTangent( precisionType );
  1705. string worldNormal = GetWorldNormal( precisionType );
  1706. string worldBinormal = GetWorldBinormal( precisionType );
  1707. string varName = GeneratorUtils.TangentToWorldFastStr;
  1708. if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
  1709. return varName;
  1710. string result = string.Format( "float3x3( {0}.x, {1}.x, {2}.x, {0}.y, {1}.y, {2}.y, {0}.z, {1}.z, {2}.z )", worldTangent, worldBinormal, worldNormal );
  1711. m_currentDataCollector.AddLocalVariable( -1, precisionType, WirePortDataType.FLOAT3x3, GeneratorUtils.TangentToWorldFastStr, result );
  1712. return GeneratorUtils.TangentToWorldFastStr;
  1713. }
  1714. public string GetTangentToWorldMatrixPrecise( PrecisionType precisionType, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1715. {
  1716. string worldToTangent = GetWorldToTangentMatrix( precisionType, useMasterNodeCategory, customCategory );
  1717. GeneratorUtils.Add3x3InverseFunction( ref m_currentDataCollector, UIUtils.PrecisionWirePortToCgType( precisionType, WirePortDataType.FLOAT ) );
  1718. m_currentDataCollector.AddLocalVariable( -1, precisionType, WirePortDataType.FLOAT3x3, GeneratorUtils.TangentToWorldPreciseStr, string.Format( GeneratorUtils.Inverse3x3Header, worldToTangent ) );
  1719. return GeneratorUtils.TangentToWorldPreciseStr;
  1720. }
  1721. public string GetWorldToTangentMatrix( PrecisionType precisionType, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1722. {
  1723. string worldTangent = GetWorldTangent( precisionType );
  1724. string worldNormal = GetWorldNormal( precisionType );
  1725. string worldBinormal = GetWorldBinormal( precisionType );
  1726. string varName = GeneratorUtils.WorldToTangentStr;// "worldToTanMat";
  1727. if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
  1728. return varName;
  1729. string worldTanMat = string.Format( "float3x3( {0}, {1}, {2} )", worldTangent, worldBinormal, worldNormal );
  1730. m_currentDataCollector.AddLocalVariable( -1, precisionType, WirePortDataType.FLOAT3x3, varName, worldTanMat );
  1731. return varName;
  1732. }
  1733. public string GetObjectToViewPos( PrecisionType precision, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1734. {
  1735. // overriding precision
  1736. precision = PrecisionType.Float;
  1737. string varName = "objectToViewPos";
  1738. if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
  1739. return varName;
  1740. string vertexPos = GetVertexPosition( WirePortDataType.FLOAT3, precision, false, MasterNodePortCategory.Vertex );
  1741. string formatStr = string.Empty;
  1742. if( IsSRP )
  1743. formatStr = "TransformWorldToView( TransformObjectToWorld( {0} ) )";
  1744. else
  1745. formatStr = "UnityObjectToViewPos( {0} )";
  1746. string objectToViewPosValue = string.Format( formatStr, vertexPos );
  1747. RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT3, precision, objectToViewPosValue, useMasterNodeCategory, customCategory );
  1748. return varName;
  1749. }
  1750. public string GetEyeDepth( PrecisionType precision, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment, bool linear01 = false )
  1751. {
  1752. // overriding precision
  1753. precision = PrecisionType.Float;
  1754. string varName = "eyeDepth";
  1755. if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
  1756. return varName;
  1757. string objectToView = GetObjectToViewPos( precision, false, MasterNodePortCategory.Vertex );
  1758. string eyeDepthValue = string.Format( "-{0}.z", objectToView );
  1759. if( linear01 )
  1760. {
  1761. eyeDepthValue += " * _ProjectionParams.w";
  1762. }
  1763. RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT, precision, eyeDepthValue, useMasterNodeCategory, customCategory );
  1764. return varName;
  1765. }
  1766. public string GetObjectSpaceLightDir( PrecisionType precisionType, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1767. {
  1768. if( !IsSRP )
  1769. {
  1770. m_currentDataCollector.AddToIncludes( -1, Constants.UnityLightingLib );
  1771. m_currentDataCollector.AddToIncludes( -1, Constants.UnityAutoLightLib );
  1772. }
  1773. string varName = "objectSpaceLightDir";
  1774. if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
  1775. return varName;
  1776. string vertexPos = GetVertexPosition( WirePortDataType.FLOAT4, precisionType, false, MasterNodePortCategory.Vertex );
  1777. string objectSpaceLightDir = string.Empty;
  1778. switch( m_currentSRPType )
  1779. {
  1780. default:
  1781. case TemplateSRPType.BiRP:
  1782. objectSpaceLightDir = string.Format( "ObjSpaceLightDir( {0} )", vertexPos );
  1783. break;
  1784. case TemplateSRPType.HDRP:
  1785. string worldSpaceLightDir = GetWorldSpaceLightDir( precisionType, useMasterNodeCategory, customCategory );
  1786. objectSpaceLightDir = string.Format( "mul( GetWorldToObjectMatrix(), {0} ).xyz", worldSpaceLightDir );
  1787. break;
  1788. case TemplateSRPType.URP:
  1789. objectSpaceLightDir = "mul( GetWorldToObjectMatrix(), _MainLightPosition ).xyz";
  1790. break;
  1791. }
  1792. RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT3, precisionType, objectSpaceLightDir, useMasterNodeCategory, customCategory );
  1793. return varName;
  1794. }
  1795. public string GetWorldSpaceLightDir( PrecisionType precision, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1796. {
  1797. if( !IsSRP )
  1798. {
  1799. m_currentDataCollector.AddToIncludes( -1, Constants.UnityLightingLib );
  1800. m_currentDataCollector.AddToIncludes( -1, Constants.UnityAutoLightLib );
  1801. AddLateDirective( AdditionalLineType.Custom, "//This is a late directive" );
  1802. }
  1803. else
  1804. {
  1805. string lightVar;
  1806. if( m_currentSRPType == TemplateSRPType.HDRP )
  1807. {
  1808. AddHDLightInfo();
  1809. lightVar = "-" + string.Format( TemplateHelperFunctions.HDLightInfoFormat, "0", "forward" );
  1810. }
  1811. else
  1812. {
  1813. lightVar = "_MainLightPosition.xyz";
  1814. }
  1815. return m_currentDataCollector.SafeNormalizeLightDir ? string.Format( "SafeNormalize( {0} )", lightVar ) : lightVar;
  1816. }
  1817. string varName = "worldSpaceLightDir";
  1818. if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
  1819. return varName;
  1820. string worldPos = GetWorldPos( useMasterNodeCategory, customCategory );
  1821. string worldSpaceLightDir = string.Format( "UnityWorldSpaceLightDir( {0} )", worldPos );
  1822. if( m_currentDataCollector.SafeNormalizeLightDir )
  1823. {
  1824. if( IsSRP )
  1825. {
  1826. worldSpaceLightDir = string.Format( "SafeNormalize( {0} )", worldSpaceLightDir );
  1827. }
  1828. else
  1829. {
  1830. m_currentDataCollector.AddToIncludes( -1, Constants.UnityBRDFLib );
  1831. worldSpaceLightDir = string.Format( "Unity_SafeNormalize( {0} )", worldSpaceLightDir );
  1832. }
  1833. }
  1834. m_currentDataCollector.AddLocalVariable( -1, precision, WirePortDataType.FLOAT3, varName, worldSpaceLightDir );
  1835. return varName;
  1836. }
  1837. public void RegisterCustomInterpolatedData( string name, WirePortDataType dataType, PrecisionType precision, string vertexInstruction, bool useMasterNodeCategory = true,
  1838. MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment, bool noInterpolationFlag = false, bool sampleFlag = false )
  1839. {
  1840. bool addLocalVariable = !name.Equals( vertexInstruction );
  1841. MasterNodePortCategory category = useMasterNodeCategory ? m_currentDataCollector.PortCategory : customCategory;
  1842. if( !m_customInterpolatedData.ContainsKey( name ) )
  1843. {
  1844. m_customInterpolatedData.Add( name, new TemplateCustomData( name, dataType ) );
  1845. }
  1846. if( !m_customInterpolatedData[ name ].IsVertex )
  1847. {
  1848. m_customInterpolatedData[ name ].IsVertex = true;
  1849. if( addLocalVariable )
  1850. m_currentDataCollector.AddToVertexLocalVariables( -1, precision, dataType, name, vertexInstruction );
  1851. }
  1852. if( category == MasterNodePortCategory.Fragment )
  1853. {
  1854. if( !m_customInterpolatedData[ name ].IsFragment )
  1855. {
  1856. m_customInterpolatedData[ name ].IsFragment = true;
  1857. TemplateVertexData interpData = RequestNewInterpolator( dataType, false,null, noInterpolationFlag,sampleFlag );
  1858. if( interpData == null )
  1859. {
  1860. Debug.LogErrorFormat( "Could not assign interpolator of type {0} to variable {1}", dataType, name );
  1861. return;
  1862. }
  1863. m_currentDataCollector.AddToVertexLocalVariables( -1, m_currentTemplateData.VertexFunctionData.OutVarName + "." + interpData.VarNameWithSwizzle, name );
  1864. m_currentDataCollector.AddToLocalVariables( -1, precision, dataType, name, m_currentTemplateData.FragmentFunctionData.InVarName + "." + interpData.VarNameWithSwizzle );
  1865. }
  1866. }
  1867. }
  1868. public bool HasCustomInterpolatedData( string name, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1869. {
  1870. if( m_customInterpolatedData.ContainsKey( name ) )
  1871. {
  1872. MasterNodePortCategory category = useMasterNodeCategory ? m_currentDataCollector.PortCategory : customCategory;
  1873. return ( category == MasterNodePortCategory.Fragment ) ? m_customInterpolatedData[ name ].IsFragment : m_customInterpolatedData[ name ].IsVertex;
  1874. }
  1875. return false;
  1876. }
  1877. public bool HasFragmentInputParams
  1878. {
  1879. get
  1880. {
  1881. if( m_fragmentInputParams != null )
  1882. return m_fragmentInputParams.Count > 0;
  1883. return false;
  1884. }
  1885. }
  1886. public string FragInputParamsStr
  1887. {
  1888. get
  1889. {
  1890. string value = string.Empty;
  1891. if( m_fragmentInputParams != null && m_fragmentInputParams.Count > 0 )
  1892. {
  1893. int count = m_fragmentInputParams.Count;
  1894. if( count > 0 )
  1895. {
  1896. value = ", ";
  1897. foreach( KeyValuePair<TemplateSemantics, TemplateInputParameters> kvp in m_fragmentInputParams )
  1898. {
  1899. value += kvp.Value.Declaration;
  1900. if( --count > 0 )
  1901. {
  1902. value += " , ";
  1903. }
  1904. }
  1905. }
  1906. }
  1907. return value;
  1908. }
  1909. }
  1910. public string VertexInputParamsStr
  1911. {
  1912. get
  1913. {
  1914. string value = string.Empty;
  1915. if( m_vertexInputParams != null && m_vertexInputParams.Count > 0 )
  1916. {
  1917. int count = m_vertexInputParams.Count;
  1918. if( count > 0 )
  1919. {
  1920. value = ", ";
  1921. foreach( KeyValuePair<TemplateSemantics, TemplateInputParameters> kvp in m_vertexInputParams )
  1922. {
  1923. value += kvp.Value.Declaration;
  1924. if( --count > 0 )
  1925. {
  1926. value += " , ";
  1927. }
  1928. }
  1929. }
  1930. }
  1931. return value;
  1932. }
  1933. }
  1934. public void Destroy()
  1935. {
  1936. m_currentTemplate = null;
  1937. m_currentTemplateData = null;
  1938. m_currentDataCollector = null;
  1939. if( m_fullSrpBatcherPropertiesList != null )
  1940. {
  1941. m_fullSrpBatcherPropertiesList.Clear();
  1942. m_fullSrpBatcherPropertiesList = null;
  1943. }
  1944. if( m_srpBatcherPropertiesList != null )
  1945. {
  1946. m_srpBatcherPropertiesList.Clear();
  1947. m_srpBatcherPropertiesList = null;
  1948. }
  1949. if( m_srpBatcherPropertiesDict != null )
  1950. {
  1951. m_srpBatcherPropertiesDict.Clear();
  1952. m_srpBatcherPropertiesDict = null;
  1953. }
  1954. if( m_lateDirectivesList != null )
  1955. {
  1956. m_lateDirectivesList.Clear();
  1957. m_lateDirectivesList = null;
  1958. }
  1959. if( m_lateDirectivesDict != null )
  1960. {
  1961. m_lateDirectivesDict.Clear();
  1962. m_lateDirectivesDict = null;
  1963. }
  1964. if( m_registeredVertexData != null )
  1965. {
  1966. m_registeredVertexData.Clear();
  1967. m_registeredVertexData = null;
  1968. }
  1969. if( m_vertexInputParams != null )
  1970. {
  1971. m_vertexInputParams.Clear();
  1972. m_vertexInputParams = null;
  1973. }
  1974. if( m_fragmentInputParams != null )
  1975. {
  1976. m_fragmentInputParams.Clear();
  1977. m_fragmentInputParams = null;
  1978. }
  1979. if( m_vertexDataDict != null )
  1980. {
  1981. m_vertexDataDict.Clear();
  1982. m_vertexDataDict = null;
  1983. }
  1984. if( m_interpolatorData != null )
  1985. {
  1986. m_interpolatorData.Destroy();
  1987. m_interpolatorData = null;
  1988. }
  1989. if( m_availableFragData != null )
  1990. {
  1991. m_availableFragData.Clear();
  1992. m_availableFragData = null;
  1993. }
  1994. if( m_availableVertData != null )
  1995. {
  1996. m_availableVertData.Clear();
  1997. m_availableVertData = null;
  1998. }
  1999. if( m_customInterpolatedData != null )
  2000. {
  2001. m_customInterpolatedData.Clear();
  2002. m_customInterpolatedData = null;
  2003. }
  2004. if( m_specialVertexLocalVars != null )
  2005. {
  2006. m_specialVertexLocalVars.Clear();
  2007. m_specialVertexLocalVars = null;
  2008. }
  2009. if( m_specialFragmentLocalVars != null )
  2010. {
  2011. m_specialFragmentLocalVars.Clear();
  2012. m_specialFragmentLocalVars = null;
  2013. }
  2014. }
  2015. public void BuildCBuffer( int nodeId )
  2016. {
  2017. m_fullSrpBatcherPropertiesList.Clear();
  2018. if( m_srpBatcherPropertiesList.Count > 0 )
  2019. {
  2020. var regex = new Regex( @"(\d)\s+\b" );
  2021. m_srpBatcherPropertiesList.Sort( ( a, b ) =>
  2022. {
  2023. var matchA = regex.Match( a.PropertyName );
  2024. int sizeA = 0;
  2025. if( matchA.Groups.Count > 1 && matchA.Groups[ 1 ].Value.Length > 0 )
  2026. sizeA = Convert.ToInt32( matchA.Groups[ 1 ].Value, System.Globalization.CultureInfo.InvariantCulture );
  2027. var matchB = regex.Match( b.PropertyName );
  2028. int sizeB = 0;
  2029. if( matchB.Groups.Count > 1 && matchB.Groups[ 1 ].Value.Length > 0 )
  2030. sizeB = Convert.ToInt32( matchB.Groups[ 1 ].Value, System.Globalization.CultureInfo.InvariantCulture );
  2031. return sizeB.CompareTo( sizeA );
  2032. } );
  2033. m_fullSrpBatcherPropertiesList.Insert(0, new PropertyDataCollector( nodeId, IOUtils.SRPCBufferPropertiesBegin ));
  2034. m_fullSrpBatcherPropertiesList.AddRange( m_srpBatcherPropertiesList );
  2035. m_fullSrpBatcherPropertiesList.Add( new PropertyDataCollector( nodeId, IOUtils.SRPCBufferPropertiesEnd ) );
  2036. }
  2037. }
  2038. public void DumpSRPBatcher()
  2039. {
  2040. for( int i = 0; i < m_srpBatcherPropertiesList.Count; i++ )
  2041. {
  2042. Debug.Log( i + "::" + m_srpBatcherPropertiesList[ i ].PropertyName );
  2043. }
  2044. }
  2045. public const string GlobalMaxInterpolatorReachedMsg = "Maximum amount of interpolators reached!\nPlease consider optmizing your shader!";
  2046. public const string MaxInterpolatorSMReachedMsg = "Maximum amount of interpolators reached for current shader model on pass {0}! Please consider increasing the shader model to {1}!";
  2047. public void CheckInterpolatorOverflow( string currShaderModel, string passName )
  2048. {
  2049. int maxInterpolatorAmount = TemplateHelperFunctions.AvailableInterpolators[ currShaderModel ];
  2050. int currInterpolatorAmount = 1 + TemplateHelperFunctions.SemanticToInt[ InterpData.AvailableInterpolators[ InterpData.AvailableInterpolators.Count - 1 ].Semantic ];
  2051. if( currInterpolatorAmount > maxInterpolatorAmount )
  2052. {
  2053. string shaderModel = string.Empty;
  2054. if( TemplateHelperFunctions.GetShaderModelForInterpolatorAmount( currInterpolatorAmount, ref shaderModel ) )
  2055. {
  2056. UIUtils.ShowMessage( string.Format( MaxInterpolatorSMReachedMsg, passName, shaderModel ), MessageSeverity.Error );
  2057. }
  2058. else
  2059. {
  2060. UIUtils.ShowMessage( GlobalMaxInterpolatorReachedMsg, MessageSeverity.Error );
  2061. }
  2062. }
  2063. }
  2064. public Dictionary<TemplateSemantics, TemplateInputParameters> FragInputParameters { get { return m_fragmentInputParams; } }
  2065. public bool HasVertexInputParams
  2066. {
  2067. get
  2068. {
  2069. if( m_vertexInputParams != null )
  2070. return m_vertexInputParams.Count > 0;
  2071. return false;
  2072. }
  2073. }
  2074. public Dictionary<TemplateSemantics, TemplateInputParameters> VertexInputParameters { get { return m_vertexInputParams; } }
  2075. public TemplateData CurrentTemplateData { get { return m_currentTemplateData; } }
  2076. public int MultipassSubshaderIdx { get { return m_multipassSubshaderIdx; } }
  2077. public int MultipassPassIdx { get { return m_multipassPassIdx; } }
  2078. public TemplateSRPType CurrentSRPType { get { return m_currentSRPType; } set { m_currentSRPType = value; } }
  2079. public bool IsHDRP { get { return m_currentSRPType == TemplateSRPType.HDRP; } }
  2080. public bool IsLWRP { get { return m_currentSRPType == TemplateSRPType.URP; } }
  2081. public bool IsSRP { get { return ( m_currentSRPType == TemplateSRPType.URP || m_currentSRPType == TemplateSRPType.HDRP ); } }
  2082. public TemplateInterpData InterpData { get { return m_interpolatorData; } }
  2083. public List<PropertyDataCollector> LateDirectivesList { get { return m_lateDirectivesList; } }
  2084. public List<PropertyDataCollector> SrpBatcherPropertiesList { get { return m_srpBatcherPropertiesList; } }
  2085. public List<PropertyDataCollector> FullSrpBatcherPropertiesList { get { return m_fullSrpBatcherPropertiesList; } }
  2086. public Dictionary<TemplateSemantics, TemplateVertexData> VertexDataDict { get { return m_vertexDataDict; } }
  2087. }
  2088. }