From d26100734623f37063206b9b144c2a29fd71d414 Mon Sep 17 00:00:00 2001 From: quou Date: Wed, 1 Jan 2025 18:43:31 +1100 Subject: material system --- intermediate/surface.glsl | 83 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 75 insertions(+), 8 deletions(-) (limited to 'intermediate/surface.glsl') diff --git a/intermediate/surface.glsl b/intermediate/surface.glsl index 18c13a3..7988502 100644 --- a/intermediate/surface.glsl +++ b/intermediate/surface.glsl @@ -14,6 +14,9 @@ type: vec3 name: normal type: vec3 [attribute] +name: tangent +type: vec3 +[attribute] name: uv type: vec2 @@ -21,8 +24,14 @@ type: vec2 name: uv type: vec2 [interpolator] +name: position +type: vec4 +[interpolator] name: normal type: vec3 +[interpolator] +name: tbn +type: mat3 [struct] name: MVP @@ -33,11 +42,52 @@ type: mat4 name: view_projection type: mat4 +[struct] +name: Material +[variable] +name: albedo +type: vec3 +[variable] +name: metalness +type: float +[variable] +name: roughness +type: float +[variable] +name: ao +type: float + [cbuffer] name: c_mvp type: MVP stage: vertex +[cbuffer] +name: material +type: Material +stage: fragment + +[texture] +name: albedo +stage: fragment +dimension: 2 +[texture] +name: ao +stage: fragment +dimension: 2 +[texture] +name: metal +stage: fragment +dimension: 2 +[texture] +name: rough +stage: fragment +dimension: 2 +[texture] +name: normal +stage: fragment +dimension: 2 + [target] name: colour type: vec4 @@ -47,12 +97,16 @@ type: vec4 #ifdef VERTEX_SHADER void main() { + vec4 pos = c_mvp.model * vec4(position, 1.0); + vec3 t = normalize((c_mvp.model * vec4(tangent, 0.0)).xyz); + vec3 b = normalize((c_mvp.model * vec4(normal, 0.0)).xyz); + vec3 n = normalize((c_mvp.model * vec4(cross(normal, tangent), 0.0)).xyz); + interpolator.normal = n; + interpolator.tbn = mat3(t, b, n); interpolator.uv = uv; - interpolator.normal = mat3(c_mvp.model) * normal; - gl_Position = - c_mvp.view_projection * - c_mvp.model * - vec4(position, 1.0); + interpolator.position = pos; + interpolator.tbn = mat3(t, b, n); + gl_Position = c_mvp.view_projection * pos; } #endif @@ -60,9 +114,22 @@ void main() { #ifdef FRAGMENT_SHADER void main() { - vec3 normal = normalize(interpolator.normal); - float light = max(dot(normal, vec3(0, 0, 1)), 0.0); - colour = vec4(light.xxx, 1.0); + vec2 uv = interpolator.uv; + + vec3 nrmsample = texture(normal, uv).rgb; + + vec2 nrmxy = nrmsample.xy * 2.0 - 1.0; + vec3 nrm = normalize(vec3(nrmxy, 1.0)); + if (nrmsample.b == 1.0) /* default texture */ + nrm = vec3(0, 0, 1); + nrm = normalize(interpolator.tbn * nrm); + + float light = max(dot(nrm, vec3(0, 0, 1)), 0.0); + + vec3 ambient = 0.05.xxx * texture(ao, uv).r; + vec3 col = light * material.albedo; + col *= texture(albedo, uv).rgb; + colour = vec4(col, 1.0); } #endif -- cgit v1.2.3-54-g00ecf