-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
240 lines (195 loc) · 5.71 KB
/
index.html
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>WebGL PBR TP</title>
<link rel="stylesheet" href="./webgl.css" type="text/css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/gl-matrix/2.8.1/gl-matrix-min.js"
integrity="sha512-zhHQR0/H5SEBL3Wn6yYSaTTZej12z0hVZKOv3TwCUXT1z5qeqGcXJLLrbERYRScEDDpYIJhPC1fk31gqR783iQ=="
crossorigin="anonymous" defer>
</script>
<script src="webgl-demo.js" defer></script>
</head>
<body>
<canvas id="glcanvas" width="960" height="720"></canvas>
<div id="parameters">
<h1>Parameters</h1>
</div>
<div id="fps">FPS</div>
<script id="vs" type="x-shader/x-vertex">
precision highp float;
attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
attribute vec2 aVertexUV;
uniform mat4 uNormalMatrix;
uniform mat4 uViewMatrix;
uniform mat4 uModelMatrix;
uniform mat4 uModelViewMatrix;
uniform mat4 uProjectionMatrix;
varying vec3 vNormal;
varying vec3 vFragPos;
varying vec2 vFragUV;
void main(void) {
vFragPos = vec3(uModelMatrix * vec4(aVertexPosition, 1.0));
vNormal = vec3(uNormalMatrix * vec4(aVertexNormal, 1.0));
vFragUV = aVertexUV;
gl_Position = uProjectionMatrix * uViewMatrix * vec4(vFragPos, 1.0);
}
</script>
<script id="fs" type="x-shader/x-fragment">
precision highp float;
// Fragment-Interpolated data
varying vec3 vNormal;
varying vec3 vFragPos;
varying vec2 vFragUV;
// Camera
uniform vec3 uViewPos;
// Light
uniform vec3 uLightPos;
uniform vec3 uLightColor;
// Material
uniform vec3 uObjectColor;
uniform vec3 uShininess;
uniform vec3 uAlbedo;
uniform float uMetalness;
uniform float uRoughness;
uniform float uAO;
uniform sampler2D uAlbedoSampler;
uniform sampler2D uMetalnessSampler;
uniform sampler2D uRoughnessSampler;
uniform sampler2D uNormalSampler;
uniform sampler2D uAOSampler;
uniform int available[5];
const float PI = 3.14159265359;
vec3 fresnelSchlick(float cosTheta, vec3 F0)
{
return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
}
float DistributionGGX(vec3 N, vec3 H, float roughness)
{
float a = roughness * roughness;
float a2 = a * a;
float NdotH = max(dot(N, H), 0.0);
float NdotH2 = NdotH*NdotH;
float num = a2;
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
denom = PI * denom * denom;
return num / denom;
}
float GeometrySchlickGGX(float NdotV, float roughness)
{
float r = (roughness + 1.0);
float k = (r*r) / 8.0;
float num = NdotV;
float denom = NdotV * (1.0 - k) + k;
return num / denom;
}
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
{
float NdotV = max(dot(N, V), 0.0);
float NdotL = max(dot(N, L), 0.0);
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
return ggx1 * ggx2;
}
void main(void) {
// highp vec4 texelColor = texture2D(uSampler, vTextureCoord);
vec3 albedo;
float metalness;
float roughness;
vec3 normals;
float ao;
vec2 cosUV;
float light_power = 20.0;
vec3 normal;
float strength_normals = 1.5;
if(vFragUV.x > 1.0){
cosUV.x = 1.0;
}
else{
cosUV.x = vFragUV.x;
}
if(vFragUV.y > 1.0){
cosUV.y = 1.0;
}
else{
cosUV.y = vFragUV.y;
}
if(vFragUV.x < 0.0){
cosUV.x = 0.0;
}
else{
cosUV.x = vFragUV.x;
}
if(vFragUV.y < 0.0){
cosUV.y = 0.0;
}
else{
cosUV.y = vFragUV.y;
}
if(available[0] == 0){
albedo = uAlbedo;
}
else{
albedo = texture2D(uAlbedoSampler, cosUV).xyz;
//albedo *= 10.0;
}
if(available[1] == 0){
metalness = uMetalness;
}
else{
metalness = (texture2D(uMetalnessSampler, cosUV)).y;
}
if(available[2] == 0){
roughness = uRoughness;
}
else{
roughness = (texture2D(uRoughnessSampler, cosUV)).x;
}
if(available[3] == 0){
normal = vNormal;
}
else{
normal = strength_normals * (texture2D(uNormalSampler, cosUV)).xyz + vNormal;
}
if(available[4] == 0){
ao = uAO;
}
else{
ao = (texture2D(uAOSampler, cosUV)).x;
ao /= 255.0;
}
vec3 N = normalize(normal);
vec3 V = normalize(uViewPos - vFragPos);
vec3 F0 = vec3(0.04);
F0 = mix(F0, albedo, metalness);
vec3 Lo = vec3(0.0); // Accumulateur
// Pour la seule lumière de la scène: la radiance
vec3 L = normalize(uLightPos - vFragPos);
vec3 H = normalize(V + L);
float distance = length(uLightPos - vFragPos);
float attenuation = 1.0 / (distance * distance);
vec3 radiance = light_power * uLightColor * attenuation;
// Eval de la BRDF
float NDF = DistributionGGX(N, H, roughness); // Distribution normale
float G = GeometrySmith(N, V, L, roughness); // Auto-occlusion
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0); // Angle Frsnel
vec3 kS = F;
vec3 kD = vec3(1.0) - kS;
kD *= 1.0 - metalness;
vec3 numerator = NDF * G * F;
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0);
vec3 specular = numerator / max(denominator, 0.001);
float NdotL = max(dot(N, L), 0.0);
Lo += (kD * albedo / PI + specular) * radiance * NdotL;
vec3 ambient = vec3(0.03) * albedo * ao;
vec3 color = ambient + Lo;
//color = color / (color + vec3(1.0));
color = pow(color, vec3(1.0/2.2));
//gl_FragColor = vec4(roughness, ao, albedo.x/255.0, 1.0);
//gl_FragColor = vec4(metalness, metalness, metalness, 1.0);
gl_FragColor = vec4(color, 1.0);
}
</script>
</body>
</html>