1- // FRAGMENT SHADER
1+ // FRAGMENT SHADER
22uniform sampler2D diffuseTexture;
33uniform sampler2D normalTexture; // New uniform for the normal map
4- uniform sampler2D bumpTexture; // New uniform for the bump map
4+ uniform sampler2D bumpTexture; // New uniform for the bump map
55
66/* Variables shared from a vertex shader to a fragment shader. */
7- varying vec2 vertexUV;
8- varying vec3 vertexNormal;
7+ in vec2 vertexUV;
8+ in vec3 vertexNormal;
99
10- /* The `gl_FragColor` has been deprecated in 450. So now we switch to an output
11- variable called `diffuseColor`. However this approach does not work with ThreeJS
12- by default. */
13-
14- // layout(location = 0) out vec4 diffuseColor;
10+ /* The `gl_FragColor` has been deprecated in GLSL 300 ES.
11+ We now use an output variable, typically named `FragColor` or similar.
12+ Three.js will automatically link this `out` variable to the framebuffer.
13+ */
14+ out vec4 FragColor; // Declare an output variable for the fragment color
1515
16- /*
17- - gl_FragColor: Reserved global built-in fragment variable that controls the color. It requires a vec4 type.
16+ /*
1817 - vec4: Reserved type that means a vector of four simple precision float numbers.
19- - texture2D : Retrieves texels from a texture.
18+ - texture : Retrieves texels from a texture (replaces texture2D in GLSL3) .
2019*/
2120void main() {
2221 /* Sample the normal map. Normal maps typically store normals in tangent space. */
23- vec3 normalMapColor = texture2D (normalTexture, vertexUV).rgb;
22+ vec3 normalMapColor = texture (normalTexture, vertexUV).rgb;
2423 // Convert normal from [0, 1] range to [-1, 1] range.
2524 vec3 normalFromMap = normalize (normalMapColor * 2.0 - 1.0 );
2625
2726 /* Sample the bump map (grayscale image, typically). */
28- float bumpValue = texture2D (bumpTexture, vertexUV).r; // Assuming grayscale, take red channel.
27+ float bumpValue = texture (bumpTexture, vertexUV).r; // Assuming grayscale, take red channel.
2928
3029 /* Apply bump mapping (simple perturbation of normal, or displacement).
31- For a true bump effect, you'd usually perturb the vertexNormal based on the
32- bump value and its derivatives (which is more complex). A simpler approach
33- for visual effect might be to just scale the normal based on bump. However,
34- for proper bump mapping, you typically need to compute derivatives or use a
35- more sophisticated approach. For this example, let's focus on combining the
36- normal map with the base normal.
30+ For a true bump effect, you'd usually perturb the vertexNormal based on the
31+ bump value and its derivatives (which is more complex). A simpler approach
32+ for visual effect might be to just scale the normal based on bump. However,
33+ for proper bump mapping, you typically need to compute derivatives or use a
34+ more sophisticated approach. For this example, let's focus on combining the
35+ normal map with the base normal.
3736 */
38-
37+
3938 // For proper bump mapping, you'd typically calculate a modified normal based on bump.
4039 // This often involves calculating tangents and bitangents, and then perturbing the normal.
4140 // For a visual bump effect without true displacement, you can adjust the normal directly.
@@ -47,7 +46,7 @@ void main() {
4746 // A higher bumpValue (lighter areas in the bump map) will make the normalMap influence stronger,
4847 // and a lower bumpValue (darker areas) will make it weaker.
4948 // You can also add a small offset directly to the normal.
50-
49+
5150 // Method 1: Scale normal map influence (subtle)
5251 // vec3 finalNormal = normalize(vertexNormal + normalFromMap * (0.8 + bumpValue * 0.2)); // Adjust factors as needed
5352
@@ -57,7 +56,7 @@ void main() {
5756 // If it's negative (or darker areas), we pull it inwards.
5857 // For a simple grayscale bump map where 0.5 is flat, values above 0.5 are raised, below 0.5 are lowered.
5958 float bumpFactor = (bumpValue - 0.5 ) * 0.5 ; // Remap [0,1] to [-0.25, 0.25] for perturbation strength
60-
59+
6160 // Combine vertexNormal and normalFromMap, then add bump influence
6261 vec3 intermediateNormal = normalize (vertexNormal + normalFromMap * 0.8 ); // Combine base and normal map
6362 vec3 finalNormal = normalize (intermediateNormal + intermediateNormal * bumpFactor);
@@ -71,12 +70,12 @@ void main() {
7170 // vec3 colorInnerGlow = vec3(226.0 / 255.0, 123.0 / 255.0, 88.0 / 255.0) * pow(intensity, 0.9); // Terra Cotta
7271
7372 /* Base texture map as vec4. */
74- vec4 textureUV = texture2D (diffuseTexture, vertexUV);
73+ vec4 textureUV = texture (diffuseTexture, vertexUV); // Use 'texture'
7574 vec4 onlyTextureUV = vec4 (textureUV.xyz, 1.0 );
7675 vec4 combinedUV = vec4 (colorInnerGlow + textureUV.xyz, 1.0 );
7776
7877 /* Combine the base texture with the inner glow effect. */
79- // gl_FragColor = vec4(colorInnerGlow, 1.0);
80- // gl_FragColor = onlyTextureUV;
81- gl_FragColor = combinedUV;
78+ // FragColor = vec4(colorInnerGlow, 1.0);
79+ // FragColor = onlyTextureUV;
80+ FragColor = combinedUV; // Assign to the output variable
8281}
0 commit comments