-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.html
312 lines (240 loc) · 8.49 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - performance</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
background:#fff;
padding:0;
margin:0;
font-weight: bold;
overflow:hidden;
text-align: center;
}
a { color: white }
#info { position: absolute; top: 10px; width: 100%; color: yellow;}
</style>
</head>
<body>
<div id="info">
<p>
Cube Map Texture by <a href="http://www.humus.name/index.php?page=Textures">Humus</a>
</p>
<p>
Check Out Source code on <a href="https://github.com/tiansijie/WebGLpbr">Sijie's Github</a>
</p>
</div>
<div id="container" style="width: 100%; height: 100%;"></div>
<script src="./threejs/build/three.js"></script>
<script src="./threejs/controls/OrbitControls.js"></script>
<script type="text/javascript" src="./dat.gui.min.js"></script>
<script src="./stats.min.js"></script>
<script id="vertexShader" type="x-shader/x-vertex">
precision mediump float;
precision mediump int;
varying vec4 vPosition;
varying vec3 vViewPosition;
varying vec4 vNormal;
varying vec3 vViewNormal;
//varying vec2 vUv;
void main() {
vViewPosition = (modelViewMatrix * vec4(position,1.0)).xyz;
vNormal = vec4(normal.xyz, 0.0);
vViewNormal = normalMatrix * normal.xyz;
vPosition = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
//vUv = uv;
gl_Position = vPosition;
}
</script>
<script id="fragmentShader_param" type="x-shader/x-fragment">
precision mediump float;
precision mediump int;
uniform vec3 u_lightColor;
uniform vec3 u_lightDir;
uniform vec3 u_lightPos;
uniform vec3 u_viewPos;
uniform vec3 u_diffuseColor;
uniform float u_roughness;
uniform float u_fresnel;
uniform float u_alpha;
uniform vec3 u_ambientColor;
uniform samplerCube u_tCube;
uniform float u_time;
varying vec4 vPosition;
varying vec3 vViewPosition;
varying vec4 vNormal;
varying vec3 vViewNormal;
varying vec2 vUv;
#define M_PI 3.1415926535897932384626433832795
float dotClamped(vec3 a, vec3 b) {
return max(dot(a,b), 0.0);
}
float F(float f0, vec3 l, vec3 h) {
float LoH = dot(l,h);
float powTerm = (-5.55473 * LoH - 6.98316) * LoH;
return f0 + (1.0 - f0) * pow(2.0, powTerm);
//return f0 + (1.0-f0) * pow(1.0-dot(l,h),5.0);
}
</script>
<script id="NDFBlinnPhong" type="x-shader/x-fragment">
float N(float a, vec3 n, vec3 h, float NoH) {
float a2 = a*a;
float powTerm = 2.0 / a2 - 2.0;
return (1.0 / (4.0 * a2)) * (pow(NoH, powTerm));
}
</script>
<script id="NDFBeckmann" type="x-shader/x-fragment">
float N(float a, vec3 n, vec3 h, float NoH) {
float a2 = a*a;
float NoH2 = pow(NoH, 2.0);
return (1.0 / (4.0 * a2 * pow(NoH, 4.0))) * exp((NoH2 - 1.0) / (a2 * NoH2));
}
</script>
<script id="NDFGGX" type="x-shader/x-fragment">
float N(float a, vec3 n, vec3 h, float NoH) {
float a2 = a*a;
return a2 / (4.0 * pow(pow(NoH, 2.0) * (a2 - 1.0) + 1.0, 2.0));
}
</script>
<script id="GImplicit" type="x-shader/x-fragment">
float G(float a, vec3 l, vec3 v, vec3 h, vec3 n, float NoL, float NoV) {
return 1.0;
}
</script>
<script id="GCookTorrance" type="x-shader/x-fragment">
float G(float a, vec3 l, vec3 v, vec3 h, vec3 n, float NoL, float NoV) {
float VdotH = max(dot(v,h), 0.0);
float NdotH = max(dot(n,h), 0.0);
float minV = 2.0 * NdotH * min(NoV, NoL) / VdotH;
return min(1.0, minV);
}
</script>
<script id="GKelemen" type="x-shader/x-fragment">
float G(float a, vec3 l, vec3 v, vec3 h, vec3 n, float NoL, float NoV) {
return (1.0) / pow(dot(v,h), 2.0);
}
</script>
<script id="GBeckmann" type="x-shader/x-fragment">
float GBeckmannHelper(float a, float NoT) {
return NoT / (a * sqrt(1.0 - pow(NoT, 2.0)));
}
float G(float a, vec3 l, vec3 v, vec3 h, vec3 n, float NoL, float NoV) {
float c1 = GBeckmannHelper(a, NoV);
float c2 = GBeckmannHelper(a, NoL);
float c12 = c1*c1;
float c22 = c2*c2;
float GV, GL;
if(c1 < 1.6) {
GV = (3.535 * c1 + 2.181 * c12) / (1.0 + 2.276*c1 + 2.577*c12);
}
else {
GV = 1.0;
}
if(c2 < 1.6) {
GL = (3.535 * c2 + 2.181 * c22) / (1.0 + 2.276*c2 + 2.577*c22);
}
else {
GL = 1.0;
}
return GL*GV;
}
</script>
<script id="GSchlick_Beckmann" type="x-shader/x-fragment">
float G(float a, vec3 l, vec3 v, vec3 h, vec3 n, float NoL, float NoV) {
float k = a * sqrt(2.0/M_PI);
float GV = 1.0 / (NoV * (1.0-k) + k);
float GL = 1.0 / (NoL * (1.0-k) + k);
return GV*GL;
}
</script>
<script id="fragmentShader_main" type="x-shader/x-fragment">
float random(vec3 scale, float seed) {
return fract(sin(dot(gl_FragCoord.xyz + seed, scale)) * 43758.5453 + seed);
}
vec2 RandomSamples(float seed) {
float u = random(vec3(12.9898, 78.233, 151.7182), seed);
float v = random(vec3(63.7264, 10.873, 623.6736), seed);
return vec2(u, v);
}
vec3 ImportanceSampleGGX( vec2 Xi, float Roughness, vec3 N ) {
float a = Roughness * Roughness;
float Phi = 2.0 * M_PI * Xi.x;
float CosTheta = sqrt( (1.0 - Xi.y) / ( 1.0 + (a*a - 1.0) * Xi.y ) );
float SinTheta = sqrt( 1.0 - CosTheta * CosTheta );
vec3 H;
H.x = SinTheta * cos( Phi );
H.y = SinTheta * sin( Phi );
H.z = CosTheta;
return H;
}
vec3 SpecularIBL( float Roughness, vec3 NL, vec3 V, float fresnel )
{
//L: viewLightDir
//H: halfVector
//V: viewNormal
//V: viewDir
vec3 SpecularLighting = vec3(0.0);
const int NumSamples = 32;
for( int i = 0; i < NumSamples; i++ )
{
vec2 Xi = RandomSamples( u_time + float(i) );
vec3 H = ImportanceSampleGGX( Xi, Roughness, NL );
vec3 L = 2.0 * dot( V, H ) * H - V;
float NoV = max( dot( NL, V ), 0.0 );
float NoL = max( dot( NL, L ), 0.0 );
float NoH = max( dot( NL, H ), 0.0 );
float VoH = max( dot( V, H ), 0.0 );
if( NoL > 0.0 )
{
vec3 SampleColor = textureCube (u_tCube, L).xyz;
float fresnel_fn = F(fresnel, L, H);
/*Put M_PI/4.0 in to NDF functions*/
float ndf_fn = N(Roughness, NL, H, NoH);
/*Put /(NoL*NoV) in G funtion*/
float g_fn = G(Roughness, L, V, H, NL, NoL, NoV);
SpecularLighting += fresnel_fn * ndf_fn * g_fn * SampleColor;
}
}
return SpecularLighting / float(NumSamples);
}
vec3 dirDiffuse = vec3(0.0);
vec3 dirSpecular = vec3(0.0);
void calDirLight(vec3 lDir, vec3 normal, vec3 diffuse, vec3 specular) {
vec3 dirLightColor = vec3(1.0);
vec4 lDirection = viewMatrix * vec4( lDir, 0.0 );
vec3 dirVector = normalize( lDirection.xyz );
float dirDiffuseWeight = max(dot( normal, dirVector ), 0.0);
dirDiffuse += diffuse * dirLightColor * dirDiffuseWeight * 0.5;
vec3 dirHalfVector = normalize( dirVector + vViewPosition );
float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );
float dirSpecularWeight = 0.5 * max( pow( dirDotNormalHalf, 0.0 ), 0.0 );
float specularNormalization = ( 0.0 + 2.0 ) / 8.0;
vec3 schlick = specular + vec3( 1.0 - specular ) * pow( max( 1.0 - dot( dirVector, dirHalfVector ), 0.0 ), 5.0 );
dirSpecular += schlick * dirLightColor * dirSpecularWeight * dirDiffuseWeight * specularNormalization;
}
void main() {
// viewMatrix
// cameraPosition
vec3 viewPosition = normalize(vViewPosition);
vec4 viewLightPos = viewMatrix * vec4( u_lightPos, 1.0 );
vec3 viewLightDir = viewLightPos.xyz - viewPosition.xyz;
viewLightDir = normalize(viewLightDir);
vec3 normal = normalize(vNormal.xyz);
vec3 viewNormal = normalize(vViewNormal.xyz);
vec3 viewDir = normalize(-vViewPosition);
vec3 halfVec = normalize(viewDir + viewLightDir);
float diffuse = max(dot(normalize(-u_lightDir), normal), 0.0);
float NoL= max(dot(viewNormal, viewLightDir), 0.0);
float fresnel = pow((1.0 - u_fresnel) / (1.0 + u_fresnel), 2.0);
float fresnel_fn = F(fresnel, viewLightDir, halfVec);
vec3 specularColor = SpecularIBL(u_alpha, viewNormal, viewDir, fresnel);
vec3 specColor = specularColor * NoL + dirSpecular;
vec3 diffuseColor = u_diffuseColor * diffuse * (1.0 - fresnel_fn) * u_lightColor + dirDiffuse;
gl_FragColor = vec4( diffuseColor + specColor + u_ambientColor * u_diffuseColor, 1.0);
}
</script>
<script type="text/javascript" src="./main.js"></script>
</body>
</html>