فهرست منبع

prefix shader parameters with "shader_parameter/"

Nathan Franke 3 سال پیش
والد
کامیت
82d2a97a83
2فایلهای تغییر یافته به همراه43 افزوده شده و 37 حذف شده
  1. 10 33
      scene/resources/material.cpp
  2. 33 4
      scene/resources/shader.h

+ 10 - 33
scene/resources/material.cpp

@@ -156,17 +156,7 @@ Material::~Material() {
 
 bool ShaderMaterial::_set(const StringName &p_name, const Variant &p_value) {
 	if (shader.is_valid()) {
-		StringName pr = shader->remap_uniform(p_name);
-		if (!pr) {
-			String n = p_name;
-			if (n.find("shader_parameter/") == 0) { //backwards compatibility
-				pr = n.replace_first("shader_parameter/", "");
-			} else if (n.find("shader_uniform/") == 0) { //backwards compatibility
-				pr = n.replace_first("shader_uniform/", "");
-			} else if (n.find("param/") == 0) { //backwards compatibility
-				pr = n.substr(6, n.length());
-			}
-		}
+		StringName pr = shader->remap_parameter(p_name);
 		if (pr) {
 			set_shader_parameter(pr, p_value);
 			return true;
@@ -178,25 +168,9 @@ bool ShaderMaterial::_set(const StringName &p_name, const Variant &p_value) {
 
 bool ShaderMaterial::_get(const StringName &p_name, Variant &r_ret) const {
 	if (shader.is_valid()) {
-		StringName pr = shader->remap_uniform(p_name);
-		if (!pr) {
-			String n = p_name;
-			if (n.find("shader_parameter/") == 0) { //backwards compatibility
-				pr = n.replace_first("shader_parameter/", "");
-			} else if (n.find("shader_uniform/") == 0) { //backwards compatibility
-				pr = n.replace_first("shader_uniform/", "");
-			} else if (n.find("param/") == 0) { //backwards compatibility
-				pr = n.substr(6, n.length());
-			}
-		}
-
+		StringName pr = shader->remap_parameter(p_name);
 		if (pr) {
-			HashMap<StringName, Variant>::ConstIterator E = param_cache.find(pr);
-			if (E) {
-				r_ret = E->value;
-			} else {
-				r_ret = Variant();
-			}
+			r_ret = get_shader_parameter(pr);
 			return true;
 		}
 	}
@@ -238,6 +212,7 @@ void ShaderMaterial::_get_property_list(List<PropertyInfo> *p_list) const {
 						PropertyInfo info;
 						info.usage = PROPERTY_USAGE_GROUP;
 						info.name = last_group.capitalize();
+						info.hint_string = "shader_parameter/";
 
 						List<PropertyInfo> none_subgroup;
 						none_subgroup.push_back(info);
@@ -252,6 +227,7 @@ void ShaderMaterial::_get_property_list(List<PropertyInfo> *p_list) const {
 						PropertyInfo info;
 						info.usage = PROPERTY_USAGE_SUBGROUP;
 						info.name = last_subgroup.capitalize();
+						info.hint_string = "shader_parameter/";
 
 						List<PropertyInfo> subgroup;
 						subgroup.push_back(info);
@@ -271,12 +247,13 @@ void ShaderMaterial::_get_property_list(List<PropertyInfo> *p_list) const {
 
 				PropertyInfo info;
 				info.usage = PROPERTY_USAGE_GROUP;
-				info.name = "Shader Param";
+				info.name = "Shader Parameters";
+				info.hint_string = "shader_parameter/";
 				groups["<None>"]["<None>"].push_back(info);
 			}
 
 			PropertyInfo info = E->get();
-			info.name = info.name;
+			info.name = "shader_parameter/" + info.name;
 			groups[last_group][last_subgroup].push_back(info);
 		}
 
@@ -303,7 +280,7 @@ void ShaderMaterial::_get_property_list(List<PropertyInfo> *p_list) const {
 
 bool ShaderMaterial::_property_can_revert(const StringName &p_name) const {
 	if (shader.is_valid()) {
-		StringName pr = shader->remap_uniform(p_name);
+		StringName pr = shader->remap_parameter(p_name);
 		if (pr) {
 			Variant default_value = RenderingServer::get_singleton()->shader_get_parameter_default(shader->get_rid(), pr);
 			Variant current_value;
@@ -316,7 +293,7 @@ bool ShaderMaterial::_property_can_revert(const StringName &p_name) const {
 
 bool ShaderMaterial::_property_get_revert(const StringName &p_name, Variant &r_property) const {
 	if (shader.is_valid()) {
-		StringName pr = shader->remap_uniform(p_name);
+		StringName pr = shader->remap_parameter(p_name);
 		if (pr) {
 			r_property = RenderingServer::get_singleton()->shader_get_parameter_default(shader->get_rid(), pr);
 			return true;

+ 33 - 4
scene/resources/shader.h

@@ -87,15 +87,44 @@ public:
 
 	virtual bool is_text_shader() const;
 
-	_FORCE_INLINE_ StringName remap_uniform(const StringName &p_uniform) const {
+	// Finds the shader parameter name for the given property name, which should start with "shader_parameter/".
+	_FORCE_INLINE_ StringName remap_parameter(const StringName &p_property) const {
 		if (params_cache_dirty) {
 			get_shader_uniform_list(nullptr);
 		}
 
-		const HashMap<StringName, StringName>::Iterator E = params_cache.find(p_uniform);
-		if (E) {
-			return E->value;
+		String n = p_property;
+
+		// Backwards compatibility with old shader parameter names.
+		// Note: The if statements are important to make sure we are only replacing text exactly at index 0.
+		if (n.find("param/") == 0) {
+			n = n.replace_first("param/", "shader_parameter/");
+		}
+		if (n.find("shader_param/") == 0) {
+			n = n.replace_first("shader_param/", "shader_parameter/");
+		}
+		if (n.find("shader_uniform/") == 0) {
+			n = n.replace_first("shader_uniform/", "shader_parameter/");
+		}
+
+		{
+			// Additional backwards compatibility for projects between #62972 and #64092 (about a month of v4.0 development).
+			// These projects did not have any prefix for shader uniforms due to a bug.
+			// This code should be removed during beta or rc of 4.0.
+			const HashMap<StringName, StringName>::Iterator E = params_cache.find(n);
+			if (E) {
+				return E->value;
+			}
+		}
+
+		if (n.begins_with("shader_parameter/")) {
+			n = n.replace_first("shader_parameter/", "");
+			const HashMap<StringName, StringName>::Iterator E = params_cache.find(n);
+			if (E) {
+				return E->value;
+			}
 		}
+
 		return StringName();
 	}