Custom Visual Shader Node. How to access input vec3 x,y,z?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By chimeforest

Hello,

I’m creating a few nodes for creating random scalars/vec3s. I’ve already got two done (scalar → random scalar, scalar → semi-random vec3), but I’m having trouble on the third(vec3 → random vec3).
I thought it must be something to do with the input_vars at the end?
But I’ve tried:

 [input_vars[0].x,input_vars[0].y,input_vars[0].z]

and

[input_vars[0].r,input_vars[0].g,input_vars[0].b]

and even

[input_vars[0][0],input_vars[0][1],input_vars[0][2]]

But none of them work. Instead of flashing random colors like the ones I have working, this one just output a solid white.
Below is my full code. Do you see anything wrong with it?

#RandomNode
tool
extends VisualShaderNodeCustom
class_name VisualShaderNodeVec3Random

func _get_name():
	return "Vec3Random"

func _get_category():
	return "Random"

func _get_description():
	return "Random function (by Chimeforest)"

func _get_return_icon_type():
	return VisualShaderNode.PORT_TYPE_VECTOR

func _get_input_port_count():
	return 1

func _get_input_port_name(port):
	match port:
		0:
			return "Seed"

func _get_input_port_type(port):
	match port:
		0:
			return VisualShaderNode.PORT_TYPE_VECTOR

func _get_output_port_count():
	return 1

func _get_output_port_name(port):
	return "result"

func _get_output_port_type(port):
	return VisualShaderNode.PORT_TYPE_VECTOR

func _get_code(input_vars, output_vars, mode, type):
	return output_vars[0] + """ = vec3(
		fract(sin(dot(vec2(%s,0.0), vec2(12.9898,78.233))) * 43758.5453),
		fract(sin(dot(vec2(%s,0.0), vec2(12.9898,78.233))) * 43758.5453),
		fract(sin(dot(vec2(%s,0.0), vec2(12.9898,78.233))) * 43758.5453));"""%[input_vars[0].x,input_vars[0].y,input_vars[0].z]
:bust_in_silhouette: Reply From: chimeforest

I figured it out. input_vars is an array of strings, so input_vars[0][0] just returns the first character of that string (the ‘v’ of ‘vec###’), and input_vars[0].x or .r don’t work on strings. So in order to access them, you have to put it in the formatted string… so (%s,0.0) needs to be (%s.r,0.0).

It feels weird to me, but I think I understand why. It’s not like writing code to be compiled, it’s writing code that’s writing code that is then compiled. Very interesting.