forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl_materials_envmaps_parallax.html
492 lines (327 loc) · 12.8 KB
/
webgl_materials_envmaps_parallax.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - box projected cubemap environment mapping</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 {
color: #888888;
font-family:Monospace;
font-size:13px;
background-color: #000;
margin: 0px;
overflow: hidden;
}
#info {
position: absolute;
top: 0px;
width: 500px;
left: calc(50% - 250px);
text-align: center;
}
a {
color: #00f;
}
</style>
</head>
<body>
<div id="container"></div>
<div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - box projected cubemap environment mapping. <br> created by <a href="https://codercat.tk" target="_blank" rel="noopener">codercat</a>
</div>
<script src="../build/three.js"></script>
<script src="js/objects/Reflector.js"></script>
<script src="js/controls/OrbitControls.js"></script>
<script src="js/libs/dat.gui.min.js"></script>
<script src="js/lights/RectAreaLightUniformsLib.js"></script>
<script>
// shader injection for box projected cube environment mapping
var worldposReplace = `
#define BOX_PROJECTED_ENV_MAP
#if defined( USE_ENVMAP ) || defined( DISTANCE ) || defined ( USE_SHADOWMAP )
vec4 worldPosition = modelMatrix * vec4( transformed, 1.0 );
#ifdef BOX_PROJECTED_ENV_MAP
vWorldPosition = worldPosition.xyz;
#endif
#endif
`;
var envmapParsReplace = `
#define BOX_PROJECTED_ENV_MAP
#if defined( USE_ENVMAP ) || defined( PHYSICAL )
uniform float reflectivity;
uniform float envMapIntensity;
#endif
#ifdef USE_ENVMAP
#ifdef BOX_PROJECTED_ENV_MAP
uniform vec3 cubeMapSize;
uniform vec3 cubeMapPos;
varying vec3 vWorldPosition;
vec3 parallaxCorrectNormal( vec3 v, vec3 cubeSize, vec3 cubePos ) {
vec3 nDir = normalize( v );
vec3 rbmax = ( .5 * ( cubeSize - cubePos ) - vWorldPosition ) / nDir;
vec3 rbmin = ( - .5 * ( cubeSize - cubePos ) - vWorldPosition ) / nDir;
vec3 rbminmax;
rbminmax.x = ( nDir.x > 0. ) ? rbmax.x : rbmin.x;
rbminmax.y = ( nDir.y > 0. ) ? rbmax.y : rbmin.y;
rbminmax.z = ( nDir.z > 0. ) ? rbmax.z : rbmin.z;
float correction = min( min( rbminmax.x, rbminmax.y ), rbminmax.z );
vec3 boxIntersection = vWorldPosition + nDir * correction;
return boxIntersection - cubePos;
}
#endif
#if ! defined( PHYSICAL ) && ( defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG ) )
varying vec3 vWorldPosition;
#endif
#ifdef ENVMAP_TYPE_CUBE
uniform samplerCube envMap;
#else
uniform sampler2D envMap;
#endif
uniform float flipEnvMap;
uniform int maxMipLevel;
#if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG ) || defined( PHYSICAL )
uniform float refractionRatio;
#else
varying vec3 vReflect;
#endif
#endif
`;
var envmapPhysicalParsReplace = `
#if defined( USE_ENVMAP ) && defined( PHYSICAL )
vec3 getLightProbeIndirectIrradiance( const in GeometricContext geometry, const in int maxMIPLevel ) {
vec3 worldNormal = inverseTransformDirection( geometry.normal, viewMatrix );
#ifdef ENVMAP_TYPE_CUBE
vec3 worldNormalFinal = worldNormal;
#ifdef BOX_PROJECTED_ENV_MAP
worldNormalFinal = parallaxCorrectNormal( worldNormal, cubeMapSize, cubeMapPos );
#endif
vec3 queryVec = vec3( flipEnvMap * worldNormalFinal.x, worldNormalFinal.yz );
#ifdef TEXTURE_LOD_EXT
vec4 envMapColor = textureCubeLodEXT( envMap, queryVec, float( maxMIPLevel ) );
#else
vec4 envMapColor = textureCube( envMap, queryVec, float( maxMIPLevel ) );
#endif
envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
#elif defined( ENVMAP_TYPE_CUBE_UV )
vec3 queryVec = vec3( flipEnvMap * worldNormal.x, worldNormal.yz );
vec4 envMapColor = textureCubeUV( envMap, queryVec, 1.0 );
#else
vec4 envMapColor = vec4( 0.0 );
#endif
return PI * envMapColor.rgb * envMapIntensity;
}
float getSpecularMIPLevel( const in float blinnShininessExponent, const in int maxMIPLevel ) {
float maxMIPLevelScalar = float( maxMIPLevel );
float desiredMIPLevel = maxMIPLevelScalar + 0.79248 - 0.5 * log2( pow2( blinnShininessExponent ) + 1.0 );
return clamp( desiredMIPLevel, 0.0, maxMIPLevelScalar );
}
vec3 getLightProbeIndirectRadiance( const in GeometricContext geometry, const in float blinnShininessExponent, const in int maxMIPLevel ) {
#ifdef ENVMAP_MODE_REFLECTION
vec3 reflectVec = reflect( -geometry.viewDir, geometry.normal );
#else
vec3 reflectVec = refract( -geometry.viewDir, geometry.normal, refractionRatio );
#endif
reflectVec = inverseTransformDirection( reflectVec, viewMatrix );
float specularMIPLevel = getSpecularMIPLevel( blinnShininessExponent, maxMIPLevel );
#ifdef ENVMAP_TYPE_CUBE
vec3 reflectVecFinal = reflectVec;
#ifdef BOX_PROJECTED_ENV_MAP
reflectVecFinal = parallaxCorrectNormal( reflectVec, cubeMapSize, cubeMapPos );
#endif
vec3 queryReflectVec = vec3( flipEnvMap * reflectVecFinal.x, reflectVecFinal.yz );
#ifdef TEXTURE_LOD_EXT
vec4 envMapColor = textureCubeLodEXT( envMap, queryReflectVec, specularMIPLevel );
#else
vec4 envMapColor = textureCube( envMap, queryReflectVec, specularMIPLevel );
#endif
envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
#elif defined( ENVMAP_TYPE_CUBE_UV )
vec3 queryReflectVec = vec3( flipEnvMap * reflectVec.x, reflectVec.yz );
vec4 envMapColor = textureCubeUV( envMap, queryReflectVec, BlinnExponentToGGXRoughness(blinnShininessExponent ));
#elif defined( ENVMAP_TYPE_EQUIREC )
vec2 sampleUV;
sampleUV.y = asin( clamp( reflectVec.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;
sampleUV.x = atan( reflectVec.z, reflectVec.x ) * RECIPROCAL_PI2 + 0.5;
#ifdef TEXTURE_LOD_EXT
vec4 envMapColor = texture2DLodEXT( envMap, sampleUV, specularMIPLevel );
#else
vec4 envMapColor = texture2D( envMap, sampleUV, specularMIPLevel );
#endif
envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
#elif defined( ENVMAP_TYPE_SPHERE )
vec3 reflectView = normalize( ( viewMatrix * vec4( reflectVec, 0.0 ) ).xyz + vec3( 0.0,0.0,1.0 ) );
#ifdef TEXTURE_LOD_EXT
vec4 envMapColor = texture2DLodEXT( envMap, reflectView.xy * 0.5 + 0.5, specularMIPLevel );
#else
vec4 envMapColor = texture2D( envMap, reflectView.xy * 0.5 + 0.5, specularMIPLevel );
#endif
envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
#endif
return envMapColor.rgb * envMapIntensity;
}
#endif
`;
// scene size
var WIDTH = window.innerWidth;
var HEIGHT = window.innerHeight;
// camera
var VIEW_ANGLE = 45;
var ASPECT = WIDTH / HEIGHT;
var NEAR = 1;
var FAR = 800;
var camera, cubeCamera, scene, renderer;
var cameraControls;
var groundPlane, wallMat;
init();
function init() {
var container = document.getElementById( 'container' );
// renderer
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( WIDTH, HEIGHT );
container.appendChild( renderer.domElement );
// gui controls
var gui = new dat.GUI();
var params = {
'box projected': true
};
var bpcemGui = gui.add( params, 'box projected' );
bpcemGui.onChange( function ( value ) {
if ( value ) {
groundPlane.material = boxProjectedMat;
} else {
groundPlane.material = defaultMat;
}
render();
} );
// scene
scene = new THREE.Scene();
// camera
camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR );
camera.position.set( 280, 106, - 5 );
cameraControls = new THREE.OrbitControls( camera, renderer.domElement );
cameraControls.target.set( 0, - 10, 0 );
cameraControls.maxDistance = 400;
cameraControls.minDistance = 10;
cameraControls.addEventListener( 'change', render );
cameraControls.update();
// cube camera for environment map
cubeCamera = new THREE.CubeCamera( 1, 1000, 512 );
cubeCamera.renderTarget.texture.generateMipmaps = true;
cubeCamera.renderTarget.texture.minFilter = THREE.LinearMipMapLinearFilter;
cubeCamera.renderTarget.texture.mapping = THREE.CubeReflectionMapping;
cubeCamera.position.set( 0, - 100, 0 );
scene.add( cubeCamera );
// ground floor ( with box projected environment mapping )
var loader = new THREE.TextureLoader();
var rMap = loader.load( 'textures/lava/lavatile.jpg' );
rMap.wrapS = THREE.RepeatWrapping;
rMap.wrapT = THREE.RepeatWrapping;
rMap.repeat.set( 2, 1 );
var defaultMat = new THREE.MeshPhysicalMaterial( {
roughness: 1,
envMap: cubeCamera.renderTarget.texture,
roughnessMap: rMap
} );
var boxProjectedMat = new THREE.MeshPhysicalMaterial( {
color: new THREE.Color( '#ffffff' ),
roughness: 1,
envMap: cubeCamera.renderTarget.texture,
roughnessMap: rMap
} );
boxProjectedMat.onBeforeCompile = function ( shader ) {
shader.uniforms.cubeMapSize = { value: new THREE.Vector3( 200, 100, 100 ) };
shader.uniforms.cubeMapPos = { value: new THREE.Vector3( 0, - 50, 0 ) };
shader.uniforms.flipEnvMap.value = true;
//replace shader chunks with box projection chunks
shader.vertexShader = 'varying vec3 vWorldPosition;\n' + shader.vertexShader;
shader.vertexShader = shader.vertexShader.replace(
'#include <worldpos_vertex>',
worldposReplace
);
shader.fragmentShader = shader.fragmentShader.replace(
'#include <envmap_pars_fragment>',
envmapParsReplace
);
shader.fragmentShader = shader.fragmentShader.replace(
'#include <envmap_physical_pars_fragment>',
envmapPhysicalParsReplace
);
};
groundPlane = new THREE.Mesh( new THREE.PlaneBufferGeometry( 200, 100, 100 ), boxProjectedMat );
groundPlane.rotateX( - Math.PI / 2 );
groundPlane.position.set( 0, - 49, 0 );
scene.add( groundPlane );
// walls
var diffuseTex = loader.load( 'textures/brick_diffuse.jpg', function () {
updateCubeMap();
} );
var bumpTex = loader.load( 'textures/brick_bump.jpg', function () {
updateCubeMap();
} );
wallMat = new THREE.MeshPhysicalMaterial( {
map: diffuseTex,
bumpMap: bumpTex,
bumpScale: 0.3,
} );
var planeGeo = new THREE.PlaneBufferGeometry( 100, 100 );
var planeBack1 = new THREE.Mesh( planeGeo, wallMat );
planeBack1.position.z = - 50;
planeBack1.position.x = - 50;
scene.add( planeBack1 );
var planeBack2 = new THREE.Mesh( planeGeo, wallMat );
planeBack2.position.z = - 50;
planeBack2.position.x = 50;
scene.add( planeBack2 );
var planeFront1 = new THREE.Mesh( planeGeo, wallMat );
planeFront1.position.z = 50;
planeFront1.position.x = - 50;
planeFront1.rotateY( Math.PI );
scene.add( planeFront1 );
var planeFront2 = new THREE.Mesh( planeGeo, wallMat );
planeFront2.position.z = 50;
planeFront2.position.x = 50;
planeFront2.rotateY( Math.PI );
scene.add( planeFront2 );
var planeRight = new THREE.Mesh( planeGeo, wallMat );
planeRight.position.x = 100;
planeRight.rotateY( - Math.PI / 2 );
scene.add( planeRight );
var planeLeft = new THREE.Mesh( planeGeo, wallMat );
planeLeft.position.x = - 100;
planeLeft.rotateY( Math.PI / 2 );
scene.add( planeLeft );
//lights
var width = 50;
var height = 50;
var intensity = 10;
var blueRectLight = new THREE.RectAreaLight( 0xf3aaaa, intensity, width, height );
blueRectLight.position.set( 99, 5, 0 );
blueRectLight.lookAt( 0, 5, 0 );
scene.add( blueRectLight );
var blueRectLightHelper = new THREE.RectAreaLightHelper( blueRectLight, 0xffffff );
blueRectLight.add( blueRectLightHelper );
var redRectLight = new THREE.RectAreaLight( 0x9aaeff, intensity, width, height );
redRectLight.position.set( - 99, 5, 0 );
redRectLight.lookAt( 0, 5, 0 );
scene.add( redRectLight );
var redRectLightHelper = new THREE.RectAreaLightHelper( redRectLight, 0xffffff );
redRectLight.add( redRectLightHelper );
render();
}
function updateCubeMap() {
//disable specular highlights on walls in the environment map
wallMat.roughness = 1;
groundPlane.visible = false;
cubeCamera.position.copy( groundPlane.position );
cubeCamera.update( renderer, scene );
wallMat.roughness = 0.6;
groundPlane.visible = true;
render();
}
function render() {
renderer.render( scene, camera );
}
</script>
</body>
</html>