webgl_materials_envmaps_parallax.html 13 KB

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