Skip to content

Commit

Permalink
removed duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
mnikelsky committed Jun 22, 2023
1 parent 1da4552 commit f268b24
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 47 deletions.
18 changes: 3 additions & 15 deletions libraries/stdlib/genglsl/mx_normalmap.glsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
void mx_normalmap(vec3 value, int map_space, float normal_scale, vec3 N, vec3 T, out vec3 result)
void mx_normalmap_vector2(vec3 value, int map_space, vec2 normal_scale, vec3 N, vec3 T, out vec3 result)
{
// Decode the normal map.
value = (value == vec3(0.0f)) ? vec3(0.0, 0.0, 1.0) : value * 2.0 - 1.0;
Expand All @@ -15,19 +15,7 @@ void mx_normalmap(vec3 value, int map_space, float normal_scale, vec3 N, vec3 T,
result = normalize(value);
}

void mx_normalmap_vector2(vec3 value, int map_space, vec2 normal_scale, vec3 N, vec3 T, out vec3 result)
void mx_normalmap(vec3 value, int map_space, float normal_scale, vec3 N, vec3 T, out vec3 result)
{
// Decode the normal map.
value = (value == vec3(0.0f)) ? vec3(0.0, 0.0, 1.0) : value * 2.0 - 1.0;

// Transform from tangent space if needed.
if (map_space == 0)
{
vec3 B = normalize(cross(N, T));
value.xy *= normal_scale;
value = T * value.x + B * value.y + N * value.z;
}

// Normalize the result.
result = normalize(value);
mx_normalmap_vector2(value, map_space, vec2(normal_scale), N, T, result);
}
18 changes: 3 additions & 15 deletions libraries/stdlib/genmsl/mx_normalmap.metal
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
void mx_normalmap(vec3 value, int map_space, float normal_scale, vec3 N, vec3 T, out vec3 result)
void mx_normalmap_vector2(vec3 value, int map_space, vec2 normal_scale, vec3 N, vec3 T, out vec3 result)
{
// Decode the normal map.
value = all(value == vec3(0.0f)) ? vec3(0.0, 0.0, 1.0) : value * 2.0 - 1.0;
Expand All @@ -15,19 +15,7 @@ void mx_normalmap(vec3 value, int map_space, float normal_scale, vec3 N, vec3 T,
result = normalize(value);
}

void mx_normalmap_vector2(vec3 value, int map_space, vec2 normal_scale, vec3 N, vec3 T, out vec3 result)
void mx_normalmap(vec3 value, int map_space, float normal_scale, vec3 N, vec3 T, out vec3 result)
{
// Decode the normal map.
value = all(value == vec3(0.0f)) ? vec3(0.0, 0.0, 1.0) : value * 2.0 - 1.0;

// Transform from tangent space if needed.
if (map_space == 0)
{
vec3 B = normalize(cross(N, T));
value.xy *= normal_scale;
value = T * value.x + B * value.y + N * value.z;
}

// Normalize the result.
result = normalize(value);
mx_normalmap_vector2(value, map_space, vec2(normal_scale), N, T, result);
}
21 changes: 4 additions & 17 deletions libraries/stdlib/genosl/mx_normalmap.osl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
void mx_normalmap(vector value, string map_space, float normal_scale, vector N, vector U, output vector result)
void mx_normalmap_vector2(vector value, string map_space, vector2 normal_scale, vector N, vector U, output vector result)
{
// Tangent space
if (map_space == "tangent")
{
vector v = value * 2.0 - 1.0;
vector T = normalize(U - dot(U, N) * N);
vector B = normalize(cross(N, T));
result = normalize(T * v[0] * normal_scale + B * v[1] * normal_scale + N * v[2]);
result = normalize(T * v[0] * normal_scale[0] + B * v[1] * normal_scale[1] + N * v[2]);
}
// Object space
else
Expand All @@ -16,20 +16,7 @@ void mx_normalmap(vector value, string map_space, float normal_scale, vector N,
}
}

void mx_normalmap_vector2(vector value, string map_space, vector2 normal_scale, vector N, vector U, output vector result)
void mx_normalmap(vector value, string map_space, float normal_scale, vector N, vector U, output vector result)
{
// Tangent space
if (map_space == "tangent")
{
vector v = value * 2.0 - 1.0;
vector T = normalize(U - dot(U, N) * N);
vector B = normalize(cross(N, T));
result = normalize(T * v[0] * normal_scale[0] + B * v[1] * normal_scale[1] + N * v[2]);
}
// Object space
else
{
vector n = value * 2.0 - 1.0;
result = normalize(n);
}
mx_normalmap_vector2(value, map_space, vector2(normal_scale, normal_scale), N, U, result);
}

0 comments on commit f268b24

Please sign in to comment.