normal.glsl
1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
@export ecgl.normal.vertex
@import ecgl.common.transformUniforms
@import ecgl.common.uv.header
@import ecgl.common.attributes
varying vec3 v_Normal;
varying vec3 v_WorldPosition;
@import ecgl.common.normalMap.vertexHeader
@import ecgl.common.vertexAnimation.header
void main()
{
// TODO Animation
@import ecgl.common.vertexAnimation.main
@import ecgl.common.uv.main
v_Normal = normalize((worldInverseTranspose * vec4(normal, 0.0)).xyz);
v_WorldPosition = (world * vec4(pos, 1.0)).xyz;
@import ecgl.common.normalMap.vertexMain
gl_Position = worldViewProjection * vec4(pos, 1.0);
}
@end
@export ecgl.normal.fragment
#define ROUGHNESS_CHANEL 0
uniform bool useBumpMap;
uniform bool useRoughnessMap;
uniform bool doubleSide;
uniform float roughness;
@import ecgl.common.uv.fragmentHeader
varying vec3 v_Normal;
varying vec3 v_WorldPosition;
uniform mat4 viewInverse : VIEWINVERSE;
@import ecgl.common.normalMap.fragmentHeader
@import ecgl.common.bumpMap.header
uniform sampler2D roughnessMap;
void main()
{
vec3 N = v_Normal;
bool flipNormal = false;
if (doubleSide) {
vec3 eyePos = viewInverse[3].xyz;
vec3 V = normalize(eyePos - v_WorldPosition);
if (dot(N, V) < 0.0) {
flipNormal = true;
}
}
@import ecgl.common.normalMap.fragmentMain
if (useBumpMap) {
N = bumpNormal(v_WorldPosition, v_Normal, N);
}
float g = 1.0 - roughness;
if (useRoughnessMap) {
float g2 = 1.0 - texture2D(roughnessMap, v_DetailTexcoord)[ROUGHNESS_CHANEL];
// Adjust the brightness
g = clamp(g2 + (g - 0.5) * 2.0, 0.0, 1.0);
}
if (flipNormal) {
N = -N;
}
gl_FragColor.rgb = (N.xyz + 1.0) * 0.5;
gl_FragColor.a = g;
}
@end