0 votes

How can i get a list of all params in a shader loaded
into a material with gdscript?
thanks

Godot version 3.51
in Engine by (16 points)

1 Answer

0 votes

Ok,

this should work for most shader (I hope).
This code assume that uniform name is always in the second place of the line,
and that the shader is written without double space.
I hope there's something simpler as i think that this feature is quite useful for shader
newbies like me.

func load_file(file):

var f = File.new()
f.open(file, File.READ)
var index = 1
while not f.eof_reached(): # iterate through all lines until the end of file is reached
    var line = f.get_line()
    line += " "
    #print(line + str(index))
    if "uniform" in line:
        var l 
        l = line.split(" ")                    
        print(l[2]) # assume that the shadere is written
                    # like this: uniform space vartype space varname

    index += 1
f.close()
return
by (16 points)

Better

file is the current shader path

func getshaderparam(file):

var f = File.new()
f.open(file, File.READ)
var index = 1
var param =[]
while not f.eof_reached(): # iterate through all lines until the end of file is reached
    var line = f.get_line()
    line += " "
    #print(line + str(index))
    if "uniform" in line:
        var l 
        l = line.split(" ")
        print(l[2])
        param.append(l[2])

    index += 1
f.close()
return param

This is not how shaders are meant to work.
Please tell me when you will ever need to do this?

Infact my question was:

Blockquote

How can i get a list of all params in a shader loaded into a material with gdscript?

Blockquote

If you have a better solution it would be nice to hear you.
Mine is quite unclean, but works for my needs.
The final purpose is to have a control for every uniform found in the shader.

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.