aboutsummaryrefslogtreecommitdiff
path: root/shaders
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-11-01 18:23:08 -0700
committer3gg <3gg@shellblade.net>2025-11-01 18:23:08 -0700
commit42b5f1997cdd5e99645e24dca6cb89cc7b081a09 (patch)
tree2a2608c892fdedd1a728e1163b01988e8264e635 /shaders
parentf494baf976c4494dd0ea4e755907cf49b026eb5d (diff)
Add support for alpha mode
Diffstat (limited to 'shaders')
-rw-r--r--shaders/cook_torrance.frag89
-rw-r--r--shaders/cook_torrance.vert24
2 files changed, 62 insertions, 51 deletions
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;
10uniform float RoughnessFactor; 10uniform float RoughnessFactor;
11uniform vec3 EmissiveFactor; 11uniform vec3 EmissiveFactor;
12 12
13#ifdef HAS_ALBEDO_MAP 13#if HAS_TRANSPARENCY
14#define ALPHA_MODE_MASK 1
15#define ALPHA_MODE_BLEND 2
16uniform int AlphaMode;
17uniform float AlphaCutoff;
18#endif
19
20#if HAS_ALBEDO_MAP
14uniform sampler2D BaseColorTexture; 21uniform sampler2D BaseColorTexture;
15#endif 22#endif
16#ifdef HAS_METALLIC_ROUGHNESS_MAP 23#if HAS_METALLIC_ROUGHNESS_MAP
17uniform sampler2D MetallicRoughnessTexture; 24uniform sampler2D MetallicRoughnessTexture;
18#endif 25#endif
19#ifdef HAS_EMISSIVE_MAP 26#if HAS_EMISSIVE_MAP
20uniform sampler2D EmissiveTexture; 27uniform sampler2D EmissiveTexture;
21#endif 28#endif
22#ifdef HAS_OCCLUSION_MAP 29#if HAS_OCCLUSION_MAP
23uniform sampler2D AmbientOcclusionTexture; 30uniform sampler2D AmbientOcclusionTexture;
24#endif 31#endif
25#ifdef HAS_NORMAL_MAP 32#if HAS_NORMAL_MAP
26uniform sampler2D NormalMap; 33uniform sampler2D NormalMap;
27#endif 34#endif
28 35
@@ -37,13 +44,13 @@ uniform vec3 CameraPosition; // World space.
37 44
38// World-space position, normal and tangent. 45// World-space position, normal and tangent.
39in vec3 Position; 46in vec3 Position;
40#ifdef HAS_NORMALS 47#if HAS_NORMALS
41in vec3 Normal; 48in vec3 Normal;
42#endif 49#endif
43#ifdef HAS_TANGENTS 50#if HAS_TANGENTS
44in vec4 Tangent; 51in vec4 Tangent;
45#endif 52#endif
46#ifdef HAS_TEXCOORDS 53#if HAS_TEXCOORDS
47in vec2 Texcoord; 54in vec2 Texcoord;
48#endif 55#endif
49 56
@@ -61,7 +68,7 @@ layout (location = 0) out vec4 Colour;
61#if defined(HAS_NORMAL_MAP) && (defined(HAS_TANGENTS) || defined(HAS_TEXCOORDS)) 68#if defined(HAS_NORMAL_MAP) && (defined(HAS_TANGENTS) || defined(HAS_TEXCOORDS))
62vec3 get_ws_normal(vec3 normalWs, vec3 normalMapSample) { 69vec3 get_ws_normal(vec3 normalWs, vec3 normalMapSample) {
63 vec3 N = normalize(Normal); 70 vec3 N = normalize(Normal);
64#ifdef HAS_TANGENTS 71#if HAS_TANGENTS
65 //vec3 T = normalize(tangent.xyz - dot(tangent.xyz, N) * N); 72 //vec3 T = normalize(tangent.xyz - dot(tangent.xyz, N) * N);
66 vec3 T = Tangent.xyz; 73 vec3 T = Tangent.xyz;
67 vec3 B = Tangent.w * cross(N, T); 74 vec3 B = Tangent.w * cross(N, T);
@@ -159,36 +166,19 @@ vec3 cook_torrance_IBL(
159 166
160void main() 167void main()
161{ 168{
162 // TODO: Also use the specular F0 map from the model, and emissive. Make sure
163 // to use all maps.
164 // https://sketchfab.com/models/b81008d513954189a063ff901f7abfe4
165#ifdef HAS_NORMAL_MAP
166 vec3 normalMapSample = texture(NormalMap, Texcoord).xyz * 2.0 - 1.0;
167 vec3 N = get_ws_normal(Normal, normalMapSample);
168#elif HAS_NORMALS
169 vec3 N = normalize(Normal);
170#endif
171 vec3 V = normalize(CameraPosition - Position);
172 vec3 R = reflect(-V, N);
173 // Not needed for IBL.
174 //vec3 L = N;
175 //vec3 H = normalize(L + V);
176
177 float NdotV = max(0.0, dot(N, V));
178 // Not needed for IBL.
179 //float NdotL = max(0.0, dot(N,L));
180 //float NdotH = max(0.0, dot(N,H));
181 //float HdotV = clamp(dot(H,V), 0.0, 1.0); // Clamp to prevent black spots.
182
183 // TODO: BaseColorFactor and BaseColorTexture are vec4/rgba quantities
184 // respectively. Handle the alpha channel.
185 // TODO: Other factors. 169 // TODO: Other factors.
186#ifdef HAS_ALBEDO_MAP 170#if HAS_ALBEDO_MAP
187 vec3 albedo = vec3(BaseColorFactor) * texture(BaseColorTexture, Texcoord).rgb; 171 vec4 base_colour = vec4(BaseColorFactor) * texture(BaseColorTexture, Texcoord);
188#else 172#else
189 vec3 albedo = vec3(BaseColorFactor); 173 vec4 base_colour = vec4(BaseColorFactor);
190#endif 174#endif
191#ifdef HAS_METALLIC_ROUGHNESS_MAP 175 vec3 albedo = base_colour.rgb;
176#if HAS_TRANSPARENCY
177 if ((AlphaMode == ALPHA_MODE_MASK) && (base_colour.a < AlphaCutoff)) {
178 discard;
179 }
180#endif
181#if HAS_METALLIC_ROUGHNESS_MAP
192 // Spec: "Its green channel contains roughness values and its blue channel 182 // Spec: "Its green channel contains roughness values and its blue channel
193 // contains metalness values." 183 // contains metalness values."
194 vec2 metal_roughness 184 vec2 metal_roughness
@@ -197,12 +187,12 @@ void main()
197#else 187#else
198 vec2 metal_roughness = vec2(MetallicFactor, RoughnessFactor); 188 vec2 metal_roughness = vec2(MetallicFactor, RoughnessFactor);
199#endif 189#endif
200#ifdef HAS_EMISSIVE_MAP 190#if HAS_EMISSIVE_MAP
201 vec3 emissive = EmissiveFactor * texture(EmissiveTexture, Texcoord).rgb; 191 vec3 emissive = EmissiveFactor * texture(EmissiveTexture, Texcoord).rgb;
202#else 192#else
203 vec3 emissive = EmissiveFactor; 193 vec3 emissive = EmissiveFactor;
204#endif 194#endif
205#ifdef HAS_OCCLUSION_MAP 195#if HAS_OCCLUSION_MAP
206 float occlusion = texture(AmbientOcclusionTexture, Texcoord).r; 196 float occlusion = texture(AmbientOcclusionTexture, Texcoord).r;
207#else 197#else
208 float occlusion = 1.0; 198 float occlusion = 1.0;
@@ -210,6 +200,27 @@ void main()
210 float metallic = metal_roughness.x; 200 float metallic = metal_roughness.x;
211 float roughness = metal_roughness.y; 201 float roughness = metal_roughness.y;
212 202
203 // TODO: Also use the specular F0 map from the model, and emissive. Make sure
204 // to use all maps.
205 // https://sketchfab.com/models/b81008d513954189a063ff901f7abfe4
206#if HAS_NORMAL_MAP
207 vec3 normalMapSample = texture(NormalMap, Texcoord).xyz * 2.0 - 1.0;
208 vec3 N = get_ws_normal(Normal, normalMapSample);
209#elif HAS_NORMALS
210 vec3 N = normalize(Normal);
211#endif
212 vec3 V = normalize(CameraPosition - Position);
213 vec3 R = reflect(-V, N);
214 // Not needed for IBL.
215 //vec3 L = N;
216 //vec3 H = normalize(L + V);
217
218 float NdotV = max(0.0, dot(N, V));
219 // Not needed for IBL.
220 //float NdotL = max(0.0, dot(N,L));
221 //float NdotH = max(0.0, dot(N,H));
222 //float HdotV = clamp(dot(H,V), 0.0, 1.0); // Clamp to prevent black spots.
223
213 // For a single light direction: 224 // For a single light direction:
214 // vec3 brdf = cook_torrance(albedo, metallic, roughness, NdotL, NdotV, NdotH, HdotV); 225 // vec3 brdf = cook_torrance(albedo, metallic, roughness, NdotL, NdotV, NdotH, HdotV);
215 // vec3 Li = texture(Sky, N).rgb; 226 // vec3 Li = texture(Sky, N).rgb;
@@ -268,5 +279,5 @@ void main()
268 // //colour = B * 0.5 + 0.5; 279 // //colour = B * 0.5 + 0.5;
269 // } 280 // }
270 281
271 Colour = vec4(colour, 1.0); 282 Colour = vec4(colour, base_colour.a);
272} 283}
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;
5uniform mat4 View; 5uniform mat4 View;
6uniform mat4 Projection; 6uniform mat4 Projection;
7//uniform mat4 MVP; 7//uniform mat4 MVP;
8#ifdef HAS_JOINTS 8#if HAS_JOINTS
9// The client should pass in an appropriate value for MAX_JOINTS. 9// The client should pass in an appropriate value for MAX_JOINTS.
10// #define MAX_JOINTS 96 10// #define MAX_JOINTS 96
11// 11//
@@ -21,35 +21,35 @@ uniform mat4 JointMatrices[MAX_JOINTS]; // Use 4x4 for now to keep it simple.
21#endif 21#endif
22 22
23layout (location = 0) in vec3 vPosition; 23layout (location = 0) in vec3 vPosition;
24#ifdef HAS_NORMALS 24#if HAS_NORMALS
25layout (location = 1) in vec3 vNormal; 25layout (location = 1) in vec3 vNormal;
26#endif 26#endif
27#ifdef HAS_TANGENTS 27#if HAS_TANGENTS
28layout (location = 2) in vec4 vTangent; 28layout (location = 2) in vec4 vTangent;
29#endif 29#endif
30#ifdef HAS_TEXCOORDS 30#if HAS_TEXCOORDS
31layout (location = 3) in vec2 vTexcoord; 31layout (location = 3) in vec2 vTexcoord;
32#endif 32#endif
33#ifdef HAS_JOINTS 33#if HAS_JOINTS
34layout (location = 4) in uvec4 vJoint; 34layout (location = 4) in uvec4 vJoint;
35layout (location = 5) in vec4 vWeight; 35layout (location = 5) in vec4 vWeight;
36#endif 36#endif
37 37
38// World-space position, normal and tangent. 38// World-space position, normal and tangent.
39out vec3 Position; 39out vec3 Position;
40#ifdef HAS_NORMALS 40#if HAS_NORMALS
41out vec3 Normal; 41out vec3 Normal;
42#endif 42#endif
43#ifdef HAS_TANGENTS 43#if HAS_TANGENTS
44out vec4 Tangent; 44out vec4 Tangent;
45#endif 45#endif
46#ifdef HAS_TEXCOORDS 46#if HAS_TEXCOORDS
47out vec2 Texcoord; 47out vec2 Texcoord;
48#endif 48#endif
49 49
50void main() 50void main()
51{ 51{
52#ifdef HAS_JOINTS 52#if HAS_JOINTS
53 mat4 skinMatrix = 53 mat4 skinMatrix =
54 vWeight.x * JointMatrices[vJoint.x] + 54 vWeight.x * JointMatrices[vJoint.x] +
55 vWeight.y * JointMatrices[vJoint.y] + 55 vWeight.y * JointMatrices[vJoint.y] +
@@ -59,14 +59,14 @@ void main()
59#else 59#else
60 Position = vec3(ModelMatrix * vec4(vPosition, 1.0)); 60 Position = vec3(ModelMatrix * vec4(vPosition, 1.0));
61#endif 61#endif
62#ifdef HAS_NORMALS 62#if HAS_NORMALS
63 Normal = mat3(ModelMatrix) * vNormal; 63 Normal = mat3(ModelMatrix) * vNormal;
64 //Normal = normalize(ModelMatrix * vec4(vNormal, 0.0)).xyz; 64 //Normal = normalize(ModelMatrix * vec4(vNormal, 0.0)).xyz;
65#endif 65#endif
66#ifdef HAS_TANGENTS 66#if HAS_TANGENTS
67 Tangent = vec4(mat3(ModelMatrix) * vTangent.xyz, vTangent.w); 67 Tangent = vec4(mat3(ModelMatrix) * vTangent.xyz, vTangent.w);
68#endif 68#endif
69#ifdef HAS_TEXCOORDS 69#if HAS_TEXCOORDS
70 Texcoord = vTexcoord; 70 Texcoord = vTexcoord;
71#endif 71#endif
72 gl_Position = Projection * View * vec4(Position, 1.0); 72 gl_Position = Projection * View * vec4(Position, 1.0);