GeneratorUtils.cs 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. namespace AmplifyShaderEditor
  4. {
  5. public static class GeneratorUtils
  6. {
  7. public const string VertexBlendWeightsStr = "ase_blendWeights";
  8. public const string VertexBlendIndicesStr = "ase_blendIndices";
  9. public const string ObjectPositionStr = "ase_objectPosition";
  10. public const string ObjectScaleStr = "ase_objectScale";
  11. public const string ParentObjectScaleStr = "ase_parentObjectScale";
  12. public const string ObjectBoundsMinStr = "ase_objectBoundsMin";
  13. public const string ObjectBoundsMaxStr = "ase_objectBoundsMax";
  14. public const string ObjectBoundsSizeStr = "ase_objectBoundsSize";
  15. public const string ScreenDepthStr = "ase_screenDepth";
  16. public const string DepthLinearEyeStr = "ase_depthLinearEye";
  17. public const string DepthLinear01Str = "ase_depthLinear01";
  18. public const string DepthEyeStr = "ase_depthEye";
  19. public const string DepthRawStr = "ase_depthRaw";
  20. public const string ViewPositionStr = "ase_positionVS";
  21. public const string ViewHPositionStr = "ase_hpositionVS";
  22. public const string WorldViewVectorStr = "ase_viewVectorWS";
  23. public const string WorldViewDirectionStr = "ase_viewDirWS";
  24. public const string WorldViewDirectionSafeStr = "ase_viewDirSafeWS";
  25. public const string ObjectViewVectorStr = "ase_viewVectorOS";
  26. public const string ObjectViewDirectionStr = "ase_viewDirOS";
  27. public const string ObjectViewDirectionSafeStr = "ase_viewDirSafeOS";
  28. public const string ViewViewVectorStr = "ase_viewVectorVS";
  29. public const string ViewViewDirectionStr = "ase_viewDirVS";
  30. public const string ViewViewDirectionSafeStr = "ase_viewDirSafeVS";
  31. public const string TangentViewVectorStr = "ase_viewVectorTS";
  32. public const string TangentViewDirectionStr = "ase_viewDirTS";
  33. public const string TangentViewDirectionSafeStr = "ase_viewDirSafeTS";
  34. public const string ClipPositionStr = "ase_positionCS";
  35. public const string VertexPosition3Str = "ase_positionOS";
  36. public const string VertexPosition4Str = "ase_positionOS4f";
  37. public const string VertexNormalStr = "ase_normalOS";
  38. public const string VertexTangentStr = "ase_tangentOS";
  39. public const string VertexTangentSignStr = "ase_tangentSign";
  40. public const string VertexBitangentStr = "ase_bitangentOS";
  41. public const string ScreenPosRawStr = "ase_positionSS";
  42. public const string ScreenPosNormStr = "ase_positionSSNorm";
  43. public const string ScreenPosCenterStr = "ase_positionSS_Center";
  44. public const string ScreenPosTiledStr = "ase_positionSS_Tiled";
  45. public const string ScreenPosPixelStr = "ase_positionSS_Pixel";
  46. public const string NormalizedScreenPosFormat = "{0} / {0}.w";
  47. public const string GrabScreenPositionStr = "ase_grabScreenPos";
  48. public const string GrabScreenPositionNormalizedStr = "ase_grabScreenPosNorm";
  49. public const string WorldPositionStr = "ase_positionWS";
  50. public const string RelativeWorldPositionStr = "ase_positionRWS";
  51. public const string VFaceStr = "ase_vface";
  52. public const string ShadowCoordsStr = "ase_shadowCoords";
  53. public const string WorldLightDirStr = "ase_lightDirWS";
  54. public const string ObjectLightDirStr = "ase_lightDirOS";
  55. public const string WorldNormalStr = "ase_normalWS";
  56. public const string NormalizedWorldNormalStr = "ase_normalWSNorm";
  57. public const string WorldReflectionStr = "ase_reflectionWS";
  58. public const string WorldTangentStr = "ase_tangentWS";
  59. public const string WorldBitangentStr = "ase_bitangentWS";
  60. public const string WorldToTangentStr = "ase_worldToTangent";
  61. public const string ObjectToTangentStr = "ase_objectToTangent";
  62. public const string TangentToWorldPreciseStr = "ase_tangentToWorldPrecise";
  63. public const string TangentToWorldFastStr = "ase_tangentToWorldFast";
  64. public const string TangentToObjectStr = "ase_tangentToObject";
  65. public const string TangentToObjectFastStr = "ase_tangentToObjectFast";
  66. public static readonly string[] DepthModeStr = { "Linear Eye", "Linear 0-1", "Eye", "0-1" };
  67. public static readonly string[] DepthModeVarNameStr = { "depthLinearEye", "depthLinear01_", "depthEye", "depth01_" };
  68. private const string Float3Format = "float3 {0} = {1};";
  69. private const string Float4Format = "float4 {0} = {1};";
  70. private const string GrabFunctionHeader = "inline float4 ASE_ComputeGrabScreenPos( float4 pos )";
  71. private const string GrabFunctionCall = "ASE_ComputeGrabScreenPos( {0} )";
  72. private const string Identity4x4 = "ase_identity4x4";
  73. private const string FaceVertex = "ase_faceVertex";
  74. private const string ase_MatrixInvP = "ase_matrixInvP";
  75. private const string ase_MatrixInvVP = "ase_matrixInvVP";
  76. private const string ase_MatrixInvMVP = "ase_matrixInvMVP";
  77. private const string SurfaceScreenPosFormat = "{0}( {1}.screenPos.xyz , {1}.screenPos.w + 1e-7 )";
  78. private static readonly string[] GrabFunctionBody = {
  79. "#if UNITY_UV_STARTS_AT_TOP",
  80. "float scale = -1.0;",
  81. "#else",
  82. "float scale = 1.0;",
  83. "#endif",
  84. "float4 o = pos;",
  85. "o.y = pos.w * 0.5f;",
  86. "o.y = ( pos.y - o.y ) * _ProjectionParams.x * scale + o.y;",
  87. "return o;"
  88. };
  89. private static readonly string InverseProjectionMatrixFunctionHeader = "InverseProjectionMatrix()";
  90. private static readonly string[] InverseProjectionMatrixFunctionBody =
  91. {
  92. "float4x4 InverseProjectionMatrix()\n",
  93. "{\n",
  94. "\tfloat4x4 m = UNITY_MATRIX_P;\n",
  95. "\tfloat n11 = m[ 0 ][ 0 ];\n",
  96. "\tfloat n22 = m[ 1 ][ 1 ];\n",
  97. "\tfloat n33 = m[ 2 ][ 2 ];\n",
  98. "\tfloat n34 = m[ 3 ][ 2 ];\n",
  99. "\tfloat n43 = m[ 2 ][ 3 ];\n",
  100. "\tfloat t11 = -n22 * n34 * n43;\n",
  101. "\tfloat det = n11 * t11;\n",
  102. "\tfloat idet = 1.0f / det;\n",
  103. "\tm[ 0 ][ 0 ] = +t11* idet;\n",
  104. "\tm[ 1 ][ 1 ] = -n11* n34 * n43* idet;\n",
  105. "\tm[ 2 ][ 2 ] = 0;\n",
  106. "\tm[ 2 ][ 3 ] = -n11* n22 * n43* idet;\n",
  107. "\tm[ 3 ][ 2 ] = -n11* n22 * n34* idet;\n",
  108. "\tm[ 3 ][ 3 ] = +n11* n22 * n33* idet;\n",
  109. "\treturn m;\n",
  110. "}\n"
  111. };
  112. private static readonly string ScreenPosNormToPixelFunctionHeader = "ASEScreenPositionNormalizedToPixel( {0} )";
  113. private static readonly string[] ScreenPosNormToPixelFunctionBody = {
  114. "float4 ASEScreenPositionNormalizedToPixel( float4 screenPosNorm )\n",
  115. "{\n",
  116. "\tfloat4 screenPosPixel = screenPosNorm * float4( _ScreenParams.xy, 1, 1 );\n",
  117. "\t#if UNITY_UV_STARTS_AT_TOP\n",
  118. "\t\tscreenPosPixel.xy = float2( screenPosPixel.x, ( _ProjectionParams.x < 0 ) ? _ScreenParams.y - screenPosPixel.y : screenPosPixel.y );\n",
  119. "\t#else\n",
  120. "\t\tscreenPosPixel.xy = float2( screenPosPixel.x, ( _ProjectionParams.x > 0 ) ? _ScreenParams.y - screenPosPixel.y : screenPosPixel.y );\n",
  121. "\t#endif\n",
  122. "\treturn screenPosPixel;\n",
  123. "}\n"
  124. };
  125. // MATRIX IDENTITY
  126. static public string GenerateIdentity4x4( ref MasterNodeDataCollector dataCollector, int uniqueId )
  127. {
  128. dataCollector.AddLocalVariable( uniqueId, "float4x4 ase_identity4x4 = float4x4(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1);" );
  129. return Identity4x4;
  130. }
  131. // OBJECT POSITION
  132. static public string GenerateObjectPosition( ref MasterNodeDataCollector dataCollector, int uniqueId )
  133. {
  134. if ( dataCollector.IsTemplate )
  135. return dataCollector.TemplateDataCollectorInstance.GenerateObjectPosition( ref dataCollector, uniqueId );
  136. string value = "UNITY_MATRIX_M._m03_m13_m23";
  137. dataCollector.AddLocalVariable( uniqueId, PrecisionType.Float, WirePortDataType.FLOAT3, ObjectPositionStr, value );
  138. return ObjectPositionStr;
  139. }
  140. // OBJECT SCALE
  141. static public string GenerateObjectScale( ref MasterNodeDataCollector dataCollector, int uniqueId )
  142. {
  143. if ( dataCollector.IsTemplate )
  144. return dataCollector.TemplateDataCollectorInstance.GenerateObjectScale( ref dataCollector, uniqueId );
  145. //string value= "1/float3( length( unity_WorldToObject[ 0 ].xyz ), length( unity_WorldToObject[ 1 ].xyz ), length( unity_WorldToObject[ 2 ].xyz ) );";
  146. string value = "float3( length( unity_ObjectToWorld[ 0 ].xyz ), length( unity_ObjectToWorld[ 1 ].xyz ), length( unity_ObjectToWorld[ 2 ].xyz ) )";
  147. dataCollector.AddLocalVariable( uniqueId, PrecisionType.Float, WirePortDataType.FLOAT3, ObjectScaleStr, value );
  148. return ObjectScaleStr;
  149. }
  150. static public string GenerateRotationIndependentObjectScale( ref MasterNodeDataCollector dataCollector, int uniqueId )
  151. {
  152. if ( dataCollector.IsTemplate )
  153. return dataCollector.TemplateDataCollectorInstance.GenerateRotationIndependentObjectScale( ref dataCollector, uniqueId );
  154. string value = "(1.0/float3( length( unity_WorldToObject[ 0 ].xyz ), length( unity_WorldToObject[ 1 ].xyz ), length( unity_WorldToObject[ 2 ].xyz ) ))";
  155. dataCollector.AddLocalVariable( uniqueId, PrecisionType.Float, WirePortDataType.FLOAT3, ParentObjectScaleStr, value );
  156. return ParentObjectScaleStr;
  157. }
  158. // POSITION
  159. static public string GeneratePosition( ref MasterNodeDataCollector dataCollector, int uniqueId, PositionNode.Space space )
  160. {
  161. if ( dataCollector.IsTemplate )
  162. {
  163. return dataCollector.TemplateDataCollectorInstance.GetPosition( space );
  164. }
  165. string result;
  166. if ( space == PositionNode.Space.Object )
  167. {
  168. result = GenerateVertexPosition( ref dataCollector, uniqueId, WirePortDataType.FLOAT3 );
  169. }
  170. else if ( space == PositionNode.Space.RelativeWorld )
  171. {
  172. result = GenerateRelativeWorldPosition( ref dataCollector, uniqueId );
  173. }
  174. else if ( space == PositionNode.Space.View )
  175. {
  176. result = GenerateViewPosition( ref dataCollector, uniqueId );
  177. }
  178. else // PositionNode.Space.World
  179. {
  180. result = GenerateWorldPosition( ref dataCollector, uniqueId );
  181. }
  182. return result;
  183. }
  184. static public string GenerateWorldPosition( ref MasterNodeDataCollector dataCollector, int uniqueId )
  185. {
  186. PrecisionType precision = PrecisionType.Float;
  187. if ( dataCollector.IsTemplate )
  188. {
  189. return dataCollector.TemplateDataCollectorInstance.GetWorldPos();
  190. }
  191. if ( !dataCollector.HasLocalVariableByName( WorldPositionStr ) )
  192. {
  193. dataCollector.AddToInput( -1, SurfaceInputs.WORLD_POS, precision );
  194. string result;
  195. if ( dataCollector.PortCategory == MasterNodePortCategory.Vertex || dataCollector.PortCategory == MasterNodePortCategory.Tessellation )
  196. {
  197. result = "mul( unity_ObjectToWorld, " + Constants.VertexShaderInputStr + ".vertex )";
  198. }
  199. else
  200. {
  201. result = Constants.InputVarStr + ".worldPos";
  202. }
  203. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT3, WorldPositionStr, result );
  204. }
  205. return WorldPositionStr;
  206. }
  207. static public string GenerateRelativeWorldPosition( ref MasterNodeDataCollector dataCollector, int uniqueId )
  208. {
  209. PrecisionType precision = PrecisionType.Float;
  210. if ( dataCollector.IsTemplate )
  211. {
  212. return dataCollector.TemplateDataCollectorInstance.GetRelativeWorldPos();
  213. }
  214. if ( !dataCollector.HasLocalVariableByName( RelativeWorldPositionStr ) )
  215. {
  216. if ( dataCollector.TesselationActive )
  217. {
  218. string positionWS = GenerateWorldPosition( ref dataCollector, uniqueId );
  219. string invProjMatrix = GeneratorUtils.GenerateInverseProjection( ref dataCollector, uniqueId, precision );
  220. if ( !dataCollector.HasLocalVariableByName( ClipPositionStr ) )
  221. {
  222. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT4, ClipPositionStr,
  223. string.Format( "UnityWorldToClipPos( {0} )", positionWS ) );
  224. }
  225. if ( !dataCollector.HasLocalVariableByName( ViewHPositionStr ) )
  226. {
  227. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT4, ViewHPositionStr,
  228. string.Format( "mul( {0}, {1} )", invProjMatrix, ClipPositionStr ) );
  229. }
  230. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT3, RelativeWorldPositionStr,
  231. string.Format( "mul( ( float3x3 )UNITY_MATRIX_I_V, {0}.xyz / {0}.w )", ViewHPositionStr ) );
  232. }
  233. else
  234. {
  235. string positionOS = GenerateVertexPosition( ref dataCollector, uniqueId, WirePortDataType.FLOAT4, false, MasterNodePortCategory.Vertex );
  236. string positionRWSOutput = string.Format( "{0}.{1}", Constants.VertexShaderOutputStr, RelativeWorldPositionStr );
  237. string positionRWSInput = string.Format( "{0}.{1}", Constants.InputVarStr, RelativeWorldPositionStr );
  238. string invProjMatrix = GeneratorUtils.GenerateInverseProjection( ref dataCollector, uniqueId, precision, false, MasterNodePortCategory.Vertex );
  239. dataCollector.AddToInput( uniqueId, RelativeWorldPositionStr, WirePortDataType.FLOAT3 );
  240. if ( !dataCollector.HasLocalVariableByName( ClipPositionStr ) )
  241. {
  242. dataCollector.AddToVertexLocalVariables( uniqueId, precision, WirePortDataType.FLOAT4, ClipPositionStr,
  243. string.Format( "UnityObjectToClipPos( {0} )", positionOS ) );
  244. }
  245. if ( !dataCollector.HasLocalVariableByName( ViewHPositionStr ) )
  246. {
  247. dataCollector.AddToVertexLocalVariables( uniqueId, precision, WirePortDataType.FLOAT4, ViewHPositionStr,
  248. string.Format( "mul( {0}, {1} )", invProjMatrix, ClipPositionStr ) );
  249. }
  250. dataCollector.AddToVertexLocalVariables( uniqueId, precision, WirePortDataType.FLOAT3, RelativeWorldPositionStr,
  251. string.Format( "mul( ( float3x3 )UNITY_MATRIX_I_V, {0}.xyz / {0}.w )", ViewHPositionStr ) );
  252. dataCollector.AddToVertexLocalVariables( uniqueId, positionRWSOutput, RelativeWorldPositionStr );
  253. if ( dataCollector.PortCategory == MasterNodePortCategory.Fragment || dataCollector.PortCategory == MasterNodePortCategory.Debug )
  254. {
  255. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT3, RelativeWorldPositionStr, positionRWSInput );
  256. }
  257. }
  258. }
  259. return RelativeWorldPositionStr;
  260. }
  261. static public string GenerateViewPosition( ref MasterNodeDataCollector dataCollector, int uniqueId )
  262. {
  263. PrecisionType precision = PrecisionType.Float;
  264. if ( dataCollector.IsTemplate )
  265. {
  266. return dataCollector.TemplateDataCollectorInstance.GetViewPos();
  267. }
  268. if ( !dataCollector.HasLocalVariableByName( ViewPositionStr ) )
  269. {
  270. string result;
  271. if ( dataCollector.PortCategory == MasterNodePortCategory.Vertex || dataCollector.PortCategory == MasterNodePortCategory.Tessellation )
  272. {
  273. string positionOS = Constants.VertexShaderInputStr + ".vertex";
  274. result = string.Format( "UnityObjectToViewPos( {0} )", positionOS );
  275. }
  276. else
  277. {
  278. dataCollector.AddToInput( -1, SurfaceInputs.WORLD_POS, precision );
  279. string positionWS = Constants.InputVarStr + ".worldPos";
  280. result = string.Format( "UnityWorldToViewPos( {0} )", positionWS );
  281. }
  282. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT3, ViewPositionStr, result );
  283. }
  284. return ViewPositionStr;
  285. }
  286. // WORLD REFLECTION
  287. static public string GenerateWorldReflection( ref MasterNodeDataCollector dataCollector, int uniqueId, bool normalize = false )
  288. {
  289. if ( dataCollector.IsTemplate )
  290. return dataCollector.TemplateDataCollectorInstance.GetWorldReflection( UIUtils.CurrentWindow.CurrentGraph.CurrentPrecision, true, MasterNodePortCategory.Fragment, normalize );
  291. string precisionType = UIUtils.PrecisionWirePortToCgType( UIUtils.CurrentWindow.CurrentGraph.CurrentPrecision, WirePortDataType.FLOAT3 );
  292. string result = string.Empty;
  293. if ( !dataCollector.DirtyNormal )
  294. result = Constants.InputVarStr + ".worldRefl";
  295. else
  296. result = "WorldReflectionVector( " + Constants.InputVarStr + ", " + precisionType + "( 0, 0, 1 ) )";
  297. if ( dataCollector.PortCategory == MasterNodePortCategory.Vertex || dataCollector.PortCategory == MasterNodePortCategory.Tessellation )
  298. result = "UnityObjectToWorldNormal( " + Constants.VertexShaderInputStr + ".normal )";
  299. if ( normalize )
  300. {
  301. result = string.Format( "normalize( {0} )", result );
  302. }
  303. dataCollector.AddToLocalVariables( dataCollector.PortCategory, uniqueId, string.Concat( precisionType, " ", WorldReflectionStr, " = ", result, ";" ) );
  304. return WorldReflectionStr;
  305. }
  306. // WORLD NORMAL
  307. static public string GenerateWorldNormal( ref MasterNodeDataCollector dataCollector, int uniqueId, PrecisionType precisionType, string normal, string outputId )
  308. {
  309. if ( dataCollector.IsTemplate )
  310. return dataCollector.TemplateDataCollectorInstance.GetWorldNormal( uniqueId, precisionType, normal, outputId );
  311. string tanToWorld = GenerateTangentToWorldMatrixFast( ref dataCollector, uniqueId, precisionType );
  312. return string.Format( "mul({0},{1})", tanToWorld, normal );
  313. }
  314. static public string GenerateWorldNormal( ref MasterNodeDataCollector dataCollector, int uniqueId, bool normalize = false )
  315. {
  316. PrecisionType precision = UIUtils.CurrentWindow.CurrentGraph.CurrentPrecision;
  317. if ( dataCollector.IsTemplate )
  318. return dataCollector.TemplateDataCollectorInstance.GetWorldNormal( precision, true, MasterNodePortCategory.Fragment, normalize );
  319. string precisionType = UIUtils.PrecisionWirePortToCgType( precision, WirePortDataType.FLOAT3 );
  320. string result = string.Empty;
  321. if ( !dataCollector.DirtyNormal )
  322. result = Constants.InputVarStr + ".worldNormal";
  323. else
  324. result = "WorldNormalVector( " + Constants.InputVarStr + ", " + precisionType + "( 0, 0, 1 ) )";
  325. if ( dataCollector.PortCategory == MasterNodePortCategory.Vertex || dataCollector.PortCategory == MasterNodePortCategory.Tessellation )
  326. result = "UnityObjectToWorldNormal( " + Constants.VertexShaderInputStr + ".normal )";
  327. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT3, WorldNormalStr, result );
  328. if ( normalize )
  329. {
  330. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT3, NormalizedWorldNormalStr, "normalize( " + WorldNormalStr + " )" );
  331. return NormalizedWorldNormalStr;
  332. }
  333. return WorldNormalStr;
  334. }
  335. // WORLD TANGENT
  336. static public string GenerateWorldTangent( ref MasterNodeDataCollector dataCollector, int uniqueId )
  337. {
  338. if ( dataCollector.IsTemplate )
  339. return dataCollector.TemplateDataCollectorInstance.GetWorldTangent( UIUtils.CurrentWindow.CurrentGraph.CurrentPrecision );
  340. string precisionType = UIUtils.PrecisionWirePortToCgType( UIUtils.CurrentWindow.CurrentGraph.CurrentPrecision, WirePortDataType.FLOAT3 );
  341. string result = "WorldNormalVector( " + Constants.InputVarStr + ", " + precisionType + "( 1, 0, 0 ) )";
  342. if ( dataCollector.PortCategory == MasterNodePortCategory.Vertex || dataCollector.PortCategory == MasterNodePortCategory.Tessellation )
  343. result = "UnityObjectToWorldDir( " + Constants.VertexShaderInputStr + ".tangent.xyz )";
  344. dataCollector.AddLocalVariable( uniqueId, UIUtils.CurrentWindow.CurrentGraph.CurrentPrecision, WirePortDataType.FLOAT3, WorldTangentStr, result );
  345. //dataCollector.AddToLocalVariables( dataCollector.PortCategory, uniqueId, string.Concat( precisionType, " ", WorldTangentStr, " = ", result, ";" ) );
  346. return WorldTangentStr;
  347. }
  348. // WORLD BITANGENT
  349. static public string GenerateWorldBitangent( ref MasterNodeDataCollector dataCollector, int uniqueId )
  350. {
  351. if ( dataCollector.IsTemplate )
  352. return dataCollector.TemplateDataCollectorInstance.GetWorldBinormal( UIUtils.CurrentWindow.CurrentGraph.CurrentPrecision );
  353. string precisionType = UIUtils.PrecisionWirePortToCgType( UIUtils.CurrentWindow.CurrentGraph.CurrentPrecision, WirePortDataType.FLOAT3 );
  354. string result = "WorldNormalVector( " + Constants.InputVarStr + ", " + precisionType + "( 0, 1, 0 ) )";
  355. if ( dataCollector.PortCategory == MasterNodePortCategory.Vertex || dataCollector.PortCategory == MasterNodePortCategory.Tessellation )
  356. {
  357. string worldNormal = GenerateWorldNormal( ref dataCollector, uniqueId );
  358. string worldTangent = GenerateWorldTangent( ref dataCollector, uniqueId );
  359. dataCollector.AddToVertexLocalVariables( uniqueId, string.Format( "half tangentSign = {0}.tangent.w * ( unity_WorldTransformParams.w >= 0.0 ? 1.0 : -1.0 );", Constants.VertexShaderInputStr ) );
  360. result = "cross( " + worldNormal + ", " + worldTangent + " ) * tangentSign";
  361. }
  362. dataCollector.AddToLocalVariables( dataCollector.PortCategory, uniqueId, string.Concat( precisionType, " ", WorldBitangentStr, " = ", result, ";" ) );
  363. return WorldBitangentStr;
  364. }
  365. // OBJECT TO TANGENT MATRIX
  366. static public string GenerateObjectToTangentMatrix( ref MasterNodeDataCollector dataCollector, int uniqueId, PrecisionType precision )
  367. {
  368. string normal = GenerateVertexNormal( ref dataCollector, uniqueId, precision );
  369. string tangent = GenerateVertexTangent( ref dataCollector, uniqueId, precision, WirePortDataType.FLOAT3 );
  370. string bitangen = GenerateVertexBitangent( ref dataCollector, uniqueId, precision );
  371. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT3x3, ObjectToTangentStr, "float3x3( " + tangent + ", " + bitangen + ", " + normal + " )" );
  372. return ObjectToTangentStr;
  373. }
  374. // TANGENT TO OBJECT
  375. //static public string GenerateTangentToObjectMatrixFast( ref MasterNodeDataCollector dataCollector, int uniqueId, PrecisionType precision )
  376. //{
  377. // string normal = GenerateVertexNormal( ref dataCollector, uniqueId, precision );
  378. // string tangent = GenerateVertexTangent( ref dataCollector, uniqueId, precision );
  379. // string bitangent = GenerateVertexBitangent( ref dataCollector, uniqueId, precision );
  380. // string result = string.Format( "float3x3({0}.x,{1}.x,{2}.x,{0}.y,{1}.y,{2}.y,{0}.z,{1}.z,{2}.z)",tangent,bitangent,normal );
  381. // dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT3x3, TangentToObjectFastStr, result );
  382. // return TangentToObjectFastStr;
  383. //}
  384. //static public string GenerateTangentToObjectMatrixPrecise( ref MasterNodeDataCollector dataCollector, int uniqueId, PrecisionType precision )
  385. //{
  386. // string objectToTangent = GenerateObjectToTangentMatrix( ref dataCollector, uniqueId, precision );
  387. // Add3x3InverseFunction( ref dataCollector, UIUtils.PrecisionWirePortToCgType( precision, WirePortDataType.FLOAT ) );
  388. // dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT3x3, TangentToObjectStr, string.Format( Inverse3x3Header, objectToTangent ) );
  389. // return TangentToObjectStr;
  390. //}
  391. // WORLD TO TANGENT MATRIX
  392. static public string GenerateWorldToTangentMatrix( ref MasterNodeDataCollector dataCollector, int uniqueId, PrecisionType precision )
  393. {
  394. if ( dataCollector.IsTemplate )
  395. return dataCollector.TemplateDataCollectorInstance.GetWorldToTangentMatrix( precision );
  396. if ( dataCollector.IsFragmentCategory )
  397. {
  398. dataCollector.ForceNormal = true;
  399. dataCollector.AddToInput( -1, SurfaceInputs.WORLD_NORMAL, precision );
  400. dataCollector.AddToInput( -1, SurfaceInputs.INTERNALDATA, addSemiColon: false );
  401. }
  402. string worldNormal = GenerateWorldNormal( ref dataCollector, uniqueId );
  403. string worldTangent = GenerateWorldTangent( ref dataCollector, uniqueId );
  404. string worldBitangent = GenerateWorldBitangent( ref dataCollector, uniqueId );
  405. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT3x3, WorldToTangentStr, "float3x3( " + worldTangent + ", " + worldBitangent + ", " + worldNormal + " )" );
  406. return WorldToTangentStr;
  407. }
  408. // TANGENT TO WORLD
  409. static public string GenerateTangentToWorldMatrixFast( ref MasterNodeDataCollector dataCollector, int uniqueId, PrecisionType precision )
  410. {
  411. if ( dataCollector.IsTemplate )
  412. return dataCollector.TemplateDataCollectorInstance.GetTangentToWorldMatrixFast( precision );
  413. if ( dataCollector.IsFragmentCategory )
  414. {
  415. dataCollector.ForceNormal = true;
  416. dataCollector.AddToInput( -1, SurfaceInputs.WORLD_NORMAL, precision );
  417. dataCollector.AddToInput( -1, SurfaceInputs.INTERNALDATA, addSemiColon: false );
  418. }
  419. string worldNormal = GenerateWorldNormal( ref dataCollector, uniqueId );
  420. string worldTangent = GenerateWorldTangent( ref dataCollector, uniqueId );
  421. string worldBitangent = GenerateWorldBitangent( ref dataCollector, uniqueId );
  422. string result = string.Format( "float3x3({0}.x,{1}.x,{2}.x,{0}.y,{1}.y,{2}.y,{0}.z,{1}.z,{2}.z)", worldTangent, worldBitangent, worldNormal );
  423. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT3x3, TangentToWorldFastStr, result );
  424. return TangentToWorldFastStr;
  425. }
  426. static public string GenerateTangentToWorldMatrixPrecise( ref MasterNodeDataCollector dataCollector, int uniqueId, PrecisionType precision )
  427. {
  428. if ( dataCollector.IsTemplate )
  429. return dataCollector.TemplateDataCollectorInstance.GetTangentToWorldMatrixPrecise( precision );
  430. if ( dataCollector.IsFragmentCategory )
  431. {
  432. dataCollector.ForceNormal = true;
  433. dataCollector.AddToInput( -1, SurfaceInputs.WORLD_NORMAL, precision );
  434. dataCollector.AddToInput( -1, SurfaceInputs.INTERNALDATA, addSemiColon: false );
  435. }
  436. string worldToTangent = GenerateWorldToTangentMatrix( ref dataCollector, uniqueId, precision );
  437. Add3x3InverseFunction( ref dataCollector, UIUtils.PrecisionWirePortToCgType( precision, WirePortDataType.FLOAT ) );
  438. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT3x3, TangentToWorldPreciseStr, string.Format( Inverse3x3Header, worldToTangent ) );
  439. return TangentToWorldPreciseStr;
  440. }
  441. // MATRICES
  442. static public string GenerateInverseProjection( ref MasterNodeDataCollector dataCollector, int uniqueId, PrecisionType precision, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  443. {
  444. string value;
  445. if ( dataCollector.IsSRP )
  446. {
  447. value = "UNITY_MATRIX_I_P";
  448. }
  449. else
  450. {
  451. dataCollector.AddFunction( InverseProjectionMatrixFunctionBody[ 0 ], InverseProjectionMatrixFunctionBody, false );
  452. value = InverseProjectionMatrixFunctionHeader;
  453. }
  454. if ( !useMasterNodeCategory && customCategory == MasterNodePortCategory.Vertex )
  455. {
  456. dataCollector.AddToVertexLocalVariables( uniqueId, precision, WirePortDataType.FLOAT4x4, ase_MatrixInvP, value );
  457. }
  458. else
  459. {
  460. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT4x4, ase_MatrixInvP, value );
  461. }
  462. return ase_MatrixInvP;
  463. }
  464. static public string GenerateInverseViewProjection( ref MasterNodeDataCollector dataCollector, int uniqueId, PrecisionType precision, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  465. {
  466. string value;
  467. if ( dataCollector.IsSRP )
  468. {
  469. value = "mul( GetViewToWorldMatrix(), UNITY_MATRIX_I_P )";
  470. }
  471. else
  472. {
  473. value = string.Format( "mul( UNITY_MATRIX_I_V, {0} )", GenerateInverseProjection( ref dataCollector, uniqueId, precision ) );
  474. }
  475. if ( !useMasterNodeCategory && customCategory == MasterNodePortCategory.Vertex )
  476. {
  477. dataCollector.AddToVertexLocalVariables( uniqueId, precision, WirePortDataType.FLOAT4x4, ase_MatrixInvVP, value );
  478. }
  479. else
  480. {
  481. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT4x4, ase_MatrixInvVP, value );
  482. }
  483. return ase_MatrixInvVP;
  484. }
  485. static public string GenerateInverseModelViewProjection( ref MasterNodeDataCollector dataCollector, int uniqueId, PrecisionType precision, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  486. {
  487. string value;
  488. if ( dataCollector.IsSRP )
  489. {
  490. value = "mul( GetWorldToObjectMatrix(), mul( GetViewToWorldMatrix(), UNITY_MATRIX_I_P ) )";
  491. }
  492. else
  493. {
  494. value = string.Format( "mul( unity_WorldToObject, {0} )", GenerateInverseViewProjection( ref dataCollector, uniqueId, precision ) );
  495. }
  496. if ( !useMasterNodeCategory && customCategory == MasterNodePortCategory.Vertex )
  497. {
  498. dataCollector.AddToVertexLocalVariables( uniqueId, precision, WirePortDataType.FLOAT4x4, ase_MatrixInvMVP, value );
  499. }
  500. else
  501. {
  502. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT4x4, ase_MatrixInvMVP, value );
  503. }
  504. return ase_MatrixInvMVP;
  505. }
  506. // SAMPLER STATES
  507. static public string GenerateSamplerState( ref MasterNodeDataCollector dataCollector, int uniqueId, string propertyName, VariableMode varMode, bool returnPropertyName = false )
  508. {
  509. string sampler = string.Format( Constants.SamplerFormat, propertyName );
  510. string samplerDecl = string.Empty;
  511. ParentGraph outsideGraph = UIUtils.CurrentWindow.OutsideGraph;
  512. if ( outsideGraph.IsSRP )
  513. //if( dataCollector.IsSRP )
  514. samplerDecl = string.Format( Constants.SamplerDeclSRPFormat, sampler ) + ";";
  515. else
  516. samplerDecl = string.Format( Constants.SamplerDeclFormat, sampler ) + ";";
  517. if ( varMode == VariableMode.Create )
  518. dataCollector.AddToUniforms( uniqueId, samplerDecl );
  519. if ( returnPropertyName )
  520. return propertyName;
  521. else
  522. return sampler;
  523. }
  524. public static string GetPropertyFromSamplerState( string sampler )
  525. {
  526. if ( sampler.StartsWith( "sampler" ) )
  527. return sampler.Remove( 0, 7 );
  528. else
  529. return sampler;
  530. }
  531. public static string GetSamplerDeclaraction( string texture, WirePortDataType type, string termination = "" )
  532. {
  533. return GetSamplerDeclaraction( texture, Constants.WireToTexture[ type ], termination );
  534. }
  535. public static string GetSamplerDeclaraction( string sampler, TextureType type, string termination = "" )
  536. {
  537. ParentGraph outsideGraph = UIUtils.CurrentWindow.OutsideGraph;
  538. if ( outsideGraph.SamplingMacros || type == TextureType.Texture2DArray )
  539. {
  540. if ( outsideGraph.IsSRP )
  541. return string.Format( Constants.SamplerDeclSRPFormat, sampler ) + termination;
  542. else
  543. return string.Format( Constants.SamplerDeclFormat, sampler ) + termination;
  544. }
  545. // we don't use sampler states when macros are not available
  546. return string.Empty;
  547. }
  548. // PROPERTY MACRO
  549. public static string GetPropertyDeclaraction( string texture, WirePortDataType type, string termination = "" )
  550. {
  551. return GetPropertyDeclaraction( texture, Constants.WireToTexture[ type ], termination );
  552. }
  553. public static string GetPropertyDeclaraction( string texture, TextureType type, string termination = "" )
  554. {
  555. if ( type == TextureType.Texture1D )
  556. return "sampler1D " + texture + termination;
  557. ParentGraph outsideGraph = UIUtils.CurrentWindow.OutsideGraph;
  558. if ( outsideGraph.SamplingMacros || type == TextureType.Texture2DArray )
  559. {
  560. if ( outsideGraph.IsSRP )
  561. return string.Format( Constants.TexDeclarationNoSamplerSRPMacros[ type ], texture ) + termination;
  562. else
  563. return string.Format( Constants.TexDeclarationNoSamplerStandardMacros[ type ], texture ) + termination;
  564. }
  565. return UIUtils.TextureTypeToCgType( type ) + " " + texture + termination;
  566. }
  567. // SAMPLING CALL
  568. public static string GenerateSamplingCall( ref MasterNodeDataCollector dataCollector, WirePortDataType type, string property, string samplerState, string uv, MipType mip = MipType.Auto, params string[] mipData )
  569. {
  570. ParentGraph ousideGraph = UIUtils.CurrentWindow.OutsideGraph;
  571. string result = string.Empty;
  572. string mipSuffix = string.Empty;
  573. //samplerState = GetPropertyFromSamplerState( samplerState );
  574. TextureType textureType = Constants.WireToTexture[ type ];
  575. bool usingMacro = false;
  576. if ( ousideGraph.SamplingMacros || textureType == TextureType.Texture2DArray )
  577. usingMacro = true;
  578. switch ( mip )
  579. {
  580. default:
  581. case MipType.Auto:
  582. break;
  583. case MipType.MipLevel:
  584. mipSuffix = usingMacro ? "_LOD" : "lod";
  585. break;
  586. case MipType.MipBias:
  587. mipSuffix = usingMacro ? "_BIAS" : "bias";
  588. break;
  589. case MipType.Derivative:
  590. mipSuffix = usingMacro ? "_GRAD" : "grad";
  591. break;
  592. }
  593. string mipParams = string.Empty;
  594. if ( mip != MipType.Auto )
  595. {
  596. for ( int i = 0; i < mipData.Length; i++ )
  597. {
  598. mipParams += ", " + mipData[ i ];
  599. }
  600. }
  601. if ( usingMacro )
  602. {
  603. if ( ousideGraph.IsSRP )
  604. {
  605. if ( textureType == TextureType.Texture3D && ( mip == MipType.MipBias || mip == MipType.Derivative ) )
  606. AddCustom3DSRPMacros( ref dataCollector );
  607. // srp macro
  608. result = string.Format( Constants.TexSampleSRPMacros[ textureType ], mipSuffix, property, samplerState, uv + mipParams );
  609. }
  610. else
  611. {
  612. AddCustomStandardSamplingMacros( ref dataCollector, type, mip );
  613. result = string.Format( Constants.TexSampleSamplerStandardMacros[ textureType ], mipSuffix, property, samplerState, uv + mipParams );
  614. }
  615. }
  616. else
  617. {
  618. //no macro : builtin and srp
  619. string uvs = uv + mipParams;
  620. string emptyParam = ", 0";
  621. if ( textureType == TextureType.Texture3D || textureType == TextureType.Cube )
  622. emptyParam = string.Empty;
  623. if ( ( mip == MipType.MipBias || mip == MipType.MipLevel ) )
  624. uvs = "float4( " + uv + emptyParam + mipParams + " )";
  625. result = string.Format( Constants.TexSampleStandard[ textureType ], mipSuffix, property, uvs );
  626. }
  627. return result;
  628. }
  629. public static string GenerateScaleOffsettedUV( TextureType texType, string uvName, string propertyName, bool addST )
  630. {
  631. if ( addST )
  632. propertyName += "_ST";
  633. switch ( texType )
  634. {
  635. case TextureType.Texture1D: return uvName + " * " + propertyName + ".x + " + propertyName + ".z";
  636. case TextureType.Texture2D: return uvName + " * " + propertyName + ".xy + " + propertyName + ".zw";
  637. case TextureType.Texture3D:
  638. case TextureType.Cube: return uvName + " * " + propertyName + ".xy + " + propertyName + ".zw";
  639. default:
  640. case TextureType.Texture2DArray:
  641. case TextureType.ProceduralTexture: return uvName;
  642. }
  643. }
  644. // AUTOMATIC UVS - SURFACE ONLY
  645. static public string GenerateAutoUVs( ref MasterNodeDataCollector dataCollector, int uniqueId, int index, string propertyName = null, WirePortDataType size = WirePortDataType.FLOAT2, string scale = null, string offset = null, string outputId = null )
  646. {
  647. string result = string.Empty;
  648. string varName = string.Empty;
  649. string indexStr = index > 0 ? ( index + 1 ).ToString() : "";
  650. string sizeDif = string.Empty;
  651. WirePortDataType maxSize = dataCollector.GetMaxTextureChannelSize( index );
  652. //if( maxSize == WirePortDataType.FLOAT3 )
  653. // sizeDif = "3";
  654. //else if( maxSize == WirePortDataType.FLOAT4 )
  655. // sizeDif = "4";
  656. if ( !dataCollector.IsTemplate && index > 3 )
  657. {
  658. string texCoordNameIn = TemplateHelperFunctions.BaseInterpolatorName + index;
  659. string texCoordNameOut = TemplateHelperFunctions.BaseInterpolatorName + ( index + 1 ).ToString();
  660. if ( dataCollector.IsFragmentCategory )
  661. {
  662. GenerateValueInVertex( ref dataCollector, uniqueId, maxSize, PrecisionType.Float, Constants.VertexShaderInputStr + "." + texCoordNameIn, texCoordNameOut, true );
  663. result = Constants.InputVarStr + "." + texCoordNameOut;
  664. }
  665. else
  666. {
  667. result = Constants.VertexShaderInputStr + "." + texCoordNameIn;
  668. }
  669. if ( !string.IsNullOrEmpty( propertyName ) )
  670. {
  671. varName = "uv" + indexStr + ( maxSize != WirePortDataType.FLOAT2 ? "s" + sizeDif : "" ) + propertyName;
  672. dataCollector.AddToUniforms( uniqueId, "uniform float4 " + propertyName + "_ST;" );
  673. if ( maxSize > WirePortDataType.FLOAT2 )
  674. {
  675. dataCollector.UsingHigherSizeTexcoords = true;
  676. dataCollector.AddToLocalVariables( dataCollector.PortCategory, uniqueId, PrecisionType.Float, maxSize, varName, result );
  677. dataCollector.AddToLocalVariables( dataCollector.PortCategory, uniqueId, varName + ".xy = " + result + ".xy * " + propertyName + "_ST.xy + " + propertyName + "_ST.zw;" );
  678. }
  679. else
  680. {
  681. dataCollector.AddToLocalVariables( dataCollector.PortCategory, uniqueId, PrecisionType.Float, maxSize, varName, result + " * " + propertyName + "_ST.xy + " + propertyName + "_ST.zw" );
  682. }
  683. result = varName;
  684. }
  685. switch ( maxSize )
  686. {
  687. default:
  688. case WirePortDataType.FLOAT2:
  689. {
  690. result += ".xy";
  691. }
  692. break;
  693. case WirePortDataType.FLOAT3:
  694. {
  695. result += ".xyz";
  696. }
  697. break;
  698. case WirePortDataType.FLOAT4: break;
  699. }
  700. if ( size < maxSize )
  701. {
  702. switch ( size )
  703. {
  704. case WirePortDataType.FLOAT2: result += ".xy"; break;
  705. case WirePortDataType.FLOAT3: result += ".xyz"; break;
  706. }
  707. }
  708. return result;
  709. }
  710. if ( dataCollector.PortCategory == MasterNodePortCategory.Fragment || dataCollector.PortCategory == MasterNodePortCategory.Debug )
  711. {
  712. string dummyPropUV = "_tex" + sizeDif + "coord" + indexStr;
  713. string dummyUV = "uv" + indexStr + dummyPropUV;
  714. dataCollector.AddToProperties( uniqueId, "[HideInInspector] " + dummyPropUV + "( \"\", 2D ) = \"white\" {}", 100 );
  715. dataCollector.AddToInput( uniqueId, dummyUV, maxSize );
  716. result = Constants.InputVarStr + "." + dummyUV;
  717. }
  718. else
  719. {
  720. result = Constants.VertexShaderInputStr + ".texcoord";
  721. if ( index > 0 )
  722. {
  723. result += index.ToString();
  724. }
  725. switch ( maxSize )
  726. {
  727. default:
  728. case WirePortDataType.FLOAT2:
  729. {
  730. result += ".xy";
  731. }
  732. break;
  733. case WirePortDataType.FLOAT3:
  734. {
  735. result += ".xyz";
  736. }
  737. break;
  738. case WirePortDataType.FLOAT4: break;
  739. }
  740. }
  741. varName = "uv" + indexStr + ( maxSize != WirePortDataType.FLOAT2 ? "s" + sizeDif : "" ) + "_TexCoord" + outputId;
  742. if ( !string.IsNullOrEmpty( propertyName ) )
  743. {
  744. string finalVarName = "uv" + indexStr + ( maxSize != WirePortDataType.FLOAT2 ? "s" + sizeDif : "" ) + propertyName;
  745. dataCollector.AddToUniforms( uniqueId, "uniform float4 " + propertyName + "_ST;" );
  746. if ( maxSize > WirePortDataType.FLOAT2 )
  747. {
  748. dataCollector.UsingHigherSizeTexcoords = true;
  749. dataCollector.AddToLocalVariables( dataCollector.PortCategory, uniqueId, PrecisionType.Float, maxSize, finalVarName, result );
  750. dataCollector.AddToLocalVariables( dataCollector.PortCategory, uniqueId, finalVarName + ".xy = " + result + ".xy * " + propertyName + "_ST.xy + " + propertyName + "_ST.zw;" );
  751. }
  752. else
  753. {
  754. dataCollector.AddToLocalVariables( dataCollector.PortCategory, uniqueId, PrecisionType.Float, size, finalVarName, result + " * " + propertyName + "_ST.xy + " + propertyName + "_ST.zw" );
  755. }
  756. result = finalVarName;
  757. }
  758. else if ( !string.IsNullOrEmpty( scale ) || !string.IsNullOrEmpty( offset ) )
  759. {
  760. if ( maxSize > WirePortDataType.FLOAT2 )
  761. {
  762. dataCollector.UsingHigherSizeTexcoords = true;
  763. dataCollector.AddToLocalVariables( dataCollector.PortCategory, uniqueId, PrecisionType.Float, size, varName, result );
  764. dataCollector.AddToLocalVariables( dataCollector.PortCategory, uniqueId, varName + ".xy = " + result + ".xy" + ( string.IsNullOrEmpty( scale ) ? "" : " * " + scale ) + ( string.IsNullOrEmpty( offset ) ? "" : " + " + offset ) + ";" );
  765. }
  766. else
  767. {
  768. dataCollector.AddToLocalVariables( dataCollector.PortCategory, uniqueId, PrecisionType.Float, size, varName, result + ( string.IsNullOrEmpty( scale ) ? "" : " * " + scale ) + ( string.IsNullOrEmpty( offset ) ? "" : " + " + offset ) );
  769. }
  770. result = varName;
  771. }
  772. else if ( dataCollector.PortCategory == MasterNodePortCategory.Fragment )
  773. {
  774. if ( maxSize > WirePortDataType.FLOAT2 )
  775. dataCollector.UsingHigherSizeTexcoords = true;
  776. }
  777. if ( size < maxSize )
  778. {
  779. switch ( size )
  780. {
  781. case WirePortDataType.FLOAT2: result += ".xy"; break;
  782. case WirePortDataType.FLOAT3: result += ".xyz"; break;
  783. }
  784. }
  785. return result;
  786. }
  787. // SCREEN POSITION
  788. static public string GenerateScreenPositionNormalizedForValue( string customVertexPos, string outputId, ref MasterNodeDataCollector dataCollector, int uniqueId, PrecisionType precision, bool addInput = true )
  789. {
  790. if ( !dataCollector.HasLocalVariableByName( ScreenPosNormStr ) )
  791. {
  792. string screenPos = GenerateScreenPositionRawForValue( customVertexPos, outputId, ref dataCollector, uniqueId, precision, addInput );
  793. // TODO: check later if precision can be half
  794. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT4, ScreenPosNormStr, string.Format( NormalizedScreenPosFormat, screenPos ) );
  795. dataCollector.AddLocalVariable( uniqueId, string.Format( "{0}.z = ( UNITY_NEAR_CLIP_VALUE >= 0 ) ? {0}.z : {0}.z * 0.5 + 0.5;", ScreenPosNormStr ) );
  796. }
  797. return ScreenPosNormStr;
  798. }
  799. static public string GenerateScreenPositionNormalized( ref MasterNodeDataCollector dataCollector, int uniqueId, PrecisionType precision, bool addInput = true, string customScreenPos = null )
  800. {
  801. string screenPos;
  802. string varName;
  803. if ( string.IsNullOrEmpty( customScreenPos ) )
  804. {
  805. screenPos = GenerateScreenPositionRaw( ref dataCollector, uniqueId, precision, addInput );
  806. varName = ScreenPosNormStr;
  807. }
  808. else
  809. {
  810. screenPos = customScreenPos;
  811. varName = ScreenPosNormStr + uniqueId;
  812. }
  813. if ( !dataCollector.HasLocalVariableByName( varName ) )
  814. {
  815. // TODO: check later if precision can be half
  816. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT4, varName, string.Format( NormalizedScreenPosFormat, screenPos ) );
  817. dataCollector.AddLocalVariable( uniqueId, string.Format( "{0}.z = ( UNITY_NEAR_CLIP_VALUE >= 0 ) ? {0}.z : {0}.z * 0.5 + 0.5;", varName ) );
  818. }
  819. return varName;
  820. }
  821. static public string GenerateScreenPositionRawForValue( string customVertexPosition, string outputId, ref MasterNodeDataCollector dataCollector, int uniqueId, PrecisionType precision, bool addInput = true )
  822. {
  823. // overriding precision
  824. precision = PrecisionType.Float;
  825. if ( dataCollector.IsTemplate )
  826. {
  827. return dataCollector.TemplateDataCollectorInstance.GetScreenPosForValue( precision, customVertexPosition, outputId );
  828. }
  829. string screenPosFrag = ScreenPosRawStr + outputId;
  830. if ( dataCollector.IsTemplate || !dataCollector.TesselationActive )
  831. {
  832. string value = GenerateVertexScreenPositionRawForValue( customVertexPosition, outputId, ref dataCollector, uniqueId, precision );
  833. if ( !dataCollector.IsFragmentCategory )
  834. {
  835. return value;
  836. }
  837. string screenPosVertex = "screenPosition" + outputId;
  838. dataCollector.AddToInput( uniqueId, screenPosVertex, WirePortDataType.FLOAT4, precision );
  839. dataCollector.AddToVertexLocalVariables( uniqueId, Constants.VertexShaderOutputStr + "." + screenPosVertex + " = " + value + ";" );
  840. string globalResult = Constants.InputVarStr + "." + screenPosVertex;
  841. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT4, screenPosFrag, globalResult );
  842. }
  843. else
  844. {
  845. string value = string.Format( "ComputeScreenPos( UnityObjectToClipPos( {0} ) )", customVertexPosition );
  846. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT4, screenPosFrag, value );
  847. }
  848. return screenPosFrag;
  849. }
  850. static public string GenerateScreenPositionRaw( ref MasterNodeDataCollector dataCollector, int uniqueId, PrecisionType precision, bool addInput = true )
  851. {
  852. // overriding precision
  853. precision = PrecisionType.Float;
  854. if ( dataCollector.IsTemplate )
  855. {
  856. return dataCollector.TemplateDataCollectorInstance.GetScreenPosRaw( precision );
  857. }
  858. if ( dataCollector.UsingCustomScreenPos && dataCollector.IsFragmentCategory )
  859. {
  860. string value = GenerateVertexScreenPositionRaw( ref dataCollector, uniqueId, precision );
  861. dataCollector.AddToInput( uniqueId, "screenPosition", WirePortDataType.FLOAT4, precision );
  862. dataCollector.AddToVertexLocalVariables( uniqueId, Constants.VertexShaderOutputStr + ".screenPosition = " + value + ";" );
  863. string globalResult = Constants.InputVarStr + ".screenPosition";
  864. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT4, ScreenPosRawStr, globalResult );
  865. return ScreenPosRawStr;
  866. }
  867. else if ( !dataCollector.IsFragmentCategory )
  868. {
  869. return GenerateVertexScreenPositionRaw( ref dataCollector, uniqueId, precision );
  870. }
  871. if ( !dataCollector.HasLocalVariableByName( ScreenPosRawStr ) )
  872. {
  873. if ( addInput )
  874. {
  875. dataCollector.AddToInput( uniqueId, SurfaceInputs.SCREEN_POS, precision );
  876. dataCollector.AddToIncludes( uniqueId, Constants.UnityShaderVariables );
  877. }
  878. string value = string.Format( SurfaceScreenPosFormat, UIUtils.PrecisionWirePortToCgType( precision, WirePortDataType.FLOAT4 ), Constants.InputVarStr );
  879. dataCollector.AddToLocalVariables( uniqueId, precision, WirePortDataType.FLOAT4, ScreenPosRawStr, value );
  880. }
  881. return ScreenPosRawStr;
  882. }
  883. static public string GenerateScreenPositionCenter( ref MasterNodeDataCollector dataCollector, int uniqueId, PrecisionType precision, bool addInput = true )
  884. {
  885. string screenPosNorm = GenerateScreenPositionNormalized( ref dataCollector, uniqueId, precision, addInput );
  886. string value = string.Format( "float4( {0}.xy * 2 - 1, 0, 0 )", screenPosNorm );
  887. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT4, GeneratorUtils.ScreenPosCenterStr, value );
  888. return GeneratorUtils.ScreenPosCenterStr;
  889. }
  890. static public string GenerateScreenPositionTiled( ref MasterNodeDataCollector dataCollector, int uniqueId, PrecisionType precision, bool addInput = true )
  891. {
  892. string screenPosNorm = GenerateScreenPositionNormalized( ref dataCollector, uniqueId, precision, addInput );
  893. string value = string.Format( "frac( float4( ( {0}.x * 2 - 1 ) * _ScreenParams.x / _ScreenParams.y, {0}.y * 2 - 1, 0, 0 ) )", screenPosNorm );
  894. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT4, GeneratorUtils.ScreenPosTiledStr, value );
  895. return GeneratorUtils.ScreenPosTiledStr;
  896. }
  897. static public void GenerateScreenPosNormToPixelFunction( ref MasterNodeDataCollector dataCollector )
  898. {
  899. dataCollector.AddFunction( ScreenPosNormToPixelFunctionBody[ 0 ], ScreenPosNormToPixelFunctionBody, false );
  900. }
  901. static public string GenerateScreenPosNormToPixelFunctionCall( string screenPosNorm )
  902. {
  903. return string.Format( ScreenPosNormToPixelFunctionHeader, screenPosNorm );
  904. }
  905. static public string GenerateScreenPositionPixel( ref MasterNodeDataCollector dataCollector, int uniqueId, PrecisionType precision, bool addInput = true )
  906. {
  907. string screenPosNorm = GenerateScreenPositionNormalized( ref dataCollector, uniqueId, precision, addInput );
  908. GenerateScreenPosNormToPixelFunction( ref dataCollector );
  909. string value = GenerateScreenPosNormToPixelFunctionCall( screenPosNorm );
  910. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT4, GeneratorUtils.ScreenPosPixelStr, value );
  911. return GeneratorUtils.ScreenPosPixelStr;
  912. }
  913. // DEPTH
  914. static public string ApplyLinearDepthModifier( ref MasterNodeDataCollector dataCollector, string instruction, DepthMode depthMode )
  915. {
  916. if ( depthMode == DepthMode.DepthLinearEye || depthMode == DepthMode.DepthLinear01 )
  917. {
  918. string space = ( depthMode == DepthMode.DepthLinearEye ) ? "LinearEyeDepth" : "Linear01Depth";
  919. if ( dataCollector.IsTemplate && dataCollector.IsSRP )
  920. {
  921. instruction = string.Format( "{0}( {1}, _ZBufferParams )", space, instruction );
  922. }
  923. else
  924. {
  925. instruction = string.Format( "{0}( {1} )", space, instruction );
  926. }
  927. }
  928. return instruction;
  929. }
  930. static public string GenerateSurfaceDepth( ref MasterNodeDataCollector dataCollector, int uniqueId, PrecisionType precision, DepthMode depthMode )
  931. {
  932. if ( dataCollector.IsTemplate )
  933. {
  934. return dataCollector.TemplateDataCollectorInstance.GetSurfaceDepth( depthMode, precision );
  935. }
  936. string screenPos = GeneratorUtils.GenerateScreenPositionRaw( ref dataCollector, uniqueId, precision );
  937. string instruction = string.Format( "{0}.z / {0}.w", screenPos );
  938. string varName;
  939. switch ( depthMode )
  940. {
  941. case DepthMode.DepthLinearEye:
  942. {
  943. instruction = ApplyLinearDepthModifier( ref dataCollector, instruction, depthMode );
  944. varName = GeneratorUtils.DepthLinearEyeStr;
  945. break;
  946. }
  947. case DepthMode.DepthLinear01:
  948. {
  949. instruction = ApplyLinearDepthModifier( ref dataCollector, instruction, depthMode );
  950. varName = GeneratorUtils.DepthLinear01Str;
  951. break;
  952. }
  953. case DepthMode.DepthEye:
  954. {
  955. instruction = string.Format( "( {0} ) * ( _ProjectionParams.z - _ProjectionParams.y )", instruction );
  956. varName = GeneratorUtils.DepthEyeStr;
  957. break;
  958. }
  959. case DepthMode.Depth01:
  960. default:
  961. {
  962. varName = GeneratorUtils.DepthRawStr;
  963. break;
  964. }
  965. }
  966. dataCollector.AddToLocalVariables( uniqueId, precision, WirePortDataType.FLOAT, varName, instruction );
  967. return varName;
  968. }
  969. // GRAB SCREEN POSITION
  970. static public string GenerateGrabScreenPosition( ref MasterNodeDataCollector dataCollector , int uniqueId , PrecisionType precision , bool addInput = true , string customScreenPos = null )
  971. {
  972. // overriding precision
  973. precision = PrecisionType.Float;
  974. string screenPos = string.Empty;
  975. if( string.IsNullOrEmpty( customScreenPos ) )
  976. screenPos = GenerateScreenPositionRaw( ref dataCollector , uniqueId , precision , addInput );
  977. else
  978. screenPos = customScreenPos;
  979. string computeBody = string.Empty;
  980. IOUtils.AddFunctionHeader( ref computeBody , GrabFunctionHeader );
  981. foreach( string line in GrabFunctionBody )
  982. IOUtils.AddFunctionLine( ref computeBody , line );
  983. IOUtils.CloseFunctionBody( ref computeBody );
  984. string functionResult = dataCollector.AddFunctions( GrabFunctionCall , computeBody , screenPos );
  985. dataCollector.AddLocalVariable( uniqueId , precision , WirePortDataType.FLOAT4 , GrabScreenPositionStr , functionResult );
  986. return GrabScreenPositionStr;
  987. }
  988. // GRAB SCREEN POSITION NORMALIZED
  989. static public string GenerateGrabScreenPositionNormalized( ref MasterNodeDataCollector dataCollector , int uniqueId , PrecisionType precision , bool addInput = true , string customScreenPos = null )
  990. {
  991. string stringPosVar = GenerateGrabScreenPosition( ref dataCollector, uniqueId, precision, addInput, customScreenPos );
  992. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT4, GrabScreenPositionNormalizedStr, string.Format( NormalizedScreenPosFormat, stringPosVar ) );
  993. return GrabScreenPositionNormalizedStr;
  994. }
  995. // SCREEN POSITION ON VERT
  996. static public string GenerateVertexScreenPositionRawForValue( string customVertexPosition , string outputId , ref MasterNodeDataCollector dataCollector , int uniqueId , PrecisionType precision )
  997. {
  998. // overriding precision
  999. precision = PrecisionType.Float;
  1000. if ( dataCollector.IsTemplate )
  1001. {
  1002. return dataCollector.TemplateDataCollectorInstance.GetScreenPosForValue( precision, customVertexPosition, outputId );
  1003. }
  1004. string screenPosVarName = ScreenPosRawStr + outputId;
  1005. if ( !dataCollector.HasLocalVariableByName( screenPosVarName, MasterNodePortCategory.Vertex ) )
  1006. {
  1007. string value = string.Format( "ComputeScreenPos( UnityObjectToClipPos( {0} ) )", customVertexPosition );
  1008. dataCollector.AddToVertexLocalVariables( uniqueId, precision, WirePortDataType.FLOAT4, screenPosVarName, value );
  1009. }
  1010. return screenPosVarName;
  1011. }
  1012. static public string GenerateVertexScreenPositionRaw( ref MasterNodeDataCollector dataCollector , int uniqueId , PrecisionType precision )
  1013. {
  1014. // overriding precision
  1015. precision = PrecisionType.Float;
  1016. if( dataCollector.IsTemplate )
  1017. {
  1018. return dataCollector.TemplateDataCollectorInstance.GetScreenPosRaw( precision );
  1019. }
  1020. if ( !dataCollector.HasLocalVariableByName( ScreenPosRawStr, MasterNodePortCategory.Vertex ) )
  1021. {
  1022. string value = string.Format( "ComputeScreenPos( UnityObjectToClipPos( {0}.vertex ) )", Constants.VertexShaderInputStr );
  1023. dataCollector.AddToVertexLocalVariables( uniqueId, precision, WirePortDataType.FLOAT4, ScreenPosRawStr, value );
  1024. }
  1025. return ScreenPosRawStr;
  1026. }
  1027. // VERTEX POSITION
  1028. static public string GenerateVertexPosition( ref MasterNodeDataCollector dataCollector, int uniqueId, WirePortDataType type, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
  1029. {
  1030. // overriding precision
  1031. var precision = PrecisionType.Float;
  1032. if ( dataCollector.IsTemplate )
  1033. {
  1034. return dataCollector.TemplateDataCollectorInstance.GetVertexPosition( type, precision );
  1035. }
  1036. MasterNodePortCategory portCategory = useMasterNodeCategory ? dataCollector.PortCategory : customCategory;
  1037. string varName = ( type == WirePortDataType.FLOAT3 ) ? VertexPosition3Str : VertexPosition4Str;
  1038. string swizzle = ( type == WirePortDataType.FLOAT3 ) ? ".xyz" : string.Empty;
  1039. if ( portCategory == MasterNodePortCategory.Vertex )
  1040. {
  1041. if ( !dataCollector.HasLocalVariableByName( varName, MasterNodePortCategory.Vertex ) )
  1042. {
  1043. string result = string.Format( "{0}.vertex{1}", Constants.VertexShaderInputStr, swizzle );
  1044. dataCollector.AddToVertexLocalVariables( uniqueId, precision, type, varName, result );
  1045. }
  1046. }
  1047. else if ( !dataCollector.HasLocalVariableByName( varName ) )
  1048. {
  1049. if ( dataCollector.TesselationActive )
  1050. {
  1051. dataCollector.AddToInput( -1, SurfaceInputs.WORLD_POS, precision );
  1052. dataCollector.AddToIncludes( uniqueId, Constants.UnityShaderVariables );
  1053. string positionWS = string.Format( "{0}.worldPos", Constants.InputVarStr );
  1054. string result = string.Format( "mul( unity_WorldToObject, float4( {0}, 1 ) )", positionWS );
  1055. dataCollector.AddLocalVariable( uniqueId, precision, type, varName, result );
  1056. }
  1057. else
  1058. {
  1059. string positionOS = GeneratorUtils.VertexPosition4Str;
  1060. string positionOSOutput = string.Format( "{0}.{1}", Constants.VertexShaderOutputStr, positionOS );
  1061. string positionOSInput = string.Format( "{0}.{1}", Constants.InputVarStr, positionOS );
  1062. dataCollector.AddToInput( uniqueId, positionOS, WirePortDataType.FLOAT4 );
  1063. if ( !dataCollector.HasLocalVariableByName( positionOS, MasterNodePortCategory.Vertex ) )
  1064. {
  1065. dataCollector.AddToVertexLocalVariables( uniqueId, precision, WirePortDataType.FLOAT4, positionOS,
  1066. string.Format( "{0}.vertex", Constants.VertexShaderInputStr ) );
  1067. }
  1068. dataCollector.AddToVertexLocalVariables( uniqueId, positionOSOutput, positionOS );
  1069. string result = positionOSInput + swizzle;
  1070. dataCollector.AddLocalVariable( uniqueId, precision, type, varName, result );
  1071. }
  1072. }
  1073. return varName;
  1074. }
  1075. // VERTEX NORMAL
  1076. static public string GenerateVertexNormal( ref MasterNodeDataCollector dataCollector , int uniqueId , PrecisionType precision )
  1077. {
  1078. if( dataCollector.MasterNodeCategory == AvailableShaderTypes.Template )
  1079. {
  1080. return dataCollector.TemplateDataCollectorInstance.GetVertexNormal( UIUtils.CurrentWindow.CurrentGraph.CurrentPrecision );
  1081. }
  1082. string value = Constants.VertexShaderInputStr + ".normal.xyz";
  1083. if( dataCollector.PortCategory == MasterNodePortCategory.Fragment || dataCollector.PortCategory == MasterNodePortCategory.Debug )
  1084. {
  1085. GenerateWorldNormal( ref dataCollector , uniqueId );
  1086. dataCollector.AddLocalVariable( uniqueId , precision , WirePortDataType.FLOAT3 , VertexNormalStr , "mul( unity_WorldToObject, float4( " + WorldNormalStr + ", 0 ) )" );
  1087. dataCollector.AddLocalVariable( uniqueId , VertexNormalStr + " = normalize( " + VertexNormalStr + " );" );
  1088. //dataCollector.AddToLocalVariables( uniqueId, precision, WirePortDataType.FLOAT3, VertexNormalStr, "mul( unity_WorldToObject, float4( " + WorldNormalStr + ", 0 ) )" );
  1089. }
  1090. else
  1091. {
  1092. dataCollector.AddLocalVariable( uniqueId , precision , WirePortDataType.FLOAT3 , VertexNormalStr , value );
  1093. }
  1094. return VertexNormalStr;
  1095. }
  1096. // VERTEX TANGENT
  1097. static public string GenerateVertexTangent( ref MasterNodeDataCollector dataCollector , int uniqueId , PrecisionType precision , WirePortDataType size )
  1098. {
  1099. if( dataCollector.MasterNodeCategory == AvailableShaderTypes.Template )
  1100. {
  1101. return dataCollector.TemplateDataCollectorInstance.GetVertexTangent( size , UIUtils.CurrentWindow.CurrentGraph.CurrentPrecision );
  1102. }
  1103. if( dataCollector.PortCategory == MasterNodePortCategory.Fragment || dataCollector.PortCategory == MasterNodePortCategory.Debug )
  1104. {
  1105. GenerateWorldTangent( ref dataCollector , uniqueId );
  1106. dataCollector.AddLocalVariable( uniqueId , precision , WirePortDataType.FLOAT4 , VertexTangentStr , "mul( unity_WorldToObject, float4( " + WorldTangentStr + ", 0 ) )" );
  1107. dataCollector.AddLocalVariable( uniqueId , VertexTangentStr +" = normalize( " + VertexTangentStr + " );");
  1108. }
  1109. else
  1110. {
  1111. string value = Constants.VertexShaderInputStr + ".tangent";
  1112. dataCollector.AddLocalVariable( uniqueId , precision , WirePortDataType.FLOAT4 , VertexTangentStr , value );
  1113. }
  1114. return ( size == WirePortDataType.FLOAT4 ) ? VertexTangentStr : VertexTangentStr + ".xyz";
  1115. }
  1116. // VERTEX TANGENT SIGN
  1117. static public string GenerateVertexTangentSign( ref MasterNodeDataCollector dataCollector , int uniqueId , PrecisionType precision )
  1118. {
  1119. if( dataCollector.MasterNodeCategory == AvailableShaderTypes.Template )
  1120. {
  1121. return dataCollector.TemplateDataCollectorInstance.GetTangentSign( UIUtils.CurrentWindow.CurrentGraph.CurrentPrecision );
  1122. }
  1123. string value = Constants.VertexShaderInputStr + ".tangent.w";
  1124. if( dataCollector.IsFragmentCategory )
  1125. {
  1126. dataCollector.AddToInput( uniqueId , VertexTangentSignStr , WirePortDataType.FLOAT , PrecisionType.Half );
  1127. dataCollector.AddToVertexLocalVariables( uniqueId , Constants.VertexShaderOutputStr + "." + VertexTangentSignStr + " = " + Constants.VertexShaderInputStr + ".tangent.w;" );
  1128. return Constants.InputVarStr + "." + VertexTangentSignStr;
  1129. }
  1130. else
  1131. {
  1132. dataCollector.AddLocalVariable( uniqueId , precision , WirePortDataType.FLOAT , VertexTangentSignStr , value );
  1133. }
  1134. return VertexTangentSignStr;
  1135. }
  1136. // VERTEX BITANGENT
  1137. static public string GenerateVertexBitangent( ref MasterNodeDataCollector dataCollector , int uniqueId , PrecisionType precision )
  1138. {
  1139. if( dataCollector.MasterNodeCategory == AvailableShaderTypes.Template )
  1140. {
  1141. return dataCollector.TemplateDataCollectorInstance.GetVertexBitangent( UIUtils.CurrentWindow.CurrentGraph.CurrentPrecision );
  1142. }
  1143. if( dataCollector.PortCategory == MasterNodePortCategory.Fragment || dataCollector.PortCategory == MasterNodePortCategory.Debug )
  1144. {
  1145. GenerateWorldBitangent( ref dataCollector , uniqueId );
  1146. dataCollector.AddLocalVariable( uniqueId , precision , WirePortDataType.FLOAT3 , VertexBitangentStr , "mul( unity_WorldToObject, float4( " + WorldBitangentStr + ", 0 ) )" );
  1147. dataCollector.AddLocalVariable( uniqueId , VertexBitangentStr + " = normalize( " + VertexBitangentStr + " );" );
  1148. }
  1149. else
  1150. {
  1151. GenerateVertexNormal( ref dataCollector , uniqueId , precision );
  1152. GenerateVertexTangent( ref dataCollector , uniqueId , precision , WirePortDataType.FLOAT3 );
  1153. dataCollector.AddLocalVariable( uniqueId , precision , WirePortDataType.FLOAT3 , VertexBitangentStr , "cross( " + VertexNormalStr + ", " + VertexTangentStr + ") * " + Constants.VertexShaderInputStr + ".tangent.w * ( unity_WorldTransformParams.w >= 0.0 ? 1.0 : -1.0 )" );
  1154. }
  1155. return VertexBitangentStr;
  1156. }
  1157. // VERTEX POSITION ON FRAG
  1158. static public string GenerateVertexPositionOnFrag( ref MasterNodeDataCollector dataCollector , int uniqueId , PrecisionType precision )
  1159. {
  1160. if ( !dataCollector.HasLocalVariableByName( VertexPosition4Str ) )
  1161. {
  1162. dataCollector.AddToInput( uniqueId, SurfaceInputs.WORLD_POS );
  1163. dataCollector.AddToIncludes( uniqueId, Constants.UnityShaderVariables );
  1164. string value = "mul( unity_WorldToObject, float4( " + Constants.InputVarStr + ".worldPos , 1 ) )";
  1165. dataCollector.AddToLocalVariables( uniqueId, precision, WirePortDataType.FLOAT4, VertexPosition4Str, value );
  1166. }
  1167. return VertexPosition4Str;
  1168. }
  1169. // CLIP POSITION ON FRAG
  1170. static public string GenerateScreenPositionRawOnFrag( ref MasterNodeDataCollector dataCollector, int uniqueId, PrecisionType precision )
  1171. {
  1172. if ( dataCollector.IsTemplate )
  1173. {
  1174. return dataCollector.TemplateDataCollectorInstance.GetScreenPosRaw( precision );
  1175. }
  1176. if ( !dataCollector.HasLocalVariableByName( GeneratorUtils.ScreenPosRawStr ) )
  1177. {
  1178. dataCollector.AddToInput( uniqueId, SurfaceInputs.SCREEN_POS );
  1179. dataCollector.AddToIncludes( uniqueId, Constants.UnityShaderVariables );
  1180. string value = string.Format( SurfaceScreenPosFormat, UIUtils.PrecisionWirePortToCgType( precision, WirePortDataType.FLOAT4 ), Constants.InputVarStr );
  1181. dataCollector.AddToLocalVariables( uniqueId, precision, WirePortDataType.FLOAT4, GeneratorUtils.ScreenPosRawStr, value );
  1182. }
  1183. return GeneratorUtils.ScreenPosRawStr;
  1184. }
  1185. static public string GenerateScreenPositionNormalizedOnFrag( ref MasterNodeDataCollector dataCollector, int uniqueId, PrecisionType precision )
  1186. {
  1187. if ( dataCollector.IsTemplate )
  1188. {
  1189. return dataCollector.TemplateDataCollectorInstance.GetScreenPosNormalized( precision );
  1190. }
  1191. if ( !dataCollector.HasLocalVariableByName( GeneratorUtils.ScreenPosNormStr ) )
  1192. {
  1193. string screenPosRaw = GenerateScreenPositionRawOnFrag( ref dataCollector, uniqueId, precision );
  1194. string clipPlaneTestOp = string.Format( "{0}.z = ( UNITY_NEAR_CLIP_VALUE >= 0 ) ? {0}.z : {0}.z * 0.5 + 0.5;", GeneratorUtils.ScreenPosNormStr );
  1195. dataCollector.AddLocalVariable( -1, precision, WirePortDataType.FLOAT4, GeneratorUtils.ScreenPosNormStr, string.Format( GeneratorUtils.NormalizedScreenPosFormat, screenPosRaw ) );
  1196. dataCollector.AddLocalVariable( -1, clipPlaneTestOp );
  1197. }
  1198. return GeneratorUtils.ScreenPosNormStr;
  1199. }
  1200. static public string GenerateScreenPositionCenterOnFrag( ref MasterNodeDataCollector dataCollector, int uniqueId, PrecisionType precision )
  1201. {
  1202. if ( dataCollector.IsTemplate )
  1203. {
  1204. return dataCollector.TemplateDataCollectorInstance.GetScreenPosCenter( precision );
  1205. }
  1206. if ( !dataCollector.HasLocalVariableByName( GeneratorUtils.ScreenPosCenterStr ) )
  1207. {
  1208. string screenPosNorm = GenerateScreenPositionNormalizedOnFrag( ref dataCollector, uniqueId, precision );
  1209. string value = string.Format( "float4( {0}.xy * 2 - 1, 0, 0 )", screenPosNorm );
  1210. dataCollector.AddLocalVariable( -1, precision, WirePortDataType.FLOAT4, GeneratorUtils.ScreenPosCenterStr, value );
  1211. }
  1212. return GeneratorUtils.ScreenPosCenterStr;
  1213. }
  1214. static public string GenerateScreenPositionTiledOnFrag( ref MasterNodeDataCollector dataCollector, int uniqueId, PrecisionType precision )
  1215. {
  1216. if ( dataCollector.IsTemplate )
  1217. {
  1218. return dataCollector.TemplateDataCollectorInstance.GetScreenPosTiled( precision );
  1219. }
  1220. if ( !dataCollector.HasLocalVariableByName( GeneratorUtils.ScreenPosTiledStr ) )
  1221. {
  1222. string screenPosNorm = GenerateScreenPositionNormalizedOnFrag( ref dataCollector, uniqueId, precision );
  1223. string value = string.Format( "frac( float4( ( {0}.x * 2 - 1 ) * _ScreenParams.x / _ScreenParams.y, {0}.y * 2 - 1, 0, 0 ) )", screenPosNorm );
  1224. dataCollector.AddLocalVariable( -1, precision, WirePortDataType.FLOAT4, GeneratorUtils.ScreenPosTiledStr, value );
  1225. }
  1226. return GeneratorUtils.ScreenPosTiledStr;
  1227. }
  1228. static public string GenerateScreenPositionPixelOnFrag( ref MasterNodeDataCollector dataCollector, int uniqueId, PrecisionType precision )
  1229. {
  1230. if ( dataCollector.IsTemplate )
  1231. {
  1232. return dataCollector.TemplateDataCollectorInstance.GetScreenPosPixel( precision );
  1233. }
  1234. if ( !dataCollector.HasLocalVariableByName( GeneratorUtils.ScreenPosPixelStr ) )
  1235. {
  1236. string screenPosNorm = GenerateScreenPositionNormalizedOnFrag( ref dataCollector, uniqueId, precision );
  1237. GeneratorUtils.GenerateScreenPosNormToPixelFunction( ref dataCollector );
  1238. dataCollector.AddLocalVariable( -1, precision, WirePortDataType.FLOAT4, GeneratorUtils.ScreenPosPixelStr, GeneratorUtils.GenerateScreenPosNormToPixelFunctionCall( screenPosNorm ) );
  1239. }
  1240. return GeneratorUtils.ScreenPosPixelStr;
  1241. }
  1242. // VIEW DIRECTION
  1243. static public string GenerateViewVector( ref MasterNodeDataCollector dataCollector, int uniqueId, ViewSpace space = ViewSpace.World )
  1244. {
  1245. PrecisionType precision = UIUtils.CurrentWindow.CurrentGraph.CurrentPrecision;
  1246. if ( dataCollector.IsTemplate )
  1247. {
  1248. return dataCollector.TemplateDataCollectorInstance.GetViewVector( precisionType: precision, space: space );
  1249. }
  1250. string varName;
  1251. switch ( space )
  1252. {
  1253. case ViewSpace.Tangent: varName = GeneratorUtils.TangentViewVectorStr; break;
  1254. case ViewSpace.Object: varName = GeneratorUtils.ObjectViewVectorStr; break;
  1255. case ViewSpace.View: varName = GeneratorUtils.ViewViewVectorStr; break;
  1256. case ViewSpace.World:
  1257. default: varName = GeneratorUtils.WorldViewVectorStr; break;
  1258. }
  1259. if ( !dataCollector.HasLocalVariableByName( varName ) )
  1260. {
  1261. string worldPos = GenerateWorldPosition( ref dataCollector, uniqueId );
  1262. string viewVectorWS = "( _WorldSpaceCameraPos.xyz - " + worldPos + " )";
  1263. string viewDir;
  1264. if ( space == ViewSpace.Tangent )
  1265. {
  1266. viewDir = "mul( " + GenerateWorldToTangentMatrix( ref dataCollector, uniqueId, precision ) + ", " + viewVectorWS + " )";
  1267. }
  1268. else if ( space == ViewSpace.Object )
  1269. {
  1270. viewDir = "mul( ( float3x3 )unity_WorldToObject, " + viewVectorWS + " )";
  1271. }
  1272. else if ( space == ViewSpace.View )
  1273. {
  1274. viewDir = "mul( ( float3x3 )UNITY_MATRIX_V, " + viewVectorWS + " )";
  1275. }
  1276. else
  1277. {
  1278. viewDir = viewVectorWS;
  1279. }
  1280. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT3, varName, viewDir );
  1281. }
  1282. return varName;
  1283. }
  1284. static public string GenerateViewDirection( ref MasterNodeDataCollector dataCollector, int uniqueId, NormalizeType normalizeType = NormalizeType.Regular, ViewSpace space = ViewSpace.World )
  1285. {
  1286. PrecisionType precision = UIUtils.CurrentWindow.CurrentGraph.CurrentPrecision;
  1287. if( dataCollector.IsTemplate )
  1288. {
  1289. return dataCollector.TemplateDataCollectorInstance.GetViewDir( precisionType: precision, normalizeType: normalizeType, space: space );
  1290. }
  1291. string varName;
  1292. switch ( space )
  1293. {
  1294. case ViewSpace.Tangent: varName = ( normalizeType == NormalizeType.Regular ) ? GeneratorUtils.TangentViewDirectionStr : GeneratorUtils.TangentViewDirectionSafeStr; break;
  1295. case ViewSpace.Object: varName = ( normalizeType == NormalizeType.Regular ) ? GeneratorUtils.ObjectViewDirectionStr : GeneratorUtils.ObjectViewDirectionSafeStr; break;
  1296. case ViewSpace.View: varName = ( normalizeType == NormalizeType.Regular ) ? GeneratorUtils.ViewViewDirectionStr : GeneratorUtils.ViewViewDirectionSafeStr; break;
  1297. case ViewSpace.World:
  1298. default: varName = ( normalizeType == NormalizeType.Regular ) ? GeneratorUtils.WorldViewDirectionStr : GeneratorUtils.WorldViewDirectionSafeStr; break;
  1299. }
  1300. if ( !dataCollector.HasLocalVariableByName( varName ) )
  1301. {
  1302. string viewVector = GenerateViewVector( ref dataCollector, uniqueId, space );
  1303. if ( normalizeType == NormalizeType.Regular )
  1304. {
  1305. viewVector = "normalize( " + viewVector + " )";
  1306. }
  1307. else if ( normalizeType == NormalizeType.Safe )
  1308. {
  1309. viewVector = TemplateHelperFunctions.SafeNormalize( dataCollector, viewVector );
  1310. }
  1311. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT3, varName, viewVector );
  1312. }
  1313. return varName;
  1314. }
  1315. const string FaceVertexInstr = "(dot({0},float3(0,0,1)))";
  1316. static public string GenerateVertexFace( ref MasterNodeDataCollector dataCollector , int uniqueId )
  1317. {
  1318. string viewDir = GenerateViewDirection( ref dataCollector, uniqueId, space: ViewSpace.Tangent );
  1319. dataCollector.AddLocalVariable( -1, PrecisionType.Float, WirePortDataType.FLOAT, FaceVertex, string.Format( FaceVertexInstr, viewDir ) );
  1320. return FaceVertex;
  1321. }
  1322. // VIEW POS
  1323. static public string GenerateViewPositionOnFrag( ref MasterNodeDataCollector dataCollector, int uniqueId, PrecisionType precision )
  1324. {
  1325. // overriding precision
  1326. precision = PrecisionType.Float;
  1327. if( dataCollector.IsTemplate )
  1328. UnityEngine.Debug.LogWarning( "View Pos not implemented on Templates" );
  1329. string vertexName = GenerateVertexPositionOnFrag( ref dataCollector, uniqueId, precision );
  1330. string value = string.Format( "UnityObjectToViewPos( {0} )", vertexName );
  1331. dataCollector.AddToLocalVariables( uniqueId, precision, WirePortDataType.FLOAT3, ViewPositionStr, value );
  1332. return ViewPositionStr;
  1333. }
  1334. // SCREEN DEPTH
  1335. static public string GenerateScreenDepthOnFrag( ref MasterNodeDataCollector dataCollector, int uniqueId, PrecisionType precision )
  1336. {
  1337. // overriding precision
  1338. precision = PrecisionType.Float;
  1339. if( dataCollector.IsTemplate )
  1340. UnityEngine.Debug.LogWarning( "Screen Depth not implemented on Templates" );
  1341. string viewPos = GenerateViewPositionOnFrag( ref dataCollector, uniqueId, precision );
  1342. string value = string.Format( "-{0}.z", viewPos );
  1343. dataCollector.AddToLocalVariables( uniqueId, precision, WirePortDataType.FLOAT, ScreenDepthStr, value );
  1344. return ScreenDepthStr;
  1345. }
  1346. // LIGHT DIRECTION WORLD
  1347. static public string GenerateWorldLightDirection( ref MasterNodeDataCollector dataCollector, int uniqueId, PrecisionType precision )
  1348. {
  1349. dataCollector.AddToIncludes( uniqueId, Constants.UnityCgLibFuncs );
  1350. string worldPos = GeneratorUtils.GenerateWorldPosition( ref dataCollector, uniqueId );
  1351. dataCollector.AddLocalVariable( uniqueId, "#if defined(LIGHTMAP_ON) && UNITY_VERSION < 560 //aseld" );
  1352. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT3, WorldLightDirStr, "0" );
  1353. dataCollector.AddLocalVariable( uniqueId, "#else //aseld" );
  1354. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT3, WorldLightDirStr, ( dataCollector.SafeNormalizeLightDir ? "Unity_SafeNormalize" : "normalize" ) + "( UnityWorldSpaceLightDir( " + worldPos + " ) )" );
  1355. dataCollector.AddLocalVariable( uniqueId, "#endif //aseld" );
  1356. return WorldLightDirStr;
  1357. }
  1358. private static readonly string[] SafeNormalize =
  1359. {
  1360. "float{0} ASESafeNormalize(float{0} inVec)\n",
  1361. "{\n",
  1362. "\tfloat dp3 = max(1.175494351e-38, dot(inVec, inVec));\n",
  1363. "\treturn inVec* rsqrt(dp3);\n",
  1364. "}\n",
  1365. };
  1366. private static readonly string ASEUnpackNormalRGBCall = "ASEUnpackNormalRGB({0},{1})";
  1367. private static readonly string[] ASEUnpackNormalRGB =
  1368. {
  1369. "float3 ASEUnpackNormalRGB(float4 PackedNormal, float Scale = 1.0 )\n",
  1370. "{\n",
  1371. "\tfloat3 normal;\n",
  1372. "\tnormal.xyz = PackedNormal.rgb * 2.0 - 1.0;\n",
  1373. "\tnormal.xy *= Scale;\n",
  1374. "\treturn normal;\n",
  1375. "}\n"
  1376. };
  1377. static public string NormalizeValue( ref MasterNodeDataCollector dataCollector , bool safeNormalize , WirePortDataType dataType, string value )
  1378. {
  1379. string normalizeInstruction = string.Empty;
  1380. if( safeNormalize )
  1381. {
  1382. string[] finalFunction = null;
  1383. string[] funcVersion = SafeNormalize;
  1384. finalFunction = new string[ funcVersion.Length ];
  1385. switch( dataType )
  1386. {
  1387. case WirePortDataType.FLOAT:
  1388. finalFunction[0] = string.Format( funcVersion[ 0 ] , string.Empty );
  1389. break;
  1390. case WirePortDataType.FLOAT2:
  1391. finalFunction[ 0 ] = string.Format( funcVersion[ 0 ] , "2" );
  1392. break;
  1393. case WirePortDataType.FLOAT3:
  1394. finalFunction[ 0 ] = string.Format( funcVersion[ 0 ] , "3" );
  1395. break;
  1396. case WirePortDataType.FLOAT4:
  1397. case WirePortDataType.COLOR:
  1398. finalFunction[ 0 ] = string.Format( funcVersion[ 0 ] , "4" );
  1399. break;
  1400. default:
  1401. case WirePortDataType.FLOAT3x3:
  1402. case WirePortDataType.FLOAT4x4:
  1403. case WirePortDataType.INT:
  1404. case WirePortDataType.OBJECT:
  1405. case WirePortDataType.SAMPLER1D:
  1406. case WirePortDataType.SAMPLER2D:
  1407. case WirePortDataType.SAMPLER3D:
  1408. case WirePortDataType.SAMPLERCUBE:
  1409. case WirePortDataType.UINT:
  1410. case WirePortDataType.UINT4:
  1411. case WirePortDataType.SAMPLER2DARRAY:
  1412. case WirePortDataType.SAMPLERSTATE:return value;
  1413. }
  1414. for( int i = 1 ; i < funcVersion.Length ; i++ )
  1415. {
  1416. finalFunction[ i ] = funcVersion[ i ];
  1417. }
  1418. dataCollector.AddFunction( finalFunction[ 0 ] , finalFunction , false );
  1419. normalizeInstruction = "ASESafeNormalize";
  1420. }
  1421. else
  1422. {
  1423. normalizeInstruction = "normalize";
  1424. }
  1425. return normalizeInstruction = normalizeInstruction + "( " + value + " )";
  1426. }
  1427. // LIGHT DIRECTION Object
  1428. static public string GenerateObjectLightDirection( ref MasterNodeDataCollector dataCollector, int uniqueId, PrecisionType precision, string vertexPos )
  1429. {
  1430. dataCollector.AddToIncludes( uniqueId, Constants.UnityCgLibFuncs );
  1431. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT3, ObjectLightDirStr, "normalize( ObjSpaceLightDir( " + vertexPos + " ) )" );
  1432. return ObjectLightDirStr;
  1433. }
  1434. // UNPACK NORMALS
  1435. public static string GenerateUnpackNormalStr( ref MasterNodeDataCollector dataCollector, PrecisionType precision, int uniqueId, string outputId, string src, bool applyScale, string scale, UnpackInputMode inputMode )
  1436. {
  1437. string funcName;
  1438. if( inputMode == UnpackInputMode.Object )
  1439. {
  1440. dataCollector.AddFunction( ASEUnpackNormalRGB[ 0 ] , ASEUnpackNormalRGB , false );
  1441. return string.Format( ASEUnpackNormalRGBCall, src , scale );
  1442. }
  1443. if( dataCollector.IsTemplate && dataCollector.IsSRP )
  1444. {
  1445. if( applyScale )
  1446. {
  1447. dataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT3, "unpack" + outputId, "UnpackNormalScale( " + src + ", " + scale + " )" );
  1448. dataCollector.AddLocalVariable( uniqueId, "unpack" + outputId + ".z = lerp( 1, unpack" + outputId + ".z, saturate(" + scale + ") );" );
  1449. funcName = "unpack" + outputId;
  1450. }
  1451. else
  1452. {
  1453. funcName = "UnpackNormalScale( " + src + ", " + scale + " )";
  1454. }
  1455. }
  1456. else
  1457. {
  1458. funcName = applyScale ? "UnpackScaleNormal( " + src + ", " + scale + " )" : "UnpackNormal( " + src + " )";
  1459. }
  1460. return funcName;
  1461. }
  1462. //MATRIX INVERSE
  1463. // 3x3
  1464. public static string Inverse3x3Header = "Inverse3x3( {0} )";
  1465. public static string[] Inverse3x3Function =
  1466. {
  1467. "{0}3x3 Inverse3x3({0}3x3 input)\n",
  1468. "{\n",
  1469. "\t{0}3 a = input._11_21_31;\n",
  1470. "\t{0}3 b = input._12_22_32;\n",
  1471. "\t{0}3 c = input._13_23_33;\n",
  1472. "\treturn {0}3x3(cross(b,c), cross(c,a), cross(a,b)) * (1.0 / dot(a,cross(b,c)));\n",
  1473. "}\n"
  1474. };
  1475. public static bool[] Inverse3x3FunctionFlags =
  1476. {
  1477. true,
  1478. false,
  1479. true,
  1480. true,
  1481. true,
  1482. true,
  1483. false
  1484. };
  1485. public static void Add3x3InverseFunction( ref MasterNodeDataCollector dataCollector, string precisionString )
  1486. {
  1487. if( !dataCollector.HasFunction( Inverse3x3Header ) )
  1488. {
  1489. //Hack to be used util indent is properly used
  1490. int currIndent = UIUtils.ShaderIndentLevel;
  1491. if( dataCollector.IsTemplate )
  1492. {
  1493. UIUtils.ShaderIndentLevel = 0;
  1494. }
  1495. else
  1496. {
  1497. UIUtils.ShaderIndentLevel = 1;
  1498. UIUtils.ShaderIndentLevel++;
  1499. }
  1500. string finalFunction = string.Empty;
  1501. for( int i = 0; i < Inverse3x3Function.Length; i++ )
  1502. {
  1503. finalFunction += UIUtils.ShaderIndentTabs + ( Inverse3x3FunctionFlags[ i ] ? string.Format( Inverse3x3Function[ i ], precisionString ) : Inverse3x3Function[ i ] );
  1504. }
  1505. UIUtils.ShaderIndentLevel = currIndent;
  1506. dataCollector.AddFunction( Inverse3x3Header, finalFunction );
  1507. }
  1508. }
  1509. public static string GenerateValueInVertex( ref MasterNodeDataCollector dataCollector, int uniqueId, WirePortDataType dataType, PrecisionType currentPrecisionType, string dataValue, string dataName, bool createInterpolator )
  1510. {
  1511. if( !dataCollector.IsFragmentCategory )
  1512. return dataValue;
  1513. //TEMPLATES
  1514. if( dataCollector.IsTemplate )
  1515. {
  1516. if( createInterpolator && dataCollector.TemplateDataCollectorInstance.HasCustomInterpolatedData( dataName ) )
  1517. return dataName;
  1518. MasterNodePortCategory category = dataCollector.PortCategory;
  1519. dataCollector.PortCategory = MasterNodePortCategory.Vertex;
  1520. dataCollector.PortCategory = category;
  1521. if( createInterpolator )
  1522. {
  1523. dataCollector.TemplateDataCollectorInstance.RegisterCustomInterpolatedData( dataName, dataType, currentPrecisionType, dataValue );
  1524. }
  1525. else
  1526. {
  1527. dataCollector.AddToVertexLocalVariables( -1, currentPrecisionType, dataType, dataName, dataValue );
  1528. }
  1529. return dataName;
  1530. }
  1531. //SURFACE
  1532. {
  1533. if( dataCollector.TesselationActive )
  1534. {
  1535. UIUtils.ShowMessage( "Unable to use Vertex to Frag when Tessellation is active" );
  1536. switch( dataType )
  1537. {
  1538. case WirePortDataType.FLOAT2:
  1539. {
  1540. return "(0).xx";
  1541. }
  1542. case WirePortDataType.FLOAT3:
  1543. {
  1544. return "(0).xxx";
  1545. }
  1546. case WirePortDataType.FLOAT4:
  1547. case WirePortDataType.COLOR:
  1548. {
  1549. return "(0).xxxx";
  1550. }
  1551. }
  1552. return "0";
  1553. }
  1554. if( createInterpolator )
  1555. dataCollector.AddToInput( uniqueId, dataName, dataType, currentPrecisionType );
  1556. MasterNodePortCategory portCategory = dataCollector.PortCategory;
  1557. dataCollector.PortCategory = MasterNodePortCategory.Vertex;
  1558. if( createInterpolator )
  1559. {
  1560. dataCollector.AddLocalVariable( uniqueId, Constants.VertexShaderOutputStr + "." + dataName, dataValue + ";" );
  1561. }
  1562. else
  1563. {
  1564. dataCollector.AddLocalVariable( uniqueId, currentPrecisionType, dataType, dataName, dataValue );
  1565. }
  1566. dataCollector.PortCategory = portCategory;
  1567. return createInterpolator ? Constants.InputVarStr + "." + dataName : dataName;
  1568. }
  1569. }
  1570. public static void AddCustomStandardSamplingMacros( ref MasterNodeDataCollector dataCollector, TextureType type, MipType mip )
  1571. {
  1572. AddCustomStandardSamplingMacros( ref dataCollector, Constants.TextureToWire[ type ], mip );
  1573. }
  1574. public static void AddCustomStandardSamplingMacros( ref MasterNodeDataCollector dataCollector, WirePortDataType type, MipType mip )
  1575. {
  1576. MacrosMask result = MacrosMask.NONE;
  1577. switch( mip )
  1578. {
  1579. default:
  1580. case MipType.Auto:
  1581. result |= MacrosMask.AUTO;
  1582. break;
  1583. case MipType.MipLevel:
  1584. result |= MacrosMask.LOD;
  1585. break;
  1586. case MipType.MipBias:
  1587. result |= MacrosMask.BIAS;
  1588. break;
  1589. case MipType.Derivative:
  1590. result |= MacrosMask.GRAD;
  1591. break;
  1592. }
  1593. switch( type )
  1594. {
  1595. default:
  1596. case WirePortDataType.SAMPLER2D:
  1597. dataCollector.Using2DMacrosMask |= result;
  1598. break;
  1599. case WirePortDataType.SAMPLER3D:
  1600. dataCollector.Using3DMacrosMask |= result;
  1601. break;
  1602. case WirePortDataType.SAMPLERCUBE:
  1603. dataCollector.UsingCUBEMacrosMask |= result;
  1604. break;
  1605. case WirePortDataType.SAMPLER2DARRAY:
  1606. dataCollector.Using2DArrayMacrosMask |= result;
  1607. break;
  1608. }
  1609. }
  1610. public static void AddCustom3DSRPMacros( ref MasterNodeDataCollector dataCollector )
  1611. {
  1612. // add just once
  1613. if( dataCollector.UsingExtra3DSRPMacros )
  1614. return;
  1615. dataCollector.UsingExtra3DSRPMacros = true;
  1616. for( int i = 0; i < Constants.CustomSRPSamplingMacros.Length; i++ )
  1617. dataCollector.AddToDirectives( Constants.CustomSRPSamplingMacros[ i ] );
  1618. }
  1619. public static void AddCustomArraySamplingMacros( ref MasterNodeDataCollector dataCollector )
  1620. {
  1621. // add just once
  1622. if( dataCollector.UsingArrayDerivatives )
  1623. return;
  1624. dataCollector.UsingArrayDerivatives = true;
  1625. for( int i = 0; i < Constants.CustomArraySamplingMacros.Length; i++ )
  1626. dataCollector.AddToDirectives( Constants.CustomArraySamplingMacros[ i ] );
  1627. }
  1628. /*public static void AddCustomASEMacros( ref MasterNodeDataCollector dataCollector )
  1629. {
  1630. string varPrefix = dataCollector.IsSRP ? varPrefix = "TEXTURE" : "UNITY_DECLARE_TEX";
  1631. if( dataCollector.IsSRP )
  1632. {
  1633. for( int i = 0; i < Constants.CustomASESRPArgsMacros.Length; i++ )
  1634. {
  1635. dataCollector.AddToDirectives( Constants.CustomASESRPArgsMacros[ i ] );
  1636. }
  1637. for( int i = 0; i < Constants.CustomSRPSamplingMacros.Length; i++ )
  1638. {
  1639. dataCollector.AddToDirectives( Constants.CustomSRPSamplingMacros[ i ] );
  1640. }
  1641. }
  1642. else
  1643. {
  1644. for( int i = 0; i < Constants.CustomASEStandardArgsMacros.Length; i++ )
  1645. {
  1646. dataCollector.AddToDirectives( Constants.CustomASEStandardArgsMacros[ i ] );
  1647. }
  1648. for( int i = 0; i < Constants.CustomStandardSamplingMacros.Length; i++ )
  1649. {
  1650. dataCollector.AddToDirectives( Constants.CustomStandardSamplingMacros[ i ] );
  1651. }
  1652. }
  1653. for( int i = 0; i < Constants.CustomASEDeclararionMacros.Length; i++ )
  1654. {
  1655. string value = string.Format( Constants.CustomASEDeclararionMacros[ i ], varPrefix );
  1656. dataCollector.AddToDirectives( value );
  1657. }
  1658. string samplePrefix = string.Empty;
  1659. string samplerArgs = string.Empty;
  1660. string samplerDecl = string.Empty;
  1661. if( dataCollector.IsSRP )
  1662. {
  1663. samplePrefix = "SAMPLE_TEXTURE";
  1664. samplerArgs = "samplerName,";
  1665. for( int i = 0; i < Constants.CustomASESamplingMacros.Length; i++ )
  1666. {
  1667. string value = string.Format( Constants.CustomASESamplingMacros[ i ], samplerArgs, samplePrefix, samplerDecl );
  1668. dataCollector.AddToDirectives( value );
  1669. }
  1670. }
  1671. else
  1672. {
  1673. samplePrefix = "UNITY_SAMPLE_TEX";
  1674. samplerArgs = "samplerName,";
  1675. samplerDecl = "_SAMPLER";
  1676. dataCollector.AddToDirectives( Constants.CustomASEStandarSamplingMacrosHelper[ 0 ] );
  1677. for( int i = 0; i < Constants.CustomASESamplingMacros.Length; i++ )
  1678. {
  1679. string value = string.Format( Constants.CustomASESamplingMacros[ i ], samplerArgs, samplePrefix, samplerDecl );
  1680. dataCollector.AddToDirectives( value );
  1681. }
  1682. dataCollector.AddToDirectives( Constants.CustomASEStandarSamplingMacrosHelper[ 1 ] );
  1683. samplerArgs = string.Empty;
  1684. samplerDecl = string.Empty;
  1685. for( int i = 0; i < Constants.CustomASESamplingMacros.Length; i++ )
  1686. {
  1687. string value = string.Format( Constants.CustomASESamplingMacros[ i ], samplerArgs, samplePrefix, samplerDecl );
  1688. dataCollector.AddToDirectives( value );
  1689. }
  1690. dataCollector.AddToDirectives( Constants.CustomASEStandarSamplingMacrosHelper[ 2 ] );
  1691. }
  1692. }*/
  1693. public static void RegisterUnity2019MatrixDefines( ref MasterNodeDataCollector dataCollector )
  1694. {
  1695. if( dataCollector.IsSRP && dataCollector.TemplateDataCollectorInstance.IsHDRP )
  1696. {
  1697. //dataCollector.AddToDefines( -1, "unity_CameraProjection UNITY_MATRIX_P" );
  1698. //dataCollector.AddToDefines( -1, "unity_CameraInvProjection UNITY_MATRIX_I_P" );
  1699. //dataCollector.AddToDefines( -1, "unity_WorldToCamera UNITY_MATRIX_V" );
  1700. //dataCollector.AddToDefines( -1, "unity_CameraToWorld UNITY_MATRIX_I_V" );
  1701. dataCollector.AddToUniforms( -1, "float4x4 unity_CameraProjection;" );
  1702. dataCollector.AddToUniforms( -1, "float4x4 unity_CameraInvProjection;" );
  1703. dataCollector.AddToUniforms( -1, "float4x4 unity_WorldToCamera;" );
  1704. dataCollector.AddToUniforms( -1, "float4x4 unity_CameraToWorld;" );
  1705. }
  1706. }
  1707. }
  1708. }