Modifying shader parameter in script

Godot Version

v4.2.1.stable.mono.official [b09f793f5]

Question

I’m trying to modify the color parameter in a shader with this script:

[Tool]
public partial class FloorTool : Node
{
    [Export] public Vector3 size;
    CollisionShape3D shape3D;
    MeshInstance3D mesh3D;
    ShaderMaterial mat;

    BoxShape3D shape;
    BoxMesh mesh;

    [Export]Color color;

    public override void _Ready()
    {
        shape3D = GetNode<CollisionShape3D>("CollisionShape3D");
        mesh3D = GetNode<MeshInstance3D>("MeshInstance3D");
        
        shape = (BoxShape3D)shape3D.Shape;
        mesh = (BoxMesh)mesh3D.Mesh;
        
        mat = (ShaderMaterial)mesh.Material;

    }

    public override void _PhysicsProcess(double delta)
    {   
        shape.Size = size;
        mesh.Size = size;
        
        mat.SetShaderParameter("shader_parameter/color", color);
    }

}

But it doesn’t work. Here is shader for context:

shader_type spatial;

render_mode blend_mix,depth_draw_opaque,cull_disabled,diffuse_burley,specular_schlick_ggx;
uniform vec4 albedo : source_color;
uniform sampler2D texture_albedo : source_color,filter_linear_mipmap,repeat_enable;
uniform float point_size : hint_range(0,128);
uniform float roughness : hint_range(0,1);
uniform sampler2D texture_metallic : hint_default_white,filter_linear_mipmap,repeat_enable;
uniform vec4 metallic_texture_channel;
uniform sampler2D texture_roughness : hint_roughness_r,filter_linear_mipmap,repeat_enable;
uniform float specular;
uniform float metallic;
uniform sampler2D texture_normal : hint_roughness_normal,filter_linear_mipmap,repeat_enable;
uniform float normal_scale : hint_range(-16,16);
uniform vec3 uv1_scale;
uniform vec3 uv1_offset;
uniform vec3 uv2_scale;
uniform vec3 uv2_offset;

uniform vec4 color : source_color;

void vertex() {
	UV=UV*uv1_scale.xy+uv1_offset.xy;
}

void fragment() {
	//Default Stuff
	vec2 base_uv = UV;
	vec4 albedo_tex = texture(texture_albedo,base_uv);
	ALBEDO = albedo.rgb * albedo_tex.rgb;
	float metallic_tex = dot(texture(texture_metallic,base_uv),metallic_texture_channel);
	METALLIC = metallic_tex * metallic;
	vec4 roughness_texture_channel = vec4(1.0,0.0,0.0,0.0);
	float roughness_tex = dot(texture(texture_roughness,base_uv),roughness_texture_channel);
	ROUGHNESS = roughness_tex * roughness;
	SPECULAR = specular;
	NORMAL_MAP = texture(texture_normal,base_uv).rgb;
	NORMAL_MAP_DEPTH = normal_scale;
	
	//Color Change
    float brightness = (albedo_tex.r + albedo_tex.g + albedo_tex.b) / 3.0;
	vec4 grayscale = vec4(brightness, brightness, brightness, COLOR.a);
	ALBEDO = albedo_tex.rgb * color.rgb;
}

Any help?

In this line mat.SetShaderParameter("shader_parameter/color", color);
try this instead: mat.SetShaderParameter("color", color);

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.