FresnelNode.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. // http://kylehalladay.com/blog/tutorial/2014/02/18/Fresnel-Shaders-From-The-Ground-Up.html
  4. // http://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter07.html
  5. using System;
  6. using UnityEngine;
  7. using UnityEditor;
  8. namespace AmplifyShaderEditor
  9. {
  10. [Serializable]
  11. [NodeAttributes( "Fresnel", "Surface Data", "Simple Fresnel effect" )]
  12. public sealed class FresnelNode : ParentNode
  13. {
  14. private const string FresnedFinalVar = "fresnelNode";
  15. [SerializeField]
  16. private ViewSpace m_normalSpace = ViewSpace.Tangent;
  17. enum FresnelType
  18. {
  19. Standard = 0,
  20. Schlick,
  21. SchlickIOR,
  22. }
  23. enum NormalType
  24. {
  25. WorldNormal = 0,
  26. TangentNormal,
  27. HalfVector,
  28. }
  29. enum ViewType
  30. {
  31. ViewDir = 0,
  32. LightDir,
  33. }
  34. [SerializeField]
  35. private FresnelType m_fresnelType = FresnelType.Standard;
  36. [SerializeField]
  37. private NormalType m_normalType = NormalType.WorldNormal;
  38. [SerializeField]
  39. private ViewType m_viewType = ViewType.ViewDir;
  40. [SerializeField]
  41. private bool m_normalizeVectors = false;
  42. [SerializeField]
  43. private bool m_safePower = false;
  44. private InputPort m_normalVecPort;
  45. private InputPort m_viewVecPort;
  46. private InputPort m_biasPort;
  47. private InputPort m_scalePort;
  48. private InputPort m_powerPort;
  49. protected override void CommonInit( int uniqueId )
  50. {
  51. base.CommonInit( uniqueId );
  52. AddInputPort( WirePortDataType.FLOAT3, false, "World Normal", -1, MasterNodePortCategory.Fragment, 0 );
  53. AddInputPort( WirePortDataType.FLOAT3, false, "View Dir", -1, MasterNodePortCategory.Fragment, 4 );
  54. AddInputPort( WirePortDataType.FLOAT, false, "Bias", -1, MasterNodePortCategory.Fragment, 1 );
  55. AddInputPort( WirePortDataType.FLOAT, false, "Scale", -1, MasterNodePortCategory.Fragment, 2 );
  56. AddInputPort( WirePortDataType.FLOAT, false, "Power", -1, MasterNodePortCategory.Fragment, 3 );
  57. AddOutputPort( WirePortDataType.FLOAT, "Out" );
  58. m_normalVecPort = m_inputPorts[ 0 ];
  59. m_viewVecPort = m_inputPorts[ 1 ];
  60. m_biasPort = m_inputPorts[ 2 ];
  61. m_scalePort = m_inputPorts[ 3 ];
  62. m_powerPort = m_inputPorts[ 4 ];
  63. m_biasPort.AutoDrawInternalData = true;
  64. m_scalePort.AutoDrawInternalData = true;
  65. m_powerPort.AutoDrawInternalData = true;
  66. m_autoWrapProperties = true;
  67. m_drawPreviewAsSphere = true;
  68. m_normalVecPort.Vector3InternalData = Vector3.forward;
  69. m_scalePort.FloatInternalData = 1;
  70. m_powerPort.FloatInternalData = 5;
  71. m_previewShaderGUID = "240145eb70cf79f428015012559f4e7d";
  72. }
  73. public override void SetPreviewInputs()
  74. {
  75. base.SetPreviewInputs();
  76. //m_mate
  77. PreviewMaterial.SetInt( "_FresnelType", (int)m_fresnelType );
  78. if( m_normalType == NormalType.TangentNormal && m_normalVecPort.IsConnected )
  79. m_previewMaterialPassId = 2;
  80. else if( (m_normalType == NormalType.WorldNormal || m_normalType == NormalType.HalfVector ) && m_normalVecPort.IsConnected && !m_viewVecPort.IsConnected )
  81. m_previewMaterialPassId = 1;
  82. else if( m_normalType == NormalType.HalfVector && !m_normalVecPort.IsConnected && !m_viewVecPort.IsConnected )
  83. m_previewMaterialPassId = 3;
  84. else if( m_normalVecPort.IsConnected && m_viewVecPort.IsConnected )
  85. m_previewMaterialPassId = 4;
  86. else if( !m_normalVecPort.IsConnected && !m_viewVecPort.IsConnected && m_viewType == ViewType.LightDir )
  87. m_previewMaterialPassId = 5;
  88. else if( !m_normalVecPort.IsConnected && m_viewVecPort.IsConnected && m_normalType == NormalType.HalfVector )
  89. m_previewMaterialPassId = 7;
  90. else if( !m_normalVecPort.IsConnected && m_viewVecPort.IsConnected )
  91. m_previewMaterialPassId = 6;
  92. else
  93. m_previewMaterialPassId = 0;
  94. }
  95. public override void DrawProperties()
  96. {
  97. base.DrawProperties();
  98. EditorGUI.BeginChangeCheck();
  99. m_fresnelType = (FresnelType)EditorGUILayoutEnumPopup( "Type", m_fresnelType );
  100. m_normalType = (NormalType)EditorGUILayoutEnumPopup( "Normal Vector", m_normalType );
  101. m_viewType = (ViewType)EditorGUILayoutEnumPopup( "View Vector", m_viewType );
  102. if( EditorGUI.EndChangeCheck() )
  103. {
  104. UpdatePort();
  105. }
  106. if( !m_biasPort.IsConnected && m_biasPort.Visible )
  107. m_biasPort.FloatInternalData = EditorGUILayoutFloatField( m_biasPort.Name, m_biasPort.FloatInternalData );
  108. if( !m_scalePort.IsConnected && m_scalePort.Visible )
  109. m_scalePort.FloatInternalData = EditorGUILayoutFloatField( m_scalePort.Name, m_scalePort.FloatInternalData );
  110. if( !m_powerPort.IsConnected && m_powerPort.Visible )
  111. m_powerPort.FloatInternalData = EditorGUILayoutFloatField( m_powerPort.Name, m_powerPort.FloatInternalData );
  112. m_normalizeVectors = EditorGUILayoutToggle( "Normalize Vectors", m_normalizeVectors );
  113. m_safePower = EditorGUILayoutToggle( PowerNode.SafePowerLabel, m_safePower );
  114. }
  115. private void UpdatePort()
  116. {
  117. switch( m_normalType )
  118. {
  119. default:
  120. case NormalType.WorldNormal:
  121. m_normalVecPort.Name = "World Normal";
  122. break;
  123. case NormalType.TangentNormal:
  124. m_normalVecPort.Name = "Normal";
  125. break;
  126. case NormalType.HalfVector:
  127. m_normalVecPort.Name = "Half Vector";
  128. break;
  129. }
  130. switch( m_viewType )
  131. {
  132. default:
  133. case ViewType.ViewDir:
  134. m_viewVecPort.Name = "View Dir";
  135. break;
  136. case ViewType.LightDir:
  137. m_viewVecPort.Name = "Light Dir";
  138. break;
  139. }
  140. switch( m_fresnelType )
  141. {
  142. default:
  143. case FresnelType.Standard:
  144. m_biasPort.Visible = true;
  145. m_biasPort.Name = "Bias";
  146. m_scalePort.Name = "Scale";
  147. m_scalePort.Visible = true;
  148. m_powerPort.Visible = true;
  149. break;
  150. case FresnelType.Schlick:
  151. m_biasPort.Visible = true;
  152. m_biasPort.Name = "F0";
  153. m_scalePort.Visible = false;
  154. m_powerPort.Visible = false;
  155. break;
  156. case FresnelType.SchlickIOR:
  157. m_biasPort.Visible = false;
  158. m_scalePort.Name = "IOR";
  159. m_scalePort.Visible = true;
  160. m_powerPort.Visible = false;
  161. break;
  162. }
  163. m_sizeIsDirty = true;
  164. }
  165. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  166. {
  167. if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  168. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  169. if( dataCollector.IsFragmentCategory )
  170. dataCollector.AddToInput( UniqueId, SurfaceInputs.WORLD_POS );
  171. string viewdir = string.Empty;
  172. if( m_viewType == ViewType.ViewDir )
  173. {
  174. if( m_viewVecPort.IsConnected )
  175. viewdir = m_viewVecPort.GeneratePortInstructions( ref dataCollector );
  176. else
  177. viewdir = GeneratorUtils.GenerateViewDirection( ref dataCollector, UniqueId, space: ViewSpace.World );
  178. }
  179. else
  180. {
  181. if( m_viewVecPort.IsConnected )
  182. viewdir = m_viewVecPort.GeneratePortInstructions( ref dataCollector );
  183. else
  184. viewdir = GeneratorUtils.GenerateWorldLightDirection( ref dataCollector, UniqueId, CurrentPrecisionType );
  185. }
  186. string normal = string.Empty;
  187. if( m_normalType == NormalType.WorldNormal || m_normalType == NormalType.TangentNormal )
  188. {
  189. if( m_normalVecPort.IsConnected )
  190. {
  191. normal = m_normalVecPort.GeneratePortInstructions( ref dataCollector );
  192. if( dataCollector.IsFragmentCategory )
  193. {
  194. dataCollector.AddToInput( UniqueId, SurfaceInputs.INTERNALDATA, addSemiColon: false );
  195. if( m_normalType == NormalType.TangentNormal )
  196. {
  197. if( dataCollector.IsTemplate )
  198. {
  199. normal = dataCollector.TemplateDataCollectorInstance.GetWorldNormal( UniqueId, CurrentPrecisionType, normal, OutputId );
  200. }
  201. else
  202. {
  203. normal = GeneratorUtils.GenerateWorldNormal( ref dataCollector, UniqueId, CurrentPrecisionType, normal, OutputId );
  204. dataCollector.AddToInput( UniqueId, SurfaceInputs.WORLD_NORMAL, UIUtils.CurrentWindow.CurrentGraph.CurrentPrecision );
  205. dataCollector.ForceNormal = true;
  206. }
  207. }
  208. else
  209. {
  210. if( m_normalizeVectors )
  211. normal = string.Format( "normalize( {0} )", normal );
  212. }
  213. }
  214. else
  215. {
  216. if( m_normalType == NormalType.TangentNormal )
  217. {
  218. string wtMatrix = GeneratorUtils.GenerateWorldToTangentMatrix( ref dataCollector, UniqueId, CurrentPrecisionType );
  219. normal = "mul( " + normal + "," + wtMatrix + " )";
  220. }
  221. }
  222. }
  223. else
  224. {
  225. if( dataCollector.IsFragmentCategory )
  226. {
  227. dataCollector.AddToInput( UniqueId, SurfaceInputs.WORLD_NORMAL, UIUtils.CurrentWindow.CurrentGraph.CurrentPrecision );
  228. if( dataCollector.DirtyNormal )
  229. dataCollector.AddToInput( UniqueId, SurfaceInputs.INTERNALDATA, addSemiColon: false );
  230. }
  231. if( dataCollector.IsTemplate )
  232. normal = dataCollector.TemplateDataCollectorInstance.GetWorldNormal( CurrentPrecisionType, normalize: ( dataCollector.DirtyNormal && m_normalizeVectors ) );
  233. else
  234. normal = GeneratorUtils.GenerateWorldNormal( ref dataCollector, UniqueId, ( dataCollector.DirtyNormal && m_normalizeVectors ) );
  235. if( dataCollector.DirtyNormal )
  236. {
  237. dataCollector.ForceNormal = true;
  238. }
  239. }
  240. }
  241. else
  242. {
  243. // generate HV
  244. if( !m_normalVecPort.IsConnected )
  245. {
  246. string halfView = GeneratorUtils.GenerateViewDirection( ref dataCollector, UniqueId, space: ViewSpace.World );
  247. string halfLight = GeneratorUtils.GenerateWorldLightDirection( ref dataCollector, UniqueId, CurrentPrecisionType );
  248. normal = "halfVector" + OutputId;
  249. dataCollector.AddLocalVariable( UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT3, normal, "normalize( " + halfView + " + " + halfLight + " )" );
  250. }
  251. else
  252. {
  253. normal = m_normalVecPort.GeneratePortInstructions( ref dataCollector );
  254. if( m_normalizeVectors )
  255. normal = string.Format( "normalize( {0} )", normal );
  256. }
  257. }
  258. string bias = m_biasPort.GeneratePortInstructions( ref dataCollector );
  259. string scale = m_scalePort.GeneratePortInstructions( ref dataCollector );
  260. string power = m_powerPort.GeneratePortInstructions( ref dataCollector );
  261. string fresnelNDotVLocalValue = "dot( " + normal + ", " + viewdir + " )";
  262. string fresnelNDotVLocalVar = "fresnelNdotV" + OutputId;
  263. dataCollector.AddLocalVariable( UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT, fresnelNDotVLocalVar, fresnelNDotVLocalValue );
  264. string fresnelFinalVar = FresnedFinalVar + OutputId;
  265. string result = string.Empty;
  266. switch( m_fresnelType )
  267. {
  268. default:
  269. case FresnelType.Standard:
  270. {
  271. string powOp = m_safePower? string.Format( "pow( max( 1.0 - {0} , 0.0001 ), {1} )", fresnelNDotVLocalVar, power ):
  272. string.Format( "pow( 1.0 - {0}, {1} )", fresnelNDotVLocalVar, power );
  273. result = string.Format( "( {0} + {1} * {2} )", bias, scale, powOp );
  274. }
  275. break;
  276. case FresnelType.Schlick:
  277. {
  278. string f0VarName = "f0" + OutputId;
  279. dataCollector.AddLocalVariable( UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT, f0VarName, bias );
  280. string powOp = m_safePower? string.Format( "pow( max( 1.0 - {0} , 0.0001 ), 5 )", fresnelNDotVLocalVar ) :
  281. string.Format( "pow( 1.0 - {0}, 5 )", fresnelNDotVLocalVar );
  282. result = string.Format( "( {0} + ( 1.0 - {0} ) * {1} )", f0VarName, powOp );
  283. }
  284. break;
  285. case FresnelType.SchlickIOR:
  286. {
  287. string iorVarName = "ior" + OutputId;
  288. dataCollector.AddLocalVariable( UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT, iorVarName, scale );
  289. string iorPowOp = m_safePower? string.Format( "pow( max( ( 1 - {0} ) / ( 1 + {0} ) , 0.0001 ), 2 )", iorVarName ):
  290. string.Format( "pow( ( 1 - {0} ) / ( 1 + {0} ), 2 )", iorVarName );
  291. dataCollector.AddLocalVariable( UniqueId, iorVarName +" = "+ iorPowOp + ";");
  292. string fresnelPowOp = m_safePower? string.Format( "pow( max( 1.0 - {0} , 0.0001 ), 5 )", fresnelNDotVLocalVar ):
  293. string.Format( "pow( 1.0 - {0}, 5 )", fresnelNDotVLocalVar );
  294. result = string.Format( "( {0} + ( 1.0 - {0} ) * {1} )", iorVarName, fresnelPowOp );
  295. }
  296. break;
  297. }
  298. RegisterLocalVariable( 0, result, ref dataCollector, fresnelFinalVar );
  299. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  300. }
  301. public override void PropagateNodeData( NodeData nodeData, ref MasterNodeDataCollector dataCollector )
  302. {
  303. base.PropagateNodeData( nodeData, ref dataCollector );
  304. if( m_normalType == NormalType.TangentNormal && m_normalVecPort.IsConnected )
  305. dataCollector.DirtyNormal = true;
  306. }
  307. public override void ReadFromString( ref string[] nodeParams )
  308. {
  309. base.ReadFromString( ref nodeParams );
  310. if( UIUtils.CurrentShaderVersion() > 15305 )
  311. {
  312. m_fresnelType = (FresnelType)Enum.Parse( typeof( FresnelType ), GetCurrentParam( ref nodeParams ) );
  313. m_normalType = (NormalType)Enum.Parse( typeof( NormalType ), GetCurrentParam( ref nodeParams ) );
  314. m_viewType = (ViewType)Enum.Parse( typeof( ViewType ), GetCurrentParam( ref nodeParams ) );
  315. m_normalizeVectors = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  316. if( UIUtils.CurrentShaderVersion() > 17502 )
  317. m_safePower = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  318. }
  319. else
  320. {
  321. if( UIUtils.CurrentShaderVersion() >= 13202 )
  322. {
  323. m_normalSpace = (ViewSpace)Enum.Parse( typeof( ViewSpace ), GetCurrentParam( ref nodeParams ) );
  324. }
  325. else
  326. {
  327. m_normalSpace = ViewSpace.World;
  328. }
  329. if( m_normalSpace == ViewSpace.World )
  330. m_normalType = NormalType.WorldNormal;
  331. else
  332. m_normalType = NormalType.TangentNormal;
  333. }
  334. UpdatePort();
  335. }
  336. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  337. {
  338. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  339. IOUtils.AddFieldValueToString( ref nodeInfo, m_fresnelType );
  340. IOUtils.AddFieldValueToString( ref nodeInfo, m_normalType );
  341. IOUtils.AddFieldValueToString( ref nodeInfo, m_viewType );
  342. IOUtils.AddFieldValueToString( ref nodeInfo, m_normalizeVectors );
  343. IOUtils.AddFieldValueToString( ref nodeInfo, m_safePower );
  344. }
  345. }
  346. }