0 votes

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]
Godot version v3.2.3.stable.official
in Engine by (24 points)

1 Answer

0 votes
Best answer

I figured it out. inputvars is an array of strings, so inputvars[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.

by (24 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.