Godot4中Material.tres使用旧弃用参数名的警告问题
问题描述
在Godot 4正式稳定版中,将以下Canvas Item类型的Shader附加到Color Rect后,运行场景时反复收到警告:
W 0:00:01:0839 _set: This material (containing shader with path: 'res://addons/scene_manager/scene_manager.tres::Shader_vksmm') uses an old deprecated parameter names. Consider re-saving this resource (or scene which contains it) in order for it to continue working in future versions.
<C++ Source> scene/resources/material.cpp:184 @ _set()
使用的Shader代码:
shader_type canvas_item; uniform sampler2D custom_texture; uniform float cutoff: hint_range(0, 1) = 1; uniform float smoothness: hint_range(0, 1) = 0.1; uniform vec3 color = vec3(0, 0, 0); uniform bool inverted = false; uniform bool linear_fade = true; void fragment() { if (!linear_fade) { float value = texture(custom_texture, UV).r; if (inverted) { value = 1.0 - value; } float alpha = smoothstep(cutoff, cutoff + smoothness, value * (1.0 - smoothness) + smoothness); COLOR = vec4(color.rgb, alpha); } else { COLOR = vec4(color.rgb, (1.0 - cutoff)); } }
解决步骤
重新保存Shader资源
找到对应的Shader文件(若Shader内嵌在材质中,打开材质的Shader编辑面板),无需修改代码,直接点击编辑器顶部的保存按钮(快捷键Ctrl+S)。重新保存关联的材质资源
打开警告中指定的材质文件res://addons/scene_manager/scene_manager.tres,确认Shader引用无误后,保存该材质。重新保存场景文件
打开包含该Color Rect的场景文件,直接保存场景,确保资源关联更新。清理Shader缓存(若上述步骤无效)
- 关闭Godot编辑器
- 删除项目根目录下
.godot文件夹中的shader_cache子目录 - 重新打开项目,重复步骤1-3
原因说明
该警告通常是因为材质/Shader资源中保留了Godot 3时代的旧参数命名映射缓存,即使Shader代码本身已经符合Godot 4的规范,资源文件的元数据仍未更新。重新保存资源会让Godot重新生成适配新版本的参数定义,清理缓存则能彻底移除旧的无效数据。
内容的提问来源于stack exchange,提问作者Mahmood

