Skip to content

Commit

Permalink
improved shader
Browse files Browse the repository at this point in the history
  • Loading branch information
4sval committed Aug 30, 2022
1 parent 7b9e53c commit b947ac3
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 78 deletions.
2 changes: 1 addition & 1 deletion CUE4Parse
79 changes: 50 additions & 29 deletions FModel/Resources/shader.frag
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ in vec2 fTexCoords;
struct Material {
sampler2D diffuseMap;
sampler2D normalMap;
sampler2D specularMap;
sampler2D metallicMap;
sampler2D emissionMap;

bool swap;
bool useSpecularMap;
sampler2D specularMap;

bool hasDiffuseColor;
vec4 diffuseColor;

vec4 emissionColor;

float shininess;
};

Expand All @@ -31,36 +33,55 @@ uniform vec3 viewPos;

out vec4 FragColor;

vec3 getNormalFromMap()
{
vec3 tangentNormal = texture(material.normalMap, fTexCoords).xyz * 2.0 - 1.0;

vec3 q1 = dFdx(fPos);
vec3 q2 = dFdy(fPos);
vec2 st1 = dFdx(fTexCoords);
vec2 st2 = dFdy(fTexCoords);

vec3 n = normalize(fNormal);
vec3 t = normalize(q1*st2.t - q2*st1.t);
vec3 b = -normalize(cross(n, t));
mat3 tbn = mat3(t, b, n);

return normalize(tbn * tangentNormal);
}

void main()
{
if (material.swap)
if (material.hasDiffuseColor)
{
FragColor = material.diffuseColor;
return;
}
else
{
vec3 n_normal_map = getNormalFromMap();
vec3 n_light_direction = normalize(light.position - fPos);
vec3 result = light.ambient * vec3(texture(material.diffuseMap, fTexCoords));

// diffuse
float diff = max(dot(n_normal_map, n_light_direction), 0.0f);
vec4 diffuse_map = texture(material.diffuseMap, fTexCoords);
if(diffuse_map.a < 0.1f) discard;
result += light.diffuse * diff * diffuse_map.rgb;

// ambient
vec3 ambient = light.ambient * vec3(texture(material.diffuseMap, fTexCoords));

// diffuse
vec3 norm = texture(material.normalMap, fTexCoords).rgb;
norm = normalize(norm * 2.0 - 1.0);
vec3 lightDir = normalize(light.position - fPos);
float diff = max(dot(norm, lightDir), 0.0f);
vec3 diffuseMap = vec3(texture(material.diffuseMap, fTexCoords));
vec3 diffuse = light.diffuse * diff * diffuseMap;

// specular
vec3 viewDir = normalize(viewPos - fPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0f), material.shininess);
vec3 specularMap = vec3(texture(material.specularMap, fTexCoords));
vec3 specular = light.specular * spec * specularMap;

// emission
vec3 emissionMap = vec3(texture(material.emissionMap, fTexCoords));
vec3 emission = material.emissionColor.rgb * emissionMap;

vec3 result = ambient + diffuse + specular + emission;
FragColor = vec4(result, 1.0);
// specular
if (material.useSpecularMap)
{
vec3 n_view_direction = normalize(viewPos - fPos);
vec3 reflect_direction = reflect(-n_light_direction, n_normal_map);
float spec = pow(max(dot(n_view_direction, reflect_direction), 0.0f), material.shininess);
vec3 specular_map = vec3(texture(material.specularMap, fTexCoords));
result += light.specular * spec * specular_map.b;
}

// emission
vec3 emission_map = vec3(texture(material.emissionMap, fTexCoords));
result += material.emissionColor.rgb * emission_map;

FragColor = vec4(result, 1.0);
}
}
3 changes: 2 additions & 1 deletion FModel/Views/Snooper/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ public void Bind(Camera camera)

ImGui.BeginTable("Sections", 2, ImGuiTableFlags.RowBg);
ImGui.TableSetupColumn("Index", ImGuiTableColumnFlags.WidthFixed);
ImGui.TableSetupColumn("Material", ImGuiTableColumnFlags.WidthStretch);
ImGui.TableSetupColumn("Material", ImGuiTableColumnFlags.WidthFixed);
ImGui.TableHeadersRow();
for (int section = 0; section < Sections.Length; section++)
{
Sections[section].Bind(camera, Indices.Length);
// if (!Sections[section].Show) continue;
_gl.DrawArrays(PrimitiveType.Triangles, Sections[section].FirstFaceIndex, Sections[section].FacesCount);
}
ImGui.EndTable();
Expand Down
Loading

0 comments on commit b947ac3

Please sign in to comment.