webgl_materials_envmaps_parallax.html 13 KB

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