webgpu_materials_envmaps_bpcem.html 7.0 KB

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