This commit is contained in:
Hugo Locurcio 2021-11-09 11:45:12 -03:00 committed by GitHub
commit a92c34f389
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 6 deletions

View file

@ -157,6 +157,14 @@
Texture that specifies the per-pixel normal of the detail overlay.
[b]Note:[/b] Godot expects the normal map to use X+, Y-, and Z+ coordinates. See [url=http://wiki.polycount.com/wiki/Normal_Map_Technical_Details#Common_Swizzle_Coordinates]this page[/url] for a comparison of normal map coordinates expected by popular engines.
</member>
<member name="detail_offset" type="Vector3" setter="set_detail_offset" getter="get_detail_offset" default="Vector3(0, 0, 0)">
How much to offset the detail texture's coordinates (relative to the UV layer chosen in [member detail_uv_layer]. This can be used to adjust the detail texture's offset without having to switch to UV2 for detail mapping. This makes it possible to use lightmapping at the same as a differently offset detail texture. Only effective if [member detail_enabled] is [code]true[/code] and a detail texture is specified in [member detail_albedo] or [memebr detail_normal].
[b]Note:[/b] [member detail_offset]'s Z axis is only taken into account if the [member detail_uv_layer] specified has triplanar mapping enabled.
</member>
<member name="detail_scale" type="Vector3" setter="set_detail_scale" getter="get_detail_scale" default="Vector3(1, 1, 1)">
How much to scale the detail texture's coordinates (relative to the UV layer chosen in [member detail_uv_layer]. This can be used to adjust the detail texture's scale without having to switch to UV2 for detail mapping. This makes it possible to use lightmapping at the same as a differently scaled detail texture. Only effective if [member detail_enabled] is [code]true[/code] and a detail texture is specified in [member detail_albedo] or [memebr detail_normal].
[b]Note:[/b] [member detail_scale]'s Z axis is only taken into account if the [member detail_uv_layer] specified has triplanar mapping enabled.
</member>
<member name="detail_uv_layer" type="int" setter="set_detail_uv" getter="get_detail_uv" enum="BaseMaterial3D.DetailUV" default="0">
Specifies whether to use [code]UV[/code] or [code]UV2[/code] for the detail layer. See [enum DetailUV] for options.
</member>

View file

@ -337,6 +337,8 @@ void BaseMaterial3D::init_shaders() {
shader_names->backlight = "backlight";
shader_names->refraction = "refraction";
shader_names->point_size = "point_size";
shader_names->detail_scale = "detail_scale";
shader_names->detail_offset = "detail_offset";
shader_names->uv1_scale = "uv1_scale";
shader_names->uv1_offset = "uv1_offset";
shader_names->uv2_scale = "uv2_scale";
@ -707,6 +709,8 @@ void BaseMaterial3D::_update_shader() {
code += "uniform sampler2D texture_detail_albedo : hint_albedo," + texfilter_str + ";\n";
code += "uniform sampler2D texture_detail_normal : hint_normal," + texfilter_str + ";\n";
code += "uniform sampler2D texture_detail_mask : hint_white," + texfilter_str + ";\n";
code += "uniform vec3 detail_scale;\n";
code += "uniform vec3 detail_offset;\n";
}
if (features[FEATURE_SUBSURFACE_SCATTERING]) {
@ -1235,14 +1239,14 @@ void BaseMaterial3D::_update_shader() {
bool triplanar = (flags[FLAG_UV1_USE_TRIPLANAR] && detail_uv == DETAIL_UV_1) || (flags[FLAG_UV2_USE_TRIPLANAR] && detail_uv == DETAIL_UV_2);
if (triplanar) {
String tp_uv = detail_uv == DETAIL_UV_1 ? "uv1" : "uv2";
code += " vec4 detail_tex = triplanar_texture(texture_detail_albedo," + tp_uv + "_power_normal," + tp_uv + "_triplanar_pos);\n";
code += " vec4 detail_norm_tex = triplanar_texture(texture_detail_normal," + tp_uv + "_power_normal," + tp_uv + "_triplanar_pos);\n";
const String tp_uv = detail_uv == DETAIL_UV_1 ? "uv1" : "uv2";
code += " vec4 detail_tex = triplanar_texture(texture_detail_albedo," + tp_uv + "_power_normal," + tp_uv + "_triplanar_pos * detail_scale + detail_offset);\n";
code += " vec4 detail_norm_tex = triplanar_texture(texture_detail_normal," + tp_uv + "_power_normal," + tp_uv + "_triplanar_pos * detail_scale + detail_offset);\n";
} else {
String det_uv = detail_uv == DETAIL_UV_1 ? "base_uv" : "base_uv2";
code += " vec4 detail_tex = texture(texture_detail_albedo," + det_uv + ");\n";
code += " vec4 detail_norm_tex = texture(texture_detail_normal," + det_uv + ");\n";
const String det_uv = detail_uv == DETAIL_UV_1 ? "base_uv" : "base_uv2";
code += " vec4 detail_tex = texture(texture_detail_albedo," + det_uv + " * detail_scale.xy + detail_offset.xy);\n";
code += " vec4 detail_norm_tex = texture(texture_detail_normal," + det_uv + " * detail_scale.xy + detail_offset.xy);\n";
}
if (flags[FLAG_UV1_USE_TRIPLANAR]) {
@ -1489,6 +1493,24 @@ float BaseMaterial3D::get_refraction() const {
return refraction;
}
void BaseMaterial3D::set_detail_scale(const Vector3 &p_scale) {
detail_scale = p_scale;
RS::get_singleton()->material_set_param(_get_material(), shader_names->detail_scale, p_scale);
}
Vector3 BaseMaterial3D::get_detail_scale() const {
return detail_scale;
}
void BaseMaterial3D::set_detail_offset(const Vector3 &p_offset) {
detail_offset = p_offset;
RS::get_singleton()->material_set_param(_get_material(), shader_names->detail_offset, p_offset);
}
Vector3 BaseMaterial3D::get_detail_offset() const {
return detail_offset;
}
void BaseMaterial3D::set_detail_uv(DetailUV p_detail_uv) {
if (detail_uv == p_detail_uv) {
return;
@ -2296,6 +2318,12 @@ void BaseMaterial3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_point_size", "point_size"), &BaseMaterial3D::set_point_size);
ClassDB::bind_method(D_METHOD("get_point_size"), &BaseMaterial3D::get_point_size);
ClassDB::bind_method(D_METHOD("set_detail_scale", "detail_scale"), &BaseMaterial3D::set_detail_scale);
ClassDB::bind_method(D_METHOD("get_detail_scale"), &BaseMaterial3D::get_detail_scale);
ClassDB::bind_method(D_METHOD("set_detail_offset", "detail_offset"), &BaseMaterial3D::set_detail_offset);
ClassDB::bind_method(D_METHOD("get_detail_offset"), &BaseMaterial3D::get_detail_offset);
ClassDB::bind_method(D_METHOD("set_detail_uv", "detail_uv"), &BaseMaterial3D::set_detail_uv);
ClassDB::bind_method(D_METHOD("get_detail_uv"), &BaseMaterial3D::get_detail_uv);
@ -2535,6 +2563,8 @@ void BaseMaterial3D::_bind_methods() {
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "detail_enabled"), "set_feature", "get_feature", FEATURE_DETAIL);
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "detail_mask", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture", TEXTURE_DETAIL_MASK);
ADD_PROPERTY(PropertyInfo(Variant::INT, "detail_blend_mode", PROPERTY_HINT_ENUM, "Mix,Add,Subtract,Multiply"), "set_detail_blend_mode", "get_detail_blend_mode");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "detail_scale"), "set_detail_scale", "get_detail_scale");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "detail_offset"), "set_detail_offset", "get_detail_offset");
ADD_PROPERTY(PropertyInfo(Variant::INT, "detail_uv_layer", PROPERTY_HINT_ENUM, "UV1,UV2"), "set_detail_uv", "get_detail_uv");
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "detail_albedo", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture", TEXTURE_DETAIL_ALBEDO);
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "detail_normal", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture", TEXTURE_DETAIL_NORMAL);
@ -2759,6 +2789,8 @@ BaseMaterial3D::BaseMaterial3D(bool p_orm) :
set_distance_fade_max_distance(10);
set_ao_light_affect(0.0);
set_detail_scale(Vector3(1, 1, 1));
set_detail_offset(Vector3(0, 0, 0));
set_metallic_texture_channel(TEXTURE_CHANNEL_RED);
set_roughness_texture_channel(TEXTURE_CHANNEL_RED);

View file

@ -402,6 +402,8 @@ private:
StringName uv1_offset;
StringName uv2_scale;
StringName uv2_offset;
StringName detail_scale;
StringName detail_offset;
StringName particles_anim_h_frames;
StringName particles_anim_v_frames;
StringName particles_anim_loop;
@ -490,6 +492,8 @@ private:
float uv2_triplanar_sharpness;
DetailUV detail_uv = DETAIL_UV_1;
Vector3 detail_scale;
Vector3 detail_offset;
bool deep_parallax = false;
int deep_parallax_min_layers;
@ -630,6 +634,12 @@ public:
void set_shading_mode(ShadingMode p_shading_mode);
ShadingMode get_shading_mode() const;
void set_detail_scale(const Vector3 &p_scale);
Vector3 get_detail_scale() const;
void set_detail_offset(const Vector3 &p_offset);
Vector3 get_detail_offset() const;
void set_detail_uv(DetailUV p_detail_uv);
DetailUV get_detail_uv() const;