Godot Version
4.5.1
Question
Hi guys.
I’m trying to add my first custom node to the Shader Editor, and I’m unable to get it to show up. I read the docs, followed tutorials, but can’t get it to show up. The code from other people’s DO show up (i.e. the docs example of PerlinNoise3D). So it’s got to be something to do with my code.
I’m defining all the required functions, tried restart and switching Vertex/Fragment, and if I insert a print statement, it does print, indicating that the code has compiled and running. Below is my script.
Please help:
@tool
extends VisualShaderNodeCustom
class_name TriplanarSampler
func _is_available(mode, type): return mode == Shader.Mode.MODE_SPATIAL && type == VisualShader.Type.TYPE_FRAGMENT
func _get_name(): return "Triplanar Texture Sampler"
func _get_description(): return "Samples single texture using world coordinates"
func _get_category(): return "MyShaderNodes"
func _get_default_input_port(type):
match type:
PortType.PORT_TYPE_VECTOR_3D: return 0
PortType.PORT_TYPE_SCALAR: return 1
_: return 2
func _get_return_icon_type(): return PORT_TYPE_VECTOR_4D
func _get_input_port_count(): return 3
func _get_input_port_name(port):
match port:
0: return "offset"
1: return "scale"
_: return "sampler"
func _get_input_port_type(port):
match port:
0: return PORT_TYPE_VECTOR_3D
1: return PORT_TYPE_SCALAR
_: return PORT_TYPE_SAMPLER
func _get_input_port_default_value(port):
match port:
0: return Vector2.ZERO
1: return 1.0
_: return null
func _get_output_port_count(): return 1
func _get_output_port_name(_port): return "result"
func _get_output_port_type(_port): return PORT_TYPE_VECTOR_4D
func _get_global_code(_mode):
return """
vec4 triplanar_map(vec4 x, vec4 y, vec4 z, vec3 n) {
n = n * n;
return (x * n.x + y * n.y + z * n.z) / (n.x + n.y + n.z);
}
vec4 triplanar(sampler2D texture, vec3 offset, float scale) {
vec3 wpos = (INV_VIEW_MATRIX * vec4(VERTEX, 1.0)).xyz;
vec3 wnorm = (INV_VIEW_MATRIX * vec4(NORMAL, 0.0)).xyz;
vec4 colx = texture(texture, wpos.zy * scale);
vec4 coly = texture(texture, wpos.xz * scale);
vec5 colz = texture(texture, wpos.xy * scale);
return triplanar_map(colx, coly, colz, wnorm);
}
"""
func _get_code(input_vars, output_vars, _mode, _type):
return output_vars[0] + " = triplanar(%s, %s, %s);" % [input_vars[0], input_vars[1], input_vars[2]]