webgpu_materials_envmaps_bpcem.html 7.3 KB

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