You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
4.5 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. //https://www.shadertoy.com/view/XsX3zB
  4. using System;
  5. using UnityEngine;
  6. using UnityEditor;
  7. namespace AmplifyShaderEditor
  8. {
  9. public enum NoiseType
  10. {
  11. Simplex3D,
  12. Simplex3DFractal
  13. }
  14. [Serializable]
  15. [NodeAttributes( "[Deprecated] Simplex Noise", "Image Effects", "Noise generated via the Simplex algorithm",null,KeyCode.None,false,true)]
  16. public sealed class SimplexNoiseNode : ParentNode
  17. {
  18. private string m_randomFuncBody;
  19. private string m_simplex3dFuncBody;
  20. private string m_simplex3dFractalFuncBody;
  21. private const string RandomfunctionHeader = "Random3({0})";
  22. private const string Simplex3dfunctionHeader = "Simplex3d({0})";
  23. private const string Simplex3dFractalfunctionHeader = "Simplex3dFractal( {0})";
  24. private const string NoiseTypeStr = "Type";
  25. [SerializeField]
  26. private NoiseType m_type = NoiseType.Simplex3D;
  27. protected override void CommonInit( int uniqueId )
  28. {
  29. base.CommonInit( uniqueId );
  30. IOUtils.AddFunctionHeader( ref m_randomFuncBody, "float3 Random3 ( float3 c )" );
  31. IOUtils.AddFunctionLine( ref m_randomFuncBody, "float fracMul = 512.0;float j = 4096.0*sin ( dot ( c, float3 ( 17.0, 59.4, 15.0 ) ) );float3 r;r.z = frac ( fracMul*j );j *= .125;r.x = frac ( fracMul*j );j *= .125;r.y = frac ( fracMul*j );return r - 0.5;" );
  32. IOUtils.CloseFunctionBody( ref m_randomFuncBody );
  33. IOUtils.AddFunctionHeader( ref m_simplex3dFuncBody, "float3 Simplex3d ( float3 p )" );
  34. IOUtils.AddFunctionLine( ref m_simplex3dFuncBody, "float F3 = 0.3333333;float G3 = 0.1666667;float3 s = floor ( p + dot ( p, F3.xxx ) );float3 x = p - s + dot ( s, G3.xxx );float3 e = step ( ( 0.0 ).xxx, x - x.yzx );float3 i1 = e*( 1.0 - e.zxy );float3 i2 = 1.0 - e.zxy*( 1.0 - e );float3 x1 = x - i1 + G3;float3 x2 = x - i2 + 2.0*G3;float3 x3 = x - 1.0 + 3.0*G3;float4 w, d;w.x = dot ( x, x );w.y = dot ( x1, x1 );w.z = dot ( x2, x2 );w.w = dot ( x3, x3 );w = max ( 0.6 - w, 0.0 );d.x = dot ( Random3 ( s ), x );d.y = dot ( Random3 ( s + i1 ), x1 );d.z = dot ( Random3 ( s + i2 ), x2 );d.w = dot ( Random3 ( s + 1.0 ), x3 );w *= w;w *= w;d *= w;return dot ( d, ( 52.0 ).xxx ).xxx;" );
  35. IOUtils.CloseFunctionBody( ref m_simplex3dFuncBody );
  36. IOUtils.AddFunctionHeader( ref m_simplex3dFractalFuncBody, "float3 Simplex3dFractal ( float3 m )" );
  37. IOUtils.AddFunctionLine( ref m_simplex3dFractalFuncBody, "return (0.5333333*Simplex3d ( m ) + 0.2666667*Simplex3d ( 2.0*m ) + 0.1333333*Simplex3d ( 4.0*m ) + 0.0666667*Simplex3d ( 8.0*m )).xxx;" );
  38. IOUtils.CloseFunctionBody( ref m_simplex3dFractalFuncBody );
  39. AddInputPort( WirePortDataType.FLOAT3, false, "Position" );
  40. AddInputPort( WirePortDataType.FLOAT, false, "Width" );
  41. AddOutputPort( WirePortDataType.FLOAT3, Constants.EmptyPortValue );
  42. m_textLabelWidth = 50;
  43. m_useInternalPortData = true;
  44. m_autoWrapProperties = true;
  45. }
  46. public override void DrawProperties()
  47. {
  48. base.DrawProperties();
  49. m_type = ( NoiseType ) EditorGUILayoutEnumPopup( NoiseTypeStr, m_type );
  50. }
  51. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  52. {
  53. string posValue = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  54. string widthValue = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector );
  55. dataCollector.AddFunctions( RandomfunctionHeader, m_randomFuncBody, "0" );
  56. string result = string.Empty;
  57. switch ( m_type )
  58. {
  59. case NoiseType.Simplex3D:
  60. {
  61. string finalValue = dataCollector.AddFunctions( Simplex3dfunctionHeader, m_simplex3dFuncBody, posValue + "*" + widthValue );
  62. result = finalValue + "* 0.5 + 0.5";
  63. }break;
  64. case NoiseType.Simplex3DFractal:
  65. {
  66. dataCollector.AddFunctions( Simplex3dfunctionHeader, m_simplex3dFuncBody, posValue + "*" + widthValue );
  67. string finalValue = dataCollector.AddFunctions( Simplex3dFractalfunctionHeader, m_simplex3dFractalFuncBody, posValue + "*" + widthValue + "+" + widthValue );
  68. result = finalValue + "* 0.5 + 0.5";
  69. }break;
  70. }
  71. return CreateOutputLocalVariable( 0, result, ref dataCollector );
  72. }
  73. public override void ReadFromString( ref string[] nodeParams )
  74. {
  75. base.ReadFromString( ref nodeParams );
  76. m_type = ( NoiseType ) Enum.Parse( typeof( NoiseType ), GetCurrentParam( ref nodeParams ) );
  77. }
  78. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  79. {
  80. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  81. IOUtils.AddFieldValueToString( ref nodeInfo, m_type );
  82. }
  83. }
  84. }