可视化着色器插件

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

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

本篇短教程将讲解如何制作 Perlin-3D 噪声节点(原代码来自这个 GPU 噪声着色器插件

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

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

VisualShader 给着色器材质的插槽:

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

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

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

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

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

Save it and open the Visual Shader. You should see your new node type within the member’s dialog under the Addons category (if you can’t see your new node, try restarting the editor):

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

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

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

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

Previous Next


© 版权所有 2014-present Juan Linietsky, Ariel Manzur and the Godot community (CC BY 3.0). Revision b1c660f7.

Built with Sphinx using a theme provided by Read the Docs.