webgpu_materials_envmaps_bpcem.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - materials - bpcem</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="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgpu - box projected cube environment mapping (BPCEM)<br/>
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.webgpu.js",
  17. "three/webgpu": "../build/three.webgpu.js",
  18. "three/tsl": "../build/three.tsl.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import { bumpMap, float, getParallaxCorrectNormal, pmremTexture, reflectVector, texture, uniform, vec3 } from 'three/tsl';
  26. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. import { RectAreaLightHelper } from 'three/addons/helpers/RectAreaLightHelper.js';
  29. import { RectAreaLightTexturesLib } from 'three/addons/lights/RectAreaLightTexturesLib.js';
  30. let camera, scene, renderer;
  31. let controls, cubeCamera;
  32. let groundPlane, wallMat;
  33. init();
  34. function init() {
  35. THREE.RectAreaLightNode.setLTC( RectAreaLightTexturesLib.init() );
  36. // scene
  37. scene = new THREE.Scene();
  38. // camera
  39. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 1000 );
  40. camera.position.set( 0, 200, - 200 );
  41. // cube camera for environment map
  42. const renderTarget = new THREE.WebGLCubeRenderTarget( 512 );
  43. renderTarget.texture.type = THREE.HalfFloatType;
  44. renderTarget.texture.minFilter = THREE.LinearMipmapLinearFilter;
  45. renderTarget.texture.magFilter = THREE.LinearFilter;
  46. renderTarget.texture.generateMipmaps = true;
  47. renderTarget.texture.mapping = THREE.CubeReflectionMapping;
  48. cubeCamera = new THREE.CubeCamera( 1, 1000, renderTarget );
  49. cubeCamera.position.set( 0, - 100, 0 );
  50. // ground floor ( with box projected environment mapping )
  51. const loader = new THREE.TextureLoader();
  52. const rMap = loader.load( 'textures/lava/lavatile.jpg' );
  53. rMap.wrapS = THREE.RepeatWrapping;
  54. rMap.wrapT = THREE.RepeatWrapping;
  55. rMap.repeat.set( 2, 1 );
  56. const roughnessUniform = uniform( 0.25 );
  57. const defaultMat = new THREE.MeshStandardNodeMaterial();
  58. defaultMat.envNode = pmremTexture( renderTarget.texture );
  59. defaultMat.roughnessNode = texture( rMap ).mul( roughnessUniform );
  60. defaultMat.metalnessNode = float( 1 );
  61. const boxProjectedMat = new THREE.MeshStandardNodeMaterial();
  62. boxProjectedMat.envNode = pmremTexture( renderTarget.texture, getParallaxCorrectNormal( reflectVector, vec3( 200, 100, 100 ), vec3( 0, - 50, 0 ) ) );
  63. boxProjectedMat.roughnessNode = texture( rMap ).mul( roughnessUniform );
  64. boxProjectedMat.metalnessNode = float( 1 );
  65. groundPlane = new THREE.Mesh( new THREE.PlaneGeometry( 200, 100, 100 ), boxProjectedMat );
  66. groundPlane.rotateX( - Math.PI / 2 );
  67. groundPlane.position.set( 0, - 49, 0 );
  68. scene.add( groundPlane );
  69. // walls
  70. const diffuseTex = loader.load( 'textures/brick_diffuse.jpg' );
  71. diffuseTex.colorSpace = THREE.SRGBColorSpace;
  72. const bumpTex = loader.load( 'textures/brick_bump.jpg' );
  73. wallMat = new THREE.MeshStandardNodeMaterial();
  74. wallMat.colorNode = texture( diffuseTex );
  75. wallMat.normalNode = bumpMap( texture( bumpTex ), float( 5 ) );
  76. const planeGeo = new THREE.PlaneGeometry( 100, 100 );
  77. const planeBack1 = new THREE.Mesh( planeGeo, wallMat );
  78. planeBack1.position.z = - 50;
  79. planeBack1.position.x = - 50;
  80. scene.add( planeBack1 );
  81. const planeBack2 = new THREE.Mesh( planeGeo, wallMat );
  82. planeBack2.position.z = - 50;
  83. planeBack2.position.x = 50;
  84. scene.add( planeBack2 );
  85. const planeFront1 = new THREE.Mesh( planeGeo, wallMat );
  86. planeFront1.position.z = 50;
  87. planeFront1.position.x = - 50;
  88. planeFront1.rotateY( Math.PI );
  89. scene.add( planeFront1 );
  90. const planeFront2 = new THREE.Mesh( planeGeo, wallMat );
  91. planeFront2.position.z = 50;
  92. planeFront2.position.x = 50;
  93. planeFront2.rotateY( Math.PI );
  94. scene.add( planeFront2 );
  95. const planeRight = new THREE.Mesh( planeGeo, wallMat );
  96. planeRight.position.x = 100;
  97. planeRight.rotateY( - Math.PI / 2 );
  98. scene.add( planeRight );
  99. const planeLeft = new THREE.Mesh( planeGeo, wallMat );
  100. planeLeft.position.x = - 100;
  101. planeLeft.rotateY( Math.PI / 2 );
  102. scene.add( planeLeft );
  103. // area lights
  104. const width = 50;
  105. const height = 50;
  106. const intensity = 5;
  107. const blueRectLight = new THREE.RectAreaLight( 0x9aaeff, intensity, width, height );
  108. blueRectLight.position.set( - 99, 5, 0 );
  109. blueRectLight.lookAt( 0, 5, 0 );
  110. scene.add( blueRectLight );
  111. const blueRectLightHelper = new RectAreaLightHelper( blueRectLight, 0xffffff );
  112. blueRectLight.add( blueRectLightHelper );
  113. const redRectLight = new THREE.RectAreaLight( 0xf3aaaa, intensity, width, height );
  114. redRectLight.position.set( 99, 5, 0 );
  115. redRectLight.lookAt( 0, 5, 0 );
  116. scene.add( redRectLight );
  117. const redRectLightHelper = new RectAreaLightHelper( redRectLight, 0xffffff );
  118. redRectLight.add( redRectLightHelper );
  119. // renderer
  120. renderer = new THREE.WebGPURenderer( { antialias: true } );
  121. renderer.setPixelRatio( window.devicePixelRatio );
  122. renderer.setSize( window.innerWidth, window.innerHeight );
  123. renderer.setAnimationLoop( animate );
  124. document.body.appendChild( renderer.domElement );
  125. window.addEventListener( 'resize', onWindowResize );
  126. // controls
  127. controls = new OrbitControls( camera, renderer.domElement );
  128. controls.target.set( 0, - 10, 0 );
  129. controls.maxDistance = 400;
  130. controls.minDistance = 10;
  131. controls.update();
  132. // gui
  133. const gui = new GUI();
  134. const params = {
  135. 'box projected': true
  136. };
  137. gui.add( params, 'box projected' ).onChange( ( value ) => {
  138. groundPlane.material = ( value ) ? boxProjectedMat : defaultMat;
  139. } );
  140. gui.add( roughnessUniform, 'value', 0, 1 ).name( 'roughness' );
  141. }
  142. function onWindowResize() {
  143. camera.aspect = window.innerWidth / window.innerHeight;
  144. camera.updateProjectionMatrix();
  145. renderer.setSize( window.innerWidth, window.innerHeight );
  146. }
  147. function updateCubeMap() {
  148. groundPlane.visible = false;
  149. cubeCamera.position.copy( groundPlane.position );
  150. cubeCamera.update( renderer, scene );
  151. groundPlane.visible = true;
  152. }
  153. function animate() {
  154. updateCubeMap();
  155. renderer.render( scene, camera );
  156. }
  157. </script>
  158. </body>
  159. </html>
粤ICP备19079148号