可视化着色器插件

Visual Shader插件用于在GDScript中创建自定义的 VisualShader 节点.

创建过程与一般的编辑器插件不同. 你不需要创建一个 plugin.cfg 文件来注册, 而是创建并保存一个脚本文件, 只要用 class_name 注册了自定义节点, 就可以使用了.

本篇短教程将讲解如何制作Perlin-3D噪声节点(原代码来自本 GPU 噪声着色器插件 <https://github.com/curly-brace/Godot-3.0-Noise-Shaders/blob/master/assets/gpu\_noise\_shaders/classic\_perlin3d.tres&gt; _.

创建一个Sprite并为其材质插槽分配一个 ShaderMaterial:

../../../_images/visual_shader_plugins_start.png

VisualShader 给着色器材质的插槽:

../../../_images/visual_shader_plugins_start2.png

不要忘记将其模式改为 “CanvasItem”(如果你使用的是Sprite):

../../../_images/visual_shader_plugins_start3.png

创建一个从 VisualShaderNodeCustom 派生的脚本. 这是你初始化你的插件所需要的全部内容.

  1. # PerlinNoise3D.gd
  2. tool
  3. extends VisualShaderNodeCustom
  4. class_name VisualShaderNodePerlinNoise3D
  5. func _get_name():
  6. return "PerlinNoise3D"
  7. func _get_category():
  8. return "MyShaderNodes"
  9. func _get_description():
  10. return "Classic Perlin-Noise-3D function (by Curly-Brace)"
  11. func _get_return_icon_type():
  12. return VisualShaderNode.PORT_TYPE_SCALAR
  13. func _get_input_port_count():
  14. return 4
  15. func _get_input_port_name(port):
  16. match port:
  17. 0:
  18. return "uv"
  19. 1:
  20. return "offset"
  21. 2:
  22. return "scale"
  23. 3:
  24. return "time"
  25. func _get_input_port_type(port):
  26. match port:
  27. 0:
  28. return VisualShaderNode.PORT_TYPE_VECTOR
  29. 1:
  30. return VisualShaderNode.PORT_TYPE_VECTOR
  31. 2:
  32. return VisualShaderNode.PORT_TYPE_SCALAR
  33. 3:
  34. return VisualShaderNode.PORT_TYPE_SCALAR
  35. func _get_output_port_count():
  36. return 1
  37. func _get_output_port_name(port):
  38. return "result"
  39. func _get_output_port_type(port):
  40. return VisualShaderNode.PORT_TYPE_SCALAR
  41. func _get_global_code(mode):
  42. return """
  43. vec3 mod289_3(vec3 x) {
  44. return x - floor(x * (1.0 / 289.0)) * 289.0;
  45. }
  46. vec4 mod289_4(vec4 x) {
  47. return x - floor(x * (1.0 / 289.0)) * 289.0;
  48. }
  49. vec4 permute(vec4 x) {
  50. return mod289_4(((x * 34.0) + 1.0) * x);
  51. }
  52. vec4 taylorInvSqrt(vec4 r) {
  53. return 1.79284291400159 - 0.85373472095314 * r;
  54. }
  55. vec3 fade(vec3 t) {
  56. return t * t * t * (t * (t * 6.0 - 15.0) + 10.0);
  57. }
  58. // Classic Perlin noise.
  59. float cnoise(vec3 P) {
  60. vec3 Pi0 = floor(P); // Integer part for indexing.
  61. vec3 Pi1 = Pi0 + vec3(1.0); // Integer part + 1.
  62. Pi0 = mod289_3(Pi0);
  63. Pi1 = mod289_3(Pi1);
  64. vec3 Pf0 = fract(P); // Fractional part for interpolation.
  65. vec3 Pf1 = Pf0 - vec3(1.0); // Fractional part - 1.0.
  66. vec4 ix = vec4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
  67. vec4 iy = vec4(Pi0.yy, Pi1.yy);
  68. vec4 iz0 = vec4(Pi0.z);
  69. vec4 iz1 = vec4(Pi1.z);
  70. vec4 ixy = permute(permute(ix) + iy);
  71. vec4 ixy0 = permute(ixy + iz0);
  72. vec4 ixy1 = permute(ixy + iz1);
  73. vec4 gx0 = ixy0 * (1.0 / 7.0);
  74. vec4 gy0 = fract(floor(gx0) * (1.0 / 7.0)) - 0.5;
  75. gx0 = fract(gx0);
  76. vec4 gz0 = vec4(0.5) - abs(gx0) - abs(gy0);
  77. vec4 sz0 = step(gz0, vec4(0.0));
  78. gx0 -= sz0 * (step(0.0, gx0) - 0.5);
  79. gy0 -= sz0 * (step(0.0, gy0) - 0.5);
  80. vec4 gx1 = ixy1 * (1.0 / 7.0);
  81. vec4 gy1 = fract(floor(gx1) * (1.0 / 7.0)) - 0.5;
  82. gx1 = fract(gx1);
  83. vec4 gz1 = vec4(0.5) - abs(gx1) - abs(gy1);
  84. vec4 sz1 = step(gz1, vec4(0.0));
  85. gx1 -= sz1 * (step(0.0, gx1) - 0.5);
  86. gy1 -= sz1 * (step(0.0, gy1) - 0.5);
  87. vec3 g000 = vec3(gx0.x, gy0.x, gz0.x);
  88. vec3 g100 = vec3(gx0.y, gy0.y, gz0.y);
  89. vec3 g010 = vec3(gx0.z, gy0.z, gz0.z);
  90. vec3 g110 = vec3(gx0.w, gy0.w, gz0.w);
  91. vec3 g001 = vec3(gx1.x, gy1.x, gz1.x);
  92. vec3 g101 = vec3(gx1.y, gy1.y, gz1.y);
  93. vec3 g011 = vec3(gx1.z, gy1.z, gz1.z);
  94. vec3 g111 = vec3(gx1.w, gy1.w, gz1.w);
  95. vec4 norm0 = taylorInvSqrt(vec4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110)));
  96. g000 *= norm0.x;
  97. g010 *= norm0.y;
  98. g100 *= norm0.z;
  99. g110 *= norm0.w;
  100. vec4 norm1 = taylorInvSqrt(vec4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111)));
  101. g001 *= norm1.x;
  102. g011 *= norm1.y;
  103. g101 *= norm1.z;
  104. g111 *= norm1.w;
  105. float n000 = dot(g000, Pf0);
  106. float n100 = dot(g100, vec3(Pf1.x, Pf0.yz));
  107. float n010 = dot(g010, vec3(Pf0.x, Pf1.y, Pf0.z));
  108. float n110 = dot(g110, vec3(Pf1.xy, Pf0.z));
  109. float n001 = dot(g001, vec3(Pf0.xy, Pf1.z));
  110. float n101 = dot(g101, vec3(Pf1.x, Pf0.y, Pf1.z));
  111. float n011 = dot(g011, vec3(Pf0.x, Pf1.yz));
  112. float n111 = dot(g111, Pf1);
  113. vec3 fade_xyz = fade(Pf0);
  114. vec4 n_z = mix(vec4(n000, n100, n010, n110), vec4(n001, n101, n011, n111), fade_xyz.z);
  115. vec2 n_yz = mix(n_z.xy, n_z.zw, fade_xyz.y);
  116. float n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x);
  117. return 2.2 * n_xyz;
  118. }
  119. """
  120. func _get_code(input_vars, output_vars, mode, type):
  121. return output_vars[0] + " = cnoise(vec3((%s.xy + %s.xy) * %s, %s)) * 0.5 + 0.5;" % [input_vars[0], input_vars[1], input_vars[2], input_vars[3]]

保存它并打开Visual Shader. 你应该可以在成员对话框中看到你的新节点类型(如果你看不到你的新节点, 请尝试重新启动编辑器):

../../../_images/visual_shader_plugins_result1.png

放到一个图并连接所需的端口:

../../../_images/visual_shader_plugins_result2.png

这就是你需要做的所有事情, 正如你所看到的那样, 创建你自己的自定义VisualShader节点很容易!