NoiseGeneratorNode.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. // Based on the work by https://github.com/keijiro/NoiseShader
  4. using System;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace AmplifyShaderEditor
  8. {
  9. public enum NoiseGeneratorType
  10. {
  11. Simplex2D,
  12. Simplex3D,
  13. Gradient,
  14. Simple
  15. };
  16. [Serializable]
  17. [NodeAttributes( "Noise Generator", "Miscellaneous", "Collection of procedural noise generators", tags: "simplex gradient" )]
  18. public sealed class NoiseGeneratorNode : ParentNode
  19. {
  20. private const string TypeLabelStr = "Type";
  21. private const string SetTo01RangeOpStr = "{0} = {0}*0.5 + 0.5;";
  22. private const string SetToMinus1To1RangeOpStr = "{0} = {0}*2 - 1;";
  23. private const string SetTo01RangeLabel = "0-1 Range";
  24. private const string SetTo01RangePreviewId = "_To01Range";
  25. private const string UseUnityVersionLabel = "Use Unity Version";
  26. // Simple
  27. private const string SimpleNoiseRandomValueFunc = "inline float noise_randomValue (float2 uv) { return frac(sin(dot(uv, float2(12.9898, 78.233)))*43758.5453); }";
  28. private const string SimpleNoiseInterpolateFunc = "inline float noise_interpolate (float a, float b, float t) { return (1.0-t)*a + (t*b); }";
  29. private const string SimpleValueNoiseHeader = "inline float valueNoise (float2 uv)";
  30. private readonly string[] SimpleValueNoiseBody = { "inline float valueNoise (float2 uv)\n",
  31. "{\n",
  32. "\tfloat2 i = floor(uv);\n",
  33. "\tfloat2 f = frac( uv );\n",
  34. "\tf = f* f * (3.0 - 2.0 * f);\n",
  35. "\tuv = abs( frac(uv) - 0.5);\n",
  36. "\tfloat2 c0 = i + float2( 0.0, 0.0 );\n",
  37. "\tfloat2 c1 = i + float2( 1.0, 0.0 );\n",
  38. "\tfloat2 c2 = i + float2( 0.0, 1.0 );\n",
  39. "\tfloat2 c3 = i + float2( 1.0, 1.0 );\n",
  40. "\tfloat r0 = noise_randomValue( c0 );\n",
  41. "\tfloat r1 = noise_randomValue( c1 );\n",
  42. "\tfloat r2 = noise_randomValue( c2 );\n",
  43. "\tfloat r3 = noise_randomValue( c3 );\n",
  44. "\tfloat bottomOfGrid = noise_interpolate( r0, r1, f.x );\n",
  45. "\tfloat topOfGrid = noise_interpolate( r2, r3, f.x );\n",
  46. "\tfloat t = noise_interpolate( bottomOfGrid, topOfGrid, f.y );\n",
  47. "\treturn t;\n",
  48. "}\n"};
  49. private const string SimpleNoiseHeader = "float SimpleNoise(float2 UV, float Scale)";
  50. private const string SimpleNoiseFunc = "SimpleNoise( {0} )";
  51. private readonly string[] SimpleNoiseBody = { "float SimpleNoise(float2 UV)\n",
  52. "{\n",
  53. "\tfloat t = 0.0;\n",
  54. "\tfloat freq = pow( 2.0, float( 0 ) );\n",
  55. "\tfloat amp = pow( 0.5, float( 3 - 0 ) );\n",
  56. "\tt += valueNoise( UV/freq )*amp;\n",
  57. "\tfreq = pow(2.0, float(1));\n",
  58. "\tamp = pow(0.5, float(3-1));\n",
  59. "\tt += valueNoise( UV/freq )*amp;\n",
  60. "\tfreq = pow(2.0, float(2));\n",
  61. "\tamp = pow(0.5, float(3-2));\n",
  62. "\tt += valueNoise( UV/freq )*amp;\n",
  63. "\treturn t;\n",
  64. "}\n"};
  65. // Simplex 2D
  66. private const string Simplex2DFloat3Mod289Func = "float3 mod2D289( float3 x ) { return x - floor( x * ( 1.0 / 289.0 ) ) * 289.0; }";
  67. private const string Simplex2DFloat2Mod289Func = "float2 mod2D289( float2 x ) { return x - floor( x * ( 1.0 / 289.0 ) ) * 289.0; }";
  68. private const string Simplex2DPermuteFunc = "float3 permute( float3 x ) { return mod2D289( ( ( x * 34.0 ) + 1.0 ) * x ); }";
  69. private const string SimplexNoise2DHeader = "float snoise( float2 v )";
  70. private const string SimplexNoise2DFunc = "snoise( {0} )";
  71. private readonly string[] SimplexNoise2DBody = {"float snoise( float2 v )\n",
  72. "{\n",
  73. "\tconst float4 C = float4( 0.211324865405187, 0.366025403784439, -0.577350269189626, 0.024390243902439 );\n",
  74. "\tfloat2 i = floor( v + dot( v, C.yy ) );\n",
  75. "\tfloat2 x0 = v - i + dot( i, C.xx );\n",
  76. "\tfloat2 i1;\n",
  77. "\ti1 = ( x0.x > x0.y ) ? float2( 1.0, 0.0 ) : float2( 0.0, 1.0 );\n",
  78. "\tfloat4 x12 = x0.xyxy + C.xxzz;\n",
  79. "\tx12.xy -= i1;\n",
  80. "\ti = mod2D289( i );\n",
  81. "\tfloat3 p = permute( permute( i.y + float3( 0.0, i1.y, 1.0 ) ) + i.x + float3( 0.0, i1.x, 1.0 ) );\n",
  82. "\tfloat3 m = max( 0.5 - float3( dot( x0, x0 ), dot( x12.xy, x12.xy ), dot( x12.zw, x12.zw ) ), 0.0 );\n",
  83. "\tm = m * m;\n",
  84. "\tm = m * m;\n",
  85. "\tfloat3 x = 2.0 * frac( p * C.www ) - 1.0;\n",
  86. "\tfloat3 h = abs( x ) - 0.5;\n",
  87. "\tfloat3 ox = floor( x + 0.5 );\n",
  88. "\tfloat3 a0 = x - ox;\n",
  89. "\tm *= 1.79284291400159 - 0.85373472095314 * ( a0 * a0 + h * h );\n",
  90. "\tfloat3 g;\n",
  91. "\tg.x = a0.x * x0.x + h.x * x0.y;\n",
  92. "\tg.yz = a0.yz * x12.xz + h.yz * x12.yw;\n",
  93. "\treturn 130.0 * dot( m, g );\n",
  94. "}\n"};
  95. // Simplex 3D
  96. private const string Simplex3DFloat3Mod289 = "float3 mod3D289( float3 x ) { return x - floor( x / 289.0 ) * 289.0; }";
  97. private const string Simplex3DFloat4Mod289 = "float4 mod3D289( float4 x ) { return x - floor( x / 289.0 ) * 289.0; }";
  98. private const string Simplex3DFloat4Permute = "float4 permute( float4 x ) { return mod3D289( ( x * 34.0 + 1.0 ) * x ); }";
  99. private const string TaylorInvSqrtFunc = "float4 taylorInvSqrt( float4 r ) { return 1.79284291400159 - r * 0.85373472095314; }";
  100. private const string SimplexNoise3DHeader = "float snoise( float3 v )";
  101. private const string SimplexNoise3DFunc = "snoise( {0} )";
  102. private readonly string[] SimplexNoise3DBody =
  103. {
  104. "float snoise( float3 v )\n",
  105. "{\n",
  106. "\tconst float2 C = float2( 1.0 / 6.0, 1.0 / 3.0 );\n",
  107. "\tfloat3 i = floor( v + dot( v, C.yyy ) );\n",
  108. "\tfloat3 x0 = v - i + dot( i, C.xxx );\n",
  109. "\tfloat3 g = step( x0.yzx, x0.xyz );\n",
  110. "\tfloat3 l = 1.0 - g;\n",
  111. "\tfloat3 i1 = min( g.xyz, l.zxy );\n",
  112. "\tfloat3 i2 = max( g.xyz, l.zxy );\n",
  113. "\tfloat3 x1 = x0 - i1 + C.xxx;\n",
  114. "\tfloat3 x2 = x0 - i2 + C.yyy;\n",
  115. "\tfloat3 x3 = x0 - 0.5;\n",
  116. "\ti = mod3D289( i);\n",
  117. "\tfloat4 p = permute( permute( permute( i.z + float4( 0.0, i1.z, i2.z, 1.0 ) ) + i.y + float4( 0.0, i1.y, i2.y, 1.0 ) ) + i.x + float4( 0.0, i1.x, i2.x, 1.0 ) );\n",
  118. "\tfloat4 j = p - 49.0 * floor( p / 49.0 ); // mod(p,7*7)\n",
  119. "\tfloat4 x_ = floor( j / 7.0 );\n",
  120. "\tfloat4 y_ = floor( j - 7.0 * x_ ); // mod(j,N)\n",
  121. "\tfloat4 x = ( x_ * 2.0 + 0.5 ) / 7.0 - 1.0;\n",
  122. "\tfloat4 y = ( y_ * 2.0 + 0.5 ) / 7.0 - 1.0;\n",
  123. "\tfloat4 h = 1.0 - abs( x ) - abs( y );\n",
  124. "\tfloat4 b0 = float4( x.xy, y.xy );\n",
  125. "\tfloat4 b1 = float4( x.zw, y.zw );\n",
  126. "\tfloat4 s0 = floor( b0 ) * 2.0 + 1.0;\n",
  127. "\tfloat4 s1 = floor( b1 ) * 2.0 + 1.0;\n",
  128. "\tfloat4 sh = -step( h, 0.0 );\n",
  129. "\tfloat4 a0 = b0.xzyw + s0.xzyw * sh.xxyy;\n",
  130. "\tfloat4 a1 = b1.xzyw + s1.xzyw * sh.zzww;\n",
  131. "\tfloat3 g0 = float3( a0.xy, h.x );\n",
  132. "\tfloat3 g1 = float3( a0.zw, h.y );\n",
  133. "\tfloat3 g2 = float3( a1.xy, h.z );\n",
  134. "\tfloat3 g3 = float3( a1.zw, h.w );\n",
  135. "\tfloat4 norm = taylorInvSqrt( float4( dot( g0, g0 ), dot( g1, g1 ), dot( g2, g2 ), dot( g3, g3 ) ) );\n",
  136. "\tg0 *= norm.x;\n",
  137. "\tg1 *= norm.y;\n",
  138. "\tg2 *= norm.z;\n",
  139. "\tg3 *= norm.w;\n",
  140. "\tfloat4 m = max( 0.6 - float4( dot( x0, x0 ), dot( x1, x1 ), dot( x2, x2 ), dot( x3, x3 ) ), 0.0 );\n",
  141. "\tm = m* m;\n",
  142. "\tm = m* m;\n",
  143. "\tfloat4 px = float4( dot( x0, g0 ), dot( x1, g1 ), dot( x2, g2 ), dot( x3, g3 ) );\n",
  144. "\treturn 42.0 * dot( m, px);\n",
  145. "}\n"
  146. };
  147. //Gradient Noise
  148. private readonly string UnityGradientNoiseFunc = "UnityGradientNoise({0},{1})";
  149. private readonly string[] UnityGradientNoiseFunctionsBody =
  150. {
  151. "float2 UnityGradientNoiseDir( float2 p )\n",
  152. "{\n",
  153. "\tp = fmod(p , 289);\n",
  154. "\tfloat x = fmod((34 * p.x + 1) * p.x , 289) + p.y;\n",
  155. "\tx = fmod( (34 * x + 1) * x , 289);\n",
  156. "\tx = frac( x / 41 ) * 2 - 1;\n",
  157. "\treturn normalize( float2(x - floor(x + 0.5 ), abs( x ) - 0.5 ) );\n",
  158. "}\n",
  159. "\n",
  160. "float UnityGradientNoise( float2 UV, float Scale )\n",
  161. "{\n",
  162. "\tfloat2 p = UV * Scale;\n",
  163. "\tfloat2 ip = floor( p );\n",
  164. "\tfloat2 fp = frac( p );\n",
  165. "\tfloat d00 = dot( UnityGradientNoiseDir( ip ), fp );\n",
  166. "\tfloat d01 = dot( UnityGradientNoiseDir( ip + float2( 0, 1 ) ), fp - float2( 0, 1 ) );\n",
  167. "\tfloat d10 = dot( UnityGradientNoiseDir( ip + float2( 1, 0 ) ), fp - float2( 1, 0 ) );\n",
  168. "\tfloat d11 = dot( UnityGradientNoiseDir( ip + float2( 1, 1 ) ), fp - float2( 1, 1 ) );\n",
  169. "\tfp = fp * fp * fp * ( fp * ( fp * 6 - 15 ) + 10 );\n",
  170. "\treturn lerp( lerp( d00, d01, fp.y ), lerp( d10, d11, fp.y ), fp.x ) + 0.5;\n",
  171. "}\n"
  172. };
  173. private readonly string GradientNoiseFunc = "GradientNoise({0},{1})";
  174. private readonly string[] GradientNoiseFunctionsBody =
  175. {
  176. "//https://www.shadertoy.com/view/XdXGW8\n",
  177. "float2 GradientNoiseDir( float2 x )\n",
  178. "{\n",
  179. "\tconst float2 k = float2( 0.3183099, 0.3678794 );\n",
  180. "\tx = x * k + k.yx;\n",
  181. "\treturn -1.0 + 2.0 * frac( 16.0 * k * frac( x.x * x.y * ( x.x + x.y ) ) );\n",
  182. "}\n",
  183. "\n",
  184. "float GradientNoise( float2 UV, float Scale )\n",
  185. "{\n",
  186. "\tfloat2 p = UV * Scale;\n",
  187. "\tfloat2 i = floor( p );\n",
  188. "\tfloat2 f = frac( p );\n",
  189. "\tfloat2 u = f * f * ( 3.0 - 2.0 * f );\n",
  190. "\treturn lerp( lerp( dot( GradientNoiseDir( i + float2( 0.0, 0.0 ) ), f - float2( 0.0, 0.0 ) ),\n",
  191. "\t\t\tdot( GradientNoiseDir( i + float2( 1.0, 0.0 ) ), f - float2( 1.0, 0.0 ) ), u.x ),\n",
  192. "\t\t\tlerp( dot( GradientNoiseDir( i + float2( 0.0, 1.0 ) ), f - float2( 0.0, 1.0 ) ),\n",
  193. "\t\t\tdot( GradientNoiseDir( i + float2( 1.0, 1.0 ) ), f - float2( 1.0, 1.0 ) ), u.x ), u.y );\n",
  194. "}\n"
  195. };
  196. [SerializeField]
  197. private NoiseGeneratorType m_type = NoiseGeneratorType.Simplex2D;
  198. [SerializeField]
  199. private bool m_setTo01Range = true;
  200. [SerializeField]
  201. private bool m_unityVersion = false;
  202. private int m_setTo01RangePreviewId;
  203. private UpperLeftWidgetHelper m_upperLeftWidget = new UpperLeftWidgetHelper();
  204. protected override void CommonInit( int uniqueId )
  205. {
  206. base.CommonInit( uniqueId );
  207. AddInputPort( WirePortDataType.FLOAT2, false, "UV" );
  208. AddInputPort( WirePortDataType.FLOAT, false, "Scale" );
  209. m_inputPorts[ 1 ].FloatInternalData = 1;
  210. AddOutputPort( WirePortDataType.FLOAT, Constants.EmptyPortValue );
  211. m_useInternalPortData = true;
  212. m_autoWrapProperties = true;
  213. m_hasLeftDropdown = true;
  214. SetAdditonalTitleText( string.Format( Constants.SubTitleTypeFormatStr, m_type ) );
  215. m_previewShaderGUID = "cd2d37ef5da190b42a91a5a690ba2a7d";
  216. ConfigurePorts();
  217. }
  218. public override void OnEnable()
  219. {
  220. base.OnEnable();
  221. m_setTo01RangePreviewId = Shader.PropertyToID( SetTo01RangePreviewId );
  222. }
  223. public override void AfterCommonInit()
  224. {
  225. base.AfterCommonInit();
  226. if( PaddingTitleLeft == 0 )
  227. {
  228. PaddingTitleLeft = Constants.PropertyPickerWidth + Constants.IconsLeftRightMargin;
  229. if( PaddingTitleRight == 0 )
  230. PaddingTitleRight = Constants.PropertyPickerWidth + Constants.IconsLeftRightMargin;
  231. }
  232. }
  233. public override void Destroy()
  234. {
  235. base.Destroy();
  236. m_upperLeftWidget = null;
  237. }
  238. public override void SetPreviewInputs()
  239. {
  240. base.SetPreviewInputs();
  241. float range01 = m_setTo01Range ? 1 : 0;
  242. PreviewMaterial.SetFloat( m_setTo01RangePreviewId, range01 );
  243. }
  244. public override void Draw( DrawInfo drawInfo )
  245. {
  246. base.Draw( drawInfo );
  247. m_upperLeftWidget.DrawWidget<NoiseGeneratorType>( ref m_type, this, OnWidgetUpdate );
  248. }
  249. private readonly Action<ParentNode> OnWidgetUpdate = ( x ) =>
  250. {
  251. ( x as NoiseGeneratorNode ).ConfigurePorts();
  252. };
  253. public override void DrawProperties()
  254. {
  255. base.DrawProperties();
  256. EditorGUI.BeginChangeCheck();
  257. m_type = (NoiseGeneratorType)EditorGUILayoutEnumPopup( TypeLabelStr, m_type );
  258. if( EditorGUI.EndChangeCheck() )
  259. {
  260. ConfigurePorts();
  261. }
  262. m_setTo01Range = EditorGUILayoutToggle( SetTo01RangeLabel, m_setTo01Range );
  263. if( m_type == NoiseGeneratorType.Gradient )
  264. {
  265. EditorGUI.BeginChangeCheck();
  266. m_unityVersion = EditorGUILayoutToggle( UseUnityVersionLabel, m_unityVersion );
  267. if( EditorGUI.EndChangeCheck() )
  268. {
  269. ConfigurePorts();
  270. }
  271. }
  272. //EditorGUILayout.HelpBox( "Node still under construction. Use with caution", MessageType.Info );
  273. }
  274. private void ConfigurePorts()
  275. {
  276. SetAdditonalTitleText( string.Format( Constants.SubTitleTypeFormatStr, m_type ) );
  277. switch( m_type )
  278. {
  279. case NoiseGeneratorType.Simplex2D:
  280. {
  281. m_inputPorts[ 0 ].ChangeType( WirePortDataType.FLOAT2, false );
  282. m_previewMaterialPassId = 0;
  283. }
  284. break;
  285. case NoiseGeneratorType.Simplex3D:
  286. {
  287. m_inputPorts[ 0 ].ChangeType( WirePortDataType.FLOAT3, false );
  288. m_previewMaterialPassId = 1;
  289. }
  290. break;
  291. case NoiseGeneratorType.Gradient:
  292. {
  293. m_inputPorts[ 0 ].ChangeType( WirePortDataType.FLOAT2, false );
  294. m_previewMaterialPassId = m_unityVersion ? 3 : 2;
  295. }
  296. break;
  297. case NoiseGeneratorType.Simple:
  298. {
  299. m_inputPorts[ 0 ].ChangeType( WirePortDataType.FLOAT2, false );
  300. m_previewMaterialPassId = 4;
  301. }
  302. break;
  303. }
  304. PreviewIsDirty = true;
  305. }
  306. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  307. {
  308. if( m_outputPorts[ outputId ].IsLocalValue( dataCollector.PortCategory ) )
  309. {
  310. return m_outputPorts[ outputId ].LocalValue( dataCollector.PortCategory );
  311. }
  312. string size = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  313. string scale = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector );
  314. switch( m_type )
  315. {
  316. case NoiseGeneratorType.Simplex2D:
  317. {
  318. string float3Mod289Func = ( dataCollector.IsTemplate ) ? Simplex2DFloat3Mod289Func : "\t\t" + Simplex2DFloat3Mod289Func;
  319. dataCollector.AddFunction( Simplex2DFloat3Mod289Func, float3Mod289Func );
  320. string float2Mod289Func = ( dataCollector.IsTemplate ) ? Simplex2DFloat2Mod289Func : "\t\t" + Simplex2DFloat2Mod289Func;
  321. dataCollector.AddFunction( Simplex2DFloat2Mod289Func, float2Mod289Func );
  322. string permuteFunc = ( dataCollector.IsTemplate ) ? Simplex2DPermuteFunc : "\t\t" + Simplex2DPermuteFunc;
  323. dataCollector.AddFunction( Simplex2DPermuteFunc, permuteFunc );
  324. dataCollector.AddFunction( SimplexNoise2DHeader, SimplexNoise2DBody, false );
  325. if( m_inputPorts[ 1 ].IsConnected || m_inputPorts[ 1 ].FloatInternalData != 1.0f )
  326. {
  327. size = string.Format( "{0}*{1}", size, scale );
  328. }
  329. RegisterLocalVariable( 0, string.Format( SimplexNoise2DFunc, size ), ref dataCollector, ( "simplePerlin2D" + OutputId ) );
  330. }
  331. break;
  332. case NoiseGeneratorType.Simplex3D:
  333. {
  334. string float3Mod289Func = ( dataCollector.IsTemplate ) ? Simplex3DFloat3Mod289 : "\t\t" + Simplex3DFloat3Mod289;
  335. dataCollector.AddFunction( Simplex3DFloat3Mod289, float3Mod289Func );
  336. string float4Mod289Func = ( dataCollector.IsTemplate ) ? Simplex3DFloat4Mod289 : "\t\t" + Simplex3DFloat4Mod289;
  337. dataCollector.AddFunction( Simplex3DFloat4Mod289, float4Mod289Func );
  338. string permuteFunc = ( dataCollector.IsTemplate ) ? Simplex3DFloat4Permute : "\t\t" + Simplex3DFloat4Permute;
  339. dataCollector.AddFunction( Simplex3DFloat4Permute, permuteFunc );
  340. string taylorInvSqrtFunc = ( dataCollector.IsTemplate ) ? TaylorInvSqrtFunc : "\t\t" + TaylorInvSqrtFunc;
  341. dataCollector.AddFunction( TaylorInvSqrtFunc, taylorInvSqrtFunc );
  342. dataCollector.AddFunction( SimplexNoise3DHeader, SimplexNoise3DBody, false );
  343. if( m_inputPorts[ 1 ].IsConnected || m_inputPorts[ 1 ].FloatInternalData != 1.0f )
  344. {
  345. size = string.Format( "{0}*{1}", size, scale );
  346. }
  347. RegisterLocalVariable( 0, string.Format( SimplexNoise3DFunc, size ), ref dataCollector, ( "simplePerlin3D" + OutputId ) );
  348. }
  349. break;
  350. case NoiseGeneratorType.Gradient:
  351. {
  352. string[] body = m_unityVersion ? UnityGradientNoiseFunctionsBody : GradientNoiseFunctionsBody;
  353. string func = m_unityVersion ? UnityGradientNoiseFunc : GradientNoiseFunc;
  354. dataCollector.AddFunction( body[ 0 ], body, false);
  355. RegisterLocalVariable( 0, string.Format( func, size, scale ), ref dataCollector, ( "gradientNoise" + OutputId ) );
  356. }
  357. break;
  358. case NoiseGeneratorType.Simple:
  359. {
  360. string randomValue = ( dataCollector.IsTemplate ) ? SimpleNoiseRandomValueFunc : "\t\t" + SimpleNoiseRandomValueFunc;
  361. dataCollector.AddFunction( SimpleNoiseRandomValueFunc, randomValue );
  362. string interpolate = ( dataCollector.IsTemplate ) ? SimpleNoiseInterpolateFunc : "\t\t" + SimpleNoiseInterpolateFunc;
  363. dataCollector.AddFunction( SimpleNoiseInterpolateFunc, interpolate );
  364. dataCollector.AddFunction( SimpleValueNoiseHeader, SimpleValueNoiseBody, false );
  365. dataCollector.AddFunction( SimpleNoiseHeader, SimpleNoiseBody, false );
  366. if( m_inputPorts[ 1 ].IsConnected || m_inputPorts[ 1 ].FloatInternalData != 1.0f )
  367. {
  368. size = string.Format( "{0}*{1}", size, scale );
  369. }
  370. RegisterLocalVariable( 0, string.Format( SimpleNoiseFunc, size ), ref dataCollector, ( "simpleNoise" + OutputId ) );
  371. }
  372. break;
  373. }
  374. if( m_type == NoiseGeneratorType.Simple && !m_setTo01Range )
  375. {
  376. dataCollector.AddLocalVariable( outputId, string.Format( SetToMinus1To1RangeOpStr, m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory ) ) );
  377. }
  378. if( m_setTo01Range && m_type != NoiseGeneratorType.Simple )
  379. {
  380. dataCollector.AddLocalVariable( outputId, string.Format( SetTo01RangeOpStr, m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory ) ) );
  381. }
  382. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  383. }
  384. public override void ReadFromString( ref string[] nodeParams )
  385. {
  386. base.ReadFromString( ref nodeParams );
  387. m_type = (NoiseGeneratorType)Enum.Parse( typeof( NoiseGeneratorType ), GetCurrentParam( ref nodeParams ) );
  388. if( UIUtils.CurrentShaderVersion() < 16903 )
  389. {
  390. m_setTo01Range = false;
  391. }
  392. else
  393. {
  394. m_setTo01Range = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  395. m_unityVersion = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  396. }
  397. ConfigurePorts();
  398. }
  399. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  400. {
  401. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  402. IOUtils.AddFieldValueToString( ref nodeInfo, m_type );
  403. IOUtils.AddFieldValueToString( ref nodeInfo, m_setTo01Range );
  404. IOUtils.AddFieldValueToString( ref nodeInfo, m_unityVersion );
  405. }
  406. }
  407. }