webgl_materials_envmaps_parallax.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - box projected cubemap environment mapping</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. color: #888888;
  10. font-family:Monospace;
  11. font-size:13px;
  12. background-color: #000;
  13. margin: 0px;
  14. overflow: hidden;
  15. }
  16. #info {
  17. position: absolute;
  18. top: 0px;
  19. width: 500px;
  20. left: calc(50% - 250px);
  21. text-align: center;
  22. }
  23. a {
  24. color: #00f;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div id="container"></div>
  30. <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>
  31. </div>
  32. <script src="../build/three.js"></script>
  33. <script src="js/objects/Reflector.js"></script>
  34. <script src="js/controls/OrbitControls.js"></script>
  35. <script src="js/libs/dat.gui.min.js"></script>
  36. <script src="js/lights/RectAreaLightUniformsLib.js"></script>
  37. <script>
  38. // shader injection for box projected cube environment mapping
  39. var worldposReplace = `
  40. #define BOX_PROJECTED_ENV_MAP
  41. #if defined( USE_ENVMAP ) || defined( DISTANCE ) || defined ( USE_SHADOWMAP )
  42. vec4 worldPosition = modelMatrix * vec4( transformed, 1.0 );
  43. #ifdef BOX_PROJECTED_ENV_MAP
  44. vWorldPosition = worldPosition.xyz;
  45. #endif
  46. #endif
  47. `
  48. var envmapParsReplace = `
  49. #define BOX_PROJECTED_ENV_MAP
  50. #if defined( USE_ENVMAP ) || defined( PHYSICAL )
  51. uniform float reflectivity;
  52. uniform float envMapIntensity;
  53. #endif
  54. #ifdef USE_ENVMAP
  55. #ifdef BOX_PROJECTED_ENV_MAP
  56. uniform vec3 cubeMapSize;
  57. uniform vec3 cubeMapPos;
  58. varying vec3 vWorldPosition;
  59. vec3 parallaxCorrectNormal( vec3 v, vec3 cubeSize, vec3 cubePos ) {
  60. vec3 nDir = normalize( v );
  61. vec3 rbmax = ( .5 * ( cubeSize - cubePos ) - vWorldPosition ) / nDir;
  62. vec3 rbmin = ( - .5 * ( cubeSize - cubePos ) - vWorldPosition ) / nDir;
  63. vec3 rbminmax;
  64. rbminmax.x = ( nDir.x > 0. ) ? rbmax.x : rbmin.x;
  65. rbminmax.y = ( nDir.y > 0. ) ? rbmax.y : rbmin.y;
  66. rbminmax.z = ( nDir.z > 0. ) ? rbmax.z : rbmin.z;
  67. float correction = min( min( rbminmax.x, rbminmax.y ), rbminmax.z );
  68. vec3 boxIntersection = vWorldPosition + nDir * correction;
  69. return boxIntersection - cubePos;
  70. }
  71. #endif
  72. #if ! defined( PHYSICAL ) && ( defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG ) )
  73. varying vec3 vWorldPosition;
  74. #endif
  75. #ifdef ENVMAP_TYPE_CUBE
  76. uniform samplerCube envMap;
  77. #else
  78. uniform sampler2D envMap;
  79. #endif
  80. uniform float flipEnvMap;
  81. uniform int maxMipLevel;
  82. #if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG ) || defined( PHYSICAL )
  83. uniform float refractionRatio;
  84. #else
  85. varying vec3 vReflect;
  86. #endif
  87. #endif
  88. `;
  89. var envmapPhysicalParsReplace = `
  90. #if defined( USE_ENVMAP ) && defined( PHYSICAL )
  91. vec3 getLightProbeIndirectIrradiance( const in GeometricContext geometry, const in int maxMIPLevel ) {
  92. vec3 worldNormal = inverseTransformDirection( geometry.normal, viewMatrix );
  93. #ifdef ENVMAP_TYPE_CUBE
  94. vec3 worldNormalFinal = worldNormal;
  95. #ifdef BOX_PROJECTED_ENV_MAP
  96. worldNormalFinal = parallaxCorrectNormal( worldNormal, cubeMapSize, cubeMapPos );
  97. #endif
  98. vec3 queryVec = vec3( flipEnvMap * worldNormalFinal.x, worldNormalFinal.yz );
  99. #ifdef TEXTURE_LOD_EXT
  100. vec4 envMapColor = textureCubeLodEXT( envMap, queryVec, float( maxMIPLevel ) );
  101. #else
  102. vec4 envMapColor = textureCube( envMap, queryVec, float( maxMIPLevel ) );
  103. #endif
  104. envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
  105. #elif defined( ENVMAP_TYPE_CUBE_UV )
  106. vec3 queryVec = vec3( flipEnvMap * worldNormal.x, worldNormal.yz );
  107. vec4 envMapColor = textureCubeUV( envMap, queryVec, 1.0 );
  108. #else
  109. vec4 envMapColor = vec4( 0.0 );
  110. #endif
  111. return PI * envMapColor.rgb * envMapIntensity;
  112. }
  113. float getSpecularMIPLevel( const in float blinnShininessExponent, const in int maxMIPLevel ) {
  114. float maxMIPLevelScalar = float( maxMIPLevel );
  115. float desiredMIPLevel = maxMIPLevelScalar + 0.79248 - 0.5 * log2( pow2( blinnShininessExponent ) + 1.0 );
  116. return clamp( desiredMIPLevel, 0.0, maxMIPLevelScalar );
  117. }
  118. vec3 getLightProbeIndirectRadiance( const in GeometricContext geometry, const in float blinnShininessExponent, const in int maxMIPLevel ) {
  119. #ifdef ENVMAP_MODE_REFLECTION
  120. vec3 reflectVec = reflect( -geometry.viewDir, geometry.normal );
  121. #else
  122. vec3 reflectVec = refract( -geometry.viewDir, geometry.normal, refractionRatio );
  123. #endif
  124. reflectVec = inverseTransformDirection( reflectVec, viewMatrix );
  125. float specularMIPLevel = getSpecularMIPLevel( blinnShininessExponent, maxMIPLevel );
  126. #ifdef ENVMAP_TYPE_CUBE
  127. vec3 reflectVecFinal = reflectVec;
  128. #ifdef BOX_PROJECTED_ENV_MAP
  129. reflectVecFinal = parallaxCorrectNormal( reflectVec, cubeMapSize, cubeMapPos );
  130. #endif
  131. vec3 queryReflectVec = vec3( flipEnvMap * reflectVecFinal.x, reflectVecFinal.yz );
  132. #ifdef TEXTURE_LOD_EXT
  133. vec4 envMapColor = textureCubeLodEXT( envMap, queryReflectVec, specularMIPLevel );
  134. #else
  135. vec4 envMapColor = textureCube( envMap, queryReflectVec, specularMIPLevel );
  136. #endif
  137. envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
  138. #elif defined( ENVMAP_TYPE_CUBE_UV )
  139. vec3 queryReflectVec = vec3( flipEnvMap * reflectVec.x, reflectVec.yz );
  140. vec4 envMapColor = textureCubeUV( envMap, queryReflectVec, BlinnExponentToGGXRoughness(blinnShininessExponent ));
  141. #elif defined( ENVMAP_TYPE_EQUIREC )
  142. vec2 sampleUV;
  143. sampleUV.y = asin( clamp( reflectVec.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;
  144. sampleUV.x = atan( reflectVec.z, reflectVec.x ) * RECIPROCAL_PI2 + 0.5;
  145. #ifdef TEXTURE_LOD_EXT
  146. vec4 envMapColor = texture2DLodEXT( envMap, sampleUV, specularMIPLevel );
  147. #else
  148. vec4 envMapColor = texture2D( envMap, sampleUV, specularMIPLevel );
  149. #endif
  150. envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
  151. #elif defined( ENVMAP_TYPE_SPHERE )
  152. vec3 reflectView = normalize( ( viewMatrix * vec4( reflectVec, 0.0 ) ).xyz + vec3( 0.0,0.0,1.0 ) );
  153. #ifdef TEXTURE_LOD_EXT
  154. vec4 envMapColor = texture2DLodEXT( envMap, reflectView.xy * 0.5 + 0.5, specularMIPLevel );
  155. #else
  156. vec4 envMapColor = texture2D( envMap, reflectView.xy * 0.5 + 0.5, specularMIPLevel );
  157. #endif
  158. envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
  159. #endif
  160. return envMapColor.rgb * envMapIntensity;
  161. }
  162. #endif
  163. `;
  164. // scene size
  165. var WIDTH = window.innerWidth;
  166. var HEIGHT = window.innerHeight;
  167. // camera
  168. var VIEW_ANGLE = 45;
  169. var ASPECT = WIDTH / HEIGHT;
  170. var NEAR = 1;
  171. var FAR = 800;
  172. var camera, scene, renderer;
  173. var cameraControls;
  174. var groundPlane, smallSphere, wallMat;
  175. init();
  176. function init() {
  177. var container = document.getElementById( 'container' );
  178. // renderer
  179. renderer = new THREE.WebGLRenderer( { antialias: true } );
  180. renderer.setPixelRatio( window.devicePixelRatio );
  181. renderer.setSize( WIDTH, HEIGHT );
  182. container.appendChild( renderer.domElement );
  183. // gui controls
  184. var gui = new dat.GUI();
  185. var params = {
  186. 'box projected': true
  187. };
  188. var bpcemGui = gui.add( params, 'box projected' );
  189. bpcemGui.onChange( function( value ) {
  190. if ( value ){
  191. groundPlane.material = boxProjectedMat;
  192. } else {
  193. groundPlane.material = defaultMat;
  194. }
  195. render();
  196. });
  197. // scene
  198. scene = new THREE.Scene();
  199. // camera
  200. camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR );
  201. camera.position.set( 280, 106, -5 );
  202. cameraControls = new THREE.OrbitControls( camera, renderer.domElement );
  203. cameraControls.target.set( 0, -10, 0 );
  204. cameraControls.maxDistance = 400;
  205. cameraControls.minDistance = 10;
  206. cameraControls.addEventListener( 'change', render );
  207. cameraControls.update();
  208. // cube camera for environment map
  209. cubeCamera = new THREE.CubeCamera( 1, 1000, 512 );
  210. cubeCamera.renderTarget.texture.generateMipmaps = true;
  211. cubeCamera.renderTarget.texture.minFilter = THREE.LinearMipMapLinearFilter;
  212. cubeCamera.renderTarget.texture.mapping = THREE.CubeReflectionMapping;
  213. cubeCamera.position.set( 0, -100, 0 );
  214. scene.add( cubeCamera );
  215. // ground floor ( with box projected environment mapping )
  216. var loader = new THREE.TextureLoader();
  217. var rMap = loader.load( 'textures/lava/lavatile.jpg' );
  218. rMap.wrapS = THREE.RepeatWrapping;
  219. rMap.wrapT = THREE.RepeatWrapping;
  220. rMap.repeat.set(2,1);
  221. var defaultMat = new THREE.MeshPhysicalMaterial({
  222. roughness: 1,
  223. envMap: cubeCamera.renderTarget.texture,
  224. roughnessMap: rMap,
  225. });
  226. var boxProjectedMat = new THREE.MeshPhysicalMaterial({
  227. color: new THREE.Color('#ffffff'),
  228. roughness: 1,
  229. envMap: cubeCamera.renderTarget.texture,
  230. roughnessMap: rMap,
  231. });
  232. boxProjectedMat.onBeforeCompile = function ( shader ) {
  233. shader.uniforms.cubeMapSize = { value: new THREE.Vector3( 200, 100, 100 ) };
  234. shader.uniforms.cubeMapPos = { value: new THREE.Vector3( 0, -50, 0 ) };
  235. shader.uniforms.flipEnvMap.value = true;
  236. //replace shader chunks with box projection chunks
  237. shader.vertexShader = 'varying vec3 vWorldPosition;\n' + shader.vertexShader;
  238. shader.vertexShader = shader.vertexShader.replace(
  239. '#include <worldpos_vertex>',
  240. worldposReplace
  241. );
  242. shader.fragmentShader = shader.fragmentShader.replace(
  243. '#include <envmap_pars_fragment>',
  244. envmapParsReplace
  245. );
  246. shader.fragmentShader = shader.fragmentShader.replace(
  247. '#include <envmap_physical_pars_fragment>',
  248. envmapPhysicalParsReplace
  249. );
  250. materialShader = shader;
  251. };
  252. groundPlane = new THREE.Mesh( new THREE.PlaneBufferGeometry( 200, 100, 100 ), boxProjectedMat );
  253. groundPlane.rotateX( - Math.PI / 2 );
  254. groundPlane.position.set( 0, - 49, 0 );
  255. scene.add( groundPlane );
  256. // walls
  257. var diffuseTex = loader.load( 'textures/brick_diffuse.jpg', () => {
  258. updateCubeMap();
  259. } );
  260. var bumpTex = loader.load( 'textures/brick_bump.jpg', () => {
  261. updateCubeMap();
  262. } );
  263. wallMat = new THREE.MeshPhysicalMaterial({
  264. map: diffuseTex,
  265. bumpMap: bumpTex,
  266. bumpScale: 0.3,
  267. });
  268. var planeGeo = new THREE.PlaneBufferGeometry( 100, 100 );
  269. var planeBack1 = new THREE.Mesh( planeGeo, wallMat );
  270. planeBack1.position.z = - 50;
  271. planeBack1.position.x = - 50;
  272. scene.add( planeBack1 );
  273. var planeBack2 = new THREE.Mesh( planeGeo, wallMat );
  274. planeBack2.position.z = - 50;
  275. planeBack2.position.x = 50;
  276. scene.add( planeBack2 );
  277. var planeFront1 = new THREE.Mesh( planeGeo, wallMat );
  278. planeFront1.position.z = 50;
  279. planeFront1.position.x = - 50;
  280. planeFront1.rotateY( Math.PI );
  281. scene.add( planeFront1 );
  282. var planeFront2 = new THREE.Mesh( planeGeo, wallMat );
  283. planeFront2.position.z = 50;
  284. planeFront2.position.x = 50;
  285. planeFront2.rotateY( Math.PI );
  286. scene.add( planeFront2 );
  287. var planeRight = new THREE.Mesh( planeGeo, wallMat );
  288. planeRight.position.x = 100;
  289. planeRight.rotateY( - Math.PI / 2 );
  290. scene.add( planeRight );
  291. var planeLeft = new THREE.Mesh( planeGeo, wallMat );
  292. planeLeft.position.x = - 100;
  293. planeLeft.rotateY( Math.PI / 2 );
  294. scene.add( planeLeft );
  295. //lights
  296. var width = 50;
  297. var height = 50;
  298. var intensity = 10;
  299. var blueRectLight = new THREE.RectAreaLight( 0xf3aaaa, intensity, width, height );
  300. blueRectLight.position.set( 99, 5, 0 );
  301. blueRectLight.lookAt( 0, 5, 0 );
  302. scene.add( blueRectLight );
  303. blueRectLightHelper = new THREE.RectAreaLightHelper( blueRectLight, 0xffffff );
  304. blueRectLight.add( blueRectLightHelper );
  305. var redRectLight = new THREE.RectAreaLight( 0x9aaeff, intensity, width, height );
  306. redRectLight.position.set( - 99, 5, 0 );
  307. redRectLight.lookAt( 0, 5, 0 );
  308. scene.add( redRectLight );
  309. redRectLightHelper = new THREE.RectAreaLightHelper( redRectLight, 0xffffff );
  310. redRectLight.add( redRectLightHelper );
  311. render();
  312. }
  313. function updateCubeMap () {
  314. //disable specular highlights on walls in the environment map
  315. wallMat.roughness = 1;
  316. groundPlane.visible = false;
  317. cubeCamera.position.copy( groundPlane.position );
  318. cubeCamera.update( renderer, scene );
  319. wallMat.roughness = 0.6;
  320. groundPlane.visible = true;
  321. render();
  322. }
  323. function render() {
  324. renderer.render( scene, camera );
  325. }
  326. </script>
  327. </body>
  328. </html>