Skip to content

Commit 34e1471

Browse files
committed
refactor: use GLSL 3.0 for shaders
1 parent 877cf57 commit 34e1471

6 files changed

Lines changed: 43 additions & 35 deletions

File tree

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
/* Variables shared from a vertex shader to a fragment shader. */
22
varying vec3 vertexNormal;
33

4+
/* Declare an output variable for the fragment color.
5+
This replaces the deprecated 'gl_FragColor' in GLSL ES 3.0.
6+
*/
7+
out vec4 FragColor;
8+
49
void main() {
510
float intensity = pow(0.6 - dot(vertexNormal, vec3(0.0, 0.0, 1.0)), 2.2);
611

712
/* Create a custom variable to configure our atmosphere color. Remember to
813
divide by 255 as the rgb values must be normalized. */
914
vec3 colorInnerGlow = vec3(255.0, 157.0, 111.0) / 255.0; // Atomic Tangerine
1015

11-
gl_FragColor = vec4(colorInnerGlow, 1.0) * intensity;
16+
FragColor = vec4(colorInnerGlow, 1.0) * intensity;
1217
}

src/app/assets/shaders/atmosphere/atmosphereVertex.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Variables to pass to the fragment shader.
2-
varying vec3 vertexNormal;
2+
out vec3 vertexNormal;
33

44
void main() {
55
vertexNormal = normalize(normalMatrix * normal);
Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,40 @@
1-
//FRAGMENT SHADER
1+
// FRAGMENT SHADER
22
uniform sampler2D diffuseTexture;
33
uniform 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
*/
2120
void 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
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// Variables to pass to the fragment shader.
2-
varying vec2 vertexUV;
3-
varying vec3 vertexNormal;
2+
out vec2 vertexUV;
3+
out vec3 vertexNormal;
44

5-
/*
5+
/*
66
- position, uv and normal are default vertex attributes provided by
7-
BufferGeometry from threejs.
8-
7+
BufferGeometry from threejs. In GLSL3, 'attribute' is replaced by 'in'.
98
- projectionMatrix, normalMatrix and modelViewMatrix are built-in uniforms
109
provided by threejs.
1110
*/
11+
1212
void main() {
1313
vertexUV = uv;
1414
vertexNormal = normalize(normalMatrix * normal);
1515
/* Respect the coordinates given by converting from "world space" to "screen
1616
space". */
17-
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1);
17+
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
1818
}

src/app/components/mars-scene/mars-atmosphere-mesh.component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import atmosphereVertexShader from '../../assets/shaders/atmosphere/atmosphereVe
1212
import {
1313
AdditiveBlending,
1414
BackSide,
15+
GLSL3,
1516
Mesh,
1617
ShaderMaterial,
1718
ShaderMaterialParameters,
@@ -49,6 +50,7 @@ export class MarsAtmosphereMeshComponent {
4950

5051
/** */
5152
protected atmosphereShaderParameters: ShaderMaterialParameters = {
53+
glslVersion: GLSL3,
5254
vertexShader: atmosphereVertexShader,
5355
fragmentShader: atmosphereFragmentShader,
5456
side: BackSide,

src/app/components/mars-scene/mars-mesh.component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
import { textureResource } from 'angular-three-soba/loaders';
1616
import {
1717
DoubleSide,
18+
GLSL3,
1819
Mesh,
1920
MeshStandardMaterial,
2021
MeshStandardMaterialParameters,
@@ -133,6 +134,7 @@ export class MarsMeshComponent {
133134

134135
/** */
135136
protected marsShaderParameters: ShaderMaterialParameters = {
137+
glslVersion: GLSL3,
136138
vertexShader: marsVertexShader,
137139
fragmentShader: marsFragmentShader,
138140
side: DoubleSide,

0 commit comments

Comments
 (0)