From 42b5f1997cdd5e99645e24dca6cb89cc7b081a09 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 1 Nov 2025 18:23:08 -0700 Subject: Add support for alpha mode --- shaders/cook_torrance.frag | 89 ++++++++++++++++++++++++++-------------------- shaders/cook_torrance.vert | 24 ++++++------- 2 files changed, 62 insertions(+), 51 deletions(-) (limited to 'shaders') diff --git a/shaders/cook_torrance.frag b/shaders/cook_torrance.frag index 9ff5a8d..c0c666c 100644 --- a/shaders/cook_torrance.frag +++ b/shaders/cook_torrance.frag @@ -10,19 +10,26 @@ uniform float MetallicFactor; uniform float RoughnessFactor; uniform vec3 EmissiveFactor; -#ifdef HAS_ALBEDO_MAP +#if HAS_TRANSPARENCY +#define ALPHA_MODE_MASK 1 +#define ALPHA_MODE_BLEND 2 +uniform int AlphaMode; +uniform float AlphaCutoff; +#endif + +#if HAS_ALBEDO_MAP uniform sampler2D BaseColorTexture; #endif -#ifdef HAS_METALLIC_ROUGHNESS_MAP +#if HAS_METALLIC_ROUGHNESS_MAP uniform sampler2D MetallicRoughnessTexture; #endif -#ifdef HAS_EMISSIVE_MAP +#if HAS_EMISSIVE_MAP uniform sampler2D EmissiveTexture; #endif -#ifdef HAS_OCCLUSION_MAP +#if HAS_OCCLUSION_MAP uniform sampler2D AmbientOcclusionTexture; #endif -#ifdef HAS_NORMAL_MAP +#if HAS_NORMAL_MAP uniform sampler2D NormalMap; #endif @@ -37,13 +44,13 @@ uniform vec3 CameraPosition; // World space. // World-space position, normal and tangent. in vec3 Position; -#ifdef HAS_NORMALS +#if HAS_NORMALS in vec3 Normal; #endif -#ifdef HAS_TANGENTS +#if HAS_TANGENTS in vec4 Tangent; #endif -#ifdef HAS_TEXCOORDS +#if HAS_TEXCOORDS in vec2 Texcoord; #endif @@ -61,7 +68,7 @@ layout (location = 0) out vec4 Colour; #if defined(HAS_NORMAL_MAP) && (defined(HAS_TANGENTS) || defined(HAS_TEXCOORDS)) vec3 get_ws_normal(vec3 normalWs, vec3 normalMapSample) { vec3 N = normalize(Normal); -#ifdef HAS_TANGENTS +#if HAS_TANGENTS //vec3 T = normalize(tangent.xyz - dot(tangent.xyz, N) * N); vec3 T = Tangent.xyz; vec3 B = Tangent.w * cross(N, T); @@ -159,36 +166,19 @@ vec3 cook_torrance_IBL( void main() { - // TODO: Also use the specular F0 map from the model, and emissive. Make sure - // to use all maps. - // https://sketchfab.com/models/b81008d513954189a063ff901f7abfe4 -#ifdef HAS_NORMAL_MAP - vec3 normalMapSample = texture(NormalMap, Texcoord).xyz * 2.0 - 1.0; - vec3 N = get_ws_normal(Normal, normalMapSample); -#elif HAS_NORMALS - vec3 N = normalize(Normal); -#endif - vec3 V = normalize(CameraPosition - Position); - vec3 R = reflect(-V, N); - // Not needed for IBL. - //vec3 L = N; - //vec3 H = normalize(L + V); - - float NdotV = max(0.0, dot(N, V)); - // Not needed for IBL. - //float NdotL = max(0.0, dot(N,L)); - //float NdotH = max(0.0, dot(N,H)); - //float HdotV = clamp(dot(H,V), 0.0, 1.0); // Clamp to prevent black spots. - - // TODO: BaseColorFactor and BaseColorTexture are vec4/rgba quantities - // respectively. Handle the alpha channel. // TODO: Other factors. -#ifdef HAS_ALBEDO_MAP - vec3 albedo = vec3(BaseColorFactor) * texture(BaseColorTexture, Texcoord).rgb; +#if HAS_ALBEDO_MAP + vec4 base_colour = vec4(BaseColorFactor) * texture(BaseColorTexture, Texcoord); #else - vec3 albedo = vec3(BaseColorFactor); + vec4 base_colour = vec4(BaseColorFactor); #endif -#ifdef HAS_METALLIC_ROUGHNESS_MAP + vec3 albedo = base_colour.rgb; +#if HAS_TRANSPARENCY + if ((AlphaMode == ALPHA_MODE_MASK) && (base_colour.a < AlphaCutoff)) { + discard; + } +#endif +#if HAS_METALLIC_ROUGHNESS_MAP // Spec: "Its green channel contains roughness values and its blue channel // contains metalness values." vec2 metal_roughness @@ -197,12 +187,12 @@ void main() #else vec2 metal_roughness = vec2(MetallicFactor, RoughnessFactor); #endif -#ifdef HAS_EMISSIVE_MAP +#if HAS_EMISSIVE_MAP vec3 emissive = EmissiveFactor * texture(EmissiveTexture, Texcoord).rgb; #else vec3 emissive = EmissiveFactor; #endif -#ifdef HAS_OCCLUSION_MAP +#if HAS_OCCLUSION_MAP float occlusion = texture(AmbientOcclusionTexture, Texcoord).r; #else float occlusion = 1.0; @@ -210,6 +200,27 @@ void main() float metallic = metal_roughness.x; float roughness = metal_roughness.y; + // TODO: Also use the specular F0 map from the model, and emissive. Make sure + // to use all maps. + // https://sketchfab.com/models/b81008d513954189a063ff901f7abfe4 +#if HAS_NORMAL_MAP + vec3 normalMapSample = texture(NormalMap, Texcoord).xyz * 2.0 - 1.0; + vec3 N = get_ws_normal(Normal, normalMapSample); +#elif HAS_NORMALS + vec3 N = normalize(Normal); +#endif + vec3 V = normalize(CameraPosition - Position); + vec3 R = reflect(-V, N); + // Not needed for IBL. + //vec3 L = N; + //vec3 H = normalize(L + V); + + float NdotV = max(0.0, dot(N, V)); + // Not needed for IBL. + //float NdotL = max(0.0, dot(N,L)); + //float NdotH = max(0.0, dot(N,H)); + //float HdotV = clamp(dot(H,V), 0.0, 1.0); // Clamp to prevent black spots. + // For a single light direction: // vec3 brdf = cook_torrance(albedo, metallic, roughness, NdotL, NdotV, NdotH, HdotV); // vec3 Li = texture(Sky, N).rgb; @@ -268,5 +279,5 @@ void main() // //colour = B * 0.5 + 0.5; // } - Colour = vec4(colour, 1.0); + Colour = vec4(colour, base_colour.a); } diff --git a/shaders/cook_torrance.vert b/shaders/cook_torrance.vert index 5f126c0..17fe1f7 100644 --- a/shaders/cook_torrance.vert +++ b/shaders/cook_torrance.vert @@ -5,7 +5,7 @@ uniform mat4 ModelMatrix; uniform mat4 View; uniform mat4 Projection; //uniform mat4 MVP; -#ifdef HAS_JOINTS +#if HAS_JOINTS // The client should pass in an appropriate value for MAX_JOINTS. // #define MAX_JOINTS 96 // @@ -21,35 +21,35 @@ uniform mat4 JointMatrices[MAX_JOINTS]; // Use 4x4 for now to keep it simple. #endif layout (location = 0) in vec3 vPosition; -#ifdef HAS_NORMALS +#if HAS_NORMALS layout (location = 1) in vec3 vNormal; #endif -#ifdef HAS_TANGENTS +#if HAS_TANGENTS layout (location = 2) in vec4 vTangent; #endif -#ifdef HAS_TEXCOORDS +#if HAS_TEXCOORDS layout (location = 3) in vec2 vTexcoord; #endif -#ifdef HAS_JOINTS +#if HAS_JOINTS layout (location = 4) in uvec4 vJoint; layout (location = 5) in vec4 vWeight; #endif // World-space position, normal and tangent. out vec3 Position; -#ifdef HAS_NORMALS +#if HAS_NORMALS out vec3 Normal; #endif -#ifdef HAS_TANGENTS +#if HAS_TANGENTS out vec4 Tangent; #endif -#ifdef HAS_TEXCOORDS +#if HAS_TEXCOORDS out vec2 Texcoord; #endif void main() { -#ifdef HAS_JOINTS +#if HAS_JOINTS mat4 skinMatrix = vWeight.x * JointMatrices[vJoint.x] + vWeight.y * JointMatrices[vJoint.y] + @@ -59,14 +59,14 @@ void main() #else Position = vec3(ModelMatrix * vec4(vPosition, 1.0)); #endif -#ifdef HAS_NORMALS +#if HAS_NORMALS Normal = mat3(ModelMatrix) * vNormal; //Normal = normalize(ModelMatrix * vec4(vNormal, 0.0)).xyz; #endif -#ifdef HAS_TANGENTS +#if HAS_TANGENTS Tangent = vec4(mat3(ModelMatrix) * vTangent.xyz, vTangent.w); #endif -#ifdef HAS_TEXCOORDS +#if HAS_TEXCOORDS Texcoord = vTexcoord; #endif gl_Position = Projection * View * vec4(Position, 1.0); -- cgit v1.2.3