webgl_materials_envmaps_parallax.html 12 KB

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